]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
PR c++/88312 - pack expansion of decltype.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int check_non_deducible_conversion (tree, tree, int, int,
159 struct conversion **, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
161 tree);
162 static int type_unification_real (tree, tree, tree, const tree *,
163 unsigned int, int, unification_kind_t,
164 vec<deferred_access_check, va_gc> **,
165 bool);
166 static void note_template_header (int);
167 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
168 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
169 static tree convert_template_argument (tree, tree, tree,
170 tsubst_flags_t, int, tree);
171 static tree for_each_template_parm (tree, tree_fn_t, void*,
172 hash_set<tree> *, bool, tree_fn_t = NULL);
173 static tree expand_template_argument_pack (tree);
174 static tree build_template_parm_index (int, int, int, tree, tree);
175 static bool inline_needs_template_parms (tree, bool);
176 static void push_inline_template_parms_recursive (tree, int);
177 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
178 static int mark_template_parm (tree, void *);
179 static int template_parm_this_level_p (tree, void *);
180 static tree tsubst_friend_function (tree, tree);
181 static tree tsubst_friend_class (tree, tree);
182 static int can_complete_type_without_circularity (tree);
183 static tree get_bindings (tree, tree, tree, bool);
184 static int template_decl_level (tree);
185 static int check_cv_quals_for_unify (int, tree, tree);
186 static void template_parm_level_and_index (tree, int*, int*);
187 static int unify_pack_expansion (tree, tree, tree,
188 tree, unification_kind_t, bool, bool);
189 static tree copy_template_args (tree);
190 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
204 tree, tree);
205 static bool template_template_parm_bindings_ok_p (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
225 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
226 static tree make_argument_pack (tree);
227 static void register_parameter_specializations (tree, tree);
228 static tree enclosing_instantiation_of (tree tctx);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
234
235 static void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_CLASS_SCOPE_P (t))
244 push_nested_class (DECL_CONTEXT (t));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 saved_access_scope = tree_cons
251 (NULL_TREE, current_function_decl, saved_access_scope);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 static void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 {
264 current_function_decl = TREE_VALUE (saved_access_scope);
265 saved_access_scope = TREE_CHAIN (saved_access_scope);
266 }
267
268 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error ("data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error ("invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 type = CP_DECL_CONTEXT (type);
394 else if (LAMBDA_TYPE_P (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Subroutine of maybe_begin_member_template_processing.
404 Returns true if processing DECL needs us to push template parms. */
405
406 static bool
407 inline_needs_template_parms (tree decl, bool nsdmi)
408 {
409 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
410 return false;
411
412 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
413 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
414 }
415
416 /* Subroutine of maybe_begin_member_template_processing.
417 Push the template parms in PARMS, starting from LEVELS steps into the
418 chain, and ending at the beginning, since template parms are listed
419 innermost first. */
420
421 static void
422 push_inline_template_parms_recursive (tree parmlist, int levels)
423 {
424 tree parms = TREE_VALUE (parmlist);
425 int i;
426
427 if (levels > 1)
428 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
429
430 ++processing_template_decl;
431 current_template_parms
432 = tree_cons (size_int (processing_template_decl),
433 parms, current_template_parms);
434 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
435
436 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
437 NULL);
438 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
439 {
440 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
441
442 if (error_operand_p (parm))
443 continue;
444
445 gcc_assert (DECL_P (parm));
446
447 switch (TREE_CODE (parm))
448 {
449 case TYPE_DECL:
450 case TEMPLATE_DECL:
451 pushdecl (parm);
452 break;
453
454 case PARM_DECL:
455 /* Push the CONST_DECL. */
456 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
457 break;
458
459 default:
460 gcc_unreachable ();
461 }
462 }
463 }
464
465 /* Restore the template parameter context for a member template, a
466 friend template defined in a class definition, or a non-template
467 member of template class. */
468
469 void
470 maybe_begin_member_template_processing (tree decl)
471 {
472 tree parms;
473 int levels = 0;
474 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
475
476 if (nsdmi)
477 {
478 tree ctx = DECL_CONTEXT (decl);
479 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
480 /* Disregard full specializations (c++/60999). */
481 && uses_template_parms (ctx)
482 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
483 }
484
485 if (inline_needs_template_parms (decl, nsdmi))
486 {
487 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
488 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
489
490 if (DECL_TEMPLATE_SPECIALIZATION (decl))
491 {
492 --levels;
493 parms = TREE_CHAIN (parms);
494 }
495
496 push_inline_template_parms_recursive (parms, levels);
497 }
498
499 /* Remember how many levels of template parameters we pushed so that
500 we can pop them later. */
501 inline_parm_levels.safe_push (levels);
502 }
503
504 /* Undo the effects of maybe_begin_member_template_processing. */
505
506 void
507 maybe_end_member_template_processing (void)
508 {
509 int i;
510 int last;
511
512 if (inline_parm_levels.length () == 0)
513 return;
514
515 last = inline_parm_levels.pop ();
516 for (i = 0; i < last; ++i)
517 {
518 --processing_template_decl;
519 current_template_parms = TREE_CHAIN (current_template_parms);
520 poplevel (0, 0, 0);
521 }
522 }
523
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. */
526
527 static tree
528 add_to_template_args (tree args, tree extra_args)
529 {
530 tree new_args;
531 int extra_depth;
532 int i;
533 int j;
534
535 if (args == NULL_TREE || extra_args == error_mark_node)
536 return extra_args;
537
538 extra_depth = TMPL_ARGS_DEPTH (extra_args);
539 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
540
541 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
543
544 for (j = 1; j <= extra_depth; ++j, ++i)
545 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
546
547 return new_args;
548 }
549
550 /* Like add_to_template_args, but only the outermost ARGS are added to
551 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
552 (EXTRA_ARGS) levels are added. This function is used to combine
553 the template arguments from a partial instantiation with the
554 template arguments used to attain the full instantiation from the
555 partial instantiation. */
556
557 static tree
558 add_outermost_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561
562 /* If there are more levels of EXTRA_ARGS than there are ARGS,
563 something very fishy is going on. */
564 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
565
566 /* If *all* the new arguments will be the EXTRA_ARGS, just return
567 them. */
568 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
569 return extra_args;
570
571 /* For the moment, we make ARGS look like it contains fewer levels. */
572 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
573
574 new_args = add_to_template_args (args, extra_args);
575
576 /* Now, we restore ARGS to its full dimensions. */
577 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
578
579 return new_args;
580 }
581
582 /* Return the N levels of innermost template arguments from the ARGS. */
583
584 tree
585 get_innermost_template_args (tree args, int n)
586 {
587 tree new_args;
588 int extra_levels;
589 int i;
590
591 gcc_assert (n >= 0);
592
593 /* If N is 1, just return the innermost set of template arguments. */
594 if (n == 1)
595 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
596
597 /* If we're not removing anything, just return the arguments we were
598 given. */
599 extra_levels = TMPL_ARGS_DEPTH (args) - n;
600 gcc_assert (extra_levels >= 0);
601 if (extra_levels == 0)
602 return args;
603
604 /* Make a new set of arguments, not containing the outer arguments. */
605 new_args = make_tree_vec (n);
606 for (i = 1; i <= n; ++i)
607 SET_TMPL_ARGS_LEVEL (new_args, i,
608 TMPL_ARGS_LEVEL (args, i + extra_levels));
609
610 return new_args;
611 }
612
613 /* The inverse of get_innermost_template_args: Return all but the innermost
614 EXTRA_LEVELS levels of template arguments from the ARGS. */
615
616 static tree
617 strip_innermost_template_args (tree args, int extra_levels)
618 {
619 tree new_args;
620 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
621 int i;
622
623 gcc_assert (n >= 0);
624
625 /* If N is 1, just return the outermost set of template arguments. */
626 if (n == 1)
627 return TMPL_ARGS_LEVEL (args, 1);
628
629 /* If we're not removing anything, just return the arguments we were
630 given. */
631 gcc_assert (extra_levels >= 0);
632 if (extra_levels == 0)
633 return args;
634
635 /* Make a new set of arguments, not containing the inner arguments. */
636 new_args = make_tree_vec (n);
637 for (i = 1; i <= n; ++i)
638 SET_TMPL_ARGS_LEVEL (new_args, i,
639 TMPL_ARGS_LEVEL (args, i));
640
641 return new_args;
642 }
643
644 /* We've got a template header coming up; push to a new level for storing
645 the parms. */
646
647 void
648 begin_template_parm_list (void)
649 {
650 /* We use a non-tag-transparent scope here, which causes pushtag to
651 put tags in this scope, rather than in the enclosing class or
652 namespace scope. This is the right thing, since we want
653 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
654 global template class, push_template_decl handles putting the
655 TEMPLATE_DECL into top-level scope. For a nested template class,
656 e.g.:
657
658 template <class T> struct S1 {
659 template <class T> struct S2 {};
660 };
661
662 pushtag contains special code to insert the TEMPLATE_DECL for S2
663 at the right scope. */
664 begin_scope (sk_template_parms, NULL);
665 ++processing_template_decl;
666 ++processing_template_parmlist;
667 note_template_header (0);
668
669 /* Add a dummy parameter level while we process the parameter list. */
670 current_template_parms
671 = tree_cons (size_int (processing_template_decl),
672 make_tree_vec (0),
673 current_template_parms);
674 }
675
676 /* This routine is called when a specialization is declared. If it is
677 invalid to declare a specialization here, an error is reported and
678 false is returned, otherwise this routine will return true. */
679
680 static bool
681 check_specialization_scope (void)
682 {
683 tree scope = current_scope ();
684
685 /* [temp.expl.spec]
686
687 An explicit specialization shall be declared in the namespace of
688 which the template is a member, or, for member templates, in the
689 namespace of which the enclosing class or enclosing class
690 template is a member. An explicit specialization of a member
691 function, member class or static data member of a class template
692 shall be declared in the namespace of which the class template
693 is a member. */
694 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
695 {
696 error ("explicit specialization in non-namespace scope %qD", scope);
697 return false;
698 }
699
700 /* [temp.expl.spec]
701
702 In an explicit specialization declaration for a member of a class
703 template or a member template that appears in namespace scope,
704 the member template and some of its enclosing class templates may
705 remain unspecialized, except that the declaration shall not
706 explicitly specialize a class member template if its enclosing
707 class templates are not explicitly specialized as well. */
708 if (current_template_parms)
709 {
710 error ("enclosing class templates are not explicitly specialized");
711 return false;
712 }
713
714 return true;
715 }
716
717 /* We've just seen template <>. */
718
719 bool
720 begin_specialization (void)
721 {
722 begin_scope (sk_template_spec, NULL);
723 note_template_header (1);
724 return check_specialization_scope ();
725 }
726
727 /* Called at then end of processing a declaration preceded by
728 template<>. */
729
730 void
731 end_specialization (void)
732 {
733 finish_scope ();
734 reset_specialization ();
735 }
736
737 /* Any template <>'s that we have seen thus far are not referring to a
738 function specialization. */
739
740 void
741 reset_specialization (void)
742 {
743 processing_specialization = 0;
744 template_header_count = 0;
745 }
746
747 /* We've just seen a template header. If SPECIALIZATION is nonzero,
748 it was of the form template <>. */
749
750 static void
751 note_template_header (int specialization)
752 {
753 processing_specialization = specialization;
754 template_header_count++;
755 }
756
757 /* We're beginning an explicit instantiation. */
758
759 void
760 begin_explicit_instantiation (void)
761 {
762 gcc_assert (!processing_explicit_instantiation);
763 processing_explicit_instantiation = true;
764 }
765
766
767 void
768 end_explicit_instantiation (void)
769 {
770 gcc_assert (processing_explicit_instantiation);
771 processing_explicit_instantiation = false;
772 }
773
774 /* An explicit specialization or partial specialization of TMPL is being
775 declared. Check that the namespace in which the specialization is
776 occurring is permissible. Returns false iff it is invalid to
777 specialize TMPL in the current namespace. */
778
779 static bool
780 check_specialization_namespace (tree tmpl)
781 {
782 tree tpl_ns = decl_namespace_context (tmpl);
783
784 /* [tmpl.expl.spec]
785
786 An explicit specialization shall be declared in a namespace enclosing the
787 specialized template. An explicit specialization whose declarator-id is
788 not qualified shall be declared in the nearest enclosing namespace of the
789 template, or, if the namespace is inline (7.3.1), any namespace from its
790 enclosing namespace set. */
791 if (current_scope() != DECL_CONTEXT (tmpl)
792 && !at_namespace_scope_p ())
793 {
794 error ("specialization of %qD must appear at namespace scope", tmpl);
795 return false;
796 }
797
798 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
799 /* Same or enclosing namespace. */
800 return true;
801 else
802 {
803 auto_diagnostic_group d;
804 if (permerror (input_location,
805 "specialization of %qD in different namespace", tmpl))
806 inform (DECL_SOURCE_LOCATION (tmpl),
807 " from definition of %q#D", tmpl);
808 return false;
809 }
810 }
811
812 /* SPEC is an explicit instantiation. Check that it is valid to
813 perform this explicit instantiation in the current namespace. */
814
815 static void
816 check_explicit_instantiation_namespace (tree spec)
817 {
818 tree ns;
819
820 /* DR 275: An explicit instantiation shall appear in an enclosing
821 namespace of its template. */
822 ns = decl_namespace_context (spec);
823 if (!is_nested_namespace (current_namespace, ns))
824 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
825 "(which does not enclose namespace %qD)",
826 spec, current_namespace, ns);
827 }
828
829 // Returns the type of a template specialization only if that
830 // specialization needs to be defined. Otherwise (e.g., if the type has
831 // already been defined), the function returns NULL_TREE.
832 static tree
833 maybe_new_partial_specialization (tree type)
834 {
835 // An implicit instantiation of an incomplete type implies
836 // the definition of a new class template.
837 //
838 // template<typename T>
839 // struct S;
840 //
841 // template<typename T>
842 // struct S<T*>;
843 //
844 // Here, S<T*> is an implicit instantiation of S whose type
845 // is incomplete.
846 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
847 return type;
848
849 // It can also be the case that TYPE is a completed specialization.
850 // Continuing the previous example, suppose we also declare:
851 //
852 // template<typename T>
853 // requires Integral<T>
854 // struct S<T*>;
855 //
856 // Here, S<T*> refers to the specialization S<T*> defined
857 // above. However, we need to differentiate definitions because
858 // we intend to define a new partial specialization. In this case,
859 // we rely on the fact that the constraints are different for
860 // this declaration than that above.
861 //
862 // Note that we also get here for injected class names and
863 // late-parsed template definitions. We must ensure that we
864 // do not create new type declarations for those cases.
865 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
866 {
867 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
868 tree args = CLASSTYPE_TI_ARGS (type);
869
870 // If there are no template parameters, this cannot be a new
871 // partial template specializtion?
872 if (!current_template_parms)
873 return NULL_TREE;
874
875 // The injected-class-name is not a new partial specialization.
876 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
877 return NULL_TREE;
878
879 // If the constraints are not the same as those of the primary
880 // then, we can probably create a new specialization.
881 tree type_constr = current_template_constraints ();
882
883 if (type == TREE_TYPE (tmpl))
884 {
885 tree main_constr = get_constraints (tmpl);
886 if (equivalent_constraints (type_constr, main_constr))
887 return NULL_TREE;
888 }
889
890 // Also, if there's a pre-existing specialization with matching
891 // constraints, then this also isn't new.
892 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
893 while (specs)
894 {
895 tree spec_tmpl = TREE_VALUE (specs);
896 tree spec_args = TREE_PURPOSE (specs);
897 tree spec_constr = get_constraints (spec_tmpl);
898 if (comp_template_args (args, spec_args)
899 && equivalent_constraints (type_constr, spec_constr))
900 return NULL_TREE;
901 specs = TREE_CHAIN (specs);
902 }
903
904 // Create a new type node (and corresponding type decl)
905 // for the newly declared specialization.
906 tree t = make_class_type (TREE_CODE (type));
907 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
908 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
909
910 /* We only need a separate type node for storing the definition of this
911 partial specialization; uses of S<T*> are unconstrained, so all are
912 equivalent. So keep TYPE_CANONICAL the same. */
913 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
914
915 // Build the corresponding type decl.
916 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
917 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
918 DECL_SOURCE_LOCATION (d) = input_location;
919
920 return t;
921 }
922
923 return NULL_TREE;
924 }
925
926 /* The TYPE is being declared. If it is a template type, that means it
927 is a partial specialization. Do appropriate error-checking. */
928
929 tree
930 maybe_process_partial_specialization (tree type)
931 {
932 tree context;
933
934 if (type == error_mark_node)
935 return error_mark_node;
936
937 /* A lambda that appears in specialization context is not itself a
938 specialization. */
939 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
940 return type;
941
942 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
943 {
944 error ("name of class shadows template template parameter %qD",
945 TYPE_NAME (type));
946 return error_mark_node;
947 }
948
949 context = TYPE_CONTEXT (type);
950
951 if (TYPE_ALIAS_P (type))
952 {
953 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
954
955 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
956 error ("specialization of alias template %qD",
957 TI_TEMPLATE (tinfo));
958 else
959 error ("explicit specialization of non-template %qT", type);
960 return error_mark_node;
961 }
962 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
963 {
964 /* This is for ordinary explicit specialization and partial
965 specialization of a template class such as:
966
967 template <> class C<int>;
968
969 or:
970
971 template <class T> class C<T*>;
972
973 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
974
975 if (tree t = maybe_new_partial_specialization (type))
976 {
977 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
978 && !at_namespace_scope_p ())
979 return error_mark_node;
980 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
981 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
982 if (processing_template_decl)
983 {
984 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
985 if (decl == error_mark_node)
986 return error_mark_node;
987 return TREE_TYPE (decl);
988 }
989 }
990 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
991 error ("specialization of %qT after instantiation", type);
992 else if (errorcount && !processing_specialization
993 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
994 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
995 /* Trying to define a specialization either without a template<> header
996 or in an inappropriate place. We've already given an error, so just
997 bail now so we don't actually define the specialization. */
998 return error_mark_node;
999 }
1000 else if (CLASS_TYPE_P (type)
1001 && !CLASSTYPE_USE_TEMPLATE (type)
1002 && CLASSTYPE_TEMPLATE_INFO (type)
1003 && context && CLASS_TYPE_P (context)
1004 && CLASSTYPE_TEMPLATE_INFO (context))
1005 {
1006 /* This is for an explicit specialization of member class
1007 template according to [temp.expl.spec/18]:
1008
1009 template <> template <class U> class C<int>::D;
1010
1011 The context `C<int>' must be an implicit instantiation.
1012 Otherwise this is just a member class template declared
1013 earlier like:
1014
1015 template <> class C<int> { template <class U> class D; };
1016 template <> template <class U> class C<int>::D;
1017
1018 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1019 while in the second case, `C<int>::D' is a primary template
1020 and `C<T>::D' may not exist. */
1021
1022 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1023 && !COMPLETE_TYPE_P (type))
1024 {
1025 tree t;
1026 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1027
1028 if (current_namespace
1029 != decl_namespace_context (tmpl))
1030 {
1031 permerror (input_location,
1032 "specializing %q#T in different namespace", type);
1033 permerror (DECL_SOURCE_LOCATION (tmpl),
1034 " from definition of %q#D", tmpl);
1035 }
1036
1037 /* Check for invalid specialization after instantiation:
1038
1039 template <> template <> class C<int>::D<int>;
1040 template <> template <class U> class C<int>::D; */
1041
1042 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1043 t; t = TREE_CHAIN (t))
1044 {
1045 tree inst = TREE_VALUE (t);
1046 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1047 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1048 {
1049 /* We already have a full specialization of this partial
1050 instantiation, or a full specialization has been
1051 looked up but not instantiated. Reassign it to the
1052 new member specialization template. */
1053 spec_entry elt;
1054 spec_entry *entry;
1055
1056 elt.tmpl = most_general_template (tmpl);
1057 elt.args = CLASSTYPE_TI_ARGS (inst);
1058 elt.spec = inst;
1059
1060 type_specializations->remove_elt (&elt);
1061
1062 elt.tmpl = tmpl;
1063 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1064
1065 spec_entry **slot
1066 = type_specializations->find_slot (&elt, INSERT);
1067 entry = ggc_alloc<spec_entry> ();
1068 *entry = elt;
1069 *slot = entry;
1070 }
1071 else
1072 /* But if we've had an implicit instantiation, that's a
1073 problem ([temp.expl.spec]/6). */
1074 error ("specialization %qT after instantiation %qT",
1075 type, inst);
1076 }
1077
1078 /* Mark TYPE as a specialization. And as a result, we only
1079 have one level of template argument for the innermost
1080 class template. */
1081 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1082 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1083 CLASSTYPE_TI_ARGS (type)
1084 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1085 }
1086 }
1087 else if (processing_specialization)
1088 {
1089 /* Someday C++0x may allow for enum template specialization. */
1090 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1091 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1092 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1093 "of %qD not allowed by ISO C++", type);
1094 else
1095 {
1096 error ("explicit specialization of non-template %qT", type);
1097 return error_mark_node;
1098 }
1099 }
1100
1101 return type;
1102 }
1103
1104 /* Returns nonzero if we can optimize the retrieval of specializations
1105 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1106 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1107
1108 static inline bool
1109 optimize_specialization_lookup_p (tree tmpl)
1110 {
1111 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1112 && DECL_CLASS_SCOPE_P (tmpl)
1113 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1114 parameter. */
1115 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1116 /* The optimized lookup depends on the fact that the
1117 template arguments for the member function template apply
1118 purely to the containing class, which is not true if the
1119 containing class is an explicit or partial
1120 specialization. */
1121 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1122 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1123 && !DECL_CONV_FN_P (tmpl)
1124 /* It is possible to have a template that is not a member
1125 template and is not a member of a template class:
1126
1127 template <typename T>
1128 struct S { friend A::f(); };
1129
1130 Here, the friend function is a template, but the context does
1131 not have template information. The optimized lookup relies
1132 on having ARGS be the template arguments for both the class
1133 and the function template. */
1134 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1135 }
1136
1137 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1138 gone through coerce_template_parms by now. */
1139
1140 static void
1141 verify_unstripped_args_1 (tree inner)
1142 {
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1144 {
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (ARGUMENT_PACK_P (arg))
1151 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1152 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1153 /* Allow typedefs on the type of a non-type argument, since a
1154 parameter can have them. */;
1155 else
1156 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 }
1158 }
1159
1160 static void
1161 verify_unstripped_args (tree args)
1162 {
1163 ++processing_template_decl;
1164 if (!any_dependent_template_arguments_p (args))
1165 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1166 --processing_template_decl;
1167 }
1168
1169 /* Retrieve the specialization (in the sense of [temp.spec] - a
1170 specialization is either an instantiation or an explicit
1171 specialization) of TMPL for the given template ARGS. If there is
1172 no such specialization, return NULL_TREE. The ARGS are a vector of
1173 arguments, or a vector of vectors of arguments, in the case of
1174 templates with more than one level of parameters.
1175
1176 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1177 then we search for a partial specialization matching ARGS. This
1178 parameter is ignored if TMPL is not a class template.
1179
1180 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1181 result is a NONTYPE_ARGUMENT_PACK. */
1182
1183 static tree
1184 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1185 {
1186 if (tmpl == NULL_TREE)
1187 return NULL_TREE;
1188
1189 if (args == error_mark_node)
1190 return NULL_TREE;
1191
1192 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1193 || TREE_CODE (tmpl) == FIELD_DECL);
1194
1195 /* There should be as many levels of arguments as there are
1196 levels of parameters. */
1197 gcc_assert (TMPL_ARGS_DEPTH (args)
1198 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1199 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1200 : template_class_depth (DECL_CONTEXT (tmpl))));
1201
1202 if (flag_checking)
1203 verify_unstripped_args (args);
1204
1205 /* Lambda functions in templates aren't instantiated normally, but through
1206 tsubst_lambda_expr. */
1207 if (lambda_fn_in_template_p (tmpl))
1208 return NULL_TREE;
1209
1210 if (optimize_specialization_lookup_p (tmpl))
1211 {
1212 /* The template arguments actually apply to the containing
1213 class. Find the class specialization with those
1214 arguments. */
1215 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1216 tree class_specialization
1217 = retrieve_specialization (class_template, args, 0);
1218 if (!class_specialization)
1219 return NULL_TREE;
1220
1221 /* Find the instance of TMPL. */
1222 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1223 for (ovl_iterator iter (fns); iter; ++iter)
1224 {
1225 tree fn = *iter;
1226 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1227 /* using-declarations can add base methods to the method vec,
1228 and we don't want those here. */
1229 && DECL_CONTEXT (fn) == class_specialization)
1230 return fn;
1231 }
1232 return NULL_TREE;
1233 }
1234 else
1235 {
1236 spec_entry *found;
1237 spec_entry elt;
1238 hash_table<spec_hasher> *specializations;
1239
1240 elt.tmpl = tmpl;
1241 elt.args = args;
1242 elt.spec = NULL_TREE;
1243
1244 if (DECL_CLASS_TEMPLATE_P (tmpl))
1245 specializations = type_specializations;
1246 else
1247 specializations = decl_specializations;
1248
1249 if (hash == 0)
1250 hash = spec_hasher::hash (&elt);
1251 found = specializations->find_with_hash (&elt, hash);
1252 if (found)
1253 return found->spec;
1254 }
1255
1256 return NULL_TREE;
1257 }
1258
1259 /* Like retrieve_specialization, but for local declarations. */
1260
1261 tree
1262 retrieve_local_specialization (tree tmpl)
1263 {
1264 if (local_specializations == NULL)
1265 return NULL_TREE;
1266
1267 tree *slot = local_specializations->get (tmpl);
1268 return slot ? *slot : NULL_TREE;
1269 }
1270
1271 /* Returns nonzero iff DECL is a specialization of TMPL. */
1272
1273 int
1274 is_specialization_of (tree decl, tree tmpl)
1275 {
1276 tree t;
1277
1278 if (TREE_CODE (decl) == FUNCTION_DECL)
1279 {
1280 for (t = decl;
1281 t != NULL_TREE;
1282 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1283 if (t == tmpl)
1284 return 1;
1285 }
1286 else
1287 {
1288 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1289
1290 for (t = TREE_TYPE (decl);
1291 t != NULL_TREE;
1292 t = CLASSTYPE_USE_TEMPLATE (t)
1293 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1294 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1295 return 1;
1296 }
1297
1298 return 0;
1299 }
1300
1301 /* Returns nonzero iff DECL is a specialization of friend declaration
1302 FRIEND_DECL according to [temp.friend]. */
1303
1304 bool
1305 is_specialization_of_friend (tree decl, tree friend_decl)
1306 {
1307 bool need_template = true;
1308 int template_depth;
1309
1310 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1311 || TREE_CODE (decl) == TYPE_DECL);
1312
1313 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1314 of a template class, we want to check if DECL is a specialization
1315 if this. */
1316 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1317 && DECL_TEMPLATE_INFO (friend_decl)
1318 && !DECL_USE_TEMPLATE (friend_decl))
1319 {
1320 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1321 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1322 need_template = false;
1323 }
1324 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1325 && !PRIMARY_TEMPLATE_P (friend_decl))
1326 need_template = false;
1327
1328 /* There is nothing to do if this is not a template friend. */
1329 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1330 return false;
1331
1332 if (is_specialization_of (decl, friend_decl))
1333 return true;
1334
1335 /* [temp.friend/6]
1336 A member of a class template may be declared to be a friend of a
1337 non-template class. In this case, the corresponding member of
1338 every specialization of the class template is a friend of the
1339 class granting friendship.
1340
1341 For example, given a template friend declaration
1342
1343 template <class T> friend void A<T>::f();
1344
1345 the member function below is considered a friend
1346
1347 template <> struct A<int> {
1348 void f();
1349 };
1350
1351 For this type of template friend, TEMPLATE_DEPTH below will be
1352 nonzero. To determine if DECL is a friend of FRIEND, we first
1353 check if the enclosing class is a specialization of another. */
1354
1355 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1356 if (template_depth
1357 && DECL_CLASS_SCOPE_P (decl)
1358 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1359 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1360 {
1361 /* Next, we check the members themselves. In order to handle
1362 a few tricky cases, such as when FRIEND_DECL's are
1363
1364 template <class T> friend void A<T>::g(T t);
1365 template <class T> template <T t> friend void A<T>::h();
1366
1367 and DECL's are
1368
1369 void A<int>::g(int);
1370 template <int> void A<int>::h();
1371
1372 we need to figure out ARGS, the template arguments from
1373 the context of DECL. This is required for template substitution
1374 of `T' in the function parameter of `g' and template parameter
1375 of `h' in the above examples. Here ARGS corresponds to `int'. */
1376
1377 tree context = DECL_CONTEXT (decl);
1378 tree args = NULL_TREE;
1379 int current_depth = 0;
1380
1381 while (current_depth < template_depth)
1382 {
1383 if (CLASSTYPE_TEMPLATE_INFO (context))
1384 {
1385 if (current_depth == 0)
1386 args = TYPE_TI_ARGS (context);
1387 else
1388 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1389 current_depth++;
1390 }
1391 context = TYPE_CONTEXT (context);
1392 }
1393
1394 if (TREE_CODE (decl) == FUNCTION_DECL)
1395 {
1396 bool is_template;
1397 tree friend_type;
1398 tree decl_type;
1399 tree friend_args_type;
1400 tree decl_args_type;
1401
1402 /* Make sure that both DECL and FRIEND_DECL are templates or
1403 non-templates. */
1404 is_template = DECL_TEMPLATE_INFO (decl)
1405 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1406 if (need_template ^ is_template)
1407 return false;
1408 else if (is_template)
1409 {
1410 /* If both are templates, check template parameter list. */
1411 tree friend_parms
1412 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1413 args, tf_none);
1414 if (!comp_template_parms
1415 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1416 friend_parms))
1417 return false;
1418
1419 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1420 }
1421 else
1422 decl_type = TREE_TYPE (decl);
1423
1424 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1425 tf_none, NULL_TREE);
1426 if (friend_type == error_mark_node)
1427 return false;
1428
1429 /* Check if return types match. */
1430 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1431 return false;
1432
1433 /* Check if function parameter types match, ignoring the
1434 `this' parameter. */
1435 friend_args_type = TYPE_ARG_TYPES (friend_type);
1436 decl_args_type = TYPE_ARG_TYPES (decl_type);
1437 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1438 friend_args_type = TREE_CHAIN (friend_args_type);
1439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1440 decl_args_type = TREE_CHAIN (decl_args_type);
1441
1442 return compparms (decl_args_type, friend_args_type);
1443 }
1444 else
1445 {
1446 /* DECL is a TYPE_DECL */
1447 bool is_template;
1448 tree decl_type = TREE_TYPE (decl);
1449
1450 /* Make sure that both DECL and FRIEND_DECL are templates or
1451 non-templates. */
1452 is_template
1453 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1454 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1455
1456 if (need_template ^ is_template)
1457 return false;
1458 else if (is_template)
1459 {
1460 tree friend_parms;
1461 /* If both are templates, check the name of the two
1462 TEMPLATE_DECL's first because is_friend didn't. */
1463 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1464 != DECL_NAME (friend_decl))
1465 return false;
1466
1467 /* Now check template parameter list. */
1468 friend_parms
1469 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1470 args, tf_none);
1471 return comp_template_parms
1472 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1473 friend_parms);
1474 }
1475 else
1476 return (DECL_NAME (decl)
1477 == DECL_NAME (friend_decl));
1478 }
1479 }
1480 return false;
1481 }
1482
1483 /* Register the specialization SPEC as a specialization of TMPL with
1484 the indicated ARGS. IS_FRIEND indicates whether the specialization
1485 is actually just a friend declaration. ATTRLIST is the list of
1486 attributes that the specialization is declared with or NULL when
1487 it isn't. Returns SPEC, or an equivalent prior declaration, if
1488 available.
1489
1490 We also store instantiations of field packs in the hash table, even
1491 though they are not themselves templates, to make lookup easier. */
1492
1493 static tree
1494 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1495 hashval_t hash)
1496 {
1497 tree fn;
1498 spec_entry **slot = NULL;
1499 spec_entry elt;
1500
1501 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1502 || (TREE_CODE (tmpl) == FIELD_DECL
1503 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1504
1505 if (TREE_CODE (spec) == FUNCTION_DECL
1506 && uses_template_parms (DECL_TI_ARGS (spec)))
1507 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1508 register it; we want the corresponding TEMPLATE_DECL instead.
1509 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1510 the more obvious `uses_template_parms (spec)' to avoid problems
1511 with default function arguments. In particular, given
1512 something like this:
1513
1514 template <class T> void f(T t1, T t = T())
1515
1516 the default argument expression is not substituted for in an
1517 instantiation unless and until it is actually needed. */
1518 return spec;
1519
1520 if (optimize_specialization_lookup_p (tmpl))
1521 /* We don't put these specializations in the hash table, but we might
1522 want to give an error about a mismatch. */
1523 fn = retrieve_specialization (tmpl, args, 0);
1524 else
1525 {
1526 elt.tmpl = tmpl;
1527 elt.args = args;
1528 elt.spec = spec;
1529
1530 if (hash == 0)
1531 hash = spec_hasher::hash (&elt);
1532
1533 slot =
1534 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1535 if (*slot)
1536 fn = ((spec_entry *) *slot)->spec;
1537 else
1538 fn = NULL_TREE;
1539 }
1540
1541 /* We can sometimes try to re-register a specialization that we've
1542 already got. In particular, regenerate_decl_from_template calls
1543 duplicate_decls which will update the specialization list. But,
1544 we'll still get called again here anyhow. It's more convenient
1545 to simply allow this than to try to prevent it. */
1546 if (fn == spec)
1547 return spec;
1548 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1549 {
1550 if (DECL_TEMPLATE_INSTANTIATION (fn))
1551 {
1552 if (DECL_ODR_USED (fn)
1553 || DECL_EXPLICIT_INSTANTIATION (fn))
1554 {
1555 error ("specialization of %qD after instantiation",
1556 fn);
1557 return error_mark_node;
1558 }
1559 else
1560 {
1561 tree clone;
1562 /* This situation should occur only if the first
1563 specialization is an implicit instantiation, the
1564 second is an explicit specialization, and the
1565 implicit instantiation has not yet been used. That
1566 situation can occur if we have implicitly
1567 instantiated a member function and then specialized
1568 it later.
1569
1570 We can also wind up here if a friend declaration that
1571 looked like an instantiation turns out to be a
1572 specialization:
1573
1574 template <class T> void foo(T);
1575 class S { friend void foo<>(int) };
1576 template <> void foo(int);
1577
1578 We transform the existing DECL in place so that any
1579 pointers to it become pointers to the updated
1580 declaration.
1581
1582 If there was a definition for the template, but not
1583 for the specialization, we want this to look as if
1584 there were no definition, and vice versa. */
1585 DECL_INITIAL (fn) = NULL_TREE;
1586 duplicate_decls (spec, fn, is_friend);
1587 /* The call to duplicate_decls will have applied
1588 [temp.expl.spec]:
1589
1590 An explicit specialization of a function template
1591 is inline only if it is explicitly declared to be,
1592 and independently of whether its function template
1593 is.
1594
1595 to the primary function; now copy the inline bits to
1596 the various clones. */
1597 FOR_EACH_CLONE (clone, fn)
1598 {
1599 DECL_DECLARED_INLINE_P (clone)
1600 = DECL_DECLARED_INLINE_P (fn);
1601 DECL_SOURCE_LOCATION (clone)
1602 = DECL_SOURCE_LOCATION (fn);
1603 DECL_DELETED_FN (clone)
1604 = DECL_DELETED_FN (fn);
1605 }
1606 check_specialization_namespace (tmpl);
1607
1608 return fn;
1609 }
1610 }
1611 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1612 {
1613 tree dd = duplicate_decls (spec, fn, is_friend);
1614 if (dd == error_mark_node)
1615 /* We've already complained in duplicate_decls. */
1616 return error_mark_node;
1617
1618 if (dd == NULL_TREE && DECL_INITIAL (spec))
1619 /* Dup decl failed, but this is a new definition. Set the
1620 line number so any errors match this new
1621 definition. */
1622 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1623
1624 return fn;
1625 }
1626 }
1627 else if (fn)
1628 return duplicate_decls (spec, fn, is_friend);
1629
1630 /* A specialization must be declared in the same namespace as the
1631 template it is specializing. */
1632 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1633 && !check_specialization_namespace (tmpl))
1634 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1635
1636 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1637 {
1638 spec_entry *entry = ggc_alloc<spec_entry> ();
1639 gcc_assert (tmpl && args && spec);
1640 *entry = elt;
1641 *slot = entry;
1642 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1643 && PRIMARY_TEMPLATE_P (tmpl)
1644 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1645 || variable_template_p (tmpl))
1646 /* If TMPL is a forward declaration of a template function, keep a list
1647 of all specializations in case we need to reassign them to a friend
1648 template later in tsubst_friend_function.
1649
1650 Also keep a list of all variable template instantiations so that
1651 process_partial_specialization can check whether a later partial
1652 specialization would have used it. */
1653 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1654 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1655 }
1656
1657 return spec;
1658 }
1659
1660 /* Returns true iff two spec_entry nodes are equivalent. */
1661
1662 int comparing_specializations;
1663
1664 bool
1665 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1666 {
1667 int equal;
1668
1669 ++comparing_specializations;
1670 equal = (e1->tmpl == e2->tmpl
1671 && comp_template_args (e1->args, e2->args));
1672 if (equal && flag_concepts
1673 /* tmpl could be a FIELD_DECL for a capture pack. */
1674 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1675 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1676 && uses_template_parms (e1->args))
1677 {
1678 /* Partial specializations of a variable template can be distinguished by
1679 constraints. */
1680 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1681 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1682 equal = equivalent_constraints (c1, c2);
1683 }
1684 --comparing_specializations;
1685
1686 return equal;
1687 }
1688
1689 /* Returns a hash for a template TMPL and template arguments ARGS. */
1690
1691 static hashval_t
1692 hash_tmpl_and_args (tree tmpl, tree args)
1693 {
1694 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1695 return iterative_hash_template_arg (args, val);
1696 }
1697
1698 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1699 ignoring SPEC. */
1700
1701 hashval_t
1702 spec_hasher::hash (spec_entry *e)
1703 {
1704 return hash_tmpl_and_args (e->tmpl, e->args);
1705 }
1706
1707 /* Recursively calculate a hash value for a template argument ARG, for use
1708 in the hash tables of template specializations. */
1709
1710 hashval_t
1711 iterative_hash_template_arg (tree arg, hashval_t val)
1712 {
1713 unsigned HOST_WIDE_INT i;
1714 enum tree_code code;
1715 char tclass;
1716
1717 if (arg == NULL_TREE)
1718 return iterative_hash_object (arg, val);
1719
1720 if (!TYPE_P (arg))
1721 STRIP_NOPS (arg);
1722
1723 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1724 gcc_unreachable ();
1725
1726 code = TREE_CODE (arg);
1727 tclass = TREE_CODE_CLASS (code);
1728
1729 val = iterative_hash_object (code, val);
1730
1731 switch (code)
1732 {
1733 case ERROR_MARK:
1734 return val;
1735
1736 case IDENTIFIER_NODE:
1737 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1738
1739 case TREE_VEC:
1740 {
1741 int i, len = TREE_VEC_LENGTH (arg);
1742 for (i = 0; i < len; ++i)
1743 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1744 return val;
1745 }
1746
1747 case TYPE_PACK_EXPANSION:
1748 case EXPR_PACK_EXPANSION:
1749 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1750 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1751
1752 case TYPE_ARGUMENT_PACK:
1753 case NONTYPE_ARGUMENT_PACK:
1754 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1755
1756 case TREE_LIST:
1757 for (; arg; arg = TREE_CHAIN (arg))
1758 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1759 return val;
1760
1761 case OVERLOAD:
1762 for (lkp_iterator iter (arg); iter; ++iter)
1763 val = iterative_hash_template_arg (*iter, val);
1764 return val;
1765
1766 case CONSTRUCTOR:
1767 {
1768 tree field, value;
1769 iterative_hash_template_arg (TREE_TYPE (arg), val);
1770 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1771 {
1772 val = iterative_hash_template_arg (field, val);
1773 val = iterative_hash_template_arg (value, val);
1774 }
1775 return val;
1776 }
1777
1778 case PARM_DECL:
1779 if (!DECL_ARTIFICIAL (arg))
1780 {
1781 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1782 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1783 }
1784 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1785
1786 case TARGET_EXPR:
1787 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1788
1789 case PTRMEM_CST:
1790 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1791 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1792
1793 case TEMPLATE_PARM_INDEX:
1794 val = iterative_hash_template_arg
1795 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1796 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1797 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1798
1799 case TRAIT_EXPR:
1800 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1801 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1802 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1803
1804 case BASELINK:
1805 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1806 val);
1807 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1808 val);
1809
1810 case MODOP_EXPR:
1811 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1812 code = TREE_CODE (TREE_OPERAND (arg, 1));
1813 val = iterative_hash_object (code, val);
1814 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1815
1816 case LAMBDA_EXPR:
1817 /* [temp.over.link] Two lambda-expressions are never considered
1818 equivalent.
1819
1820 So just hash the closure type. */
1821 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1822
1823 case CAST_EXPR:
1824 case IMPLICIT_CONV_EXPR:
1825 case STATIC_CAST_EXPR:
1826 case REINTERPRET_CAST_EXPR:
1827 case CONST_CAST_EXPR:
1828 case DYNAMIC_CAST_EXPR:
1829 case NEW_EXPR:
1830 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1831 /* Now hash operands as usual. */
1832 break;
1833
1834 default:
1835 break;
1836 }
1837
1838 switch (tclass)
1839 {
1840 case tcc_type:
1841 if (alias_template_specialization_p (arg))
1842 {
1843 // We want an alias specialization that survived strip_typedefs
1844 // to hash differently from its TYPE_CANONICAL, to avoid hash
1845 // collisions that compare as different in template_args_equal.
1846 // These could be dependent specializations that strip_typedefs
1847 // left alone, or untouched specializations because
1848 // coerce_template_parms returns the unconverted template
1849 // arguments if it sees incomplete argument packs.
1850 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1851 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1852 }
1853 if (TYPE_CANONICAL (arg))
1854 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1855 val);
1856 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1857 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1858 /* Otherwise just compare the types during lookup. */
1859 return val;
1860
1861 case tcc_declaration:
1862 case tcc_constant:
1863 return iterative_hash_expr (arg, val);
1864
1865 default:
1866 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1867 {
1868 unsigned n = cp_tree_operand_length (arg);
1869 for (i = 0; i < n; ++i)
1870 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1871 return val;
1872 }
1873 }
1874 gcc_unreachable ();
1875 return 0;
1876 }
1877
1878 /* Unregister the specialization SPEC as a specialization of TMPL.
1879 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1880 if the SPEC was listed as a specialization of TMPL.
1881
1882 Note that SPEC has been ggc_freed, so we can't look inside it. */
1883
1884 bool
1885 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1886 {
1887 spec_entry *entry;
1888 spec_entry elt;
1889
1890 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1891 elt.args = TI_ARGS (tinfo);
1892 elt.spec = NULL_TREE;
1893
1894 entry = decl_specializations->find (&elt);
1895 if (entry != NULL)
1896 {
1897 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1898 gcc_assert (new_spec != NULL_TREE);
1899 entry->spec = new_spec;
1900 return 1;
1901 }
1902
1903 return 0;
1904 }
1905
1906 /* Like register_specialization, but for local declarations. We are
1907 registering SPEC, an instantiation of TMPL. */
1908
1909 void
1910 register_local_specialization (tree spec, tree tmpl)
1911 {
1912 gcc_assert (tmpl != spec);
1913 local_specializations->put (tmpl, spec);
1914 }
1915
1916 /* TYPE is a class type. Returns true if TYPE is an explicitly
1917 specialized class. */
1918
1919 bool
1920 explicit_class_specialization_p (tree type)
1921 {
1922 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1923 return false;
1924 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1925 }
1926
1927 /* Print the list of functions at FNS, going through all the overloads
1928 for each element of the list. Alternatively, FNS cannot be a
1929 TREE_LIST, in which case it will be printed together with all the
1930 overloads.
1931
1932 MORE and *STR should respectively be FALSE and NULL when the function
1933 is called from the outside. They are used internally on recursive
1934 calls. print_candidates manages the two parameters and leaves NULL
1935 in *STR when it ends. */
1936
1937 static void
1938 print_candidates_1 (tree fns, char **str, bool more = false)
1939 {
1940 if (TREE_CODE (fns) == TREE_LIST)
1941 for (; fns; fns = TREE_CHAIN (fns))
1942 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1943 else
1944 for (lkp_iterator iter (fns); iter;)
1945 {
1946 tree cand = *iter;
1947 ++iter;
1948
1949 const char *pfx = *str;
1950 if (!pfx)
1951 {
1952 if (more || iter)
1953 pfx = _("candidates are:");
1954 else
1955 pfx = _("candidate is:");
1956 *str = get_spaces (pfx);
1957 }
1958 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1959 }
1960 }
1961
1962 /* Print the list of candidate FNS in an error message. FNS can also
1963 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1964
1965 void
1966 print_candidates (tree fns)
1967 {
1968 char *str = NULL;
1969 print_candidates_1 (fns, &str);
1970 free (str);
1971 }
1972
1973 /* Get a (possibly) constrained template declaration for the
1974 purpose of ordering candidates. */
1975 static tree
1976 get_template_for_ordering (tree list)
1977 {
1978 gcc_assert (TREE_CODE (list) == TREE_LIST);
1979 tree f = TREE_VALUE (list);
1980 if (tree ti = DECL_TEMPLATE_INFO (f))
1981 return TI_TEMPLATE (ti);
1982 return f;
1983 }
1984
1985 /* Among candidates having the same signature, return the
1986 most constrained or NULL_TREE if there is no best candidate.
1987 If the signatures of candidates vary (e.g., template
1988 specialization vs. member function), then there can be no
1989 most constrained.
1990
1991 Note that we don't compare constraints on the functions
1992 themselves, but rather those of their templates. */
1993 static tree
1994 most_constrained_function (tree candidates)
1995 {
1996 // Try to find the best candidate in a first pass.
1997 tree champ = candidates;
1998 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1999 {
2000 int winner = more_constrained (get_template_for_ordering (champ),
2001 get_template_for_ordering (c));
2002 if (winner == -1)
2003 champ = c; // The candidate is more constrained
2004 else if (winner == 0)
2005 return NULL_TREE; // Neither is more constrained
2006 }
2007
2008 // Verify that the champ is better than previous candidates.
2009 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2010 if (!more_constrained (get_template_for_ordering (champ),
2011 get_template_for_ordering (c)))
2012 return NULL_TREE;
2013 }
2014
2015 return champ;
2016 }
2017
2018
2019 /* Returns the template (one of the functions given by TEMPLATE_ID)
2020 which can be specialized to match the indicated DECL with the
2021 explicit template args given in TEMPLATE_ID. The DECL may be
2022 NULL_TREE if none is available. In that case, the functions in
2023 TEMPLATE_ID are non-members.
2024
2025 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2026 specialization of a member template.
2027
2028 The TEMPLATE_COUNT is the number of references to qualifying
2029 template classes that appeared in the name of the function. See
2030 check_explicit_specialization for a more accurate description.
2031
2032 TSK indicates what kind of template declaration (if any) is being
2033 declared. TSK_TEMPLATE indicates that the declaration given by
2034 DECL, though a FUNCTION_DECL, has template parameters, and is
2035 therefore a template function.
2036
2037 The template args (those explicitly specified and those deduced)
2038 are output in a newly created vector *TARGS_OUT.
2039
2040 If it is impossible to determine the result, an error message is
2041 issued. The error_mark_node is returned to indicate failure. */
2042
2043 static tree
2044 determine_specialization (tree template_id,
2045 tree decl,
2046 tree* targs_out,
2047 int need_member_template,
2048 int template_count,
2049 tmpl_spec_kind tsk)
2050 {
2051 tree fns;
2052 tree targs;
2053 tree explicit_targs;
2054 tree candidates = NULL_TREE;
2055
2056 /* A TREE_LIST of templates of which DECL may be a specialization.
2057 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2058 corresponding TREE_PURPOSE is the set of template arguments that,
2059 when used to instantiate the template, would produce a function
2060 with the signature of DECL. */
2061 tree templates = NULL_TREE;
2062 int header_count;
2063 cp_binding_level *b;
2064
2065 *targs_out = NULL_TREE;
2066
2067 if (template_id == error_mark_node || decl == error_mark_node)
2068 return error_mark_node;
2069
2070 /* We shouldn't be specializing a member template of an
2071 unspecialized class template; we already gave an error in
2072 check_specialization_scope, now avoid crashing. */
2073 if (!VAR_P (decl)
2074 && template_count && DECL_CLASS_SCOPE_P (decl)
2075 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2076 {
2077 gcc_assert (errorcount);
2078 return error_mark_node;
2079 }
2080
2081 fns = TREE_OPERAND (template_id, 0);
2082 explicit_targs = TREE_OPERAND (template_id, 1);
2083
2084 if (fns == error_mark_node)
2085 return error_mark_node;
2086
2087 /* Check for baselinks. */
2088 if (BASELINK_P (fns))
2089 fns = BASELINK_FUNCTIONS (fns);
2090
2091 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2092 {
2093 error ("%qD is not a function template", fns);
2094 return error_mark_node;
2095 }
2096 else if (VAR_P (decl) && !variable_template_p (fns))
2097 {
2098 error ("%qD is not a variable template", fns);
2099 return error_mark_node;
2100 }
2101
2102 /* Count the number of template headers specified for this
2103 specialization. */
2104 header_count = 0;
2105 for (b = current_binding_level;
2106 b->kind == sk_template_parms;
2107 b = b->level_chain)
2108 ++header_count;
2109
2110 tree orig_fns = fns;
2111
2112 if (variable_template_p (fns))
2113 {
2114 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2115 targs = coerce_template_parms (parms, explicit_targs, fns,
2116 tf_warning_or_error,
2117 /*req_all*/true, /*use_defarg*/true);
2118 if (targs != error_mark_node)
2119 templates = tree_cons (targs, fns, templates);
2120 }
2121 else for (lkp_iterator iter (fns); iter; ++iter)
2122 {
2123 tree fn = *iter;
2124
2125 if (TREE_CODE (fn) == TEMPLATE_DECL)
2126 {
2127 tree decl_arg_types;
2128 tree fn_arg_types;
2129 tree insttype;
2130
2131 /* In case of explicit specialization, we need to check if
2132 the number of template headers appearing in the specialization
2133 is correct. This is usually done in check_explicit_specialization,
2134 but the check done there cannot be exhaustive when specializing
2135 member functions. Consider the following code:
2136
2137 template <> void A<int>::f(int);
2138 template <> template <> void A<int>::f(int);
2139
2140 Assuming that A<int> is not itself an explicit specialization
2141 already, the first line specializes "f" which is a non-template
2142 member function, whilst the second line specializes "f" which
2143 is a template member function. So both lines are syntactically
2144 correct, and check_explicit_specialization does not reject
2145 them.
2146
2147 Here, we can do better, as we are matching the specialization
2148 against the declarations. We count the number of template
2149 headers, and we check if they match TEMPLATE_COUNT + 1
2150 (TEMPLATE_COUNT is the number of qualifying template classes,
2151 plus there must be another header for the member template
2152 itself).
2153
2154 Notice that if header_count is zero, this is not a
2155 specialization but rather a template instantiation, so there
2156 is no check we can perform here. */
2157 if (header_count && header_count != template_count + 1)
2158 continue;
2159
2160 /* Check that the number of template arguments at the
2161 innermost level for DECL is the same as for FN. */
2162 if (current_binding_level->kind == sk_template_parms
2163 && !current_binding_level->explicit_spec_p
2164 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2165 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2166 (current_template_parms))))
2167 continue;
2168
2169 /* DECL might be a specialization of FN. */
2170 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2171 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2172
2173 /* For a non-static member function, we need to make sure
2174 that the const qualification is the same. Since
2175 get_bindings does not try to merge the "this" parameter,
2176 we must do the comparison explicitly. */
2177 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2178 {
2179 if (!same_type_p (TREE_VALUE (fn_arg_types),
2180 TREE_VALUE (decl_arg_types)))
2181 continue;
2182
2183 /* And the ref-qualification. */
2184 if (type_memfn_rqual (TREE_TYPE (decl))
2185 != type_memfn_rqual (TREE_TYPE (fn)))
2186 continue;
2187 }
2188
2189 /* Skip the "this" parameter and, for constructors of
2190 classes with virtual bases, the VTT parameter. A
2191 full specialization of a constructor will have a VTT
2192 parameter, but a template never will. */
2193 decl_arg_types
2194 = skip_artificial_parms_for (decl, decl_arg_types);
2195 fn_arg_types
2196 = skip_artificial_parms_for (fn, fn_arg_types);
2197
2198 /* Function templates cannot be specializations; there are
2199 no partial specializations of functions. Therefore, if
2200 the type of DECL does not match FN, there is no
2201 match.
2202
2203 Note that it should never be the case that we have both
2204 candidates added here, and for regular member functions
2205 below. */
2206 if (tsk == tsk_template)
2207 {
2208 if (compparms (fn_arg_types, decl_arg_types))
2209 candidates = tree_cons (NULL_TREE, fn, candidates);
2210 continue;
2211 }
2212
2213 /* See whether this function might be a specialization of this
2214 template. Suppress access control because we might be trying
2215 to make this specialization a friend, and we have already done
2216 access control for the declaration of the specialization. */
2217 push_deferring_access_checks (dk_no_check);
2218 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2219 pop_deferring_access_checks ();
2220
2221 if (!targs)
2222 /* We cannot deduce template arguments that when used to
2223 specialize TMPL will produce DECL. */
2224 continue;
2225
2226 if (uses_template_parms (targs))
2227 /* We deduced something involving 'auto', which isn't a valid
2228 template argument. */
2229 continue;
2230
2231 /* Remove, from the set of candidates, all those functions
2232 whose constraints are not satisfied. */
2233 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2234 continue;
2235
2236 // Then, try to form the new function type.
2237 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2238 if (insttype == error_mark_node)
2239 continue;
2240 fn_arg_types
2241 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2242 if (!compparms (fn_arg_types, decl_arg_types))
2243 continue;
2244
2245 /* Save this template, and the arguments deduced. */
2246 templates = tree_cons (targs, fn, templates);
2247 }
2248 else if (need_member_template)
2249 /* FN is an ordinary member function, and we need a
2250 specialization of a member template. */
2251 ;
2252 else if (TREE_CODE (fn) != FUNCTION_DECL)
2253 /* We can get IDENTIFIER_NODEs here in certain erroneous
2254 cases. */
2255 ;
2256 else if (!DECL_FUNCTION_MEMBER_P (fn))
2257 /* This is just an ordinary non-member function. Nothing can
2258 be a specialization of that. */
2259 ;
2260 else if (DECL_ARTIFICIAL (fn))
2261 /* Cannot specialize functions that are created implicitly. */
2262 ;
2263 else
2264 {
2265 tree decl_arg_types;
2266
2267 /* This is an ordinary member function. However, since
2268 we're here, we can assume its enclosing class is a
2269 template class. For example,
2270
2271 template <typename T> struct S { void f(); };
2272 template <> void S<int>::f() {}
2273
2274 Here, S<int>::f is a non-template, but S<int> is a
2275 template class. If FN has the same type as DECL, we
2276 might be in business. */
2277
2278 if (!DECL_TEMPLATE_INFO (fn))
2279 /* Its enclosing class is an explicit specialization
2280 of a template class. This is not a candidate. */
2281 continue;
2282
2283 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2284 TREE_TYPE (TREE_TYPE (fn))))
2285 /* The return types differ. */
2286 continue;
2287
2288 /* Adjust the type of DECL in case FN is a static member. */
2289 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2290 if (DECL_STATIC_FUNCTION_P (fn)
2291 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2292 decl_arg_types = TREE_CHAIN (decl_arg_types);
2293
2294 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2295 decl_arg_types))
2296 continue;
2297
2298 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2299 && (type_memfn_rqual (TREE_TYPE (decl))
2300 != type_memfn_rqual (TREE_TYPE (fn))))
2301 continue;
2302
2303 // If the deduced arguments do not satisfy the constraints,
2304 // this is not a candidate.
2305 if (flag_concepts && !constraints_satisfied_p (fn))
2306 continue;
2307
2308 // Add the candidate.
2309 candidates = tree_cons (NULL_TREE, fn, candidates);
2310 }
2311 }
2312
2313 if (templates && TREE_CHAIN (templates))
2314 {
2315 /* We have:
2316
2317 [temp.expl.spec]
2318
2319 It is possible for a specialization with a given function
2320 signature to be instantiated from more than one function
2321 template. In such cases, explicit specification of the
2322 template arguments must be used to uniquely identify the
2323 function template specialization being specialized.
2324
2325 Note that here, there's no suggestion that we're supposed to
2326 determine which of the candidate templates is most
2327 specialized. However, we, also have:
2328
2329 [temp.func.order]
2330
2331 Partial ordering of overloaded function template
2332 declarations is used in the following contexts to select
2333 the function template to which a function template
2334 specialization refers:
2335
2336 -- when an explicit specialization refers to a function
2337 template.
2338
2339 So, we do use the partial ordering rules, at least for now.
2340 This extension can only serve to make invalid programs valid,
2341 so it's safe. And, there is strong anecdotal evidence that
2342 the committee intended the partial ordering rules to apply;
2343 the EDG front end has that behavior, and John Spicer claims
2344 that the committee simply forgot to delete the wording in
2345 [temp.expl.spec]. */
2346 tree tmpl = most_specialized_instantiation (templates);
2347 if (tmpl != error_mark_node)
2348 {
2349 templates = tmpl;
2350 TREE_CHAIN (templates) = NULL_TREE;
2351 }
2352 }
2353
2354 // Concepts allows multiple declarations of member functions
2355 // with the same signature. Like above, we need to rely on
2356 // on the partial ordering of those candidates to determine which
2357 // is the best.
2358 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2359 {
2360 if (tree cand = most_constrained_function (candidates))
2361 {
2362 candidates = cand;
2363 TREE_CHAIN (cand) = NULL_TREE;
2364 }
2365 }
2366
2367 if (templates == NULL_TREE && candidates == NULL_TREE)
2368 {
2369 error ("template-id %qD for %q+D does not match any template "
2370 "declaration", template_id, decl);
2371 if (header_count && header_count != template_count + 1)
2372 inform (input_location, "saw %d %<template<>%>, need %d for "
2373 "specializing a member function template",
2374 header_count, template_count + 1);
2375 else
2376 print_candidates (orig_fns);
2377 return error_mark_node;
2378 }
2379 else if ((templates && TREE_CHAIN (templates))
2380 || (candidates && TREE_CHAIN (candidates))
2381 || (templates && candidates))
2382 {
2383 error ("ambiguous template specialization %qD for %q+D",
2384 template_id, decl);
2385 candidates = chainon (candidates, templates);
2386 print_candidates (candidates);
2387 return error_mark_node;
2388 }
2389
2390 /* We have one, and exactly one, match. */
2391 if (candidates)
2392 {
2393 tree fn = TREE_VALUE (candidates);
2394 *targs_out = copy_node (DECL_TI_ARGS (fn));
2395
2396 // Propagate the candidate's constraints to the declaration.
2397 set_constraints (decl, get_constraints (fn));
2398
2399 /* DECL is a re-declaration or partial instantiation of a template
2400 function. */
2401 if (TREE_CODE (fn) == TEMPLATE_DECL)
2402 return fn;
2403 /* It was a specialization of an ordinary member function in a
2404 template class. */
2405 return DECL_TI_TEMPLATE (fn);
2406 }
2407
2408 /* It was a specialization of a template. */
2409 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2410 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2411 {
2412 *targs_out = copy_node (targs);
2413 SET_TMPL_ARGS_LEVEL (*targs_out,
2414 TMPL_ARGS_DEPTH (*targs_out),
2415 TREE_PURPOSE (templates));
2416 }
2417 else
2418 *targs_out = TREE_PURPOSE (templates);
2419 return TREE_VALUE (templates);
2420 }
2421
2422 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2423 but with the default argument values filled in from those in the
2424 TMPL_TYPES. */
2425
2426 static tree
2427 copy_default_args_to_explicit_spec_1 (tree spec_types,
2428 tree tmpl_types)
2429 {
2430 tree new_spec_types;
2431
2432 if (!spec_types)
2433 return NULL_TREE;
2434
2435 if (spec_types == void_list_node)
2436 return void_list_node;
2437
2438 /* Substitute into the rest of the list. */
2439 new_spec_types =
2440 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2441 TREE_CHAIN (tmpl_types));
2442
2443 /* Add the default argument for this parameter. */
2444 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2445 TREE_VALUE (spec_types),
2446 new_spec_types);
2447 }
2448
2449 /* DECL is an explicit specialization. Replicate default arguments
2450 from the template it specializes. (That way, code like:
2451
2452 template <class T> void f(T = 3);
2453 template <> void f(double);
2454 void g () { f (); }
2455
2456 works, as required.) An alternative approach would be to look up
2457 the correct default arguments at the call-site, but this approach
2458 is consistent with how implicit instantiations are handled. */
2459
2460 static void
2461 copy_default_args_to_explicit_spec (tree decl)
2462 {
2463 tree tmpl;
2464 tree spec_types;
2465 tree tmpl_types;
2466 tree new_spec_types;
2467 tree old_type;
2468 tree new_type;
2469 tree t;
2470 tree object_type = NULL_TREE;
2471 tree in_charge = NULL_TREE;
2472 tree vtt = NULL_TREE;
2473
2474 /* See if there's anything we need to do. */
2475 tmpl = DECL_TI_TEMPLATE (decl);
2476 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2477 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2478 if (TREE_PURPOSE (t))
2479 break;
2480 if (!t)
2481 return;
2482
2483 old_type = TREE_TYPE (decl);
2484 spec_types = TYPE_ARG_TYPES (old_type);
2485
2486 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2487 {
2488 /* Remove the this pointer, but remember the object's type for
2489 CV quals. */
2490 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2491 spec_types = TREE_CHAIN (spec_types);
2492 tmpl_types = TREE_CHAIN (tmpl_types);
2493
2494 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2495 {
2496 /* DECL may contain more parameters than TMPL due to the extra
2497 in-charge parameter in constructors and destructors. */
2498 in_charge = spec_types;
2499 spec_types = TREE_CHAIN (spec_types);
2500 }
2501 if (DECL_HAS_VTT_PARM_P (decl))
2502 {
2503 vtt = spec_types;
2504 spec_types = TREE_CHAIN (spec_types);
2505 }
2506 }
2507
2508 /* Compute the merged default arguments. */
2509 new_spec_types =
2510 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2511
2512 /* Compute the new FUNCTION_TYPE. */
2513 if (object_type)
2514 {
2515 if (vtt)
2516 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2517 TREE_VALUE (vtt),
2518 new_spec_types);
2519
2520 if (in_charge)
2521 /* Put the in-charge parameter back. */
2522 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2523 TREE_VALUE (in_charge),
2524 new_spec_types);
2525
2526 new_type = build_method_type_directly (object_type,
2527 TREE_TYPE (old_type),
2528 new_spec_types);
2529 }
2530 else
2531 new_type = build_function_type (TREE_TYPE (old_type),
2532 new_spec_types);
2533 new_type = cp_build_type_attribute_variant (new_type,
2534 TYPE_ATTRIBUTES (old_type));
2535 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2536
2537 TREE_TYPE (decl) = new_type;
2538 }
2539
2540 /* Return the number of template headers we expect to see for a definition
2541 or specialization of CTYPE or one of its non-template members. */
2542
2543 int
2544 num_template_headers_for_class (tree ctype)
2545 {
2546 int num_templates = 0;
2547
2548 while (ctype && CLASS_TYPE_P (ctype))
2549 {
2550 /* You're supposed to have one `template <...>' for every
2551 template class, but you don't need one for a full
2552 specialization. For example:
2553
2554 template <class T> struct S{};
2555 template <> struct S<int> { void f(); };
2556 void S<int>::f () {}
2557
2558 is correct; there shouldn't be a `template <>' for the
2559 definition of `S<int>::f'. */
2560 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2561 /* If CTYPE does not have template information of any
2562 kind, then it is not a template, nor is it nested
2563 within a template. */
2564 break;
2565 if (explicit_class_specialization_p (ctype))
2566 break;
2567 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2568 ++num_templates;
2569
2570 ctype = TYPE_CONTEXT (ctype);
2571 }
2572
2573 return num_templates;
2574 }
2575
2576 /* Do a simple sanity check on the template headers that precede the
2577 variable declaration DECL. */
2578
2579 void
2580 check_template_variable (tree decl)
2581 {
2582 tree ctx = CP_DECL_CONTEXT (decl);
2583 int wanted = num_template_headers_for_class (ctx);
2584 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2585 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2586 {
2587 if (cxx_dialect < cxx14)
2588 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2589 "variable templates only available with "
2590 "-std=c++14 or -std=gnu++14");
2591
2592 // Namespace-scope variable templates should have a template header.
2593 ++wanted;
2594 }
2595 if (template_header_count > wanted)
2596 {
2597 auto_diagnostic_group d;
2598 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2599 "too many template headers for %qD "
2600 "(should be %d)",
2601 decl, wanted);
2602 if (warned && CLASS_TYPE_P (ctx)
2603 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2604 inform (DECL_SOURCE_LOCATION (decl),
2605 "members of an explicitly specialized class are defined "
2606 "without a template header");
2607 }
2608 }
2609
2610 /* An explicit specialization whose declarator-id or class-head-name is not
2611 qualified shall be declared in the nearest enclosing namespace of the
2612 template, or, if the namespace is inline (7.3.1), any namespace from its
2613 enclosing namespace set.
2614
2615 If the name declared in the explicit instantiation is an unqualified name,
2616 the explicit instantiation shall appear in the namespace where its template
2617 is declared or, if that namespace is inline (7.3.1), any namespace from its
2618 enclosing namespace set. */
2619
2620 void
2621 check_unqualified_spec_or_inst (tree t, location_t loc)
2622 {
2623 tree tmpl = most_general_template (t);
2624 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2625 && !is_nested_namespace (current_namespace,
2626 CP_DECL_CONTEXT (tmpl), true))
2627 {
2628 if (processing_specialization)
2629 permerror (loc, "explicit specialization of %qD outside its "
2630 "namespace must use a nested-name-specifier", tmpl);
2631 else if (processing_explicit_instantiation
2632 && cxx_dialect >= cxx11)
2633 /* This was allowed in C++98, so only pedwarn. */
2634 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2635 "outside its namespace must use a nested-name-"
2636 "specifier", tmpl);
2637 }
2638 }
2639
2640 /* Warn for a template specialization SPEC that is missing some of a set
2641 of function or type attributes that the template TEMPL is declared with.
2642 ATTRLIST is a list of additional attributes that SPEC should be taken
2643 to ultimately be declared with. */
2644
2645 static void
2646 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2647 {
2648 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2649 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2650
2651 /* Avoid warning if the difference between the primary and
2652 the specialization is not in one of the attributes below. */
2653 const char* const blacklist[] = {
2654 "alloc_align", "alloc_size", "assume_aligned", "format",
2655 "format_arg", "malloc", "nonnull", NULL
2656 };
2657
2658 /* Put together a list of the black listed attributes that the primary
2659 template is declared with that the specialization is not, in case
2660 it's not apparent from the most recent declaration of the primary. */
2661 pretty_printer str;
2662 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2663 blacklist, &str);
2664
2665 if (!nattrs)
2666 return;
2667
2668 auto_diagnostic_group d;
2669 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2670 "explicit specialization %q#D may be missing attributes",
2671 spec))
2672 inform (DECL_SOURCE_LOCATION (tmpl),
2673 nattrs > 1
2674 ? G_("missing primary template attributes %s")
2675 : G_("missing primary template attribute %s"),
2676 pp_formatted_text (&str));
2677 }
2678
2679 /* Check to see if the function just declared, as indicated in
2680 DECLARATOR, and in DECL, is a specialization of a function
2681 template. We may also discover that the declaration is an explicit
2682 instantiation at this point.
2683
2684 Returns DECL, or an equivalent declaration that should be used
2685 instead if all goes well. Issues an error message if something is
2686 amiss. Returns error_mark_node if the error is not easily
2687 recoverable.
2688
2689 FLAGS is a bitmask consisting of the following flags:
2690
2691 2: The function has a definition.
2692 4: The function is a friend.
2693
2694 The TEMPLATE_COUNT is the number of references to qualifying
2695 template classes that appeared in the name of the function. For
2696 example, in
2697
2698 template <class T> struct S { void f(); };
2699 void S<int>::f();
2700
2701 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2702 classes are not counted in the TEMPLATE_COUNT, so that in
2703
2704 template <class T> struct S {};
2705 template <> struct S<int> { void f(); }
2706 template <> void S<int>::f();
2707
2708 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2709 invalid; there should be no template <>.)
2710
2711 If the function is a specialization, it is marked as such via
2712 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2713 is set up correctly, and it is added to the list of specializations
2714 for that template. */
2715
2716 tree
2717 check_explicit_specialization (tree declarator,
2718 tree decl,
2719 int template_count,
2720 int flags,
2721 tree attrlist)
2722 {
2723 int have_def = flags & 2;
2724 int is_friend = flags & 4;
2725 bool is_concept = flags & 8;
2726 int specialization = 0;
2727 int explicit_instantiation = 0;
2728 int member_specialization = 0;
2729 tree ctype = DECL_CLASS_CONTEXT (decl);
2730 tree dname = DECL_NAME (decl);
2731 tmpl_spec_kind tsk;
2732
2733 if (is_friend)
2734 {
2735 if (!processing_specialization)
2736 tsk = tsk_none;
2737 else
2738 tsk = tsk_excessive_parms;
2739 }
2740 else
2741 tsk = current_tmpl_spec_kind (template_count);
2742
2743 switch (tsk)
2744 {
2745 case tsk_none:
2746 if (processing_specialization && !VAR_P (decl))
2747 {
2748 specialization = 1;
2749 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2750 }
2751 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2752 {
2753 if (is_friend)
2754 /* This could be something like:
2755
2756 template <class T> void f(T);
2757 class S { friend void f<>(int); } */
2758 specialization = 1;
2759 else
2760 {
2761 /* This case handles bogus declarations like template <>
2762 template <class T> void f<int>(); */
2763
2764 error ("template-id %qD in declaration of primary template",
2765 declarator);
2766 return decl;
2767 }
2768 }
2769 break;
2770
2771 case tsk_invalid_member_spec:
2772 /* The error has already been reported in
2773 check_specialization_scope. */
2774 return error_mark_node;
2775
2776 case tsk_invalid_expl_inst:
2777 error ("template parameter list used in explicit instantiation");
2778
2779 /* Fall through. */
2780
2781 case tsk_expl_inst:
2782 if (have_def)
2783 error ("definition provided for explicit instantiation");
2784
2785 explicit_instantiation = 1;
2786 break;
2787
2788 case tsk_excessive_parms:
2789 case tsk_insufficient_parms:
2790 if (tsk == tsk_excessive_parms)
2791 error ("too many template parameter lists in declaration of %qD",
2792 decl);
2793 else if (template_header_count)
2794 error("too few template parameter lists in declaration of %qD", decl);
2795 else
2796 error("explicit specialization of %qD must be introduced by "
2797 "%<template <>%>", decl);
2798
2799 /* Fall through. */
2800 case tsk_expl_spec:
2801 if (is_concept)
2802 error ("explicit specialization declared %<concept%>");
2803
2804 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2805 /* In cases like template<> constexpr bool v = true;
2806 We'll give an error in check_template_variable. */
2807 break;
2808
2809 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2810 if (ctype)
2811 member_specialization = 1;
2812 else
2813 specialization = 1;
2814 break;
2815
2816 case tsk_template:
2817 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2818 {
2819 /* This case handles bogus declarations like template <>
2820 template <class T> void f<int>(); */
2821
2822 if (!uses_template_parms (declarator))
2823 error ("template-id %qD in declaration of primary template",
2824 declarator);
2825 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2826 {
2827 /* Partial specialization of variable template. */
2828 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2829 specialization = 1;
2830 goto ok;
2831 }
2832 else if (cxx_dialect < cxx14)
2833 error ("non-type partial specialization %qD "
2834 "is not allowed", declarator);
2835 else
2836 error ("non-class, non-variable partial specialization %qD "
2837 "is not allowed", declarator);
2838 return decl;
2839 ok:;
2840 }
2841
2842 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2843 /* This is a specialization of a member template, without
2844 specialization the containing class. Something like:
2845
2846 template <class T> struct S {
2847 template <class U> void f (U);
2848 };
2849 template <> template <class U> void S<int>::f(U) {}
2850
2851 That's a specialization -- but of the entire template. */
2852 specialization = 1;
2853 break;
2854
2855 default:
2856 gcc_unreachable ();
2857 }
2858
2859 if ((specialization || member_specialization)
2860 /* This doesn't apply to variable templates. */
2861 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2862 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2863 {
2864 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2865 for (; t; t = TREE_CHAIN (t))
2866 if (TREE_PURPOSE (t))
2867 {
2868 permerror (input_location,
2869 "default argument specified in explicit specialization");
2870 break;
2871 }
2872 }
2873
2874 if (specialization || member_specialization || explicit_instantiation)
2875 {
2876 tree tmpl = NULL_TREE;
2877 tree targs = NULL_TREE;
2878 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2879
2880 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2881 if (!was_template_id)
2882 {
2883 tree fns;
2884
2885 gcc_assert (identifier_p (declarator));
2886 if (ctype)
2887 fns = dname;
2888 else
2889 {
2890 /* If there is no class context, the explicit instantiation
2891 must be at namespace scope. */
2892 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2893
2894 /* Find the namespace binding, using the declaration
2895 context. */
2896 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2897 false, true);
2898 if (fns == error_mark_node)
2899 /* If lookup fails, look for a friend declaration so we can
2900 give a better diagnostic. */
2901 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2902 /*type*/false, /*complain*/true,
2903 /*hidden*/true);
2904
2905 if (fns == error_mark_node || !is_overloaded_fn (fns))
2906 {
2907 error ("%qD is not a template function", dname);
2908 fns = error_mark_node;
2909 }
2910 }
2911
2912 declarator = lookup_template_function (fns, NULL_TREE);
2913 }
2914
2915 if (declarator == error_mark_node)
2916 return error_mark_node;
2917
2918 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2919 {
2920 if (!explicit_instantiation)
2921 /* A specialization in class scope. This is invalid,
2922 but the error will already have been flagged by
2923 check_specialization_scope. */
2924 return error_mark_node;
2925 else
2926 {
2927 /* It's not valid to write an explicit instantiation in
2928 class scope, e.g.:
2929
2930 class C { template void f(); }
2931
2932 This case is caught by the parser. However, on
2933 something like:
2934
2935 template class C { void f(); };
2936
2937 (which is invalid) we can get here. The error will be
2938 issued later. */
2939 ;
2940 }
2941
2942 return decl;
2943 }
2944 else if (ctype != NULL_TREE
2945 && (identifier_p (TREE_OPERAND (declarator, 0))))
2946 {
2947 // We'll match variable templates in start_decl.
2948 if (VAR_P (decl))
2949 return decl;
2950
2951 /* Find the list of functions in ctype that have the same
2952 name as the declared function. */
2953 tree name = TREE_OPERAND (declarator, 0);
2954
2955 if (constructor_name_p (name, ctype))
2956 {
2957 if (DECL_CONSTRUCTOR_P (decl)
2958 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2959 : !CLASSTYPE_DESTRUCTOR (ctype))
2960 {
2961 /* From [temp.expl.spec]:
2962
2963 If such an explicit specialization for the member
2964 of a class template names an implicitly-declared
2965 special member function (clause _special_), the
2966 program is ill-formed.
2967
2968 Similar language is found in [temp.explicit]. */
2969 error ("specialization of implicitly-declared special member function");
2970 return error_mark_node;
2971 }
2972
2973 name = DECL_NAME (decl);
2974 }
2975
2976 /* For a type-conversion operator, We might be looking for
2977 `operator int' which will be a specialization of
2978 `operator T'. Grab all the conversion operators, and
2979 then select from them. */
2980 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2981 ? conv_op_identifier : name);
2982
2983 if (fns == NULL_TREE)
2984 {
2985 error ("no member function %qD declared in %qT", name, ctype);
2986 return error_mark_node;
2987 }
2988 else
2989 TREE_OPERAND (declarator, 0) = fns;
2990 }
2991
2992 /* Figure out what exactly is being specialized at this point.
2993 Note that for an explicit instantiation, even one for a
2994 member function, we cannot tell a priori whether the
2995 instantiation is for a member template, or just a member
2996 function of a template class. Even if a member template is
2997 being instantiated, the member template arguments may be
2998 elided if they can be deduced from the rest of the
2999 declaration. */
3000 tmpl = determine_specialization (declarator, decl,
3001 &targs,
3002 member_specialization,
3003 template_count,
3004 tsk);
3005
3006 if (!tmpl || tmpl == error_mark_node)
3007 /* We couldn't figure out what this declaration was
3008 specializing. */
3009 return error_mark_node;
3010 else
3011 {
3012 if (TREE_CODE (decl) == FUNCTION_DECL
3013 && DECL_HIDDEN_FRIEND_P (tmpl))
3014 {
3015 auto_diagnostic_group d;
3016 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3017 "friend declaration %qD is not visible to "
3018 "explicit specialization", tmpl))
3019 inform (DECL_SOURCE_LOCATION (tmpl),
3020 "friend declaration here");
3021 }
3022 else if (!ctype && !is_friend
3023 && CP_DECL_CONTEXT (decl) == current_namespace)
3024 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3025
3026 tree gen_tmpl = most_general_template (tmpl);
3027
3028 if (explicit_instantiation)
3029 {
3030 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3031 is done by do_decl_instantiation later. */
3032
3033 int arg_depth = TMPL_ARGS_DEPTH (targs);
3034 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3035
3036 if (arg_depth > parm_depth)
3037 {
3038 /* If TMPL is not the most general template (for
3039 example, if TMPL is a friend template that is
3040 injected into namespace scope), then there will
3041 be too many levels of TARGS. Remove some of them
3042 here. */
3043 int i;
3044 tree new_targs;
3045
3046 new_targs = make_tree_vec (parm_depth);
3047 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3048 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3049 = TREE_VEC_ELT (targs, i);
3050 targs = new_targs;
3051 }
3052
3053 return instantiate_template (tmpl, targs, tf_error);
3054 }
3055
3056 /* If we thought that the DECL was a member function, but it
3057 turns out to be specializing a static member function,
3058 make DECL a static member function as well. */
3059 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3060 && DECL_STATIC_FUNCTION_P (tmpl)
3061 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3062 revert_static_member_fn (decl);
3063
3064 /* If this is a specialization of a member template of a
3065 template class, we want to return the TEMPLATE_DECL, not
3066 the specialization of it. */
3067 if (tsk == tsk_template && !was_template_id)
3068 {
3069 tree result = DECL_TEMPLATE_RESULT (tmpl);
3070 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3071 DECL_INITIAL (result) = NULL_TREE;
3072 if (have_def)
3073 {
3074 tree parm;
3075 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3076 DECL_SOURCE_LOCATION (result)
3077 = DECL_SOURCE_LOCATION (decl);
3078 /* We want to use the argument list specified in the
3079 definition, not in the original declaration. */
3080 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3081 for (parm = DECL_ARGUMENTS (result); parm;
3082 parm = DECL_CHAIN (parm))
3083 DECL_CONTEXT (parm) = result;
3084 }
3085 return register_specialization (tmpl, gen_tmpl, targs,
3086 is_friend, 0);
3087 }
3088
3089 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3090 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3091
3092 if (was_template_id)
3093 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3094
3095 /* Inherit default function arguments from the template
3096 DECL is specializing. */
3097 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3098 copy_default_args_to_explicit_spec (decl);
3099
3100 /* This specialization has the same protection as the
3101 template it specializes. */
3102 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3103 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3104
3105 /* 7.1.1-1 [dcl.stc]
3106
3107 A storage-class-specifier shall not be specified in an
3108 explicit specialization...
3109
3110 The parser rejects these, so unless action is taken here,
3111 explicit function specializations will always appear with
3112 global linkage.
3113
3114 The action recommended by the C++ CWG in response to C++
3115 defect report 605 is to make the storage class and linkage
3116 of the explicit specialization match the templated function:
3117
3118 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3119 */
3120 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3121 {
3122 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3123 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3124
3125 /* A concept cannot be specialized. */
3126 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3127 {
3128 error ("explicit specialization of function concept %qD",
3129 gen_tmpl);
3130 return error_mark_node;
3131 }
3132
3133 /* This specialization has the same linkage and visibility as
3134 the function template it specializes. */
3135 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3136 if (! TREE_PUBLIC (decl))
3137 {
3138 DECL_INTERFACE_KNOWN (decl) = 1;
3139 DECL_NOT_REALLY_EXTERN (decl) = 1;
3140 }
3141 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3142 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3143 {
3144 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3145 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3146 }
3147 }
3148
3149 /* If DECL is a friend declaration, declared using an
3150 unqualified name, the namespace associated with DECL may
3151 have been set incorrectly. For example, in:
3152
3153 template <typename T> void f(T);
3154 namespace N {
3155 struct S { friend void f<int>(int); }
3156 }
3157
3158 we will have set the DECL_CONTEXT for the friend
3159 declaration to N, rather than to the global namespace. */
3160 if (DECL_NAMESPACE_SCOPE_P (decl))
3161 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3162
3163 if (is_friend && !have_def)
3164 /* This is not really a declaration of a specialization.
3165 It's just the name of an instantiation. But, it's not
3166 a request for an instantiation, either. */
3167 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3168 else if (TREE_CODE (decl) == FUNCTION_DECL)
3169 /* A specialization is not necessarily COMDAT. */
3170 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3171 && DECL_DECLARED_INLINE_P (decl));
3172 else if (VAR_P (decl))
3173 DECL_COMDAT (decl) = false;
3174
3175 /* If this is a full specialization, register it so that we can find
3176 it again. Partial specializations will be registered in
3177 process_partial_specialization. */
3178 if (!processing_template_decl)
3179 {
3180 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3181
3182 decl = register_specialization (decl, gen_tmpl, targs,
3183 is_friend, 0);
3184 }
3185
3186
3187 /* A 'structor should already have clones. */
3188 gcc_assert (decl == error_mark_node
3189 || variable_template_p (tmpl)
3190 || !(DECL_CONSTRUCTOR_P (decl)
3191 || DECL_DESTRUCTOR_P (decl))
3192 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3193 }
3194 }
3195
3196 return decl;
3197 }
3198
3199 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3200 parameters. These are represented in the same format used for
3201 DECL_TEMPLATE_PARMS. */
3202
3203 int
3204 comp_template_parms (const_tree parms1, const_tree parms2)
3205 {
3206 const_tree p1;
3207 const_tree p2;
3208
3209 if (parms1 == parms2)
3210 return 1;
3211
3212 for (p1 = parms1, p2 = parms2;
3213 p1 != NULL_TREE && p2 != NULL_TREE;
3214 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3215 {
3216 tree t1 = TREE_VALUE (p1);
3217 tree t2 = TREE_VALUE (p2);
3218 int i;
3219
3220 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3221 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3222
3223 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3224 return 0;
3225
3226 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3227 {
3228 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3229 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3230
3231 /* If either of the template parameters are invalid, assume
3232 they match for the sake of error recovery. */
3233 if (error_operand_p (parm1) || error_operand_p (parm2))
3234 return 1;
3235
3236 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3237 return 0;
3238
3239 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3240 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3241 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3242 continue;
3243 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3244 return 0;
3245 }
3246 }
3247
3248 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3249 /* One set of parameters has more parameters lists than the
3250 other. */
3251 return 0;
3252
3253 return 1;
3254 }
3255
3256 /* Determine whether PARM is a parameter pack. */
3257
3258 bool
3259 template_parameter_pack_p (const_tree parm)
3260 {
3261 /* Determine if we have a non-type template parameter pack. */
3262 if (TREE_CODE (parm) == PARM_DECL)
3263 return (DECL_TEMPLATE_PARM_P (parm)
3264 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3265 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3266 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3267
3268 /* If this is a list of template parameters, we could get a
3269 TYPE_DECL or a TEMPLATE_DECL. */
3270 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3271 parm = TREE_TYPE (parm);
3272
3273 /* Otherwise it must be a type template parameter. */
3274 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3275 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3276 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3277 }
3278
3279 /* Determine if T is a function parameter pack. */
3280
3281 bool
3282 function_parameter_pack_p (const_tree t)
3283 {
3284 if (t && TREE_CODE (t) == PARM_DECL)
3285 return DECL_PACK_P (t);
3286 return false;
3287 }
3288
3289 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3290 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3291
3292 tree
3293 get_function_template_decl (const_tree primary_func_tmpl_inst)
3294 {
3295 if (! primary_func_tmpl_inst
3296 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3297 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3298 return NULL;
3299
3300 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3301 }
3302
3303 /* Return true iff the function parameter PARAM_DECL was expanded
3304 from the function parameter pack PACK. */
3305
3306 bool
3307 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3308 {
3309 if (DECL_ARTIFICIAL (param_decl)
3310 || !function_parameter_pack_p (pack))
3311 return false;
3312
3313 /* The parameter pack and its pack arguments have the same
3314 DECL_PARM_INDEX. */
3315 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3316 }
3317
3318 /* Determine whether ARGS describes a variadic template args list,
3319 i.e., one that is terminated by a template argument pack. */
3320
3321 static bool
3322 template_args_variadic_p (tree args)
3323 {
3324 int nargs;
3325 tree last_parm;
3326
3327 if (args == NULL_TREE)
3328 return false;
3329
3330 args = INNERMOST_TEMPLATE_ARGS (args);
3331 nargs = TREE_VEC_LENGTH (args);
3332
3333 if (nargs == 0)
3334 return false;
3335
3336 last_parm = TREE_VEC_ELT (args, nargs - 1);
3337
3338 return ARGUMENT_PACK_P (last_parm);
3339 }
3340
3341 /* Generate a new name for the parameter pack name NAME (an
3342 IDENTIFIER_NODE) that incorporates its */
3343
3344 static tree
3345 make_ith_pack_parameter_name (tree name, int i)
3346 {
3347 /* Munge the name to include the parameter index. */
3348 #define NUMBUF_LEN 128
3349 char numbuf[NUMBUF_LEN];
3350 char* newname;
3351 int newname_len;
3352
3353 if (name == NULL_TREE)
3354 return name;
3355 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3356 newname_len = IDENTIFIER_LENGTH (name)
3357 + strlen (numbuf) + 2;
3358 newname = (char*)alloca (newname_len);
3359 snprintf (newname, newname_len,
3360 "%s#%i", IDENTIFIER_POINTER (name), i);
3361 return get_identifier (newname);
3362 }
3363
3364 /* Return true if T is a primary function, class or alias template
3365 specialization, not including the template pattern. */
3366
3367 bool
3368 primary_template_specialization_p (const_tree t)
3369 {
3370 if (!t)
3371 return false;
3372
3373 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3374 return (DECL_LANG_SPECIFIC (t)
3375 && DECL_USE_TEMPLATE (t)
3376 && DECL_TEMPLATE_INFO (t)
3377 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3378 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3379 return (CLASSTYPE_TEMPLATE_INFO (t)
3380 && CLASSTYPE_USE_TEMPLATE (t)
3381 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3382 else if (alias_template_specialization_p (t))
3383 return true;
3384 return false;
3385 }
3386
3387 /* Return true if PARM is a template template parameter. */
3388
3389 bool
3390 template_template_parameter_p (const_tree parm)
3391 {
3392 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3393 }
3394
3395 /* Return true iff PARM is a DECL representing a type template
3396 parameter. */
3397
3398 bool
3399 template_type_parameter_p (const_tree parm)
3400 {
3401 return (parm
3402 && (TREE_CODE (parm) == TYPE_DECL
3403 || TREE_CODE (parm) == TEMPLATE_DECL)
3404 && DECL_TEMPLATE_PARM_P (parm));
3405 }
3406
3407 /* Return the template parameters of T if T is a
3408 primary template instantiation, NULL otherwise. */
3409
3410 tree
3411 get_primary_template_innermost_parameters (const_tree t)
3412 {
3413 tree parms = NULL, template_info = NULL;
3414
3415 if ((template_info = get_template_info (t))
3416 && primary_template_specialization_p (t))
3417 parms = INNERMOST_TEMPLATE_PARMS
3418 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3419
3420 return parms;
3421 }
3422
3423 /* Return the template parameters of the LEVELth level from the full list
3424 of template parameters PARMS. */
3425
3426 tree
3427 get_template_parms_at_level (tree parms, int level)
3428 {
3429 tree p;
3430 if (!parms
3431 || TREE_CODE (parms) != TREE_LIST
3432 || level > TMPL_PARMS_DEPTH (parms))
3433 return NULL_TREE;
3434
3435 for (p = parms; p; p = TREE_CHAIN (p))
3436 if (TMPL_PARMS_DEPTH (p) == level)
3437 return p;
3438
3439 return NULL_TREE;
3440 }
3441
3442 /* Returns the template arguments of T if T is a template instantiation,
3443 NULL otherwise. */
3444
3445 tree
3446 get_template_innermost_arguments (const_tree t)
3447 {
3448 tree args = NULL, template_info = NULL;
3449
3450 if ((template_info = get_template_info (t))
3451 && TI_ARGS (template_info))
3452 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3453
3454 return args;
3455 }
3456
3457 /* Return the argument pack elements of T if T is a template argument pack,
3458 NULL otherwise. */
3459
3460 tree
3461 get_template_argument_pack_elems (const_tree t)
3462 {
3463 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3464 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3465 return NULL;
3466
3467 return ARGUMENT_PACK_ARGS (t);
3468 }
3469
3470 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3471 ARGUMENT_PACK_SELECT represents. */
3472
3473 static tree
3474 argument_pack_select_arg (tree t)
3475 {
3476 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3477 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3478
3479 /* If the selected argument is an expansion E, that most likely means we were
3480 called from gen_elem_of_pack_expansion_instantiation during the
3481 substituting of an argument pack (of which the Ith element is a pack
3482 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3483 In this case, the Ith element resulting from this substituting is going to
3484 be a pack expansion, which pattern is the pattern of E. Let's return the
3485 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3486 resulting pack expansion from it. */
3487 if (PACK_EXPANSION_P (arg))
3488 {
3489 /* Make sure we aren't throwing away arg info. */
3490 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3491 arg = PACK_EXPANSION_PATTERN (arg);
3492 }
3493
3494 return arg;
3495 }
3496
3497
3498 /* True iff FN is a function representing a built-in variadic parameter
3499 pack. */
3500
3501 bool
3502 builtin_pack_fn_p (tree fn)
3503 {
3504 if (!fn
3505 || TREE_CODE (fn) != FUNCTION_DECL
3506 || !DECL_IS_BUILTIN (fn))
3507 return false;
3508
3509 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3510 return true;
3511
3512 return false;
3513 }
3514
3515 /* True iff CALL is a call to a function representing a built-in variadic
3516 parameter pack. */
3517
3518 static bool
3519 builtin_pack_call_p (tree call)
3520 {
3521 if (TREE_CODE (call) != CALL_EXPR)
3522 return false;
3523 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3524 }
3525
3526 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3527
3528 static tree
3529 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3530 tree in_decl)
3531 {
3532 tree ohi = CALL_EXPR_ARG (call, 0);
3533 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3534 false/*fn*/, true/*int_cst*/);
3535
3536 if (value_dependent_expression_p (hi))
3537 {
3538 if (hi != ohi)
3539 {
3540 call = copy_node (call);
3541 CALL_EXPR_ARG (call, 0) = hi;
3542 }
3543 tree ex = make_pack_expansion (call, complain);
3544 tree vec = make_tree_vec (1);
3545 TREE_VEC_ELT (vec, 0) = ex;
3546 return vec;
3547 }
3548 else
3549 {
3550 hi = cxx_constant_value (hi);
3551 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3552
3553 /* Calculate the largest value of len that won't make the size of the vec
3554 overflow an int. The compiler will exceed resource limits long before
3555 this, but it seems a decent place to diagnose. */
3556 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3557
3558 if (len < 0 || len > max)
3559 {
3560 if ((complain & tf_error)
3561 && hi != error_mark_node)
3562 error ("argument to __integer_pack must be between 0 and %d", max);
3563 return error_mark_node;
3564 }
3565
3566 tree vec = make_tree_vec (len);
3567
3568 for (int i = 0; i < len; ++i)
3569 TREE_VEC_ELT (vec, i) = size_int (i);
3570
3571 return vec;
3572 }
3573 }
3574
3575 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3576 CALL. */
3577
3578 static tree
3579 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3580 tree in_decl)
3581 {
3582 if (!builtin_pack_call_p (call))
3583 return NULL_TREE;
3584
3585 tree fn = CALL_EXPR_FN (call);
3586
3587 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3588 return expand_integer_pack (call, args, complain, in_decl);
3589
3590 return NULL_TREE;
3591 }
3592
3593 /* Structure used to track the progress of find_parameter_packs_r. */
3594 struct find_parameter_pack_data
3595 {
3596 /* TREE_LIST that will contain all of the parameter packs found by
3597 the traversal. */
3598 tree* parameter_packs;
3599
3600 /* Set of AST nodes that have been visited by the traversal. */
3601 hash_set<tree> *visited;
3602
3603 /* True iff we're making a type pack expansion. */
3604 bool type_pack_expansion_p;
3605 };
3606
3607 /* Identifies all of the argument packs that occur in a template
3608 argument and appends them to the TREE_LIST inside DATA, which is a
3609 find_parameter_pack_data structure. This is a subroutine of
3610 make_pack_expansion and uses_parameter_packs. */
3611 static tree
3612 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3613 {
3614 tree t = *tp;
3615 struct find_parameter_pack_data* ppd =
3616 (struct find_parameter_pack_data*)data;
3617 bool parameter_pack_p = false;
3618
3619 /* Handle type aliases/typedefs. */
3620 if (TYPE_ALIAS_P (t))
3621 {
3622 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3623 cp_walk_tree (&TI_ARGS (tinfo),
3624 &find_parameter_packs_r,
3625 ppd, ppd->visited);
3626 *walk_subtrees = 0;
3627 return NULL_TREE;
3628 }
3629
3630 /* Identify whether this is a parameter pack or not. */
3631 switch (TREE_CODE (t))
3632 {
3633 case TEMPLATE_PARM_INDEX:
3634 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3635 parameter_pack_p = true;
3636 break;
3637
3638 case TEMPLATE_TYPE_PARM:
3639 t = TYPE_MAIN_VARIANT (t);
3640 /* FALLTHRU */
3641 case TEMPLATE_TEMPLATE_PARM:
3642 /* If the placeholder appears in the decl-specifier-seq of a function
3643 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3644 is a pack expansion, the invented template parameter is a template
3645 parameter pack. */
3646 if (ppd->type_pack_expansion_p && is_auto (t))
3647 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3648 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3649 parameter_pack_p = true;
3650 break;
3651
3652 case FIELD_DECL:
3653 case PARM_DECL:
3654 if (DECL_PACK_P (t))
3655 {
3656 /* We don't want to walk into the type of a PARM_DECL,
3657 because we don't want to see the type parameter pack. */
3658 *walk_subtrees = 0;
3659 parameter_pack_p = true;
3660 }
3661 break;
3662
3663 case VAR_DECL:
3664 if (DECL_PACK_P (t))
3665 {
3666 /* We don't want to walk into the type of a variadic capture proxy,
3667 because we don't want to see the type parameter pack. */
3668 *walk_subtrees = 0;
3669 parameter_pack_p = true;
3670 }
3671 else if (variable_template_specialization_p (t))
3672 {
3673 cp_walk_tree (&DECL_TI_ARGS (t),
3674 find_parameter_packs_r,
3675 ppd, ppd->visited);
3676 *walk_subtrees = 0;
3677 }
3678 break;
3679
3680 case CALL_EXPR:
3681 if (builtin_pack_call_p (t))
3682 parameter_pack_p = true;
3683 break;
3684
3685 case BASES:
3686 parameter_pack_p = true;
3687 break;
3688 default:
3689 /* Not a parameter pack. */
3690 break;
3691 }
3692
3693 if (parameter_pack_p)
3694 {
3695 /* Add this parameter pack to the list. */
3696 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3697 }
3698
3699 if (TYPE_P (t))
3700 cp_walk_tree (&TYPE_CONTEXT (t),
3701 &find_parameter_packs_r, ppd, ppd->visited);
3702
3703 /* This switch statement will return immediately if we don't find a
3704 parameter pack. */
3705 switch (TREE_CODE (t))
3706 {
3707 case TEMPLATE_PARM_INDEX:
3708 return NULL_TREE;
3709
3710 case BOUND_TEMPLATE_TEMPLATE_PARM:
3711 /* Check the template itself. */
3712 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3713 &find_parameter_packs_r, ppd, ppd->visited);
3714 /* Check the template arguments. */
3715 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3716 ppd->visited);
3717 *walk_subtrees = 0;
3718 return NULL_TREE;
3719
3720 case TEMPLATE_TYPE_PARM:
3721 case TEMPLATE_TEMPLATE_PARM:
3722 return NULL_TREE;
3723
3724 case PARM_DECL:
3725 return NULL_TREE;
3726
3727 case DECL_EXPR:
3728 /* Ignore the declaration of a capture proxy for a parameter pack. */
3729 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3730 *walk_subtrees = 0;
3731 return NULL_TREE;
3732
3733 case RECORD_TYPE:
3734 if (TYPE_PTRMEMFUNC_P (t))
3735 return NULL_TREE;
3736 /* Fall through. */
3737
3738 case UNION_TYPE:
3739 case ENUMERAL_TYPE:
3740 if (TYPE_TEMPLATE_INFO (t))
3741 cp_walk_tree (&TYPE_TI_ARGS (t),
3742 &find_parameter_packs_r, ppd, ppd->visited);
3743
3744 *walk_subtrees = 0;
3745 return NULL_TREE;
3746
3747 case TEMPLATE_DECL:
3748 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3749 return NULL_TREE;
3750 gcc_fallthrough();
3751
3752 case CONSTRUCTOR:
3753 cp_walk_tree (&TREE_TYPE (t),
3754 &find_parameter_packs_r, ppd, ppd->visited);
3755 return NULL_TREE;
3756
3757 case TYPENAME_TYPE:
3758 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3759 ppd, ppd->visited);
3760 *walk_subtrees = 0;
3761 return NULL_TREE;
3762
3763 case TYPE_PACK_EXPANSION:
3764 case EXPR_PACK_EXPANSION:
3765 *walk_subtrees = 0;
3766 return NULL_TREE;
3767
3768 case INTEGER_TYPE:
3769 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3770 ppd, ppd->visited);
3771 *walk_subtrees = 0;
3772 return NULL_TREE;
3773
3774 case IDENTIFIER_NODE:
3775 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3776 ppd->visited);
3777 *walk_subtrees = 0;
3778 return NULL_TREE;
3779
3780 case LAMBDA_EXPR:
3781 {
3782 /* Look at explicit captures. */
3783 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3784 cap; cap = TREE_CHAIN (cap))
3785 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3786 ppd->visited);
3787 /* Since we defer implicit capture, look in the parms and body. */
3788 tree fn = lambda_function (t);
3789 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3790 ppd->visited);
3791 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3792 ppd->visited);
3793 *walk_subtrees = 0;
3794 return NULL_TREE;
3795 }
3796
3797 case DECLTYPE_TYPE:
3798 {
3799 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3800 type_pack_expansion_p to false so that any placeholders
3801 within the expression don't get marked as parameter packs. */
3802 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3803 ppd->type_pack_expansion_p = false;
3804 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3805 ppd, ppd->visited);
3806 ppd->type_pack_expansion_p = type_pack_expansion_p;
3807 *walk_subtrees = 0;
3808 return NULL_TREE;
3809 }
3810
3811 case IF_STMT:
3812 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3813 ppd, ppd->visited);
3814 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3815 ppd, ppd->visited);
3816 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3817 ppd, ppd->visited);
3818 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3819 *walk_subtrees = 0;
3820 return NULL_TREE;
3821
3822 default:
3823 return NULL_TREE;
3824 }
3825
3826 return NULL_TREE;
3827 }
3828
3829 /* Determines if the expression or type T uses any parameter packs. */
3830 bool
3831 uses_parameter_packs (tree t)
3832 {
3833 tree parameter_packs = NULL_TREE;
3834 struct find_parameter_pack_data ppd;
3835 ppd.parameter_packs = &parameter_packs;
3836 ppd.visited = new hash_set<tree>;
3837 ppd.type_pack_expansion_p = false;
3838 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3839 delete ppd.visited;
3840 return parameter_packs != NULL_TREE;
3841 }
3842
3843 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3844 representation a base-class initializer into a parameter pack
3845 expansion. If all goes well, the resulting node will be an
3846 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3847 respectively. */
3848 tree
3849 make_pack_expansion (tree arg, tsubst_flags_t complain)
3850 {
3851 tree result;
3852 tree parameter_packs = NULL_TREE;
3853 bool for_types = false;
3854 struct find_parameter_pack_data ppd;
3855
3856 if (!arg || arg == error_mark_node)
3857 return arg;
3858
3859 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3860 {
3861 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3862 class initializer. In this case, the TREE_PURPOSE will be a
3863 _TYPE node (representing the base class expansion we're
3864 initializing) and the TREE_VALUE will be a TREE_LIST
3865 containing the initialization arguments.
3866
3867 The resulting expansion looks somewhat different from most
3868 expansions. Rather than returning just one _EXPANSION, we
3869 return a TREE_LIST whose TREE_PURPOSE is a
3870 TYPE_PACK_EXPANSION containing the bases that will be
3871 initialized. The TREE_VALUE will be identical to the
3872 original TREE_VALUE, which is a list of arguments that will
3873 be passed to each base. We do not introduce any new pack
3874 expansion nodes into the TREE_VALUE (although it is possible
3875 that some already exist), because the TREE_PURPOSE and
3876 TREE_VALUE all need to be expanded together with the same
3877 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3878 resulting TREE_PURPOSE will mention the parameter packs in
3879 both the bases and the arguments to the bases. */
3880 tree purpose;
3881 tree value;
3882 tree parameter_packs = NULL_TREE;
3883
3884 /* Determine which parameter packs will be used by the base
3885 class expansion. */
3886 ppd.visited = new hash_set<tree>;
3887 ppd.parameter_packs = &parameter_packs;
3888 ppd.type_pack_expansion_p = true;
3889 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3890 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3891 &ppd, ppd.visited);
3892
3893 if (parameter_packs == NULL_TREE)
3894 {
3895 if (complain & tf_error)
3896 error ("base initializer expansion %qT contains no parameter packs",
3897 arg);
3898 delete ppd.visited;
3899 return error_mark_node;
3900 }
3901
3902 if (TREE_VALUE (arg) != void_type_node)
3903 {
3904 /* Collect the sets of parameter packs used in each of the
3905 initialization arguments. */
3906 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3907 {
3908 /* Determine which parameter packs will be expanded in this
3909 argument. */
3910 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3911 &ppd, ppd.visited);
3912 }
3913 }
3914
3915 delete ppd.visited;
3916
3917 /* Create the pack expansion type for the base type. */
3918 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3919 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3920 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3921 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3922
3923 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3924 they will rarely be compared to anything. */
3925 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3926
3927 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3928 }
3929
3930 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3931 for_types = true;
3932
3933 /* Build the PACK_EXPANSION_* node. */
3934 result = for_types
3935 ? cxx_make_type (TYPE_PACK_EXPANSION)
3936 : make_node (EXPR_PACK_EXPANSION);
3937 SET_PACK_EXPANSION_PATTERN (result, arg);
3938 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3939 {
3940 /* Propagate type and const-expression information. */
3941 TREE_TYPE (result) = TREE_TYPE (arg);
3942 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3943 /* Mark this read now, since the expansion might be length 0. */
3944 mark_exp_read (arg);
3945 }
3946 else
3947 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3948 they will rarely be compared to anything. */
3949 SET_TYPE_STRUCTURAL_EQUALITY (result);
3950
3951 /* Determine which parameter packs will be expanded. */
3952 ppd.parameter_packs = &parameter_packs;
3953 ppd.visited = new hash_set<tree>;
3954 ppd.type_pack_expansion_p = TYPE_P (arg);
3955 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3956 delete ppd.visited;
3957
3958 /* Make sure we found some parameter packs. */
3959 if (parameter_packs == NULL_TREE)
3960 {
3961 if (complain & tf_error)
3962 {
3963 if (TYPE_P (arg))
3964 error ("expansion pattern %qT contains no parameter packs", arg);
3965 else
3966 error ("expansion pattern %qE contains no parameter packs", arg);
3967 }
3968 return error_mark_node;
3969 }
3970 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3971
3972 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3973
3974 return result;
3975 }
3976
3977 /* Checks T for any "bare" parameter packs, which have not yet been
3978 expanded, and issues an error if any are found. This operation can
3979 only be done on full expressions or types (e.g., an expression
3980 statement, "if" condition, etc.), because we could have expressions like:
3981
3982 foo(f(g(h(args)))...)
3983
3984 where "args" is a parameter pack. check_for_bare_parameter_packs
3985 should not be called for the subexpressions args, h(args),
3986 g(h(args)), or f(g(h(args))), because we would produce erroneous
3987 error messages.
3988
3989 Returns TRUE and emits an error if there were bare parameter packs,
3990 returns FALSE otherwise. */
3991 bool
3992 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
3993 {
3994 tree parameter_packs = NULL_TREE;
3995 struct find_parameter_pack_data ppd;
3996
3997 if (!processing_template_decl || !t || t == error_mark_node)
3998 return false;
3999
4000 /* A lambda might use a parameter pack from the containing context. */
4001 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4002 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4003 return false;
4004
4005 if (TREE_CODE (t) == TYPE_DECL)
4006 t = TREE_TYPE (t);
4007
4008 ppd.parameter_packs = &parameter_packs;
4009 ppd.visited = new hash_set<tree>;
4010 ppd.type_pack_expansion_p = false;
4011 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4012 delete ppd.visited;
4013
4014 if (parameter_packs)
4015 {
4016 if (loc == UNKNOWN_LOCATION)
4017 loc = cp_expr_loc_or_loc (t, input_location);
4018 error_at (loc, "parameter packs not expanded with %<...%>:");
4019 while (parameter_packs)
4020 {
4021 tree pack = TREE_VALUE (parameter_packs);
4022 tree name = NULL_TREE;
4023
4024 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4025 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4026 name = TYPE_NAME (pack);
4027 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4028 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4029 else if (TREE_CODE (pack) == CALL_EXPR)
4030 name = DECL_NAME (CALL_EXPR_FN (pack));
4031 else
4032 name = DECL_NAME (pack);
4033
4034 if (name)
4035 inform (loc, " %qD", name);
4036 else
4037 inform (loc, " <anonymous>");
4038
4039 parameter_packs = TREE_CHAIN (parameter_packs);
4040 }
4041
4042 return true;
4043 }
4044
4045 return false;
4046 }
4047
4048 /* Expand any parameter packs that occur in the template arguments in
4049 ARGS. */
4050 tree
4051 expand_template_argument_pack (tree args)
4052 {
4053 if (args == error_mark_node)
4054 return error_mark_node;
4055
4056 tree result_args = NULL_TREE;
4057 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4058 int num_result_args = -1;
4059 int non_default_args_count = -1;
4060
4061 /* First, determine if we need to expand anything, and the number of
4062 slots we'll need. */
4063 for (in_arg = 0; in_arg < nargs; ++in_arg)
4064 {
4065 tree arg = TREE_VEC_ELT (args, in_arg);
4066 if (arg == NULL_TREE)
4067 return args;
4068 if (ARGUMENT_PACK_P (arg))
4069 {
4070 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4071 if (num_result_args < 0)
4072 num_result_args = in_arg + num_packed;
4073 else
4074 num_result_args += num_packed;
4075 }
4076 else
4077 {
4078 if (num_result_args >= 0)
4079 num_result_args++;
4080 }
4081 }
4082
4083 /* If no expansion is necessary, we're done. */
4084 if (num_result_args < 0)
4085 return args;
4086
4087 /* Expand arguments. */
4088 result_args = make_tree_vec (num_result_args);
4089 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4090 non_default_args_count =
4091 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4092 for (in_arg = 0; in_arg < nargs; ++in_arg)
4093 {
4094 tree arg = TREE_VEC_ELT (args, in_arg);
4095 if (ARGUMENT_PACK_P (arg))
4096 {
4097 tree packed = ARGUMENT_PACK_ARGS (arg);
4098 int i, num_packed = TREE_VEC_LENGTH (packed);
4099 for (i = 0; i < num_packed; ++i, ++out_arg)
4100 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4101 if (non_default_args_count > 0)
4102 non_default_args_count += num_packed - 1;
4103 }
4104 else
4105 {
4106 TREE_VEC_ELT (result_args, out_arg) = arg;
4107 ++out_arg;
4108 }
4109 }
4110 if (non_default_args_count >= 0)
4111 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4112 return result_args;
4113 }
4114
4115 /* Checks if DECL shadows a template parameter.
4116
4117 [temp.local]: A template-parameter shall not be redeclared within its
4118 scope (including nested scopes).
4119
4120 Emits an error and returns TRUE if the DECL shadows a parameter,
4121 returns FALSE otherwise. */
4122
4123 bool
4124 check_template_shadow (tree decl)
4125 {
4126 tree olddecl;
4127
4128 /* If we're not in a template, we can't possibly shadow a template
4129 parameter. */
4130 if (!current_template_parms)
4131 return true;
4132
4133 /* Figure out what we're shadowing. */
4134 decl = OVL_FIRST (decl);
4135 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4136
4137 /* If there's no previous binding for this name, we're not shadowing
4138 anything, let alone a template parameter. */
4139 if (!olddecl)
4140 return true;
4141
4142 /* If we're not shadowing a template parameter, we're done. Note
4143 that OLDDECL might be an OVERLOAD (or perhaps even an
4144 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4145 node. */
4146 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4147 return true;
4148
4149 /* We check for decl != olddecl to avoid bogus errors for using a
4150 name inside a class. We check TPFI to avoid duplicate errors for
4151 inline member templates. */
4152 if (decl == olddecl
4153 || (DECL_TEMPLATE_PARM_P (decl)
4154 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4155 return true;
4156
4157 /* Don't complain about the injected class name, as we've already
4158 complained about the class itself. */
4159 if (DECL_SELF_REFERENCE_P (decl))
4160 return false;
4161
4162 if (DECL_TEMPLATE_PARM_P (decl))
4163 error ("declaration of template parameter %q+D shadows "
4164 "template parameter", decl);
4165 else
4166 error ("declaration of %q+#D shadows template parameter", decl);
4167 inform (DECL_SOURCE_LOCATION (olddecl),
4168 "template parameter %qD declared here", olddecl);
4169 return false;
4170 }
4171
4172 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4173 ORIG_LEVEL, DECL, and TYPE. */
4174
4175 static tree
4176 build_template_parm_index (int index,
4177 int level,
4178 int orig_level,
4179 tree decl,
4180 tree type)
4181 {
4182 tree t = make_node (TEMPLATE_PARM_INDEX);
4183 TEMPLATE_PARM_IDX (t) = index;
4184 TEMPLATE_PARM_LEVEL (t) = level;
4185 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4186 TEMPLATE_PARM_DECL (t) = decl;
4187 TREE_TYPE (t) = type;
4188 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4189 TREE_READONLY (t) = TREE_READONLY (decl);
4190
4191 return t;
4192 }
4193
4194 /* Find the canonical type parameter for the given template type
4195 parameter. Returns the canonical type parameter, which may be TYPE
4196 if no such parameter existed. */
4197
4198 static tree
4199 canonical_type_parameter (tree type)
4200 {
4201 tree list;
4202 int idx = TEMPLATE_TYPE_IDX (type);
4203 if (!canonical_template_parms)
4204 vec_alloc (canonical_template_parms, idx + 1);
4205
4206 if (canonical_template_parms->length () <= (unsigned) idx)
4207 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4208
4209 list = (*canonical_template_parms)[idx];
4210 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4211 list = TREE_CHAIN (list);
4212
4213 if (list)
4214 return TREE_VALUE (list);
4215 else
4216 {
4217 (*canonical_template_parms)[idx]
4218 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4219 return type;
4220 }
4221 }
4222
4223 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4224 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4225 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4226 new one is created. */
4227
4228 static tree
4229 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4230 tsubst_flags_t complain)
4231 {
4232 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4233 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4234 != TEMPLATE_PARM_LEVEL (index) - levels)
4235 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4236 {
4237 tree orig_decl = TEMPLATE_PARM_DECL (index);
4238 tree decl, t;
4239
4240 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4241 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4242 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4243 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4244 DECL_ARTIFICIAL (decl) = 1;
4245 SET_DECL_TEMPLATE_PARM_P (decl);
4246
4247 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4248 TEMPLATE_PARM_LEVEL (index) - levels,
4249 TEMPLATE_PARM_ORIG_LEVEL (index),
4250 decl, type);
4251 TEMPLATE_PARM_DESCENDANTS (index) = t;
4252 TEMPLATE_PARM_PARAMETER_PACK (t)
4253 = TEMPLATE_PARM_PARAMETER_PACK (index);
4254
4255 /* Template template parameters need this. */
4256 if (TREE_CODE (decl) == TEMPLATE_DECL)
4257 {
4258 DECL_TEMPLATE_RESULT (decl)
4259 = build_decl (DECL_SOURCE_LOCATION (decl),
4260 TYPE_DECL, DECL_NAME (decl), type);
4261 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4262 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4263 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4264 }
4265 }
4266
4267 return TEMPLATE_PARM_DESCENDANTS (index);
4268 }
4269
4270 /* Process information from new template parameter PARM and append it
4271 to the LIST being built. This new parameter is a non-type
4272 parameter iff IS_NON_TYPE is true. This new parameter is a
4273 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4274 is in PARM_LOC. */
4275
4276 tree
4277 process_template_parm (tree list, location_t parm_loc, tree parm,
4278 bool is_non_type, bool is_parameter_pack)
4279 {
4280 tree decl = 0;
4281 int idx = 0;
4282
4283 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4284 tree defval = TREE_PURPOSE (parm);
4285 tree constr = TREE_TYPE (parm);
4286
4287 if (list)
4288 {
4289 tree p = tree_last (list);
4290
4291 if (p && TREE_VALUE (p) != error_mark_node)
4292 {
4293 p = TREE_VALUE (p);
4294 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4295 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4296 else
4297 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4298 }
4299
4300 ++idx;
4301 }
4302
4303 if (is_non_type)
4304 {
4305 parm = TREE_VALUE (parm);
4306
4307 SET_DECL_TEMPLATE_PARM_P (parm);
4308
4309 if (TREE_TYPE (parm) != error_mark_node)
4310 {
4311 /* [temp.param]
4312
4313 The top-level cv-qualifiers on the template-parameter are
4314 ignored when determining its type. */
4315 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4316 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4317 TREE_TYPE (parm) = error_mark_node;
4318 else if (uses_parameter_packs (TREE_TYPE (parm))
4319 && !is_parameter_pack
4320 /* If we're in a nested template parameter list, the template
4321 template parameter could be a parameter pack. */
4322 && processing_template_parmlist == 1)
4323 {
4324 /* This template parameter is not a parameter pack, but it
4325 should be. Complain about "bare" parameter packs. */
4326 check_for_bare_parameter_packs (TREE_TYPE (parm));
4327
4328 /* Recover by calling this a parameter pack. */
4329 is_parameter_pack = true;
4330 }
4331 }
4332
4333 /* A template parameter is not modifiable. */
4334 TREE_CONSTANT (parm) = 1;
4335 TREE_READONLY (parm) = 1;
4336 decl = build_decl (parm_loc,
4337 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4338 TREE_CONSTANT (decl) = 1;
4339 TREE_READONLY (decl) = 1;
4340 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4341 = build_template_parm_index (idx, processing_template_decl,
4342 processing_template_decl,
4343 decl, TREE_TYPE (parm));
4344
4345 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4346 = is_parameter_pack;
4347 }
4348 else
4349 {
4350 tree t;
4351 parm = TREE_VALUE (TREE_VALUE (parm));
4352
4353 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4354 {
4355 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4356 /* This is for distinguishing between real templates and template
4357 template parameters */
4358 TREE_TYPE (parm) = t;
4359 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4360 decl = parm;
4361 }
4362 else
4363 {
4364 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4365 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4366 decl = build_decl (parm_loc,
4367 TYPE_DECL, parm, t);
4368 }
4369
4370 TYPE_NAME (t) = decl;
4371 TYPE_STUB_DECL (t) = decl;
4372 parm = decl;
4373 TEMPLATE_TYPE_PARM_INDEX (t)
4374 = build_template_parm_index (idx, processing_template_decl,
4375 processing_template_decl,
4376 decl, TREE_TYPE (parm));
4377 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4378 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4379 }
4380 DECL_ARTIFICIAL (decl) = 1;
4381 SET_DECL_TEMPLATE_PARM_P (decl);
4382
4383 /* Build requirements for the type/template parameter.
4384 This must be done after SET_DECL_TEMPLATE_PARM_P or
4385 process_template_parm could fail. */
4386 tree reqs = finish_shorthand_constraint (parm, constr);
4387
4388 pushdecl (decl);
4389
4390 /* Build the parameter node linking the parameter declaration,
4391 its default argument (if any), and its constraints (if any). */
4392 parm = build_tree_list (defval, parm);
4393 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4394
4395 return chainon (list, parm);
4396 }
4397
4398 /* The end of a template parameter list has been reached. Process the
4399 tree list into a parameter vector, converting each parameter into a more
4400 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4401 as PARM_DECLs. */
4402
4403 tree
4404 end_template_parm_list (tree parms)
4405 {
4406 int nparms;
4407 tree parm, next;
4408 tree saved_parmlist = make_tree_vec (list_length (parms));
4409
4410 /* Pop the dummy parameter level and add the real one. */
4411 current_template_parms = TREE_CHAIN (current_template_parms);
4412
4413 current_template_parms
4414 = tree_cons (size_int (processing_template_decl),
4415 saved_parmlist, current_template_parms);
4416
4417 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4418 {
4419 next = TREE_CHAIN (parm);
4420 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4421 TREE_CHAIN (parm) = NULL_TREE;
4422 }
4423
4424 --processing_template_parmlist;
4425
4426 return saved_parmlist;
4427 }
4428
4429 // Explicitly indicate the end of the template parameter list. We assume
4430 // that the current template parameters have been constructed and/or
4431 // managed explicitly, as when creating new template template parameters
4432 // from a shorthand constraint.
4433 void
4434 end_template_parm_list ()
4435 {
4436 --processing_template_parmlist;
4437 }
4438
4439 /* end_template_decl is called after a template declaration is seen. */
4440
4441 void
4442 end_template_decl (void)
4443 {
4444 reset_specialization ();
4445
4446 if (! processing_template_decl)
4447 return;
4448
4449 /* This matches the pushlevel in begin_template_parm_list. */
4450 finish_scope ();
4451
4452 --processing_template_decl;
4453 current_template_parms = TREE_CHAIN (current_template_parms);
4454 }
4455
4456 /* Takes a TREE_LIST representing a template parameter and convert it
4457 into an argument suitable to be passed to the type substitution
4458 functions. Note that If the TREE_LIST contains an error_mark
4459 node, the returned argument is error_mark_node. */
4460
4461 tree
4462 template_parm_to_arg (tree t)
4463 {
4464
4465 if (t == NULL_TREE
4466 || TREE_CODE (t) != TREE_LIST)
4467 return t;
4468
4469 if (error_operand_p (TREE_VALUE (t)))
4470 return error_mark_node;
4471
4472 t = TREE_VALUE (t);
4473
4474 if (TREE_CODE (t) == TYPE_DECL
4475 || TREE_CODE (t) == TEMPLATE_DECL)
4476 {
4477 t = TREE_TYPE (t);
4478
4479 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4480 {
4481 /* Turn this argument into a TYPE_ARGUMENT_PACK
4482 with a single element, which expands T. */
4483 tree vec = make_tree_vec (1);
4484 if (CHECKING_P)
4485 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4486
4487 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4488
4489 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4490 SET_ARGUMENT_PACK_ARGS (t, vec);
4491 }
4492 }
4493 else
4494 {
4495 t = DECL_INITIAL (t);
4496
4497 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4498 {
4499 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4500 with a single element, which expands T. */
4501 tree vec = make_tree_vec (1);
4502 if (CHECKING_P)
4503 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4504
4505 t = convert_from_reference (t);
4506 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4507
4508 t = make_node (NONTYPE_ARGUMENT_PACK);
4509 SET_ARGUMENT_PACK_ARGS (t, vec);
4510 }
4511 else
4512 t = convert_from_reference (t);
4513 }
4514 return t;
4515 }
4516
4517 /* Given a single level of template parameters (a TREE_VEC), return it
4518 as a set of template arguments. */
4519
4520 static tree
4521 template_parms_level_to_args (tree parms)
4522 {
4523 tree a = copy_node (parms);
4524 TREE_TYPE (a) = NULL_TREE;
4525 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4526 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4527
4528 if (CHECKING_P)
4529 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4530
4531 return a;
4532 }
4533
4534 /* Given a set of template parameters, return them as a set of template
4535 arguments. The template parameters are represented as a TREE_VEC, in
4536 the form documented in cp-tree.h for template arguments. */
4537
4538 static tree
4539 template_parms_to_args (tree parms)
4540 {
4541 tree header;
4542 tree args = NULL_TREE;
4543 int length = TMPL_PARMS_DEPTH (parms);
4544 int l = length;
4545
4546 /* If there is only one level of template parameters, we do not
4547 create a TREE_VEC of TREE_VECs. Instead, we return a single
4548 TREE_VEC containing the arguments. */
4549 if (length > 1)
4550 args = make_tree_vec (length);
4551
4552 for (header = parms; header; header = TREE_CHAIN (header))
4553 {
4554 tree a = template_parms_level_to_args (TREE_VALUE (header));
4555
4556 if (length > 1)
4557 TREE_VEC_ELT (args, --l) = a;
4558 else
4559 args = a;
4560 }
4561
4562 return args;
4563 }
4564
4565 /* Within the declaration of a template, return the currently active
4566 template parameters as an argument TREE_VEC. */
4567
4568 static tree
4569 current_template_args (void)
4570 {
4571 return template_parms_to_args (current_template_parms);
4572 }
4573
4574 /* Update the declared TYPE by doing any lookups which were thought to be
4575 dependent, but are not now that we know the SCOPE of the declarator. */
4576
4577 tree
4578 maybe_update_decl_type (tree orig_type, tree scope)
4579 {
4580 tree type = orig_type;
4581
4582 if (type == NULL_TREE)
4583 return type;
4584
4585 if (TREE_CODE (orig_type) == TYPE_DECL)
4586 type = TREE_TYPE (type);
4587
4588 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4589 && dependent_type_p (type)
4590 /* Don't bother building up the args in this case. */
4591 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4592 {
4593 /* tsubst in the args corresponding to the template parameters,
4594 including auto if present. Most things will be unchanged, but
4595 make_typename_type and tsubst_qualified_id will resolve
4596 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4597 tree args = current_template_args ();
4598 tree auto_node = type_uses_auto (type);
4599 tree pushed;
4600 if (auto_node)
4601 {
4602 tree auto_vec = make_tree_vec (1);
4603 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4604 args = add_to_template_args (args, auto_vec);
4605 }
4606 pushed = push_scope (scope);
4607 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4608 if (pushed)
4609 pop_scope (scope);
4610 }
4611
4612 if (type == error_mark_node)
4613 return orig_type;
4614
4615 if (TREE_CODE (orig_type) == TYPE_DECL)
4616 {
4617 if (same_type_p (type, TREE_TYPE (orig_type)))
4618 type = orig_type;
4619 else
4620 type = TYPE_NAME (type);
4621 }
4622 return type;
4623 }
4624
4625 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4626 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4627 the new template is a member template. */
4628
4629 static tree
4630 build_template_decl (tree decl, tree parms, bool member_template_p)
4631 {
4632 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4633 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4634 DECL_TEMPLATE_PARMS (tmpl) = parms;
4635 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4636 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4637 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4638
4639 return tmpl;
4640 }
4641
4642 struct template_parm_data
4643 {
4644 /* The level of the template parameters we are currently
4645 processing. */
4646 int level;
4647
4648 /* The index of the specialization argument we are currently
4649 processing. */
4650 int current_arg;
4651
4652 /* An array whose size is the number of template parameters. The
4653 elements are nonzero if the parameter has been used in any one
4654 of the arguments processed so far. */
4655 int* parms;
4656
4657 /* An array whose size is the number of template arguments. The
4658 elements are nonzero if the argument makes use of template
4659 parameters of this level. */
4660 int* arg_uses_template_parms;
4661 };
4662
4663 /* Subroutine of push_template_decl used to see if each template
4664 parameter in a partial specialization is used in the explicit
4665 argument list. If T is of the LEVEL given in DATA (which is
4666 treated as a template_parm_data*), then DATA->PARMS is marked
4667 appropriately. */
4668
4669 static int
4670 mark_template_parm (tree t, void* data)
4671 {
4672 int level;
4673 int idx;
4674 struct template_parm_data* tpd = (struct template_parm_data*) data;
4675
4676 template_parm_level_and_index (t, &level, &idx);
4677
4678 if (level == tpd->level)
4679 {
4680 tpd->parms[idx] = 1;
4681 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4682 }
4683
4684 /* In C++17 the type of a non-type argument is a deduced context. */
4685 if (cxx_dialect >= cxx17
4686 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4687 for_each_template_parm (TREE_TYPE (t),
4688 &mark_template_parm,
4689 data,
4690 NULL,
4691 /*include_nondeduced_p=*/false);
4692
4693 /* Return zero so that for_each_template_parm will continue the
4694 traversal of the tree; we want to mark *every* template parm. */
4695 return 0;
4696 }
4697
4698 /* Process the partial specialization DECL. */
4699
4700 static tree
4701 process_partial_specialization (tree decl)
4702 {
4703 tree type = TREE_TYPE (decl);
4704 tree tinfo = get_template_info (decl);
4705 tree maintmpl = TI_TEMPLATE (tinfo);
4706 tree specargs = TI_ARGS (tinfo);
4707 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4708 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4709 tree inner_parms;
4710 tree inst;
4711 int nargs = TREE_VEC_LENGTH (inner_args);
4712 int ntparms;
4713 int i;
4714 bool did_error_intro = false;
4715 struct template_parm_data tpd;
4716 struct template_parm_data tpd2;
4717
4718 gcc_assert (current_template_parms);
4719
4720 /* A concept cannot be specialized. */
4721 if (flag_concepts && variable_concept_p (maintmpl))
4722 {
4723 error ("specialization of variable concept %q#D", maintmpl);
4724 return error_mark_node;
4725 }
4726
4727 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4728 ntparms = TREE_VEC_LENGTH (inner_parms);
4729
4730 /* We check that each of the template parameters given in the
4731 partial specialization is used in the argument list to the
4732 specialization. For example:
4733
4734 template <class T> struct S;
4735 template <class T> struct S<T*>;
4736
4737 The second declaration is OK because `T*' uses the template
4738 parameter T, whereas
4739
4740 template <class T> struct S<int>;
4741
4742 is no good. Even trickier is:
4743
4744 template <class T>
4745 struct S1
4746 {
4747 template <class U>
4748 struct S2;
4749 template <class U>
4750 struct S2<T>;
4751 };
4752
4753 The S2<T> declaration is actually invalid; it is a
4754 full-specialization. Of course,
4755
4756 template <class U>
4757 struct S2<T (*)(U)>;
4758
4759 or some such would have been OK. */
4760 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4761 tpd.parms = XALLOCAVEC (int, ntparms);
4762 memset (tpd.parms, 0, sizeof (int) * ntparms);
4763
4764 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4765 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4766 for (i = 0; i < nargs; ++i)
4767 {
4768 tpd.current_arg = i;
4769 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4770 &mark_template_parm,
4771 &tpd,
4772 NULL,
4773 /*include_nondeduced_p=*/false);
4774 }
4775 for (i = 0; i < ntparms; ++i)
4776 if (tpd.parms[i] == 0)
4777 {
4778 /* One of the template parms was not used in a deduced context in the
4779 specialization. */
4780 if (!did_error_intro)
4781 {
4782 error ("template parameters not deducible in "
4783 "partial specialization:");
4784 did_error_intro = true;
4785 }
4786
4787 inform (input_location, " %qD",
4788 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4789 }
4790
4791 if (did_error_intro)
4792 return error_mark_node;
4793
4794 /* [temp.class.spec]
4795
4796 The argument list of the specialization shall not be identical to
4797 the implicit argument list of the primary template. */
4798 tree main_args
4799 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4800 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4801 && (!flag_concepts
4802 || !strictly_subsumes (current_template_constraints (),
4803 get_constraints (maintmpl))))
4804 {
4805 if (!flag_concepts)
4806 error ("partial specialization %q+D does not specialize "
4807 "any template arguments; to define the primary template, "
4808 "remove the template argument list", decl);
4809 else
4810 error ("partial specialization %q+D does not specialize any "
4811 "template arguments and is not more constrained than "
4812 "the primary template; to define the primary template, "
4813 "remove the template argument list", decl);
4814 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4815 }
4816
4817 /* A partial specialization that replaces multiple parameters of the
4818 primary template with a pack expansion is less specialized for those
4819 parameters. */
4820 if (nargs < DECL_NTPARMS (maintmpl))
4821 {
4822 error ("partial specialization is not more specialized than the "
4823 "primary template because it replaces multiple parameters "
4824 "with a pack expansion");
4825 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4826 /* Avoid crash in process_partial_specialization. */
4827 return decl;
4828 }
4829
4830 /* If we aren't in a dependent class, we can actually try deduction. */
4831 else if (tpd.level == 1
4832 /* FIXME we should be able to handle a partial specialization of a
4833 partial instantiation, but currently we can't (c++/41727). */
4834 && TMPL_ARGS_DEPTH (specargs) == 1
4835 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4836 {
4837 auto_diagnostic_group d;
4838 if (permerror (input_location, "partial specialization %qD is not "
4839 "more specialized than", decl))
4840 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4841 maintmpl);
4842 }
4843
4844 /* [temp.class.spec]
4845
4846 A partially specialized non-type argument expression shall not
4847 involve template parameters of the partial specialization except
4848 when the argument expression is a simple identifier.
4849
4850 The type of a template parameter corresponding to a specialized
4851 non-type argument shall not be dependent on a parameter of the
4852 specialization.
4853
4854 Also, we verify that pack expansions only occur at the
4855 end of the argument list. */
4856 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4857 tpd2.parms = 0;
4858 for (i = 0; i < nargs; ++i)
4859 {
4860 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4861 tree arg = TREE_VEC_ELT (inner_args, i);
4862 tree packed_args = NULL_TREE;
4863 int j, len = 1;
4864
4865 if (ARGUMENT_PACK_P (arg))
4866 {
4867 /* Extract the arguments from the argument pack. We'll be
4868 iterating over these in the following loop. */
4869 packed_args = ARGUMENT_PACK_ARGS (arg);
4870 len = TREE_VEC_LENGTH (packed_args);
4871 }
4872
4873 for (j = 0; j < len; j++)
4874 {
4875 if (packed_args)
4876 /* Get the Jth argument in the parameter pack. */
4877 arg = TREE_VEC_ELT (packed_args, j);
4878
4879 if (PACK_EXPANSION_P (arg))
4880 {
4881 /* Pack expansions must come at the end of the
4882 argument list. */
4883 if ((packed_args && j < len - 1)
4884 || (!packed_args && i < nargs - 1))
4885 {
4886 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4887 error ("parameter pack argument %qE must be at the "
4888 "end of the template argument list", arg);
4889 else
4890 error ("parameter pack argument %qT must be at the "
4891 "end of the template argument list", arg);
4892 }
4893 }
4894
4895 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4896 /* We only care about the pattern. */
4897 arg = PACK_EXPANSION_PATTERN (arg);
4898
4899 if (/* These first two lines are the `non-type' bit. */
4900 !TYPE_P (arg)
4901 && TREE_CODE (arg) != TEMPLATE_DECL
4902 /* This next two lines are the `argument expression is not just a
4903 simple identifier' condition and also the `specialized
4904 non-type argument' bit. */
4905 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4906 && !(REFERENCE_REF_P (arg)
4907 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4908 {
4909 if ((!packed_args && tpd.arg_uses_template_parms[i])
4910 || (packed_args && uses_template_parms (arg)))
4911 error ("template argument %qE involves template parameter(s)",
4912 arg);
4913 else
4914 {
4915 /* Look at the corresponding template parameter,
4916 marking which template parameters its type depends
4917 upon. */
4918 tree type = TREE_TYPE (parm);
4919
4920 if (!tpd2.parms)
4921 {
4922 /* We haven't yet initialized TPD2. Do so now. */
4923 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4924 /* The number of parameters here is the number in the
4925 main template, which, as checked in the assertion
4926 above, is NARGS. */
4927 tpd2.parms = XALLOCAVEC (int, nargs);
4928 tpd2.level =
4929 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4930 }
4931
4932 /* Mark the template parameters. But this time, we're
4933 looking for the template parameters of the main
4934 template, not in the specialization. */
4935 tpd2.current_arg = i;
4936 tpd2.arg_uses_template_parms[i] = 0;
4937 memset (tpd2.parms, 0, sizeof (int) * nargs);
4938 for_each_template_parm (type,
4939 &mark_template_parm,
4940 &tpd2,
4941 NULL,
4942 /*include_nondeduced_p=*/false);
4943
4944 if (tpd2.arg_uses_template_parms [i])
4945 {
4946 /* The type depended on some template parameters.
4947 If they are fully specialized in the
4948 specialization, that's OK. */
4949 int j;
4950 int count = 0;
4951 for (j = 0; j < nargs; ++j)
4952 if (tpd2.parms[j] != 0
4953 && tpd.arg_uses_template_parms [j])
4954 ++count;
4955 if (count != 0)
4956 error_n (input_location, count,
4957 "type %qT of template argument %qE depends "
4958 "on a template parameter",
4959 "type %qT of template argument %qE depends "
4960 "on template parameters",
4961 type,
4962 arg);
4963 }
4964 }
4965 }
4966 }
4967 }
4968
4969 /* We should only get here once. */
4970 if (TREE_CODE (decl) == TYPE_DECL)
4971 gcc_assert (!COMPLETE_TYPE_P (type));
4972
4973 // Build the template decl.
4974 tree tmpl = build_template_decl (decl, current_template_parms,
4975 DECL_MEMBER_TEMPLATE_P (maintmpl));
4976 TREE_TYPE (tmpl) = type;
4977 DECL_TEMPLATE_RESULT (tmpl) = decl;
4978 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4979 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4980 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4981
4982 /* Give template template parms a DECL_CONTEXT of the template
4983 for which they are a parameter. */
4984 for (i = 0; i < ntparms; ++i)
4985 {
4986 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4987 if (TREE_CODE (parm) == TEMPLATE_DECL)
4988 DECL_CONTEXT (parm) = tmpl;
4989 }
4990
4991 if (VAR_P (decl))
4992 /* We didn't register this in check_explicit_specialization so we could
4993 wait until the constraints were set. */
4994 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4995 else
4996 associate_classtype_constraints (type);
4997
4998 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4999 = tree_cons (specargs, tmpl,
5000 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5001 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5002
5003 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5004 inst = TREE_CHAIN (inst))
5005 {
5006 tree instance = TREE_VALUE (inst);
5007 if (TYPE_P (instance)
5008 ? (COMPLETE_TYPE_P (instance)
5009 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5010 : DECL_TEMPLATE_INSTANTIATION (instance))
5011 {
5012 tree spec = most_specialized_partial_spec (instance, tf_none);
5013 tree inst_decl = (DECL_P (instance)
5014 ? instance : TYPE_NAME (instance));
5015 if (!spec)
5016 /* OK */;
5017 else if (spec == error_mark_node)
5018 permerror (input_location,
5019 "declaration of %qD ambiguates earlier template "
5020 "instantiation for %qD", decl, inst_decl);
5021 else if (TREE_VALUE (spec) == tmpl)
5022 permerror (input_location,
5023 "partial specialization of %qD after instantiation "
5024 "of %qD", decl, inst_decl);
5025 }
5026 }
5027
5028 return decl;
5029 }
5030
5031 /* PARM is a template parameter of some form; return the corresponding
5032 TEMPLATE_PARM_INDEX. */
5033
5034 static tree
5035 get_template_parm_index (tree parm)
5036 {
5037 if (TREE_CODE (parm) == PARM_DECL
5038 || TREE_CODE (parm) == CONST_DECL)
5039 parm = DECL_INITIAL (parm);
5040 else if (TREE_CODE (parm) == TYPE_DECL
5041 || TREE_CODE (parm) == TEMPLATE_DECL)
5042 parm = TREE_TYPE (parm);
5043 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5044 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5045 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5046 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5047 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5048 return parm;
5049 }
5050
5051 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5052 parameter packs used by the template parameter PARM. */
5053
5054 static void
5055 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5056 {
5057 /* A type parm can't refer to another parm. */
5058 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5059 return;
5060 else if (TREE_CODE (parm) == PARM_DECL)
5061 {
5062 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5063 ppd, ppd->visited);
5064 return;
5065 }
5066
5067 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5068
5069 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5070 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5071 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5072 }
5073
5074 /* PARM is a template parameter pack. Return any parameter packs used in
5075 its type or the type of any of its template parameters. If there are
5076 any such packs, it will be instantiated into a fixed template parameter
5077 list by partial instantiation rather than be fully deduced. */
5078
5079 tree
5080 fixed_parameter_pack_p (tree parm)
5081 {
5082 /* This can only be true in a member template. */
5083 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5084 return NULL_TREE;
5085 /* This can only be true for a parameter pack. */
5086 if (!template_parameter_pack_p (parm))
5087 return NULL_TREE;
5088 /* A type parm can't refer to another parm. */
5089 if (TREE_CODE (parm) == TYPE_DECL)
5090 return NULL_TREE;
5091
5092 tree parameter_packs = NULL_TREE;
5093 struct find_parameter_pack_data ppd;
5094 ppd.parameter_packs = &parameter_packs;
5095 ppd.visited = new hash_set<tree>;
5096 ppd.type_pack_expansion_p = false;
5097
5098 fixed_parameter_pack_p_1 (parm, &ppd);
5099
5100 delete ppd.visited;
5101 return parameter_packs;
5102 }
5103
5104 /* Check that a template declaration's use of default arguments and
5105 parameter packs is not invalid. Here, PARMS are the template
5106 parameters. IS_PRIMARY is true if DECL is the thing declared by
5107 a primary template. IS_PARTIAL is true if DECL is a partial
5108 specialization.
5109
5110 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5111 function template declaration or a friend class template
5112 declaration. In the function case, 1 indicates a declaration, 2
5113 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5114 emitted for extraneous default arguments.
5115
5116 Returns TRUE if there were no errors found, FALSE otherwise. */
5117
5118 bool
5119 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5120 bool is_partial, int is_friend_decl)
5121 {
5122 const char *msg;
5123 int last_level_to_check;
5124 tree parm_level;
5125 bool no_errors = true;
5126
5127 /* [temp.param]
5128
5129 A default template-argument shall not be specified in a
5130 function template declaration or a function template definition, nor
5131 in the template-parameter-list of the definition of a member of a
5132 class template. */
5133
5134 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5135 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5136 /* You can't have a function template declaration in a local
5137 scope, nor you can you define a member of a class template in a
5138 local scope. */
5139 return true;
5140
5141 if ((TREE_CODE (decl) == TYPE_DECL
5142 && TREE_TYPE (decl)
5143 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5144 || (TREE_CODE (decl) == FUNCTION_DECL
5145 && LAMBDA_FUNCTION_P (decl)))
5146 /* A lambda doesn't have an explicit declaration; don't complain
5147 about the parms of the enclosing class. */
5148 return true;
5149
5150 if (current_class_type
5151 && !TYPE_BEING_DEFINED (current_class_type)
5152 && DECL_LANG_SPECIFIC (decl)
5153 && DECL_DECLARES_FUNCTION_P (decl)
5154 /* If this is either a friend defined in the scope of the class
5155 or a member function. */
5156 && (DECL_FUNCTION_MEMBER_P (decl)
5157 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5158 : DECL_FRIEND_CONTEXT (decl)
5159 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5160 : false)
5161 /* And, if it was a member function, it really was defined in
5162 the scope of the class. */
5163 && (!DECL_FUNCTION_MEMBER_P (decl)
5164 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5165 /* We already checked these parameters when the template was
5166 declared, so there's no need to do it again now. This function
5167 was defined in class scope, but we're processing its body now
5168 that the class is complete. */
5169 return true;
5170
5171 /* Core issue 226 (C++0x only): the following only applies to class
5172 templates. */
5173 if (is_primary
5174 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5175 {
5176 /* [temp.param]
5177
5178 If a template-parameter has a default template-argument, all
5179 subsequent template-parameters shall have a default
5180 template-argument supplied. */
5181 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5182 {
5183 tree inner_parms = TREE_VALUE (parm_level);
5184 int ntparms = TREE_VEC_LENGTH (inner_parms);
5185 int seen_def_arg_p = 0;
5186 int i;
5187
5188 for (i = 0; i < ntparms; ++i)
5189 {
5190 tree parm = TREE_VEC_ELT (inner_parms, i);
5191
5192 if (parm == error_mark_node)
5193 continue;
5194
5195 if (TREE_PURPOSE (parm))
5196 seen_def_arg_p = 1;
5197 else if (seen_def_arg_p
5198 && !template_parameter_pack_p (TREE_VALUE (parm)))
5199 {
5200 error ("no default argument for %qD", TREE_VALUE (parm));
5201 /* For better subsequent error-recovery, we indicate that
5202 there should have been a default argument. */
5203 TREE_PURPOSE (parm) = error_mark_node;
5204 no_errors = false;
5205 }
5206 else if (!is_partial
5207 && !is_friend_decl
5208 /* Don't complain about an enclosing partial
5209 specialization. */
5210 && parm_level == parms
5211 && TREE_CODE (decl) == TYPE_DECL
5212 && i < ntparms - 1
5213 && template_parameter_pack_p (TREE_VALUE (parm))
5214 /* A fixed parameter pack will be partially
5215 instantiated into a fixed length list. */
5216 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5217 {
5218 /* A primary class template can only have one
5219 parameter pack, at the end of the template
5220 parameter list. */
5221
5222 error ("parameter pack %q+D must be at the end of the"
5223 " template parameter list", TREE_VALUE (parm));
5224
5225 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5226 = error_mark_node;
5227 no_errors = false;
5228 }
5229 }
5230 }
5231 }
5232
5233 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5234 || is_partial
5235 || !is_primary
5236 || is_friend_decl)
5237 /* For an ordinary class template, default template arguments are
5238 allowed at the innermost level, e.g.:
5239 template <class T = int>
5240 struct S {};
5241 but, in a partial specialization, they're not allowed even
5242 there, as we have in [temp.class.spec]:
5243
5244 The template parameter list of a specialization shall not
5245 contain default template argument values.
5246
5247 So, for a partial specialization, or for a function template
5248 (in C++98/C++03), we look at all of them. */
5249 ;
5250 else
5251 /* But, for a primary class template that is not a partial
5252 specialization we look at all template parameters except the
5253 innermost ones. */
5254 parms = TREE_CHAIN (parms);
5255
5256 /* Figure out what error message to issue. */
5257 if (is_friend_decl == 2)
5258 msg = G_("default template arguments may not be used in function template "
5259 "friend re-declaration");
5260 else if (is_friend_decl)
5261 msg = G_("default template arguments may not be used in template "
5262 "friend declarations");
5263 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5264 msg = G_("default template arguments may not be used in function templates "
5265 "without -std=c++11 or -std=gnu++11");
5266 else if (is_partial)
5267 msg = G_("default template arguments may not be used in "
5268 "partial specializations");
5269 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5270 msg = G_("default argument for template parameter for class enclosing %qD");
5271 else
5272 /* Per [temp.param]/9, "A default template-argument shall not be
5273 specified in the template-parameter-lists of the definition of
5274 a member of a class template that appears outside of the member's
5275 class.", thus if we aren't handling a member of a class template
5276 there is no need to examine the parameters. */
5277 return true;
5278
5279 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5280 /* If we're inside a class definition, there's no need to
5281 examine the parameters to the class itself. On the one
5282 hand, they will be checked when the class is defined, and,
5283 on the other, default arguments are valid in things like:
5284 template <class T = double>
5285 struct S { template <class U> void f(U); };
5286 Here the default argument for `S' has no bearing on the
5287 declaration of `f'. */
5288 last_level_to_check = template_class_depth (current_class_type) + 1;
5289 else
5290 /* Check everything. */
5291 last_level_to_check = 0;
5292
5293 for (parm_level = parms;
5294 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5295 parm_level = TREE_CHAIN (parm_level))
5296 {
5297 tree inner_parms = TREE_VALUE (parm_level);
5298 int i;
5299 int ntparms;
5300
5301 ntparms = TREE_VEC_LENGTH (inner_parms);
5302 for (i = 0; i < ntparms; ++i)
5303 {
5304 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5305 continue;
5306
5307 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5308 {
5309 if (msg)
5310 {
5311 no_errors = false;
5312 if (is_friend_decl == 2)
5313 return no_errors;
5314
5315 error (msg, decl);
5316 msg = 0;
5317 }
5318
5319 /* Clear out the default argument so that we are not
5320 confused later. */
5321 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5322 }
5323 }
5324
5325 /* At this point, if we're still interested in issuing messages,
5326 they must apply to classes surrounding the object declared. */
5327 if (msg)
5328 msg = G_("default argument for template parameter for class "
5329 "enclosing %qD");
5330 }
5331
5332 return no_errors;
5333 }
5334
5335 /* Worker for push_template_decl_real, called via
5336 for_each_template_parm. DATA is really an int, indicating the
5337 level of the parameters we are interested in. If T is a template
5338 parameter of that level, return nonzero. */
5339
5340 static int
5341 template_parm_this_level_p (tree t, void* data)
5342 {
5343 int this_level = *(int *)data;
5344 int level;
5345
5346 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5347 level = TEMPLATE_PARM_LEVEL (t);
5348 else
5349 level = TEMPLATE_TYPE_LEVEL (t);
5350 return level == this_level;
5351 }
5352
5353 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5354 DATA is really an int, indicating the innermost outer level of parameters.
5355 If T is a template parameter of that level or further out, return
5356 nonzero. */
5357
5358 static int
5359 template_parm_outer_level (tree t, void *data)
5360 {
5361 int this_level = *(int *)data;
5362 int level;
5363
5364 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5365 level = TEMPLATE_PARM_LEVEL (t);
5366 else
5367 level = TEMPLATE_TYPE_LEVEL (t);
5368 return level <= this_level;
5369 }
5370
5371 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5372 parameters given by current_template_args, or reuses a
5373 previously existing one, if appropriate. Returns the DECL, or an
5374 equivalent one, if it is replaced via a call to duplicate_decls.
5375
5376 If IS_FRIEND is true, DECL is a friend declaration. */
5377
5378 tree
5379 push_template_decl_real (tree decl, bool is_friend)
5380 {
5381 tree tmpl;
5382 tree args;
5383 tree info;
5384 tree ctx;
5385 bool is_primary;
5386 bool is_partial;
5387 int new_template_p = 0;
5388 /* True if the template is a member template, in the sense of
5389 [temp.mem]. */
5390 bool member_template_p = false;
5391
5392 if (decl == error_mark_node || !current_template_parms)
5393 return error_mark_node;
5394
5395 /* See if this is a partial specialization. */
5396 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5397 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5398 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5399 || (VAR_P (decl)
5400 && DECL_LANG_SPECIFIC (decl)
5401 && DECL_TEMPLATE_SPECIALIZATION (decl)
5402 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5403
5404 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5405 is_friend = true;
5406
5407 if (is_friend)
5408 /* For a friend, we want the context of the friend, not
5409 the type of which it is a friend. */
5410 ctx = CP_DECL_CONTEXT (decl);
5411 else if (CP_DECL_CONTEXT (decl)
5412 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5413 /* In the case of a virtual function, we want the class in which
5414 it is defined. */
5415 ctx = CP_DECL_CONTEXT (decl);
5416 else
5417 /* Otherwise, if we're currently defining some class, the DECL
5418 is assumed to be a member of the class. */
5419 ctx = current_scope ();
5420
5421 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5422 ctx = NULL_TREE;
5423
5424 if (!DECL_CONTEXT (decl))
5425 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5426
5427 /* See if this is a primary template. */
5428 if (is_friend && ctx
5429 && uses_template_parms_level (ctx, processing_template_decl))
5430 /* A friend template that specifies a class context, i.e.
5431 template <typename T> friend void A<T>::f();
5432 is not primary. */
5433 is_primary = false;
5434 else if (TREE_CODE (decl) == TYPE_DECL
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5436 is_primary = false;
5437 else
5438 is_primary = template_parm_scope_p ();
5439
5440 if (is_primary)
5441 {
5442 warning (OPT_Wtemplates, "template %qD declared", decl);
5443
5444 if (DECL_CLASS_SCOPE_P (decl))
5445 member_template_p = true;
5446 if (TREE_CODE (decl) == TYPE_DECL
5447 && anon_aggrname_p (DECL_NAME (decl)))
5448 {
5449 error ("template class without a name");
5450 return error_mark_node;
5451 }
5452 else if (TREE_CODE (decl) == FUNCTION_DECL)
5453 {
5454 if (member_template_p)
5455 {
5456 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5457 error ("member template %qD may not have virt-specifiers", decl);
5458 }
5459 if (DECL_DESTRUCTOR_P (decl))
5460 {
5461 /* [temp.mem]
5462
5463 A destructor shall not be a member template. */
5464 error ("destructor %qD declared as member template", decl);
5465 return error_mark_node;
5466 }
5467 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5468 && (!prototype_p (TREE_TYPE (decl))
5469 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5470 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5471 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5472 == void_list_node)))
5473 {
5474 /* [basic.stc.dynamic.allocation]
5475
5476 An allocation function can be a function
5477 template. ... Template allocation functions shall
5478 have two or more parameters. */
5479 error ("invalid template declaration of %qD", decl);
5480 return error_mark_node;
5481 }
5482 }
5483 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5484 && CLASS_TYPE_P (TREE_TYPE (decl)))
5485 {
5486 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5487 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5488 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5489 {
5490 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5491 if (TREE_CODE (t) == TYPE_DECL)
5492 t = TREE_TYPE (t);
5493 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5494 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5495 }
5496 }
5497 else if (TREE_CODE (decl) == TYPE_DECL
5498 && TYPE_DECL_ALIAS_P (decl))
5499 /* alias-declaration */
5500 gcc_assert (!DECL_ARTIFICIAL (decl));
5501 else if (VAR_P (decl))
5502 /* C++14 variable template. */;
5503 else
5504 {
5505 error ("template declaration of %q#D", decl);
5506 return error_mark_node;
5507 }
5508 }
5509
5510 /* Check to see that the rules regarding the use of default
5511 arguments are not being violated. We check args for a friend
5512 functions when we know whether it's a definition, introducing
5513 declaration or re-declaration. */
5514 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5515 check_default_tmpl_args (decl, current_template_parms,
5516 is_primary, is_partial, is_friend);
5517
5518 /* Ensure that there are no parameter packs in the type of this
5519 declaration that have not been expanded. */
5520 if (TREE_CODE (decl) == FUNCTION_DECL)
5521 {
5522 /* Check each of the arguments individually to see if there are
5523 any bare parameter packs. */
5524 tree type = TREE_TYPE (decl);
5525 tree arg = DECL_ARGUMENTS (decl);
5526 tree argtype = TYPE_ARG_TYPES (type);
5527
5528 while (arg && argtype)
5529 {
5530 if (!DECL_PACK_P (arg)
5531 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5532 {
5533 /* This is a PARM_DECL that contains unexpanded parameter
5534 packs. We have already complained about this in the
5535 check_for_bare_parameter_packs call, so just replace
5536 these types with ERROR_MARK_NODE. */
5537 TREE_TYPE (arg) = error_mark_node;
5538 TREE_VALUE (argtype) = error_mark_node;
5539 }
5540
5541 arg = DECL_CHAIN (arg);
5542 argtype = TREE_CHAIN (argtype);
5543 }
5544
5545 /* Check for bare parameter packs in the return type and the
5546 exception specifiers. */
5547 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5548 /* Errors were already issued, set return type to int
5549 as the frontend doesn't expect error_mark_node as
5550 the return type. */
5551 TREE_TYPE (type) = integer_type_node;
5552 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5553 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5554 }
5555 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5556 && TYPE_DECL_ALIAS_P (decl))
5557 ? DECL_ORIGINAL_TYPE (decl)
5558 : TREE_TYPE (decl)))
5559 {
5560 TREE_TYPE (decl) = error_mark_node;
5561 return error_mark_node;
5562 }
5563
5564 if (is_partial)
5565 return process_partial_specialization (decl);
5566
5567 args = current_template_args ();
5568
5569 if (!ctx
5570 || TREE_CODE (ctx) == FUNCTION_DECL
5571 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5572 || (TREE_CODE (decl) == TYPE_DECL
5573 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5574 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5575 {
5576 if (DECL_LANG_SPECIFIC (decl)
5577 && DECL_TEMPLATE_INFO (decl)
5578 && DECL_TI_TEMPLATE (decl))
5579 tmpl = DECL_TI_TEMPLATE (decl);
5580 /* If DECL is a TYPE_DECL for a class-template, then there won't
5581 be DECL_LANG_SPECIFIC. The information equivalent to
5582 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5583 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5584 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5585 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5586 {
5587 /* Since a template declaration already existed for this
5588 class-type, we must be redeclaring it here. Make sure
5589 that the redeclaration is valid. */
5590 redeclare_class_template (TREE_TYPE (decl),
5591 current_template_parms,
5592 current_template_constraints ());
5593 /* We don't need to create a new TEMPLATE_DECL; just use the
5594 one we already had. */
5595 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5596 }
5597 else
5598 {
5599 tmpl = build_template_decl (decl, current_template_parms,
5600 member_template_p);
5601 new_template_p = 1;
5602
5603 if (DECL_LANG_SPECIFIC (decl)
5604 && DECL_TEMPLATE_SPECIALIZATION (decl))
5605 {
5606 /* A specialization of a member template of a template
5607 class. */
5608 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5609 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5610 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5611 }
5612 }
5613 }
5614 else
5615 {
5616 tree a, t, current, parms;
5617 int i;
5618 tree tinfo = get_template_info (decl);
5619
5620 if (!tinfo)
5621 {
5622 error ("template definition of non-template %q#D", decl);
5623 return error_mark_node;
5624 }
5625
5626 tmpl = TI_TEMPLATE (tinfo);
5627
5628 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5629 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5630 && DECL_TEMPLATE_SPECIALIZATION (decl)
5631 && DECL_MEMBER_TEMPLATE_P (tmpl))
5632 {
5633 tree new_tmpl;
5634
5635 /* The declaration is a specialization of a member
5636 template, declared outside the class. Therefore, the
5637 innermost template arguments will be NULL, so we
5638 replace them with the arguments determined by the
5639 earlier call to check_explicit_specialization. */
5640 args = DECL_TI_ARGS (decl);
5641
5642 new_tmpl
5643 = build_template_decl (decl, current_template_parms,
5644 member_template_p);
5645 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5646 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5647 DECL_TI_TEMPLATE (decl) = new_tmpl;
5648 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5649 DECL_TEMPLATE_INFO (new_tmpl)
5650 = build_template_info (tmpl, args);
5651
5652 register_specialization (new_tmpl,
5653 most_general_template (tmpl),
5654 args,
5655 is_friend, 0);
5656 return decl;
5657 }
5658
5659 /* Make sure the template headers we got make sense. */
5660
5661 parms = DECL_TEMPLATE_PARMS (tmpl);
5662 i = TMPL_PARMS_DEPTH (parms);
5663 if (TMPL_ARGS_DEPTH (args) != i)
5664 {
5665 error ("expected %d levels of template parms for %q#D, got %d",
5666 i, decl, TMPL_ARGS_DEPTH (args));
5667 DECL_INTERFACE_KNOWN (decl) = 1;
5668 return error_mark_node;
5669 }
5670 else
5671 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5672 {
5673 a = TMPL_ARGS_LEVEL (args, i);
5674 t = INNERMOST_TEMPLATE_PARMS (parms);
5675
5676 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5677 {
5678 if (current == decl)
5679 error ("got %d template parameters for %q#D",
5680 TREE_VEC_LENGTH (a), decl);
5681 else
5682 error ("got %d template parameters for %q#T",
5683 TREE_VEC_LENGTH (a), current);
5684 error (" but %d required", TREE_VEC_LENGTH (t));
5685 /* Avoid crash in import_export_decl. */
5686 DECL_INTERFACE_KNOWN (decl) = 1;
5687 return error_mark_node;
5688 }
5689
5690 if (current == decl)
5691 current = ctx;
5692 else if (current == NULL_TREE)
5693 /* Can happen in erroneous input. */
5694 break;
5695 else
5696 current = get_containing_scope (current);
5697 }
5698
5699 /* Check that the parms are used in the appropriate qualifying scopes
5700 in the declarator. */
5701 if (!comp_template_args
5702 (TI_ARGS (tinfo),
5703 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5704 {
5705 error ("template arguments to %qD do not match original "
5706 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5707 if (!uses_template_parms (TI_ARGS (tinfo)))
5708 inform (input_location, "use %<template<>%> for"
5709 " an explicit specialization");
5710 /* Avoid crash in import_export_decl. */
5711 DECL_INTERFACE_KNOWN (decl) = 1;
5712 return error_mark_node;
5713 }
5714 }
5715
5716 DECL_TEMPLATE_RESULT (tmpl) = decl;
5717 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5718
5719 /* Push template declarations for global functions and types. Note
5720 that we do not try to push a global template friend declared in a
5721 template class; such a thing may well depend on the template
5722 parameters of the class. */
5723 if (new_template_p && !ctx
5724 && !(is_friend && template_class_depth (current_class_type) > 0))
5725 {
5726 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5727 if (tmpl == error_mark_node)
5728 return error_mark_node;
5729
5730 /* Hide template friend classes that haven't been declared yet. */
5731 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5732 {
5733 DECL_ANTICIPATED (tmpl) = 1;
5734 DECL_FRIEND_P (tmpl) = 1;
5735 }
5736 }
5737
5738 if (is_primary)
5739 {
5740 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5741
5742 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5743
5744 /* Give template template parms a DECL_CONTEXT of the template
5745 for which they are a parameter. */
5746 parms = INNERMOST_TEMPLATE_PARMS (parms);
5747 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5748 {
5749 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5750 if (TREE_CODE (parm) == TEMPLATE_DECL)
5751 DECL_CONTEXT (parm) = tmpl;
5752 }
5753
5754 if (TREE_CODE (decl) == TYPE_DECL
5755 && TYPE_DECL_ALIAS_P (decl)
5756 && complex_alias_template_p (tmpl))
5757 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5758 }
5759
5760 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5761 back to its most general template. If TMPL is a specialization,
5762 ARGS may only have the innermost set of arguments. Add the missing
5763 argument levels if necessary. */
5764 if (DECL_TEMPLATE_INFO (tmpl))
5765 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5766
5767 info = build_template_info (tmpl, args);
5768
5769 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5770 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5771 else
5772 {
5773 if (is_primary)
5774 retrofit_lang_decl (decl);
5775 if (DECL_LANG_SPECIFIC (decl))
5776 DECL_TEMPLATE_INFO (decl) = info;
5777 }
5778
5779 if (flag_implicit_templates
5780 && !is_friend
5781 && TREE_PUBLIC (decl)
5782 && VAR_OR_FUNCTION_DECL_P (decl))
5783 /* Set DECL_COMDAT on template instantiations; if we force
5784 them to be emitted by explicit instantiation or -frepo,
5785 mark_needed will tell cgraph to do the right thing. */
5786 DECL_COMDAT (decl) = true;
5787
5788 return DECL_TEMPLATE_RESULT (tmpl);
5789 }
5790
5791 tree
5792 push_template_decl (tree decl)
5793 {
5794 return push_template_decl_real (decl, false);
5795 }
5796
5797 /* FN is an inheriting constructor that inherits from the constructor
5798 template INHERITED; turn FN into a constructor template with a matching
5799 template header. */
5800
5801 tree
5802 add_inherited_template_parms (tree fn, tree inherited)
5803 {
5804 tree inner_parms
5805 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5806 inner_parms = copy_node (inner_parms);
5807 tree parms
5808 = tree_cons (size_int (processing_template_decl + 1),
5809 inner_parms, current_template_parms);
5810 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5811 tree args = template_parms_to_args (parms);
5812 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5813 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5814 DECL_TEMPLATE_RESULT (tmpl) = fn;
5815 DECL_ARTIFICIAL (tmpl) = true;
5816 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5817 return tmpl;
5818 }
5819
5820 /* Called when a class template TYPE is redeclared with the indicated
5821 template PARMS, e.g.:
5822
5823 template <class T> struct S;
5824 template <class T> struct S {}; */
5825
5826 bool
5827 redeclare_class_template (tree type, tree parms, tree cons)
5828 {
5829 tree tmpl;
5830 tree tmpl_parms;
5831 int i;
5832
5833 if (!TYPE_TEMPLATE_INFO (type))
5834 {
5835 error ("%qT is not a template type", type);
5836 return false;
5837 }
5838
5839 tmpl = TYPE_TI_TEMPLATE (type);
5840 if (!PRIMARY_TEMPLATE_P (tmpl))
5841 /* The type is nested in some template class. Nothing to worry
5842 about here; there are no new template parameters for the nested
5843 type. */
5844 return true;
5845
5846 if (!parms)
5847 {
5848 error ("template specifiers not specified in declaration of %qD",
5849 tmpl);
5850 return false;
5851 }
5852
5853 parms = INNERMOST_TEMPLATE_PARMS (parms);
5854 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5855
5856 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5857 {
5858 error_n (input_location, TREE_VEC_LENGTH (parms),
5859 "redeclared with %d template parameter",
5860 "redeclared with %d template parameters",
5861 TREE_VEC_LENGTH (parms));
5862 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5863 "previous declaration %qD used %d template parameter",
5864 "previous declaration %qD used %d template parameters",
5865 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5866 return false;
5867 }
5868
5869 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5870 {
5871 tree tmpl_parm;
5872 tree parm;
5873 tree tmpl_default;
5874 tree parm_default;
5875
5876 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5877 || TREE_VEC_ELT (parms, i) == error_mark_node)
5878 continue;
5879
5880 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5881 if (error_operand_p (tmpl_parm))
5882 return false;
5883
5884 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5885 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5886 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5887
5888 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5889 TEMPLATE_DECL. */
5890 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5891 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5892 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5893 || (TREE_CODE (tmpl_parm) != PARM_DECL
5894 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5895 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5896 || (TREE_CODE (tmpl_parm) == PARM_DECL
5897 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5898 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5899 {
5900 error ("template parameter %q+#D", tmpl_parm);
5901 error ("redeclared here as %q#D", parm);
5902 return false;
5903 }
5904
5905 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5906 {
5907 /* We have in [temp.param]:
5908
5909 A template-parameter may not be given default arguments
5910 by two different declarations in the same scope. */
5911 error_at (input_location, "redefinition of default argument for %q#D", parm);
5912 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5913 "original definition appeared here");
5914 return false;
5915 }
5916
5917 if (parm_default != NULL_TREE)
5918 /* Update the previous template parameters (which are the ones
5919 that will really count) with the new default value. */
5920 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5921 else if (tmpl_default != NULL_TREE)
5922 /* Update the new parameters, too; they'll be used as the
5923 parameters for any members. */
5924 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5925
5926 /* Give each template template parm in this redeclaration a
5927 DECL_CONTEXT of the template for which they are a parameter. */
5928 if (TREE_CODE (parm) == TEMPLATE_DECL)
5929 {
5930 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5931 DECL_CONTEXT (parm) = tmpl;
5932 }
5933
5934 if (TREE_CODE (parm) == TYPE_DECL)
5935 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5936 }
5937
5938 // Cannot redeclare a class template with a different set of constraints.
5939 if (!equivalent_constraints (get_constraints (tmpl), cons))
5940 {
5941 error_at (input_location, "redeclaration %q#D with different "
5942 "constraints", tmpl);
5943 inform (DECL_SOURCE_LOCATION (tmpl),
5944 "original declaration appeared here");
5945 }
5946
5947 return true;
5948 }
5949
5950 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5951 to be used when the caller has already checked
5952 (processing_template_decl
5953 && !instantiation_dependent_expression_p (expr)
5954 && potential_constant_expression (expr))
5955 and cleared processing_template_decl. */
5956
5957 tree
5958 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5959 {
5960 return tsubst_copy_and_build (expr,
5961 /*args=*/NULL_TREE,
5962 complain,
5963 /*in_decl=*/NULL_TREE,
5964 /*function_p=*/false,
5965 /*integral_constant_expression_p=*/true);
5966 }
5967
5968 /* Simplify EXPR if it is a non-dependent expression. Returns the
5969 (possibly simplified) expression. */
5970
5971 tree
5972 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5973 {
5974 if (expr == NULL_TREE)
5975 return NULL_TREE;
5976
5977 /* If we're in a template, but EXPR isn't value dependent, simplify
5978 it. We're supposed to treat:
5979
5980 template <typename T> void f(T[1 + 1]);
5981 template <typename T> void f(T[2]);
5982
5983 as two declarations of the same function, for example. */
5984 if (processing_template_decl
5985 && is_nondependent_constant_expression (expr))
5986 {
5987 processing_template_decl_sentinel s;
5988 expr = instantiate_non_dependent_expr_internal (expr, complain);
5989 }
5990 return expr;
5991 }
5992
5993 tree
5994 instantiate_non_dependent_expr (tree expr)
5995 {
5996 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5997 }
5998
5999 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6000 an uninstantiated expression. */
6001
6002 tree
6003 instantiate_non_dependent_or_null (tree expr)
6004 {
6005 if (expr == NULL_TREE)
6006 return NULL_TREE;
6007 if (processing_template_decl)
6008 {
6009 if (!is_nondependent_constant_expression (expr))
6010 expr = NULL_TREE;
6011 else
6012 {
6013 processing_template_decl_sentinel s;
6014 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6015 }
6016 }
6017 return expr;
6018 }
6019
6020 /* True iff T is a specialization of a variable template. */
6021
6022 bool
6023 variable_template_specialization_p (tree t)
6024 {
6025 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6026 return false;
6027 tree tmpl = DECL_TI_TEMPLATE (t);
6028 return variable_template_p (tmpl);
6029 }
6030
6031 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6032 template declaration, or a TYPE_DECL for an alias declaration. */
6033
6034 bool
6035 alias_type_or_template_p (tree t)
6036 {
6037 if (t == NULL_TREE)
6038 return false;
6039 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6040 || (TYPE_P (t)
6041 && TYPE_NAME (t)
6042 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6043 || DECL_ALIAS_TEMPLATE_P (t));
6044 }
6045
6046 /* Return TRUE iff T is a specialization of an alias template. */
6047
6048 bool
6049 alias_template_specialization_p (const_tree t)
6050 {
6051 /* It's an alias template specialization if it's an alias and its
6052 TYPE_NAME is a specialization of a primary template. */
6053 if (TYPE_ALIAS_P (t))
6054 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6055 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6056
6057 return false;
6058 }
6059
6060 /* An alias template is complex from a SFINAE perspective if a template-id
6061 using that alias can be ill-formed when the expansion is not, as with
6062 the void_t template. We determine this by checking whether the
6063 expansion for the alias template uses all its template parameters. */
6064
6065 struct uses_all_template_parms_data
6066 {
6067 int level;
6068 bool *seen;
6069 };
6070
6071 static int
6072 uses_all_template_parms_r (tree t, void *data_)
6073 {
6074 struct uses_all_template_parms_data &data
6075 = *(struct uses_all_template_parms_data*)data_;
6076 tree idx = get_template_parm_index (t);
6077
6078 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6079 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6080 return 0;
6081 }
6082
6083 static bool
6084 complex_alias_template_p (const_tree tmpl)
6085 {
6086 struct uses_all_template_parms_data data;
6087 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6088 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6089 data.level = TMPL_PARMS_DEPTH (parms);
6090 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6091 data.seen = XALLOCAVEC (bool, len);
6092 for (int i = 0; i < len; ++i)
6093 data.seen[i] = false;
6094
6095 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6096 for (int i = 0; i < len; ++i)
6097 if (!data.seen[i])
6098 return true;
6099 return false;
6100 }
6101
6102 /* Return TRUE iff T is a specialization of a complex alias template with
6103 dependent template-arguments. */
6104
6105 bool
6106 dependent_alias_template_spec_p (const_tree t)
6107 {
6108 if (!alias_template_specialization_p (t))
6109 return false;
6110
6111 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6112 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6113 return false;
6114
6115 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6116 if (!any_dependent_template_arguments_p (args))
6117 return false;
6118
6119 return true;
6120 }
6121
6122 /* Return the number of innermost template parameters in TMPL. */
6123
6124 static int
6125 num_innermost_template_parms (tree tmpl)
6126 {
6127 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6128 return TREE_VEC_LENGTH (parms);
6129 }
6130
6131 /* Return either TMPL or another template that it is equivalent to under DR
6132 1286: An alias that just changes the name of a template is equivalent to
6133 the other template. */
6134
6135 static tree
6136 get_underlying_template (tree tmpl)
6137 {
6138 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6139 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6140 {
6141 /* Determine if the alias is equivalent to an underlying template. */
6142 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6143 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6144 if (!tinfo)
6145 break;
6146
6147 tree underlying = TI_TEMPLATE (tinfo);
6148 if (!PRIMARY_TEMPLATE_P (underlying)
6149 || (num_innermost_template_parms (tmpl)
6150 != num_innermost_template_parms (underlying)))
6151 break;
6152
6153 tree alias_args = INNERMOST_TEMPLATE_ARGS
6154 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6155 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6156 break;
6157
6158 /* Alias is equivalent. Strip it and repeat. */
6159 tmpl = underlying;
6160 }
6161
6162 return tmpl;
6163 }
6164
6165 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6166 must be a reference-to-function or a pointer-to-function type, as specified
6167 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6168 and check that the resulting function has external linkage. */
6169
6170 static tree
6171 convert_nontype_argument_function (tree type, tree expr,
6172 tsubst_flags_t complain)
6173 {
6174 tree fns = expr;
6175 tree fn, fn_no_ptr;
6176 linkage_kind linkage;
6177
6178 fn = instantiate_type (type, fns, tf_none);
6179 if (fn == error_mark_node)
6180 return error_mark_node;
6181
6182 if (value_dependent_expression_p (fn))
6183 goto accept;
6184
6185 fn_no_ptr = strip_fnptr_conv (fn);
6186 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6187 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6188 if (BASELINK_P (fn_no_ptr))
6189 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6190
6191 /* [temp.arg.nontype]/1
6192
6193 A template-argument for a non-type, non-template template-parameter
6194 shall be one of:
6195 [...]
6196 -- the address of an object or function with external [C++11: or
6197 internal] linkage. */
6198
6199 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6200 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6201 {
6202 if (complain & tf_error)
6203 {
6204 error ("%qE is not a valid template argument for type %qT",
6205 expr, type);
6206 if (TYPE_PTR_P (type))
6207 inform (input_location, "it must be the address of a function "
6208 "with external linkage");
6209 else
6210 inform (input_location, "it must be the name of a function with "
6211 "external linkage");
6212 }
6213 return NULL_TREE;
6214 }
6215
6216 linkage = decl_linkage (fn_no_ptr);
6217 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6218 {
6219 if (complain & tf_error)
6220 {
6221 if (cxx_dialect >= cxx11)
6222 error ("%qE is not a valid template argument for type %qT "
6223 "because %qD has no linkage",
6224 expr, type, fn_no_ptr);
6225 else
6226 error ("%qE is not a valid template argument for type %qT "
6227 "because %qD does not have external linkage",
6228 expr, type, fn_no_ptr);
6229 }
6230 return NULL_TREE;
6231 }
6232
6233 accept:
6234 if (TYPE_REF_P (type))
6235 {
6236 if (REFERENCE_REF_P (fn))
6237 fn = TREE_OPERAND (fn, 0);
6238 else
6239 fn = build_address (fn);
6240 }
6241 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6242 fn = build_nop (type, fn);
6243
6244 return fn;
6245 }
6246
6247 /* Subroutine of convert_nontype_argument.
6248 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6249 Emit an error otherwise. */
6250
6251 static bool
6252 check_valid_ptrmem_cst_expr (tree type, tree expr,
6253 tsubst_flags_t complain)
6254 {
6255 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6256 tree orig_expr = expr;
6257 STRIP_NOPS (expr);
6258 if (null_ptr_cst_p (expr))
6259 return true;
6260 if (TREE_CODE (expr) == PTRMEM_CST
6261 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6262 PTRMEM_CST_CLASS (expr)))
6263 return true;
6264 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6265 return true;
6266 if (processing_template_decl
6267 && TREE_CODE (expr) == ADDR_EXPR
6268 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6269 return true;
6270 if (complain & tf_error)
6271 {
6272 error_at (loc, "%qE is not a valid template argument for type %qT",
6273 orig_expr, type);
6274 if (TREE_CODE (expr) != PTRMEM_CST)
6275 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6276 else
6277 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6278 }
6279 return false;
6280 }
6281
6282 /* Returns TRUE iff the address of OP is value-dependent.
6283
6284 14.6.2.4 [temp.dep.temp]:
6285 A non-integral non-type template-argument is dependent if its type is
6286 dependent or it has either of the following forms
6287 qualified-id
6288 & qualified-id
6289 and contains a nested-name-specifier which specifies a class-name that
6290 names a dependent type.
6291
6292 We generalize this to just say that the address of a member of a
6293 dependent class is value-dependent; the above doesn't cover the
6294 address of a static data member named with an unqualified-id. */
6295
6296 static bool
6297 has_value_dependent_address (tree op)
6298 {
6299 /* We could use get_inner_reference here, but there's no need;
6300 this is only relevant for template non-type arguments, which
6301 can only be expressed as &id-expression. */
6302 if (DECL_P (op))
6303 {
6304 tree ctx = CP_DECL_CONTEXT (op);
6305 if (TYPE_P (ctx) && dependent_type_p (ctx))
6306 return true;
6307 }
6308
6309 return false;
6310 }
6311
6312 /* The next set of functions are used for providing helpful explanatory
6313 diagnostics for failed overload resolution. Their messages should be
6314 indented by two spaces for consistency with the messages in
6315 call.c */
6316
6317 static int
6318 unify_success (bool /*explain_p*/)
6319 {
6320 return 0;
6321 }
6322
6323 /* Other failure functions should call this one, to provide a single function
6324 for setting a breakpoint on. */
6325
6326 static int
6327 unify_invalid (bool /*explain_p*/)
6328 {
6329 return 1;
6330 }
6331
6332 static int
6333 unify_parameter_deduction_failure (bool explain_p, tree parm)
6334 {
6335 if (explain_p)
6336 inform (input_location,
6337 " couldn't deduce template parameter %qD", parm);
6338 return unify_invalid (explain_p);
6339 }
6340
6341 static int
6342 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6343 {
6344 if (explain_p)
6345 inform (input_location,
6346 " types %qT and %qT have incompatible cv-qualifiers",
6347 parm, arg);
6348 return unify_invalid (explain_p);
6349 }
6350
6351 static int
6352 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6353 {
6354 if (explain_p)
6355 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6356 return unify_invalid (explain_p);
6357 }
6358
6359 static int
6360 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6361 {
6362 if (explain_p)
6363 inform (input_location,
6364 " template parameter %qD is not a parameter pack, but "
6365 "argument %qD is",
6366 parm, arg);
6367 return unify_invalid (explain_p);
6368 }
6369
6370 static int
6371 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6372 {
6373 if (explain_p)
6374 inform (input_location,
6375 " template argument %qE does not match "
6376 "pointer-to-member constant %qE",
6377 arg, parm);
6378 return unify_invalid (explain_p);
6379 }
6380
6381 static int
6382 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6383 {
6384 if (explain_p)
6385 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6386 return unify_invalid (explain_p);
6387 }
6388
6389 static int
6390 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6391 {
6392 if (explain_p)
6393 inform (input_location,
6394 " inconsistent parameter pack deduction with %qT and %qT",
6395 old_arg, new_arg);
6396 return unify_invalid (explain_p);
6397 }
6398
6399 static int
6400 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6401 {
6402 if (explain_p)
6403 {
6404 if (TYPE_P (parm))
6405 inform (input_location,
6406 " deduced conflicting types for parameter %qT (%qT and %qT)",
6407 parm, first, second);
6408 else
6409 inform (input_location,
6410 " deduced conflicting values for non-type parameter "
6411 "%qE (%qE and %qE)", parm, first, second);
6412 }
6413 return unify_invalid (explain_p);
6414 }
6415
6416 static int
6417 unify_vla_arg (bool explain_p, tree arg)
6418 {
6419 if (explain_p)
6420 inform (input_location,
6421 " variable-sized array type %qT is not "
6422 "a valid template argument",
6423 arg);
6424 return unify_invalid (explain_p);
6425 }
6426
6427 static int
6428 unify_method_type_error (bool explain_p, tree arg)
6429 {
6430 if (explain_p)
6431 inform (input_location,
6432 " member function type %qT is not a valid template argument",
6433 arg);
6434 return unify_invalid (explain_p);
6435 }
6436
6437 static int
6438 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6439 {
6440 if (explain_p)
6441 {
6442 if (least_p)
6443 inform_n (input_location, wanted,
6444 " candidate expects at least %d argument, %d provided",
6445 " candidate expects at least %d arguments, %d provided",
6446 wanted, have);
6447 else
6448 inform_n (input_location, wanted,
6449 " candidate expects %d argument, %d provided",
6450 " candidate expects %d arguments, %d provided",
6451 wanted, have);
6452 }
6453 return unify_invalid (explain_p);
6454 }
6455
6456 static int
6457 unify_too_many_arguments (bool explain_p, int have, int wanted)
6458 {
6459 return unify_arity (explain_p, have, wanted);
6460 }
6461
6462 static int
6463 unify_too_few_arguments (bool explain_p, int have, int wanted,
6464 bool least_p = false)
6465 {
6466 return unify_arity (explain_p, have, wanted, least_p);
6467 }
6468
6469 static int
6470 unify_arg_conversion (bool explain_p, tree to_type,
6471 tree from_type, tree arg)
6472 {
6473 if (explain_p)
6474 inform (cp_expr_loc_or_loc (arg, input_location),
6475 " cannot convert %qE (type %qT) to type %qT",
6476 arg, from_type, to_type);
6477 return unify_invalid (explain_p);
6478 }
6479
6480 static int
6481 unify_no_common_base (bool explain_p, enum template_base_result r,
6482 tree parm, tree arg)
6483 {
6484 if (explain_p)
6485 switch (r)
6486 {
6487 case tbr_ambiguous_baseclass:
6488 inform (input_location, " %qT is an ambiguous base class of %qT",
6489 parm, arg);
6490 break;
6491 default:
6492 inform (input_location, " %qT is not derived from %qT", arg, parm);
6493 break;
6494 }
6495 return unify_invalid (explain_p);
6496 }
6497
6498 static int
6499 unify_inconsistent_template_template_parameters (bool explain_p)
6500 {
6501 if (explain_p)
6502 inform (input_location,
6503 " template parameters of a template template argument are "
6504 "inconsistent with other deduced template arguments");
6505 return unify_invalid (explain_p);
6506 }
6507
6508 static int
6509 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6510 {
6511 if (explain_p)
6512 inform (input_location,
6513 " can't deduce a template for %qT from non-template type %qT",
6514 parm, arg);
6515 return unify_invalid (explain_p);
6516 }
6517
6518 static int
6519 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6520 {
6521 if (explain_p)
6522 inform (input_location,
6523 " template argument %qE does not match %qE", arg, parm);
6524 return unify_invalid (explain_p);
6525 }
6526
6527 /* True if T is a C++20 template parameter object to store the argument for a
6528 template parameter of class type. */
6529
6530 bool
6531 template_parm_object_p (const_tree t)
6532 {
6533 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6534 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6535 }
6536
6537 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6538 argument for TYPE, points to an unsuitable object. */
6539
6540 static bool
6541 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6542 {
6543 switch (TREE_CODE (expr))
6544 {
6545 CASE_CONVERT:
6546 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6547 complain);
6548
6549 case TARGET_EXPR:
6550 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6551 complain);
6552
6553 case CONSTRUCTOR:
6554 {
6555 unsigned i; tree elt;
6556 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6557 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6558 return true;
6559 }
6560 break;
6561
6562 case ADDR_EXPR:
6563 {
6564 tree decl = TREE_OPERAND (expr, 0);
6565
6566 if (!VAR_P (decl))
6567 {
6568 if (complain & tf_error)
6569 error ("%qE is not a valid template argument of type %qT "
6570 "because %qE is not a variable", expr, type, decl);
6571 return true;
6572 }
6573 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6574 {
6575 if (complain & tf_error)
6576 error ("%qE is not a valid template argument of type %qT "
6577 "in C++98 because %qD does not have external linkage",
6578 expr, type, decl);
6579 return true;
6580 }
6581 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6582 && decl_linkage (decl) == lk_none)
6583 {
6584 if (complain & tf_error)
6585 error ("%qE is not a valid template argument of type %qT "
6586 "because %qD has no linkage", expr, type, decl);
6587 return true;
6588 }
6589 /* C++17: For a non-type template-parameter of reference or pointer
6590 type, the value of the constant expression shall not refer to (or
6591 for a pointer type, shall not be the address of):
6592 * a subobject (4.5),
6593 * a temporary object (15.2),
6594 * a string literal (5.13.5),
6595 * the result of a typeid expression (8.2.8), or
6596 * a predefined __func__ variable (11.4.1). */
6597 else if (DECL_ARTIFICIAL (decl))
6598 {
6599 if (complain & tf_error)
6600 error ("the address of %qD is not a valid template argument",
6601 decl);
6602 return true;
6603 }
6604 else if (!same_type_ignoring_top_level_qualifiers_p
6605 (strip_array_types (TREE_TYPE (type)),
6606 strip_array_types (TREE_TYPE (decl))))
6607 {
6608 if (complain & tf_error)
6609 error ("the address of the %qT subobject of %qD is not a "
6610 "valid template argument", TREE_TYPE (type), decl);
6611 return true;
6612 }
6613 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6614 {
6615 if (complain & tf_error)
6616 error ("the address of %qD is not a valid template argument "
6617 "because it does not have static storage duration",
6618 decl);
6619 return true;
6620 }
6621 }
6622 break;
6623
6624 default:
6625 if (!INDIRECT_TYPE_P (type))
6626 /* We're only concerned about pointers and references here. */;
6627 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6628 /* Null pointer values are OK in C++11. */;
6629 else
6630 {
6631 if (VAR_P (expr))
6632 {
6633 if (complain & tf_error)
6634 error ("%qD is not a valid template argument "
6635 "because %qD is a variable, not the address of "
6636 "a variable", expr, expr);
6637 return true;
6638 }
6639 else
6640 {
6641 if (complain & tf_error)
6642 error ("%qE is not a valid template argument for %qT "
6643 "because it is not the address of a variable",
6644 expr, type);
6645 return true;
6646 }
6647 }
6648 }
6649 return false;
6650
6651 }
6652
6653 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6654 template argument EXPR. */
6655
6656 static tree
6657 get_template_parm_object (tree expr, tsubst_flags_t complain)
6658 {
6659 if (TREE_CODE (expr) == TARGET_EXPR)
6660 expr = TARGET_EXPR_INITIAL (expr);
6661
6662 if (!TREE_CONSTANT (expr))
6663 {
6664 if ((complain & tf_error)
6665 && require_rvalue_constant_expression (expr))
6666 cxx_constant_value (expr);
6667 return error_mark_node;
6668 }
6669 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6670 return error_mark_node;
6671
6672 tree name = mangle_template_parm_object (expr);
6673 tree decl = get_global_binding (name);
6674 if (decl)
6675 return decl;
6676
6677 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6678 decl = create_temporary_var (type);
6679 TREE_STATIC (decl) = true;
6680 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6681 TREE_READONLY (decl) = true;
6682 DECL_NAME (decl) = name;
6683 SET_DECL_ASSEMBLER_NAME (decl, name);
6684 DECL_CONTEXT (decl) = global_namespace;
6685 comdat_linkage (decl);
6686 pushdecl_top_level_and_finish (decl, expr);
6687 return decl;
6688 }
6689
6690 /* Attempt to convert the non-type template parameter EXPR to the
6691 indicated TYPE. If the conversion is successful, return the
6692 converted value. If the conversion is unsuccessful, return
6693 NULL_TREE if we issued an error message, or error_mark_node if we
6694 did not. We issue error messages for out-and-out bad template
6695 parameters, but not simply because the conversion failed, since we
6696 might be just trying to do argument deduction. Both TYPE and EXPR
6697 must be non-dependent.
6698
6699 The conversion follows the special rules described in
6700 [temp.arg.nontype], and it is much more strict than an implicit
6701 conversion.
6702
6703 This function is called twice for each template argument (see
6704 lookup_template_class for a more accurate description of this
6705 problem). This means that we need to handle expressions which
6706 are not valid in a C++ source, but can be created from the
6707 first call (for instance, casts to perform conversions). These
6708 hacks can go away after we fix the double coercion problem. */
6709
6710 static tree
6711 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6712 {
6713 tree expr_type;
6714 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6715
6716 /* Detect immediately string literals as invalid non-type argument.
6717 This special-case is not needed for correctness (we would easily
6718 catch this later), but only to provide better diagnostic for this
6719 common user mistake. As suggested by DR 100, we do not mention
6720 linkage issues in the diagnostic as this is not the point. */
6721 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6722 {
6723 if (complain & tf_error)
6724 error ("%qE is not a valid template argument for type %qT "
6725 "because string literals can never be used in this context",
6726 expr, type);
6727 return NULL_TREE;
6728 }
6729
6730 /* Add the ADDR_EXPR now for the benefit of
6731 value_dependent_expression_p. */
6732 if (TYPE_PTROBV_P (type)
6733 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6734 {
6735 expr = decay_conversion (expr, complain);
6736 if (expr == error_mark_node)
6737 return error_mark_node;
6738 }
6739
6740 /* If we are in a template, EXPR may be non-dependent, but still
6741 have a syntactic, rather than semantic, form. For example, EXPR
6742 might be a SCOPE_REF, rather than the VAR_DECL to which the
6743 SCOPE_REF refers. Preserving the qualifying scope is necessary
6744 so that access checking can be performed when the template is
6745 instantiated -- but here we need the resolved form so that we can
6746 convert the argument. */
6747 bool non_dep = false;
6748 if (TYPE_REF_OBJ_P (type)
6749 && has_value_dependent_address (expr))
6750 /* If we want the address and it's value-dependent, don't fold. */;
6751 else if (processing_template_decl
6752 && is_nondependent_constant_expression (expr))
6753 non_dep = true;
6754 if (error_operand_p (expr))
6755 return error_mark_node;
6756 expr_type = TREE_TYPE (expr);
6757
6758 /* If the argument is non-dependent, perform any conversions in
6759 non-dependent context as well. */
6760 processing_template_decl_sentinel s (non_dep);
6761 if (non_dep)
6762 expr = instantiate_non_dependent_expr_internal (expr, complain);
6763
6764 if (value_dependent_expression_p (expr))
6765 expr = canonicalize_expr_argument (expr, complain);
6766
6767 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6768 to a non-type argument of "nullptr". */
6769 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6770 expr = fold_simple (convert (type, expr));
6771
6772 /* In C++11, integral or enumeration non-type template arguments can be
6773 arbitrary constant expressions. Pointer and pointer to
6774 member arguments can be general constant expressions that evaluate
6775 to a null value, but otherwise still need to be of a specific form. */
6776 if (cxx_dialect >= cxx11)
6777 {
6778 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6779 /* A PTRMEM_CST is already constant, and a valid template
6780 argument for a parameter of pointer to member type, we just want
6781 to leave it in that form rather than lower it to a
6782 CONSTRUCTOR. */;
6783 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6784 || cxx_dialect >= cxx17)
6785 {
6786 /* C++17: A template-argument for a non-type template-parameter shall
6787 be a converted constant expression (8.20) of the type of the
6788 template-parameter. */
6789 expr = build_converted_constant_expr (type, expr, complain);
6790 if (expr == error_mark_node)
6791 /* Make sure we return NULL_TREE only if we have really issued
6792 an error, as described above. */
6793 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6794 expr = maybe_constant_value (expr);
6795 expr = convert_from_reference (expr);
6796 }
6797 else if (TYPE_PTR_OR_PTRMEM_P (type))
6798 {
6799 tree folded = maybe_constant_value (expr);
6800 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6801 : null_member_pointer_value_p (folded))
6802 expr = folded;
6803 }
6804 }
6805
6806 if (TYPE_REF_P (type))
6807 expr = mark_lvalue_use (expr);
6808 else
6809 expr = mark_rvalue_use (expr);
6810
6811 /* HACK: Due to double coercion, we can get a
6812 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6813 which is the tree that we built on the first call (see
6814 below when coercing to reference to object or to reference to
6815 function). We just strip everything and get to the arg.
6816 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6817 for examples. */
6818 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6819 {
6820 tree probe_type, probe = expr;
6821 if (REFERENCE_REF_P (probe))
6822 probe = TREE_OPERAND (probe, 0);
6823 probe_type = TREE_TYPE (probe);
6824 if (TREE_CODE (probe) == NOP_EXPR)
6825 {
6826 /* ??? Maybe we could use convert_from_reference here, but we
6827 would need to relax its constraints because the NOP_EXPR
6828 could actually change the type to something more cv-qualified,
6829 and this is not folded by convert_from_reference. */
6830 tree addr = TREE_OPERAND (probe, 0);
6831 if (TYPE_REF_P (probe_type)
6832 && TREE_CODE (addr) == ADDR_EXPR
6833 && TYPE_PTR_P (TREE_TYPE (addr))
6834 && (same_type_ignoring_top_level_qualifiers_p
6835 (TREE_TYPE (probe_type),
6836 TREE_TYPE (TREE_TYPE (addr)))))
6837 {
6838 expr = TREE_OPERAND (addr, 0);
6839 expr_type = TREE_TYPE (probe_type);
6840 }
6841 }
6842 }
6843
6844 /* [temp.arg.nontype]/5, bullet 1
6845
6846 For a non-type template-parameter of integral or enumeration type,
6847 integral promotions (_conv.prom_) and integral conversions
6848 (_conv.integral_) are applied. */
6849 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6850 {
6851 if (cxx_dialect < cxx11)
6852 {
6853 tree t = build_converted_constant_expr (type, expr, complain);
6854 t = maybe_constant_value (t);
6855 if (t != error_mark_node)
6856 expr = t;
6857 }
6858
6859 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6860 return error_mark_node;
6861
6862 /* Notice that there are constant expressions like '4 % 0' which
6863 do not fold into integer constants. */
6864 if (TREE_CODE (expr) != INTEGER_CST
6865 && !value_dependent_expression_p (expr))
6866 {
6867 if (complain & tf_error)
6868 {
6869 int errs = errorcount, warns = warningcount + werrorcount;
6870 if (!require_potential_constant_expression (expr))
6871 expr = error_mark_node;
6872 else
6873 expr = cxx_constant_value (expr);
6874 if (errorcount > errs || warningcount + werrorcount > warns)
6875 inform (loc, "in template argument for type %qT", type);
6876 if (expr == error_mark_node)
6877 return NULL_TREE;
6878 /* else cxx_constant_value complained but gave us
6879 a real constant, so go ahead. */
6880 if (TREE_CODE (expr) != INTEGER_CST)
6881 {
6882 /* Some assemble time constant expressions like
6883 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6884 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6885 as we can emit them into .rodata initializers of
6886 variables, yet they can't fold into an INTEGER_CST at
6887 compile time. Refuse them here. */
6888 gcc_checking_assert (reduced_constant_expression_p (expr));
6889 error_at (loc, "template argument %qE for type %qT not "
6890 "a constant integer", expr, type);
6891 return NULL_TREE;
6892 }
6893 }
6894 else
6895 return NULL_TREE;
6896 }
6897
6898 /* Avoid typedef problems. */
6899 if (TREE_TYPE (expr) != type)
6900 expr = fold_convert (type, expr);
6901 }
6902 /* [temp.arg.nontype]/5, bullet 2
6903
6904 For a non-type template-parameter of type pointer to object,
6905 qualification conversions (_conv.qual_) and the array-to-pointer
6906 conversion (_conv.array_) are applied. */
6907 else if (TYPE_PTROBV_P (type))
6908 {
6909 tree decayed = expr;
6910
6911 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6912 decay_conversion or an explicit cast. If it's a problematic cast,
6913 we'll complain about it below. */
6914 if (TREE_CODE (expr) == NOP_EXPR)
6915 {
6916 tree probe = expr;
6917 STRIP_NOPS (probe);
6918 if (TREE_CODE (probe) == ADDR_EXPR
6919 && TYPE_PTR_P (TREE_TYPE (probe)))
6920 {
6921 expr = probe;
6922 expr_type = TREE_TYPE (expr);
6923 }
6924 }
6925
6926 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6927
6928 A template-argument for a non-type, non-template template-parameter
6929 shall be one of: [...]
6930
6931 -- the name of a non-type template-parameter;
6932 -- the address of an object or function with external linkage, [...]
6933 expressed as "& id-expression" where the & is optional if the name
6934 refers to a function or array, or if the corresponding
6935 template-parameter is a reference.
6936
6937 Here, we do not care about functions, as they are invalid anyway
6938 for a parameter of type pointer-to-object. */
6939
6940 if (value_dependent_expression_p (expr))
6941 /* Non-type template parameters are OK. */
6942 ;
6943 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6944 /* Null pointer values are OK in C++11. */;
6945 else if (TREE_CODE (expr) != ADDR_EXPR
6946 && !INDIRECT_TYPE_P (expr_type))
6947 /* Other values, like integer constants, might be valid
6948 non-type arguments of some other type. */
6949 return error_mark_node;
6950 else if (invalid_tparm_referent_p (type, expr, complain))
6951 return NULL_TREE;
6952
6953 expr = decayed;
6954
6955 expr = perform_qualification_conversions (type, expr);
6956 if (expr == error_mark_node)
6957 return error_mark_node;
6958 }
6959 /* [temp.arg.nontype]/5, bullet 3
6960
6961 For a non-type template-parameter of type reference to object, no
6962 conversions apply. The type referred to by the reference may be more
6963 cv-qualified than the (otherwise identical) type of the
6964 template-argument. The template-parameter is bound directly to the
6965 template-argument, which must be an lvalue. */
6966 else if (TYPE_REF_OBJ_P (type))
6967 {
6968 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6969 expr_type))
6970 return error_mark_node;
6971
6972 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6973 {
6974 if (complain & tf_error)
6975 error ("%qE is not a valid template argument for type %qT "
6976 "because of conflicts in cv-qualification", expr, type);
6977 return NULL_TREE;
6978 }
6979
6980 if (!lvalue_p (expr))
6981 {
6982 if (complain & tf_error)
6983 error ("%qE is not a valid template argument for type %qT "
6984 "because it is not an lvalue", expr, type);
6985 return NULL_TREE;
6986 }
6987
6988 /* [temp.arg.nontype]/1
6989
6990 A template-argument for a non-type, non-template template-parameter
6991 shall be one of: [...]
6992
6993 -- the address of an object or function with external linkage. */
6994 if (INDIRECT_REF_P (expr)
6995 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6996 {
6997 expr = TREE_OPERAND (expr, 0);
6998 if (DECL_P (expr))
6999 {
7000 if (complain & tf_error)
7001 error ("%q#D is not a valid template argument for type %qT "
7002 "because a reference variable does not have a constant "
7003 "address", expr, type);
7004 return NULL_TREE;
7005 }
7006 }
7007
7008 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7009 && value_dependent_expression_p (expr))
7010 /* OK, dependent reference. We don't want to ask whether a DECL is
7011 itself value-dependent, since what we want here is its address. */;
7012 else
7013 {
7014 expr = build_address (expr);
7015
7016 if (invalid_tparm_referent_p (type, expr, complain))
7017 return NULL_TREE;
7018 }
7019
7020 if (!same_type_p (type, TREE_TYPE (expr)))
7021 expr = build_nop (type, expr);
7022 }
7023 /* [temp.arg.nontype]/5, bullet 4
7024
7025 For a non-type template-parameter of type pointer to function, only
7026 the function-to-pointer conversion (_conv.func_) is applied. If the
7027 template-argument represents a set of overloaded functions (or a
7028 pointer to such), the matching function is selected from the set
7029 (_over.over_). */
7030 else if (TYPE_PTRFN_P (type))
7031 {
7032 /* If the argument is a template-id, we might not have enough
7033 context information to decay the pointer. */
7034 if (!type_unknown_p (expr_type))
7035 {
7036 expr = decay_conversion (expr, complain);
7037 if (expr == error_mark_node)
7038 return error_mark_node;
7039 }
7040
7041 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7042 /* Null pointer values are OK in C++11. */
7043 return perform_qualification_conversions (type, expr);
7044
7045 expr = convert_nontype_argument_function (type, expr, complain);
7046 if (!expr || expr == error_mark_node)
7047 return expr;
7048 }
7049 /* [temp.arg.nontype]/5, bullet 5
7050
7051 For a non-type template-parameter of type reference to function, no
7052 conversions apply. If the template-argument represents a set of
7053 overloaded functions, the matching function is selected from the set
7054 (_over.over_). */
7055 else if (TYPE_REFFN_P (type))
7056 {
7057 if (TREE_CODE (expr) == ADDR_EXPR)
7058 {
7059 if (complain & tf_error)
7060 {
7061 error ("%qE is not a valid template argument for type %qT "
7062 "because it is a pointer", expr, type);
7063 inform (input_location, "try using %qE instead",
7064 TREE_OPERAND (expr, 0));
7065 }
7066 return NULL_TREE;
7067 }
7068
7069 expr = convert_nontype_argument_function (type, expr, complain);
7070 if (!expr || expr == error_mark_node)
7071 return expr;
7072 }
7073 /* [temp.arg.nontype]/5, bullet 6
7074
7075 For a non-type template-parameter of type pointer to member function,
7076 no conversions apply. If the template-argument represents a set of
7077 overloaded member functions, the matching member function is selected
7078 from the set (_over.over_). */
7079 else if (TYPE_PTRMEMFUNC_P (type))
7080 {
7081 expr = instantiate_type (type, expr, tf_none);
7082 if (expr == error_mark_node)
7083 return error_mark_node;
7084
7085 /* [temp.arg.nontype] bullet 1 says the pointer to member
7086 expression must be a pointer-to-member constant. */
7087 if (!value_dependent_expression_p (expr)
7088 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7089 return NULL_TREE;
7090
7091 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7092 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7093 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7094 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7095 }
7096 /* [temp.arg.nontype]/5, bullet 7
7097
7098 For a non-type template-parameter of type pointer to data member,
7099 qualification conversions (_conv.qual_) are applied. */
7100 else if (TYPE_PTRDATAMEM_P (type))
7101 {
7102 /* [temp.arg.nontype] bullet 1 says the pointer to member
7103 expression must be a pointer-to-member constant. */
7104 if (!value_dependent_expression_p (expr)
7105 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7106 return NULL_TREE;
7107
7108 expr = perform_qualification_conversions (type, expr);
7109 if (expr == error_mark_node)
7110 return expr;
7111 }
7112 else if (NULLPTR_TYPE_P (type))
7113 {
7114 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7115 {
7116 if (complain & tf_error)
7117 error ("%qE is not a valid template argument for type %qT "
7118 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7119 return NULL_TREE;
7120 }
7121 return expr;
7122 }
7123 else if (CLASS_TYPE_P (type))
7124 {
7125 /* Replace the argument with a reference to the corresponding template
7126 parameter object. */
7127 if (!value_dependent_expression_p (expr))
7128 expr = get_template_parm_object (expr, complain);
7129 if (expr == error_mark_node)
7130 return NULL_TREE;
7131 }
7132 /* A template non-type parameter must be one of the above. */
7133 else
7134 gcc_unreachable ();
7135
7136 /* Sanity check: did we actually convert the argument to the
7137 right type? */
7138 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7139 (type, TREE_TYPE (expr)));
7140 return convert_from_reference (expr);
7141 }
7142
7143 /* Subroutine of coerce_template_template_parms, which returns 1 if
7144 PARM_PARM and ARG_PARM match using the rule for the template
7145 parameters of template template parameters. Both PARM and ARG are
7146 template parameters; the rest of the arguments are the same as for
7147 coerce_template_template_parms.
7148 */
7149 static int
7150 coerce_template_template_parm (tree parm,
7151 tree arg,
7152 tsubst_flags_t complain,
7153 tree in_decl,
7154 tree outer_args)
7155 {
7156 if (arg == NULL_TREE || error_operand_p (arg)
7157 || parm == NULL_TREE || error_operand_p (parm))
7158 return 0;
7159
7160 if (TREE_CODE (arg) != TREE_CODE (parm))
7161 return 0;
7162
7163 switch (TREE_CODE (parm))
7164 {
7165 case TEMPLATE_DECL:
7166 /* We encounter instantiations of templates like
7167 template <template <template <class> class> class TT>
7168 class C; */
7169 {
7170 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7171 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7172
7173 if (!coerce_template_template_parms
7174 (parmparm, argparm, complain, in_decl, outer_args))
7175 return 0;
7176 }
7177 /* Fall through. */
7178
7179 case TYPE_DECL:
7180 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7181 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7182 /* Argument is a parameter pack but parameter is not. */
7183 return 0;
7184 break;
7185
7186 case PARM_DECL:
7187 /* The tsubst call is used to handle cases such as
7188
7189 template <int> class C {};
7190 template <class T, template <T> class TT> class D {};
7191 D<int, C> d;
7192
7193 i.e. the parameter list of TT depends on earlier parameters. */
7194 if (!uses_template_parms (TREE_TYPE (arg)))
7195 {
7196 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7197 if (!uses_template_parms (t)
7198 && !same_type_p (t, TREE_TYPE (arg)))
7199 return 0;
7200 }
7201
7202 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7203 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7204 /* Argument is a parameter pack but parameter is not. */
7205 return 0;
7206
7207 break;
7208
7209 default:
7210 gcc_unreachable ();
7211 }
7212
7213 return 1;
7214 }
7215
7216 /* Coerce template argument list ARGLIST for use with template
7217 template-parameter TEMPL. */
7218
7219 static tree
7220 coerce_template_args_for_ttp (tree templ, tree arglist,
7221 tsubst_flags_t complain)
7222 {
7223 /* Consider an example where a template template parameter declared as
7224
7225 template <class T, class U = std::allocator<T> > class TT
7226
7227 The template parameter level of T and U are one level larger than
7228 of TT. To proper process the default argument of U, say when an
7229 instantiation `TT<int>' is seen, we need to build the full
7230 arguments containing {int} as the innermost level. Outer levels,
7231 available when not appearing as default template argument, can be
7232 obtained from the arguments of the enclosing template.
7233
7234 Suppose that TT is later substituted with std::vector. The above
7235 instantiation is `TT<int, std::allocator<T> >' with TT at
7236 level 1, and T at level 2, while the template arguments at level 1
7237 becomes {std::vector} and the inner level 2 is {int}. */
7238
7239 tree outer = DECL_CONTEXT (templ);
7240 if (outer)
7241 {
7242 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7243 /* We want arguments for the partial specialization, not arguments for
7244 the primary template. */
7245 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7246 else
7247 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7248 }
7249 else if (current_template_parms)
7250 {
7251 /* This is an argument of the current template, so we haven't set
7252 DECL_CONTEXT yet. */
7253 tree relevant_template_parms;
7254
7255 /* Parameter levels that are greater than the level of the given
7256 template template parm are irrelevant. */
7257 relevant_template_parms = current_template_parms;
7258 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7259 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7260 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7261
7262 outer = template_parms_to_args (relevant_template_parms);
7263 }
7264
7265 if (outer)
7266 arglist = add_to_template_args (outer, arglist);
7267
7268 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7269 return coerce_template_parms (parmlist, arglist, templ,
7270 complain,
7271 /*require_all_args=*/true,
7272 /*use_default_args=*/true);
7273 }
7274
7275 /* A cache of template template parameters with match-all default
7276 arguments. */
7277 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7278 static void
7279 store_defaulted_ttp (tree v, tree t)
7280 {
7281 if (!defaulted_ttp_cache)
7282 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7283 defaulted_ttp_cache->put (v, t);
7284 }
7285 static tree
7286 lookup_defaulted_ttp (tree v)
7287 {
7288 if (defaulted_ttp_cache)
7289 if (tree *p = defaulted_ttp_cache->get (v))
7290 return *p;
7291 return NULL_TREE;
7292 }
7293
7294 /* T is a bound template template-parameter. Copy its arguments into default
7295 arguments of the template template-parameter's template parameters. */
7296
7297 static tree
7298 add_defaults_to_ttp (tree otmpl)
7299 {
7300 if (tree c = lookup_defaulted_ttp (otmpl))
7301 return c;
7302
7303 tree ntmpl = copy_node (otmpl);
7304
7305 tree ntype = copy_node (TREE_TYPE (otmpl));
7306 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7307 TYPE_MAIN_VARIANT (ntype) = ntype;
7308 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7309 TYPE_NAME (ntype) = ntmpl;
7310 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7311
7312 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7313 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7314 TEMPLATE_PARM_DECL (idx) = ntmpl;
7315 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7316
7317 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7318 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7319 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7320 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7321 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7322 {
7323 tree o = TREE_VEC_ELT (vec, i);
7324 if (!template_parameter_pack_p (TREE_VALUE (o)))
7325 {
7326 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7327 TREE_PURPOSE (n) = any_targ_node;
7328 }
7329 }
7330
7331 store_defaulted_ttp (otmpl, ntmpl);
7332 return ntmpl;
7333 }
7334
7335 /* ARG is a bound potential template template-argument, and PARGS is a list
7336 of arguments for the corresponding template template-parameter. Adjust
7337 PARGS as appropriate for application to ARG's template, and if ARG is a
7338 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7339 arguments to the template template parameter. */
7340
7341 static tree
7342 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7343 {
7344 ++processing_template_decl;
7345 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7346 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7347 {
7348 /* When comparing two template template-parameters in partial ordering,
7349 rewrite the one currently being used as an argument to have default
7350 arguments for all parameters. */
7351 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7352 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7353 if (pargs != error_mark_node)
7354 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7355 TYPE_TI_ARGS (arg));
7356 }
7357 else
7358 {
7359 tree aparms
7360 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7361 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7362 /*require_all*/true,
7363 /*use_default*/true);
7364 }
7365 --processing_template_decl;
7366 return pargs;
7367 }
7368
7369 /* Subroutine of unify for the case when PARM is a
7370 BOUND_TEMPLATE_TEMPLATE_PARM. */
7371
7372 static int
7373 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7374 bool explain_p)
7375 {
7376 tree parmvec = TYPE_TI_ARGS (parm);
7377 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7378
7379 /* The template template parm might be variadic and the argument
7380 not, so flatten both argument lists. */
7381 parmvec = expand_template_argument_pack (parmvec);
7382 argvec = expand_template_argument_pack (argvec);
7383
7384 if (flag_new_ttp)
7385 {
7386 /* In keeping with P0522R0, adjust P's template arguments
7387 to apply to A's template; then flatten it again. */
7388 tree nparmvec = parmvec;
7389 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7390 nparmvec = expand_template_argument_pack (nparmvec);
7391
7392 if (unify (tparms, targs, nparmvec, argvec,
7393 UNIFY_ALLOW_NONE, explain_p))
7394 return 1;
7395
7396 /* If the P0522 adjustment eliminated a pack expansion, deduce
7397 empty packs. */
7398 if (flag_new_ttp
7399 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7400 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7401 DEDUCE_EXACT, /*sub*/true, explain_p))
7402 return 1;
7403 }
7404 else
7405 {
7406 /* Deduce arguments T, i from TT<T> or TT<i>.
7407 We check each element of PARMVEC and ARGVEC individually
7408 rather than the whole TREE_VEC since they can have
7409 different number of elements, which is allowed under N2555. */
7410
7411 int len = TREE_VEC_LENGTH (parmvec);
7412
7413 /* Check if the parameters end in a pack, making them
7414 variadic. */
7415 int parm_variadic_p = 0;
7416 if (len > 0
7417 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7418 parm_variadic_p = 1;
7419
7420 for (int i = 0; i < len - parm_variadic_p; ++i)
7421 /* If the template argument list of P contains a pack
7422 expansion that is not the last template argument, the
7423 entire template argument list is a non-deduced
7424 context. */
7425 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7426 return unify_success (explain_p);
7427
7428 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7429 return unify_too_few_arguments (explain_p,
7430 TREE_VEC_LENGTH (argvec), len);
7431
7432 for (int i = 0; i < len - parm_variadic_p; ++i)
7433 if (unify (tparms, targs,
7434 TREE_VEC_ELT (parmvec, i),
7435 TREE_VEC_ELT (argvec, i),
7436 UNIFY_ALLOW_NONE, explain_p))
7437 return 1;
7438
7439 if (parm_variadic_p
7440 && unify_pack_expansion (tparms, targs,
7441 parmvec, argvec,
7442 DEDUCE_EXACT,
7443 /*subr=*/true, explain_p))
7444 return 1;
7445 }
7446
7447 return 0;
7448 }
7449
7450 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7451 template template parameters. Both PARM_PARMS and ARG_PARMS are
7452 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7453 or PARM_DECL.
7454
7455 Consider the example:
7456 template <class T> class A;
7457 template<template <class U> class TT> class B;
7458
7459 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7460 the parameters to A, and OUTER_ARGS contains A. */
7461
7462 static int
7463 coerce_template_template_parms (tree parm_parms,
7464 tree arg_parms,
7465 tsubst_flags_t complain,
7466 tree in_decl,
7467 tree outer_args)
7468 {
7469 int nparms, nargs, i;
7470 tree parm, arg;
7471 int variadic_p = 0;
7472
7473 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7474 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7475
7476 nparms = TREE_VEC_LENGTH (parm_parms);
7477 nargs = TREE_VEC_LENGTH (arg_parms);
7478
7479 if (flag_new_ttp)
7480 {
7481 /* P0522R0: A template template-parameter P is at least as specialized as
7482 a template template-argument A if, given the following rewrite to two
7483 function templates, the function template corresponding to P is at
7484 least as specialized as the function template corresponding to A
7485 according to the partial ordering rules for function templates
7486 ([temp.func.order]). Given an invented class template X with the
7487 template parameter list of A (including default arguments):
7488
7489 * Each of the two function templates has the same template parameters,
7490 respectively, as P or A.
7491
7492 * Each function template has a single function parameter whose type is
7493 a specialization of X with template arguments corresponding to the
7494 template parameters from the respective function template where, for
7495 each template parameter PP in the template parameter list of the
7496 function template, a corresponding template argument AA is formed. If
7497 PP declares a parameter pack, then AA is the pack expansion
7498 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7499
7500 If the rewrite produces an invalid type, then P is not at least as
7501 specialized as A. */
7502
7503 /* So coerce P's args to apply to A's parms, and then deduce between A's
7504 args and the converted args. If that succeeds, A is at least as
7505 specialized as P, so they match.*/
7506 tree pargs = template_parms_level_to_args (parm_parms);
7507 ++processing_template_decl;
7508 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7509 /*require_all*/true, /*use_default*/true);
7510 --processing_template_decl;
7511 if (pargs != error_mark_node)
7512 {
7513 tree targs = make_tree_vec (nargs);
7514 tree aargs = template_parms_level_to_args (arg_parms);
7515 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7516 /*explain*/false))
7517 return 1;
7518 }
7519 }
7520
7521 /* Determine whether we have a parameter pack at the end of the
7522 template template parameter's template parameter list. */
7523 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7524 {
7525 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7526
7527 if (error_operand_p (parm))
7528 return 0;
7529
7530 switch (TREE_CODE (parm))
7531 {
7532 case TEMPLATE_DECL:
7533 case TYPE_DECL:
7534 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7535 variadic_p = 1;
7536 break;
7537
7538 case PARM_DECL:
7539 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7540 variadic_p = 1;
7541 break;
7542
7543 default:
7544 gcc_unreachable ();
7545 }
7546 }
7547
7548 if (nargs != nparms
7549 && !(variadic_p && nargs >= nparms - 1))
7550 return 0;
7551
7552 /* Check all of the template parameters except the parameter pack at
7553 the end (if any). */
7554 for (i = 0; i < nparms - variadic_p; ++i)
7555 {
7556 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7557 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7558 continue;
7559
7560 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7561 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7562
7563 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7564 outer_args))
7565 return 0;
7566
7567 }
7568
7569 if (variadic_p)
7570 {
7571 /* Check each of the template parameters in the template
7572 argument against the template parameter pack at the end of
7573 the template template parameter. */
7574 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7575 return 0;
7576
7577 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7578
7579 for (; i < nargs; ++i)
7580 {
7581 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7582 continue;
7583
7584 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7585
7586 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7587 outer_args))
7588 return 0;
7589 }
7590 }
7591
7592 return 1;
7593 }
7594
7595 /* Verifies that the deduced template arguments (in TARGS) for the
7596 template template parameters (in TPARMS) represent valid bindings,
7597 by comparing the template parameter list of each template argument
7598 to the template parameter list of its corresponding template
7599 template parameter, in accordance with DR150. This
7600 routine can only be called after all template arguments have been
7601 deduced. It will return TRUE if all of the template template
7602 parameter bindings are okay, FALSE otherwise. */
7603 bool
7604 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7605 {
7606 int i, ntparms = TREE_VEC_LENGTH (tparms);
7607 bool ret = true;
7608
7609 /* We're dealing with template parms in this process. */
7610 ++processing_template_decl;
7611
7612 targs = INNERMOST_TEMPLATE_ARGS (targs);
7613
7614 for (i = 0; i < ntparms; ++i)
7615 {
7616 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7617 tree targ = TREE_VEC_ELT (targs, i);
7618
7619 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7620 {
7621 tree packed_args = NULL_TREE;
7622 int idx, len = 1;
7623
7624 if (ARGUMENT_PACK_P (targ))
7625 {
7626 /* Look inside the argument pack. */
7627 packed_args = ARGUMENT_PACK_ARGS (targ);
7628 len = TREE_VEC_LENGTH (packed_args);
7629 }
7630
7631 for (idx = 0; idx < len; ++idx)
7632 {
7633 tree targ_parms = NULL_TREE;
7634
7635 if (packed_args)
7636 /* Extract the next argument from the argument
7637 pack. */
7638 targ = TREE_VEC_ELT (packed_args, idx);
7639
7640 if (PACK_EXPANSION_P (targ))
7641 /* Look at the pattern of the pack expansion. */
7642 targ = PACK_EXPANSION_PATTERN (targ);
7643
7644 /* Extract the template parameters from the template
7645 argument. */
7646 if (TREE_CODE (targ) == TEMPLATE_DECL)
7647 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7648 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7649 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7650
7651 /* Verify that we can coerce the template template
7652 parameters from the template argument to the template
7653 parameter. This requires an exact match. */
7654 if (targ_parms
7655 && !coerce_template_template_parms
7656 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7657 targ_parms,
7658 tf_none,
7659 tparm,
7660 targs))
7661 {
7662 ret = false;
7663 goto out;
7664 }
7665 }
7666 }
7667 }
7668
7669 out:
7670
7671 --processing_template_decl;
7672 return ret;
7673 }
7674
7675 /* Since type attributes aren't mangled, we need to strip them from
7676 template type arguments. */
7677
7678 static tree
7679 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7680 {
7681 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7682 return arg;
7683 bool removed_attributes = false;
7684 tree canon = strip_typedefs (arg, &removed_attributes);
7685 if (removed_attributes
7686 && (complain & tf_warning))
7687 warning (OPT_Wignored_attributes,
7688 "ignoring attributes on template argument %qT", arg);
7689 return canon;
7690 }
7691
7692 /* And from inside dependent non-type arguments like sizeof(Type). */
7693
7694 static tree
7695 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7696 {
7697 if (!arg || arg == error_mark_node)
7698 return arg;
7699 bool removed_attributes = false;
7700 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7701 if (removed_attributes
7702 && (complain & tf_warning))
7703 warning (OPT_Wignored_attributes,
7704 "ignoring attributes in template argument %qE", arg);
7705 return canon;
7706 }
7707
7708 // A template declaration can be substituted for a constrained
7709 // template template parameter only when the argument is more
7710 // constrained than the parameter.
7711 static bool
7712 is_compatible_template_arg (tree parm, tree arg)
7713 {
7714 tree parm_cons = get_constraints (parm);
7715
7716 /* For now, allow constrained template template arguments
7717 and unconstrained template template parameters. */
7718 if (parm_cons == NULL_TREE)
7719 return true;
7720
7721 tree arg_cons = get_constraints (arg);
7722
7723 // If the template parameter is constrained, we need to rewrite its
7724 // constraints in terms of the ARG's template parameters. This ensures
7725 // that all of the template parameter types will have the same depth.
7726 //
7727 // Note that this is only valid when coerce_template_template_parm is
7728 // true for the innermost template parameters of PARM and ARG. In other
7729 // words, because coercion is successful, this conversion will be valid.
7730 if (parm_cons)
7731 {
7732 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7733 parm_cons = tsubst_constraint_info (parm_cons,
7734 INNERMOST_TEMPLATE_ARGS (args),
7735 tf_none, NULL_TREE);
7736 if (parm_cons == error_mark_node)
7737 return false;
7738 }
7739
7740 return subsumes (parm_cons, arg_cons);
7741 }
7742
7743 // Convert a placeholder argument into a binding to the original
7744 // parameter. The original parameter is saved as the TREE_TYPE of
7745 // ARG.
7746 static inline tree
7747 convert_wildcard_argument (tree parm, tree arg)
7748 {
7749 TREE_TYPE (arg) = parm;
7750 return arg;
7751 }
7752
7753 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7754 because one of them is dependent. But we need to represent the
7755 conversion for the benefit of cp_tree_equal. */
7756
7757 static tree
7758 maybe_convert_nontype_argument (tree type, tree arg)
7759 {
7760 /* Auto parms get no conversion. */
7761 if (type_uses_auto (type))
7762 return arg;
7763 /* We don't need or want to add this conversion now if we're going to use the
7764 argument for deduction. */
7765 if (value_dependent_expression_p (arg))
7766 return arg;
7767
7768 type = cv_unqualified (type);
7769 tree argtype = TREE_TYPE (arg);
7770 if (same_type_p (type, argtype))
7771 return arg;
7772
7773 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7774 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7775 return arg;
7776 }
7777
7778 /* Convert the indicated template ARG as necessary to match the
7779 indicated template PARM. Returns the converted ARG, or
7780 error_mark_node if the conversion was unsuccessful. Error and
7781 warning messages are issued under control of COMPLAIN. This
7782 conversion is for the Ith parameter in the parameter list. ARGS is
7783 the full set of template arguments deduced so far. */
7784
7785 static tree
7786 convert_template_argument (tree parm,
7787 tree arg,
7788 tree args,
7789 tsubst_flags_t complain,
7790 int i,
7791 tree in_decl)
7792 {
7793 tree orig_arg;
7794 tree val;
7795 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7796
7797 if (parm == error_mark_node || error_operand_p (arg))
7798 return error_mark_node;
7799
7800 /* Trivially convert placeholders. */
7801 if (TREE_CODE (arg) == WILDCARD_DECL)
7802 return convert_wildcard_argument (parm, arg);
7803
7804 if (arg == any_targ_node)
7805 return arg;
7806
7807 if (TREE_CODE (arg) == TREE_LIST
7808 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7809 {
7810 /* The template argument was the name of some
7811 member function. That's usually
7812 invalid, but static members are OK. In any
7813 case, grab the underlying fields/functions
7814 and issue an error later if required. */
7815 orig_arg = TREE_VALUE (arg);
7816 TREE_TYPE (arg) = unknown_type_node;
7817 }
7818
7819 orig_arg = arg;
7820
7821 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7822 requires_type = (TREE_CODE (parm) == TYPE_DECL
7823 || requires_tmpl_type);
7824
7825 /* When determining whether an argument pack expansion is a template,
7826 look at the pattern. */
7827 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7828 arg = PACK_EXPANSION_PATTERN (arg);
7829
7830 /* Deal with an injected-class-name used as a template template arg. */
7831 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7832 {
7833 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7834 if (TREE_CODE (t) == TEMPLATE_DECL)
7835 {
7836 if (cxx_dialect >= cxx11)
7837 /* OK under DR 1004. */;
7838 else if (complain & tf_warning_or_error)
7839 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7840 " used as template template argument", TYPE_NAME (arg));
7841 else if (flag_pedantic_errors)
7842 t = arg;
7843
7844 arg = t;
7845 }
7846 }
7847
7848 is_tmpl_type =
7849 ((TREE_CODE (arg) == TEMPLATE_DECL
7850 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7851 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7852 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7853 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7854
7855 if (is_tmpl_type
7856 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7857 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7858 arg = TYPE_STUB_DECL (arg);
7859
7860 is_type = TYPE_P (arg) || is_tmpl_type;
7861
7862 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7863 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7864 {
7865 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7866 {
7867 if (complain & tf_error)
7868 error ("invalid use of destructor %qE as a type", orig_arg);
7869 return error_mark_node;
7870 }
7871
7872 permerror (input_location,
7873 "to refer to a type member of a template parameter, "
7874 "use %<typename %E%>", orig_arg);
7875
7876 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7877 TREE_OPERAND (arg, 1),
7878 typename_type,
7879 complain);
7880 arg = orig_arg;
7881 is_type = 1;
7882 }
7883 if (is_type != requires_type)
7884 {
7885 if (in_decl)
7886 {
7887 if (complain & tf_error)
7888 {
7889 error ("type/value mismatch at argument %d in template "
7890 "parameter list for %qD",
7891 i + 1, in_decl);
7892 if (is_type)
7893 inform (input_location,
7894 " expected a constant of type %qT, got %qT",
7895 TREE_TYPE (parm),
7896 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7897 else if (requires_tmpl_type)
7898 inform (input_location,
7899 " expected a class template, got %qE", orig_arg);
7900 else
7901 inform (input_location,
7902 " expected a type, got %qE", orig_arg);
7903 }
7904 }
7905 return error_mark_node;
7906 }
7907 if (is_tmpl_type ^ requires_tmpl_type)
7908 {
7909 if (in_decl && (complain & tf_error))
7910 {
7911 error ("type/value mismatch at argument %d in template "
7912 "parameter list for %qD",
7913 i + 1, in_decl);
7914 if (is_tmpl_type)
7915 inform (input_location,
7916 " expected a type, got %qT", DECL_NAME (arg));
7917 else
7918 inform (input_location,
7919 " expected a class template, got %qT", orig_arg);
7920 }
7921 return error_mark_node;
7922 }
7923
7924 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7925 /* We already did the appropriate conversion when packing args. */
7926 val = orig_arg;
7927 else if (is_type)
7928 {
7929 if (requires_tmpl_type)
7930 {
7931 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7932 /* The number of argument required is not known yet.
7933 Just accept it for now. */
7934 val = orig_arg;
7935 else
7936 {
7937 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7938 tree argparm;
7939
7940 /* Strip alias templates that are equivalent to another
7941 template. */
7942 arg = get_underlying_template (arg);
7943 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7944
7945 if (coerce_template_template_parms (parmparm, argparm,
7946 complain, in_decl,
7947 args))
7948 {
7949 val = arg;
7950
7951 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7952 TEMPLATE_DECL. */
7953 if (val != error_mark_node)
7954 {
7955 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7956 val = TREE_TYPE (val);
7957 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7958 val = make_pack_expansion (val, complain);
7959 }
7960 }
7961 else
7962 {
7963 if (in_decl && (complain & tf_error))
7964 {
7965 error ("type/value mismatch at argument %d in "
7966 "template parameter list for %qD",
7967 i + 1, in_decl);
7968 inform (input_location,
7969 " expected a template of type %qD, got %qT",
7970 parm, orig_arg);
7971 }
7972
7973 val = error_mark_node;
7974 }
7975
7976 // Check that the constraints are compatible before allowing the
7977 // substitution.
7978 if (val != error_mark_node)
7979 if (!is_compatible_template_arg (parm, arg))
7980 {
7981 if (in_decl && (complain & tf_error))
7982 {
7983 error ("constraint mismatch at argument %d in "
7984 "template parameter list for %qD",
7985 i + 1, in_decl);
7986 inform (input_location, " expected %qD but got %qD",
7987 parm, arg);
7988 }
7989 val = error_mark_node;
7990 }
7991 }
7992 }
7993 else
7994 val = orig_arg;
7995 /* We only form one instance of each template specialization.
7996 Therefore, if we use a non-canonical variant (i.e., a
7997 typedef), any future messages referring to the type will use
7998 the typedef, which is confusing if those future uses do not
7999 themselves also use the typedef. */
8000 if (TYPE_P (val))
8001 val = canonicalize_type_argument (val, complain);
8002 }
8003 else
8004 {
8005 tree t = TREE_TYPE (parm);
8006
8007 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8008 > TMPL_ARGS_DEPTH (args))
8009 /* We don't have enough levels of args to do any substitution. This
8010 can happen in the context of -fnew-ttp-matching. */;
8011 else if (tree a = type_uses_auto (t))
8012 {
8013 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8014 if (t == error_mark_node)
8015 return error_mark_node;
8016 }
8017 else
8018 t = tsubst (t, args, complain, in_decl);
8019
8020 if (invalid_nontype_parm_type_p (t, complain))
8021 return error_mark_node;
8022
8023 if (t != TREE_TYPE (parm))
8024 t = canonicalize_type_argument (t, complain);
8025
8026 if (!type_dependent_expression_p (orig_arg)
8027 && !uses_template_parms (t))
8028 /* We used to call digest_init here. However, digest_init
8029 will report errors, which we don't want when complain
8030 is zero. More importantly, digest_init will try too
8031 hard to convert things: for example, `0' should not be
8032 converted to pointer type at this point according to
8033 the standard. Accepting this is not merely an
8034 extension, since deciding whether or not these
8035 conversions can occur is part of determining which
8036 function template to call, or whether a given explicit
8037 argument specification is valid. */
8038 val = convert_nontype_argument (t, orig_arg, complain);
8039 else
8040 {
8041 val = canonicalize_expr_argument (orig_arg, complain);
8042 val = maybe_convert_nontype_argument (t, val);
8043 }
8044
8045
8046 if (val == NULL_TREE)
8047 val = error_mark_node;
8048 else if (val == error_mark_node && (complain & tf_error))
8049 error ("could not convert template argument %qE from %qT to %qT",
8050 orig_arg, TREE_TYPE (orig_arg), t);
8051
8052 if (INDIRECT_REF_P (val))
8053 {
8054 /* Reject template arguments that are references to built-in
8055 functions with no library fallbacks. */
8056 const_tree inner = TREE_OPERAND (val, 0);
8057 const_tree innertype = TREE_TYPE (inner);
8058 if (innertype
8059 && TYPE_REF_P (innertype)
8060 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8061 && TREE_OPERAND_LENGTH (inner) > 0
8062 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8063 return error_mark_node;
8064 }
8065
8066 if (TREE_CODE (val) == SCOPE_REF)
8067 {
8068 /* Strip typedefs from the SCOPE_REF. */
8069 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8070 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8071 complain);
8072 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8073 QUALIFIED_NAME_IS_TEMPLATE (val));
8074 }
8075 }
8076
8077 return val;
8078 }
8079
8080 /* Coerces the remaining template arguments in INNER_ARGS (from
8081 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8082 Returns the coerced argument pack. PARM_IDX is the position of this
8083 parameter in the template parameter list. ARGS is the original
8084 template argument list. */
8085 static tree
8086 coerce_template_parameter_pack (tree parms,
8087 int parm_idx,
8088 tree args,
8089 tree inner_args,
8090 int arg_idx,
8091 tree new_args,
8092 int* lost,
8093 tree in_decl,
8094 tsubst_flags_t complain)
8095 {
8096 tree parm = TREE_VEC_ELT (parms, parm_idx);
8097 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8098 tree packed_args;
8099 tree argument_pack;
8100 tree packed_parms = NULL_TREE;
8101
8102 if (arg_idx > nargs)
8103 arg_idx = nargs;
8104
8105 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8106 {
8107 /* When the template parameter is a non-type template parameter pack
8108 or template template parameter pack whose type or template
8109 parameters use parameter packs, we know exactly how many arguments
8110 we are looking for. Build a vector of the instantiated decls for
8111 these template parameters in PACKED_PARMS. */
8112 /* We can't use make_pack_expansion here because it would interpret a
8113 _DECL as a use rather than a declaration. */
8114 tree decl = TREE_VALUE (parm);
8115 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8116 SET_PACK_EXPANSION_PATTERN (exp, decl);
8117 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8118 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8119
8120 TREE_VEC_LENGTH (args)--;
8121 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8122 TREE_VEC_LENGTH (args)++;
8123
8124 if (packed_parms == error_mark_node)
8125 return error_mark_node;
8126
8127 /* If we're doing a partial instantiation of a member template,
8128 verify that all of the types used for the non-type
8129 template parameter pack are, in fact, valid for non-type
8130 template parameters. */
8131 if (arg_idx < nargs
8132 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8133 {
8134 int j, len = TREE_VEC_LENGTH (packed_parms);
8135 for (j = 0; j < len; ++j)
8136 {
8137 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8138 if (invalid_nontype_parm_type_p (t, complain))
8139 return error_mark_node;
8140 }
8141 /* We don't know how many args we have yet, just
8142 use the unconverted ones for now. */
8143 return NULL_TREE;
8144 }
8145
8146 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8147 }
8148 /* Check if we have a placeholder pack, which indicates we're
8149 in the context of a introduction list. In that case we want
8150 to match this pack to the single placeholder. */
8151 else if (arg_idx < nargs
8152 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8153 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8154 {
8155 nargs = arg_idx + 1;
8156 packed_args = make_tree_vec (1);
8157 }
8158 else
8159 packed_args = make_tree_vec (nargs - arg_idx);
8160
8161 /* Convert the remaining arguments, which will be a part of the
8162 parameter pack "parm". */
8163 int first_pack_arg = arg_idx;
8164 for (; arg_idx < nargs; ++arg_idx)
8165 {
8166 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8167 tree actual_parm = TREE_VALUE (parm);
8168 int pack_idx = arg_idx - first_pack_arg;
8169
8170 if (packed_parms)
8171 {
8172 /* Once we've packed as many args as we have types, stop. */
8173 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8174 break;
8175 else if (PACK_EXPANSION_P (arg))
8176 /* We don't know how many args we have yet, just
8177 use the unconverted ones for now. */
8178 return NULL_TREE;
8179 else
8180 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8181 }
8182
8183 if (arg == error_mark_node)
8184 {
8185 if (complain & tf_error)
8186 error ("template argument %d is invalid", arg_idx + 1);
8187 }
8188 else
8189 arg = convert_template_argument (actual_parm,
8190 arg, new_args, complain, parm_idx,
8191 in_decl);
8192 if (arg == error_mark_node)
8193 (*lost)++;
8194 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8195 }
8196
8197 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8198 && TREE_VEC_LENGTH (packed_args) > 0)
8199 {
8200 if (complain & tf_error)
8201 error ("wrong number of template arguments (%d, should be %d)",
8202 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8203 return error_mark_node;
8204 }
8205
8206 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8207 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8208 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8209 else
8210 {
8211 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8212 TREE_CONSTANT (argument_pack) = 1;
8213 }
8214
8215 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8216 if (CHECKING_P)
8217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8218 TREE_VEC_LENGTH (packed_args));
8219 return argument_pack;
8220 }
8221
8222 /* Returns the number of pack expansions in the template argument vector
8223 ARGS. */
8224
8225 static int
8226 pack_expansion_args_count (tree args)
8227 {
8228 int i;
8229 int count = 0;
8230 if (args)
8231 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8232 {
8233 tree elt = TREE_VEC_ELT (args, i);
8234 if (elt && PACK_EXPANSION_P (elt))
8235 ++count;
8236 }
8237 return count;
8238 }
8239
8240 /* Convert all template arguments to their appropriate types, and
8241 return a vector containing the innermost resulting template
8242 arguments. If any error occurs, return error_mark_node. Error and
8243 warning messages are issued under control of COMPLAIN.
8244
8245 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8246 for arguments not specified in ARGS. Otherwise, if
8247 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8248 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8249 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8250 ARGS. */
8251
8252 static tree
8253 coerce_template_parms (tree parms,
8254 tree args,
8255 tree in_decl,
8256 tsubst_flags_t complain,
8257 bool require_all_args,
8258 bool use_default_args)
8259 {
8260 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8261 tree orig_inner_args;
8262 tree inner_args;
8263 tree new_args;
8264 tree new_inner_args;
8265
8266 /* When used as a boolean value, indicates whether this is a
8267 variadic template parameter list. Since it's an int, we can also
8268 subtract it from nparms to get the number of non-variadic
8269 parameters. */
8270 int variadic_p = 0;
8271 int variadic_args_p = 0;
8272 int post_variadic_parms = 0;
8273
8274 /* Adjustment to nparms for fixed parameter packs. */
8275 int fixed_pack_adjust = 0;
8276 int fixed_packs = 0;
8277 int missing = 0;
8278
8279 /* Likewise for parameters with default arguments. */
8280 int default_p = 0;
8281
8282 if (args == error_mark_node)
8283 return error_mark_node;
8284
8285 nparms = TREE_VEC_LENGTH (parms);
8286
8287 /* Determine if there are any parameter packs or default arguments. */
8288 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8289 {
8290 tree parm = TREE_VEC_ELT (parms, parm_idx);
8291 if (variadic_p)
8292 ++post_variadic_parms;
8293 if (template_parameter_pack_p (TREE_VALUE (parm)))
8294 ++variadic_p;
8295 if (TREE_PURPOSE (parm))
8296 ++default_p;
8297 }
8298
8299 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8300 /* If there are no parameters that follow a parameter pack, we need to
8301 expand any argument packs so that we can deduce a parameter pack from
8302 some non-packed args followed by an argument pack, as in variadic85.C.
8303 If there are such parameters, we need to leave argument packs intact
8304 so the arguments are assigned properly. This can happen when dealing
8305 with a nested class inside a partial specialization of a class
8306 template, as in variadic92.C, or when deducing a template parameter pack
8307 from a sub-declarator, as in variadic114.C. */
8308 if (!post_variadic_parms)
8309 inner_args = expand_template_argument_pack (inner_args);
8310
8311 /* Count any pack expansion args. */
8312 variadic_args_p = pack_expansion_args_count (inner_args);
8313
8314 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8315 if ((nargs - variadic_args_p > nparms && !variadic_p)
8316 || (nargs < nparms - variadic_p
8317 && require_all_args
8318 && !variadic_args_p
8319 && (!use_default_args
8320 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8321 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8322 {
8323 bad_nargs:
8324 if (complain & tf_error)
8325 {
8326 if (variadic_p || default_p)
8327 {
8328 nparms -= variadic_p + default_p;
8329 error ("wrong number of template arguments "
8330 "(%d, should be at least %d)", nargs, nparms);
8331 }
8332 else
8333 error ("wrong number of template arguments "
8334 "(%d, should be %d)", nargs, nparms);
8335
8336 if (in_decl)
8337 inform (DECL_SOURCE_LOCATION (in_decl),
8338 "provided for %qD", in_decl);
8339 }
8340
8341 return error_mark_node;
8342 }
8343 /* We can't pass a pack expansion to a non-pack parameter of an alias
8344 template (DR 1430). */
8345 else if (in_decl
8346 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8347 || concept_template_p (in_decl))
8348 && variadic_args_p
8349 && nargs - variadic_args_p < nparms - variadic_p)
8350 {
8351 if (complain & tf_error)
8352 {
8353 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8354 {
8355 tree arg = TREE_VEC_ELT (inner_args, i);
8356 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8357
8358 if (PACK_EXPANSION_P (arg)
8359 && !template_parameter_pack_p (parm))
8360 {
8361 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8362 error_at (location_of (arg),
8363 "pack expansion argument for non-pack parameter "
8364 "%qD of alias template %qD", parm, in_decl);
8365 else
8366 error_at (location_of (arg),
8367 "pack expansion argument for non-pack parameter "
8368 "%qD of concept %qD", parm, in_decl);
8369 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8370 goto found;
8371 }
8372 }
8373 gcc_unreachable ();
8374 found:;
8375 }
8376 return error_mark_node;
8377 }
8378
8379 /* We need to evaluate the template arguments, even though this
8380 template-id may be nested within a "sizeof". */
8381 cp_evaluated ev;
8382
8383 new_inner_args = make_tree_vec (nparms);
8384 new_args = add_outermost_template_args (args, new_inner_args);
8385 int pack_adjust = 0;
8386 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8387 {
8388 tree arg;
8389 tree parm;
8390
8391 /* Get the Ith template parameter. */
8392 parm = TREE_VEC_ELT (parms, parm_idx);
8393
8394 if (parm == error_mark_node)
8395 {
8396 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8397 continue;
8398 }
8399
8400 /* Calculate the next argument. */
8401 if (arg_idx < nargs)
8402 arg = TREE_VEC_ELT (inner_args, arg_idx);
8403 else
8404 arg = NULL_TREE;
8405
8406 if (template_parameter_pack_p (TREE_VALUE (parm))
8407 && !(arg && ARGUMENT_PACK_P (arg)))
8408 {
8409 /* Some arguments will be placed in the
8410 template parameter pack PARM. */
8411 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8412 inner_args, arg_idx,
8413 new_args, &lost,
8414 in_decl, complain);
8415
8416 if (arg == NULL_TREE)
8417 {
8418 /* We don't know how many args we have yet, just use the
8419 unconverted (and still packed) ones for now. */
8420 new_inner_args = orig_inner_args;
8421 arg_idx = nargs;
8422 break;
8423 }
8424
8425 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8426
8427 /* Store this argument. */
8428 if (arg == error_mark_node)
8429 {
8430 lost++;
8431 /* We are done with all of the arguments. */
8432 arg_idx = nargs;
8433 break;
8434 }
8435 else
8436 {
8437 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8438 arg_idx += pack_adjust;
8439 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8440 {
8441 ++fixed_packs;
8442 fixed_pack_adjust += pack_adjust;
8443 }
8444 }
8445
8446 continue;
8447 }
8448 else if (arg)
8449 {
8450 if (PACK_EXPANSION_P (arg))
8451 {
8452 /* "If every valid specialization of a variadic template
8453 requires an empty template parameter pack, the template is
8454 ill-formed, no diagnostic required." So check that the
8455 pattern works with this parameter. */
8456 tree pattern = PACK_EXPANSION_PATTERN (arg);
8457 tree conv = convert_template_argument (TREE_VALUE (parm),
8458 pattern, new_args,
8459 complain, parm_idx,
8460 in_decl);
8461 if (conv == error_mark_node)
8462 {
8463 if (complain & tf_error)
8464 inform (input_location, "so any instantiation with a "
8465 "non-empty parameter pack would be ill-formed");
8466 ++lost;
8467 }
8468 else if (TYPE_P (conv) && !TYPE_P (pattern))
8469 /* Recover from missing typename. */
8470 TREE_VEC_ELT (inner_args, arg_idx)
8471 = make_pack_expansion (conv, complain);
8472
8473 /* We don't know how many args we have yet, just
8474 use the unconverted ones for now. */
8475 new_inner_args = inner_args;
8476 arg_idx = nargs;
8477 break;
8478 }
8479 }
8480 else if (require_all_args)
8481 {
8482 /* There must be a default arg in this case. */
8483 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8484 complain, in_decl);
8485 /* The position of the first default template argument,
8486 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8487 Record that. */
8488 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8489 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8490 arg_idx - pack_adjust);
8491 }
8492 else
8493 break;
8494
8495 if (arg == error_mark_node)
8496 {
8497 if (complain & tf_error)
8498 error ("template argument %d is invalid", arg_idx + 1);
8499 }
8500 else if (!arg)
8501 {
8502 /* This can occur if there was an error in the template
8503 parameter list itself (which we would already have
8504 reported) that we are trying to recover from, e.g., a class
8505 template with a parameter list such as
8506 template<typename..., typename> (cpp0x/variadic150.C). */
8507 ++lost;
8508
8509 /* This can also happen with a fixed parameter pack (71834). */
8510 if (arg_idx >= nargs)
8511 ++missing;
8512 }
8513 else
8514 arg = convert_template_argument (TREE_VALUE (parm),
8515 arg, new_args, complain,
8516 parm_idx, in_decl);
8517
8518 if (arg == error_mark_node)
8519 lost++;
8520 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8521 }
8522
8523 if (missing || arg_idx < nargs - variadic_args_p)
8524 {
8525 /* If we had fixed parameter packs, we didn't know how many arguments we
8526 actually needed earlier; now we do. */
8527 nparms += fixed_pack_adjust;
8528 variadic_p -= fixed_packs;
8529 goto bad_nargs;
8530 }
8531
8532 if (arg_idx < nargs)
8533 {
8534 /* We had some pack expansion arguments that will only work if the packs
8535 are empty, but wait until instantiation time to complain.
8536 See variadic-ttp3.C. */
8537 int len = nparms + (nargs - arg_idx);
8538 tree args = make_tree_vec (len);
8539 int i = 0;
8540 for (; i < nparms; ++i)
8541 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8542 for (; i < len; ++i, ++arg_idx)
8543 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8544 arg_idx - pack_adjust);
8545 new_inner_args = args;
8546 }
8547
8548 if (lost)
8549 {
8550 gcc_assert (!(complain & tf_error) || seen_error ());
8551 return error_mark_node;
8552 }
8553
8554 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8555 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8556 TREE_VEC_LENGTH (new_inner_args));
8557
8558 return new_inner_args;
8559 }
8560
8561 /* Convert all template arguments to their appropriate types, and
8562 return a vector containing the innermost resulting template
8563 arguments. If any error occurs, return error_mark_node. Error and
8564 warning messages are not issued.
8565
8566 Note that no function argument deduction is performed, and default
8567 arguments are used to fill in unspecified arguments. */
8568 tree
8569 coerce_template_parms (tree parms, tree args, tree in_decl)
8570 {
8571 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8572 }
8573
8574 /* Convert all template arguments to their appropriate type, and
8575 instantiate default arguments as needed. This returns a vector
8576 containing the innermost resulting template arguments, or
8577 error_mark_node if unsuccessful. */
8578 tree
8579 coerce_template_parms (tree parms, tree args, tree in_decl,
8580 tsubst_flags_t complain)
8581 {
8582 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8583 }
8584
8585 /* Like coerce_template_parms. If PARMS represents all template
8586 parameters levels, this function returns a vector of vectors
8587 representing all the resulting argument levels. Note that in this
8588 case, only the innermost arguments are coerced because the
8589 outermost ones are supposed to have been coerced already.
8590
8591 Otherwise, if PARMS represents only (the innermost) vector of
8592 parameters, this function returns a vector containing just the
8593 innermost resulting arguments. */
8594
8595 static tree
8596 coerce_innermost_template_parms (tree parms,
8597 tree args,
8598 tree in_decl,
8599 tsubst_flags_t complain,
8600 bool require_all_args,
8601 bool use_default_args)
8602 {
8603 int parms_depth = TMPL_PARMS_DEPTH (parms);
8604 int args_depth = TMPL_ARGS_DEPTH (args);
8605 tree coerced_args;
8606
8607 if (parms_depth > 1)
8608 {
8609 coerced_args = make_tree_vec (parms_depth);
8610 tree level;
8611 int cur_depth;
8612
8613 for (level = parms, cur_depth = parms_depth;
8614 parms_depth > 0 && level != NULL_TREE;
8615 level = TREE_CHAIN (level), --cur_depth)
8616 {
8617 tree l;
8618 if (cur_depth == args_depth)
8619 l = coerce_template_parms (TREE_VALUE (level),
8620 args, in_decl, complain,
8621 require_all_args,
8622 use_default_args);
8623 else
8624 l = TMPL_ARGS_LEVEL (args, cur_depth);
8625
8626 if (l == error_mark_node)
8627 return error_mark_node;
8628
8629 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8630 }
8631 }
8632 else
8633 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8634 args, in_decl, complain,
8635 require_all_args,
8636 use_default_args);
8637 return coerced_args;
8638 }
8639
8640 /* Returns 1 if template args OT and NT are equivalent. */
8641
8642 int
8643 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8644 {
8645 if (nt == ot)
8646 return 1;
8647 if (nt == NULL_TREE || ot == NULL_TREE)
8648 return false;
8649 if (nt == any_targ_node || ot == any_targ_node)
8650 return true;
8651
8652 if (TREE_CODE (nt) == TREE_VEC)
8653 /* For member templates */
8654 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8655 else if (PACK_EXPANSION_P (ot))
8656 return (PACK_EXPANSION_P (nt)
8657 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8658 PACK_EXPANSION_PATTERN (nt))
8659 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8660 PACK_EXPANSION_EXTRA_ARGS (nt)));
8661 else if (ARGUMENT_PACK_P (ot))
8662 {
8663 int i, len;
8664 tree opack, npack;
8665
8666 if (!ARGUMENT_PACK_P (nt))
8667 return 0;
8668
8669 opack = ARGUMENT_PACK_ARGS (ot);
8670 npack = ARGUMENT_PACK_ARGS (nt);
8671 len = TREE_VEC_LENGTH (opack);
8672 if (TREE_VEC_LENGTH (npack) != len)
8673 return 0;
8674 for (i = 0; i < len; ++i)
8675 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8676 TREE_VEC_ELT (npack, i)))
8677 return 0;
8678 return 1;
8679 }
8680 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8681 gcc_unreachable ();
8682 else if (TYPE_P (nt))
8683 {
8684 if (!TYPE_P (ot))
8685 return false;
8686 /* Don't treat an alias template specialization with dependent
8687 arguments as equivalent to its underlying type when used as a
8688 template argument; we need them to be distinct so that we
8689 substitute into the specialization arguments at instantiation
8690 time. And aliases can't be equivalent without being ==, so
8691 we don't need to look any deeper.
8692
8693 During partial ordering, however, we need to treat them normally so
8694 that we can order uses of the same alias with different
8695 cv-qualification (79960). */
8696 if (!partial_order
8697 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8698 return false;
8699 else
8700 return same_type_p (ot, nt);
8701 }
8702 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8703 return 0;
8704 else
8705 {
8706 /* Try to treat a template non-type argument that has been converted
8707 to the parameter type as equivalent to one that hasn't yet. */
8708 for (enum tree_code code1 = TREE_CODE (ot);
8709 CONVERT_EXPR_CODE_P (code1)
8710 || code1 == NON_LVALUE_EXPR;
8711 code1 = TREE_CODE (ot))
8712 ot = TREE_OPERAND (ot, 0);
8713 for (enum tree_code code2 = TREE_CODE (nt);
8714 CONVERT_EXPR_CODE_P (code2)
8715 || code2 == NON_LVALUE_EXPR;
8716 code2 = TREE_CODE (nt))
8717 nt = TREE_OPERAND (nt, 0);
8718
8719 return cp_tree_equal (ot, nt);
8720 }
8721 }
8722
8723 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8724 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8725 NEWARG_PTR with the offending arguments if they are non-NULL. */
8726
8727 int
8728 comp_template_args (tree oldargs, tree newargs,
8729 tree *oldarg_ptr, tree *newarg_ptr,
8730 bool partial_order)
8731 {
8732 int i;
8733
8734 if (oldargs == newargs)
8735 return 1;
8736
8737 if (!oldargs || !newargs)
8738 return 0;
8739
8740 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8741 return 0;
8742
8743 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8744 {
8745 tree nt = TREE_VEC_ELT (newargs, i);
8746 tree ot = TREE_VEC_ELT (oldargs, i);
8747
8748 if (! template_args_equal (ot, nt, partial_order))
8749 {
8750 if (oldarg_ptr != NULL)
8751 *oldarg_ptr = ot;
8752 if (newarg_ptr != NULL)
8753 *newarg_ptr = nt;
8754 return 0;
8755 }
8756 }
8757 return 1;
8758 }
8759
8760 inline bool
8761 comp_template_args_porder (tree oargs, tree nargs)
8762 {
8763 return comp_template_args (oargs, nargs, NULL, NULL, true);
8764 }
8765
8766 /* Implement a freelist interface for objects of type T.
8767
8768 Head is a separate object, rather than a regular member, so that we
8769 can define it as a GTY deletable pointer, which is highly
8770 desirable. A data member could be declared that way, but then the
8771 containing object would implicitly get GTY((user)), which would
8772 prevent us from instantiating freelists as global objects.
8773 Although this way we can create freelist global objects, they're
8774 such thin wrappers that instantiating temporaries at every use
8775 loses nothing and saves permanent storage for the freelist object.
8776
8777 Member functions next, anew, poison and reinit have default
8778 implementations that work for most of the types we're interested
8779 in, but if they don't work for some type, they should be explicitly
8780 specialized. See the comments before them for requirements, and
8781 the example specializations for the tree_list_freelist. */
8782 template <typename T>
8783 class freelist
8784 {
8785 /* Return the next object in a chain. We could just do type
8786 punning, but if we access the object with its underlying type, we
8787 avoid strict-aliasing trouble. This needs only work between
8788 poison and reinit. */
8789 static T *&next (T *obj) { return obj->next; }
8790
8791 /* Return a newly allocated, uninitialized or minimally-initialized
8792 object of type T. Any initialization performed by anew should
8793 either remain across the life of the object and the execution of
8794 poison, or be redone by reinit. */
8795 static T *anew () { return ggc_alloc<T> (); }
8796
8797 /* Optionally scribble all over the bits holding the object, so that
8798 they become (mostly?) uninitialized memory. This is called while
8799 preparing to make the object part of the free list. */
8800 static void poison (T *obj) {
8801 T *p ATTRIBUTE_UNUSED = obj;
8802 T **q ATTRIBUTE_UNUSED = &next (obj);
8803
8804 #ifdef ENABLE_GC_CHECKING
8805 /* Poison the data, to indicate the data is garbage. */
8806 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8807 memset (p, 0xa5, sizeof (*p));
8808 #endif
8809 /* Let valgrind know the object is free. */
8810 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8811
8812 /* Let valgrind know the next portion of the object is available,
8813 but uninitialized. */
8814 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8815 }
8816
8817 /* Bring an object that underwent at least one lifecycle after anew
8818 and before the most recent free and poison, back to a usable
8819 state, reinitializing whatever is needed for it to be
8820 functionally equivalent to an object just allocated and returned
8821 by anew. This may poison or clear the next field, used by
8822 freelist housekeeping after poison was called. */
8823 static void reinit (T *obj) {
8824 T **q ATTRIBUTE_UNUSED = &next (obj);
8825
8826 #ifdef ENABLE_GC_CHECKING
8827 memset (q, 0xa5, sizeof (*q));
8828 #endif
8829 /* Let valgrind know the entire object is available, but
8830 uninitialized. */
8831 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8832 }
8833
8834 /* Reference a GTY-deletable pointer that points to the first object
8835 in the free list proper. */
8836 T *&head;
8837 public:
8838 /* Construct a freelist object chaining objects off of HEAD. */
8839 freelist (T *&head) : head(head) {}
8840
8841 /* Add OBJ to the free object list. The former head becomes OBJ's
8842 successor. */
8843 void free (T *obj)
8844 {
8845 poison (obj);
8846 next (obj) = head;
8847 head = obj;
8848 }
8849
8850 /* Take an object from the free list, if one is available, or
8851 allocate a new one. Objects taken from the free list should be
8852 regarded as filled with garbage, except for bits that are
8853 configured to be preserved across free and alloc. */
8854 T *alloc ()
8855 {
8856 if (head)
8857 {
8858 T *obj = head;
8859 head = next (head);
8860 reinit (obj);
8861 return obj;
8862 }
8863 else
8864 return anew ();
8865 }
8866 };
8867
8868 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8869 want to allocate a TREE_LIST using the usual interface, and ensure
8870 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8871 build_tree_list logic in reinit, so this could go out of sync. */
8872 template <>
8873 inline tree &
8874 freelist<tree_node>::next (tree obj)
8875 {
8876 return TREE_CHAIN (obj);
8877 }
8878 template <>
8879 inline tree
8880 freelist<tree_node>::anew ()
8881 {
8882 return build_tree_list (NULL, NULL);
8883 }
8884 template <>
8885 inline void
8886 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8887 {
8888 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8889 tree p ATTRIBUTE_UNUSED = obj;
8890 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8891 tree *q ATTRIBUTE_UNUSED = &next (obj);
8892
8893 #ifdef ENABLE_GC_CHECKING
8894 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8895
8896 /* Poison the data, to indicate the data is garbage. */
8897 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8898 memset (p, 0xa5, size);
8899 #endif
8900 /* Let valgrind know the object is free. */
8901 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8902 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8903 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8904 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8905
8906 #ifdef ENABLE_GC_CHECKING
8907 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8908 /* Keep TREE_CHAIN functional. */
8909 TREE_SET_CODE (obj, TREE_LIST);
8910 #else
8911 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8912 #endif
8913 }
8914 template <>
8915 inline void
8916 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8917 {
8918 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8919
8920 #ifdef ENABLE_GC_CHECKING
8921 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8922 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8923 memset (obj, 0, sizeof (tree_list));
8924 #endif
8925
8926 /* Let valgrind know the entire object is available, but
8927 uninitialized. */
8928 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8929
8930 #ifdef ENABLE_GC_CHECKING
8931 TREE_SET_CODE (obj, TREE_LIST);
8932 #else
8933 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8934 #endif
8935 }
8936
8937 /* Point to the first object in the TREE_LIST freelist. */
8938 static GTY((deletable)) tree tree_list_freelist_head;
8939 /* Return the/an actual TREE_LIST freelist. */
8940 static inline freelist<tree_node>
8941 tree_list_freelist ()
8942 {
8943 return tree_list_freelist_head;
8944 }
8945
8946 /* Point to the first object in the tinst_level freelist. */
8947 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
8948 /* Return the/an actual tinst_level freelist. */
8949 static inline freelist<tinst_level>
8950 tinst_level_freelist ()
8951 {
8952 return tinst_level_freelist_head;
8953 }
8954
8955 /* Point to the first object in the pending_template freelist. */
8956 static GTY((deletable)) pending_template *pending_template_freelist_head;
8957 /* Return the/an actual pending_template freelist. */
8958 static inline freelist<pending_template>
8959 pending_template_freelist ()
8960 {
8961 return pending_template_freelist_head;
8962 }
8963
8964 /* Build the TREE_LIST object out of a split list, store it
8965 permanently, and return it. */
8966 tree
8967 tinst_level::to_list ()
8968 {
8969 gcc_assert (split_list_p ());
8970 tree ret = tree_list_freelist ().alloc ();
8971 TREE_PURPOSE (ret) = tldcl;
8972 TREE_VALUE (ret) = targs;
8973 tldcl = ret;
8974 targs = NULL;
8975 gcc_assert (tree_list_p ());
8976 return ret;
8977 }
8978
8979 const unsigned short tinst_level::refcount_infinity;
8980
8981 /* Increment OBJ's refcount unless it is already infinite. */
8982 static tinst_level *
8983 inc_refcount_use (tinst_level *obj)
8984 {
8985 if (obj && obj->refcount != tinst_level::refcount_infinity)
8986 ++obj->refcount;
8987 return obj;
8988 }
8989
8990 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8991 void
8992 tinst_level::free (tinst_level *obj)
8993 {
8994 if (obj->tree_list_p ())
8995 tree_list_freelist ().free (obj->get_node ());
8996 tinst_level_freelist ().free (obj);
8997 }
8998
8999 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9000 OBJ's DECL and OBJ, and start over with the tinst_level object that
9001 used to be referenced by OBJ's NEXT. */
9002 static void
9003 dec_refcount_use (tinst_level *obj)
9004 {
9005 while (obj
9006 && obj->refcount != tinst_level::refcount_infinity
9007 && !--obj->refcount)
9008 {
9009 tinst_level *next = obj->next;
9010 tinst_level::free (obj);
9011 obj = next;
9012 }
9013 }
9014
9015 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9016 and of the former PTR. Omitting the second argument is equivalent
9017 to passing (T*)NULL; this is allowed because passing the
9018 zero-valued integral constant NULL confuses type deduction and/or
9019 overload resolution. */
9020 template <typename T>
9021 static void
9022 set_refcount_ptr (T *& ptr, T *obj = NULL)
9023 {
9024 T *save = ptr;
9025 ptr = inc_refcount_use (obj);
9026 dec_refcount_use (save);
9027 }
9028
9029 static void
9030 add_pending_template (tree d)
9031 {
9032 tree ti = (TYPE_P (d)
9033 ? CLASSTYPE_TEMPLATE_INFO (d)
9034 : DECL_TEMPLATE_INFO (d));
9035 struct pending_template *pt;
9036 int level;
9037
9038 if (TI_PENDING_TEMPLATE_FLAG (ti))
9039 return;
9040
9041 /* We are called both from instantiate_decl, where we've already had a
9042 tinst_level pushed, and instantiate_template, where we haven't.
9043 Compensate. */
9044 gcc_assert (TREE_CODE (d) != TREE_LIST);
9045 level = !current_tinst_level
9046 || current_tinst_level->maybe_get_node () != d;
9047
9048 if (level)
9049 push_tinst_level (d);
9050
9051 pt = pending_template_freelist ().alloc ();
9052 pt->next = NULL;
9053 pt->tinst = NULL;
9054 set_refcount_ptr (pt->tinst, current_tinst_level);
9055 if (last_pending_template)
9056 last_pending_template->next = pt;
9057 else
9058 pending_templates = pt;
9059
9060 last_pending_template = pt;
9061
9062 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9063
9064 if (level)
9065 pop_tinst_level ();
9066 }
9067
9068
9069 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9070 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9071 documentation for TEMPLATE_ID_EXPR. */
9072
9073 tree
9074 lookup_template_function (tree fns, tree arglist)
9075 {
9076 if (fns == error_mark_node || arglist == error_mark_node)
9077 return error_mark_node;
9078
9079 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9080
9081 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9082 {
9083 error ("%q#D is not a function template", fns);
9084 return error_mark_node;
9085 }
9086
9087 if (BASELINK_P (fns))
9088 {
9089 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9090 unknown_type_node,
9091 BASELINK_FUNCTIONS (fns),
9092 arglist);
9093 return fns;
9094 }
9095
9096 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9097 }
9098
9099 /* Within the scope of a template class S<T>, the name S gets bound
9100 (in build_self_reference) to a TYPE_DECL for the class, not a
9101 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9102 or one of its enclosing classes, and that type is a template,
9103 return the associated TEMPLATE_DECL. Otherwise, the original
9104 DECL is returned.
9105
9106 Also handle the case when DECL is a TREE_LIST of ambiguous
9107 injected-class-names from different bases. */
9108
9109 tree
9110 maybe_get_template_decl_from_type_decl (tree decl)
9111 {
9112 if (decl == NULL_TREE)
9113 return decl;
9114
9115 /* DR 176: A lookup that finds an injected-class-name (10.2
9116 [class.member.lookup]) can result in an ambiguity in certain cases
9117 (for example, if it is found in more than one base class). If all of
9118 the injected-class-names that are found refer to specializations of
9119 the same class template, and if the name is followed by a
9120 template-argument-list, the reference refers to the class template
9121 itself and not a specialization thereof, and is not ambiguous. */
9122 if (TREE_CODE (decl) == TREE_LIST)
9123 {
9124 tree t, tmpl = NULL_TREE;
9125 for (t = decl; t; t = TREE_CHAIN (t))
9126 {
9127 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9128 if (!tmpl)
9129 tmpl = elt;
9130 else if (tmpl != elt)
9131 break;
9132 }
9133 if (tmpl && t == NULL_TREE)
9134 return tmpl;
9135 else
9136 return decl;
9137 }
9138
9139 return (decl != NULL_TREE
9140 && DECL_SELF_REFERENCE_P (decl)
9141 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9142 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9143 }
9144
9145 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9146 parameters, find the desired type.
9147
9148 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9149
9150 IN_DECL, if non-NULL, is the template declaration we are trying to
9151 instantiate.
9152
9153 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9154 the class we are looking up.
9155
9156 Issue error and warning messages under control of COMPLAIN.
9157
9158 If the template class is really a local class in a template
9159 function, then the FUNCTION_CONTEXT is the function in which it is
9160 being instantiated.
9161
9162 ??? Note that this function is currently called *twice* for each
9163 template-id: the first time from the parser, while creating the
9164 incomplete type (finish_template_type), and the second type during the
9165 real instantiation (instantiate_template_class). This is surely something
9166 that we want to avoid. It also causes some problems with argument
9167 coercion (see convert_nontype_argument for more information on this). */
9168
9169 static tree
9170 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9171 int entering_scope, tsubst_flags_t complain)
9172 {
9173 tree templ = NULL_TREE, parmlist;
9174 tree t;
9175 spec_entry **slot;
9176 spec_entry *entry;
9177 spec_entry elt;
9178 hashval_t hash;
9179
9180 if (identifier_p (d1))
9181 {
9182 tree value = innermost_non_namespace_value (d1);
9183 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9184 templ = value;
9185 else
9186 {
9187 if (context)
9188 push_decl_namespace (context);
9189 templ = lookup_name (d1);
9190 templ = maybe_get_template_decl_from_type_decl (templ);
9191 if (context)
9192 pop_decl_namespace ();
9193 }
9194 if (templ)
9195 context = DECL_CONTEXT (templ);
9196 }
9197 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9198 {
9199 tree type = TREE_TYPE (d1);
9200
9201 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9202 an implicit typename for the second A. Deal with it. */
9203 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9204 type = TREE_TYPE (type);
9205
9206 if (CLASSTYPE_TEMPLATE_INFO (type))
9207 {
9208 templ = CLASSTYPE_TI_TEMPLATE (type);
9209 d1 = DECL_NAME (templ);
9210 }
9211 }
9212 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9213 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9214 {
9215 templ = TYPE_TI_TEMPLATE (d1);
9216 d1 = DECL_NAME (templ);
9217 }
9218 else if (DECL_TYPE_TEMPLATE_P (d1))
9219 {
9220 templ = d1;
9221 d1 = DECL_NAME (templ);
9222 context = DECL_CONTEXT (templ);
9223 }
9224 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9225 {
9226 templ = d1;
9227 d1 = DECL_NAME (templ);
9228 }
9229
9230 /* Issue an error message if we didn't find a template. */
9231 if (! templ)
9232 {
9233 if (complain & tf_error)
9234 error ("%qT is not a template", d1);
9235 return error_mark_node;
9236 }
9237
9238 if (TREE_CODE (templ) != TEMPLATE_DECL
9239 /* Make sure it's a user visible template, if it was named by
9240 the user. */
9241 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9242 && !PRIMARY_TEMPLATE_P (templ)))
9243 {
9244 if (complain & tf_error)
9245 {
9246 error ("non-template type %qT used as a template", d1);
9247 if (in_decl)
9248 error ("for template declaration %q+D", in_decl);
9249 }
9250 return error_mark_node;
9251 }
9252
9253 complain &= ~tf_user;
9254
9255 /* An alias that just changes the name of a template is equivalent to the
9256 other template, so if any of the arguments are pack expansions, strip
9257 the alias to avoid problems with a pack expansion passed to a non-pack
9258 alias template parameter (DR 1430). */
9259 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9260 templ = get_underlying_template (templ);
9261
9262 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9263 {
9264 tree parm;
9265 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9266 if (arglist2 == error_mark_node
9267 || (!uses_template_parms (arglist2)
9268 && check_instantiated_args (templ, arglist2, complain)))
9269 return error_mark_node;
9270
9271 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9272 return parm;
9273 }
9274 else
9275 {
9276 tree template_type = TREE_TYPE (templ);
9277 tree gen_tmpl;
9278 tree type_decl;
9279 tree found = NULL_TREE;
9280 int arg_depth;
9281 int parm_depth;
9282 int is_dependent_type;
9283 int use_partial_inst_tmpl = false;
9284
9285 if (template_type == error_mark_node)
9286 /* An error occurred while building the template TEMPL, and a
9287 diagnostic has most certainly been emitted for that
9288 already. Let's propagate that error. */
9289 return error_mark_node;
9290
9291 gen_tmpl = most_general_template (templ);
9292 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9293 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9294 arg_depth = TMPL_ARGS_DEPTH (arglist);
9295
9296 if (arg_depth == 1 && parm_depth > 1)
9297 {
9298 /* We've been given an incomplete set of template arguments.
9299 For example, given:
9300
9301 template <class T> struct S1 {
9302 template <class U> struct S2 {};
9303 template <class U> struct S2<U*> {};
9304 };
9305
9306 we will be called with an ARGLIST of `U*', but the
9307 TEMPLATE will be `template <class T> template
9308 <class U> struct S1<T>::S2'. We must fill in the missing
9309 arguments. */
9310 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9311 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9312 arg_depth = TMPL_ARGS_DEPTH (arglist);
9313 }
9314
9315 /* Now we should have enough arguments. */
9316 gcc_assert (parm_depth == arg_depth);
9317
9318 /* From here on, we're only interested in the most general
9319 template. */
9320
9321 /* Calculate the BOUND_ARGS. These will be the args that are
9322 actually tsubst'd into the definition to create the
9323 instantiation. */
9324 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9325 complain,
9326 /*require_all_args=*/true,
9327 /*use_default_args=*/true);
9328
9329 if (arglist == error_mark_node)
9330 /* We were unable to bind the arguments. */
9331 return error_mark_node;
9332
9333 /* In the scope of a template class, explicit references to the
9334 template class refer to the type of the template, not any
9335 instantiation of it. For example, in:
9336
9337 template <class T> class C { void f(C<T>); }
9338
9339 the `C<T>' is just the same as `C'. Outside of the
9340 class, however, such a reference is an instantiation. */
9341 if (entering_scope
9342 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9343 || currently_open_class (template_type))
9344 {
9345 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9346
9347 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9348 return template_type;
9349 }
9350
9351 /* If we already have this specialization, return it. */
9352 elt.tmpl = gen_tmpl;
9353 elt.args = arglist;
9354 elt.spec = NULL_TREE;
9355 hash = spec_hasher::hash (&elt);
9356 entry = type_specializations->find_with_hash (&elt, hash);
9357
9358 if (entry)
9359 return entry->spec;
9360
9361 /* If the the template's constraints are not satisfied,
9362 then we cannot form a valid type.
9363
9364 Note that the check is deferred until after the hash
9365 lookup. This prevents redundant checks on previously
9366 instantiated specializations. */
9367 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9368 {
9369 if (complain & tf_error)
9370 {
9371 auto_diagnostic_group d;
9372 error ("template constraint failure");
9373 diagnose_constraints (input_location, gen_tmpl, arglist);
9374 }
9375 return error_mark_node;
9376 }
9377
9378 is_dependent_type = uses_template_parms (arglist);
9379
9380 /* If the deduced arguments are invalid, then the binding
9381 failed. */
9382 if (!is_dependent_type
9383 && check_instantiated_args (gen_tmpl,
9384 INNERMOST_TEMPLATE_ARGS (arglist),
9385 complain))
9386 return error_mark_node;
9387
9388 if (!is_dependent_type
9389 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9390 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9391 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9392 {
9393 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9394 DECL_NAME (gen_tmpl),
9395 /*tag_scope=*/ts_global);
9396 return found;
9397 }
9398
9399 context = DECL_CONTEXT (gen_tmpl);
9400 if (context && TYPE_P (context))
9401 {
9402 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9403 context = complete_type (context);
9404 }
9405 else
9406 context = tsubst (context, arglist, complain, in_decl);
9407
9408 if (context == error_mark_node)
9409 return error_mark_node;
9410
9411 if (!context)
9412 context = global_namespace;
9413
9414 /* Create the type. */
9415 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9416 {
9417 /* The user referred to a specialization of an alias
9418 template represented by GEN_TMPL.
9419
9420 [temp.alias]/2 says:
9421
9422 When a template-id refers to the specialization of an
9423 alias template, it is equivalent to the associated
9424 type obtained by substitution of its
9425 template-arguments for the template-parameters in the
9426 type-id of the alias template. */
9427
9428 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9429 /* Note that the call above (by indirectly calling
9430 register_specialization in tsubst_decl) registers the
9431 TYPE_DECL representing the specialization of the alias
9432 template. So next time someone substitutes ARGLIST for
9433 the template parms into the alias template (GEN_TMPL),
9434 she'll get that TYPE_DECL back. */
9435
9436 if (t == error_mark_node)
9437 return t;
9438 }
9439 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9440 {
9441 if (!is_dependent_type)
9442 {
9443 set_current_access_from_decl (TYPE_NAME (template_type));
9444 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9445 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9446 arglist, complain, in_decl),
9447 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9448 arglist, complain, in_decl),
9449 SCOPED_ENUM_P (template_type), NULL);
9450
9451 if (t == error_mark_node)
9452 return t;
9453 }
9454 else
9455 {
9456 /* We don't want to call start_enum for this type, since
9457 the values for the enumeration constants may involve
9458 template parameters. And, no one should be interested
9459 in the enumeration constants for such a type. */
9460 t = cxx_make_type (ENUMERAL_TYPE);
9461 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9462 }
9463 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9464 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9465 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9466 }
9467 else if (CLASS_TYPE_P (template_type))
9468 {
9469 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9470 instantiated here. */
9471 gcc_assert (!LAMBDA_TYPE_P (template_type));
9472
9473 t = make_class_type (TREE_CODE (template_type));
9474 CLASSTYPE_DECLARED_CLASS (t)
9475 = CLASSTYPE_DECLARED_CLASS (template_type);
9476 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9477
9478 /* A local class. Make sure the decl gets registered properly. */
9479 if (context == current_function_decl)
9480 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9481 == error_mark_node)
9482 return error_mark_node;
9483
9484 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9485 /* This instantiation is another name for the primary
9486 template type. Set the TYPE_CANONICAL field
9487 appropriately. */
9488 TYPE_CANONICAL (t) = template_type;
9489 else if (any_template_arguments_need_structural_equality_p (arglist))
9490 /* Some of the template arguments require structural
9491 equality testing, so this template class requires
9492 structural equality testing. */
9493 SET_TYPE_STRUCTURAL_EQUALITY (t);
9494 }
9495 else
9496 gcc_unreachable ();
9497
9498 /* If we called start_enum or pushtag above, this information
9499 will already be set up. */
9500 if (!TYPE_NAME (t))
9501 {
9502 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9503
9504 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9505 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9506 DECL_SOURCE_LOCATION (type_decl)
9507 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9508 }
9509 else
9510 type_decl = TYPE_NAME (t);
9511
9512 if (CLASS_TYPE_P (template_type))
9513 {
9514 TREE_PRIVATE (type_decl)
9515 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9516 TREE_PROTECTED (type_decl)
9517 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9518 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9519 {
9520 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9521 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9522 }
9523 }
9524
9525 if (OVERLOAD_TYPE_P (t)
9526 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9527 {
9528 static const char *tags[] = {"abi_tag", "may_alias"};
9529
9530 for (unsigned ix = 0; ix != 2; ix++)
9531 {
9532 tree attributes
9533 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9534
9535 if (attributes)
9536 TYPE_ATTRIBUTES (t)
9537 = tree_cons (TREE_PURPOSE (attributes),
9538 TREE_VALUE (attributes),
9539 TYPE_ATTRIBUTES (t));
9540 }
9541 }
9542
9543 /* Let's consider the explicit specialization of a member
9544 of a class template specialization that is implicitly instantiated,
9545 e.g.:
9546 template<class T>
9547 struct S
9548 {
9549 template<class U> struct M {}; //#0
9550 };
9551
9552 template<>
9553 template<>
9554 struct S<int>::M<char> //#1
9555 {
9556 int i;
9557 };
9558 [temp.expl.spec]/4 says this is valid.
9559
9560 In this case, when we write:
9561 S<int>::M<char> m;
9562
9563 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9564 the one of #0.
9565
9566 When we encounter #1, we want to store the partial instantiation
9567 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9568
9569 For all cases other than this "explicit specialization of member of a
9570 class template", we just want to store the most general template into
9571 the CLASSTYPE_TI_TEMPLATE of M.
9572
9573 This case of "explicit specialization of member of a class template"
9574 only happens when:
9575 1/ the enclosing class is an instantiation of, and therefore not
9576 the same as, the context of the most general template, and
9577 2/ we aren't looking at the partial instantiation itself, i.e.
9578 the innermost arguments are not the same as the innermost parms of
9579 the most general template.
9580
9581 So it's only when 1/ and 2/ happens that we want to use the partial
9582 instantiation of the member template in lieu of its most general
9583 template. */
9584
9585 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9586 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9587 /* the enclosing class must be an instantiation... */
9588 && CLASS_TYPE_P (context)
9589 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9590 {
9591 TREE_VEC_LENGTH (arglist)--;
9592 ++processing_template_decl;
9593 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9594 tree partial_inst_args =
9595 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9596 arglist, complain, NULL_TREE);
9597 --processing_template_decl;
9598 TREE_VEC_LENGTH (arglist)++;
9599 if (partial_inst_args == error_mark_node)
9600 return error_mark_node;
9601 use_partial_inst_tmpl =
9602 /*...and we must not be looking at the partial instantiation
9603 itself. */
9604 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9605 partial_inst_args);
9606 }
9607
9608 if (!use_partial_inst_tmpl)
9609 /* This case is easy; there are no member templates involved. */
9610 found = gen_tmpl;
9611 else
9612 {
9613 /* This is a full instantiation of a member template. Find
9614 the partial instantiation of which this is an instance. */
9615
9616 /* Temporarily reduce by one the number of levels in the ARGLIST
9617 so as to avoid comparing the last set of arguments. */
9618 TREE_VEC_LENGTH (arglist)--;
9619 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9620 TREE_VEC_LENGTH (arglist)++;
9621 /* FOUND is either a proper class type, or an alias
9622 template specialization. In the later case, it's a
9623 TYPE_DECL, resulting from the substituting of arguments
9624 for parameters in the TYPE_DECL of the alias template
9625 done earlier. So be careful while getting the template
9626 of FOUND. */
9627 found = (TREE_CODE (found) == TEMPLATE_DECL
9628 ? found
9629 : (TREE_CODE (found) == TYPE_DECL
9630 ? DECL_TI_TEMPLATE (found)
9631 : CLASSTYPE_TI_TEMPLATE (found)));
9632 }
9633
9634 // Build template info for the new specialization.
9635 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9636
9637 elt.spec = t;
9638 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9639 entry = ggc_alloc<spec_entry> ();
9640 *entry = elt;
9641 *slot = entry;
9642
9643 /* Note this use of the partial instantiation so we can check it
9644 later in maybe_process_partial_specialization. */
9645 DECL_TEMPLATE_INSTANTIATIONS (found)
9646 = tree_cons (arglist, t,
9647 DECL_TEMPLATE_INSTANTIATIONS (found));
9648
9649 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9650 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9651 /* Now that the type has been registered on the instantiations
9652 list, we set up the enumerators. Because the enumeration
9653 constants may involve the enumeration type itself, we make
9654 sure to register the type first, and then create the
9655 constants. That way, doing tsubst_expr for the enumeration
9656 constants won't result in recursive calls here; we'll find
9657 the instantiation and exit above. */
9658 tsubst_enum (template_type, t, arglist);
9659
9660 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9661 /* If the type makes use of template parameters, the
9662 code that generates debugging information will crash. */
9663 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9664
9665 /* Possibly limit visibility based on template args. */
9666 TREE_PUBLIC (type_decl) = 1;
9667 determine_visibility (type_decl);
9668
9669 inherit_targ_abi_tags (t);
9670
9671 return t;
9672 }
9673 }
9674
9675 /* Wrapper for lookup_template_class_1. */
9676
9677 tree
9678 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9679 int entering_scope, tsubst_flags_t complain)
9680 {
9681 tree ret;
9682 timevar_push (TV_TEMPLATE_INST);
9683 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9684 entering_scope, complain);
9685 timevar_pop (TV_TEMPLATE_INST);
9686 return ret;
9687 }
9688
9689 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9690
9691 tree
9692 lookup_template_variable (tree templ, tree arglist)
9693 {
9694 /* The type of the expression is NULL_TREE since the template-id could refer
9695 to an explicit or partial specialization. */
9696 tree type = NULL_TREE;
9697 if (flag_concepts && variable_concept_p (templ))
9698 /* Except that concepts are always bool. */
9699 type = boolean_type_node;
9700 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9701 }
9702
9703 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9704
9705 tree
9706 finish_template_variable (tree var, tsubst_flags_t complain)
9707 {
9708 tree templ = TREE_OPERAND (var, 0);
9709 tree arglist = TREE_OPERAND (var, 1);
9710
9711 /* We never want to return a VAR_DECL for a variable concept, since they
9712 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9713 bool concept_p = flag_concepts && variable_concept_p (templ);
9714 if (concept_p && processing_template_decl)
9715 return var;
9716
9717 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9718 arglist = add_outermost_template_args (tmpl_args, arglist);
9719
9720 templ = most_general_template (templ);
9721 tree parms = DECL_TEMPLATE_PARMS (templ);
9722 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9723 /*req_all*/true,
9724 /*use_default*/true);
9725
9726 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9727 {
9728 if (complain & tf_error)
9729 {
9730 auto_diagnostic_group d;
9731 error ("use of invalid variable template %qE", var);
9732 diagnose_constraints (location_of (var), templ, arglist);
9733 }
9734 return error_mark_node;
9735 }
9736
9737 /* If a template-id refers to a specialization of a variable
9738 concept, then the expression is true if and only if the
9739 concept's constraints are satisfied by the given template
9740 arguments.
9741
9742 NOTE: This is an extension of Concepts Lite TS that
9743 allows constraints to be used in expressions. */
9744 if (concept_p)
9745 {
9746 tree decl = DECL_TEMPLATE_RESULT (templ);
9747 return evaluate_variable_concept (decl, arglist);
9748 }
9749
9750 return instantiate_template (templ, arglist, complain);
9751 }
9752
9753 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9754 TARGS template args, and instantiate it if it's not dependent. */
9755
9756 tree
9757 lookup_and_finish_template_variable (tree templ, tree targs,
9758 tsubst_flags_t complain)
9759 {
9760 templ = lookup_template_variable (templ, targs);
9761 if (!any_dependent_template_arguments_p (targs))
9762 {
9763 templ = finish_template_variable (templ, complain);
9764 mark_used (templ);
9765 }
9766
9767 return convert_from_reference (templ);
9768 }
9769
9770 \f
9771 struct pair_fn_data
9772 {
9773 tree_fn_t fn;
9774 tree_fn_t any_fn;
9775 void *data;
9776 /* True when we should also visit template parameters that occur in
9777 non-deduced contexts. */
9778 bool include_nondeduced_p;
9779 hash_set<tree> *visited;
9780 };
9781
9782 /* Called from for_each_template_parm via walk_tree. */
9783
9784 static tree
9785 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9786 {
9787 tree t = *tp;
9788 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9789 tree_fn_t fn = pfd->fn;
9790 void *data = pfd->data;
9791 tree result = NULL_TREE;
9792
9793 #define WALK_SUBTREE(NODE) \
9794 do \
9795 { \
9796 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9797 pfd->include_nondeduced_p, \
9798 pfd->any_fn); \
9799 if (result) goto out; \
9800 } \
9801 while (0)
9802
9803 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9804 return t;
9805
9806 if (TYPE_P (t)
9807 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9808 WALK_SUBTREE (TYPE_CONTEXT (t));
9809
9810 switch (TREE_CODE (t))
9811 {
9812 case RECORD_TYPE:
9813 if (TYPE_PTRMEMFUNC_P (t))
9814 break;
9815 /* Fall through. */
9816
9817 case UNION_TYPE:
9818 case ENUMERAL_TYPE:
9819 if (!TYPE_TEMPLATE_INFO (t))
9820 *walk_subtrees = 0;
9821 else
9822 WALK_SUBTREE (TYPE_TI_ARGS (t));
9823 break;
9824
9825 case INTEGER_TYPE:
9826 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9827 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9828 break;
9829
9830 case METHOD_TYPE:
9831 /* Since we're not going to walk subtrees, we have to do this
9832 explicitly here. */
9833 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9834 /* Fall through. */
9835
9836 case FUNCTION_TYPE:
9837 /* Check the return type. */
9838 WALK_SUBTREE (TREE_TYPE (t));
9839
9840 /* Check the parameter types. Since default arguments are not
9841 instantiated until they are needed, the TYPE_ARG_TYPES may
9842 contain expressions that involve template parameters. But,
9843 no-one should be looking at them yet. And, once they're
9844 instantiated, they don't contain template parameters, so
9845 there's no point in looking at them then, either. */
9846 {
9847 tree parm;
9848
9849 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9850 WALK_SUBTREE (TREE_VALUE (parm));
9851
9852 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9853 want walk_tree walking into them itself. */
9854 *walk_subtrees = 0;
9855 }
9856
9857 if (flag_noexcept_type)
9858 {
9859 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9860 if (spec)
9861 WALK_SUBTREE (TREE_PURPOSE (spec));
9862 }
9863 break;
9864
9865 case TYPEOF_TYPE:
9866 case DECLTYPE_TYPE:
9867 case UNDERLYING_TYPE:
9868 if (pfd->include_nondeduced_p
9869 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9870 pfd->visited,
9871 pfd->include_nondeduced_p,
9872 pfd->any_fn))
9873 return error_mark_node;
9874 *walk_subtrees = false;
9875 break;
9876
9877 case FUNCTION_DECL:
9878 case VAR_DECL:
9879 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9880 WALK_SUBTREE (DECL_TI_ARGS (t));
9881 /* Fall through. */
9882
9883 case PARM_DECL:
9884 case CONST_DECL:
9885 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9886 WALK_SUBTREE (DECL_INITIAL (t));
9887 if (DECL_CONTEXT (t)
9888 && pfd->include_nondeduced_p)
9889 WALK_SUBTREE (DECL_CONTEXT (t));
9890 break;
9891
9892 case BOUND_TEMPLATE_TEMPLATE_PARM:
9893 /* Record template parameters such as `T' inside `TT<T>'. */
9894 WALK_SUBTREE (TYPE_TI_ARGS (t));
9895 /* Fall through. */
9896
9897 case TEMPLATE_TEMPLATE_PARM:
9898 case TEMPLATE_TYPE_PARM:
9899 case TEMPLATE_PARM_INDEX:
9900 if (fn && (*fn)(t, data))
9901 return t;
9902 else if (!fn)
9903 return t;
9904 break;
9905
9906 case TEMPLATE_DECL:
9907 /* A template template parameter is encountered. */
9908 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9909 WALK_SUBTREE (TREE_TYPE (t));
9910
9911 /* Already substituted template template parameter */
9912 *walk_subtrees = 0;
9913 break;
9914
9915 case TYPENAME_TYPE:
9916 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9917 partial instantiation. */
9918 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9919 break;
9920
9921 case CONSTRUCTOR:
9922 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9923 && pfd->include_nondeduced_p)
9924 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9925 break;
9926
9927 case INDIRECT_REF:
9928 case COMPONENT_REF:
9929 /* If there's no type, then this thing must be some expression
9930 involving template parameters. */
9931 if (!fn && !TREE_TYPE (t))
9932 return error_mark_node;
9933 break;
9934
9935 case MODOP_EXPR:
9936 case CAST_EXPR:
9937 case IMPLICIT_CONV_EXPR:
9938 case REINTERPRET_CAST_EXPR:
9939 case CONST_CAST_EXPR:
9940 case STATIC_CAST_EXPR:
9941 case DYNAMIC_CAST_EXPR:
9942 case ARROW_EXPR:
9943 case DOTSTAR_EXPR:
9944 case TYPEID_EXPR:
9945 case PSEUDO_DTOR_EXPR:
9946 if (!fn)
9947 return error_mark_node;
9948 break;
9949
9950 default:
9951 break;
9952 }
9953
9954 #undef WALK_SUBTREE
9955
9956 /* We didn't find any template parameters we liked. */
9957 out:
9958 return result;
9959 }
9960
9961 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9962 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9963 call FN with the parameter and the DATA.
9964 If FN returns nonzero, the iteration is terminated, and
9965 for_each_template_parm returns 1. Otherwise, the iteration
9966 continues. If FN never returns a nonzero value, the value
9967 returned by for_each_template_parm is 0. If FN is NULL, it is
9968 considered to be the function which always returns 1.
9969
9970 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9971 parameters that occur in non-deduced contexts. When false, only
9972 visits those template parameters that can be deduced. */
9973
9974 static tree
9975 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9976 hash_set<tree> *visited,
9977 bool include_nondeduced_p,
9978 tree_fn_t any_fn)
9979 {
9980 struct pair_fn_data pfd;
9981 tree result;
9982
9983 /* Set up. */
9984 pfd.fn = fn;
9985 pfd.any_fn = any_fn;
9986 pfd.data = data;
9987 pfd.include_nondeduced_p = include_nondeduced_p;
9988
9989 /* Walk the tree. (Conceptually, we would like to walk without
9990 duplicates, but for_each_template_parm_r recursively calls
9991 for_each_template_parm, so we would need to reorganize a fair
9992 bit to use walk_tree_without_duplicates, so we keep our own
9993 visited list.) */
9994 if (visited)
9995 pfd.visited = visited;
9996 else
9997 pfd.visited = new hash_set<tree>;
9998 result = cp_walk_tree (&t,
9999 for_each_template_parm_r,
10000 &pfd,
10001 pfd.visited);
10002
10003 /* Clean up. */
10004 if (!visited)
10005 {
10006 delete pfd.visited;
10007 pfd.visited = 0;
10008 }
10009
10010 return result;
10011 }
10012
10013 /* Returns true if T depends on any template parameter. */
10014
10015 int
10016 uses_template_parms (tree t)
10017 {
10018 if (t == NULL_TREE)
10019 return false;
10020
10021 bool dependent_p;
10022 int saved_processing_template_decl;
10023
10024 saved_processing_template_decl = processing_template_decl;
10025 if (!saved_processing_template_decl)
10026 processing_template_decl = 1;
10027 if (TYPE_P (t))
10028 dependent_p = dependent_type_p (t);
10029 else if (TREE_CODE (t) == TREE_VEC)
10030 dependent_p = any_dependent_template_arguments_p (t);
10031 else if (TREE_CODE (t) == TREE_LIST)
10032 dependent_p = (uses_template_parms (TREE_VALUE (t))
10033 || uses_template_parms (TREE_CHAIN (t)));
10034 else if (TREE_CODE (t) == TYPE_DECL)
10035 dependent_p = dependent_type_p (TREE_TYPE (t));
10036 else if (DECL_P (t)
10037 || EXPR_P (t)
10038 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10039 || TREE_CODE (t) == OVERLOAD
10040 || BASELINK_P (t)
10041 || identifier_p (t)
10042 || TREE_CODE (t) == TRAIT_EXPR
10043 || TREE_CODE (t) == CONSTRUCTOR
10044 || CONSTANT_CLASS_P (t))
10045 dependent_p = (type_dependent_expression_p (t)
10046 || value_dependent_expression_p (t));
10047 else
10048 {
10049 gcc_assert (t == error_mark_node);
10050 dependent_p = false;
10051 }
10052
10053 processing_template_decl = saved_processing_template_decl;
10054
10055 return dependent_p;
10056 }
10057
10058 /* Returns true iff current_function_decl is an incompletely instantiated
10059 template. Useful instead of processing_template_decl because the latter
10060 is set to 0 during instantiate_non_dependent_expr. */
10061
10062 bool
10063 in_template_function (void)
10064 {
10065 tree fn = current_function_decl;
10066 bool ret;
10067 ++processing_template_decl;
10068 ret = (fn && DECL_LANG_SPECIFIC (fn)
10069 && DECL_TEMPLATE_INFO (fn)
10070 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10071 --processing_template_decl;
10072 return ret;
10073 }
10074
10075 /* Returns true if T depends on any template parameter with level LEVEL. */
10076
10077 bool
10078 uses_template_parms_level (tree t, int level)
10079 {
10080 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10081 /*include_nondeduced_p=*/true);
10082 }
10083
10084 /* Returns true if the signature of DECL depends on any template parameter from
10085 its enclosing class. */
10086
10087 bool
10088 uses_outer_template_parms (tree decl)
10089 {
10090 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10091 if (depth == 0)
10092 return false;
10093 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10094 &depth, NULL, /*include_nondeduced_p=*/true))
10095 return true;
10096 if (PRIMARY_TEMPLATE_P (decl)
10097 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10098 (DECL_TEMPLATE_PARMS (decl)),
10099 template_parm_outer_level,
10100 &depth, NULL, /*include_nondeduced_p=*/true))
10101 return true;
10102 tree ci = get_constraints (decl);
10103 if (ci)
10104 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10105 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10106 &depth, NULL, /*nondeduced*/true))
10107 return true;
10108 return false;
10109 }
10110
10111 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10112 ill-formed translation unit, i.e. a variable or function that isn't
10113 usable in a constant expression. */
10114
10115 static inline bool
10116 neglectable_inst_p (tree d)
10117 {
10118 return (d && DECL_P (d)
10119 && !undeduced_auto_decl (d)
10120 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10121 : decl_maybe_constant_var_p (d)));
10122 }
10123
10124 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10125 neglectable and instantiated from within an erroneous instantiation. */
10126
10127 static bool
10128 limit_bad_template_recursion (tree decl)
10129 {
10130 struct tinst_level *lev = current_tinst_level;
10131 int errs = errorcount + sorrycount;
10132 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10133 return false;
10134
10135 for (; lev; lev = lev->next)
10136 if (neglectable_inst_p (lev->maybe_get_node ()))
10137 break;
10138
10139 return (lev && errs > lev->errors);
10140 }
10141
10142 static int tinst_depth;
10143 extern int max_tinst_depth;
10144 int depth_reached;
10145
10146 static GTY(()) struct tinst_level *last_error_tinst_level;
10147
10148 /* We're starting to instantiate D; record the template instantiation context
10149 at LOC for diagnostics and to restore it later. */
10150
10151 static bool
10152 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10153 {
10154 struct tinst_level *new_level;
10155
10156 if (tinst_depth >= max_tinst_depth)
10157 {
10158 /* Tell error.c not to try to instantiate any templates. */
10159 at_eof = 2;
10160 fatal_error (input_location,
10161 "template instantiation depth exceeds maximum of %d"
10162 " (use -ftemplate-depth= to increase the maximum)",
10163 max_tinst_depth);
10164 return false;
10165 }
10166
10167 /* If the current instantiation caused problems, don't let it instantiate
10168 anything else. Do allow deduction substitution and decls usable in
10169 constant expressions. */
10170 if (!targs && limit_bad_template_recursion (tldcl))
10171 return false;
10172
10173 /* When not -quiet, dump template instantiations other than functions, since
10174 announce_function will take care of those. */
10175 if (!quiet_flag && !targs
10176 && TREE_CODE (tldcl) != TREE_LIST
10177 && TREE_CODE (tldcl) != FUNCTION_DECL)
10178 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10179
10180 new_level = tinst_level_freelist ().alloc ();
10181 new_level->tldcl = tldcl;
10182 new_level->targs = targs;
10183 new_level->locus = loc;
10184 new_level->errors = errorcount + sorrycount;
10185 new_level->next = NULL;
10186 new_level->refcount = 0;
10187 set_refcount_ptr (new_level->next, current_tinst_level);
10188 set_refcount_ptr (current_tinst_level, new_level);
10189
10190 ++tinst_depth;
10191 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10192 depth_reached = tinst_depth;
10193
10194 return true;
10195 }
10196
10197 /* We're starting substitution of TMPL<ARGS>; record the template
10198 substitution context for diagnostics and to restore it later. */
10199
10200 static bool
10201 push_tinst_level (tree tmpl, tree args)
10202 {
10203 return push_tinst_level_loc (tmpl, args, input_location);
10204 }
10205
10206 /* We're starting to instantiate D; record INPUT_LOCATION and the
10207 template instantiation context for diagnostics and to restore it
10208 later. */
10209
10210 bool
10211 push_tinst_level (tree d)
10212 {
10213 return push_tinst_level_loc (d, input_location);
10214 }
10215
10216 /* Likewise, but record LOC as the program location. */
10217
10218 bool
10219 push_tinst_level_loc (tree d, location_t loc)
10220 {
10221 gcc_assert (TREE_CODE (d) != TREE_LIST);
10222 return push_tinst_level_loc (d, NULL, loc);
10223 }
10224
10225 /* We're done instantiating this template; return to the instantiation
10226 context. */
10227
10228 void
10229 pop_tinst_level (void)
10230 {
10231 /* Restore the filename and line number stashed away when we started
10232 this instantiation. */
10233 input_location = current_tinst_level->locus;
10234 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10235 --tinst_depth;
10236 }
10237
10238 /* We're instantiating a deferred template; restore the template
10239 instantiation context in which the instantiation was requested, which
10240 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10241
10242 static tree
10243 reopen_tinst_level (struct tinst_level *level)
10244 {
10245 struct tinst_level *t;
10246
10247 tinst_depth = 0;
10248 for (t = level; t; t = t->next)
10249 ++tinst_depth;
10250
10251 set_refcount_ptr (current_tinst_level, level);
10252 pop_tinst_level ();
10253 if (current_tinst_level)
10254 current_tinst_level->errors = errorcount+sorrycount;
10255 return level->maybe_get_node ();
10256 }
10257
10258 /* Returns the TINST_LEVEL which gives the original instantiation
10259 context. */
10260
10261 struct tinst_level *
10262 outermost_tinst_level (void)
10263 {
10264 struct tinst_level *level = current_tinst_level;
10265 if (level)
10266 while (level->next)
10267 level = level->next;
10268 return level;
10269 }
10270
10271 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10272 vector of template arguments, as for tsubst.
10273
10274 Returns an appropriate tsubst'd friend declaration. */
10275
10276 static tree
10277 tsubst_friend_function (tree decl, tree args)
10278 {
10279 tree new_friend;
10280
10281 if (TREE_CODE (decl) == FUNCTION_DECL
10282 && DECL_TEMPLATE_INSTANTIATION (decl)
10283 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10284 /* This was a friend declared with an explicit template
10285 argument list, e.g.:
10286
10287 friend void f<>(T);
10288
10289 to indicate that f was a template instantiation, not a new
10290 function declaration. Now, we have to figure out what
10291 instantiation of what template. */
10292 {
10293 tree template_id, arglist, fns;
10294 tree new_args;
10295 tree tmpl;
10296 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10297
10298 /* Friend functions are looked up in the containing namespace scope.
10299 We must enter that scope, to avoid finding member functions of the
10300 current class with same name. */
10301 push_nested_namespace (ns);
10302 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10303 tf_warning_or_error, NULL_TREE,
10304 /*integral_constant_expression_p=*/false);
10305 pop_nested_namespace (ns);
10306 arglist = tsubst (DECL_TI_ARGS (decl), args,
10307 tf_warning_or_error, NULL_TREE);
10308 template_id = lookup_template_function (fns, arglist);
10309
10310 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10311 tmpl = determine_specialization (template_id, new_friend,
10312 &new_args,
10313 /*need_member_template=*/0,
10314 TREE_VEC_LENGTH (args),
10315 tsk_none);
10316 return instantiate_template (tmpl, new_args, tf_error);
10317 }
10318
10319 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10320
10321 /* The NEW_FRIEND will look like an instantiation, to the
10322 compiler, but is not an instantiation from the point of view of
10323 the language. For example, we might have had:
10324
10325 template <class T> struct S {
10326 template <class U> friend void f(T, U);
10327 };
10328
10329 Then, in S<int>, template <class U> void f(int, U) is not an
10330 instantiation of anything. */
10331 if (new_friend == error_mark_node)
10332 return error_mark_node;
10333
10334 DECL_USE_TEMPLATE (new_friend) = 0;
10335 if (TREE_CODE (decl) == TEMPLATE_DECL)
10336 {
10337 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10338 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10339 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10340 }
10341
10342 /* The mangled name for the NEW_FRIEND is incorrect. The function
10343 is not a template instantiation and should not be mangled like
10344 one. Therefore, we forget the mangling here; we'll recompute it
10345 later if we need it. */
10346 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10347 {
10348 SET_DECL_RTL (new_friend, NULL);
10349 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10350 }
10351
10352 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10353 {
10354 tree old_decl;
10355 tree new_friend_template_info;
10356 tree new_friend_result_template_info;
10357 tree ns;
10358 int new_friend_is_defn;
10359
10360 /* We must save some information from NEW_FRIEND before calling
10361 duplicate decls since that function will free NEW_FRIEND if
10362 possible. */
10363 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10364 new_friend_is_defn =
10365 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10366 (template_for_substitution (new_friend)))
10367 != NULL_TREE);
10368 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10369 {
10370 /* This declaration is a `primary' template. */
10371 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10372
10373 new_friend_result_template_info
10374 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10375 }
10376 else
10377 new_friend_result_template_info = NULL_TREE;
10378
10379 /* Inside pushdecl_namespace_level, we will push into the
10380 current namespace. However, the friend function should go
10381 into the namespace of the template. */
10382 ns = decl_namespace_context (new_friend);
10383 push_nested_namespace (ns);
10384 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10385 pop_nested_namespace (ns);
10386
10387 if (old_decl == error_mark_node)
10388 return error_mark_node;
10389
10390 if (old_decl != new_friend)
10391 {
10392 /* This new friend declaration matched an existing
10393 declaration. For example, given:
10394
10395 template <class T> void f(T);
10396 template <class U> class C {
10397 template <class T> friend void f(T) {}
10398 };
10399
10400 the friend declaration actually provides the definition
10401 of `f', once C has been instantiated for some type. So,
10402 old_decl will be the out-of-class template declaration,
10403 while new_friend is the in-class definition.
10404
10405 But, if `f' was called before this point, the
10406 instantiation of `f' will have DECL_TI_ARGS corresponding
10407 to `T' but not to `U', references to which might appear
10408 in the definition of `f'. Previously, the most general
10409 template for an instantiation of `f' was the out-of-class
10410 version; now it is the in-class version. Therefore, we
10411 run through all specialization of `f', adding to their
10412 DECL_TI_ARGS appropriately. In particular, they need a
10413 new set of outer arguments, corresponding to the
10414 arguments for this class instantiation.
10415
10416 The same situation can arise with something like this:
10417
10418 friend void f(int);
10419 template <class T> class C {
10420 friend void f(T) {}
10421 };
10422
10423 when `C<int>' is instantiated. Now, `f(int)' is defined
10424 in the class. */
10425
10426 if (!new_friend_is_defn)
10427 /* On the other hand, if the in-class declaration does
10428 *not* provide a definition, then we don't want to alter
10429 existing definitions. We can just leave everything
10430 alone. */
10431 ;
10432 else
10433 {
10434 tree new_template = TI_TEMPLATE (new_friend_template_info);
10435 tree new_args = TI_ARGS (new_friend_template_info);
10436
10437 /* Overwrite whatever template info was there before, if
10438 any, with the new template information pertaining to
10439 the declaration. */
10440 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10441
10442 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10443 {
10444 /* We should have called reregister_specialization in
10445 duplicate_decls. */
10446 gcc_assert (retrieve_specialization (new_template,
10447 new_args, 0)
10448 == old_decl);
10449
10450 /* Instantiate it if the global has already been used. */
10451 if (DECL_ODR_USED (old_decl))
10452 instantiate_decl (old_decl, /*defer_ok=*/true,
10453 /*expl_inst_class_mem_p=*/false);
10454 }
10455 else
10456 {
10457 tree t;
10458
10459 /* Indicate that the old function template is a partial
10460 instantiation. */
10461 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10462 = new_friend_result_template_info;
10463
10464 gcc_assert (new_template
10465 == most_general_template (new_template));
10466 gcc_assert (new_template != old_decl);
10467
10468 /* Reassign any specializations already in the hash table
10469 to the new more general template, and add the
10470 additional template args. */
10471 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10472 t != NULL_TREE;
10473 t = TREE_CHAIN (t))
10474 {
10475 tree spec = TREE_VALUE (t);
10476 spec_entry elt;
10477
10478 elt.tmpl = old_decl;
10479 elt.args = DECL_TI_ARGS (spec);
10480 elt.spec = NULL_TREE;
10481
10482 decl_specializations->remove_elt (&elt);
10483
10484 DECL_TI_ARGS (spec)
10485 = add_outermost_template_args (new_args,
10486 DECL_TI_ARGS (spec));
10487
10488 register_specialization
10489 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10490
10491 }
10492 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10493 }
10494 }
10495
10496 /* The information from NEW_FRIEND has been merged into OLD_DECL
10497 by duplicate_decls. */
10498 new_friend = old_decl;
10499 }
10500 }
10501 else
10502 {
10503 tree context = DECL_CONTEXT (new_friend);
10504 bool dependent_p;
10505
10506 /* In the code
10507 template <class T> class C {
10508 template <class U> friend void C1<U>::f (); // case 1
10509 friend void C2<T>::f (); // case 2
10510 };
10511 we only need to make sure CONTEXT is a complete type for
10512 case 2. To distinguish between the two cases, we note that
10513 CONTEXT of case 1 remains dependent type after tsubst while
10514 this isn't true for case 2. */
10515 ++processing_template_decl;
10516 dependent_p = dependent_type_p (context);
10517 --processing_template_decl;
10518
10519 if (!dependent_p
10520 && !complete_type_or_else (context, NULL_TREE))
10521 return error_mark_node;
10522
10523 if (COMPLETE_TYPE_P (context))
10524 {
10525 tree fn = new_friend;
10526 /* do_friend adds the TEMPLATE_DECL for any member friend
10527 template even if it isn't a member template, i.e.
10528 template <class T> friend A<T>::f();
10529 Look through it in that case. */
10530 if (TREE_CODE (fn) == TEMPLATE_DECL
10531 && !PRIMARY_TEMPLATE_P (fn))
10532 fn = DECL_TEMPLATE_RESULT (fn);
10533 /* Check to see that the declaration is really present, and,
10534 possibly obtain an improved declaration. */
10535 fn = check_classfn (context, fn, NULL_TREE);
10536
10537 if (fn)
10538 new_friend = fn;
10539 }
10540 }
10541
10542 return new_friend;
10543 }
10544
10545 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10546 template arguments, as for tsubst.
10547
10548 Returns an appropriate tsubst'd friend type or error_mark_node on
10549 failure. */
10550
10551 static tree
10552 tsubst_friend_class (tree friend_tmpl, tree args)
10553 {
10554 tree tmpl;
10555
10556 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10557 {
10558 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10559 return TREE_TYPE (tmpl);
10560 }
10561
10562 tree context = CP_DECL_CONTEXT (friend_tmpl);
10563 if (TREE_CODE (context) == NAMESPACE_DECL)
10564 push_nested_namespace (context);
10565 else
10566 {
10567 context = tsubst (context, args, tf_error, NULL_TREE);
10568 push_nested_class (context);
10569 }
10570
10571 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10572 /*non_class=*/false, /*block_p=*/false,
10573 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10574
10575 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10576 {
10577 /* The friend template has already been declared. Just
10578 check to see that the declarations match, and install any new
10579 default parameters. We must tsubst the default parameters,
10580 of course. We only need the innermost template parameters
10581 because that is all that redeclare_class_template will look
10582 at. */
10583 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10584 > TMPL_ARGS_DEPTH (args))
10585 {
10586 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10587 args, tf_warning_or_error);
10588 location_t saved_input_location = input_location;
10589 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10590 tree cons = get_constraints (tmpl);
10591 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10592 input_location = saved_input_location;
10593 }
10594 }
10595 else
10596 {
10597 /* The friend template has not already been declared. In this
10598 case, the instantiation of the template class will cause the
10599 injection of this template into the namespace scope. */
10600 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10601
10602 if (tmpl != error_mark_node)
10603 {
10604 /* The new TMPL is not an instantiation of anything, so we
10605 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10606 for the new type because that is supposed to be the
10607 corresponding template decl, i.e., TMPL. */
10608 DECL_USE_TEMPLATE (tmpl) = 0;
10609 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10610 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10611 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10612 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10613
10614 /* It is hidden. */
10615 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10616 DECL_ANTICIPATED (tmpl)
10617 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10618
10619 /* Inject this template into the enclosing namspace scope. */
10620 tmpl = pushdecl_namespace_level (tmpl, true);
10621 }
10622 }
10623
10624 if (TREE_CODE (context) == NAMESPACE_DECL)
10625 pop_nested_namespace (context);
10626 else
10627 pop_nested_class ();
10628
10629 return TREE_TYPE (tmpl);
10630 }
10631
10632 /* Returns zero if TYPE cannot be completed later due to circularity.
10633 Otherwise returns one. */
10634
10635 static int
10636 can_complete_type_without_circularity (tree type)
10637 {
10638 if (type == NULL_TREE || type == error_mark_node)
10639 return 0;
10640 else if (COMPLETE_TYPE_P (type))
10641 return 1;
10642 else if (TREE_CODE (type) == ARRAY_TYPE)
10643 return can_complete_type_without_circularity (TREE_TYPE (type));
10644 else if (CLASS_TYPE_P (type)
10645 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10646 return 0;
10647 else
10648 return 1;
10649 }
10650
10651 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10652 tsubst_flags_t, tree);
10653
10654 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10655 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10656
10657 static tree
10658 tsubst_attribute (tree t, tree *decl_p, tree args,
10659 tsubst_flags_t complain, tree in_decl)
10660 {
10661 gcc_assert (ATTR_IS_DEPENDENT (t));
10662
10663 tree val = TREE_VALUE (t);
10664 if (val == NULL_TREE)
10665 /* Nothing to do. */;
10666 else if ((flag_openmp || flag_openmp_simd)
10667 && is_attribute_p ("omp declare simd",
10668 get_attribute_name (t)))
10669 {
10670 tree clauses = TREE_VALUE (val);
10671 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10672 complain, in_decl);
10673 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10674 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10675 tree parms = DECL_ARGUMENTS (*decl_p);
10676 clauses
10677 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10678 if (clauses)
10679 val = build_tree_list (NULL_TREE, clauses);
10680 else
10681 val = NULL_TREE;
10682 }
10683 /* If the first attribute argument is an identifier, don't
10684 pass it through tsubst. Attributes like mode, format,
10685 cleanup and several target specific attributes expect it
10686 unmodified. */
10687 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10688 {
10689 tree chain
10690 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10691 /*integral_constant_expression_p=*/false);
10692 if (chain != TREE_CHAIN (val))
10693 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10694 }
10695 else if (PACK_EXPANSION_P (val))
10696 {
10697 /* An attribute pack expansion. */
10698 tree purp = TREE_PURPOSE (t);
10699 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10700 if (pack == error_mark_node)
10701 return error_mark_node;
10702 int len = TREE_VEC_LENGTH (pack);
10703 tree list = NULL_TREE;
10704 tree *q = &list;
10705 for (int i = 0; i < len; ++i)
10706 {
10707 tree elt = TREE_VEC_ELT (pack, i);
10708 *q = build_tree_list (purp, elt);
10709 q = &TREE_CHAIN (*q);
10710 }
10711 return list;
10712 }
10713 else
10714 val = tsubst_expr (val, args, complain, in_decl,
10715 /*integral_constant_expression_p=*/false);
10716
10717 if (val != TREE_VALUE (t))
10718 return build_tree_list (TREE_PURPOSE (t), val);
10719 return t;
10720 }
10721
10722 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10723 unchanged or a new TREE_LIST chain. */
10724
10725 static tree
10726 tsubst_attributes (tree attributes, tree args,
10727 tsubst_flags_t complain, tree in_decl)
10728 {
10729 tree last_dep = NULL_TREE;
10730
10731 for (tree t = attributes; t; t = TREE_CHAIN (t))
10732 if (ATTR_IS_DEPENDENT (t))
10733 {
10734 last_dep = t;
10735 attributes = copy_list (attributes);
10736 break;
10737 }
10738
10739 if (last_dep)
10740 for (tree *p = &attributes; *p; )
10741 {
10742 tree t = *p;
10743 if (ATTR_IS_DEPENDENT (t))
10744 {
10745 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10746 if (subst != t)
10747 {
10748 *p = subst;
10749 while (*p)
10750 p = &TREE_CHAIN (*p);
10751 *p = TREE_CHAIN (t);
10752 continue;
10753 }
10754 }
10755 p = &TREE_CHAIN (*p);
10756 }
10757
10758 return attributes;
10759 }
10760
10761 /* Apply any attributes which had to be deferred until instantiation
10762 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10763 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10764
10765 static void
10766 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10767 tree args, tsubst_flags_t complain, tree in_decl)
10768 {
10769 tree last_dep = NULL_TREE;
10770 tree t;
10771 tree *p;
10772
10773 if (attributes == NULL_TREE)
10774 return;
10775
10776 if (DECL_P (*decl_p))
10777 {
10778 if (TREE_TYPE (*decl_p) == error_mark_node)
10779 return;
10780 p = &DECL_ATTRIBUTES (*decl_p);
10781 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10782 to our attributes parameter. */
10783 gcc_assert (*p == attributes);
10784 }
10785 else
10786 {
10787 p = &TYPE_ATTRIBUTES (*decl_p);
10788 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10789 lookup_template_class_1, and should be preserved. */
10790 gcc_assert (*p != attributes);
10791 while (*p)
10792 p = &TREE_CHAIN (*p);
10793 }
10794
10795 for (t = attributes; t; t = TREE_CHAIN (t))
10796 if (ATTR_IS_DEPENDENT (t))
10797 {
10798 last_dep = t;
10799 attributes = copy_list (attributes);
10800 break;
10801 }
10802
10803 *p = attributes;
10804 if (last_dep)
10805 {
10806 tree late_attrs = NULL_TREE;
10807 tree *q = &late_attrs;
10808
10809 for (; *p; )
10810 {
10811 t = *p;
10812 if (ATTR_IS_DEPENDENT (t))
10813 {
10814 *p = TREE_CHAIN (t);
10815 TREE_CHAIN (t) = NULL_TREE;
10816 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10817 while (*q)
10818 q = &TREE_CHAIN (*q);
10819 }
10820 else
10821 p = &TREE_CHAIN (t);
10822 }
10823
10824 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10825 }
10826 }
10827
10828 /* Perform (or defer) access check for typedefs that were referenced
10829 from within the template TMPL code.
10830 This is a subroutine of instantiate_decl and instantiate_class_template.
10831 TMPL is the template to consider and TARGS is the list of arguments of
10832 that template. */
10833
10834 static void
10835 perform_typedefs_access_check (tree tmpl, tree targs)
10836 {
10837 location_t saved_location;
10838 unsigned i;
10839 qualified_typedef_usage_t *iter;
10840
10841 if (!tmpl
10842 || (!CLASS_TYPE_P (tmpl)
10843 && TREE_CODE (tmpl) != FUNCTION_DECL))
10844 return;
10845
10846 saved_location = input_location;
10847 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10848 {
10849 tree type_decl = iter->typedef_decl;
10850 tree type_scope = iter->context;
10851
10852 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10853 continue;
10854
10855 if (uses_template_parms (type_decl))
10856 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10857 if (uses_template_parms (type_scope))
10858 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10859
10860 /* Make access check error messages point to the location
10861 of the use of the typedef. */
10862 input_location = iter->locus;
10863 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10864 type_decl, type_decl,
10865 tf_warning_or_error);
10866 }
10867 input_location = saved_location;
10868 }
10869
10870 static tree
10871 instantiate_class_template_1 (tree type)
10872 {
10873 tree templ, args, pattern, t, member;
10874 tree typedecl;
10875 tree pbinfo;
10876 tree base_list;
10877 unsigned int saved_maximum_field_alignment;
10878 tree fn_context;
10879
10880 if (type == error_mark_node)
10881 return error_mark_node;
10882
10883 if (COMPLETE_OR_OPEN_TYPE_P (type)
10884 || uses_template_parms (type))
10885 return type;
10886
10887 /* Figure out which template is being instantiated. */
10888 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10889 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10890
10891 /* Mark the type as in the process of being defined. */
10892 TYPE_BEING_DEFINED (type) = 1;
10893
10894 /* We may be in the middle of deferred access check. Disable
10895 it now. */
10896 deferring_access_check_sentinel acs (dk_no_deferred);
10897
10898 /* Determine what specialization of the original template to
10899 instantiate. */
10900 t = most_specialized_partial_spec (type, tf_warning_or_error);
10901 if (t == error_mark_node)
10902 return error_mark_node;
10903 else if (t)
10904 {
10905 /* This TYPE is actually an instantiation of a partial
10906 specialization. We replace the innermost set of ARGS with
10907 the arguments appropriate for substitution. For example,
10908 given:
10909
10910 template <class T> struct S {};
10911 template <class T> struct S<T*> {};
10912
10913 and supposing that we are instantiating S<int*>, ARGS will
10914 presently be {int*} -- but we need {int}. */
10915 pattern = TREE_TYPE (t);
10916 args = TREE_PURPOSE (t);
10917 }
10918 else
10919 {
10920 pattern = TREE_TYPE (templ);
10921 args = CLASSTYPE_TI_ARGS (type);
10922 }
10923
10924 /* If the template we're instantiating is incomplete, then clearly
10925 there's nothing we can do. */
10926 if (!COMPLETE_TYPE_P (pattern))
10927 {
10928 /* We can try again later. */
10929 TYPE_BEING_DEFINED (type) = 0;
10930 return type;
10931 }
10932
10933 /* If we've recursively instantiated too many templates, stop. */
10934 if (! push_tinst_level (type))
10935 return type;
10936
10937 int saved_unevaluated_operand = cp_unevaluated_operand;
10938 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10939
10940 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10941 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10942 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10943 fn_context = error_mark_node;
10944 if (!fn_context)
10945 push_to_top_level ();
10946 else
10947 {
10948 cp_unevaluated_operand = 0;
10949 c_inhibit_evaluation_warnings = 0;
10950 }
10951 /* Use #pragma pack from the template context. */
10952 saved_maximum_field_alignment = maximum_field_alignment;
10953 maximum_field_alignment = TYPE_PRECISION (pattern);
10954
10955 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10956
10957 /* Set the input location to the most specialized template definition.
10958 This is needed if tsubsting causes an error. */
10959 typedecl = TYPE_MAIN_DECL (pattern);
10960 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10961 DECL_SOURCE_LOCATION (typedecl);
10962
10963 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10964 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10965 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10966 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10967 if (ANON_AGGR_TYPE_P (pattern))
10968 SET_ANON_AGGR_TYPE_P (type);
10969 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10970 {
10971 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10972 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10973 /* Adjust visibility for template arguments. */
10974 determine_visibility (TYPE_MAIN_DECL (type));
10975 }
10976 if (CLASS_TYPE_P (type))
10977 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10978
10979 pbinfo = TYPE_BINFO (pattern);
10980
10981 /* We should never instantiate a nested class before its enclosing
10982 class; we need to look up the nested class by name before we can
10983 instantiate it, and that lookup should instantiate the enclosing
10984 class. */
10985 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10986 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10987
10988 base_list = NULL_TREE;
10989 if (BINFO_N_BASE_BINFOS (pbinfo))
10990 {
10991 tree pbase_binfo;
10992 tree pushed_scope;
10993 int i;
10994
10995 /* We must enter the scope containing the type, as that is where
10996 the accessibility of types named in dependent bases are
10997 looked up from. */
10998 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10999
11000 /* Substitute into each of the bases to determine the actual
11001 basetypes. */
11002 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11003 {
11004 tree base;
11005 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11006 tree expanded_bases = NULL_TREE;
11007 int idx, len = 1;
11008
11009 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11010 {
11011 expanded_bases =
11012 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11013 args, tf_error, NULL_TREE);
11014 if (expanded_bases == error_mark_node)
11015 continue;
11016
11017 len = TREE_VEC_LENGTH (expanded_bases);
11018 }
11019
11020 for (idx = 0; idx < len; idx++)
11021 {
11022 if (expanded_bases)
11023 /* Extract the already-expanded base class. */
11024 base = TREE_VEC_ELT (expanded_bases, idx);
11025 else
11026 /* Substitute to figure out the base class. */
11027 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11028 NULL_TREE);
11029
11030 if (base == error_mark_node)
11031 continue;
11032
11033 base_list = tree_cons (access, base, base_list);
11034 if (BINFO_VIRTUAL_P (pbase_binfo))
11035 TREE_TYPE (base_list) = integer_type_node;
11036 }
11037 }
11038
11039 /* The list is now in reverse order; correct that. */
11040 base_list = nreverse (base_list);
11041
11042 if (pushed_scope)
11043 pop_scope (pushed_scope);
11044 }
11045 /* Now call xref_basetypes to set up all the base-class
11046 information. */
11047 xref_basetypes (type, base_list);
11048
11049 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11050 (int) ATTR_FLAG_TYPE_IN_PLACE,
11051 args, tf_error, NULL_TREE);
11052 fixup_attribute_variants (type);
11053
11054 /* Now that our base classes are set up, enter the scope of the
11055 class, so that name lookups into base classes, etc. will work
11056 correctly. This is precisely analogous to what we do in
11057 begin_class_definition when defining an ordinary non-template
11058 class, except we also need to push the enclosing classes. */
11059 push_nested_class (type);
11060
11061 /* Now members are processed in the order of declaration. */
11062 for (member = CLASSTYPE_DECL_LIST (pattern);
11063 member; member = TREE_CHAIN (member))
11064 {
11065 tree t = TREE_VALUE (member);
11066
11067 if (TREE_PURPOSE (member))
11068 {
11069 if (TYPE_P (t))
11070 {
11071 if (LAMBDA_TYPE_P (t))
11072 /* A closure type for a lambda in an NSDMI or default argument.
11073 Ignore it; it will be regenerated when needed. */
11074 continue;
11075
11076 /* Build new CLASSTYPE_NESTED_UTDS. */
11077
11078 tree newtag;
11079 bool class_template_p;
11080
11081 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11082 && TYPE_LANG_SPECIFIC (t)
11083 && CLASSTYPE_IS_TEMPLATE (t));
11084 /* If the member is a class template, then -- even after
11085 substitution -- there may be dependent types in the
11086 template argument list for the class. We increment
11087 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11088 that function will assume that no types are dependent
11089 when outside of a template. */
11090 if (class_template_p)
11091 ++processing_template_decl;
11092 newtag = tsubst (t, args, tf_error, NULL_TREE);
11093 if (class_template_p)
11094 --processing_template_decl;
11095 if (newtag == error_mark_node)
11096 continue;
11097
11098 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11099 {
11100 tree name = TYPE_IDENTIFIER (t);
11101
11102 if (class_template_p)
11103 /* Unfortunately, lookup_template_class sets
11104 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11105 instantiation (i.e., for the type of a member
11106 template class nested within a template class.)
11107 This behavior is required for
11108 maybe_process_partial_specialization to work
11109 correctly, but is not accurate in this case;
11110 the TAG is not an instantiation of anything.
11111 (The corresponding TEMPLATE_DECL is an
11112 instantiation, but the TYPE is not.) */
11113 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11114
11115 /* Now, we call pushtag to put this NEWTAG into the scope of
11116 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11117 pushtag calling push_template_decl. We don't have to do
11118 this for enums because it will already have been done in
11119 tsubst_enum. */
11120 if (name)
11121 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11122 pushtag (name, newtag, /*tag_scope=*/ts_current);
11123 }
11124 }
11125 else if (DECL_DECLARES_FUNCTION_P (t))
11126 {
11127 tree r;
11128
11129 if (TREE_CODE (t) == TEMPLATE_DECL)
11130 ++processing_template_decl;
11131 r = tsubst (t, args, tf_error, NULL_TREE);
11132 if (TREE_CODE (t) == TEMPLATE_DECL)
11133 --processing_template_decl;
11134 set_current_access_from_decl (r);
11135 finish_member_declaration (r);
11136 /* Instantiate members marked with attribute used. */
11137 if (r != error_mark_node && DECL_PRESERVE_P (r))
11138 mark_used (r);
11139 if (TREE_CODE (r) == FUNCTION_DECL
11140 && DECL_OMP_DECLARE_REDUCTION_P (r))
11141 cp_check_omp_declare_reduction (r);
11142 }
11143 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11144 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11145 /* A closure type for a lambda in an NSDMI or default argument.
11146 Ignore it; it will be regenerated when needed. */;
11147 else
11148 {
11149 /* Build new TYPE_FIELDS. */
11150 if (TREE_CODE (t) == STATIC_ASSERT)
11151 {
11152 tree condition;
11153
11154 ++c_inhibit_evaluation_warnings;
11155 condition =
11156 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11157 tf_warning_or_error, NULL_TREE,
11158 /*integral_constant_expression_p=*/true);
11159 --c_inhibit_evaluation_warnings;
11160
11161 finish_static_assert (condition,
11162 STATIC_ASSERT_MESSAGE (t),
11163 STATIC_ASSERT_SOURCE_LOCATION (t),
11164 /*member_p=*/true);
11165 }
11166 else if (TREE_CODE (t) != CONST_DECL)
11167 {
11168 tree r;
11169 tree vec = NULL_TREE;
11170 int len = 1;
11171
11172 /* The file and line for this declaration, to
11173 assist in error message reporting. Since we
11174 called push_tinst_level above, we don't need to
11175 restore these. */
11176 input_location = DECL_SOURCE_LOCATION (t);
11177
11178 if (TREE_CODE (t) == TEMPLATE_DECL)
11179 ++processing_template_decl;
11180 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11181 if (TREE_CODE (t) == TEMPLATE_DECL)
11182 --processing_template_decl;
11183
11184 if (TREE_CODE (r) == TREE_VEC)
11185 {
11186 /* A capture pack became multiple fields. */
11187 vec = r;
11188 len = TREE_VEC_LENGTH (vec);
11189 }
11190
11191 for (int i = 0; i < len; ++i)
11192 {
11193 if (vec)
11194 r = TREE_VEC_ELT (vec, i);
11195 if (VAR_P (r))
11196 {
11197 /* In [temp.inst]:
11198
11199 [t]he initialization (and any associated
11200 side-effects) of a static data member does
11201 not occur unless the static data member is
11202 itself used in a way that requires the
11203 definition of the static data member to
11204 exist.
11205
11206 Therefore, we do not substitute into the
11207 initialized for the static data member here. */
11208 finish_static_data_member_decl
11209 (r,
11210 /*init=*/NULL_TREE,
11211 /*init_const_expr_p=*/false,
11212 /*asmspec_tree=*/NULL_TREE,
11213 /*flags=*/0);
11214 /* Instantiate members marked with attribute used. */
11215 if (r != error_mark_node && DECL_PRESERVE_P (r))
11216 mark_used (r);
11217 }
11218 else if (TREE_CODE (r) == FIELD_DECL)
11219 {
11220 /* Determine whether R has a valid type and can be
11221 completed later. If R is invalid, then its type
11222 is replaced by error_mark_node. */
11223 tree rtype = TREE_TYPE (r);
11224 if (can_complete_type_without_circularity (rtype))
11225 complete_type (rtype);
11226
11227 if (!complete_or_array_type_p (rtype))
11228 {
11229 /* If R's type couldn't be completed and
11230 it isn't a flexible array member (whose
11231 type is incomplete by definition) give
11232 an error. */
11233 cxx_incomplete_type_error (r, rtype);
11234 TREE_TYPE (r) = error_mark_node;
11235 }
11236 else if (TREE_CODE (rtype) == ARRAY_TYPE
11237 && TYPE_DOMAIN (rtype) == NULL_TREE
11238 && (TREE_CODE (type) == UNION_TYPE
11239 || TREE_CODE (type) == QUAL_UNION_TYPE))
11240 {
11241 error ("flexible array member %qD in union", r);
11242 TREE_TYPE (r) = error_mark_node;
11243 }
11244 }
11245
11246 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11247 such a thing will already have been added to the field
11248 list by tsubst_enum in finish_member_declaration in the
11249 CLASSTYPE_NESTED_UTDS case above. */
11250 if (!(TREE_CODE (r) == TYPE_DECL
11251 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11252 && DECL_ARTIFICIAL (r)))
11253 {
11254 set_current_access_from_decl (r);
11255 finish_member_declaration (r);
11256 }
11257 }
11258 }
11259 }
11260 }
11261 else
11262 {
11263 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11264 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11265 {
11266 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11267
11268 tree friend_type = t;
11269 bool adjust_processing_template_decl = false;
11270
11271 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11272 {
11273 /* template <class T> friend class C; */
11274 friend_type = tsubst_friend_class (friend_type, args);
11275 adjust_processing_template_decl = true;
11276 }
11277 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11278 {
11279 /* template <class T> friend class C::D; */
11280 friend_type = tsubst (friend_type, args,
11281 tf_warning_or_error, NULL_TREE);
11282 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11283 friend_type = TREE_TYPE (friend_type);
11284 adjust_processing_template_decl = true;
11285 }
11286 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11287 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11288 {
11289 /* This could be either
11290
11291 friend class T::C;
11292
11293 when dependent_type_p is false or
11294
11295 template <class U> friend class T::C;
11296
11297 otherwise. */
11298 /* Bump processing_template_decl in case this is something like
11299 template <class T> friend struct A<T>::B. */
11300 ++processing_template_decl;
11301 friend_type = tsubst (friend_type, args,
11302 tf_warning_or_error, NULL_TREE);
11303 if (dependent_type_p (friend_type))
11304 adjust_processing_template_decl = true;
11305 --processing_template_decl;
11306 }
11307 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11308 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11309 && TYPE_HIDDEN_P (friend_type))
11310 {
11311 /* friend class C;
11312
11313 where C hasn't been declared yet. Let's lookup name
11314 from namespace scope directly, bypassing any name that
11315 come from dependent base class. */
11316 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11317
11318 /* The call to xref_tag_from_type does injection for friend
11319 classes. */
11320 push_nested_namespace (ns);
11321 friend_type =
11322 xref_tag_from_type (friend_type, NULL_TREE,
11323 /*tag_scope=*/ts_current);
11324 pop_nested_namespace (ns);
11325 }
11326 else if (uses_template_parms (friend_type))
11327 /* friend class C<T>; */
11328 friend_type = tsubst (friend_type, args,
11329 tf_warning_or_error, NULL_TREE);
11330 /* Otherwise it's
11331
11332 friend class C;
11333
11334 where C is already declared or
11335
11336 friend class C<int>;
11337
11338 We don't have to do anything in these cases. */
11339
11340 if (adjust_processing_template_decl)
11341 /* Trick make_friend_class into realizing that the friend
11342 we're adding is a template, not an ordinary class. It's
11343 important that we use make_friend_class since it will
11344 perform some error-checking and output cross-reference
11345 information. */
11346 ++processing_template_decl;
11347
11348 if (friend_type != error_mark_node)
11349 make_friend_class (type, friend_type, /*complain=*/false);
11350
11351 if (adjust_processing_template_decl)
11352 --processing_template_decl;
11353 }
11354 else
11355 {
11356 /* Build new DECL_FRIENDLIST. */
11357 tree r;
11358
11359 /* The file and line for this declaration, to
11360 assist in error message reporting. Since we
11361 called push_tinst_level above, we don't need to
11362 restore these. */
11363 input_location = DECL_SOURCE_LOCATION (t);
11364
11365 if (TREE_CODE (t) == TEMPLATE_DECL)
11366 {
11367 ++processing_template_decl;
11368 push_deferring_access_checks (dk_no_check);
11369 }
11370
11371 r = tsubst_friend_function (t, args);
11372 add_friend (type, r, /*complain=*/false);
11373 if (TREE_CODE (t) == TEMPLATE_DECL)
11374 {
11375 pop_deferring_access_checks ();
11376 --processing_template_decl;
11377 }
11378 }
11379 }
11380 }
11381
11382 if (fn_context)
11383 {
11384 /* Restore these before substituting into the lambda capture
11385 initializers. */
11386 cp_unevaluated_operand = saved_unevaluated_operand;
11387 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11388 }
11389
11390 /* Set the file and line number information to whatever is given for
11391 the class itself. This puts error messages involving generated
11392 implicit functions at a predictable point, and the same point
11393 that would be used for non-template classes. */
11394 input_location = DECL_SOURCE_LOCATION (typedecl);
11395
11396 unreverse_member_declarations (type);
11397 finish_struct_1 (type);
11398 TYPE_BEING_DEFINED (type) = 0;
11399
11400 /* We don't instantiate default arguments for member functions. 14.7.1:
11401
11402 The implicit instantiation of a class template specialization causes
11403 the implicit instantiation of the declarations, but not of the
11404 definitions or default arguments, of the class member functions,
11405 member classes, static data members and member templates.... */
11406
11407 /* Some typedefs referenced from within the template code need to be access
11408 checked at template instantiation time, i.e now. These types were
11409 added to the template at parsing time. Let's get those and perform
11410 the access checks then. */
11411 perform_typedefs_access_check (pattern, args);
11412 perform_deferred_access_checks (tf_warning_or_error);
11413 pop_nested_class ();
11414 maximum_field_alignment = saved_maximum_field_alignment;
11415 if (!fn_context)
11416 pop_from_top_level ();
11417 pop_tinst_level ();
11418
11419 /* The vtable for a template class can be emitted in any translation
11420 unit in which the class is instantiated. When there is no key
11421 method, however, finish_struct_1 will already have added TYPE to
11422 the keyed_classes. */
11423 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11424 vec_safe_push (keyed_classes, type);
11425
11426 return type;
11427 }
11428
11429 /* Wrapper for instantiate_class_template_1. */
11430
11431 tree
11432 instantiate_class_template (tree type)
11433 {
11434 tree ret;
11435 timevar_push (TV_TEMPLATE_INST);
11436 ret = instantiate_class_template_1 (type);
11437 timevar_pop (TV_TEMPLATE_INST);
11438 return ret;
11439 }
11440
11441 static tree
11442 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11443 {
11444 tree r;
11445
11446 if (!t)
11447 r = t;
11448 else if (TYPE_P (t))
11449 r = tsubst (t, args, complain, in_decl);
11450 else
11451 {
11452 if (!(complain & tf_warning))
11453 ++c_inhibit_evaluation_warnings;
11454 r = tsubst_expr (t, args, complain, in_decl,
11455 /*integral_constant_expression_p=*/true);
11456 if (!(complain & tf_warning))
11457 --c_inhibit_evaluation_warnings;
11458 }
11459 return r;
11460 }
11461
11462 /* Given a function parameter pack TMPL_PARM and some function parameters
11463 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11464 and set *SPEC_P to point at the next point in the list. */
11465
11466 tree
11467 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11468 {
11469 /* Collect all of the extra "packed" parameters into an
11470 argument pack. */
11471 tree parmvec;
11472 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11473 tree spec_parm = *spec_p;
11474 int i, len;
11475
11476 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11477 if (tmpl_parm
11478 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11479 break;
11480
11481 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11482 parmvec = make_tree_vec (len);
11483 spec_parm = *spec_p;
11484 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11485 {
11486 tree elt = spec_parm;
11487 if (DECL_PACK_P (elt))
11488 elt = make_pack_expansion (elt);
11489 TREE_VEC_ELT (parmvec, i) = elt;
11490 }
11491
11492 /* Build the argument packs. */
11493 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11494 *spec_p = spec_parm;
11495
11496 return argpack;
11497 }
11498
11499 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11500 NONTYPE_ARGUMENT_PACK. */
11501
11502 static tree
11503 make_fnparm_pack (tree spec_parm)
11504 {
11505 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11506 }
11507
11508 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11509 pack expansion with no extra args, 2 if it has extra args, or 0
11510 if it is not a pack expansion. */
11511
11512 static int
11513 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11514 {
11515 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11516 if (i >= TREE_VEC_LENGTH (vec))
11517 return 0;
11518 tree elt = TREE_VEC_ELT (vec, i);
11519 if (DECL_P (elt))
11520 /* A decl pack is itself an expansion. */
11521 elt = TREE_TYPE (elt);
11522 if (!PACK_EXPANSION_P (elt))
11523 return 0;
11524 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11525 return 2;
11526 return 1;
11527 }
11528
11529
11530 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11531
11532 static tree
11533 make_argument_pack_select (tree arg_pack, unsigned index)
11534 {
11535 tree aps = make_node (ARGUMENT_PACK_SELECT);
11536
11537 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11538 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11539
11540 return aps;
11541 }
11542
11543 /* This is a subroutine of tsubst_pack_expansion.
11544
11545 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11546 mechanism to store the (non complete list of) arguments of the
11547 substitution and return a non substituted pack expansion, in order
11548 to wait for when we have enough arguments to really perform the
11549 substitution. */
11550
11551 static bool
11552 use_pack_expansion_extra_args_p (tree parm_packs,
11553 int arg_pack_len,
11554 bool has_empty_arg)
11555 {
11556 /* If one pack has an expansion and another pack has a normal
11557 argument or if one pack has an empty argument and an another
11558 one hasn't then tsubst_pack_expansion cannot perform the
11559 substitution and need to fall back on the
11560 PACK_EXPANSION_EXTRA mechanism. */
11561 if (parm_packs == NULL_TREE)
11562 return false;
11563 else if (has_empty_arg)
11564 return true;
11565
11566 bool has_expansion_arg = false;
11567 for (int i = 0 ; i < arg_pack_len; ++i)
11568 {
11569 bool has_non_expansion_arg = false;
11570 for (tree parm_pack = parm_packs;
11571 parm_pack;
11572 parm_pack = TREE_CHAIN (parm_pack))
11573 {
11574 tree arg = TREE_VALUE (parm_pack);
11575
11576 int exp = argument_pack_element_is_expansion_p (arg, i);
11577 if (exp == 2)
11578 /* We can't substitute a pack expansion with extra args into
11579 our pattern. */
11580 return true;
11581 else if (exp)
11582 has_expansion_arg = true;
11583 else
11584 has_non_expansion_arg = true;
11585 }
11586
11587 if (has_expansion_arg && has_non_expansion_arg)
11588 return true;
11589 }
11590 return false;
11591 }
11592
11593 /* [temp.variadic]/6 says that:
11594
11595 The instantiation of a pack expansion [...]
11596 produces a list E1,E2, ..., En, where N is the number of elements
11597 in the pack expansion parameters.
11598
11599 This subroutine of tsubst_pack_expansion produces one of these Ei.
11600
11601 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11602 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11603 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11604 INDEX is the index 'i' of the element Ei to produce. ARGS,
11605 COMPLAIN, and IN_DECL are the same parameters as for the
11606 tsubst_pack_expansion function.
11607
11608 The function returns the resulting Ei upon successful completion,
11609 or error_mark_node.
11610
11611 Note that this function possibly modifies the ARGS parameter, so
11612 it's the responsibility of the caller to restore it. */
11613
11614 static tree
11615 gen_elem_of_pack_expansion_instantiation (tree pattern,
11616 tree parm_packs,
11617 unsigned index,
11618 tree args /* This parm gets
11619 modified. */,
11620 tsubst_flags_t complain,
11621 tree in_decl)
11622 {
11623 tree t;
11624 bool ith_elem_is_expansion = false;
11625
11626 /* For each parameter pack, change the substitution of the parameter
11627 pack to the ith argument in its argument pack, then expand the
11628 pattern. */
11629 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11630 {
11631 tree parm = TREE_PURPOSE (pack);
11632 tree arg_pack = TREE_VALUE (pack);
11633 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11634
11635 ith_elem_is_expansion |=
11636 argument_pack_element_is_expansion_p (arg_pack, index);
11637
11638 /* Select the Ith argument from the pack. */
11639 if (TREE_CODE (parm) == PARM_DECL
11640 || VAR_P (parm)
11641 || TREE_CODE (parm) == FIELD_DECL)
11642 {
11643 if (index == 0)
11644 {
11645 aps = make_argument_pack_select (arg_pack, index);
11646 if (!mark_used (parm, complain) && !(complain & tf_error))
11647 return error_mark_node;
11648 register_local_specialization (aps, parm);
11649 }
11650 else
11651 aps = retrieve_local_specialization (parm);
11652 }
11653 else
11654 {
11655 int idx, level;
11656 template_parm_level_and_index (parm, &level, &idx);
11657
11658 if (index == 0)
11659 {
11660 aps = make_argument_pack_select (arg_pack, index);
11661 /* Update the corresponding argument. */
11662 TMPL_ARG (args, level, idx) = aps;
11663 }
11664 else
11665 /* Re-use the ARGUMENT_PACK_SELECT. */
11666 aps = TMPL_ARG (args, level, idx);
11667 }
11668 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11669 }
11670
11671 /* Substitute into the PATTERN with the (possibly altered)
11672 arguments. */
11673 if (pattern == in_decl)
11674 /* Expanding a fixed parameter pack from
11675 coerce_template_parameter_pack. */
11676 t = tsubst_decl (pattern, args, complain);
11677 else if (pattern == error_mark_node)
11678 t = error_mark_node;
11679 else if (constraint_p (pattern))
11680 {
11681 if (processing_template_decl)
11682 t = tsubst_constraint (pattern, args, complain, in_decl);
11683 else
11684 t = (constraints_satisfied_p (pattern, args)
11685 ? boolean_true_node : boolean_false_node);
11686 }
11687 else if (!TYPE_P (pattern))
11688 t = tsubst_expr (pattern, args, complain, in_decl,
11689 /*integral_constant_expression_p=*/false);
11690 else
11691 t = tsubst (pattern, args, complain, in_decl);
11692
11693 /* If the Ith argument pack element is a pack expansion, then
11694 the Ith element resulting from the substituting is going to
11695 be a pack expansion as well. */
11696 if (ith_elem_is_expansion)
11697 t = make_pack_expansion (t, complain);
11698
11699 return t;
11700 }
11701
11702 /* When the unexpanded parameter pack in a fold expression expands to an empty
11703 sequence, the value of the expression is as follows; the program is
11704 ill-formed if the operator is not listed in this table.
11705
11706 && true
11707 || false
11708 , void() */
11709
11710 tree
11711 expand_empty_fold (tree t, tsubst_flags_t complain)
11712 {
11713 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11714 if (!FOLD_EXPR_MODIFY_P (t))
11715 switch (code)
11716 {
11717 case TRUTH_ANDIF_EXPR:
11718 return boolean_true_node;
11719 case TRUTH_ORIF_EXPR:
11720 return boolean_false_node;
11721 case COMPOUND_EXPR:
11722 return void_node;
11723 default:
11724 break;
11725 }
11726
11727 if (complain & tf_error)
11728 error_at (location_of (t),
11729 "fold of empty expansion over %O", code);
11730 return error_mark_node;
11731 }
11732
11733 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11734 form an expression that combines the two terms using the
11735 operator of T. */
11736
11737 static tree
11738 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11739 {
11740 tree op = FOLD_EXPR_OP (t);
11741 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11742
11743 // Handle compound assignment operators.
11744 if (FOLD_EXPR_MODIFY_P (t))
11745 return build_x_modify_expr (input_location, left, code, right, complain);
11746
11747 switch (code)
11748 {
11749 case COMPOUND_EXPR:
11750 return build_x_compound_expr (input_location, left, right, complain);
11751 case DOTSTAR_EXPR:
11752 return build_m_component_ref (left, right, complain);
11753 default:
11754 return build_x_binary_op (input_location, code,
11755 left, TREE_CODE (left),
11756 right, TREE_CODE (right),
11757 /*overload=*/NULL,
11758 complain);
11759 }
11760 }
11761
11762 /* Substitute ARGS into the pack of a fold expression T. */
11763
11764 static inline tree
11765 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11766 {
11767 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11768 }
11769
11770 /* Substitute ARGS into the pack of a fold expression T. */
11771
11772 static inline tree
11773 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11774 {
11775 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11776 }
11777
11778 /* Expand a PACK of arguments into a grouped as left fold.
11779 Given a pack containing elements A0, A1, ..., An and an
11780 operator @, this builds the expression:
11781
11782 ((A0 @ A1) @ A2) ... @ An
11783
11784 Note that PACK must not be empty.
11785
11786 The operator is defined by the original fold expression T. */
11787
11788 static tree
11789 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11790 {
11791 tree left = TREE_VEC_ELT (pack, 0);
11792 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11793 {
11794 tree right = TREE_VEC_ELT (pack, i);
11795 left = fold_expression (t, left, right, complain);
11796 }
11797 return left;
11798 }
11799
11800 /* Substitute into a unary left fold expression. */
11801
11802 static tree
11803 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11804 tree in_decl)
11805 {
11806 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11807 if (pack == error_mark_node)
11808 return error_mark_node;
11809 if (PACK_EXPANSION_P (pack))
11810 {
11811 tree r = copy_node (t);
11812 FOLD_EXPR_PACK (r) = pack;
11813 return r;
11814 }
11815 if (TREE_VEC_LENGTH (pack) == 0)
11816 return expand_empty_fold (t, complain);
11817 else
11818 return expand_left_fold (t, pack, complain);
11819 }
11820
11821 /* Substitute into a binary left fold expression.
11822
11823 Do ths by building a single (non-empty) vector of argumnts and
11824 building the expression from those elements. */
11825
11826 static tree
11827 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11828 tree in_decl)
11829 {
11830 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11831 if (pack == error_mark_node)
11832 return error_mark_node;
11833 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11834 if (init == error_mark_node)
11835 return error_mark_node;
11836
11837 if (PACK_EXPANSION_P (pack))
11838 {
11839 tree r = copy_node (t);
11840 FOLD_EXPR_PACK (r) = pack;
11841 FOLD_EXPR_INIT (r) = init;
11842 return r;
11843 }
11844
11845 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11846 TREE_VEC_ELT (vec, 0) = init;
11847 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11848 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11849
11850 return expand_left_fold (t, vec, complain);
11851 }
11852
11853 /* Expand a PACK of arguments into a grouped as right fold.
11854 Given a pack containing elementns A0, A1, ..., and an
11855 operator @, this builds the expression:
11856
11857 A0@ ... (An-2 @ (An-1 @ An))
11858
11859 Note that PACK must not be empty.
11860
11861 The operator is defined by the original fold expression T. */
11862
11863 tree
11864 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11865 {
11866 // Build the expression.
11867 int n = TREE_VEC_LENGTH (pack);
11868 tree right = TREE_VEC_ELT (pack, n - 1);
11869 for (--n; n != 0; --n)
11870 {
11871 tree left = TREE_VEC_ELT (pack, n - 1);
11872 right = fold_expression (t, left, right, complain);
11873 }
11874 return right;
11875 }
11876
11877 /* Substitute into a unary right fold expression. */
11878
11879 static tree
11880 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11881 tree in_decl)
11882 {
11883 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11884 if (pack == error_mark_node)
11885 return error_mark_node;
11886 if (PACK_EXPANSION_P (pack))
11887 {
11888 tree r = copy_node (t);
11889 FOLD_EXPR_PACK (r) = pack;
11890 return r;
11891 }
11892 if (TREE_VEC_LENGTH (pack) == 0)
11893 return expand_empty_fold (t, complain);
11894 else
11895 return expand_right_fold (t, pack, complain);
11896 }
11897
11898 /* Substitute into a binary right fold expression.
11899
11900 Do ths by building a single (non-empty) vector of arguments and
11901 building the expression from those elements. */
11902
11903 static tree
11904 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11905 tree in_decl)
11906 {
11907 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11908 if (pack == error_mark_node)
11909 return error_mark_node;
11910 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11911 if (init == error_mark_node)
11912 return error_mark_node;
11913
11914 if (PACK_EXPANSION_P (pack))
11915 {
11916 tree r = copy_node (t);
11917 FOLD_EXPR_PACK (r) = pack;
11918 FOLD_EXPR_INIT (r) = init;
11919 return r;
11920 }
11921
11922 int n = TREE_VEC_LENGTH (pack);
11923 tree vec = make_tree_vec (n + 1);
11924 for (int i = 0; i < n; ++i)
11925 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11926 TREE_VEC_ELT (vec, n) = init;
11927
11928 return expand_right_fold (t, vec, complain);
11929 }
11930
11931 /* Walk through the pattern of a pack expansion, adding everything in
11932 local_specializations to a list. */
11933
11934 struct el_data
11935 {
11936 hash_set<tree> internal;
11937 tree extra;
11938 tsubst_flags_t complain;
11939
11940 el_data (tsubst_flags_t c)
11941 : extra (NULL_TREE), complain (c) {}
11942 };
11943 static tree
11944 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11945 {
11946 el_data &data = *reinterpret_cast<el_data*>(data_);
11947 tree *extra = &data.extra;
11948 tsubst_flags_t complain = data.complain;
11949
11950 if (TYPE_P (*tp) && typedef_variant_p (*tp))
11951 /* Remember local typedefs (85214). */
11952 tp = &TYPE_NAME (*tp);
11953
11954 if (TREE_CODE (*tp) == DECL_EXPR)
11955 data.internal.add (DECL_EXPR_DECL (*tp));
11956 else if (tree spec = retrieve_local_specialization (*tp))
11957 {
11958 if (data.internal.contains (*tp))
11959 /* Don't mess with variables declared within the pattern. */
11960 return NULL_TREE;
11961 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11962 {
11963 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11964 tree args = ARGUMENT_PACK_ARGS (spec);
11965 if (TREE_VEC_LENGTH (args) == 1)
11966 {
11967 tree elt = TREE_VEC_ELT (args, 0);
11968 if (PACK_EXPANSION_P (elt))
11969 elt = PACK_EXPANSION_PATTERN (elt);
11970 if (DECL_PACK_P (elt))
11971 spec = elt;
11972 }
11973 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11974 {
11975 /* Handle lambda capture here, since we aren't doing any
11976 substitution now, and so tsubst_copy won't call
11977 process_outer_var_ref. */
11978 tree args = ARGUMENT_PACK_ARGS (spec);
11979 int len = TREE_VEC_LENGTH (args);
11980 for (int i = 0; i < len; ++i)
11981 {
11982 tree arg = TREE_VEC_ELT (args, i);
11983 tree carg = arg;
11984 if (outer_automatic_var_p (arg))
11985 carg = process_outer_var_ref (arg, complain);
11986 if (carg != arg)
11987 {
11988 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11989 proxies. */
11990 if (i == 0)
11991 {
11992 spec = copy_node (spec);
11993 args = copy_node (args);
11994 SET_ARGUMENT_PACK_ARGS (spec, args);
11995 register_local_specialization (spec, *tp);
11996 }
11997 TREE_VEC_ELT (args, i) = carg;
11998 }
11999 }
12000 }
12001 }
12002 if (outer_automatic_var_p (spec))
12003 spec = process_outer_var_ref (spec, complain);
12004 *extra = tree_cons (*tp, spec, *extra);
12005 }
12006 return NULL_TREE;
12007 }
12008 static tree
12009 extract_local_specs (tree pattern, tsubst_flags_t complain)
12010 {
12011 el_data data (complain);
12012 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12013 return data.extra;
12014 }
12015
12016 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12017 for use in PACK_EXPANSION_EXTRA_ARGS. */
12018
12019 tree
12020 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12021 {
12022 tree extra = args;
12023 if (local_specializations)
12024 if (tree locals = extract_local_specs (pattern, complain))
12025 extra = tree_cons (NULL_TREE, extra, locals);
12026 return extra;
12027 }
12028
12029 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12030 normal template args to ARGS. */
12031
12032 tree
12033 add_extra_args (tree extra, tree args)
12034 {
12035 if (extra && TREE_CODE (extra) == TREE_LIST)
12036 {
12037 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12038 {
12039 /* The partial instantiation involved local declarations collected in
12040 extract_local_specs; map from the general template to our local
12041 context. */
12042 tree gen = TREE_PURPOSE (elt);
12043 tree inst = TREE_VALUE (elt);
12044 if (DECL_P (inst))
12045 if (tree local = retrieve_local_specialization (inst))
12046 inst = local;
12047 /* else inst is already a full instantiation of the pack. */
12048 register_local_specialization (inst, gen);
12049 }
12050 gcc_assert (!TREE_PURPOSE (extra));
12051 extra = TREE_VALUE (extra);
12052 }
12053 return add_to_template_args (extra, args);
12054 }
12055
12056 /* Substitute ARGS into T, which is an pack expansion
12057 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12058 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12059 (if only a partial substitution could be performed) or
12060 ERROR_MARK_NODE if there was an error. */
12061 tree
12062 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12063 tree in_decl)
12064 {
12065 tree pattern;
12066 tree pack, packs = NULL_TREE;
12067 bool unsubstituted_packs = false;
12068 bool unsubstituted_fn_pack = false;
12069 int i, len = -1;
12070 tree result;
12071 hash_map<tree, tree> *saved_local_specializations = NULL;
12072 bool need_local_specializations = false;
12073 int levels;
12074
12075 gcc_assert (PACK_EXPANSION_P (t));
12076 pattern = PACK_EXPANSION_PATTERN (t);
12077
12078 /* Add in any args remembered from an earlier partial instantiation. */
12079 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12080
12081 levels = TMPL_ARGS_DEPTH (args);
12082
12083 /* Determine the argument packs that will instantiate the parameter
12084 packs used in the expansion expression. While we're at it,
12085 compute the number of arguments to be expanded and make sure it
12086 is consistent. */
12087 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12088 pack = TREE_CHAIN (pack))
12089 {
12090 tree parm_pack = TREE_VALUE (pack);
12091 tree arg_pack = NULL_TREE;
12092 tree orig_arg = NULL_TREE;
12093 int level = 0;
12094
12095 if (TREE_CODE (parm_pack) == BASES)
12096 {
12097 gcc_assert (parm_pack == pattern);
12098 if (BASES_DIRECT (parm_pack))
12099 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12100 args, complain,
12101 in_decl, false),
12102 complain);
12103 else
12104 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12105 args, complain, in_decl,
12106 false), complain);
12107 }
12108 else if (builtin_pack_call_p (parm_pack))
12109 {
12110 if (parm_pack != pattern)
12111 {
12112 if (complain & tf_error)
12113 sorry ("%qE is not the entire pattern of the pack expansion",
12114 parm_pack);
12115 return error_mark_node;
12116 }
12117 return expand_builtin_pack_call (parm_pack, args,
12118 complain, in_decl);
12119 }
12120 else if (TREE_CODE (parm_pack) == PARM_DECL)
12121 {
12122 /* We know we have correct local_specializations if this
12123 expansion is at function scope, or if we're dealing with a
12124 local parameter in a requires expression; for the latter,
12125 tsubst_requires_expr set it up appropriately. */
12126 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12127 arg_pack = retrieve_local_specialization (parm_pack);
12128 else
12129 /* We can't rely on local_specializations for a parameter
12130 name used later in a function declaration (such as in a
12131 late-specified return type). Even if it exists, it might
12132 have the wrong value for a recursive call. */
12133 need_local_specializations = true;
12134
12135 if (!arg_pack)
12136 {
12137 /* This parameter pack was used in an unevaluated context. Just
12138 make a dummy decl, since it's only used for its type. */
12139 ++cp_unevaluated_operand;
12140 arg_pack = tsubst_decl (parm_pack, args, complain);
12141 --cp_unevaluated_operand;
12142 if (arg_pack && DECL_PACK_P (arg_pack))
12143 /* Partial instantiation of the parm_pack, we can't build
12144 up an argument pack yet. */
12145 arg_pack = NULL_TREE;
12146 else
12147 arg_pack = make_fnparm_pack (arg_pack);
12148 }
12149 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12150 /* This argument pack isn't fully instantiated yet. We set this
12151 flag rather than clear arg_pack because we do want to do the
12152 optimization below, and we don't want to substitute directly
12153 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12154 where it isn't expected). */
12155 unsubstituted_fn_pack = true;
12156 }
12157 else if (is_capture_proxy (parm_pack))
12158 {
12159 arg_pack = retrieve_local_specialization (parm_pack);
12160 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12161 unsubstituted_fn_pack = true;
12162 }
12163 else
12164 {
12165 int idx;
12166 template_parm_level_and_index (parm_pack, &level, &idx);
12167
12168 if (level <= levels)
12169 arg_pack = TMPL_ARG (args, level, idx);
12170 }
12171
12172 orig_arg = arg_pack;
12173 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12174 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12175
12176 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12177 /* This can only happen if we forget to expand an argument
12178 pack somewhere else. Just return an error, silently. */
12179 {
12180 result = make_tree_vec (1);
12181 TREE_VEC_ELT (result, 0) = error_mark_node;
12182 return result;
12183 }
12184
12185 if (arg_pack)
12186 {
12187 int my_len =
12188 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12189
12190 /* Don't bother trying to do a partial substitution with
12191 incomplete packs; we'll try again after deduction. */
12192 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12193 return t;
12194
12195 if (len < 0)
12196 len = my_len;
12197 else if (len != my_len
12198 && !unsubstituted_fn_pack)
12199 {
12200 if (!(complain & tf_error))
12201 /* Fail quietly. */;
12202 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12203 error ("mismatched argument pack lengths while expanding %qT",
12204 pattern);
12205 else
12206 error ("mismatched argument pack lengths while expanding %qE",
12207 pattern);
12208 return error_mark_node;
12209 }
12210
12211 /* Keep track of the parameter packs and their corresponding
12212 argument packs. */
12213 packs = tree_cons (parm_pack, arg_pack, packs);
12214 TREE_TYPE (packs) = orig_arg;
12215 }
12216 else
12217 {
12218 /* We can't substitute for this parameter pack. We use a flag as
12219 well as the missing_level counter because function parameter
12220 packs don't have a level. */
12221 gcc_assert (processing_template_decl || is_auto (parm_pack));
12222 unsubstituted_packs = true;
12223 }
12224 }
12225
12226 /* If the expansion is just T..., return the matching argument pack, unless
12227 we need to call convert_from_reference on all the elements. This is an
12228 important optimization; see c++/68422. */
12229 if (!unsubstituted_packs
12230 && TREE_PURPOSE (packs) == pattern)
12231 {
12232 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12233
12234 /* If the argument pack is a single pack expansion, pull it out. */
12235 if (TREE_VEC_LENGTH (args) == 1
12236 && pack_expansion_args_count (args))
12237 return TREE_VEC_ELT (args, 0);
12238
12239 /* Types need no adjustment, nor does sizeof..., and if we still have
12240 some pack expansion args we won't do anything yet. */
12241 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12242 || PACK_EXPANSION_SIZEOF_P (t)
12243 || pack_expansion_args_count (args))
12244 return args;
12245 /* Also optimize expression pack expansions if we can tell that the
12246 elements won't have reference type. */
12247 tree type = TREE_TYPE (pattern);
12248 if (type && !TYPE_REF_P (type)
12249 && !PACK_EXPANSION_P (type)
12250 && !WILDCARD_TYPE_P (type))
12251 return args;
12252 /* Otherwise use the normal path so we get convert_from_reference. */
12253 }
12254
12255 /* We cannot expand this expansion expression, because we don't have
12256 all of the argument packs we need. */
12257 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12258 {
12259 /* We got some full packs, but we can't substitute them in until we
12260 have values for all the packs. So remember these until then. */
12261
12262 t = make_pack_expansion (pattern, complain);
12263 PACK_EXPANSION_EXTRA_ARGS (t)
12264 = build_extra_args (pattern, args, complain);
12265 return t;
12266 }
12267 else if (unsubstituted_packs)
12268 {
12269 /* There were no real arguments, we're just replacing a parameter
12270 pack with another version of itself. Substitute into the
12271 pattern and return a PACK_EXPANSION_*. The caller will need to
12272 deal with that. */
12273 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12274 t = tsubst_expr (pattern, args, complain, in_decl,
12275 /*integral_constant_expression_p=*/false);
12276 else
12277 t = tsubst (pattern, args, complain, in_decl);
12278 t = make_pack_expansion (t, complain);
12279 return t;
12280 }
12281
12282 gcc_assert (len >= 0);
12283
12284 if (need_local_specializations)
12285 {
12286 /* We're in a late-specified return type, so create our own local
12287 specializations map; the current map is either NULL or (in the
12288 case of recursive unification) might have bindings that we don't
12289 want to use or alter. */
12290 saved_local_specializations = local_specializations;
12291 local_specializations = new hash_map<tree, tree>;
12292 }
12293
12294 /* For each argument in each argument pack, substitute into the
12295 pattern. */
12296 result = make_tree_vec (len);
12297 tree elem_args = copy_template_args (args);
12298 for (i = 0; i < len; ++i)
12299 {
12300 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12301 i,
12302 elem_args, complain,
12303 in_decl);
12304 TREE_VEC_ELT (result, i) = t;
12305 if (t == error_mark_node)
12306 {
12307 result = error_mark_node;
12308 break;
12309 }
12310 }
12311
12312 /* Update ARGS to restore the substitution from parameter packs to
12313 their argument packs. */
12314 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12315 {
12316 tree parm = TREE_PURPOSE (pack);
12317
12318 if (TREE_CODE (parm) == PARM_DECL
12319 || VAR_P (parm)
12320 || TREE_CODE (parm) == FIELD_DECL)
12321 register_local_specialization (TREE_TYPE (pack), parm);
12322 else
12323 {
12324 int idx, level;
12325
12326 if (TREE_VALUE (pack) == NULL_TREE)
12327 continue;
12328
12329 template_parm_level_and_index (parm, &level, &idx);
12330
12331 /* Update the corresponding argument. */
12332 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12333 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12334 TREE_TYPE (pack);
12335 else
12336 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12337 }
12338 }
12339
12340 if (need_local_specializations)
12341 {
12342 delete local_specializations;
12343 local_specializations = saved_local_specializations;
12344 }
12345
12346 /* If the dependent pack arguments were such that we end up with only a
12347 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12348 if (len == 1 && TREE_CODE (result) == TREE_VEC
12349 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12350 return TREE_VEC_ELT (result, 0);
12351
12352 return result;
12353 }
12354
12355 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12356 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12357 parameter packs; all parms generated from a function parameter pack will
12358 have the same DECL_PARM_INDEX. */
12359
12360 tree
12361 get_pattern_parm (tree parm, tree tmpl)
12362 {
12363 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12364 tree patparm;
12365
12366 if (DECL_ARTIFICIAL (parm))
12367 {
12368 for (patparm = DECL_ARGUMENTS (pattern);
12369 patparm; patparm = DECL_CHAIN (patparm))
12370 if (DECL_ARTIFICIAL (patparm)
12371 && DECL_NAME (parm) == DECL_NAME (patparm))
12372 break;
12373 }
12374 else
12375 {
12376 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12377 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12378 gcc_assert (DECL_PARM_INDEX (patparm)
12379 == DECL_PARM_INDEX (parm));
12380 }
12381
12382 return patparm;
12383 }
12384
12385 /* Make an argument pack out of the TREE_VEC VEC. */
12386
12387 static tree
12388 make_argument_pack (tree vec)
12389 {
12390 tree pack;
12391 tree elt = TREE_VEC_ELT (vec, 0);
12392 if (TYPE_P (elt))
12393 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12394 else
12395 {
12396 pack = make_node (NONTYPE_ARGUMENT_PACK);
12397 TREE_CONSTANT (pack) = 1;
12398 }
12399 SET_ARGUMENT_PACK_ARGS (pack, vec);
12400 return pack;
12401 }
12402
12403 /* Return an exact copy of template args T that can be modified
12404 independently. */
12405
12406 static tree
12407 copy_template_args (tree t)
12408 {
12409 if (t == error_mark_node)
12410 return t;
12411
12412 int len = TREE_VEC_LENGTH (t);
12413 tree new_vec = make_tree_vec (len);
12414
12415 for (int i = 0; i < len; ++i)
12416 {
12417 tree elt = TREE_VEC_ELT (t, i);
12418 if (elt && TREE_CODE (elt) == TREE_VEC)
12419 elt = copy_template_args (elt);
12420 TREE_VEC_ELT (new_vec, i) = elt;
12421 }
12422
12423 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12424 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12425
12426 return new_vec;
12427 }
12428
12429 /* Substitute ARGS into the vector or list of template arguments T. */
12430
12431 static tree
12432 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12433 {
12434 tree orig_t = t;
12435 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12436 tree *elts;
12437
12438 if (t == error_mark_node)
12439 return error_mark_node;
12440
12441 len = TREE_VEC_LENGTH (t);
12442 elts = XALLOCAVEC (tree, len);
12443
12444 for (i = 0; i < len; i++)
12445 {
12446 tree orig_arg = TREE_VEC_ELT (t, i);
12447 tree new_arg;
12448
12449 if (TREE_CODE (orig_arg) == TREE_VEC)
12450 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12451 else if (PACK_EXPANSION_P (orig_arg))
12452 {
12453 /* Substitute into an expansion expression. */
12454 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12455
12456 if (TREE_CODE (new_arg) == TREE_VEC)
12457 /* Add to the expanded length adjustment the number of
12458 expanded arguments. We subtract one from this
12459 measurement, because the argument pack expression
12460 itself is already counted as 1 in
12461 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12462 the argument pack is empty. */
12463 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12464 }
12465 else if (ARGUMENT_PACK_P (orig_arg))
12466 {
12467 /* Substitute into each of the arguments. */
12468 new_arg = TYPE_P (orig_arg)
12469 ? cxx_make_type (TREE_CODE (orig_arg))
12470 : make_node (TREE_CODE (orig_arg));
12471
12472 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12473 args, complain, in_decl);
12474 if (pack_args == error_mark_node)
12475 new_arg = error_mark_node;
12476 else
12477 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12478
12479 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12480 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12481 }
12482 else
12483 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12484
12485 if (new_arg == error_mark_node)
12486 return error_mark_node;
12487
12488 elts[i] = new_arg;
12489 if (new_arg != orig_arg)
12490 need_new = 1;
12491 }
12492
12493 if (!need_new)
12494 return t;
12495
12496 /* Make space for the expanded arguments coming from template
12497 argument packs. */
12498 t = make_tree_vec (len + expanded_len_adjust);
12499 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12500 arguments for a member template.
12501 In that case each TREE_VEC in ORIG_T represents a level of template
12502 arguments, and ORIG_T won't carry any non defaulted argument count.
12503 It will rather be the nested TREE_VECs that will carry one.
12504 In other words, ORIG_T carries a non defaulted argument count only
12505 if it doesn't contain any nested TREE_VEC. */
12506 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12507 {
12508 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12509 count += expanded_len_adjust;
12510 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12511 }
12512 for (i = 0, out = 0; i < len; i++)
12513 {
12514 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12515 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12516 && TREE_CODE (elts[i]) == TREE_VEC)
12517 {
12518 int idx;
12519
12520 /* Now expand the template argument pack "in place". */
12521 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12522 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12523 }
12524 else
12525 {
12526 TREE_VEC_ELT (t, out) = elts[i];
12527 out++;
12528 }
12529 }
12530
12531 return t;
12532 }
12533
12534 /* Substitute ARGS into one level PARMS of template parameters. */
12535
12536 static tree
12537 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12538 {
12539 if (parms == error_mark_node)
12540 return error_mark_node;
12541
12542 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12543
12544 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12545 {
12546 tree tuple = TREE_VEC_ELT (parms, i);
12547
12548 if (tuple == error_mark_node)
12549 continue;
12550
12551 TREE_VEC_ELT (new_vec, i) =
12552 tsubst_template_parm (tuple, args, complain);
12553 }
12554
12555 return new_vec;
12556 }
12557
12558 /* Return the result of substituting ARGS into the template parameters
12559 given by PARMS. If there are m levels of ARGS and m + n levels of
12560 PARMS, then the result will contain n levels of PARMS. For
12561 example, if PARMS is `template <class T> template <class U>
12562 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12563 result will be `template <int*, double, class V>'. */
12564
12565 static tree
12566 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12567 {
12568 tree r = NULL_TREE;
12569 tree* new_parms;
12570
12571 /* When substituting into a template, we must set
12572 PROCESSING_TEMPLATE_DECL as the template parameters may be
12573 dependent if they are based on one-another, and the dependency
12574 predicates are short-circuit outside of templates. */
12575 ++processing_template_decl;
12576
12577 for (new_parms = &r;
12578 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12579 new_parms = &(TREE_CHAIN (*new_parms)),
12580 parms = TREE_CHAIN (parms))
12581 {
12582 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12583 args, complain);
12584 *new_parms =
12585 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12586 - TMPL_ARGS_DEPTH (args)),
12587 new_vec, NULL_TREE);
12588 }
12589
12590 --processing_template_decl;
12591
12592 return r;
12593 }
12594
12595 /* Return the result of substituting ARGS into one template parameter
12596 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12597 parameter and which TREE_PURPOSE is the default argument of the
12598 template parameter. */
12599
12600 static tree
12601 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12602 {
12603 tree default_value, parm_decl;
12604
12605 if (args == NULL_TREE
12606 || t == NULL_TREE
12607 || t == error_mark_node)
12608 return t;
12609
12610 gcc_assert (TREE_CODE (t) == TREE_LIST);
12611
12612 default_value = TREE_PURPOSE (t);
12613 parm_decl = TREE_VALUE (t);
12614
12615 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12616 if (TREE_CODE (parm_decl) == PARM_DECL
12617 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12618 parm_decl = error_mark_node;
12619 default_value = tsubst_template_arg (default_value, args,
12620 complain, NULL_TREE);
12621
12622 return build_tree_list (default_value, parm_decl);
12623 }
12624
12625 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12626 type T. If T is not an aggregate or enumeration type, it is
12627 handled as if by tsubst. IN_DECL is as for tsubst. If
12628 ENTERING_SCOPE is nonzero, T is the context for a template which
12629 we are presently tsubst'ing. Return the substituted value. */
12630
12631 static tree
12632 tsubst_aggr_type (tree t,
12633 tree args,
12634 tsubst_flags_t complain,
12635 tree in_decl,
12636 int entering_scope)
12637 {
12638 if (t == NULL_TREE)
12639 return NULL_TREE;
12640
12641 switch (TREE_CODE (t))
12642 {
12643 case RECORD_TYPE:
12644 if (TYPE_PTRMEMFUNC_P (t))
12645 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12646
12647 /* Fall through. */
12648 case ENUMERAL_TYPE:
12649 case UNION_TYPE:
12650 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12651 {
12652 tree argvec;
12653 tree context;
12654 tree r;
12655
12656 /* In "sizeof(X<I>)" we need to evaluate "I". */
12657 cp_evaluated ev;
12658
12659 /* First, determine the context for the type we are looking
12660 up. */
12661 context = TYPE_CONTEXT (t);
12662 if (context && TYPE_P (context))
12663 {
12664 context = tsubst_aggr_type (context, args, complain,
12665 in_decl, /*entering_scope=*/1);
12666 /* If context is a nested class inside a class template,
12667 it may still need to be instantiated (c++/33959). */
12668 context = complete_type (context);
12669 }
12670
12671 /* Then, figure out what arguments are appropriate for the
12672 type we are trying to find. For example, given:
12673
12674 template <class T> struct S;
12675 template <class T, class U> void f(T, U) { S<U> su; }
12676
12677 and supposing that we are instantiating f<int, double>,
12678 then our ARGS will be {int, double}, but, when looking up
12679 S we only want {double}. */
12680 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12681 complain, in_decl);
12682 if (argvec == error_mark_node)
12683 r = error_mark_node;
12684 else
12685 {
12686 r = lookup_template_class (t, argvec, in_decl, context,
12687 entering_scope, complain);
12688 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12689 }
12690
12691 return r;
12692 }
12693 else
12694 /* This is not a template type, so there's nothing to do. */
12695 return t;
12696
12697 default:
12698 return tsubst (t, args, complain, in_decl);
12699 }
12700 }
12701
12702 static GTY((cache)) tree_cache_map *defarg_inst;
12703
12704 /* Substitute into the default argument ARG (a default argument for
12705 FN), which has the indicated TYPE. */
12706
12707 tree
12708 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12709 tsubst_flags_t complain)
12710 {
12711 int errs = errorcount + sorrycount;
12712
12713 /* This can happen in invalid code. */
12714 if (TREE_CODE (arg) == DEFAULT_ARG)
12715 return arg;
12716
12717 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12718 parm = chain_index (parmnum, parm);
12719 tree parmtype = TREE_TYPE (parm);
12720 if (DECL_BY_REFERENCE (parm))
12721 parmtype = TREE_TYPE (parmtype);
12722 if (parmtype == error_mark_node)
12723 return error_mark_node;
12724
12725 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12726
12727 tree *slot;
12728 if (defarg_inst && (slot = defarg_inst->get (parm)))
12729 return *slot;
12730
12731 /* This default argument came from a template. Instantiate the
12732 default argument here, not in tsubst. In the case of
12733 something like:
12734
12735 template <class T>
12736 struct S {
12737 static T t();
12738 void f(T = t());
12739 };
12740
12741 we must be careful to do name lookup in the scope of S<T>,
12742 rather than in the current class. */
12743 push_to_top_level ();
12744 push_access_scope (fn);
12745 start_lambda_scope (parm);
12746
12747 /* The default argument expression may cause implicitly defined
12748 member functions to be synthesized, which will result in garbage
12749 collection. We must treat this situation as if we were within
12750 the body of function so as to avoid collecting live data on the
12751 stack. */
12752 ++function_depth;
12753 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12754 complain, NULL_TREE,
12755 /*integral_constant_expression_p=*/false);
12756 --function_depth;
12757
12758 finish_lambda_scope ();
12759
12760 /* Make sure the default argument is reasonable. */
12761 arg = check_default_argument (type, arg, complain);
12762
12763 if (errorcount+sorrycount > errs
12764 && (complain & tf_warning_or_error))
12765 inform (input_location,
12766 " when instantiating default argument for call to %qD", fn);
12767
12768 pop_access_scope (fn);
12769 pop_from_top_level ();
12770
12771 if (arg != error_mark_node && !cp_unevaluated_operand)
12772 {
12773 if (!defarg_inst)
12774 defarg_inst = tree_cache_map::create_ggc (37);
12775 defarg_inst->put (parm, arg);
12776 }
12777
12778 return arg;
12779 }
12780
12781 /* Substitute into all the default arguments for FN. */
12782
12783 static void
12784 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12785 {
12786 tree arg;
12787 tree tmpl_args;
12788
12789 tmpl_args = DECL_TI_ARGS (fn);
12790
12791 /* If this function is not yet instantiated, we certainly don't need
12792 its default arguments. */
12793 if (uses_template_parms (tmpl_args))
12794 return;
12795 /* Don't do this again for clones. */
12796 if (DECL_CLONED_FUNCTION_P (fn))
12797 return;
12798
12799 int i = 0;
12800 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12801 arg;
12802 arg = TREE_CHAIN (arg), ++i)
12803 if (TREE_PURPOSE (arg))
12804 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12805 TREE_VALUE (arg),
12806 TREE_PURPOSE (arg),
12807 complain);
12808 }
12809
12810 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12811 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12812
12813 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12814
12815 void
12816 store_explicit_specifier (tree v, tree t)
12817 {
12818 if (!explicit_specifier_map)
12819 explicit_specifier_map = tree_cache_map::create_ggc (37);
12820 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12821 explicit_specifier_map->put (v, t);
12822 }
12823
12824 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12825
12826 static tree
12827 lookup_explicit_specifier (tree v)
12828 {
12829 return *explicit_specifier_map->get (v);
12830 }
12831
12832 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12833
12834 static tree
12835 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12836 tree lambda_fntype)
12837 {
12838 tree gen_tmpl, argvec;
12839 hashval_t hash = 0;
12840 tree in_decl = t;
12841
12842 /* Nobody should be tsubst'ing into non-template functions. */
12843 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12844
12845 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12846 {
12847 /* If T is not dependent, just return it. */
12848 if (!uses_template_parms (DECL_TI_ARGS (t))
12849 && !LAMBDA_FUNCTION_P (t))
12850 return t;
12851
12852 /* Calculate the most general template of which R is a
12853 specialization. */
12854 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12855
12856 /* We're substituting a lambda function under tsubst_lambda_expr but not
12857 directly from it; find the matching function we're already inside.
12858 But don't do this if T is a generic lambda with a single level of
12859 template parms, as in that case we're doing a normal instantiation. */
12860 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12861 && (!generic_lambda_fn_p (t)
12862 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12863 return enclosing_instantiation_of (t);
12864
12865 /* Calculate the complete set of arguments used to
12866 specialize R. */
12867 argvec = tsubst_template_args (DECL_TI_ARGS
12868 (DECL_TEMPLATE_RESULT
12869 (DECL_TI_TEMPLATE (t))),
12870 args, complain, in_decl);
12871 if (argvec == error_mark_node)
12872 return error_mark_node;
12873
12874 /* Check to see if we already have this specialization. */
12875 if (!lambda_fntype)
12876 {
12877 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12878 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12879 return spec;
12880 }
12881
12882 /* We can see more levels of arguments than parameters if
12883 there was a specialization of a member template, like
12884 this:
12885
12886 template <class T> struct S { template <class U> void f(); }
12887 template <> template <class U> void S<int>::f(U);
12888
12889 Here, we'll be substituting into the specialization,
12890 because that's where we can find the code we actually
12891 want to generate, but we'll have enough arguments for
12892 the most general template.
12893
12894 We also deal with the peculiar case:
12895
12896 template <class T> struct S {
12897 template <class U> friend void f();
12898 };
12899 template <class U> void f() {}
12900 template S<int>;
12901 template void f<double>();
12902
12903 Here, the ARGS for the instantiation of will be {int,
12904 double}. But, we only need as many ARGS as there are
12905 levels of template parameters in CODE_PATTERN. We are
12906 careful not to get fooled into reducing the ARGS in
12907 situations like:
12908
12909 template <class T> struct S { template <class U> void f(U); }
12910 template <class T> template <> void S<T>::f(int) {}
12911
12912 which we can spot because the pattern will be a
12913 specialization in this case. */
12914 int args_depth = TMPL_ARGS_DEPTH (args);
12915 int parms_depth =
12916 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12917
12918 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12919 args = get_innermost_template_args (args, parms_depth);
12920 }
12921 else
12922 {
12923 /* This special case arises when we have something like this:
12924
12925 template <class T> struct S {
12926 friend void f<int>(int, double);
12927 };
12928
12929 Here, the DECL_TI_TEMPLATE for the friend declaration
12930 will be an IDENTIFIER_NODE. We are being called from
12931 tsubst_friend_function, and we want only to create a
12932 new decl (R) with appropriate types so that we can call
12933 determine_specialization. */
12934 gen_tmpl = NULL_TREE;
12935 argvec = NULL_TREE;
12936 }
12937
12938 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12939 : NULL_TREE);
12940 tree ctx = closure ? closure : DECL_CONTEXT (t);
12941 bool member = ctx && TYPE_P (ctx);
12942
12943 if (member && !closure)
12944 ctx = tsubst_aggr_type (ctx, args,
12945 complain, t, /*entering_scope=*/1);
12946
12947 tree type = (lambda_fntype ? lambda_fntype
12948 : tsubst (TREE_TYPE (t), args,
12949 complain | tf_fndecl_type, in_decl));
12950 if (type == error_mark_node)
12951 return error_mark_node;
12952
12953 /* If we hit excessive deduction depth, the type is bogus even if
12954 it isn't error_mark_node, so don't build a decl. */
12955 if (excessive_deduction_depth)
12956 return error_mark_node;
12957
12958 /* We do NOT check for matching decls pushed separately at this
12959 point, as they may not represent instantiations of this
12960 template, and in any case are considered separate under the
12961 discrete model. */
12962 tree r = copy_decl (t);
12963 DECL_USE_TEMPLATE (r) = 0;
12964 TREE_TYPE (r) = type;
12965 /* Clear out the mangled name and RTL for the instantiation. */
12966 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12967 SET_DECL_RTL (r, NULL);
12968 /* Leave DECL_INITIAL set on deleted instantiations. */
12969 if (!DECL_DELETED_FN (r))
12970 DECL_INITIAL (r) = NULL_TREE;
12971 DECL_CONTEXT (r) = ctx;
12972
12973 /* Handle explicit(dependent-expr). */
12974 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
12975 {
12976 tree spec = lookup_explicit_specifier (t);
12977 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
12978 /*function_p=*/false,
12979 /*i_c_e_p=*/true);
12980 spec = build_explicit_specifier (spec, complain);
12981 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
12982 }
12983
12984 /* OpenMP UDRs have the only argument a reference to the declared
12985 type. We want to diagnose if the declared type is a reference,
12986 which is invalid, but as references to references are usually
12987 quietly merged, diagnose it here. */
12988 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12989 {
12990 tree argtype
12991 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12992 argtype = tsubst (argtype, args, complain, in_decl);
12993 if (TYPE_REF_P (argtype))
12994 error_at (DECL_SOURCE_LOCATION (t),
12995 "reference type %qT in "
12996 "%<#pragma omp declare reduction%>", argtype);
12997 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12998 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12999 argtype);
13000 }
13001
13002 if (member && DECL_CONV_FN_P (r))
13003 /* Type-conversion operator. Reconstruct the name, in
13004 case it's the name of one of the template's parameters. */
13005 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13006
13007 tree parms = DECL_ARGUMENTS (t);
13008 if (closure)
13009 parms = DECL_CHAIN (parms);
13010 parms = tsubst (parms, args, complain, t);
13011 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13012 DECL_CONTEXT (parm) = r;
13013 if (closure)
13014 {
13015 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13016 DECL_CHAIN (tparm) = parms;
13017 parms = tparm;
13018 }
13019 DECL_ARGUMENTS (r) = parms;
13020 DECL_RESULT (r) = NULL_TREE;
13021
13022 TREE_STATIC (r) = 0;
13023 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13024 DECL_EXTERNAL (r) = 1;
13025 /* If this is an instantiation of a function with internal
13026 linkage, we already know what object file linkage will be
13027 assigned to the instantiation. */
13028 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13029 DECL_DEFER_OUTPUT (r) = 0;
13030 DECL_CHAIN (r) = NULL_TREE;
13031 DECL_PENDING_INLINE_INFO (r) = 0;
13032 DECL_PENDING_INLINE_P (r) = 0;
13033 DECL_SAVED_TREE (r) = NULL_TREE;
13034 DECL_STRUCT_FUNCTION (r) = NULL;
13035 TREE_USED (r) = 0;
13036 /* We'll re-clone as appropriate in instantiate_template. */
13037 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13038
13039 /* If we aren't complaining now, return on error before we register
13040 the specialization so that we'll complain eventually. */
13041 if ((complain & tf_error) == 0
13042 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13043 && !grok_op_properties (r, /*complain=*/false))
13044 return error_mark_node;
13045
13046 /* When instantiating a constrained member, substitute
13047 into the constraints to create a new constraint. */
13048 if (tree ci = get_constraints (t))
13049 if (member)
13050 {
13051 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13052 set_constraints (r, ci);
13053 }
13054
13055 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13056 this in the special friend case mentioned above where
13057 GEN_TMPL is NULL. */
13058 if (gen_tmpl && !closure)
13059 {
13060 DECL_TEMPLATE_INFO (r)
13061 = build_template_info (gen_tmpl, argvec);
13062 SET_DECL_IMPLICIT_INSTANTIATION (r);
13063
13064 tree new_r
13065 = register_specialization (r, gen_tmpl, argvec, false, hash);
13066 if (new_r != r)
13067 /* We instantiated this while substituting into
13068 the type earlier (template/friend54.C). */
13069 return new_r;
13070
13071 /* We're not supposed to instantiate default arguments
13072 until they are called, for a template. But, for a
13073 declaration like:
13074
13075 template <class T> void f ()
13076 { extern void g(int i = T()); }
13077
13078 we should do the substitution when the template is
13079 instantiated. We handle the member function case in
13080 instantiate_class_template since the default arguments
13081 might refer to other members of the class. */
13082 if (!member
13083 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13084 && !uses_template_parms (argvec))
13085 tsubst_default_arguments (r, complain);
13086 }
13087 else
13088 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13089
13090 /* Copy the list of befriending classes. */
13091 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13092 *friends;
13093 friends = &TREE_CHAIN (*friends))
13094 {
13095 *friends = copy_node (*friends);
13096 TREE_VALUE (*friends)
13097 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13098 }
13099
13100 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13101 {
13102 maybe_retrofit_in_chrg (r);
13103 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13104 return error_mark_node;
13105 /* If this is an instantiation of a member template, clone it.
13106 If it isn't, that'll be handled by
13107 clone_constructors_and_destructors. */
13108 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13109 clone_function_decl (r, /*update_methods=*/false);
13110 }
13111 else if ((complain & tf_error) != 0
13112 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13113 && !grok_op_properties (r, /*complain=*/true))
13114 return error_mark_node;
13115
13116 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13117 SET_DECL_FRIEND_CONTEXT (r,
13118 tsubst (DECL_FRIEND_CONTEXT (t),
13119 args, complain, in_decl));
13120
13121 /* Possibly limit visibility based on template args. */
13122 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13123 if (DECL_VISIBILITY_SPECIFIED (t))
13124 {
13125 DECL_VISIBILITY_SPECIFIED (r) = 0;
13126 DECL_ATTRIBUTES (r)
13127 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13128 }
13129 determine_visibility (r);
13130 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13131 && !processing_template_decl)
13132 defaulted_late_check (r);
13133
13134 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13135 args, complain, in_decl);
13136 return r;
13137 }
13138
13139 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13140
13141 static tree
13142 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13143 tree lambda_fntype)
13144 {
13145 /* We can get here when processing a member function template,
13146 member class template, or template template parameter. */
13147 tree decl = DECL_TEMPLATE_RESULT (t);
13148 tree in_decl = t;
13149 tree spec;
13150 tree tmpl_args;
13151 tree full_args;
13152 tree r;
13153 hashval_t hash = 0;
13154
13155 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13156 {
13157 /* Template template parameter is treated here. */
13158 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13159 if (new_type == error_mark_node)
13160 r = error_mark_node;
13161 /* If we get a real template back, return it. This can happen in
13162 the context of most_specialized_partial_spec. */
13163 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13164 r = new_type;
13165 else
13166 /* The new TEMPLATE_DECL was built in
13167 reduce_template_parm_level. */
13168 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13169 return r;
13170 }
13171
13172 if (!lambda_fntype)
13173 {
13174 /* We might already have an instance of this template.
13175 The ARGS are for the surrounding class type, so the
13176 full args contain the tsubst'd args for the context,
13177 plus the innermost args from the template decl. */
13178 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13179 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13180 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13181 /* Because this is a template, the arguments will still be
13182 dependent, even after substitution. If
13183 PROCESSING_TEMPLATE_DECL is not set, the dependency
13184 predicates will short-circuit. */
13185 ++processing_template_decl;
13186 full_args = tsubst_template_args (tmpl_args, args,
13187 complain, in_decl);
13188 --processing_template_decl;
13189 if (full_args == error_mark_node)
13190 return error_mark_node;
13191
13192 /* If this is a default template template argument,
13193 tsubst might not have changed anything. */
13194 if (full_args == tmpl_args)
13195 return t;
13196
13197 hash = hash_tmpl_and_args (t, full_args);
13198 spec = retrieve_specialization (t, full_args, hash);
13199 if (spec != NULL_TREE)
13200 return spec;
13201 }
13202
13203 /* Make a new template decl. It will be similar to the
13204 original, but will record the current template arguments.
13205 We also create a new function declaration, which is just
13206 like the old one, but points to this new template, rather
13207 than the old one. */
13208 r = copy_decl (t);
13209 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13210 DECL_CHAIN (r) = NULL_TREE;
13211
13212 // Build new template info linking to the original template decl.
13213 if (!lambda_fntype)
13214 {
13215 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13216 SET_DECL_IMPLICIT_INSTANTIATION (r);
13217 }
13218 else
13219 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13220
13221 /* The template parameters for this new template are all the
13222 template parameters for the old template, except the
13223 outermost level of parameters. */
13224 DECL_TEMPLATE_PARMS (r)
13225 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13226 complain);
13227
13228 if (TREE_CODE (decl) == TYPE_DECL
13229 && !TYPE_DECL_ALIAS_P (decl))
13230 {
13231 tree new_type;
13232 ++processing_template_decl;
13233 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13234 --processing_template_decl;
13235 if (new_type == error_mark_node)
13236 return error_mark_node;
13237
13238 TREE_TYPE (r) = new_type;
13239 /* For a partial specialization, we need to keep pointing to
13240 the primary template. */
13241 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13242 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13243 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13244 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13245 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13246 }
13247 else
13248 {
13249 tree new_decl;
13250 ++processing_template_decl;
13251 if (TREE_CODE (decl) == FUNCTION_DECL)
13252 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13253 else
13254 new_decl = tsubst (decl, args, complain, in_decl);
13255 --processing_template_decl;
13256 if (new_decl == error_mark_node)
13257 return error_mark_node;
13258
13259 DECL_TEMPLATE_RESULT (r) = new_decl;
13260 TREE_TYPE (r) = TREE_TYPE (new_decl);
13261 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13262 if (lambda_fntype)
13263 {
13264 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13265 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13266 }
13267 else
13268 {
13269 DECL_TI_TEMPLATE (new_decl) = r;
13270 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13271 }
13272 }
13273
13274 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13275 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13276
13277 if (PRIMARY_TEMPLATE_P (t))
13278 DECL_PRIMARY_TEMPLATE (r) = r;
13279
13280 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13281 && !lambda_fntype)
13282 /* Record this non-type partial instantiation. */
13283 register_specialization (r, t,
13284 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13285 false, hash);
13286
13287 return r;
13288 }
13289
13290 /* True if FN is the op() for a lambda in an uninstantiated template. */
13291
13292 bool
13293 lambda_fn_in_template_p (tree fn)
13294 {
13295 if (!fn || !LAMBDA_FUNCTION_P (fn))
13296 return false;
13297 tree closure = DECL_CONTEXT (fn);
13298 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13299 }
13300
13301 /* We're instantiating a variable from template function TCTX. Return the
13302 corresponding current enclosing scope. This gets complicated because lambda
13303 functions in templates are regenerated rather than instantiated, but generic
13304 lambda functions are subsequently instantiated. */
13305
13306 static tree
13307 enclosing_instantiation_of (tree otctx)
13308 {
13309 tree tctx = otctx;
13310 tree fn = current_function_decl;
13311 int lambda_count = 0;
13312
13313 for (; tctx && lambda_fn_in_template_p (tctx);
13314 tctx = decl_function_context (tctx))
13315 ++lambda_count;
13316 for (; fn; fn = decl_function_context (fn))
13317 {
13318 tree ofn = fn;
13319 int flambda_count = 0;
13320 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
13321 fn = decl_function_context (fn))
13322 ++flambda_count;
13323 if ((fn && DECL_TEMPLATE_INFO (fn))
13324 ? most_general_template (fn) != most_general_template (tctx)
13325 : fn != tctx)
13326 continue;
13327 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13328 || DECL_CONV_FN_P (ofn));
13329 return ofn;
13330 }
13331 gcc_unreachable ();
13332 }
13333
13334 /* Substitute the ARGS into the T, which is a _DECL. Return the
13335 result of the substitution. Issue error and warning messages under
13336 control of COMPLAIN. */
13337
13338 static tree
13339 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13340 {
13341 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13342 location_t saved_loc;
13343 tree r = NULL_TREE;
13344 tree in_decl = t;
13345 hashval_t hash = 0;
13346
13347 /* Set the filename and linenumber to improve error-reporting. */
13348 saved_loc = input_location;
13349 input_location = DECL_SOURCE_LOCATION (t);
13350
13351 switch (TREE_CODE (t))
13352 {
13353 case TEMPLATE_DECL:
13354 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13355 break;
13356
13357 case FUNCTION_DECL:
13358 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13359 break;
13360
13361 case PARM_DECL:
13362 {
13363 tree type = NULL_TREE;
13364 int i, len = 1;
13365 tree expanded_types = NULL_TREE;
13366 tree prev_r = NULL_TREE;
13367 tree first_r = NULL_TREE;
13368
13369 if (DECL_PACK_P (t))
13370 {
13371 /* If there is a local specialization that isn't a
13372 parameter pack, it means that we're doing a "simple"
13373 substitution from inside tsubst_pack_expansion. Just
13374 return the local specialization (which will be a single
13375 parm). */
13376 tree spec = retrieve_local_specialization (t);
13377 if (spec
13378 && TREE_CODE (spec) == PARM_DECL
13379 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13380 RETURN (spec);
13381
13382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13383 the parameters in this function parameter pack. */
13384 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13385 complain, in_decl);
13386 if (TREE_CODE (expanded_types) == TREE_VEC)
13387 {
13388 len = TREE_VEC_LENGTH (expanded_types);
13389
13390 /* Zero-length parameter packs are boring. Just substitute
13391 into the chain. */
13392 if (len == 0)
13393 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13394 TREE_CHAIN (t)));
13395 }
13396 else
13397 {
13398 /* All we did was update the type. Make a note of that. */
13399 type = expanded_types;
13400 expanded_types = NULL_TREE;
13401 }
13402 }
13403
13404 /* Loop through all of the parameters we'll build. When T is
13405 a function parameter pack, LEN is the number of expanded
13406 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13407 r = NULL_TREE;
13408 for (i = 0; i < len; ++i)
13409 {
13410 prev_r = r;
13411 r = copy_node (t);
13412 if (DECL_TEMPLATE_PARM_P (t))
13413 SET_DECL_TEMPLATE_PARM_P (r);
13414
13415 if (expanded_types)
13416 /* We're on the Ith parameter of the function parameter
13417 pack. */
13418 {
13419 /* Get the Ith type. */
13420 type = TREE_VEC_ELT (expanded_types, i);
13421
13422 /* Rename the parameter to include the index. */
13423 DECL_NAME (r)
13424 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13425 }
13426 else if (!type)
13427 /* We're dealing with a normal parameter. */
13428 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13429
13430 type = type_decays_to (type);
13431 TREE_TYPE (r) = type;
13432 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13433
13434 if (DECL_INITIAL (r))
13435 {
13436 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13437 DECL_INITIAL (r) = TREE_TYPE (r);
13438 else
13439 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13440 complain, in_decl);
13441 }
13442
13443 DECL_CONTEXT (r) = NULL_TREE;
13444
13445 if (!DECL_TEMPLATE_PARM_P (r))
13446 DECL_ARG_TYPE (r) = type_passed_as (type);
13447
13448 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13449 args, complain, in_decl);
13450
13451 /* Keep track of the first new parameter we
13452 generate. That's what will be returned to the
13453 caller. */
13454 if (!first_r)
13455 first_r = r;
13456
13457 /* Build a proper chain of parameters when substituting
13458 into a function parameter pack. */
13459 if (prev_r)
13460 DECL_CHAIN (prev_r) = r;
13461 }
13462
13463 /* If cp_unevaluated_operand is set, we're just looking for a
13464 single dummy parameter, so don't keep going. */
13465 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13466 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13467 complain, DECL_CHAIN (t));
13468
13469 /* FIRST_R contains the start of the chain we've built. */
13470 r = first_r;
13471 }
13472 break;
13473
13474 case FIELD_DECL:
13475 {
13476 tree type = NULL_TREE;
13477 tree vec = NULL_TREE;
13478 tree expanded_types = NULL_TREE;
13479 int len = 1;
13480
13481 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13482 {
13483 /* This field is a lambda capture pack. Return a TREE_VEC of
13484 the expanded fields to instantiate_class_template_1. */
13485 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13486 complain, in_decl);
13487 if (TREE_CODE (expanded_types) == TREE_VEC)
13488 {
13489 len = TREE_VEC_LENGTH (expanded_types);
13490 vec = make_tree_vec (len);
13491 }
13492 else
13493 {
13494 /* All we did was update the type. Make a note of that. */
13495 type = expanded_types;
13496 expanded_types = NULL_TREE;
13497 }
13498 }
13499
13500 for (int i = 0; i < len; ++i)
13501 {
13502 r = copy_decl (t);
13503 if (expanded_types)
13504 {
13505 type = TREE_VEC_ELT (expanded_types, i);
13506 DECL_NAME (r)
13507 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13508 }
13509 else if (!type)
13510 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13511
13512 if (type == error_mark_node)
13513 RETURN (error_mark_node);
13514 TREE_TYPE (r) = type;
13515 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13516
13517 if (DECL_C_BIT_FIELD (r))
13518 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13519 number of bits. */
13520 DECL_BIT_FIELD_REPRESENTATIVE (r)
13521 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13522 complain, in_decl,
13523 /*integral_constant_expression_p=*/true);
13524 if (DECL_INITIAL (t))
13525 {
13526 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13527 NSDMI in perform_member_init. Still set DECL_INITIAL
13528 so that we know there is one. */
13529 DECL_INITIAL (r) = void_node;
13530 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13531 retrofit_lang_decl (r);
13532 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13533 }
13534 /* We don't have to set DECL_CONTEXT here; it is set by
13535 finish_member_declaration. */
13536 DECL_CHAIN (r) = NULL_TREE;
13537
13538 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13539 args, complain, in_decl);
13540
13541 if (vec)
13542 TREE_VEC_ELT (vec, i) = r;
13543 }
13544
13545 if (vec)
13546 r = vec;
13547 }
13548 break;
13549
13550 case USING_DECL:
13551 /* We reach here only for member using decls. We also need to check
13552 uses_template_parms because DECL_DEPENDENT_P is not set for a
13553 using-declaration that designates a member of the current
13554 instantiation (c++/53549). */
13555 if (DECL_DEPENDENT_P (t)
13556 || uses_template_parms (USING_DECL_SCOPE (t)))
13557 {
13558 tree scope = USING_DECL_SCOPE (t);
13559 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13560 if (PACK_EXPANSION_P (scope))
13561 {
13562 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13563 int len = TREE_VEC_LENGTH (vec);
13564 r = make_tree_vec (len);
13565 for (int i = 0; i < len; ++i)
13566 {
13567 tree escope = TREE_VEC_ELT (vec, i);
13568 tree elt = do_class_using_decl (escope, name);
13569 if (!elt)
13570 {
13571 r = error_mark_node;
13572 break;
13573 }
13574 else
13575 {
13576 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13577 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13578 }
13579 TREE_VEC_ELT (r, i) = elt;
13580 }
13581 }
13582 else
13583 {
13584 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13585 complain, in_decl);
13586 r = do_class_using_decl (inst_scope, name);
13587 if (!r)
13588 r = error_mark_node;
13589 else
13590 {
13591 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13592 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13593 }
13594 }
13595 }
13596 else
13597 {
13598 r = copy_node (t);
13599 DECL_CHAIN (r) = NULL_TREE;
13600 }
13601 break;
13602
13603 case TYPE_DECL:
13604 case VAR_DECL:
13605 {
13606 tree argvec = NULL_TREE;
13607 tree gen_tmpl = NULL_TREE;
13608 tree spec;
13609 tree tmpl = NULL_TREE;
13610 tree ctx;
13611 tree type = NULL_TREE;
13612 bool local_p;
13613
13614 if (TREE_TYPE (t) == error_mark_node)
13615 RETURN (error_mark_node);
13616
13617 if (TREE_CODE (t) == TYPE_DECL
13618 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13619 {
13620 /* If this is the canonical decl, we don't have to
13621 mess with instantiations, and often we can't (for
13622 typename, template type parms and such). Note that
13623 TYPE_NAME is not correct for the above test if
13624 we've copied the type for a typedef. */
13625 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13626 if (type == error_mark_node)
13627 RETURN (error_mark_node);
13628 r = TYPE_NAME (type);
13629 break;
13630 }
13631
13632 /* Check to see if we already have the specialization we
13633 need. */
13634 spec = NULL_TREE;
13635 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13636 {
13637 /* T is a static data member or namespace-scope entity.
13638 We have to substitute into namespace-scope variables
13639 (not just variable templates) because of cases like:
13640
13641 template <class T> void f() { extern T t; }
13642
13643 where the entity referenced is not known until
13644 instantiation time. */
13645 local_p = false;
13646 ctx = DECL_CONTEXT (t);
13647 if (DECL_CLASS_SCOPE_P (t))
13648 {
13649 ctx = tsubst_aggr_type (ctx, args,
13650 complain,
13651 in_decl, /*entering_scope=*/1);
13652 /* If CTX is unchanged, then T is in fact the
13653 specialization we want. That situation occurs when
13654 referencing a static data member within in its own
13655 class. We can use pointer equality, rather than
13656 same_type_p, because DECL_CONTEXT is always
13657 canonical... */
13658 if (ctx == DECL_CONTEXT (t)
13659 /* ... unless T is a member template; in which
13660 case our caller can be willing to create a
13661 specialization of that template represented
13662 by T. */
13663 && !(DECL_TI_TEMPLATE (t)
13664 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13665 spec = t;
13666 }
13667
13668 if (!spec)
13669 {
13670 tmpl = DECL_TI_TEMPLATE (t);
13671 gen_tmpl = most_general_template (tmpl);
13672 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13673 if (argvec != error_mark_node)
13674 argvec = (coerce_innermost_template_parms
13675 (DECL_TEMPLATE_PARMS (gen_tmpl),
13676 argvec, t, complain,
13677 /*all*/true, /*defarg*/true));
13678 if (argvec == error_mark_node)
13679 RETURN (error_mark_node);
13680 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13681 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13682 }
13683 }
13684 else
13685 {
13686 /* A local variable. */
13687 local_p = true;
13688 /* Subsequent calls to pushdecl will fill this in. */
13689 ctx = NULL_TREE;
13690 /* Unless this is a reference to a static variable from an
13691 enclosing function, in which case we need to fill it in now. */
13692 if (TREE_STATIC (t))
13693 {
13694 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13695 if (fn != current_function_decl)
13696 ctx = fn;
13697 }
13698 spec = retrieve_local_specialization (t);
13699 }
13700 /* If we already have the specialization we need, there is
13701 nothing more to do. */
13702 if (spec)
13703 {
13704 r = spec;
13705 break;
13706 }
13707
13708 /* Create a new node for the specialization we need. */
13709 if (type == NULL_TREE)
13710 {
13711 if (is_typedef_decl (t))
13712 type = DECL_ORIGINAL_TYPE (t);
13713 else
13714 type = TREE_TYPE (t);
13715 if (VAR_P (t)
13716 && VAR_HAD_UNKNOWN_BOUND (t)
13717 && type != error_mark_node)
13718 type = strip_array_domain (type);
13719 tree sub_args = args;
13720 if (tree auto_node = type_uses_auto (type))
13721 {
13722 /* Mask off any template args past the variable's context so we
13723 don't replace the auto with an unrelated argument. */
13724 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13725 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13726 if (extra > 0)
13727 /* This should never happen with the new lambda instantiation
13728 model, but keep the handling just in case. */
13729 gcc_assert (!CHECKING_P),
13730 sub_args = strip_innermost_template_args (args, extra);
13731 }
13732 type = tsubst (type, sub_args, complain, in_decl);
13733 /* Substituting the type might have recursively instantiated this
13734 same alias (c++/86171). */
13735 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13736 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13737 {
13738 r = spec;
13739 break;
13740 }
13741 }
13742 r = copy_decl (t);
13743 if (VAR_P (r))
13744 {
13745 DECL_INITIALIZED_P (r) = 0;
13746 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13747 if (type == error_mark_node)
13748 RETURN (error_mark_node);
13749 if (TREE_CODE (type) == FUNCTION_TYPE)
13750 {
13751 /* It may seem that this case cannot occur, since:
13752
13753 typedef void f();
13754 void g() { f x; }
13755
13756 declares a function, not a variable. However:
13757
13758 typedef void f();
13759 template <typename T> void g() { T t; }
13760 template void g<f>();
13761
13762 is an attempt to declare a variable with function
13763 type. */
13764 error ("variable %qD has function type",
13765 /* R is not yet sufficiently initialized, so we
13766 just use its name. */
13767 DECL_NAME (r));
13768 RETURN (error_mark_node);
13769 }
13770 type = complete_type (type);
13771 /* Wait until cp_finish_decl to set this again, to handle
13772 circular dependency (template/instantiate6.C). */
13773 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13774 type = check_var_type (DECL_NAME (r), type);
13775
13776 if (DECL_HAS_VALUE_EXPR_P (t))
13777 {
13778 tree ve = DECL_VALUE_EXPR (t);
13779 ve = tsubst_expr (ve, args, complain, in_decl,
13780 /*constant_expression_p=*/false);
13781 if (REFERENCE_REF_P (ve))
13782 {
13783 gcc_assert (TYPE_REF_P (type));
13784 ve = TREE_OPERAND (ve, 0);
13785 }
13786 SET_DECL_VALUE_EXPR (r, ve);
13787 }
13788 if (CP_DECL_THREAD_LOCAL_P (r)
13789 && !processing_template_decl)
13790 set_decl_tls_model (r, decl_default_tls_model (r));
13791 }
13792 else if (DECL_SELF_REFERENCE_P (t))
13793 SET_DECL_SELF_REFERENCE_P (r);
13794 TREE_TYPE (r) = type;
13795 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13796 DECL_CONTEXT (r) = ctx;
13797 /* Clear out the mangled name and RTL for the instantiation. */
13798 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13799 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13800 SET_DECL_RTL (r, NULL);
13801 /* The initializer must not be expanded until it is required;
13802 see [temp.inst]. */
13803 DECL_INITIAL (r) = NULL_TREE;
13804 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13805 if (VAR_P (r))
13806 {
13807 if (DECL_LANG_SPECIFIC (r))
13808 SET_DECL_DEPENDENT_INIT_P (r, false);
13809
13810 SET_DECL_MODE (r, VOIDmode);
13811
13812 /* Possibly limit visibility based on template args. */
13813 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13814 if (DECL_VISIBILITY_SPECIFIED (t))
13815 {
13816 DECL_VISIBILITY_SPECIFIED (r) = 0;
13817 DECL_ATTRIBUTES (r)
13818 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13819 }
13820 determine_visibility (r);
13821 }
13822
13823 if (!local_p)
13824 {
13825 /* A static data member declaration is always marked
13826 external when it is declared in-class, even if an
13827 initializer is present. We mimic the non-template
13828 processing here. */
13829 DECL_EXTERNAL (r) = 1;
13830 if (DECL_NAMESPACE_SCOPE_P (t))
13831 DECL_NOT_REALLY_EXTERN (r) = 1;
13832
13833 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13834 SET_DECL_IMPLICIT_INSTANTIATION (r);
13835 register_specialization (r, gen_tmpl, argvec, false, hash);
13836 }
13837 else
13838 {
13839 if (DECL_LANG_SPECIFIC (r))
13840 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13841 if (!cp_unevaluated_operand)
13842 register_local_specialization (r, t);
13843 }
13844
13845 DECL_CHAIN (r) = NULL_TREE;
13846
13847 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13848 /*flags=*/0,
13849 args, complain, in_decl);
13850
13851 /* Preserve a typedef that names a type. */
13852 if (is_typedef_decl (r) && type != error_mark_node)
13853 {
13854 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13855 set_underlying_type (r);
13856 if (TYPE_DECL_ALIAS_P (r))
13857 /* An alias template specialization can be dependent
13858 even if its underlying type is not. */
13859 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13860 }
13861
13862 layout_decl (r, 0);
13863 }
13864 break;
13865
13866 default:
13867 gcc_unreachable ();
13868 }
13869 #undef RETURN
13870
13871 out:
13872 /* Restore the file and line information. */
13873 input_location = saved_loc;
13874
13875 return r;
13876 }
13877
13878 /* Substitute into the ARG_TYPES of a function type.
13879 If END is a TREE_CHAIN, leave it and any following types
13880 un-substituted. */
13881
13882 static tree
13883 tsubst_arg_types (tree arg_types,
13884 tree args,
13885 tree end,
13886 tsubst_flags_t complain,
13887 tree in_decl)
13888 {
13889 tree remaining_arg_types;
13890 tree type = NULL_TREE;
13891 int i = 1;
13892 tree expanded_args = NULL_TREE;
13893 tree default_arg;
13894
13895 if (!arg_types || arg_types == void_list_node || arg_types == end)
13896 return arg_types;
13897
13898 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13899 args, end, complain, in_decl);
13900 if (remaining_arg_types == error_mark_node)
13901 return error_mark_node;
13902
13903 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13904 {
13905 /* For a pack expansion, perform substitution on the
13906 entire expression. Later on, we'll handle the arguments
13907 one-by-one. */
13908 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13909 args, complain, in_decl);
13910
13911 if (TREE_CODE (expanded_args) == TREE_VEC)
13912 /* So that we'll spin through the parameters, one by one. */
13913 i = TREE_VEC_LENGTH (expanded_args);
13914 else
13915 {
13916 /* We only partially substituted into the parameter
13917 pack. Our type is TYPE_PACK_EXPANSION. */
13918 type = expanded_args;
13919 expanded_args = NULL_TREE;
13920 }
13921 }
13922
13923 while (i > 0) {
13924 --i;
13925
13926 if (expanded_args)
13927 type = TREE_VEC_ELT (expanded_args, i);
13928 else if (!type)
13929 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13930
13931 if (type == error_mark_node)
13932 return error_mark_node;
13933 if (VOID_TYPE_P (type))
13934 {
13935 if (complain & tf_error)
13936 {
13937 error ("invalid parameter type %qT", type);
13938 if (in_decl)
13939 error ("in declaration %q+D", in_decl);
13940 }
13941 return error_mark_node;
13942 }
13943 /* DR 657. */
13944 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13945 return error_mark_node;
13946
13947 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13948 top-level qualifiers as required. */
13949 type = cv_unqualified (type_decays_to (type));
13950
13951 /* We do not substitute into default arguments here. The standard
13952 mandates that they be instantiated only when needed, which is
13953 done in build_over_call. */
13954 default_arg = TREE_PURPOSE (arg_types);
13955
13956 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13957 since the new op() won't have any associated template arguments for us
13958 to refer to later. */
13959 if (lambda_fn_in_template_p (in_decl))
13960 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13961 false/*fn*/, false/*constexpr*/);
13962
13963 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13964 {
13965 /* We've instantiated a template before its default arguments
13966 have been parsed. This can happen for a nested template
13967 class, and is not an error unless we require the default
13968 argument in a call of this function. */
13969 remaining_arg_types =
13970 tree_cons (default_arg, type, remaining_arg_types);
13971 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13972 }
13973 else
13974 remaining_arg_types =
13975 hash_tree_cons (default_arg, type, remaining_arg_types);
13976 }
13977
13978 return remaining_arg_types;
13979 }
13980
13981 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13982 *not* handle the exception-specification for FNTYPE, because the
13983 initial substitution of explicitly provided template parameters
13984 during argument deduction forbids substitution into the
13985 exception-specification:
13986
13987 [temp.deduct]
13988
13989 All references in the function type of the function template to the
13990 corresponding template parameters are replaced by the specified tem-
13991 plate argument values. If a substitution in a template parameter or
13992 in the function type of the function template results in an invalid
13993 type, type deduction fails. [Note: The equivalent substitution in
13994 exception specifications is done only when the function is instanti-
13995 ated, at which point a program is ill-formed if the substitution
13996 results in an invalid type.] */
13997
13998 static tree
13999 tsubst_function_type (tree t,
14000 tree args,
14001 tsubst_flags_t complain,
14002 tree in_decl)
14003 {
14004 tree return_type;
14005 tree arg_types = NULL_TREE;
14006 tree fntype;
14007
14008 /* The TYPE_CONTEXT is not used for function/method types. */
14009 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14010
14011 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14012 failure. */
14013 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14014
14015 if (late_return_type_p)
14016 {
14017 /* Substitute the argument types. */
14018 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14019 complain, in_decl);
14020 if (arg_types == error_mark_node)
14021 return error_mark_node;
14022
14023 tree save_ccp = current_class_ptr;
14024 tree save_ccr = current_class_ref;
14025 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14026 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14027 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14028 if (do_inject)
14029 {
14030 /* DR 1207: 'this' is in scope in the trailing return type. */
14031 inject_this_parameter (this_type, cp_type_quals (this_type));
14032 }
14033
14034 /* Substitute the return type. */
14035 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14036
14037 if (do_inject)
14038 {
14039 current_class_ptr = save_ccp;
14040 current_class_ref = save_ccr;
14041 }
14042 }
14043 else
14044 /* Substitute the return type. */
14045 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14046
14047 if (return_type == error_mark_node)
14048 return error_mark_node;
14049 /* DR 486 clarifies that creation of a function type with an
14050 invalid return type is a deduction failure. */
14051 if (TREE_CODE (return_type) == ARRAY_TYPE
14052 || TREE_CODE (return_type) == FUNCTION_TYPE)
14053 {
14054 if (complain & tf_error)
14055 {
14056 if (TREE_CODE (return_type) == ARRAY_TYPE)
14057 error ("function returning an array");
14058 else
14059 error ("function returning a function");
14060 }
14061 return error_mark_node;
14062 }
14063 /* And DR 657. */
14064 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14065 return error_mark_node;
14066
14067 if (!late_return_type_p)
14068 {
14069 /* Substitute the argument types. */
14070 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14071 complain, in_decl);
14072 if (arg_types == error_mark_node)
14073 return error_mark_node;
14074 }
14075
14076 /* Construct a new type node and return it. */
14077 if (TREE_CODE (t) == FUNCTION_TYPE)
14078 {
14079 fntype = build_function_type (return_type, arg_types);
14080 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14081 }
14082 else
14083 {
14084 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14085 /* Don't pick up extra function qualifiers from the basetype. */
14086 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14087 if (! MAYBE_CLASS_TYPE_P (r))
14088 {
14089 /* [temp.deduct]
14090
14091 Type deduction may fail for any of the following
14092 reasons:
14093
14094 -- Attempting to create "pointer to member of T" when T
14095 is not a class type. */
14096 if (complain & tf_error)
14097 error ("creating pointer to member function of non-class type %qT",
14098 r);
14099 return error_mark_node;
14100 }
14101
14102 fntype = build_method_type_directly (r, return_type,
14103 TREE_CHAIN (arg_types));
14104 }
14105 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14106
14107 /* See comment above. */
14108 tree raises = NULL_TREE;
14109 cp_ref_qualifier rqual = type_memfn_rqual (t);
14110 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14111
14112 return fntype;
14113 }
14114
14115 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14116 ARGS into that specification, and return the substituted
14117 specification. If there is no specification, return NULL_TREE. */
14118
14119 static tree
14120 tsubst_exception_specification (tree fntype,
14121 tree args,
14122 tsubst_flags_t complain,
14123 tree in_decl,
14124 bool defer_ok)
14125 {
14126 tree specs;
14127 tree new_specs;
14128
14129 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14130 new_specs = NULL_TREE;
14131 if (specs && TREE_PURPOSE (specs))
14132 {
14133 /* A noexcept-specifier. */
14134 tree expr = TREE_PURPOSE (specs);
14135 if (TREE_CODE (expr) == INTEGER_CST)
14136 new_specs = expr;
14137 else if (defer_ok)
14138 {
14139 /* Defer instantiation of noexcept-specifiers to avoid
14140 excessive instantiations (c++/49107). */
14141 new_specs = make_node (DEFERRED_NOEXCEPT);
14142 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14143 {
14144 /* We already partially instantiated this member template,
14145 so combine the new args with the old. */
14146 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14147 = DEFERRED_NOEXCEPT_PATTERN (expr);
14148 DEFERRED_NOEXCEPT_ARGS (new_specs)
14149 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14150 }
14151 else
14152 {
14153 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14154 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14155 }
14156 }
14157 else
14158 {
14159 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14160 {
14161 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14162 args);
14163 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14164 }
14165 new_specs = tsubst_copy_and_build
14166 (expr, args, complain, in_decl, /*function_p=*/false,
14167 /*integral_constant_expression_p=*/true);
14168 }
14169 new_specs = build_noexcept_spec (new_specs, complain);
14170 }
14171 else if (specs)
14172 {
14173 if (! TREE_VALUE (specs))
14174 new_specs = specs;
14175 else
14176 while (specs)
14177 {
14178 tree spec;
14179 int i, len = 1;
14180 tree expanded_specs = NULL_TREE;
14181
14182 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14183 {
14184 /* Expand the pack expansion type. */
14185 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14186 args, complain,
14187 in_decl);
14188
14189 if (expanded_specs == error_mark_node)
14190 return error_mark_node;
14191 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14192 len = TREE_VEC_LENGTH (expanded_specs);
14193 else
14194 {
14195 /* We're substituting into a member template, so
14196 we got a TYPE_PACK_EXPANSION back. Add that
14197 expansion and move on. */
14198 gcc_assert (TREE_CODE (expanded_specs)
14199 == TYPE_PACK_EXPANSION);
14200 new_specs = add_exception_specifier (new_specs,
14201 expanded_specs,
14202 complain);
14203 specs = TREE_CHAIN (specs);
14204 continue;
14205 }
14206 }
14207
14208 for (i = 0; i < len; ++i)
14209 {
14210 if (expanded_specs)
14211 spec = TREE_VEC_ELT (expanded_specs, i);
14212 else
14213 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14214 if (spec == error_mark_node)
14215 return spec;
14216 new_specs = add_exception_specifier (new_specs, spec,
14217 complain);
14218 }
14219
14220 specs = TREE_CHAIN (specs);
14221 }
14222 }
14223 return new_specs;
14224 }
14225
14226 /* Take the tree structure T and replace template parameters used
14227 therein with the argument vector ARGS. IN_DECL is an associated
14228 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14229 Issue error and warning messages under control of COMPLAIN. Note
14230 that we must be relatively non-tolerant of extensions here, in
14231 order to preserve conformance; if we allow substitutions that
14232 should not be allowed, we may allow argument deductions that should
14233 not succeed, and therefore report ambiguous overload situations
14234 where there are none. In theory, we could allow the substitution,
14235 but indicate that it should have failed, and allow our caller to
14236 make sure that the right thing happens, but we don't try to do this
14237 yet.
14238
14239 This function is used for dealing with types, decls and the like;
14240 for expressions, use tsubst_expr or tsubst_copy. */
14241
14242 tree
14243 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14244 {
14245 enum tree_code code;
14246 tree type, r = NULL_TREE;
14247
14248 if (t == NULL_TREE || t == error_mark_node
14249 || t == integer_type_node
14250 || t == void_type_node
14251 || t == char_type_node
14252 || t == unknown_type_node
14253 || TREE_CODE (t) == NAMESPACE_DECL
14254 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14255 return t;
14256
14257 if (DECL_P (t))
14258 return tsubst_decl (t, args, complain);
14259
14260 if (args == NULL_TREE)
14261 return t;
14262
14263 code = TREE_CODE (t);
14264
14265 if (code == IDENTIFIER_NODE)
14266 type = IDENTIFIER_TYPE_VALUE (t);
14267 else
14268 type = TREE_TYPE (t);
14269
14270 gcc_assert (type != unknown_type_node);
14271
14272 /* Reuse typedefs. We need to do this to handle dependent attributes,
14273 such as attribute aligned. */
14274 if (TYPE_P (t)
14275 && typedef_variant_p (t))
14276 {
14277 tree decl = TYPE_NAME (t);
14278
14279 if (alias_template_specialization_p (t))
14280 {
14281 /* DECL represents an alias template and we want to
14282 instantiate it. */
14283 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14284 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14285 r = instantiate_alias_template (tmpl, gen_args, complain);
14286 }
14287 else if (DECL_CLASS_SCOPE_P (decl)
14288 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14289 && uses_template_parms (DECL_CONTEXT (decl)))
14290 {
14291 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14292 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14293 r = retrieve_specialization (tmpl, gen_args, 0);
14294 }
14295 else if (DECL_FUNCTION_SCOPE_P (decl)
14296 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14297 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14298 r = retrieve_local_specialization (decl);
14299 else
14300 /* The typedef is from a non-template context. */
14301 return t;
14302
14303 if (r)
14304 {
14305 r = TREE_TYPE (r);
14306 r = cp_build_qualified_type_real
14307 (r, cp_type_quals (t) | cp_type_quals (r),
14308 complain | tf_ignore_bad_quals);
14309 return r;
14310 }
14311 else
14312 {
14313 /* We don't have an instantiation yet, so drop the typedef. */
14314 int quals = cp_type_quals (t);
14315 t = DECL_ORIGINAL_TYPE (decl);
14316 t = cp_build_qualified_type_real (t, quals,
14317 complain | tf_ignore_bad_quals);
14318 }
14319 }
14320
14321 bool fndecl_type = (complain & tf_fndecl_type);
14322 complain &= ~tf_fndecl_type;
14323
14324 if (type
14325 && code != TYPENAME_TYPE
14326 && code != TEMPLATE_TYPE_PARM
14327 && code != TEMPLATE_PARM_INDEX
14328 && code != IDENTIFIER_NODE
14329 && code != FUNCTION_TYPE
14330 && code != METHOD_TYPE)
14331 type = tsubst (type, args, complain, in_decl);
14332 if (type == error_mark_node)
14333 return error_mark_node;
14334
14335 switch (code)
14336 {
14337 case RECORD_TYPE:
14338 case UNION_TYPE:
14339 case ENUMERAL_TYPE:
14340 return tsubst_aggr_type (t, args, complain, in_decl,
14341 /*entering_scope=*/0);
14342
14343 case ERROR_MARK:
14344 case IDENTIFIER_NODE:
14345 case VOID_TYPE:
14346 case REAL_TYPE:
14347 case COMPLEX_TYPE:
14348 case VECTOR_TYPE:
14349 case BOOLEAN_TYPE:
14350 case NULLPTR_TYPE:
14351 case LANG_TYPE:
14352 return t;
14353
14354 case INTEGER_TYPE:
14355 if (t == integer_type_node)
14356 return t;
14357
14358 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14359 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14360 return t;
14361
14362 {
14363 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14364
14365 max = tsubst_expr (omax, args, complain, in_decl,
14366 /*integral_constant_expression_p=*/false);
14367
14368 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14369 needed. */
14370 if (TREE_CODE (max) == NOP_EXPR
14371 && TREE_SIDE_EFFECTS (omax)
14372 && !TREE_TYPE (max))
14373 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14374
14375 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14376 with TREE_SIDE_EFFECTS that indicates this is not an integral
14377 constant expression. */
14378 if (processing_template_decl
14379 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14380 {
14381 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14382 TREE_SIDE_EFFECTS (max) = 1;
14383 }
14384
14385 return compute_array_index_type (NULL_TREE, max, complain);
14386 }
14387
14388 case TEMPLATE_TYPE_PARM:
14389 case TEMPLATE_TEMPLATE_PARM:
14390 case BOUND_TEMPLATE_TEMPLATE_PARM:
14391 case TEMPLATE_PARM_INDEX:
14392 {
14393 int idx;
14394 int level;
14395 int levels;
14396 tree arg = NULL_TREE;
14397
14398 /* Early in template argument deduction substitution, we don't
14399 want to reduce the level of 'auto', or it will be confused
14400 with a normal template parm in subsequent deduction. */
14401 if (is_auto (t) && (complain & tf_partial))
14402 return t;
14403
14404 r = NULL_TREE;
14405
14406 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14407 template_parm_level_and_index (t, &level, &idx);
14408
14409 levels = TMPL_ARGS_DEPTH (args);
14410 if (level <= levels
14411 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14412 {
14413 arg = TMPL_ARG (args, level, idx);
14414
14415 /* See through ARGUMENT_PACK_SELECT arguments. */
14416 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14417 arg = argument_pack_select_arg (arg);
14418 }
14419
14420 if (arg == error_mark_node)
14421 return error_mark_node;
14422 else if (arg != NULL_TREE)
14423 {
14424 if (ARGUMENT_PACK_P (arg))
14425 /* If ARG is an argument pack, we don't actually want to
14426 perform a substitution here, because substitutions
14427 for argument packs are only done
14428 element-by-element. We can get to this point when
14429 substituting the type of a non-type template
14430 parameter pack, when that type actually contains
14431 template parameter packs from an outer template, e.g.,
14432
14433 template<typename... Types> struct A {
14434 template<Types... Values> struct B { };
14435 }; */
14436 return t;
14437
14438 if (code == TEMPLATE_TYPE_PARM)
14439 {
14440 int quals;
14441 gcc_assert (TYPE_P (arg));
14442
14443 quals = cp_type_quals (arg) | cp_type_quals (t);
14444
14445 return cp_build_qualified_type_real
14446 (arg, quals, complain | tf_ignore_bad_quals);
14447 }
14448 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14449 {
14450 /* We are processing a type constructed from a
14451 template template parameter. */
14452 tree argvec = tsubst (TYPE_TI_ARGS (t),
14453 args, complain, in_decl);
14454 if (argvec == error_mark_node)
14455 return error_mark_node;
14456
14457 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14458 || TREE_CODE (arg) == TEMPLATE_DECL
14459 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14460
14461 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14462 /* Consider this code:
14463
14464 template <template <class> class Template>
14465 struct Internal {
14466 template <class Arg> using Bind = Template<Arg>;
14467 };
14468
14469 template <template <class> class Template, class Arg>
14470 using Instantiate = Template<Arg>; //#0
14471
14472 template <template <class> class Template,
14473 class Argument>
14474 using Bind =
14475 Instantiate<Internal<Template>::template Bind,
14476 Argument>; //#1
14477
14478 When #1 is parsed, the
14479 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14480 parameter `Template' in #0 matches the
14481 UNBOUND_CLASS_TEMPLATE representing the argument
14482 `Internal<Template>::template Bind'; We then want
14483 to assemble the type `Bind<Argument>' that can't
14484 be fully created right now, because
14485 `Internal<Template>' not being complete, the Bind
14486 template cannot be looked up in that context. So
14487 we need to "store" `Bind<Argument>' for later
14488 when the context of Bind becomes complete. Let's
14489 store that in a TYPENAME_TYPE. */
14490 return make_typename_type (TYPE_CONTEXT (arg),
14491 build_nt (TEMPLATE_ID_EXPR,
14492 TYPE_IDENTIFIER (arg),
14493 argvec),
14494 typename_type,
14495 complain);
14496
14497 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14498 are resolving nested-types in the signature of a
14499 member function templates. Otherwise ARG is a
14500 TEMPLATE_DECL and is the real template to be
14501 instantiated. */
14502 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14503 arg = TYPE_NAME (arg);
14504
14505 r = lookup_template_class (arg,
14506 argvec, in_decl,
14507 DECL_CONTEXT (arg),
14508 /*entering_scope=*/0,
14509 complain);
14510 return cp_build_qualified_type_real
14511 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14512 }
14513 else if (code == TEMPLATE_TEMPLATE_PARM)
14514 return arg;
14515 else
14516 /* TEMPLATE_PARM_INDEX. */
14517 return convert_from_reference (unshare_expr (arg));
14518 }
14519
14520 if (level == 1)
14521 /* This can happen during the attempted tsubst'ing in
14522 unify. This means that we don't yet have any information
14523 about the template parameter in question. */
14524 return t;
14525
14526 /* If we get here, we must have been looking at a parm for a
14527 more deeply nested template. Make a new version of this
14528 template parameter, but with a lower level. */
14529 switch (code)
14530 {
14531 case TEMPLATE_TYPE_PARM:
14532 case TEMPLATE_TEMPLATE_PARM:
14533 case BOUND_TEMPLATE_TEMPLATE_PARM:
14534 if (cp_type_quals (t))
14535 {
14536 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14537 r = cp_build_qualified_type_real
14538 (r, cp_type_quals (t),
14539 complain | (code == TEMPLATE_TYPE_PARM
14540 ? tf_ignore_bad_quals : 0));
14541 }
14542 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14543 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14544 && (r = (TEMPLATE_PARM_DESCENDANTS
14545 (TEMPLATE_TYPE_PARM_INDEX (t))))
14546 && (r = TREE_TYPE (r))
14547 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14548 /* Break infinite recursion when substituting the constraints
14549 of a constrained placeholder. */;
14550 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14551 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14552 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14553 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14554 r = TEMPLATE_PARM_DESCENDANTS (arg))
14555 && (TEMPLATE_PARM_LEVEL (r)
14556 == TEMPLATE_PARM_LEVEL (arg) - levels))
14557 /* Cache the simple case of lowering a type parameter. */
14558 r = TREE_TYPE (r);
14559 else
14560 {
14561 r = copy_type (t);
14562 TEMPLATE_TYPE_PARM_INDEX (r)
14563 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14564 r, levels, args, complain);
14565 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14566 TYPE_MAIN_VARIANT (r) = r;
14567 TYPE_POINTER_TO (r) = NULL_TREE;
14568 TYPE_REFERENCE_TO (r) = NULL_TREE;
14569
14570 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14571 {
14572 /* Propagate constraints on placeholders. */
14573 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14574 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14575 = tsubst_constraint (constr, args, complain, in_decl);
14576 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14577 {
14578 pl = tsubst_copy (pl, args, complain, in_decl);
14579 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14580 }
14581 }
14582
14583 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14584 /* We have reduced the level of the template
14585 template parameter, but not the levels of its
14586 template parameters, so canonical_type_parameter
14587 will not be able to find the canonical template
14588 template parameter for this level. Thus, we
14589 require structural equality checking to compare
14590 TEMPLATE_TEMPLATE_PARMs. */
14591 SET_TYPE_STRUCTURAL_EQUALITY (r);
14592 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14593 SET_TYPE_STRUCTURAL_EQUALITY (r);
14594 else
14595 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14596
14597 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14598 {
14599 tree tinfo = TYPE_TEMPLATE_INFO (t);
14600 /* We might need to substitute into the types of non-type
14601 template parameters. */
14602 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14603 complain, in_decl);
14604 if (tmpl == error_mark_node)
14605 return error_mark_node;
14606 tree argvec = tsubst (TI_ARGS (tinfo), args,
14607 complain, in_decl);
14608 if (argvec == error_mark_node)
14609 return error_mark_node;
14610
14611 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14612 = build_template_info (tmpl, argvec);
14613 }
14614 }
14615 break;
14616
14617 case TEMPLATE_PARM_INDEX:
14618 /* OK, now substitute the type of the non-type parameter. We
14619 couldn't do it earlier because it might be an auto parameter,
14620 and we wouldn't need to if we had an argument. */
14621 type = tsubst (type, args, complain, in_decl);
14622 if (type == error_mark_node)
14623 return error_mark_node;
14624 r = reduce_template_parm_level (t, type, levels, args, complain);
14625 break;
14626
14627 default:
14628 gcc_unreachable ();
14629 }
14630
14631 return r;
14632 }
14633
14634 case TREE_LIST:
14635 {
14636 tree purpose, value, chain;
14637
14638 if (t == void_list_node)
14639 return t;
14640
14641 purpose = TREE_PURPOSE (t);
14642 if (purpose)
14643 {
14644 purpose = tsubst (purpose, args, complain, in_decl);
14645 if (purpose == error_mark_node)
14646 return error_mark_node;
14647 }
14648 value = TREE_VALUE (t);
14649 if (value)
14650 {
14651 value = tsubst (value, args, complain, in_decl);
14652 if (value == error_mark_node)
14653 return error_mark_node;
14654 }
14655 chain = TREE_CHAIN (t);
14656 if (chain && chain != void_type_node)
14657 {
14658 chain = tsubst (chain, args, complain, in_decl);
14659 if (chain == error_mark_node)
14660 return error_mark_node;
14661 }
14662 if (purpose == TREE_PURPOSE (t)
14663 && value == TREE_VALUE (t)
14664 && chain == TREE_CHAIN (t))
14665 return t;
14666 return hash_tree_cons (purpose, value, chain);
14667 }
14668
14669 case TREE_BINFO:
14670 /* We should never be tsubsting a binfo. */
14671 gcc_unreachable ();
14672
14673 case TREE_VEC:
14674 /* A vector of template arguments. */
14675 gcc_assert (!type);
14676 return tsubst_template_args (t, args, complain, in_decl);
14677
14678 case POINTER_TYPE:
14679 case REFERENCE_TYPE:
14680 {
14681 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14682 return t;
14683
14684 /* [temp.deduct]
14685
14686 Type deduction may fail for any of the following
14687 reasons:
14688
14689 -- Attempting to create a pointer to reference type.
14690 -- Attempting to create a reference to a reference type or
14691 a reference to void.
14692
14693 Core issue 106 says that creating a reference to a reference
14694 during instantiation is no longer a cause for failure. We
14695 only enforce this check in strict C++98 mode. */
14696 if ((TYPE_REF_P (type)
14697 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14698 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14699 {
14700 static location_t last_loc;
14701
14702 /* We keep track of the last time we issued this error
14703 message to avoid spewing a ton of messages during a
14704 single bad template instantiation. */
14705 if (complain & tf_error
14706 && last_loc != input_location)
14707 {
14708 if (VOID_TYPE_P (type))
14709 error ("forming reference to void");
14710 else if (code == POINTER_TYPE)
14711 error ("forming pointer to reference type %qT", type);
14712 else
14713 error ("forming reference to reference type %qT", type);
14714 last_loc = input_location;
14715 }
14716
14717 return error_mark_node;
14718 }
14719 else if (TREE_CODE (type) == FUNCTION_TYPE
14720 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14721 || type_memfn_rqual (type) != REF_QUAL_NONE))
14722 {
14723 if (complain & tf_error)
14724 {
14725 if (code == POINTER_TYPE)
14726 error ("forming pointer to qualified function type %qT",
14727 type);
14728 else
14729 error ("forming reference to qualified function type %qT",
14730 type);
14731 }
14732 return error_mark_node;
14733 }
14734 else if (code == POINTER_TYPE)
14735 {
14736 r = build_pointer_type (type);
14737 if (TREE_CODE (type) == METHOD_TYPE)
14738 r = build_ptrmemfunc_type (r);
14739 }
14740 else if (TYPE_REF_P (type))
14741 /* In C++0x, during template argument substitution, when there is an
14742 attempt to create a reference to a reference type, reference
14743 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14744
14745 "If a template-argument for a template-parameter T names a type
14746 that is a reference to a type A, an attempt to create the type
14747 'lvalue reference to cv T' creates the type 'lvalue reference to
14748 A,' while an attempt to create the type type rvalue reference to
14749 cv T' creates the type T"
14750 */
14751 r = cp_build_reference_type
14752 (TREE_TYPE (type),
14753 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14754 else
14755 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14756 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14757
14758 if (r != error_mark_node)
14759 /* Will this ever be needed for TYPE_..._TO values? */
14760 layout_type (r);
14761
14762 return r;
14763 }
14764 case OFFSET_TYPE:
14765 {
14766 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14767 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14768 {
14769 /* [temp.deduct]
14770
14771 Type deduction may fail for any of the following
14772 reasons:
14773
14774 -- Attempting to create "pointer to member of T" when T
14775 is not a class type. */
14776 if (complain & tf_error)
14777 error ("creating pointer to member of non-class type %qT", r);
14778 return error_mark_node;
14779 }
14780 if (TYPE_REF_P (type))
14781 {
14782 if (complain & tf_error)
14783 error ("creating pointer to member reference type %qT", type);
14784 return error_mark_node;
14785 }
14786 if (VOID_TYPE_P (type))
14787 {
14788 if (complain & tf_error)
14789 error ("creating pointer to member of type void");
14790 return error_mark_node;
14791 }
14792 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14793 if (TREE_CODE (type) == FUNCTION_TYPE)
14794 {
14795 /* The type of the implicit object parameter gets its
14796 cv-qualifiers from the FUNCTION_TYPE. */
14797 tree memptr;
14798 tree method_type
14799 = build_memfn_type (type, r, type_memfn_quals (type),
14800 type_memfn_rqual (type));
14801 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14802 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14803 complain);
14804 }
14805 else
14806 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14807 cp_type_quals (t),
14808 complain);
14809 }
14810 case FUNCTION_TYPE:
14811 case METHOD_TYPE:
14812 {
14813 tree fntype;
14814 tree specs;
14815 fntype = tsubst_function_type (t, args, complain, in_decl);
14816 if (fntype == error_mark_node)
14817 return error_mark_node;
14818
14819 /* Substitute the exception specification. */
14820 specs = tsubst_exception_specification (t, args, complain, in_decl,
14821 /*defer_ok*/fndecl_type);
14822 if (specs == error_mark_node)
14823 return error_mark_node;
14824 if (specs)
14825 fntype = build_exception_variant (fntype, specs);
14826 return fntype;
14827 }
14828 case ARRAY_TYPE:
14829 {
14830 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14831 if (domain == error_mark_node)
14832 return error_mark_node;
14833
14834 /* As an optimization, we avoid regenerating the array type if
14835 it will obviously be the same as T. */
14836 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14837 return t;
14838
14839 /* These checks should match the ones in create_array_type_for_decl.
14840
14841 [temp.deduct]
14842
14843 The deduction may fail for any of the following reasons:
14844
14845 -- Attempting to create an array with an element type that
14846 is void, a function type, or a reference type, or [DR337]
14847 an abstract class type. */
14848 if (VOID_TYPE_P (type)
14849 || TREE_CODE (type) == FUNCTION_TYPE
14850 || (TREE_CODE (type) == ARRAY_TYPE
14851 && TYPE_DOMAIN (type) == NULL_TREE)
14852 || TYPE_REF_P (type))
14853 {
14854 if (complain & tf_error)
14855 error ("creating array of %qT", type);
14856 return error_mark_node;
14857 }
14858
14859 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14860 return error_mark_node;
14861
14862 r = build_cplus_array_type (type, domain);
14863
14864 if (!valid_array_size_p (input_location, r, in_decl,
14865 (complain & tf_error)))
14866 return error_mark_node;
14867
14868 if (TYPE_USER_ALIGN (t))
14869 {
14870 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14871 TYPE_USER_ALIGN (r) = 1;
14872 }
14873
14874 return r;
14875 }
14876
14877 case TYPENAME_TYPE:
14878 {
14879 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14880 in_decl, /*entering_scope=*/1);
14881 if (ctx == error_mark_node)
14882 return error_mark_node;
14883
14884 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14885 complain, in_decl);
14886 if (f == error_mark_node)
14887 return error_mark_node;
14888
14889 if (!MAYBE_CLASS_TYPE_P (ctx))
14890 {
14891 if (complain & tf_error)
14892 error ("%qT is not a class, struct, or union type", ctx);
14893 return error_mark_node;
14894 }
14895 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14896 {
14897 /* Normally, make_typename_type does not require that the CTX
14898 have complete type in order to allow things like:
14899
14900 template <class T> struct S { typename S<T>::X Y; };
14901
14902 But, such constructs have already been resolved by this
14903 point, so here CTX really should have complete type, unless
14904 it's a partial instantiation. */
14905 ctx = complete_type (ctx);
14906 if (!COMPLETE_TYPE_P (ctx))
14907 {
14908 if (complain & tf_error)
14909 cxx_incomplete_type_error (NULL_TREE, ctx);
14910 return error_mark_node;
14911 }
14912 }
14913
14914 f = make_typename_type (ctx, f, typename_type,
14915 complain | tf_keep_type_decl);
14916 if (f == error_mark_node)
14917 return f;
14918 if (TREE_CODE (f) == TYPE_DECL)
14919 {
14920 complain |= tf_ignore_bad_quals;
14921 f = TREE_TYPE (f);
14922 }
14923
14924 if (TREE_CODE (f) != TYPENAME_TYPE)
14925 {
14926 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14927 {
14928 if (complain & tf_error)
14929 error ("%qT resolves to %qT, which is not an enumeration type",
14930 t, f);
14931 else
14932 return error_mark_node;
14933 }
14934 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14935 {
14936 if (complain & tf_error)
14937 error ("%qT resolves to %qT, which is is not a class type",
14938 t, f);
14939 else
14940 return error_mark_node;
14941 }
14942 }
14943
14944 return cp_build_qualified_type_real
14945 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14946 }
14947
14948 case UNBOUND_CLASS_TEMPLATE:
14949 {
14950 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14951 in_decl, /*entering_scope=*/1);
14952 tree name = TYPE_IDENTIFIER (t);
14953 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14954
14955 if (ctx == error_mark_node || name == error_mark_node)
14956 return error_mark_node;
14957
14958 if (parm_list)
14959 parm_list = tsubst_template_parms (parm_list, args, complain);
14960 return make_unbound_class_template (ctx, name, parm_list, complain);
14961 }
14962
14963 case TYPEOF_TYPE:
14964 {
14965 tree type;
14966
14967 ++cp_unevaluated_operand;
14968 ++c_inhibit_evaluation_warnings;
14969
14970 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14971 complain, in_decl,
14972 /*integral_constant_expression_p=*/false);
14973
14974 --cp_unevaluated_operand;
14975 --c_inhibit_evaluation_warnings;
14976
14977 type = finish_typeof (type);
14978 return cp_build_qualified_type_real (type,
14979 cp_type_quals (t)
14980 | cp_type_quals (type),
14981 complain);
14982 }
14983
14984 case DECLTYPE_TYPE:
14985 {
14986 tree type;
14987
14988 ++cp_unevaluated_operand;
14989 ++c_inhibit_evaluation_warnings;
14990
14991 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14992 complain|tf_decltype, in_decl,
14993 /*function_p*/false,
14994 /*integral_constant_expression*/false);
14995
14996 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14997 {
14998 if (type == NULL_TREE)
14999 {
15000 if (complain & tf_error)
15001 error ("empty initializer in lambda init-capture");
15002 type = error_mark_node;
15003 }
15004 else if (TREE_CODE (type) == TREE_LIST)
15005 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15006 }
15007
15008 --cp_unevaluated_operand;
15009 --c_inhibit_evaluation_warnings;
15010
15011 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15012 type = lambda_capture_field_type (type,
15013 DECLTYPE_FOR_INIT_CAPTURE (t),
15014 DECLTYPE_FOR_REF_CAPTURE (t));
15015 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15016 type = lambda_proxy_type (type);
15017 else
15018 {
15019 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15020 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15021 && EXPR_P (type))
15022 /* In a template ~id could be either a complement expression
15023 or an unqualified-id naming a destructor; if instantiating
15024 it produces an expression, it's not an id-expression or
15025 member access. */
15026 id = false;
15027 type = finish_decltype_type (type, id, complain);
15028 }
15029 return cp_build_qualified_type_real (type,
15030 cp_type_quals (t)
15031 | cp_type_quals (type),
15032 complain | tf_ignore_bad_quals);
15033 }
15034
15035 case UNDERLYING_TYPE:
15036 {
15037 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15038 complain, in_decl);
15039 return finish_underlying_type (type);
15040 }
15041
15042 case TYPE_ARGUMENT_PACK:
15043 case NONTYPE_ARGUMENT_PACK:
15044 {
15045 tree r;
15046
15047 if (code == NONTYPE_ARGUMENT_PACK)
15048 r = make_node (code);
15049 else
15050 r = cxx_make_type (code);
15051
15052 tree pack_args = ARGUMENT_PACK_ARGS (t);
15053 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15054 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15055
15056 return r;
15057 }
15058
15059 case VOID_CST:
15060 case INTEGER_CST:
15061 case REAL_CST:
15062 case STRING_CST:
15063 case PLUS_EXPR:
15064 case MINUS_EXPR:
15065 case NEGATE_EXPR:
15066 case NOP_EXPR:
15067 case INDIRECT_REF:
15068 case ADDR_EXPR:
15069 case CALL_EXPR:
15070 case ARRAY_REF:
15071 case SCOPE_REF:
15072 /* We should use one of the expression tsubsts for these codes. */
15073 gcc_unreachable ();
15074
15075 default:
15076 sorry ("use of %qs in template", get_tree_code_name (code));
15077 return error_mark_node;
15078 }
15079 }
15080
15081 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15082 expression on the left-hand side of the "." or "->" operator. We
15083 only do the lookup if we had a dependent BASELINK. Otherwise we
15084 adjust it onto the instantiated heirarchy. */
15085
15086 static tree
15087 tsubst_baselink (tree baselink, tree object_type,
15088 tree args, tsubst_flags_t complain, tree in_decl)
15089 {
15090 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15091 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15092 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15093
15094 tree optype = BASELINK_OPTYPE (baselink);
15095 optype = tsubst (optype, args, complain, in_decl);
15096
15097 tree template_args = NULL_TREE;
15098 bool template_id_p = false;
15099 tree fns = BASELINK_FUNCTIONS (baselink);
15100 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15101 {
15102 template_id_p = true;
15103 template_args = TREE_OPERAND (fns, 1);
15104 fns = TREE_OPERAND (fns, 0);
15105 if (template_args)
15106 template_args = tsubst_template_args (template_args, args,
15107 complain, in_decl);
15108 }
15109
15110 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15111 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15112 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15113
15114 if (dependent_p)
15115 {
15116 tree name = OVL_NAME (fns);
15117 if (IDENTIFIER_CONV_OP_P (name))
15118 name = make_conv_op_name (optype);
15119
15120 if (name == complete_dtor_identifier)
15121 /* Treat as-if non-dependent below. */
15122 dependent_p = false;
15123
15124 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15125 if (!baselink)
15126 {
15127 if ((complain & tf_error)
15128 && constructor_name_p (name, qualifying_scope))
15129 error ("cannot call constructor %<%T::%D%> directly",
15130 qualifying_scope, name);
15131 return error_mark_node;
15132 }
15133
15134 if (BASELINK_P (baselink))
15135 fns = BASELINK_FUNCTIONS (baselink);
15136 }
15137 else
15138 /* We're going to overwrite pieces below, make a duplicate. */
15139 baselink = copy_node (baselink);
15140
15141 /* If lookup found a single function, mark it as used at this point.
15142 (If lookup found multiple functions the one selected later by
15143 overload resolution will be marked as used at that point.) */
15144 if (!template_id_p && !really_overloaded_fn (fns))
15145 {
15146 tree fn = OVL_FIRST (fns);
15147 bool ok = mark_used (fn, complain);
15148 if (!ok && !(complain & tf_error))
15149 return error_mark_node;
15150 if (ok && BASELINK_P (baselink))
15151 /* We might have instantiated an auto function. */
15152 TREE_TYPE (baselink) = TREE_TYPE (fn);
15153 }
15154
15155 if (BASELINK_P (baselink))
15156 {
15157 /* Add back the template arguments, if present. */
15158 if (template_id_p)
15159 BASELINK_FUNCTIONS (baselink)
15160 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15161
15162 /* Update the conversion operator type. */
15163 BASELINK_OPTYPE (baselink) = optype;
15164 }
15165
15166 if (!object_type)
15167 object_type = current_class_type;
15168
15169 if (qualified_p || !dependent_p)
15170 {
15171 baselink = adjust_result_of_qualified_name_lookup (baselink,
15172 qualifying_scope,
15173 object_type);
15174 if (!qualified_p)
15175 /* We need to call adjust_result_of_qualified_name_lookup in case the
15176 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15177 so that we still get virtual function binding. */
15178 BASELINK_QUALIFIED_P (baselink) = false;
15179 }
15180
15181 return baselink;
15182 }
15183
15184 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15185 true if the qualified-id will be a postfix-expression in-and-of
15186 itself; false if more of the postfix-expression follows the
15187 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15188 of "&". */
15189
15190 static tree
15191 tsubst_qualified_id (tree qualified_id, tree args,
15192 tsubst_flags_t complain, tree in_decl,
15193 bool done, bool address_p)
15194 {
15195 tree expr;
15196 tree scope;
15197 tree name;
15198 bool is_template;
15199 tree template_args;
15200 location_t loc = UNKNOWN_LOCATION;
15201
15202 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15203
15204 /* Figure out what name to look up. */
15205 name = TREE_OPERAND (qualified_id, 1);
15206 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15207 {
15208 is_template = true;
15209 loc = EXPR_LOCATION (name);
15210 template_args = TREE_OPERAND (name, 1);
15211 if (template_args)
15212 template_args = tsubst_template_args (template_args, args,
15213 complain, in_decl);
15214 if (template_args == error_mark_node)
15215 return error_mark_node;
15216 name = TREE_OPERAND (name, 0);
15217 }
15218 else
15219 {
15220 is_template = false;
15221 template_args = NULL_TREE;
15222 }
15223
15224 /* Substitute into the qualifying scope. When there are no ARGS, we
15225 are just trying to simplify a non-dependent expression. In that
15226 case the qualifying scope may be dependent, and, in any case,
15227 substituting will not help. */
15228 scope = TREE_OPERAND (qualified_id, 0);
15229 if (args)
15230 {
15231 scope = tsubst (scope, args, complain, in_decl);
15232 expr = tsubst_copy (name, args, complain, in_decl);
15233 }
15234 else
15235 expr = name;
15236
15237 if (dependent_scope_p (scope))
15238 {
15239 if (is_template)
15240 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15241 tree r = build_qualified_name (NULL_TREE, scope, expr,
15242 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15243 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15244 return r;
15245 }
15246
15247 if (!BASELINK_P (name) && !DECL_P (expr))
15248 {
15249 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15250 {
15251 /* A BIT_NOT_EXPR is used to represent a destructor. */
15252 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15253 {
15254 error ("qualifying type %qT does not match destructor name ~%qT",
15255 scope, TREE_OPERAND (expr, 0));
15256 expr = error_mark_node;
15257 }
15258 else
15259 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15260 /*is_type_p=*/0, false);
15261 }
15262 else
15263 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15264 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15265 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15266 {
15267 if (complain & tf_error)
15268 {
15269 error ("dependent-name %qE is parsed as a non-type, but "
15270 "instantiation yields a type", qualified_id);
15271 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15272 }
15273 return error_mark_node;
15274 }
15275 }
15276
15277 if (DECL_P (expr))
15278 {
15279 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15280 scope);
15281 /* Remember that there was a reference to this entity. */
15282 if (!mark_used (expr, complain) && !(complain & tf_error))
15283 return error_mark_node;
15284 }
15285
15286 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15287 {
15288 if (complain & tf_error)
15289 qualified_name_lookup_error (scope,
15290 TREE_OPERAND (qualified_id, 1),
15291 expr, input_location);
15292 return error_mark_node;
15293 }
15294
15295 if (is_template)
15296 {
15297 /* We may be repeating a check already done during parsing, but
15298 if it was well-formed and passed then, it will pass again
15299 now, and if it didn't, we wouldn't have got here. The case
15300 we want to catch is when we couldn't tell then, and can now,
15301 namely when templ prior to substitution was an
15302 identifier. */
15303 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15304 return error_mark_node;
15305
15306 if (variable_template_p (expr))
15307 expr = lookup_and_finish_template_variable (expr, template_args,
15308 complain);
15309 else
15310 expr = lookup_template_function (expr, template_args);
15311 }
15312
15313 if (expr == error_mark_node && complain & tf_error)
15314 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15315 expr, input_location);
15316 else if (TYPE_P (scope))
15317 {
15318 expr = (adjust_result_of_qualified_name_lookup
15319 (expr, scope, current_nonlambda_class_type ()));
15320 expr = (finish_qualified_id_expr
15321 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15322 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15323 /*template_arg_p=*/false, complain));
15324 }
15325
15326 /* Expressions do not generally have reference type. */
15327 if (TREE_CODE (expr) != SCOPE_REF
15328 /* However, if we're about to form a pointer-to-member, we just
15329 want the referenced member referenced. */
15330 && TREE_CODE (expr) != OFFSET_REF)
15331 expr = convert_from_reference (expr);
15332
15333 if (REF_PARENTHESIZED_P (qualified_id))
15334 expr = force_paren_expr (expr);
15335
15336 return expr;
15337 }
15338
15339 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15340 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15341 for tsubst. */
15342
15343 static tree
15344 tsubst_init (tree init, tree decl, tree args,
15345 tsubst_flags_t complain, tree in_decl)
15346 {
15347 if (!init)
15348 return NULL_TREE;
15349
15350 init = tsubst_expr (init, args, complain, in_decl, false);
15351
15352 if (!init && TREE_TYPE (decl) != error_mark_node)
15353 {
15354 /* If we had an initializer but it
15355 instantiated to nothing,
15356 value-initialize the object. This will
15357 only occur when the initializer was a
15358 pack expansion where the parameter packs
15359 used in that expansion were of length
15360 zero. */
15361 init = build_value_init (TREE_TYPE (decl),
15362 complain);
15363 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15364 init = get_target_expr_sfinae (init, complain);
15365 if (TREE_CODE (init) == TARGET_EXPR)
15366 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15367 }
15368
15369 return init;
15370 }
15371
15372 /* Like tsubst, but deals with expressions. This function just replaces
15373 template parms; to finish processing the resultant expression, use
15374 tsubst_copy_and_build or tsubst_expr. */
15375
15376 static tree
15377 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15378 {
15379 enum tree_code code;
15380 tree r;
15381
15382 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15383 return t;
15384
15385 code = TREE_CODE (t);
15386
15387 switch (code)
15388 {
15389 case PARM_DECL:
15390 r = retrieve_local_specialization (t);
15391
15392 if (r == NULL_TREE)
15393 {
15394 /* We get here for a use of 'this' in an NSDMI. */
15395 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15396 return current_class_ptr;
15397
15398 /* This can happen for a parameter name used later in a function
15399 declaration (such as in a late-specified return type). Just
15400 make a dummy decl, since it's only used for its type. */
15401 gcc_assert (cp_unevaluated_operand != 0);
15402 r = tsubst_decl (t, args, complain);
15403 /* Give it the template pattern as its context; its true context
15404 hasn't been instantiated yet and this is good enough for
15405 mangling. */
15406 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15407 }
15408
15409 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15410 r = argument_pack_select_arg (r);
15411 if (!mark_used (r, complain) && !(complain & tf_error))
15412 return error_mark_node;
15413 return r;
15414
15415 case CONST_DECL:
15416 {
15417 tree enum_type;
15418 tree v;
15419
15420 if (DECL_TEMPLATE_PARM_P (t))
15421 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15422 /* There is no need to substitute into namespace-scope
15423 enumerators. */
15424 if (DECL_NAMESPACE_SCOPE_P (t))
15425 return t;
15426 /* If ARGS is NULL, then T is known to be non-dependent. */
15427 if (args == NULL_TREE)
15428 return scalar_constant_value (t);
15429
15430 /* Unfortunately, we cannot just call lookup_name here.
15431 Consider:
15432
15433 template <int I> int f() {
15434 enum E { a = I };
15435 struct S { void g() { E e = a; } };
15436 };
15437
15438 When we instantiate f<7>::S::g(), say, lookup_name is not
15439 clever enough to find f<7>::a. */
15440 enum_type
15441 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15442 /*entering_scope=*/0);
15443
15444 for (v = TYPE_VALUES (enum_type);
15445 v != NULL_TREE;
15446 v = TREE_CHAIN (v))
15447 if (TREE_PURPOSE (v) == DECL_NAME (t))
15448 return TREE_VALUE (v);
15449
15450 /* We didn't find the name. That should never happen; if
15451 name-lookup found it during preliminary parsing, we
15452 should find it again here during instantiation. */
15453 gcc_unreachable ();
15454 }
15455 return t;
15456
15457 case FIELD_DECL:
15458 if (DECL_CONTEXT (t))
15459 {
15460 tree ctx;
15461
15462 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15463 /*entering_scope=*/1);
15464 if (ctx != DECL_CONTEXT (t))
15465 {
15466 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15467 if (!r)
15468 {
15469 if (complain & tf_error)
15470 error ("using invalid field %qD", t);
15471 return error_mark_node;
15472 }
15473 return r;
15474 }
15475 }
15476
15477 return t;
15478
15479 case VAR_DECL:
15480 case FUNCTION_DECL:
15481 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15482 r = tsubst (t, args, complain, in_decl);
15483 else if (local_variable_p (t)
15484 && uses_template_parms (DECL_CONTEXT (t)))
15485 {
15486 r = retrieve_local_specialization (t);
15487 if (r == NULL_TREE)
15488 {
15489 /* First try name lookup to find the instantiation. */
15490 r = lookup_name (DECL_NAME (t));
15491 if (r && !is_capture_proxy (r))
15492 {
15493 /* Make sure that the one we found is the one we want. */
15494 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15495 if (ctx != DECL_CONTEXT (r))
15496 r = NULL_TREE;
15497 }
15498
15499 if (r)
15500 /* OK */;
15501 else
15502 {
15503 /* This can happen for a variable used in a
15504 late-specified return type of a local lambda, or for a
15505 local static or constant. Building a new VAR_DECL
15506 should be OK in all those cases. */
15507 r = tsubst_decl (t, args, complain);
15508 if (local_specializations)
15509 /* Avoid infinite recursion (79640). */
15510 register_local_specialization (r, t);
15511 if (decl_maybe_constant_var_p (r))
15512 {
15513 /* We can't call cp_finish_decl, so handle the
15514 initializer by hand. */
15515 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15516 complain, in_decl);
15517 if (!processing_template_decl)
15518 init = maybe_constant_init (init);
15519 if (processing_template_decl
15520 ? potential_constant_expression (init)
15521 : reduced_constant_expression_p (init))
15522 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15523 = TREE_CONSTANT (r) = true;
15524 DECL_INITIAL (r) = init;
15525 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15526 TREE_TYPE (r)
15527 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15528 complain, adc_variable_type);
15529 }
15530 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15531 || decl_constant_var_p (r)
15532 || errorcount || sorrycount);
15533 if (!processing_template_decl
15534 && !TREE_STATIC (r))
15535 r = process_outer_var_ref (r, complain);
15536 }
15537 /* Remember this for subsequent uses. */
15538 if (local_specializations)
15539 register_local_specialization (r, t);
15540 }
15541 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15542 r = argument_pack_select_arg (r);
15543 }
15544 else
15545 r = t;
15546 if (!mark_used (r, complain))
15547 return error_mark_node;
15548 return r;
15549
15550 case NAMESPACE_DECL:
15551 return t;
15552
15553 case OVERLOAD:
15554 return t;
15555
15556 case BASELINK:
15557 return tsubst_baselink (t, current_nonlambda_class_type (),
15558 args, complain, in_decl);
15559
15560 case TEMPLATE_DECL:
15561 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15562 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15563 args, complain, in_decl);
15564 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15565 return tsubst (t, args, complain, in_decl);
15566 else if (DECL_CLASS_SCOPE_P (t)
15567 && uses_template_parms (DECL_CONTEXT (t)))
15568 {
15569 /* Template template argument like the following example need
15570 special treatment:
15571
15572 template <template <class> class TT> struct C {};
15573 template <class T> struct D {
15574 template <class U> struct E {};
15575 C<E> c; // #1
15576 };
15577 D<int> d; // #2
15578
15579 We are processing the template argument `E' in #1 for
15580 the template instantiation #2. Originally, `E' is a
15581 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15582 have to substitute this with one having context `D<int>'. */
15583
15584 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15585 if (dependent_scope_p (context))
15586 {
15587 /* When rewriting a constructor into a deduction guide, a
15588 non-dependent name can become dependent, so memtmpl<args>
15589 becomes context::template memtmpl<args>. */
15590 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15591 return build_qualified_name (type, context, DECL_NAME (t),
15592 /*template*/true);
15593 }
15594 return lookup_field (context, DECL_NAME(t), 0, false);
15595 }
15596 else
15597 /* Ordinary template template argument. */
15598 return t;
15599
15600 case NON_LVALUE_EXPR:
15601 case VIEW_CONVERT_EXPR:
15602 {
15603 /* Handle location wrappers by substituting the wrapped node
15604 first, *then* reusing the resulting type. Doing the type
15605 first ensures that we handle template parameters and
15606 parameter pack expansions. */
15607 if (location_wrapper_p (t))
15608 {
15609 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15610 complain, in_decl);
15611 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15612 }
15613 tree op = TREE_OPERAND (t, 0);
15614 if (code == VIEW_CONVERT_EXPR
15615 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15616 {
15617 /* Wrapper to make a C++20 template parameter object const. */
15618 op = tsubst_copy (op, args, complain, in_decl);
15619 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15620 {
15621 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15622 return build1 (code, type, op);
15623 }
15624 else
15625 {
15626 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15627 return op;
15628 }
15629 }
15630 /* We shouldn't see any other uses of these in templates. */
15631 gcc_unreachable ();
15632 }
15633
15634 case CAST_EXPR:
15635 case REINTERPRET_CAST_EXPR:
15636 case CONST_CAST_EXPR:
15637 case STATIC_CAST_EXPR:
15638 case DYNAMIC_CAST_EXPR:
15639 case IMPLICIT_CONV_EXPR:
15640 case CONVERT_EXPR:
15641 case NOP_EXPR:
15642 {
15643 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15644 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15645 return build1 (code, type, op0);
15646 }
15647
15648 case SIZEOF_EXPR:
15649 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15650 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15651 {
15652 tree expanded, op = TREE_OPERAND (t, 0);
15653 int len = 0;
15654
15655 if (SIZEOF_EXPR_TYPE_P (t))
15656 op = TREE_TYPE (op);
15657
15658 ++cp_unevaluated_operand;
15659 ++c_inhibit_evaluation_warnings;
15660 /* We only want to compute the number of arguments. */
15661 if (PACK_EXPANSION_P (op))
15662 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15663 else
15664 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15665 args, complain, in_decl);
15666 --cp_unevaluated_operand;
15667 --c_inhibit_evaluation_warnings;
15668
15669 if (TREE_CODE (expanded) == TREE_VEC)
15670 {
15671 len = TREE_VEC_LENGTH (expanded);
15672 /* Set TREE_USED for the benefit of -Wunused. */
15673 for (int i = 0; i < len; i++)
15674 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15675 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15676 }
15677
15678 if (expanded == error_mark_node)
15679 return error_mark_node;
15680 else if (PACK_EXPANSION_P (expanded)
15681 || (TREE_CODE (expanded) == TREE_VEC
15682 && pack_expansion_args_count (expanded)))
15683
15684 {
15685 if (PACK_EXPANSION_P (expanded))
15686 /* OK. */;
15687 else if (TREE_VEC_LENGTH (expanded) == 1)
15688 expanded = TREE_VEC_ELT (expanded, 0);
15689 else
15690 expanded = make_argument_pack (expanded);
15691
15692 if (TYPE_P (expanded))
15693 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15694 false,
15695 complain & tf_error);
15696 else
15697 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15698 complain & tf_error);
15699 }
15700 else
15701 return build_int_cst (size_type_node, len);
15702 }
15703 if (SIZEOF_EXPR_TYPE_P (t))
15704 {
15705 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15706 args, complain, in_decl);
15707 r = build1 (NOP_EXPR, r, error_mark_node);
15708 r = build1 (SIZEOF_EXPR,
15709 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15710 SIZEOF_EXPR_TYPE_P (r) = 1;
15711 return r;
15712 }
15713 /* Fall through */
15714
15715 case INDIRECT_REF:
15716 case NEGATE_EXPR:
15717 case TRUTH_NOT_EXPR:
15718 case BIT_NOT_EXPR:
15719 case ADDR_EXPR:
15720 case UNARY_PLUS_EXPR: /* Unary + */
15721 case ALIGNOF_EXPR:
15722 case AT_ENCODE_EXPR:
15723 case ARROW_EXPR:
15724 case THROW_EXPR:
15725 case TYPEID_EXPR:
15726 case REALPART_EXPR:
15727 case IMAGPART_EXPR:
15728 case PAREN_EXPR:
15729 {
15730 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15731 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15732 r = build1 (code, type, op0);
15733 if (code == ALIGNOF_EXPR)
15734 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15735 return r;
15736 }
15737
15738 case COMPONENT_REF:
15739 {
15740 tree object;
15741 tree name;
15742
15743 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15744 name = TREE_OPERAND (t, 1);
15745 if (TREE_CODE (name) == BIT_NOT_EXPR)
15746 {
15747 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15748 complain, in_decl);
15749 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15750 }
15751 else if (TREE_CODE (name) == SCOPE_REF
15752 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15753 {
15754 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15755 complain, in_decl);
15756 name = TREE_OPERAND (name, 1);
15757 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15758 complain, in_decl);
15759 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15760 name = build_qualified_name (/*type=*/NULL_TREE,
15761 base, name,
15762 /*template_p=*/false);
15763 }
15764 else if (BASELINK_P (name))
15765 name = tsubst_baselink (name,
15766 non_reference (TREE_TYPE (object)),
15767 args, complain,
15768 in_decl);
15769 else
15770 name = tsubst_copy (name, args, complain, in_decl);
15771 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15772 }
15773
15774 case PLUS_EXPR:
15775 case MINUS_EXPR:
15776 case MULT_EXPR:
15777 case TRUNC_DIV_EXPR:
15778 case CEIL_DIV_EXPR:
15779 case FLOOR_DIV_EXPR:
15780 case ROUND_DIV_EXPR:
15781 case EXACT_DIV_EXPR:
15782 case BIT_AND_EXPR:
15783 case BIT_IOR_EXPR:
15784 case BIT_XOR_EXPR:
15785 case TRUNC_MOD_EXPR:
15786 case FLOOR_MOD_EXPR:
15787 case TRUTH_ANDIF_EXPR:
15788 case TRUTH_ORIF_EXPR:
15789 case TRUTH_AND_EXPR:
15790 case TRUTH_OR_EXPR:
15791 case RSHIFT_EXPR:
15792 case LSHIFT_EXPR:
15793 case RROTATE_EXPR:
15794 case LROTATE_EXPR:
15795 case EQ_EXPR:
15796 case NE_EXPR:
15797 case MAX_EXPR:
15798 case MIN_EXPR:
15799 case LE_EXPR:
15800 case GE_EXPR:
15801 case LT_EXPR:
15802 case GT_EXPR:
15803 case COMPOUND_EXPR:
15804 case DOTSTAR_EXPR:
15805 case MEMBER_REF:
15806 case PREDECREMENT_EXPR:
15807 case PREINCREMENT_EXPR:
15808 case POSTDECREMENT_EXPR:
15809 case POSTINCREMENT_EXPR:
15810 {
15811 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15812 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15813 return build_nt (code, op0, op1);
15814 }
15815
15816 case SCOPE_REF:
15817 {
15818 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15819 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15820 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15821 QUALIFIED_NAME_IS_TEMPLATE (t));
15822 }
15823
15824 case ARRAY_REF:
15825 {
15826 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15827 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15828 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15829 }
15830
15831 case CALL_EXPR:
15832 {
15833 int n = VL_EXP_OPERAND_LENGTH (t);
15834 tree result = build_vl_exp (CALL_EXPR, n);
15835 int i;
15836 for (i = 0; i < n; i++)
15837 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15838 complain, in_decl);
15839 return result;
15840 }
15841
15842 case COND_EXPR:
15843 case MODOP_EXPR:
15844 case PSEUDO_DTOR_EXPR:
15845 case VEC_PERM_EXPR:
15846 {
15847 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15848 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15849 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15850 r = build_nt (code, op0, op1, op2);
15851 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15852 return r;
15853 }
15854
15855 case NEW_EXPR:
15856 {
15857 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15858 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15859 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15860 r = build_nt (code, op0, op1, op2);
15861 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15862 return r;
15863 }
15864
15865 case DELETE_EXPR:
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 r = build_nt (code, op0, op1);
15870 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15871 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15872 return r;
15873 }
15874
15875 case TEMPLATE_ID_EXPR:
15876 {
15877 /* Substituted template arguments */
15878 tree fn = TREE_OPERAND (t, 0);
15879 tree targs = TREE_OPERAND (t, 1);
15880
15881 fn = tsubst_copy (fn, args, complain, in_decl);
15882 if (targs)
15883 targs = tsubst_template_args (targs, args, complain, in_decl);
15884
15885 return lookup_template_function (fn, targs);
15886 }
15887
15888 case TREE_LIST:
15889 {
15890 tree purpose, value, chain;
15891
15892 if (t == void_list_node)
15893 return t;
15894
15895 purpose = TREE_PURPOSE (t);
15896 if (purpose)
15897 purpose = tsubst_copy (purpose, args, complain, in_decl);
15898 value = TREE_VALUE (t);
15899 if (value)
15900 value = tsubst_copy (value, args, complain, in_decl);
15901 chain = TREE_CHAIN (t);
15902 if (chain && chain != void_type_node)
15903 chain = tsubst_copy (chain, args, complain, in_decl);
15904 if (purpose == TREE_PURPOSE (t)
15905 && value == TREE_VALUE (t)
15906 && chain == TREE_CHAIN (t))
15907 return t;
15908 return tree_cons (purpose, value, chain);
15909 }
15910
15911 case RECORD_TYPE:
15912 case UNION_TYPE:
15913 case ENUMERAL_TYPE:
15914 case INTEGER_TYPE:
15915 case TEMPLATE_TYPE_PARM:
15916 case TEMPLATE_TEMPLATE_PARM:
15917 case BOUND_TEMPLATE_TEMPLATE_PARM:
15918 case TEMPLATE_PARM_INDEX:
15919 case POINTER_TYPE:
15920 case REFERENCE_TYPE:
15921 case OFFSET_TYPE:
15922 case FUNCTION_TYPE:
15923 case METHOD_TYPE:
15924 case ARRAY_TYPE:
15925 case TYPENAME_TYPE:
15926 case UNBOUND_CLASS_TEMPLATE:
15927 case TYPEOF_TYPE:
15928 case DECLTYPE_TYPE:
15929 case TYPE_DECL:
15930 return tsubst (t, args, complain, in_decl);
15931
15932 case USING_DECL:
15933 t = DECL_NAME (t);
15934 /* Fall through. */
15935 case IDENTIFIER_NODE:
15936 if (IDENTIFIER_CONV_OP_P (t))
15937 {
15938 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15939 return make_conv_op_name (new_type);
15940 }
15941 else
15942 return t;
15943
15944 case CONSTRUCTOR:
15945 /* This is handled by tsubst_copy_and_build. */
15946 gcc_unreachable ();
15947
15948 case VA_ARG_EXPR:
15949 {
15950 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15951 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15952 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15953 }
15954
15955 case CLEANUP_POINT_EXPR:
15956 /* We shouldn't have built any of these during initial template
15957 generation. Instead, they should be built during instantiation
15958 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15959 gcc_unreachable ();
15960
15961 case OFFSET_REF:
15962 {
15963 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15964 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15965 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15966 r = build2 (code, type, op0, op1);
15967 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15968 if (!mark_used (TREE_OPERAND (r, 1), complain)
15969 && !(complain & tf_error))
15970 return error_mark_node;
15971 return r;
15972 }
15973
15974 case EXPR_PACK_EXPANSION:
15975 error ("invalid use of pack expansion expression");
15976 return error_mark_node;
15977
15978 case NONTYPE_ARGUMENT_PACK:
15979 error ("use %<...%> to expand argument pack");
15980 return error_mark_node;
15981
15982 case VOID_CST:
15983 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15984 return t;
15985
15986 case INTEGER_CST:
15987 case REAL_CST:
15988 case STRING_CST:
15989 case COMPLEX_CST:
15990 {
15991 /* Instantiate any typedefs in the type. */
15992 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15993 r = fold_convert (type, t);
15994 gcc_assert (TREE_CODE (r) == code);
15995 return r;
15996 }
15997
15998 case PTRMEM_CST:
15999 /* These can sometimes show up in a partial instantiation, but never
16000 involve template parms. */
16001 gcc_assert (!uses_template_parms (t));
16002 return t;
16003
16004 case UNARY_LEFT_FOLD_EXPR:
16005 return tsubst_unary_left_fold (t, args, complain, in_decl);
16006 case UNARY_RIGHT_FOLD_EXPR:
16007 return tsubst_unary_right_fold (t, args, complain, in_decl);
16008 case BINARY_LEFT_FOLD_EXPR:
16009 return tsubst_binary_left_fold (t, args, complain, in_decl);
16010 case BINARY_RIGHT_FOLD_EXPR:
16011 return tsubst_binary_right_fold (t, args, complain, in_decl);
16012 case PREDICT_EXPR:
16013 return t;
16014
16015 case DEBUG_BEGIN_STMT:
16016 /* ??? There's no point in copying it for now, but maybe some
16017 day it will contain more information, such as a pointer back
16018 to the containing function, inlined copy or so. */
16019 return t;
16020
16021 default:
16022 /* We shouldn't get here, but keep going if !flag_checking. */
16023 if (flag_checking)
16024 gcc_unreachable ();
16025 return t;
16026 }
16027 }
16028
16029 /* Helper function for tsubst_omp_clauses, used for instantiation of
16030 OMP_CLAUSE_DECL of clauses. */
16031
16032 static tree
16033 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16034 tree in_decl, tree *iterator_cache)
16035 {
16036 if (decl == NULL_TREE)
16037 return NULL_TREE;
16038
16039 /* Handle OpenMP iterators. */
16040 if (TREE_CODE (decl) == TREE_LIST
16041 && TREE_PURPOSE (decl)
16042 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16043 {
16044 tree ret;
16045 if (iterator_cache[0] == TREE_PURPOSE (decl))
16046 ret = iterator_cache[1];
16047 else
16048 {
16049 tree *tp = &ret;
16050 begin_scope (sk_omp, NULL);
16051 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16052 {
16053 *tp = copy_node (it);
16054 TREE_VEC_ELT (*tp, 0)
16055 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16056 TREE_VEC_ELT (*tp, 1)
16057 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16058 /*integral_constant_expression_p=*/false);
16059 TREE_VEC_ELT (*tp, 2)
16060 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16061 /*integral_constant_expression_p=*/false);
16062 TREE_VEC_ELT (*tp, 3)
16063 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16064 /*integral_constant_expression_p=*/false);
16065 TREE_CHAIN (*tp) = NULL_TREE;
16066 tp = &TREE_CHAIN (*tp);
16067 }
16068 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16069 iterator_cache[0] = TREE_PURPOSE (decl);
16070 iterator_cache[1] = ret;
16071 }
16072 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16073 args, complain,
16074 in_decl, NULL));
16075 }
16076
16077 /* Handle an OpenMP array section represented as a TREE_LIST (or
16078 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16079 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16080 TREE_LIST. We can handle it exactly the same as an array section
16081 (purpose, value, and a chain), even though the nomenclature
16082 (low_bound, length, etc) is different. */
16083 if (TREE_CODE (decl) == TREE_LIST)
16084 {
16085 tree low_bound
16086 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16087 /*integral_constant_expression_p=*/false);
16088 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16089 /*integral_constant_expression_p=*/false);
16090 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16091 in_decl, NULL);
16092 if (TREE_PURPOSE (decl) == low_bound
16093 && TREE_VALUE (decl) == length
16094 && TREE_CHAIN (decl) == chain)
16095 return decl;
16096 tree ret = tree_cons (low_bound, length, chain);
16097 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16098 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16099 return ret;
16100 }
16101 tree ret = tsubst_expr (decl, args, complain, in_decl,
16102 /*integral_constant_expression_p=*/false);
16103 /* Undo convert_from_reference tsubst_expr could have called. */
16104 if (decl
16105 && REFERENCE_REF_P (ret)
16106 && !REFERENCE_REF_P (decl))
16107 ret = TREE_OPERAND (ret, 0);
16108 return ret;
16109 }
16110
16111 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16112
16113 static tree
16114 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16115 tree args, tsubst_flags_t complain, tree in_decl)
16116 {
16117 tree new_clauses = NULL_TREE, nc, oc;
16118 tree linear_no_step = NULL_TREE;
16119 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16120
16121 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16122 {
16123 nc = copy_node (oc);
16124 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16125 new_clauses = nc;
16126
16127 switch (OMP_CLAUSE_CODE (nc))
16128 {
16129 case OMP_CLAUSE_LASTPRIVATE:
16130 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16131 {
16132 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16133 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16134 in_decl, /*integral_constant_expression_p=*/false);
16135 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16136 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16137 }
16138 /* FALLTHRU */
16139 case OMP_CLAUSE_PRIVATE:
16140 case OMP_CLAUSE_SHARED:
16141 case OMP_CLAUSE_FIRSTPRIVATE:
16142 case OMP_CLAUSE_COPYIN:
16143 case OMP_CLAUSE_COPYPRIVATE:
16144 case OMP_CLAUSE_UNIFORM:
16145 case OMP_CLAUSE_DEPEND:
16146 case OMP_CLAUSE_FROM:
16147 case OMP_CLAUSE_TO:
16148 case OMP_CLAUSE_MAP:
16149 case OMP_CLAUSE_NONTEMPORAL:
16150 case OMP_CLAUSE_USE_DEVICE_PTR:
16151 case OMP_CLAUSE_IS_DEVICE_PTR:
16152 OMP_CLAUSE_DECL (nc)
16153 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16154 in_decl, iterator_cache);
16155 break;
16156 case OMP_CLAUSE_TILE:
16157 case OMP_CLAUSE_IF:
16158 case OMP_CLAUSE_NUM_THREADS:
16159 case OMP_CLAUSE_SCHEDULE:
16160 case OMP_CLAUSE_COLLAPSE:
16161 case OMP_CLAUSE_FINAL:
16162 case OMP_CLAUSE_DEVICE:
16163 case OMP_CLAUSE_DIST_SCHEDULE:
16164 case OMP_CLAUSE_NUM_TEAMS:
16165 case OMP_CLAUSE_THREAD_LIMIT:
16166 case OMP_CLAUSE_SAFELEN:
16167 case OMP_CLAUSE_SIMDLEN:
16168 case OMP_CLAUSE_NUM_TASKS:
16169 case OMP_CLAUSE_GRAINSIZE:
16170 case OMP_CLAUSE_PRIORITY:
16171 case OMP_CLAUSE_ORDERED:
16172 case OMP_CLAUSE_HINT:
16173 case OMP_CLAUSE_NUM_GANGS:
16174 case OMP_CLAUSE_NUM_WORKERS:
16175 case OMP_CLAUSE_VECTOR_LENGTH:
16176 case OMP_CLAUSE_WORKER:
16177 case OMP_CLAUSE_VECTOR:
16178 case OMP_CLAUSE_ASYNC:
16179 case OMP_CLAUSE_WAIT:
16180 OMP_CLAUSE_OPERAND (nc, 0)
16181 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16182 in_decl, /*integral_constant_expression_p=*/false);
16183 break;
16184 case OMP_CLAUSE_REDUCTION:
16185 case OMP_CLAUSE_IN_REDUCTION:
16186 case OMP_CLAUSE_TASK_REDUCTION:
16187 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16188 {
16189 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16190 if (TREE_CODE (placeholder) == SCOPE_REF)
16191 {
16192 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16193 complain, in_decl);
16194 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16195 = build_qualified_name (NULL_TREE, scope,
16196 TREE_OPERAND (placeholder, 1),
16197 false);
16198 }
16199 else
16200 gcc_assert (identifier_p (placeholder));
16201 }
16202 OMP_CLAUSE_DECL (nc)
16203 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16204 in_decl, NULL);
16205 break;
16206 case OMP_CLAUSE_GANG:
16207 case OMP_CLAUSE_ALIGNED:
16208 OMP_CLAUSE_DECL (nc)
16209 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16210 in_decl, NULL);
16211 OMP_CLAUSE_OPERAND (nc, 1)
16212 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16213 in_decl, /*integral_constant_expression_p=*/false);
16214 break;
16215 case OMP_CLAUSE_LINEAR:
16216 OMP_CLAUSE_DECL (nc)
16217 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16218 in_decl, NULL);
16219 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16220 {
16221 gcc_assert (!linear_no_step);
16222 linear_no_step = nc;
16223 }
16224 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16225 OMP_CLAUSE_LINEAR_STEP (nc)
16226 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16227 complain, in_decl, NULL);
16228 else
16229 OMP_CLAUSE_LINEAR_STEP (nc)
16230 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16231 in_decl,
16232 /*integral_constant_expression_p=*/false);
16233 break;
16234 case OMP_CLAUSE_NOWAIT:
16235 case OMP_CLAUSE_DEFAULT:
16236 case OMP_CLAUSE_UNTIED:
16237 case OMP_CLAUSE_MERGEABLE:
16238 case OMP_CLAUSE_INBRANCH:
16239 case OMP_CLAUSE_NOTINBRANCH:
16240 case OMP_CLAUSE_PROC_BIND:
16241 case OMP_CLAUSE_FOR:
16242 case OMP_CLAUSE_PARALLEL:
16243 case OMP_CLAUSE_SECTIONS:
16244 case OMP_CLAUSE_TASKGROUP:
16245 case OMP_CLAUSE_NOGROUP:
16246 case OMP_CLAUSE_THREADS:
16247 case OMP_CLAUSE_SIMD:
16248 case OMP_CLAUSE_DEFAULTMAP:
16249 case OMP_CLAUSE_INDEPENDENT:
16250 case OMP_CLAUSE_AUTO:
16251 case OMP_CLAUSE_SEQ:
16252 case OMP_CLAUSE_IF_PRESENT:
16253 case OMP_CLAUSE_FINALIZE:
16254 break;
16255 default:
16256 gcc_unreachable ();
16257 }
16258 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16259 switch (OMP_CLAUSE_CODE (nc))
16260 {
16261 case OMP_CLAUSE_SHARED:
16262 case OMP_CLAUSE_PRIVATE:
16263 case OMP_CLAUSE_FIRSTPRIVATE:
16264 case OMP_CLAUSE_LASTPRIVATE:
16265 case OMP_CLAUSE_COPYPRIVATE:
16266 case OMP_CLAUSE_LINEAR:
16267 case OMP_CLAUSE_REDUCTION:
16268 case OMP_CLAUSE_IN_REDUCTION:
16269 case OMP_CLAUSE_TASK_REDUCTION:
16270 case OMP_CLAUSE_USE_DEVICE_PTR:
16271 case OMP_CLAUSE_IS_DEVICE_PTR:
16272 /* tsubst_expr on SCOPE_REF results in returning
16273 finish_non_static_data_member result. Undo that here. */
16274 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16275 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16276 == IDENTIFIER_NODE))
16277 {
16278 tree t = OMP_CLAUSE_DECL (nc);
16279 tree v = t;
16280 while (v)
16281 switch (TREE_CODE (v))
16282 {
16283 case COMPONENT_REF:
16284 case MEM_REF:
16285 case INDIRECT_REF:
16286 CASE_CONVERT:
16287 case POINTER_PLUS_EXPR:
16288 v = TREE_OPERAND (v, 0);
16289 continue;
16290 case PARM_DECL:
16291 if (DECL_CONTEXT (v) == current_function_decl
16292 && DECL_ARTIFICIAL (v)
16293 && DECL_NAME (v) == this_identifier)
16294 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16295 /* FALLTHRU */
16296 default:
16297 v = NULL_TREE;
16298 break;
16299 }
16300 }
16301 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16302 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16303 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16304 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16305 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16306 {
16307 tree decl = OMP_CLAUSE_DECL (nc);
16308 if (VAR_P (decl))
16309 {
16310 retrofit_lang_decl (decl);
16311 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16312 }
16313 }
16314 break;
16315 default:
16316 break;
16317 }
16318 }
16319
16320 new_clauses = nreverse (new_clauses);
16321 if (ort != C_ORT_OMP_DECLARE_SIMD)
16322 {
16323 new_clauses = finish_omp_clauses (new_clauses, ort);
16324 if (linear_no_step)
16325 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16326 if (nc == linear_no_step)
16327 {
16328 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16329 break;
16330 }
16331 }
16332 return new_clauses;
16333 }
16334
16335 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16336
16337 static tree
16338 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16339 tree in_decl)
16340 {
16341 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16342
16343 tree purpose, value, chain;
16344
16345 if (t == NULL)
16346 return t;
16347
16348 if (TREE_CODE (t) != TREE_LIST)
16349 return tsubst_copy_and_build (t, args, complain, in_decl,
16350 /*function_p=*/false,
16351 /*integral_constant_expression_p=*/false);
16352
16353 if (t == void_list_node)
16354 return t;
16355
16356 purpose = TREE_PURPOSE (t);
16357 if (purpose)
16358 purpose = RECUR (purpose);
16359 value = TREE_VALUE (t);
16360 if (value)
16361 {
16362 if (TREE_CODE (value) != LABEL_DECL)
16363 value = RECUR (value);
16364 else
16365 {
16366 value = lookup_label (DECL_NAME (value));
16367 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16368 TREE_USED (value) = 1;
16369 }
16370 }
16371 chain = TREE_CHAIN (t);
16372 if (chain && chain != void_type_node)
16373 chain = RECUR (chain);
16374 return tree_cons (purpose, value, chain);
16375 #undef RECUR
16376 }
16377
16378 /* Used to temporarily communicate the list of #pragma omp parallel
16379 clauses to #pragma omp for instantiation if they are combined
16380 together. */
16381
16382 static tree *omp_parallel_combined_clauses;
16383
16384 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16385 tree *, unsigned int *);
16386
16387 /* Substitute one OMP_FOR iterator. */
16388
16389 static bool
16390 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16391 tree initv, tree condv, tree incrv, tree *clauses,
16392 tree args, tsubst_flags_t complain, tree in_decl,
16393 bool integral_constant_expression_p)
16394 {
16395 #define RECUR(NODE) \
16396 tsubst_expr ((NODE), args, complain, in_decl, \
16397 integral_constant_expression_p)
16398 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16399 bool ret = false;
16400
16401 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16402 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16403
16404 decl = TREE_OPERAND (init, 0);
16405 init = TREE_OPERAND (init, 1);
16406 tree decl_expr = NULL_TREE;
16407 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16408 if (range_for)
16409 {
16410 bool decomp = false;
16411 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16412 {
16413 tree v = DECL_VALUE_EXPR (decl);
16414 if (TREE_CODE (v) == ARRAY_REF
16415 && VAR_P (TREE_OPERAND (v, 0))
16416 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16417 {
16418 tree decomp_first = NULL_TREE;
16419 unsigned decomp_cnt = 0;
16420 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16421 maybe_push_decl (d);
16422 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16423 in_decl, &decomp_first, &decomp_cnt);
16424 decomp = true;
16425 if (d == error_mark_node)
16426 decl = error_mark_node;
16427 else
16428 for (unsigned int i = 0; i < decomp_cnt; i++)
16429 {
16430 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16431 {
16432 tree v = build_nt (ARRAY_REF, d,
16433 size_int (decomp_cnt - i - 1),
16434 NULL_TREE, NULL_TREE);
16435 SET_DECL_VALUE_EXPR (decomp_first, v);
16436 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16437 }
16438 fit_decomposition_lang_decl (decomp_first, d);
16439 decomp_first = DECL_CHAIN (decomp_first);
16440 }
16441 }
16442 }
16443 decl = tsubst_decl (decl, args, complain);
16444 if (!decomp)
16445 maybe_push_decl (decl);
16446 }
16447 else if (init && TREE_CODE (init) == DECL_EXPR)
16448 {
16449 /* We need to jump through some hoops to handle declarations in the
16450 init-statement, since we might need to handle auto deduction,
16451 but we need to keep control of initialization. */
16452 decl_expr = init;
16453 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16454 decl = tsubst_decl (decl, args, complain);
16455 }
16456 else
16457 {
16458 if (TREE_CODE (decl) == SCOPE_REF)
16459 {
16460 decl = RECUR (decl);
16461 if (TREE_CODE (decl) == COMPONENT_REF)
16462 {
16463 tree v = decl;
16464 while (v)
16465 switch (TREE_CODE (v))
16466 {
16467 case COMPONENT_REF:
16468 case MEM_REF:
16469 case INDIRECT_REF:
16470 CASE_CONVERT:
16471 case POINTER_PLUS_EXPR:
16472 v = TREE_OPERAND (v, 0);
16473 continue;
16474 case PARM_DECL:
16475 if (DECL_CONTEXT (v) == current_function_decl
16476 && DECL_ARTIFICIAL (v)
16477 && DECL_NAME (v) == this_identifier)
16478 {
16479 decl = TREE_OPERAND (decl, 1);
16480 decl = omp_privatize_field (decl, false);
16481 }
16482 /* FALLTHRU */
16483 default:
16484 v = NULL_TREE;
16485 break;
16486 }
16487 }
16488 }
16489 else
16490 decl = RECUR (decl);
16491 }
16492 init = RECUR (init);
16493
16494 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16495 {
16496 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16497 if (TREE_CODE (o) == TREE_LIST)
16498 TREE_VEC_ELT (orig_declv, i)
16499 = tree_cons (RECUR (TREE_PURPOSE (o)),
16500 RECUR (TREE_VALUE (o)),
16501 NULL_TREE);
16502 else
16503 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16504 }
16505
16506 if (range_for)
16507 {
16508 tree this_pre_body = NULL_TREE;
16509 tree orig_init = NULL_TREE;
16510 tree orig_decl = NULL_TREE;
16511 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16512 orig_init, cond, incr);
16513 if (orig_decl)
16514 {
16515 if (orig_declv == NULL_TREE)
16516 orig_declv = copy_node (declv);
16517 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16518 ret = true;
16519 }
16520 else if (orig_declv)
16521 TREE_VEC_ELT (orig_declv, i) = decl;
16522 }
16523
16524 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16525 if (!range_for && auto_node && init)
16526 TREE_TYPE (decl)
16527 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16528
16529 gcc_assert (!type_dependent_expression_p (decl));
16530
16531 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16532 {
16533 if (decl_expr)
16534 {
16535 /* Declare the variable, but don't let that initialize it. */
16536 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16537 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16538 RECUR (decl_expr);
16539 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16540 }
16541
16542 if (!range_for)
16543 {
16544 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16545 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16546 if (TREE_CODE (incr) == MODIFY_EXPR)
16547 {
16548 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16549 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16550 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16551 NOP_EXPR, rhs, complain);
16552 }
16553 else
16554 incr = RECUR (incr);
16555 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16556 TREE_VEC_ELT (orig_declv, i) = decl;
16557 }
16558 TREE_VEC_ELT (declv, i) = decl;
16559 TREE_VEC_ELT (initv, i) = init;
16560 TREE_VEC_ELT (condv, i) = cond;
16561 TREE_VEC_ELT (incrv, i) = incr;
16562 return ret;
16563 }
16564
16565 if (decl_expr)
16566 {
16567 /* Declare and initialize the variable. */
16568 RECUR (decl_expr);
16569 init = NULL_TREE;
16570 }
16571 else if (init)
16572 {
16573 tree *pc;
16574 int j;
16575 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16576 {
16577 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16578 {
16579 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16580 && OMP_CLAUSE_DECL (*pc) == decl)
16581 break;
16582 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16583 && OMP_CLAUSE_DECL (*pc) == decl)
16584 {
16585 if (j)
16586 break;
16587 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16588 tree c = *pc;
16589 *pc = OMP_CLAUSE_CHAIN (c);
16590 OMP_CLAUSE_CHAIN (c) = *clauses;
16591 *clauses = c;
16592 }
16593 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16594 && OMP_CLAUSE_DECL (*pc) == decl)
16595 {
16596 error ("iteration variable %qD should not be firstprivate",
16597 decl);
16598 *pc = OMP_CLAUSE_CHAIN (*pc);
16599 }
16600 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16601 && OMP_CLAUSE_DECL (*pc) == decl)
16602 {
16603 error ("iteration variable %qD should not be reduction",
16604 decl);
16605 *pc = OMP_CLAUSE_CHAIN (*pc);
16606 }
16607 else
16608 pc = &OMP_CLAUSE_CHAIN (*pc);
16609 }
16610 if (*pc)
16611 break;
16612 }
16613 if (*pc == NULL_TREE)
16614 {
16615 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16616 OMP_CLAUSE_DECL (c) = decl;
16617 c = finish_omp_clauses (c, C_ORT_OMP);
16618 if (c)
16619 {
16620 OMP_CLAUSE_CHAIN (c) = *clauses;
16621 *clauses = c;
16622 }
16623 }
16624 }
16625 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16626 if (COMPARISON_CLASS_P (cond))
16627 {
16628 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16629 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16630 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16631 }
16632 else
16633 cond = RECUR (cond);
16634 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16635 switch (TREE_CODE (incr))
16636 {
16637 case PREINCREMENT_EXPR:
16638 case PREDECREMENT_EXPR:
16639 case POSTINCREMENT_EXPR:
16640 case POSTDECREMENT_EXPR:
16641 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16642 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16643 break;
16644 case MODIFY_EXPR:
16645 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16646 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16647 {
16648 tree rhs = TREE_OPERAND (incr, 1);
16649 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16650 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16651 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16652 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16653 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16654 rhs0, rhs1));
16655 }
16656 else
16657 incr = RECUR (incr);
16658 break;
16659 case MODOP_EXPR:
16660 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16661 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16662 {
16663 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16664 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16665 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16666 TREE_TYPE (decl), lhs,
16667 RECUR (TREE_OPERAND (incr, 2))));
16668 }
16669 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16670 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16671 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16672 {
16673 tree rhs = TREE_OPERAND (incr, 2);
16674 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16675 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16676 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16677 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16678 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16679 rhs0, rhs1));
16680 }
16681 else
16682 incr = RECUR (incr);
16683 break;
16684 default:
16685 incr = RECUR (incr);
16686 break;
16687 }
16688
16689 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16690 TREE_VEC_ELT (orig_declv, i) = decl;
16691 TREE_VEC_ELT (declv, i) = decl;
16692 TREE_VEC_ELT (initv, i) = init;
16693 TREE_VEC_ELT (condv, i) = cond;
16694 TREE_VEC_ELT (incrv, i) = incr;
16695 return false;
16696 #undef RECUR
16697 }
16698
16699 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16700 of OMP_TARGET's body. */
16701
16702 static tree
16703 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16704 {
16705 *walk_subtrees = 0;
16706 switch (TREE_CODE (*tp))
16707 {
16708 case OMP_TEAMS:
16709 return *tp;
16710 case BIND_EXPR:
16711 case STATEMENT_LIST:
16712 *walk_subtrees = 1;
16713 break;
16714 default:
16715 break;
16716 }
16717 return NULL_TREE;
16718 }
16719
16720 /* Helper function for tsubst_expr. For decomposition declaration
16721 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16722 also the corresponding decls representing the identifiers
16723 of the decomposition declaration. Return DECL if successful
16724 or error_mark_node otherwise, set *FIRST to the first decl
16725 in the list chained through DECL_CHAIN and *CNT to the number
16726 of such decls. */
16727
16728 static tree
16729 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16730 tsubst_flags_t complain, tree in_decl, tree *first,
16731 unsigned int *cnt)
16732 {
16733 tree decl2, decl3, prev = decl;
16734 *cnt = 0;
16735 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16736 for (decl2 = DECL_CHAIN (pattern_decl);
16737 decl2
16738 && VAR_P (decl2)
16739 && DECL_DECOMPOSITION_P (decl2)
16740 && DECL_NAME (decl2);
16741 decl2 = DECL_CHAIN (decl2))
16742 {
16743 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16744 {
16745 gcc_assert (errorcount);
16746 return error_mark_node;
16747 }
16748 (*cnt)++;
16749 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16750 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16751 tree v = DECL_VALUE_EXPR (decl2);
16752 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16753 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16754 decl3 = tsubst (decl2, args, complain, in_decl);
16755 SET_DECL_VALUE_EXPR (decl2, v);
16756 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16757 if (VAR_P (decl3))
16758 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16759 else
16760 {
16761 gcc_assert (errorcount);
16762 decl = error_mark_node;
16763 continue;
16764 }
16765 maybe_push_decl (decl3);
16766 if (error_operand_p (decl3))
16767 decl = error_mark_node;
16768 else if (decl != error_mark_node
16769 && DECL_CHAIN (decl3) != prev
16770 && decl != prev)
16771 {
16772 gcc_assert (errorcount);
16773 decl = error_mark_node;
16774 }
16775 else
16776 prev = decl3;
16777 }
16778 *first = prev;
16779 return decl;
16780 }
16781
16782 /* Return the proper local_specialization for init-capture pack DECL. */
16783
16784 static tree
16785 lookup_init_capture_pack (tree decl)
16786 {
16787 /* We handle normal pack captures by forwarding to the specialization of the
16788 captured parameter. We can't do that for pack init-captures; we need them
16789 to have their own local_specialization. We created the individual
16790 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16791 when we process the DECL_EXPR for the pack init-capture in the template.
16792 So, how do we find them? We don't know the capture proxy pack when
16793 building the individual resulting proxies, and we don't know the
16794 individual proxies when instantiating the pack. What we have in common is
16795 the FIELD_DECL.
16796
16797 So...when we instantiate the FIELD_DECL, we stick the result in
16798 local_specializations. Then at the DECL_EXPR we look up that result, see
16799 how many elements it has, synthesize the names, and look them up. */
16800
16801 tree cname = DECL_NAME (decl);
16802 tree val = DECL_VALUE_EXPR (decl);
16803 tree field = TREE_OPERAND (val, 1);
16804 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16805 tree fpack = retrieve_local_specialization (field);
16806 if (fpack == error_mark_node)
16807 return error_mark_node;
16808
16809 int len = 1;
16810 tree vec = NULL_TREE;
16811 tree r = NULL_TREE;
16812 if (TREE_CODE (fpack) == TREE_VEC)
16813 {
16814 len = TREE_VEC_LENGTH (fpack);
16815 vec = make_tree_vec (len);
16816 r = make_node (NONTYPE_ARGUMENT_PACK);
16817 SET_ARGUMENT_PACK_ARGS (r, vec);
16818 }
16819 for (int i = 0; i < len; ++i)
16820 {
16821 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16822 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
16823 if (vec)
16824 TREE_VEC_ELT (vec, i) = elt;
16825 else
16826 r = elt;
16827 }
16828 return r;
16829 }
16830
16831 /* Like tsubst_copy for expressions, etc. but also does semantic
16832 processing. */
16833
16834 tree
16835 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16836 bool integral_constant_expression_p)
16837 {
16838 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16839 #define RECUR(NODE) \
16840 tsubst_expr ((NODE), args, complain, in_decl, \
16841 integral_constant_expression_p)
16842
16843 tree stmt, tmp;
16844 tree r;
16845 location_t loc;
16846
16847 if (t == NULL_TREE || t == error_mark_node)
16848 return t;
16849
16850 loc = input_location;
16851 if (location_t eloc = cp_expr_location (t))
16852 input_location = eloc;
16853 if (STATEMENT_CODE_P (TREE_CODE (t)))
16854 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16855
16856 switch (TREE_CODE (t))
16857 {
16858 case STATEMENT_LIST:
16859 {
16860 tree_stmt_iterator i;
16861 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16862 RECUR (tsi_stmt (i));
16863 break;
16864 }
16865
16866 case CTOR_INITIALIZER:
16867 finish_mem_initializers (tsubst_initializer_list
16868 (TREE_OPERAND (t, 0), args));
16869 break;
16870
16871 case RETURN_EXPR:
16872 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16873 break;
16874
16875 case EXPR_STMT:
16876 tmp = RECUR (EXPR_STMT_EXPR (t));
16877 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16878 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16879 else
16880 finish_expr_stmt (tmp);
16881 break;
16882
16883 case USING_STMT:
16884 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16885 /*attribs=*/NULL_TREE);
16886 break;
16887
16888 case DECL_EXPR:
16889 {
16890 tree decl, pattern_decl;
16891 tree init;
16892
16893 pattern_decl = decl = DECL_EXPR_DECL (t);
16894 if (TREE_CODE (decl) == LABEL_DECL)
16895 finish_label_decl (DECL_NAME (decl));
16896 else if (TREE_CODE (decl) == USING_DECL)
16897 {
16898 tree scope = USING_DECL_SCOPE (decl);
16899 tree name = DECL_NAME (decl);
16900
16901 scope = tsubst (scope, args, complain, in_decl);
16902 decl = lookup_qualified_name (scope, name,
16903 /*is_type_p=*/false,
16904 /*complain=*/false);
16905 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16906 qualified_name_lookup_error (scope, name, decl, input_location);
16907 else
16908 finish_local_using_decl (decl, scope, name);
16909 }
16910 else if (is_capture_proxy (decl)
16911 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16912 {
16913 /* We're in tsubst_lambda_expr, we've already inserted a new
16914 capture proxy, so look it up and register it. */
16915 tree inst;
16916 if (!DECL_PACK_P (decl))
16917 {
16918 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
16919 /*nonclass*/1, /*block_p=*/true,
16920 /*ns_only*/0, LOOKUP_HIDDEN);
16921 gcc_assert (inst != decl && is_capture_proxy (inst));
16922 }
16923 else if (is_normal_capture_proxy (decl))
16924 {
16925 inst = (retrieve_local_specialization
16926 (DECL_CAPTURED_VARIABLE (decl)));
16927 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16928 }
16929 else
16930 inst = lookup_init_capture_pack (decl);
16931
16932 register_local_specialization (inst, decl);
16933 break;
16934 }
16935 else if (DECL_PRETTY_FUNCTION_P (decl))
16936 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
16937 DECL_NAME (decl),
16938 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
16939 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16940 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16941 /* Don't copy the old closure; we'll create a new one in
16942 tsubst_lambda_expr. */
16943 break;
16944 else
16945 {
16946 init = DECL_INITIAL (decl);
16947 decl = tsubst (decl, args, complain, in_decl);
16948 if (decl != error_mark_node)
16949 {
16950 /* By marking the declaration as instantiated, we avoid
16951 trying to instantiate it. Since instantiate_decl can't
16952 handle local variables, and since we've already done
16953 all that needs to be done, that's the right thing to
16954 do. */
16955 if (VAR_P (decl))
16956 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16957 if (VAR_P (decl) && !DECL_NAME (decl)
16958 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16959 /* Anonymous aggregates are a special case. */
16960 finish_anon_union (decl);
16961 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16962 {
16963 DECL_CONTEXT (decl) = current_function_decl;
16964 if (DECL_NAME (decl) == this_identifier)
16965 {
16966 tree lam = DECL_CONTEXT (current_function_decl);
16967 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16968 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16969 }
16970 insert_capture_proxy (decl);
16971 }
16972 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16973 /* We already did a pushtag. */;
16974 else if (TREE_CODE (decl) == FUNCTION_DECL
16975 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16976 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16977 {
16978 DECL_CONTEXT (decl) = NULL_TREE;
16979 pushdecl (decl);
16980 DECL_CONTEXT (decl) = current_function_decl;
16981 cp_check_omp_declare_reduction (decl);
16982 }
16983 else
16984 {
16985 int const_init = false;
16986 unsigned int cnt = 0;
16987 tree first = NULL_TREE, ndecl = error_mark_node;
16988 maybe_push_decl (decl);
16989
16990 if (VAR_P (decl)
16991 && DECL_DECOMPOSITION_P (decl)
16992 && TREE_TYPE (pattern_decl) != error_mark_node)
16993 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
16994 complain, in_decl, &first,
16995 &cnt);
16996
16997 init = tsubst_init (init, decl, args, complain, in_decl);
16998
16999 if (VAR_P (decl))
17000 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17001 (pattern_decl));
17002
17003 if (ndecl != error_mark_node)
17004 cp_maybe_mangle_decomp (ndecl, first, cnt);
17005
17006 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17007
17008 if (ndecl != error_mark_node)
17009 cp_finish_decomp (ndecl, first, cnt);
17010 }
17011 }
17012 }
17013
17014 break;
17015 }
17016
17017 case FOR_STMT:
17018 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17019 RECUR (FOR_INIT_STMT (t));
17020 finish_init_stmt (stmt);
17021 tmp = RECUR (FOR_COND (t));
17022 finish_for_cond (tmp, stmt, false, 0);
17023 tmp = RECUR (FOR_EXPR (t));
17024 finish_for_expr (tmp, stmt);
17025 {
17026 bool prev = note_iteration_stmt_body_start ();
17027 RECUR (FOR_BODY (t));
17028 note_iteration_stmt_body_end (prev);
17029 }
17030 finish_for_stmt (stmt);
17031 break;
17032
17033 case RANGE_FOR_STMT:
17034 {
17035 /* Construct another range_for, if this is not a final
17036 substitution (for inside inside a generic lambda of a
17037 template). Otherwise convert to a regular for. */
17038 tree decl, expr;
17039 stmt = (processing_template_decl
17040 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17041 : begin_for_stmt (NULL_TREE, NULL_TREE));
17042 RECUR (RANGE_FOR_INIT_STMT (t));
17043 decl = RANGE_FOR_DECL (t);
17044 decl = tsubst (decl, args, complain, in_decl);
17045 maybe_push_decl (decl);
17046 expr = RECUR (RANGE_FOR_EXPR (t));
17047
17048 tree decomp_first = NULL_TREE;
17049 unsigned decomp_cnt = 0;
17050 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17051 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17052 complain, in_decl,
17053 &decomp_first, &decomp_cnt);
17054
17055 if (processing_template_decl)
17056 {
17057 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17058 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17059 finish_range_for_decl (stmt, decl, expr);
17060 if (decomp_first && decl != error_mark_node)
17061 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17062 }
17063 else
17064 {
17065 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17066 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17067 stmt = cp_convert_range_for (stmt, decl, expr,
17068 decomp_first, decomp_cnt,
17069 RANGE_FOR_IVDEP (t), unroll);
17070 }
17071
17072 bool prev = note_iteration_stmt_body_start ();
17073 RECUR (RANGE_FOR_BODY (t));
17074 note_iteration_stmt_body_end (prev);
17075 finish_for_stmt (stmt);
17076 }
17077 break;
17078
17079 case WHILE_STMT:
17080 stmt = begin_while_stmt ();
17081 tmp = RECUR (WHILE_COND (t));
17082 finish_while_stmt_cond (tmp, stmt, false, 0);
17083 {
17084 bool prev = note_iteration_stmt_body_start ();
17085 RECUR (WHILE_BODY (t));
17086 note_iteration_stmt_body_end (prev);
17087 }
17088 finish_while_stmt (stmt);
17089 break;
17090
17091 case DO_STMT:
17092 stmt = begin_do_stmt ();
17093 {
17094 bool prev = note_iteration_stmt_body_start ();
17095 RECUR (DO_BODY (t));
17096 note_iteration_stmt_body_end (prev);
17097 }
17098 finish_do_body (stmt);
17099 tmp = RECUR (DO_COND (t));
17100 finish_do_stmt (tmp, stmt, false, 0);
17101 break;
17102
17103 case IF_STMT:
17104 stmt = begin_if_stmt ();
17105 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17106 if (IF_STMT_CONSTEXPR_P (t))
17107 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17108 tmp = RECUR (IF_COND (t));
17109 tmp = finish_if_stmt_cond (tmp, stmt);
17110 if (IF_STMT_CONSTEXPR_P (t)
17111 && instantiation_dependent_expression_p (tmp))
17112 {
17113 /* We're partially instantiating a generic lambda, but the condition
17114 of the constexpr if is still dependent. Don't substitute into the
17115 branches now, just remember the template arguments. */
17116 do_poplevel (IF_SCOPE (stmt));
17117 IF_COND (stmt) = IF_COND (t);
17118 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17119 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17120 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17121 add_stmt (stmt);
17122 break;
17123 }
17124 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17125 /* Don't instantiate the THEN_CLAUSE. */;
17126 else
17127 {
17128 tree folded = fold_non_dependent_expr (tmp, complain);
17129 bool inhibit = integer_zerop (folded);
17130 if (inhibit)
17131 ++c_inhibit_evaluation_warnings;
17132 RECUR (THEN_CLAUSE (t));
17133 if (inhibit)
17134 --c_inhibit_evaluation_warnings;
17135 }
17136 finish_then_clause (stmt);
17137
17138 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17139 /* Don't instantiate the ELSE_CLAUSE. */;
17140 else if (ELSE_CLAUSE (t))
17141 {
17142 tree folded = fold_non_dependent_expr (tmp, complain);
17143 bool inhibit = integer_nonzerop (folded);
17144 begin_else_clause (stmt);
17145 if (inhibit)
17146 ++c_inhibit_evaluation_warnings;
17147 RECUR (ELSE_CLAUSE (t));
17148 if (inhibit)
17149 --c_inhibit_evaluation_warnings;
17150 finish_else_clause (stmt);
17151 }
17152
17153 finish_if_stmt (stmt);
17154 break;
17155
17156 case BIND_EXPR:
17157 if (BIND_EXPR_BODY_BLOCK (t))
17158 stmt = begin_function_body ();
17159 else
17160 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17161 ? BCS_TRY_BLOCK : 0);
17162
17163 RECUR (BIND_EXPR_BODY (t));
17164
17165 if (BIND_EXPR_BODY_BLOCK (t))
17166 finish_function_body (stmt);
17167 else
17168 finish_compound_stmt (stmt);
17169 break;
17170
17171 case BREAK_STMT:
17172 finish_break_stmt ();
17173 break;
17174
17175 case CONTINUE_STMT:
17176 finish_continue_stmt ();
17177 break;
17178
17179 case SWITCH_STMT:
17180 stmt = begin_switch_stmt ();
17181 tmp = RECUR (SWITCH_STMT_COND (t));
17182 finish_switch_cond (tmp, stmt);
17183 RECUR (SWITCH_STMT_BODY (t));
17184 finish_switch_stmt (stmt);
17185 break;
17186
17187 case CASE_LABEL_EXPR:
17188 {
17189 tree decl = CASE_LABEL (t);
17190 tree low = RECUR (CASE_LOW (t));
17191 tree high = RECUR (CASE_HIGH (t));
17192 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17193 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17194 {
17195 tree label = CASE_LABEL (l);
17196 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17197 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17198 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17199 }
17200 }
17201 break;
17202
17203 case LABEL_EXPR:
17204 {
17205 tree decl = LABEL_EXPR_LABEL (t);
17206 tree label;
17207
17208 label = finish_label_stmt (DECL_NAME (decl));
17209 if (TREE_CODE (label) == LABEL_DECL)
17210 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17211 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17212 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17213 }
17214 break;
17215
17216 case GOTO_EXPR:
17217 tmp = GOTO_DESTINATION (t);
17218 if (TREE_CODE (tmp) != LABEL_DECL)
17219 /* Computed goto's must be tsubst'd into. On the other hand,
17220 non-computed gotos must not be; the identifier in question
17221 will have no binding. */
17222 tmp = RECUR (tmp);
17223 else
17224 tmp = DECL_NAME (tmp);
17225 finish_goto_stmt (tmp);
17226 break;
17227
17228 case ASM_EXPR:
17229 {
17230 tree string = RECUR (ASM_STRING (t));
17231 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17232 complain, in_decl);
17233 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17234 complain, in_decl);
17235 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17236 complain, in_decl);
17237 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17238 complain, in_decl);
17239 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17240 clobbers, labels, ASM_INLINE_P (t));
17241 tree asm_expr = tmp;
17242 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17243 asm_expr = TREE_OPERAND (asm_expr, 0);
17244 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17245 }
17246 break;
17247
17248 case TRY_BLOCK:
17249 if (CLEANUP_P (t))
17250 {
17251 stmt = begin_try_block ();
17252 RECUR (TRY_STMTS (t));
17253 finish_cleanup_try_block (stmt);
17254 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17255 }
17256 else
17257 {
17258 tree compound_stmt = NULL_TREE;
17259
17260 if (FN_TRY_BLOCK_P (t))
17261 stmt = begin_function_try_block (&compound_stmt);
17262 else
17263 stmt = begin_try_block ();
17264
17265 RECUR (TRY_STMTS (t));
17266
17267 if (FN_TRY_BLOCK_P (t))
17268 finish_function_try_block (stmt);
17269 else
17270 finish_try_block (stmt);
17271
17272 RECUR (TRY_HANDLERS (t));
17273 if (FN_TRY_BLOCK_P (t))
17274 finish_function_handler_sequence (stmt, compound_stmt);
17275 else
17276 finish_handler_sequence (stmt);
17277 }
17278 break;
17279
17280 case HANDLER:
17281 {
17282 tree decl = HANDLER_PARMS (t);
17283
17284 if (decl)
17285 {
17286 decl = tsubst (decl, args, complain, in_decl);
17287 /* Prevent instantiate_decl from trying to instantiate
17288 this variable. We've already done all that needs to be
17289 done. */
17290 if (decl != error_mark_node)
17291 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17292 }
17293 stmt = begin_handler ();
17294 finish_handler_parms (decl, stmt);
17295 RECUR (HANDLER_BODY (t));
17296 finish_handler (stmt);
17297 }
17298 break;
17299
17300 case TAG_DEFN:
17301 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17302 if (CLASS_TYPE_P (tmp))
17303 {
17304 /* Local classes are not independent templates; they are
17305 instantiated along with their containing function. And this
17306 way we don't have to deal with pushing out of one local class
17307 to instantiate a member of another local class. */
17308 /* Closures are handled by the LAMBDA_EXPR. */
17309 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17310 complete_type (tmp);
17311 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17312 if ((VAR_P (fld)
17313 || (TREE_CODE (fld) == FUNCTION_DECL
17314 && !DECL_ARTIFICIAL (fld)))
17315 && DECL_TEMPLATE_INSTANTIATION (fld))
17316 instantiate_decl (fld, /*defer_ok=*/false,
17317 /*expl_inst_class=*/false);
17318 }
17319 break;
17320
17321 case STATIC_ASSERT:
17322 {
17323 tree condition;
17324
17325 ++c_inhibit_evaluation_warnings;
17326 condition =
17327 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17328 args,
17329 complain, in_decl,
17330 /*integral_constant_expression_p=*/true);
17331 --c_inhibit_evaluation_warnings;
17332
17333 finish_static_assert (condition,
17334 STATIC_ASSERT_MESSAGE (t),
17335 STATIC_ASSERT_SOURCE_LOCATION (t),
17336 /*member_p=*/false);
17337 }
17338 break;
17339
17340 case OACC_KERNELS:
17341 case OACC_PARALLEL:
17342 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17343 in_decl);
17344 stmt = begin_omp_parallel ();
17345 RECUR (OMP_BODY (t));
17346 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17347 break;
17348
17349 case OMP_PARALLEL:
17350 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17351 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17352 complain, in_decl);
17353 if (OMP_PARALLEL_COMBINED (t))
17354 omp_parallel_combined_clauses = &tmp;
17355 stmt = begin_omp_parallel ();
17356 RECUR (OMP_PARALLEL_BODY (t));
17357 gcc_assert (omp_parallel_combined_clauses == NULL);
17358 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17359 = OMP_PARALLEL_COMBINED (t);
17360 pop_omp_privatization_clauses (r);
17361 break;
17362
17363 case OMP_TASK:
17364 if (OMP_TASK_BODY (t) == NULL_TREE)
17365 {
17366 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17367 complain, in_decl);
17368 t = copy_node (t);
17369 OMP_TASK_CLAUSES (t) = tmp;
17370 add_stmt (t);
17371 break;
17372 }
17373 r = push_omp_privatization_clauses (false);
17374 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17375 complain, in_decl);
17376 stmt = begin_omp_task ();
17377 RECUR (OMP_TASK_BODY (t));
17378 finish_omp_task (tmp, stmt);
17379 pop_omp_privatization_clauses (r);
17380 break;
17381
17382 case OMP_FOR:
17383 case OMP_SIMD:
17384 case OMP_DISTRIBUTE:
17385 case OMP_TASKLOOP:
17386 case OACC_LOOP:
17387 {
17388 tree clauses, body, pre_body;
17389 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17390 tree orig_declv = NULL_TREE;
17391 tree incrv = NULL_TREE;
17392 enum c_omp_region_type ort = C_ORT_OMP;
17393 bool any_range_for = false;
17394 int i;
17395
17396 if (TREE_CODE (t) == OACC_LOOP)
17397 ort = C_ORT_ACC;
17398
17399 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17400 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17401 in_decl);
17402 if (OMP_FOR_INIT (t) != NULL_TREE)
17403 {
17404 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17405 if (OMP_FOR_ORIG_DECLS (t))
17406 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17407 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17408 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17409 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17410 }
17411
17412 keep_next_level (true);
17413 stmt = begin_omp_structured_block ();
17414
17415 pre_body = push_stmt_list ();
17416 RECUR (OMP_FOR_PRE_BODY (t));
17417 pre_body = pop_stmt_list (pre_body);
17418
17419 if (OMP_FOR_INIT (t) != NULL_TREE)
17420 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17421 any_range_for
17422 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17423 condv, incrv, &clauses, args,
17424 complain, in_decl,
17425 integral_constant_expression_p);
17426 omp_parallel_combined_clauses = NULL;
17427
17428 if (any_range_for)
17429 {
17430 gcc_assert (orig_declv);
17431 body = begin_omp_structured_block ();
17432 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17433 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17434 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17435 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17436 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17437 TREE_VEC_ELT (declv, i));
17438 }
17439 else
17440 body = push_stmt_list ();
17441 RECUR (OMP_FOR_BODY (t));
17442 if (any_range_for)
17443 body = finish_omp_structured_block (body);
17444 else
17445 body = pop_stmt_list (body);
17446
17447 if (OMP_FOR_INIT (t) != NULL_TREE)
17448 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17449 orig_declv, initv, condv, incrv, body, pre_body,
17450 NULL, clauses);
17451 else
17452 {
17453 t = make_node (TREE_CODE (t));
17454 TREE_TYPE (t) = void_type_node;
17455 OMP_FOR_BODY (t) = body;
17456 OMP_FOR_PRE_BODY (t) = pre_body;
17457 OMP_FOR_CLAUSES (t) = clauses;
17458 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17459 add_stmt (t);
17460 }
17461
17462 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17463 t));
17464 pop_omp_privatization_clauses (r);
17465 }
17466 break;
17467
17468 case OMP_SECTIONS:
17469 omp_parallel_combined_clauses = NULL;
17470 /* FALLTHRU */
17471 case OMP_SINGLE:
17472 case OMP_TEAMS:
17473 case OMP_CRITICAL:
17474 case OMP_TASKGROUP:
17475 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17476 && OMP_TEAMS_COMBINED (t));
17477 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17478 in_decl);
17479 if (TREE_CODE (t) == OMP_TEAMS)
17480 {
17481 keep_next_level (true);
17482 stmt = begin_omp_structured_block ();
17483 RECUR (OMP_BODY (t));
17484 stmt = finish_omp_structured_block (stmt);
17485 }
17486 else
17487 {
17488 stmt = push_stmt_list ();
17489 RECUR (OMP_BODY (t));
17490 stmt = pop_stmt_list (stmt);
17491 }
17492
17493 t = copy_node (t);
17494 OMP_BODY (t) = stmt;
17495 OMP_CLAUSES (t) = tmp;
17496 add_stmt (t);
17497 pop_omp_privatization_clauses (r);
17498 break;
17499
17500 case OMP_DEPOBJ:
17501 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17502 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17503 {
17504 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17505 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17506 {
17507 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17508 args, complain, in_decl);
17509 if (tmp == NULL_TREE)
17510 tmp = error_mark_node;
17511 }
17512 else
17513 {
17514 kind = (enum omp_clause_depend_kind)
17515 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17516 tmp = NULL_TREE;
17517 }
17518 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17519 }
17520 else
17521 finish_omp_depobj (EXPR_LOCATION (t), r,
17522 OMP_CLAUSE_DEPEND_SOURCE,
17523 OMP_DEPOBJ_CLAUSES (t));
17524 break;
17525
17526 case OACC_DATA:
17527 case OMP_TARGET_DATA:
17528 case OMP_TARGET:
17529 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17530 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17531 in_decl);
17532 keep_next_level (true);
17533 stmt = begin_omp_structured_block ();
17534
17535 RECUR (OMP_BODY (t));
17536 stmt = finish_omp_structured_block (stmt);
17537
17538 t = copy_node (t);
17539 OMP_BODY (t) = stmt;
17540 OMP_CLAUSES (t) = tmp;
17541 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17542 {
17543 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17544 if (teams)
17545 {
17546 /* For combined target teams, ensure the num_teams and
17547 thread_limit clause expressions are evaluated on the host,
17548 before entering the target construct. */
17549 tree c;
17550 for (c = OMP_TEAMS_CLAUSES (teams);
17551 c; c = OMP_CLAUSE_CHAIN (c))
17552 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17553 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17554 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17555 {
17556 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17557 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17558 if (expr == error_mark_node)
17559 continue;
17560 tmp = TARGET_EXPR_SLOT (expr);
17561 add_stmt (expr);
17562 OMP_CLAUSE_OPERAND (c, 0) = expr;
17563 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17564 OMP_CLAUSE_FIRSTPRIVATE);
17565 OMP_CLAUSE_DECL (tc) = tmp;
17566 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17567 OMP_TARGET_CLAUSES (t) = tc;
17568 }
17569 }
17570 }
17571 add_stmt (t);
17572 break;
17573
17574 case OACC_DECLARE:
17575 t = copy_node (t);
17576 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17577 complain, in_decl);
17578 OACC_DECLARE_CLAUSES (t) = tmp;
17579 add_stmt (t);
17580 break;
17581
17582 case OMP_TARGET_UPDATE:
17583 case OMP_TARGET_ENTER_DATA:
17584 case OMP_TARGET_EXIT_DATA:
17585 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17586 complain, in_decl);
17587 t = copy_node (t);
17588 OMP_STANDALONE_CLAUSES (t) = tmp;
17589 add_stmt (t);
17590 break;
17591
17592 case OACC_ENTER_DATA:
17593 case OACC_EXIT_DATA:
17594 case OACC_UPDATE:
17595 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17596 complain, in_decl);
17597 t = copy_node (t);
17598 OMP_STANDALONE_CLAUSES (t) = tmp;
17599 add_stmt (t);
17600 break;
17601
17602 case OMP_ORDERED:
17603 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17604 complain, in_decl);
17605 stmt = push_stmt_list ();
17606 RECUR (OMP_BODY (t));
17607 stmt = pop_stmt_list (stmt);
17608
17609 t = copy_node (t);
17610 OMP_BODY (t) = stmt;
17611 OMP_ORDERED_CLAUSES (t) = tmp;
17612 add_stmt (t);
17613 break;
17614
17615 case OMP_SECTION:
17616 case OMP_MASTER:
17617 stmt = push_stmt_list ();
17618 RECUR (OMP_BODY (t));
17619 stmt = pop_stmt_list (stmt);
17620
17621 t = copy_node (t);
17622 OMP_BODY (t) = stmt;
17623 add_stmt (t);
17624 break;
17625
17626 case OMP_ATOMIC:
17627 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17628 tmp = NULL_TREE;
17629 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17630 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17631 complain, in_decl);
17632 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17633 {
17634 tree op1 = TREE_OPERAND (t, 1);
17635 tree rhs1 = NULL_TREE;
17636 tree lhs, rhs;
17637 if (TREE_CODE (op1) == COMPOUND_EXPR)
17638 {
17639 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17640 op1 = TREE_OPERAND (op1, 1);
17641 }
17642 lhs = RECUR (TREE_OPERAND (op1, 0));
17643 rhs = RECUR (TREE_OPERAND (op1, 1));
17644 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17645 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17646 OMP_ATOMIC_MEMORY_ORDER (t));
17647 }
17648 else
17649 {
17650 tree op1 = TREE_OPERAND (t, 1);
17651 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17652 tree rhs1 = NULL_TREE;
17653 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17654 enum tree_code opcode = NOP_EXPR;
17655 if (code == OMP_ATOMIC_READ)
17656 {
17657 v = RECUR (TREE_OPERAND (op1, 0));
17658 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17659 }
17660 else if (code == OMP_ATOMIC_CAPTURE_OLD
17661 || code == OMP_ATOMIC_CAPTURE_NEW)
17662 {
17663 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17664 v = RECUR (TREE_OPERAND (op1, 0));
17665 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17666 if (TREE_CODE (op11) == COMPOUND_EXPR)
17667 {
17668 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17669 op11 = TREE_OPERAND (op11, 1);
17670 }
17671 lhs = RECUR (TREE_OPERAND (op11, 0));
17672 rhs = RECUR (TREE_OPERAND (op11, 1));
17673 opcode = TREE_CODE (op11);
17674 if (opcode == MODIFY_EXPR)
17675 opcode = NOP_EXPR;
17676 }
17677 else
17678 {
17679 code = OMP_ATOMIC;
17680 lhs = RECUR (TREE_OPERAND (op1, 0));
17681 rhs = RECUR (TREE_OPERAND (op1, 1));
17682 }
17683 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17684 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17685 }
17686 break;
17687
17688 case TRANSACTION_EXPR:
17689 {
17690 int flags = 0;
17691 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17692 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17693
17694 if (TRANSACTION_EXPR_IS_STMT (t))
17695 {
17696 tree body = TRANSACTION_EXPR_BODY (t);
17697 tree noex = NULL_TREE;
17698 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17699 {
17700 noex = MUST_NOT_THROW_COND (body);
17701 if (noex == NULL_TREE)
17702 noex = boolean_true_node;
17703 body = TREE_OPERAND (body, 0);
17704 }
17705 stmt = begin_transaction_stmt (input_location, NULL, flags);
17706 RECUR (body);
17707 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17708 }
17709 else
17710 {
17711 stmt = build_transaction_expr (EXPR_LOCATION (t),
17712 RECUR (TRANSACTION_EXPR_BODY (t)),
17713 flags, NULL_TREE);
17714 RETURN (stmt);
17715 }
17716 }
17717 break;
17718
17719 case MUST_NOT_THROW_EXPR:
17720 {
17721 tree op0 = RECUR (TREE_OPERAND (t, 0));
17722 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17723 RETURN (build_must_not_throw_expr (op0, cond));
17724 }
17725
17726 case EXPR_PACK_EXPANSION:
17727 error ("invalid use of pack expansion expression");
17728 RETURN (error_mark_node);
17729
17730 case NONTYPE_ARGUMENT_PACK:
17731 error ("use %<...%> to expand argument pack");
17732 RETURN (error_mark_node);
17733
17734 case COMPOUND_EXPR:
17735 tmp = RECUR (TREE_OPERAND (t, 0));
17736 if (tmp == NULL_TREE)
17737 /* If the first operand was a statement, we're done with it. */
17738 RETURN (RECUR (TREE_OPERAND (t, 1)));
17739 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17740 RECUR (TREE_OPERAND (t, 1)),
17741 complain));
17742
17743 case ANNOTATE_EXPR:
17744 tmp = RECUR (TREE_OPERAND (t, 0));
17745 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17746 TREE_TYPE (tmp), tmp,
17747 RECUR (TREE_OPERAND (t, 1)),
17748 RECUR (TREE_OPERAND (t, 2))));
17749
17750 case PREDICT_EXPR:
17751 RETURN (add_stmt (copy_node (t)));
17752
17753 default:
17754 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17755
17756 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17757 /*function_p=*/false,
17758 integral_constant_expression_p));
17759 }
17760
17761 RETURN (NULL_TREE);
17762 out:
17763 input_location = loc;
17764 return r;
17765 #undef RECUR
17766 #undef RETURN
17767 }
17768
17769 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17770 function. For description of the body see comment above
17771 cp_parser_omp_declare_reduction_exprs. */
17772
17773 static void
17774 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17775 {
17776 if (t == NULL_TREE || t == error_mark_node)
17777 return;
17778
17779 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17780
17781 tree_stmt_iterator tsi;
17782 int i;
17783 tree stmts[7];
17784 memset (stmts, 0, sizeof stmts);
17785 for (i = 0, tsi = tsi_start (t);
17786 i < 7 && !tsi_end_p (tsi);
17787 i++, tsi_next (&tsi))
17788 stmts[i] = tsi_stmt (tsi);
17789 gcc_assert (tsi_end_p (tsi));
17790
17791 if (i >= 3)
17792 {
17793 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17794 && TREE_CODE (stmts[1]) == DECL_EXPR);
17795 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17796 args, complain, in_decl);
17797 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17798 args, complain, in_decl);
17799 DECL_CONTEXT (omp_out) = current_function_decl;
17800 DECL_CONTEXT (omp_in) = current_function_decl;
17801 keep_next_level (true);
17802 tree block = begin_omp_structured_block ();
17803 tsubst_expr (stmts[2], args, complain, in_decl, false);
17804 block = finish_omp_structured_block (block);
17805 block = maybe_cleanup_point_expr_void (block);
17806 add_decl_expr (omp_out);
17807 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17808 TREE_NO_WARNING (omp_out) = 1;
17809 add_decl_expr (omp_in);
17810 finish_expr_stmt (block);
17811 }
17812 if (i >= 6)
17813 {
17814 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17815 && TREE_CODE (stmts[4]) == DECL_EXPR);
17816 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17817 args, complain, in_decl);
17818 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17819 args, complain, in_decl);
17820 DECL_CONTEXT (omp_priv) = current_function_decl;
17821 DECL_CONTEXT (omp_orig) = current_function_decl;
17822 keep_next_level (true);
17823 tree block = begin_omp_structured_block ();
17824 tsubst_expr (stmts[5], args, complain, in_decl, false);
17825 block = finish_omp_structured_block (block);
17826 block = maybe_cleanup_point_expr_void (block);
17827 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17828 add_decl_expr (omp_priv);
17829 add_decl_expr (omp_orig);
17830 finish_expr_stmt (block);
17831 if (i == 7)
17832 add_decl_expr (omp_orig);
17833 }
17834 }
17835
17836 /* T is a postfix-expression that is not being used in a function
17837 call. Return the substituted version of T. */
17838
17839 static tree
17840 tsubst_non_call_postfix_expression (tree t, tree args,
17841 tsubst_flags_t complain,
17842 tree in_decl)
17843 {
17844 if (TREE_CODE (t) == SCOPE_REF)
17845 t = tsubst_qualified_id (t, args, complain, in_decl,
17846 /*done=*/false, /*address_p=*/false);
17847 else
17848 t = tsubst_copy_and_build (t, args, complain, in_decl,
17849 /*function_p=*/false,
17850 /*integral_constant_expression_p=*/false);
17851
17852 return t;
17853 }
17854
17855 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17856 instantiation context. Instantiating a pack expansion containing a lambda
17857 might result in multiple lambdas all based on the same lambda in the
17858 template. */
17859
17860 tree
17861 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17862 {
17863 tree oldfn = lambda_function (t);
17864 in_decl = oldfn;
17865
17866 tree r = build_lambda_expr ();
17867
17868 LAMBDA_EXPR_LOCATION (r)
17869 = LAMBDA_EXPR_LOCATION (t);
17870 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17871 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17872 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17873
17874 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17875 /* A lambda in a default argument outside a class gets no
17876 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17877 tsubst_default_argument calls start_lambda_scope, so we need to
17878 specifically ignore it here, and use the global scope. */
17879 record_null_lambda_scope (r);
17880 else
17881 record_lambda_scope (r);
17882
17883 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17884 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17885
17886 vec<tree,va_gc>* field_packs = NULL;
17887
17888 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17889 cap = TREE_CHAIN (cap))
17890 {
17891 tree ofield = TREE_PURPOSE (cap);
17892 if (PACK_EXPANSION_P (ofield))
17893 ofield = PACK_EXPANSION_PATTERN (ofield);
17894 tree field = tsubst_decl (ofield, args, complain);
17895
17896 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
17897 {
17898 /* Remember these for when we've pushed local_specializations. */
17899 vec_safe_push (field_packs, ofield);
17900 vec_safe_push (field_packs, field);
17901 }
17902
17903 if (field == error_mark_node)
17904 return error_mark_node;
17905
17906 tree init = TREE_VALUE (cap);
17907 if (PACK_EXPANSION_P (init))
17908 init = tsubst_pack_expansion (init, args, complain, in_decl);
17909 else
17910 init = tsubst_copy_and_build (init, args, complain, in_decl,
17911 /*fn*/false, /*constexpr*/false);
17912
17913 if (TREE_CODE (field) == TREE_VEC)
17914 {
17915 int len = TREE_VEC_LENGTH (field);
17916 gcc_assert (TREE_CODE (init) == TREE_VEC
17917 && TREE_VEC_LENGTH (init) == len);
17918 for (int i = 0; i < len; ++i)
17919 LAMBDA_EXPR_CAPTURE_LIST (r)
17920 = tree_cons (TREE_VEC_ELT (field, i),
17921 TREE_VEC_ELT (init, i),
17922 LAMBDA_EXPR_CAPTURE_LIST (r));
17923 }
17924 else
17925 {
17926 LAMBDA_EXPR_CAPTURE_LIST (r)
17927 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17928
17929 if (id_equal (DECL_NAME (field), "__this"))
17930 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17931 }
17932 }
17933
17934 tree type = begin_lambda_type (r);
17935 if (type == error_mark_node)
17936 return error_mark_node;
17937
17938 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17939 determine_visibility (TYPE_NAME (type));
17940
17941 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17942
17943 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17944 ? DECL_TI_TEMPLATE (oldfn)
17945 : NULL_TREE);
17946
17947 tree fntype = static_fn_type (oldfn);
17948 if (oldtmpl)
17949 ++processing_template_decl;
17950 fntype = tsubst (fntype, args, complain, in_decl);
17951 if (oldtmpl)
17952 --processing_template_decl;
17953
17954 if (fntype == error_mark_node)
17955 r = error_mark_node;
17956 else
17957 {
17958 /* Fix the type of 'this'. */
17959 fntype = build_memfn_type (fntype, type,
17960 type_memfn_quals (fntype),
17961 type_memfn_rqual (fntype));
17962 tree fn, tmpl;
17963 if (oldtmpl)
17964 {
17965 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17966 fn = DECL_TEMPLATE_RESULT (tmpl);
17967 finish_member_declaration (tmpl);
17968 }
17969 else
17970 {
17971 tmpl = NULL_TREE;
17972 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17973 finish_member_declaration (fn);
17974 }
17975
17976 /* Let finish_function set this. */
17977 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17978
17979 /* The body of a lambda-expression is not a subexpression of the
17980 enclosing expression. */
17981 cp_evaluated ev;
17982
17983 bool nested = cfun;
17984 if (nested)
17985 push_function_context ();
17986 else
17987 /* Still increment function_depth so that we don't GC in the
17988 middle of an expression. */
17989 ++function_depth;
17990
17991 local_specialization_stack s (lss_copy);
17992
17993 tree body = start_lambda_function (fn, r);
17994
17995 /* Now record them for lookup_init_capture_pack. */
17996 int fplen = vec_safe_length (field_packs);
17997 for (int i = 0; i < fplen; )
17998 {
17999 tree pack = (*field_packs)[i++];
18000 tree inst = (*field_packs)[i++];
18001 register_local_specialization (inst, pack);
18002 }
18003 release_tree_vector (field_packs);
18004
18005 register_parameter_specializations (oldfn, fn);
18006
18007 if (oldtmpl)
18008 {
18009 /* We might not partially instantiate some parts of the function, so
18010 copy these flags from the original template. */
18011 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18012 current_function_returns_value = ol->returns_value;
18013 current_function_returns_null = ol->returns_null;
18014 current_function_returns_abnormally = ol->returns_abnormally;
18015 current_function_infinite_loop = ol->infinite_loop;
18016 }
18017
18018 /* [temp.deduct] A lambda-expression appearing in a function type or a
18019 template parameter is not considered part of the immediate context for
18020 the purposes of template argument deduction. */
18021 complain = tf_warning_or_error;
18022
18023 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18024 /*constexpr*/false);
18025
18026 finish_lambda_function (body);
18027
18028 if (nested)
18029 pop_function_context ();
18030 else
18031 --function_depth;
18032
18033 /* The capture list was built up in reverse order; fix that now. */
18034 LAMBDA_EXPR_CAPTURE_LIST (r)
18035 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18036
18037 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18038
18039 maybe_add_lambda_conv_op (type);
18040 }
18041
18042 finish_struct (type, /*attr*/NULL_TREE);
18043
18044 insert_pending_capture_proxies ();
18045
18046 return r;
18047 }
18048
18049 /* Like tsubst but deals with expressions and performs semantic
18050 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18051
18052 tree
18053 tsubst_copy_and_build (tree t,
18054 tree args,
18055 tsubst_flags_t complain,
18056 tree in_decl,
18057 bool function_p,
18058 bool integral_constant_expression_p)
18059 {
18060 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18061 #define RECUR(NODE) \
18062 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18063 /*function_p=*/false, \
18064 integral_constant_expression_p)
18065
18066 tree retval, op1;
18067 location_t loc;
18068
18069 if (t == NULL_TREE || t == error_mark_node)
18070 return t;
18071
18072 loc = input_location;
18073 if (location_t eloc = cp_expr_location (t))
18074 input_location = eloc;
18075
18076 /* N3276 decltype magic only applies to calls at the top level or on the
18077 right side of a comma. */
18078 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18079 complain &= ~tf_decltype;
18080
18081 switch (TREE_CODE (t))
18082 {
18083 case USING_DECL:
18084 t = DECL_NAME (t);
18085 /* Fall through. */
18086 case IDENTIFIER_NODE:
18087 {
18088 tree decl;
18089 cp_id_kind idk;
18090 bool non_integral_constant_expression_p;
18091 const char *error_msg;
18092
18093 if (IDENTIFIER_CONV_OP_P (t))
18094 {
18095 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18096 t = make_conv_op_name (new_type);
18097 }
18098
18099 /* Look up the name. */
18100 decl = lookup_name (t);
18101
18102 /* By convention, expressions use ERROR_MARK_NODE to indicate
18103 failure, not NULL_TREE. */
18104 if (decl == NULL_TREE)
18105 decl = error_mark_node;
18106
18107 decl = finish_id_expression (t, decl, NULL_TREE,
18108 &idk,
18109 integral_constant_expression_p,
18110 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18111 &non_integral_constant_expression_p,
18112 /*template_p=*/false,
18113 /*done=*/true,
18114 /*address_p=*/false,
18115 /*template_arg_p=*/false,
18116 &error_msg,
18117 input_location);
18118 if (error_msg)
18119 error (error_msg);
18120 if (!function_p && identifier_p (decl))
18121 {
18122 if (complain & tf_error)
18123 unqualified_name_lookup_error (decl);
18124 decl = error_mark_node;
18125 }
18126 RETURN (decl);
18127 }
18128
18129 case TEMPLATE_ID_EXPR:
18130 {
18131 tree object;
18132 tree templ = RECUR (TREE_OPERAND (t, 0));
18133 tree targs = TREE_OPERAND (t, 1);
18134
18135 if (targs)
18136 targs = tsubst_template_args (targs, args, complain, in_decl);
18137 if (targs == error_mark_node)
18138 RETURN (error_mark_node);
18139
18140 if (TREE_CODE (templ) == SCOPE_REF)
18141 {
18142 tree name = TREE_OPERAND (templ, 1);
18143 tree tid = lookup_template_function (name, targs);
18144 TREE_OPERAND (templ, 1) = tid;
18145 RETURN (templ);
18146 }
18147
18148 if (variable_template_p (templ))
18149 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18150
18151 if (TREE_CODE (templ) == COMPONENT_REF)
18152 {
18153 object = TREE_OPERAND (templ, 0);
18154 templ = TREE_OPERAND (templ, 1);
18155 }
18156 else
18157 object = NULL_TREE;
18158 templ = lookup_template_function (templ, targs);
18159
18160 if (object)
18161 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18162 object, templ, NULL_TREE));
18163 else
18164 RETURN (baselink_for_fns (templ));
18165 }
18166
18167 case INDIRECT_REF:
18168 {
18169 tree r = RECUR (TREE_OPERAND (t, 0));
18170
18171 if (REFERENCE_REF_P (t))
18172 {
18173 /* A type conversion to reference type will be enclosed in
18174 such an indirect ref, but the substitution of the cast
18175 will have also added such an indirect ref. */
18176 r = convert_from_reference (r);
18177 }
18178 else
18179 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18180 complain|decltype_flag);
18181
18182 if (REF_PARENTHESIZED_P (t))
18183 r = force_paren_expr (r);
18184
18185 RETURN (r);
18186 }
18187
18188 case NOP_EXPR:
18189 {
18190 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18191 tree op0 = RECUR (TREE_OPERAND (t, 0));
18192 RETURN (build_nop (type, op0));
18193 }
18194
18195 case IMPLICIT_CONV_EXPR:
18196 {
18197 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18198 tree expr = RECUR (TREE_OPERAND (t, 0));
18199 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18200 {
18201 retval = copy_node (t);
18202 TREE_TYPE (retval) = type;
18203 TREE_OPERAND (retval, 0) = expr;
18204 RETURN (retval);
18205 }
18206 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18207 /* We'll pass this to convert_nontype_argument again, we don't need
18208 to actually perform any conversion here. */
18209 RETURN (expr);
18210 int flags = LOOKUP_IMPLICIT;
18211 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18212 flags = LOOKUP_NORMAL;
18213 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18214 flags));
18215 }
18216
18217 case CONVERT_EXPR:
18218 {
18219 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18220 tree op0 = RECUR (TREE_OPERAND (t, 0));
18221 if (op0 == error_mark_node)
18222 RETURN (error_mark_node);
18223 RETURN (build1 (CONVERT_EXPR, type, op0));
18224 }
18225
18226 case CAST_EXPR:
18227 case REINTERPRET_CAST_EXPR:
18228 case CONST_CAST_EXPR:
18229 case DYNAMIC_CAST_EXPR:
18230 case STATIC_CAST_EXPR:
18231 {
18232 tree type;
18233 tree op, r = NULL_TREE;
18234
18235 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18236 if (integral_constant_expression_p
18237 && !cast_valid_in_integral_constant_expression_p (type))
18238 {
18239 if (complain & tf_error)
18240 error ("a cast to a type other than an integral or "
18241 "enumeration type cannot appear in a constant-expression");
18242 RETURN (error_mark_node);
18243 }
18244
18245 op = RECUR (TREE_OPERAND (t, 0));
18246
18247 warning_sentinel s(warn_useless_cast);
18248 warning_sentinel s2(warn_ignored_qualifiers);
18249 switch (TREE_CODE (t))
18250 {
18251 case CAST_EXPR:
18252 r = build_functional_cast (type, op, complain);
18253 break;
18254 case REINTERPRET_CAST_EXPR:
18255 r = build_reinterpret_cast (type, op, complain);
18256 break;
18257 case CONST_CAST_EXPR:
18258 r = build_const_cast (type, op, complain);
18259 break;
18260 case DYNAMIC_CAST_EXPR:
18261 r = build_dynamic_cast (type, op, complain);
18262 break;
18263 case STATIC_CAST_EXPR:
18264 r = build_static_cast (type, op, complain);
18265 break;
18266 default:
18267 gcc_unreachable ();
18268 }
18269
18270 RETURN (r);
18271 }
18272
18273 case POSTDECREMENT_EXPR:
18274 case POSTINCREMENT_EXPR:
18275 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18276 args, complain, in_decl);
18277 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18278 complain|decltype_flag));
18279
18280 case PREDECREMENT_EXPR:
18281 case PREINCREMENT_EXPR:
18282 case NEGATE_EXPR:
18283 case BIT_NOT_EXPR:
18284 case ABS_EXPR:
18285 case TRUTH_NOT_EXPR:
18286 case UNARY_PLUS_EXPR: /* Unary + */
18287 case REALPART_EXPR:
18288 case IMAGPART_EXPR:
18289 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18290 RECUR (TREE_OPERAND (t, 0)),
18291 complain|decltype_flag));
18292
18293 case FIX_TRUNC_EXPR:
18294 gcc_unreachable ();
18295
18296 case ADDR_EXPR:
18297 op1 = TREE_OPERAND (t, 0);
18298 if (TREE_CODE (op1) == LABEL_DECL)
18299 RETURN (finish_label_address_expr (DECL_NAME (op1),
18300 EXPR_LOCATION (op1)));
18301 if (TREE_CODE (op1) == SCOPE_REF)
18302 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18303 /*done=*/true, /*address_p=*/true);
18304 else
18305 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18306 in_decl);
18307 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18308 complain|decltype_flag));
18309
18310 case PLUS_EXPR:
18311 case MINUS_EXPR:
18312 case MULT_EXPR:
18313 case TRUNC_DIV_EXPR:
18314 case CEIL_DIV_EXPR:
18315 case FLOOR_DIV_EXPR:
18316 case ROUND_DIV_EXPR:
18317 case EXACT_DIV_EXPR:
18318 case BIT_AND_EXPR:
18319 case BIT_IOR_EXPR:
18320 case BIT_XOR_EXPR:
18321 case TRUNC_MOD_EXPR:
18322 case FLOOR_MOD_EXPR:
18323 case TRUTH_ANDIF_EXPR:
18324 case TRUTH_ORIF_EXPR:
18325 case TRUTH_AND_EXPR:
18326 case TRUTH_OR_EXPR:
18327 case RSHIFT_EXPR:
18328 case LSHIFT_EXPR:
18329 case RROTATE_EXPR:
18330 case LROTATE_EXPR:
18331 case EQ_EXPR:
18332 case NE_EXPR:
18333 case MAX_EXPR:
18334 case MIN_EXPR:
18335 case LE_EXPR:
18336 case GE_EXPR:
18337 case LT_EXPR:
18338 case GT_EXPR:
18339 case MEMBER_REF:
18340 case DOTSTAR_EXPR:
18341 {
18342 warning_sentinel s1(warn_type_limits);
18343 warning_sentinel s2(warn_div_by_zero);
18344 warning_sentinel s3(warn_logical_op);
18345 warning_sentinel s4(warn_tautological_compare);
18346 tree op0 = RECUR (TREE_OPERAND (t, 0));
18347 tree op1 = RECUR (TREE_OPERAND (t, 1));
18348 tree r = build_x_binary_op
18349 (input_location, TREE_CODE (t),
18350 op0,
18351 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18352 ? ERROR_MARK
18353 : TREE_CODE (TREE_OPERAND (t, 0))),
18354 op1,
18355 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18356 ? ERROR_MARK
18357 : TREE_CODE (TREE_OPERAND (t, 1))),
18358 /*overload=*/NULL,
18359 complain|decltype_flag);
18360 if (EXPR_P (r) && TREE_NO_WARNING (t))
18361 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18362
18363 RETURN (r);
18364 }
18365
18366 case POINTER_PLUS_EXPR:
18367 {
18368 tree op0 = RECUR (TREE_OPERAND (t, 0));
18369 tree op1 = RECUR (TREE_OPERAND (t, 1));
18370 RETURN (fold_build_pointer_plus (op0, op1));
18371 }
18372
18373 case SCOPE_REF:
18374 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18375 /*address_p=*/false));
18376 case ARRAY_REF:
18377 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18378 args, complain, in_decl);
18379 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18380 RECUR (TREE_OPERAND (t, 1)),
18381 complain|decltype_flag));
18382
18383 case SIZEOF_EXPR:
18384 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18385 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18386 RETURN (tsubst_copy (t, args, complain, in_decl));
18387 /* Fall through */
18388
18389 case ALIGNOF_EXPR:
18390 {
18391 tree r;
18392
18393 op1 = TREE_OPERAND (t, 0);
18394 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18395 op1 = TREE_TYPE (op1);
18396 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18397 && ALIGNOF_EXPR_STD_P (t));
18398 if (!args)
18399 {
18400 /* When there are no ARGS, we are trying to evaluate a
18401 non-dependent expression from the parser. Trying to do
18402 the substitutions may not work. */
18403 if (!TYPE_P (op1))
18404 op1 = TREE_TYPE (op1);
18405 }
18406 else
18407 {
18408 ++cp_unevaluated_operand;
18409 ++c_inhibit_evaluation_warnings;
18410 if (TYPE_P (op1))
18411 op1 = tsubst (op1, args, complain, in_decl);
18412 else
18413 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18414 /*function_p=*/false,
18415 /*integral_constant_expression_p=*/
18416 false);
18417 --cp_unevaluated_operand;
18418 --c_inhibit_evaluation_warnings;
18419 }
18420 if (TYPE_P (op1))
18421 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18422 complain & tf_error);
18423 else
18424 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18425 complain & tf_error);
18426 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18427 {
18428 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18429 {
18430 if (!processing_template_decl && TYPE_P (op1))
18431 {
18432 r = build_min (SIZEOF_EXPR, size_type_node,
18433 build1 (NOP_EXPR, op1, error_mark_node));
18434 SIZEOF_EXPR_TYPE_P (r) = 1;
18435 }
18436 else
18437 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18438 TREE_SIDE_EFFECTS (r) = 0;
18439 TREE_READONLY (r) = 1;
18440 }
18441 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18442 }
18443 RETURN (r);
18444 }
18445
18446 case AT_ENCODE_EXPR:
18447 {
18448 op1 = TREE_OPERAND (t, 0);
18449 ++cp_unevaluated_operand;
18450 ++c_inhibit_evaluation_warnings;
18451 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18452 /*function_p=*/false,
18453 /*integral_constant_expression_p=*/false);
18454 --cp_unevaluated_operand;
18455 --c_inhibit_evaluation_warnings;
18456 RETURN (objc_build_encode_expr (op1));
18457 }
18458
18459 case NOEXCEPT_EXPR:
18460 op1 = TREE_OPERAND (t, 0);
18461 ++cp_unevaluated_operand;
18462 ++c_inhibit_evaluation_warnings;
18463 ++cp_noexcept_operand;
18464 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18465 /*function_p=*/false,
18466 /*integral_constant_expression_p=*/false);
18467 --cp_unevaluated_operand;
18468 --c_inhibit_evaluation_warnings;
18469 --cp_noexcept_operand;
18470 RETURN (finish_noexcept_expr (op1, complain));
18471
18472 case MODOP_EXPR:
18473 {
18474 warning_sentinel s(warn_div_by_zero);
18475 tree lhs = RECUR (TREE_OPERAND (t, 0));
18476 tree rhs = RECUR (TREE_OPERAND (t, 2));
18477 tree r = build_x_modify_expr
18478 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18479 complain|decltype_flag);
18480 /* TREE_NO_WARNING must be set if either the expression was
18481 parenthesized or it uses an operator such as >>= rather
18482 than plain assignment. In the former case, it was already
18483 set and must be copied. In the latter case,
18484 build_x_modify_expr sets it and it must not be reset
18485 here. */
18486 if (TREE_NO_WARNING (t))
18487 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18488
18489 RETURN (r);
18490 }
18491
18492 case ARROW_EXPR:
18493 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18494 args, complain, in_decl);
18495 /* Remember that there was a reference to this entity. */
18496 if (DECL_P (op1)
18497 && !mark_used (op1, complain) && !(complain & tf_error))
18498 RETURN (error_mark_node);
18499 RETURN (build_x_arrow (input_location, op1, complain));
18500
18501 case NEW_EXPR:
18502 {
18503 tree placement = RECUR (TREE_OPERAND (t, 0));
18504 tree init = RECUR (TREE_OPERAND (t, 3));
18505 vec<tree, va_gc> *placement_vec;
18506 vec<tree, va_gc> *init_vec;
18507 tree ret;
18508
18509 if (placement == NULL_TREE)
18510 placement_vec = NULL;
18511 else
18512 {
18513 placement_vec = make_tree_vector ();
18514 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18515 vec_safe_push (placement_vec, TREE_VALUE (placement));
18516 }
18517
18518 /* If there was an initializer in the original tree, but it
18519 instantiated to an empty list, then we should pass a
18520 non-NULL empty vector to tell build_new that it was an
18521 empty initializer() rather than no initializer. This can
18522 only happen when the initializer is a pack expansion whose
18523 parameter packs are of length zero. */
18524 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18525 init_vec = NULL;
18526 else
18527 {
18528 init_vec = make_tree_vector ();
18529 if (init == void_node)
18530 gcc_assert (init_vec != NULL);
18531 else
18532 {
18533 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18534 vec_safe_push (init_vec, TREE_VALUE (init));
18535 }
18536 }
18537
18538 /* Avoid passing an enclosing decl to valid_array_size_p. */
18539 in_decl = NULL_TREE;
18540
18541 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18542 tree op2 = RECUR (TREE_OPERAND (t, 2));
18543 ret = build_new (&placement_vec, op1, op2, &init_vec,
18544 NEW_EXPR_USE_GLOBAL (t),
18545 complain);
18546
18547 if (placement_vec != NULL)
18548 release_tree_vector (placement_vec);
18549 if (init_vec != NULL)
18550 release_tree_vector (init_vec);
18551
18552 RETURN (ret);
18553 }
18554
18555 case DELETE_EXPR:
18556 {
18557 tree op0 = RECUR (TREE_OPERAND (t, 0));
18558 tree op1 = RECUR (TREE_OPERAND (t, 1));
18559 RETURN (delete_sanity (op0, op1,
18560 DELETE_EXPR_USE_VEC (t),
18561 DELETE_EXPR_USE_GLOBAL (t),
18562 complain));
18563 }
18564
18565 case COMPOUND_EXPR:
18566 {
18567 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18568 complain & ~tf_decltype, in_decl,
18569 /*function_p=*/false,
18570 integral_constant_expression_p);
18571 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18572 op0,
18573 RECUR (TREE_OPERAND (t, 1)),
18574 complain|decltype_flag));
18575 }
18576
18577 case CALL_EXPR:
18578 {
18579 tree function;
18580 vec<tree, va_gc> *call_args;
18581 unsigned int nargs, i;
18582 bool qualified_p;
18583 bool koenig_p;
18584 tree ret;
18585
18586 function = CALL_EXPR_FN (t);
18587 /* Internal function with no arguments. */
18588 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18589 RETURN (t);
18590
18591 /* When we parsed the expression, we determined whether or
18592 not Koenig lookup should be performed. */
18593 koenig_p = KOENIG_LOOKUP_P (t);
18594 if (function == NULL_TREE)
18595 {
18596 koenig_p = false;
18597 qualified_p = false;
18598 }
18599 else if (TREE_CODE (function) == SCOPE_REF)
18600 {
18601 qualified_p = true;
18602 function = tsubst_qualified_id (function, args, complain, in_decl,
18603 /*done=*/false,
18604 /*address_p=*/false);
18605 }
18606 else if (koenig_p && identifier_p (function))
18607 {
18608 /* Do nothing; calling tsubst_copy_and_build on an identifier
18609 would incorrectly perform unqualified lookup again.
18610
18611 Note that we can also have an IDENTIFIER_NODE if the earlier
18612 unqualified lookup found a member function; in that case
18613 koenig_p will be false and we do want to do the lookup
18614 again to find the instantiated member function.
18615
18616 FIXME but doing that causes c++/15272, so we need to stop
18617 using IDENTIFIER_NODE in that situation. */
18618 qualified_p = false;
18619 }
18620 else
18621 {
18622 if (TREE_CODE (function) == COMPONENT_REF)
18623 {
18624 tree op = TREE_OPERAND (function, 1);
18625
18626 qualified_p = (TREE_CODE (op) == SCOPE_REF
18627 || (BASELINK_P (op)
18628 && BASELINK_QUALIFIED_P (op)));
18629 }
18630 else
18631 qualified_p = false;
18632
18633 if (TREE_CODE (function) == ADDR_EXPR
18634 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18635 /* Avoid error about taking the address of a constructor. */
18636 function = TREE_OPERAND (function, 0);
18637
18638 function = tsubst_copy_and_build (function, args, complain,
18639 in_decl,
18640 !qualified_p,
18641 integral_constant_expression_p);
18642
18643 if (BASELINK_P (function))
18644 qualified_p = true;
18645 }
18646
18647 nargs = call_expr_nargs (t);
18648 call_args = make_tree_vector ();
18649 for (i = 0; i < nargs; ++i)
18650 {
18651 tree arg = CALL_EXPR_ARG (t, i);
18652
18653 if (!PACK_EXPANSION_P (arg))
18654 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18655 else
18656 {
18657 /* Expand the pack expansion and push each entry onto
18658 CALL_ARGS. */
18659 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18660 if (TREE_CODE (arg) == TREE_VEC)
18661 {
18662 unsigned int len, j;
18663
18664 len = TREE_VEC_LENGTH (arg);
18665 for (j = 0; j < len; ++j)
18666 {
18667 tree value = TREE_VEC_ELT (arg, j);
18668 if (value != NULL_TREE)
18669 value = convert_from_reference (value);
18670 vec_safe_push (call_args, value);
18671 }
18672 }
18673 else
18674 {
18675 /* A partial substitution. Add one entry. */
18676 vec_safe_push (call_args, arg);
18677 }
18678 }
18679 }
18680
18681 /* We do not perform argument-dependent lookup if normal
18682 lookup finds a non-function, in accordance with the
18683 expected resolution of DR 218. */
18684 if (koenig_p
18685 && ((is_overloaded_fn (function)
18686 /* If lookup found a member function, the Koenig lookup is
18687 not appropriate, even if an unqualified-name was used
18688 to denote the function. */
18689 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18690 || identifier_p (function))
18691 /* Only do this when substitution turns a dependent call
18692 into a non-dependent call. */
18693 && type_dependent_expression_p_push (t)
18694 && !any_type_dependent_arguments_p (call_args))
18695 function = perform_koenig_lookup (function, call_args, tf_none);
18696
18697 if (function != NULL_TREE
18698 && identifier_p (function)
18699 && !any_type_dependent_arguments_p (call_args))
18700 {
18701 if (koenig_p && (complain & tf_warning_or_error))
18702 {
18703 /* For backwards compatibility and good diagnostics, try
18704 the unqualified lookup again if we aren't in SFINAE
18705 context. */
18706 tree unq = (tsubst_copy_and_build
18707 (function, args, complain, in_decl, true,
18708 integral_constant_expression_p));
18709 if (unq == error_mark_node)
18710 {
18711 release_tree_vector (call_args);
18712 RETURN (error_mark_node);
18713 }
18714
18715 if (unq != function)
18716 {
18717 /* In a lambda fn, we have to be careful to not
18718 introduce new this captures. Legacy code can't
18719 be using lambdas anyway, so it's ok to be
18720 stricter. */
18721 bool in_lambda = (current_class_type
18722 && LAMBDA_TYPE_P (current_class_type));
18723 char const *const msg
18724 = G_("%qD was not declared in this scope, "
18725 "and no declarations were found by "
18726 "argument-dependent lookup at the point "
18727 "of instantiation");
18728
18729 bool diag = true;
18730 if (in_lambda)
18731 error_at (cp_expr_loc_or_loc (t, input_location),
18732 msg, function);
18733 else
18734 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18735 msg, function);
18736 if (diag)
18737 {
18738 tree fn = unq;
18739
18740 if (INDIRECT_REF_P (fn))
18741 fn = TREE_OPERAND (fn, 0);
18742 if (is_overloaded_fn (fn))
18743 fn = get_first_fn (fn);
18744
18745 if (!DECL_P (fn))
18746 /* Can't say anything more. */;
18747 else if (DECL_CLASS_SCOPE_P (fn))
18748 {
18749 location_t loc = cp_expr_loc_or_loc (t,
18750 input_location);
18751 inform (loc,
18752 "declarations in dependent base %qT are "
18753 "not found by unqualified lookup",
18754 DECL_CLASS_CONTEXT (fn));
18755 if (current_class_ptr)
18756 inform (loc,
18757 "use %<this->%D%> instead", function);
18758 else
18759 inform (loc,
18760 "use %<%T::%D%> instead",
18761 current_class_name, function);
18762 }
18763 else
18764 inform (DECL_SOURCE_LOCATION (fn),
18765 "%qD declared here, later in the "
18766 "translation unit", fn);
18767 if (in_lambda)
18768 {
18769 release_tree_vector (call_args);
18770 RETURN (error_mark_node);
18771 }
18772 }
18773
18774 function = unq;
18775 }
18776 }
18777 if (identifier_p (function))
18778 {
18779 if (complain & tf_error)
18780 unqualified_name_lookup_error (function);
18781 release_tree_vector (call_args);
18782 RETURN (error_mark_node);
18783 }
18784 }
18785
18786 /* Remember that there was a reference to this entity. */
18787 if (function != NULL_TREE
18788 && DECL_P (function)
18789 && !mark_used (function, complain) && !(complain & tf_error))
18790 {
18791 release_tree_vector (call_args);
18792 RETURN (error_mark_node);
18793 }
18794
18795 /* Put back tf_decltype for the actual call. */
18796 complain |= decltype_flag;
18797
18798 if (function == NULL_TREE)
18799 switch (CALL_EXPR_IFN (t))
18800 {
18801 case IFN_LAUNDER:
18802 gcc_assert (nargs == 1);
18803 if (vec_safe_length (call_args) != 1)
18804 {
18805 error_at (cp_expr_loc_or_loc (t, input_location),
18806 "wrong number of arguments to "
18807 "%<__builtin_launder%>");
18808 ret = error_mark_node;
18809 }
18810 else
18811 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
18812 input_location),
18813 (*call_args)[0], complain);
18814 break;
18815
18816 case IFN_VEC_CONVERT:
18817 gcc_assert (nargs == 1);
18818 if (vec_safe_length (call_args) != 1)
18819 {
18820 error_at (cp_expr_loc_or_loc (t, input_location),
18821 "wrong number of arguments to "
18822 "%<__builtin_convertvector%>");
18823 ret = error_mark_node;
18824 break;
18825 }
18826 ret = cp_build_vec_convert ((*call_args)[0], input_location,
18827 tsubst (TREE_TYPE (t), args,
18828 complain, in_decl),
18829 complain);
18830 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
18831 {
18832 release_tree_vector (call_args);
18833 RETURN (ret);
18834 }
18835 break;
18836
18837 default:
18838 /* Unsupported internal function with arguments. */
18839 gcc_unreachable ();
18840 }
18841 else if (TREE_CODE (function) == OFFSET_REF
18842 || TREE_CODE (function) == DOTSTAR_EXPR
18843 || TREE_CODE (function) == MEMBER_REF)
18844 ret = build_offset_ref_call_from_tree (function, &call_args,
18845 complain);
18846 else if (TREE_CODE (function) == COMPONENT_REF)
18847 {
18848 tree instance = TREE_OPERAND (function, 0);
18849 tree fn = TREE_OPERAND (function, 1);
18850
18851 if (processing_template_decl
18852 && (type_dependent_expression_p (instance)
18853 || (!BASELINK_P (fn)
18854 && TREE_CODE (fn) != FIELD_DECL)
18855 || type_dependent_expression_p (fn)
18856 || any_type_dependent_arguments_p (call_args)))
18857 ret = build_min_nt_call_vec (function, call_args);
18858 else if (!BASELINK_P (fn))
18859 ret = finish_call_expr (function, &call_args,
18860 /*disallow_virtual=*/false,
18861 /*koenig_p=*/false,
18862 complain);
18863 else
18864 ret = (build_new_method_call
18865 (instance, fn,
18866 &call_args, NULL_TREE,
18867 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18868 /*fn_p=*/NULL,
18869 complain));
18870 }
18871 else
18872 ret = finish_call_expr (function, &call_args,
18873 /*disallow_virtual=*/qualified_p,
18874 koenig_p,
18875 complain);
18876
18877 release_tree_vector (call_args);
18878
18879 if (ret != error_mark_node)
18880 {
18881 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18882 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18883 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18884 bool thk = CALL_FROM_THUNK_P (t);
18885 if (op || ord || rev || thk)
18886 {
18887 function = extract_call_expr (ret);
18888 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18889 CALL_EXPR_ORDERED_ARGS (function) = ord;
18890 CALL_EXPR_REVERSE_ARGS (function) = rev;
18891 if (thk)
18892 {
18893 if (TREE_CODE (function) == CALL_EXPR)
18894 CALL_FROM_THUNK_P (function) = true;
18895 else
18896 AGGR_INIT_FROM_THUNK_P (function) = true;
18897 /* The thunk location is not interesting. */
18898 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18899 }
18900 }
18901 }
18902
18903 RETURN (ret);
18904 }
18905
18906 case COND_EXPR:
18907 {
18908 tree cond = RECUR (TREE_OPERAND (t, 0));
18909 cond = mark_rvalue_use (cond);
18910 tree folded_cond = fold_non_dependent_expr (cond, complain);
18911 tree exp1, exp2;
18912
18913 if (TREE_CODE (folded_cond) == INTEGER_CST)
18914 {
18915 if (integer_zerop (folded_cond))
18916 {
18917 ++c_inhibit_evaluation_warnings;
18918 exp1 = RECUR (TREE_OPERAND (t, 1));
18919 --c_inhibit_evaluation_warnings;
18920 exp2 = RECUR (TREE_OPERAND (t, 2));
18921 }
18922 else
18923 {
18924 exp1 = RECUR (TREE_OPERAND (t, 1));
18925 ++c_inhibit_evaluation_warnings;
18926 exp2 = RECUR (TREE_OPERAND (t, 2));
18927 --c_inhibit_evaluation_warnings;
18928 }
18929 cond = folded_cond;
18930 }
18931 else
18932 {
18933 exp1 = RECUR (TREE_OPERAND (t, 1));
18934 exp2 = RECUR (TREE_OPERAND (t, 2));
18935 }
18936
18937 warning_sentinel s(warn_duplicated_branches);
18938 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18939 cond, exp1, exp2, complain));
18940 }
18941
18942 case PSEUDO_DTOR_EXPR:
18943 {
18944 tree op0 = RECUR (TREE_OPERAND (t, 0));
18945 tree op1 = RECUR (TREE_OPERAND (t, 1));
18946 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18947 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18948 input_location));
18949 }
18950
18951 case TREE_LIST:
18952 {
18953 tree purpose, value, chain;
18954
18955 if (t == void_list_node)
18956 RETURN (t);
18957
18958 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18959 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18960 {
18961 /* We have pack expansions, so expand those and
18962 create a new list out of it. */
18963 tree purposevec = NULL_TREE;
18964 tree valuevec = NULL_TREE;
18965 tree chain;
18966 int i, len = -1;
18967
18968 /* Expand the argument expressions. */
18969 if (TREE_PURPOSE (t))
18970 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18971 complain, in_decl);
18972 if (TREE_VALUE (t))
18973 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18974 complain, in_decl);
18975
18976 /* Build the rest of the list. */
18977 chain = TREE_CHAIN (t);
18978 if (chain && chain != void_type_node)
18979 chain = RECUR (chain);
18980
18981 /* Determine the number of arguments. */
18982 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18983 {
18984 len = TREE_VEC_LENGTH (purposevec);
18985 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18986 }
18987 else if (TREE_CODE (valuevec) == TREE_VEC)
18988 len = TREE_VEC_LENGTH (valuevec);
18989 else
18990 {
18991 /* Since we only performed a partial substitution into
18992 the argument pack, we only RETURN (a single list
18993 node. */
18994 if (purposevec == TREE_PURPOSE (t)
18995 && valuevec == TREE_VALUE (t)
18996 && chain == TREE_CHAIN (t))
18997 RETURN (t);
18998
18999 RETURN (tree_cons (purposevec, valuevec, chain));
19000 }
19001
19002 /* Convert the argument vectors into a TREE_LIST */
19003 i = len;
19004 while (i > 0)
19005 {
19006 /* Grab the Ith values. */
19007 i--;
19008 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19009 : NULL_TREE;
19010 value
19011 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19012 : NULL_TREE;
19013
19014 /* Build the list (backwards). */
19015 chain = tree_cons (purpose, value, chain);
19016 }
19017
19018 RETURN (chain);
19019 }
19020
19021 purpose = TREE_PURPOSE (t);
19022 if (purpose)
19023 purpose = RECUR (purpose);
19024 value = TREE_VALUE (t);
19025 if (value)
19026 value = RECUR (value);
19027 chain = TREE_CHAIN (t);
19028 if (chain && chain != void_type_node)
19029 chain = RECUR (chain);
19030 if (purpose == TREE_PURPOSE (t)
19031 && value == TREE_VALUE (t)
19032 && chain == TREE_CHAIN (t))
19033 RETURN (t);
19034 RETURN (tree_cons (purpose, value, chain));
19035 }
19036
19037 case COMPONENT_REF:
19038 {
19039 tree object;
19040 tree object_type;
19041 tree member;
19042 tree r;
19043
19044 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19045 args, complain, in_decl);
19046 /* Remember that there was a reference to this entity. */
19047 if (DECL_P (object)
19048 && !mark_used (object, complain) && !(complain & tf_error))
19049 RETURN (error_mark_node);
19050 object_type = TREE_TYPE (object);
19051
19052 member = TREE_OPERAND (t, 1);
19053 if (BASELINK_P (member))
19054 member = tsubst_baselink (member,
19055 non_reference (TREE_TYPE (object)),
19056 args, complain, in_decl);
19057 else
19058 member = tsubst_copy (member, args, complain, in_decl);
19059 if (member == error_mark_node)
19060 RETURN (error_mark_node);
19061
19062 if (TREE_CODE (member) == FIELD_DECL)
19063 {
19064 r = finish_non_static_data_member (member, object, NULL_TREE);
19065 if (TREE_CODE (r) == COMPONENT_REF)
19066 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19067 RETURN (r);
19068 }
19069 else if (type_dependent_expression_p (object))
19070 /* We can't do much here. */;
19071 else if (!CLASS_TYPE_P (object_type))
19072 {
19073 if (scalarish_type_p (object_type))
19074 {
19075 tree s = NULL_TREE;
19076 tree dtor = member;
19077
19078 if (TREE_CODE (dtor) == SCOPE_REF)
19079 {
19080 s = TREE_OPERAND (dtor, 0);
19081 dtor = TREE_OPERAND (dtor, 1);
19082 }
19083 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19084 {
19085 dtor = TREE_OPERAND (dtor, 0);
19086 if (TYPE_P (dtor))
19087 RETURN (finish_pseudo_destructor_expr
19088 (object, s, dtor, input_location));
19089 }
19090 }
19091 }
19092 else if (TREE_CODE (member) == SCOPE_REF
19093 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19094 {
19095 /* Lookup the template functions now that we know what the
19096 scope is. */
19097 tree scope = TREE_OPERAND (member, 0);
19098 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19099 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19100 member = lookup_qualified_name (scope, tmpl,
19101 /*is_type_p=*/false,
19102 /*complain=*/false);
19103 if (BASELINK_P (member))
19104 {
19105 BASELINK_FUNCTIONS (member)
19106 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19107 args);
19108 member = (adjust_result_of_qualified_name_lookup
19109 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19110 object_type));
19111 }
19112 else
19113 {
19114 qualified_name_lookup_error (scope, tmpl, member,
19115 input_location);
19116 RETURN (error_mark_node);
19117 }
19118 }
19119 else if (TREE_CODE (member) == SCOPE_REF
19120 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19121 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19122 {
19123 if (complain & tf_error)
19124 {
19125 if (TYPE_P (TREE_OPERAND (member, 0)))
19126 error ("%qT is not a class or namespace",
19127 TREE_OPERAND (member, 0));
19128 else
19129 error ("%qD is not a class or namespace",
19130 TREE_OPERAND (member, 0));
19131 }
19132 RETURN (error_mark_node);
19133 }
19134
19135 r = finish_class_member_access_expr (object, member,
19136 /*template_p=*/false,
19137 complain);
19138 if (TREE_CODE (r) == COMPONENT_REF)
19139 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19140 RETURN (r);
19141 }
19142
19143 case THROW_EXPR:
19144 RETURN (build_throw
19145 (RECUR (TREE_OPERAND (t, 0))));
19146
19147 case CONSTRUCTOR:
19148 {
19149 vec<constructor_elt, va_gc> *n;
19150 constructor_elt *ce;
19151 unsigned HOST_WIDE_INT idx;
19152 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19153 bool process_index_p;
19154 int newlen;
19155 bool need_copy_p = false;
19156 tree r;
19157
19158 if (type == error_mark_node)
19159 RETURN (error_mark_node);
19160
19161 /* We do not want to process the index of aggregate
19162 initializers as they are identifier nodes which will be
19163 looked up by digest_init. */
19164 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19165
19166 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19167 newlen = vec_safe_length (n);
19168 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19169 {
19170 if (ce->index && process_index_p
19171 /* An identifier index is looked up in the type
19172 being initialized, not the current scope. */
19173 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19174 ce->index = RECUR (ce->index);
19175
19176 if (PACK_EXPANSION_P (ce->value))
19177 {
19178 /* Substitute into the pack expansion. */
19179 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19180 in_decl);
19181
19182 if (ce->value == error_mark_node
19183 || PACK_EXPANSION_P (ce->value))
19184 ;
19185 else if (TREE_VEC_LENGTH (ce->value) == 1)
19186 /* Just move the argument into place. */
19187 ce->value = TREE_VEC_ELT (ce->value, 0);
19188 else
19189 {
19190 /* Update the length of the final CONSTRUCTOR
19191 arguments vector, and note that we will need to
19192 copy.*/
19193 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19194 need_copy_p = true;
19195 }
19196 }
19197 else
19198 ce->value = RECUR (ce->value);
19199 }
19200
19201 if (need_copy_p)
19202 {
19203 vec<constructor_elt, va_gc> *old_n = n;
19204
19205 vec_alloc (n, newlen);
19206 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19207 {
19208 if (TREE_CODE (ce->value) == TREE_VEC)
19209 {
19210 int i, len = TREE_VEC_LENGTH (ce->value);
19211 for (i = 0; i < len; ++i)
19212 CONSTRUCTOR_APPEND_ELT (n, 0,
19213 TREE_VEC_ELT (ce->value, i));
19214 }
19215 else
19216 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19217 }
19218 }
19219
19220 r = build_constructor (init_list_type_node, n);
19221 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19222
19223 if (TREE_HAS_CONSTRUCTOR (t))
19224 {
19225 fcl_t cl = fcl_functional;
19226 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19227 cl = fcl_c99;
19228 RETURN (finish_compound_literal (type, r, complain, cl));
19229 }
19230
19231 TREE_TYPE (r) = type;
19232 RETURN (r);
19233 }
19234
19235 case TYPEID_EXPR:
19236 {
19237 tree operand_0 = TREE_OPERAND (t, 0);
19238 if (TYPE_P (operand_0))
19239 {
19240 operand_0 = tsubst (operand_0, args, complain, in_decl);
19241 RETURN (get_typeid (operand_0, complain));
19242 }
19243 else
19244 {
19245 operand_0 = RECUR (operand_0);
19246 RETURN (build_typeid (operand_0, complain));
19247 }
19248 }
19249
19250 case VAR_DECL:
19251 if (!args)
19252 RETURN (t);
19253 /* Fall through */
19254
19255 case PARM_DECL:
19256 {
19257 tree r = tsubst_copy (t, args, complain, in_decl);
19258 /* ??? We're doing a subset of finish_id_expression here. */
19259 if (VAR_P (r)
19260 && !processing_template_decl
19261 && !cp_unevaluated_operand
19262 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
19263 && CP_DECL_THREAD_LOCAL_P (r))
19264 {
19265 if (tree wrap = get_tls_wrapper_fn (r))
19266 /* Replace an evaluated use of the thread_local variable with
19267 a call to its wrapper. */
19268 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
19269 }
19270 else if (outer_automatic_var_p (r))
19271 r = process_outer_var_ref (r, complain);
19272
19273 if (!TYPE_REF_P (TREE_TYPE (t)))
19274 /* If the original type was a reference, we'll be wrapped in
19275 the appropriate INDIRECT_REF. */
19276 r = convert_from_reference (r);
19277 RETURN (r);
19278 }
19279
19280 case VA_ARG_EXPR:
19281 {
19282 tree op0 = RECUR (TREE_OPERAND (t, 0));
19283 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19284 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19285 }
19286
19287 case OFFSETOF_EXPR:
19288 {
19289 tree object_ptr
19290 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19291 in_decl, /*function_p=*/false,
19292 /*integral_constant_expression_p=*/false);
19293 RETURN (finish_offsetof (object_ptr,
19294 RECUR (TREE_OPERAND (t, 0)),
19295 EXPR_LOCATION (t)));
19296 }
19297
19298 case ADDRESSOF_EXPR:
19299 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19300 RECUR (TREE_OPERAND (t, 0)), complain));
19301
19302 case TRAIT_EXPR:
19303 {
19304 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19305 complain, in_decl);
19306
19307 tree type2 = TRAIT_EXPR_TYPE2 (t);
19308 if (type2 && TREE_CODE (type2) == TREE_LIST)
19309 type2 = RECUR (type2);
19310 else if (type2)
19311 type2 = tsubst (type2, args, complain, in_decl);
19312
19313 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19314 }
19315
19316 case STMT_EXPR:
19317 {
19318 tree old_stmt_expr = cur_stmt_expr;
19319 tree stmt_expr = begin_stmt_expr ();
19320
19321 cur_stmt_expr = stmt_expr;
19322 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19323 integral_constant_expression_p);
19324 stmt_expr = finish_stmt_expr (stmt_expr, false);
19325 cur_stmt_expr = old_stmt_expr;
19326
19327 /* If the resulting list of expression statement is empty,
19328 fold it further into void_node. */
19329 if (empty_expr_stmt_p (stmt_expr))
19330 stmt_expr = void_node;
19331
19332 RETURN (stmt_expr);
19333 }
19334
19335 case LAMBDA_EXPR:
19336 {
19337 if (complain & tf_partial)
19338 {
19339 /* We don't have a full set of template arguments yet; don't touch
19340 the lambda at all. */
19341 gcc_assert (processing_template_decl);
19342 return t;
19343 }
19344 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19345
19346 RETURN (build_lambda_object (r));
19347 }
19348
19349 case TARGET_EXPR:
19350 /* We can get here for a constant initializer of non-dependent type.
19351 FIXME stop folding in cp_parser_initializer_clause. */
19352 {
19353 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19354 complain);
19355 RETURN (r);
19356 }
19357
19358 case TRANSACTION_EXPR:
19359 RETURN (tsubst_expr(t, args, complain, in_decl,
19360 integral_constant_expression_p));
19361
19362 case PAREN_EXPR:
19363 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19364
19365 case VEC_PERM_EXPR:
19366 {
19367 tree op0 = RECUR (TREE_OPERAND (t, 0));
19368 tree op1 = RECUR (TREE_OPERAND (t, 1));
19369 tree op2 = RECUR (TREE_OPERAND (t, 2));
19370 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19371 complain));
19372 }
19373
19374 case REQUIRES_EXPR:
19375 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19376
19377 case NON_LVALUE_EXPR:
19378 case VIEW_CONVERT_EXPR:
19379 if (location_wrapper_p (t))
19380 /* We need to do this here as well as in tsubst_copy so we get the
19381 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19382 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19383 EXPR_LOCATION (t)));
19384 /* fallthrough. */
19385
19386 default:
19387 /* Handle Objective-C++ constructs, if appropriate. */
19388 {
19389 tree subst
19390 = objcp_tsubst_copy_and_build (t, args, complain,
19391 in_decl, /*function_p=*/false);
19392 if (subst)
19393 RETURN (subst);
19394 }
19395 RETURN (tsubst_copy (t, args, complain, in_decl));
19396 }
19397
19398 #undef RECUR
19399 #undef RETURN
19400 out:
19401 input_location = loc;
19402 return retval;
19403 }
19404
19405 /* Verify that the instantiated ARGS are valid. For type arguments,
19406 make sure that the type's linkage is ok. For non-type arguments,
19407 make sure they are constants if they are integral or enumerations.
19408 Emit an error under control of COMPLAIN, and return TRUE on error. */
19409
19410 static bool
19411 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19412 {
19413 if (dependent_template_arg_p (t))
19414 return false;
19415 if (ARGUMENT_PACK_P (t))
19416 {
19417 tree vec = ARGUMENT_PACK_ARGS (t);
19418 int len = TREE_VEC_LENGTH (vec);
19419 bool result = false;
19420 int i;
19421
19422 for (i = 0; i < len; ++i)
19423 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19424 result = true;
19425 return result;
19426 }
19427 else if (TYPE_P (t))
19428 {
19429 /* [basic.link]: A name with no linkage (notably, the name
19430 of a class or enumeration declared in a local scope)
19431 shall not be used to declare an entity with linkage.
19432 This implies that names with no linkage cannot be used as
19433 template arguments
19434
19435 DR 757 relaxes this restriction for C++0x. */
19436 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19437 : no_linkage_check (t, /*relaxed_p=*/false));
19438
19439 if (nt)
19440 {
19441 /* DR 488 makes use of a type with no linkage cause
19442 type deduction to fail. */
19443 if (complain & tf_error)
19444 {
19445 if (TYPE_UNNAMED_P (nt))
19446 error ("%qT is/uses unnamed type", t);
19447 else
19448 error ("template argument for %qD uses local type %qT",
19449 tmpl, t);
19450 }
19451 return true;
19452 }
19453 /* In order to avoid all sorts of complications, we do not
19454 allow variably-modified types as template arguments. */
19455 else if (variably_modified_type_p (t, NULL_TREE))
19456 {
19457 if (complain & tf_error)
19458 error ("%qT is a variably modified type", t);
19459 return true;
19460 }
19461 }
19462 /* Class template and alias template arguments should be OK. */
19463 else if (DECL_TYPE_TEMPLATE_P (t))
19464 ;
19465 /* A non-type argument of integral or enumerated type must be a
19466 constant. */
19467 else if (TREE_TYPE (t)
19468 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19469 && !REFERENCE_REF_P (t)
19470 && !TREE_CONSTANT (t))
19471 {
19472 if (complain & tf_error)
19473 error ("integral expression %qE is not constant", t);
19474 return true;
19475 }
19476 return false;
19477 }
19478
19479 static bool
19480 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19481 {
19482 int ix, len = DECL_NTPARMS (tmpl);
19483 bool result = false;
19484
19485 for (ix = 0; ix != len; ix++)
19486 {
19487 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19488 result = true;
19489 }
19490 if (result && (complain & tf_error))
19491 error (" trying to instantiate %qD", tmpl);
19492 return result;
19493 }
19494
19495 /* We're out of SFINAE context now, so generate diagnostics for the access
19496 errors we saw earlier when instantiating D from TMPL and ARGS. */
19497
19498 static void
19499 recheck_decl_substitution (tree d, tree tmpl, tree args)
19500 {
19501 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19502 tree type = TREE_TYPE (pattern);
19503 location_t loc = input_location;
19504
19505 push_access_scope (d);
19506 push_deferring_access_checks (dk_no_deferred);
19507 input_location = DECL_SOURCE_LOCATION (pattern);
19508 tsubst (type, args, tf_warning_or_error, d);
19509 input_location = loc;
19510 pop_deferring_access_checks ();
19511 pop_access_scope (d);
19512 }
19513
19514 /* Instantiate the indicated variable, function, or alias template TMPL with
19515 the template arguments in TARG_PTR. */
19516
19517 static tree
19518 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19519 {
19520 tree targ_ptr = orig_args;
19521 tree fndecl;
19522 tree gen_tmpl;
19523 tree spec;
19524 bool access_ok = true;
19525
19526 if (tmpl == error_mark_node)
19527 return error_mark_node;
19528
19529 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19530
19531 /* If this function is a clone, handle it specially. */
19532 if (DECL_CLONED_FUNCTION_P (tmpl))
19533 {
19534 tree spec;
19535 tree clone;
19536
19537 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19538 DECL_CLONED_FUNCTION. */
19539 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19540 targ_ptr, complain);
19541 if (spec == error_mark_node)
19542 return error_mark_node;
19543
19544 /* Look for the clone. */
19545 FOR_EACH_CLONE (clone, spec)
19546 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19547 return clone;
19548 /* We should always have found the clone by now. */
19549 gcc_unreachable ();
19550 return NULL_TREE;
19551 }
19552
19553 if (targ_ptr == error_mark_node)
19554 return error_mark_node;
19555
19556 /* Check to see if we already have this specialization. */
19557 gen_tmpl = most_general_template (tmpl);
19558 if (TMPL_ARGS_DEPTH (targ_ptr)
19559 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19560 /* targ_ptr only has the innermost template args, so add the outer ones
19561 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19562 the case of a non-dependent call within a template definition). */
19563 targ_ptr = (add_outermost_template_args
19564 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19565 targ_ptr));
19566
19567 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19568 but it doesn't seem to be on the hot path. */
19569 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19570
19571 gcc_assert (tmpl == gen_tmpl
19572 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19573 == spec)
19574 || fndecl == NULL_TREE);
19575
19576 if (spec != NULL_TREE)
19577 {
19578 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19579 {
19580 if (complain & tf_error)
19581 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19582 return error_mark_node;
19583 }
19584 return spec;
19585 }
19586
19587 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19588 complain))
19589 return error_mark_node;
19590
19591 /* We are building a FUNCTION_DECL, during which the access of its
19592 parameters and return types have to be checked. However this
19593 FUNCTION_DECL which is the desired context for access checking
19594 is not built yet. We solve this chicken-and-egg problem by
19595 deferring all checks until we have the FUNCTION_DECL. */
19596 push_deferring_access_checks (dk_deferred);
19597
19598 /* Instantiation of the function happens in the context of the function
19599 template, not the context of the overload resolution we're doing. */
19600 push_to_top_level ();
19601 /* If there are dependent arguments, e.g. because we're doing partial
19602 ordering, make sure processing_template_decl stays set. */
19603 if (uses_template_parms (targ_ptr))
19604 ++processing_template_decl;
19605 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19606 {
19607 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19608 complain, gen_tmpl, true);
19609 push_nested_class (ctx);
19610 }
19611
19612 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19613
19614 fndecl = NULL_TREE;
19615 if (VAR_P (pattern))
19616 {
19617 /* We need to determine if we're using a partial or explicit
19618 specialization now, because the type of the variable could be
19619 different. */
19620 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19621 tree elt = most_specialized_partial_spec (tid, complain);
19622 if (elt == error_mark_node)
19623 pattern = error_mark_node;
19624 else if (elt)
19625 {
19626 tree partial_tmpl = TREE_VALUE (elt);
19627 tree partial_args = TREE_PURPOSE (elt);
19628 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19629 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19630 }
19631 }
19632
19633 /* Substitute template parameters to obtain the specialization. */
19634 if (fndecl == NULL_TREE)
19635 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19636 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19637 pop_nested_class ();
19638 pop_from_top_level ();
19639
19640 if (fndecl == error_mark_node)
19641 {
19642 pop_deferring_access_checks ();
19643 return error_mark_node;
19644 }
19645
19646 /* The DECL_TI_TEMPLATE should always be the immediate parent
19647 template, not the most general template. */
19648 DECL_TI_TEMPLATE (fndecl) = tmpl;
19649 DECL_TI_ARGS (fndecl) = targ_ptr;
19650
19651 /* Now we know the specialization, compute access previously
19652 deferred. Do no access control for inheriting constructors,
19653 as we already checked access for the inherited constructor. */
19654 if (!(flag_new_inheriting_ctors
19655 && DECL_INHERITED_CTOR (fndecl)))
19656 {
19657 push_access_scope (fndecl);
19658 if (!perform_deferred_access_checks (complain))
19659 access_ok = false;
19660 pop_access_scope (fndecl);
19661 }
19662 pop_deferring_access_checks ();
19663
19664 /* If we've just instantiated the main entry point for a function,
19665 instantiate all the alternate entry points as well. We do this
19666 by cloning the instantiation of the main entry point, not by
19667 instantiating the template clones. */
19668 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19669 clone_function_decl (fndecl, /*update_methods=*/false);
19670
19671 if (!access_ok)
19672 {
19673 if (!(complain & tf_error))
19674 {
19675 /* Remember to reinstantiate when we're out of SFINAE so the user
19676 can see the errors. */
19677 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19678 }
19679 return error_mark_node;
19680 }
19681 return fndecl;
19682 }
19683
19684 /* Wrapper for instantiate_template_1. */
19685
19686 tree
19687 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19688 {
19689 tree ret;
19690 timevar_push (TV_TEMPLATE_INST);
19691 ret = instantiate_template_1 (tmpl, orig_args, complain);
19692 timevar_pop (TV_TEMPLATE_INST);
19693 return ret;
19694 }
19695
19696 /* Instantiate the alias template TMPL with ARGS. Also push a template
19697 instantiation level, which instantiate_template doesn't do because
19698 functions and variables have sufficient context established by the
19699 callers. */
19700
19701 static tree
19702 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19703 {
19704 if (tmpl == error_mark_node || args == error_mark_node)
19705 return error_mark_node;
19706 if (!push_tinst_level (tmpl, args))
19707 return error_mark_node;
19708
19709 args =
19710 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19711 args, tmpl, complain,
19712 /*require_all_args=*/true,
19713 /*use_default_args=*/true);
19714
19715 tree r = instantiate_template (tmpl, args, complain);
19716 pop_tinst_level ();
19717
19718 return r;
19719 }
19720
19721 /* PARM is a template parameter pack for FN. Returns true iff
19722 PARM is used in a deducible way in the argument list of FN. */
19723
19724 static bool
19725 pack_deducible_p (tree parm, tree fn)
19726 {
19727 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19728 for (; t; t = TREE_CHAIN (t))
19729 {
19730 tree type = TREE_VALUE (t);
19731 tree packs;
19732 if (!PACK_EXPANSION_P (type))
19733 continue;
19734 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19735 packs; packs = TREE_CHAIN (packs))
19736 if (template_args_equal (TREE_VALUE (packs), parm))
19737 {
19738 /* The template parameter pack is used in a function parameter
19739 pack. If this is the end of the parameter list, the
19740 template parameter pack is deducible. */
19741 if (TREE_CHAIN (t) == void_list_node)
19742 return true;
19743 else
19744 /* Otherwise, not. Well, it could be deduced from
19745 a non-pack parameter, but doing so would end up with
19746 a deduction mismatch, so don't bother. */
19747 return false;
19748 }
19749 }
19750 /* The template parameter pack isn't used in any function parameter
19751 packs, but it might be used deeper, e.g. tuple<Args...>. */
19752 return true;
19753 }
19754
19755 /* Subroutine of fn_type_unification: check non-dependent parms for
19756 convertibility. */
19757
19758 static int
19759 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19760 tree fn, unification_kind_t strict, int flags,
19761 struct conversion **convs, bool explain_p)
19762 {
19763 /* Non-constructor methods need to leave a conversion for 'this', which
19764 isn't included in nargs here. */
19765 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19766 && !DECL_CONSTRUCTOR_P (fn));
19767
19768 for (unsigned ia = 0;
19769 parms && parms != void_list_node && ia < nargs; )
19770 {
19771 tree parm = TREE_VALUE (parms);
19772
19773 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19774 && (!TREE_CHAIN (parms)
19775 || TREE_CHAIN (parms) == void_list_node))
19776 /* For a function parameter pack that occurs at the end of the
19777 parameter-declaration-list, the type A of each remaining
19778 argument of the call is compared with the type P of the
19779 declarator-id of the function parameter pack. */
19780 break;
19781
19782 parms = TREE_CHAIN (parms);
19783
19784 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19785 /* For a function parameter pack that does not occur at the
19786 end of the parameter-declaration-list, the type of the
19787 parameter pack is a non-deduced context. */
19788 continue;
19789
19790 if (!uses_template_parms (parm))
19791 {
19792 tree arg = args[ia];
19793 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19794 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19795
19796 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19797 conv_p, explain_p))
19798 return 1;
19799 }
19800
19801 ++ia;
19802 }
19803
19804 return 0;
19805 }
19806
19807 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19808 NARGS elements of the arguments that are being used when calling
19809 it. TARGS is a vector into which the deduced template arguments
19810 are placed.
19811
19812 Returns either a FUNCTION_DECL for the matching specialization of FN or
19813 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19814 true, diagnostics will be printed to explain why it failed.
19815
19816 If FN is a conversion operator, or we are trying to produce a specific
19817 specialization, RETURN_TYPE is the return type desired.
19818
19819 The EXPLICIT_TARGS are explicit template arguments provided via a
19820 template-id.
19821
19822 The parameter STRICT is one of:
19823
19824 DEDUCE_CALL:
19825 We are deducing arguments for a function call, as in
19826 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19827 deducing arguments for a call to the result of a conversion
19828 function template, as in [over.call.object].
19829
19830 DEDUCE_CONV:
19831 We are deducing arguments for a conversion function, as in
19832 [temp.deduct.conv].
19833
19834 DEDUCE_EXACT:
19835 We are deducing arguments when doing an explicit instantiation
19836 as in [temp.explicit], when determining an explicit specialization
19837 as in [temp.expl.spec], or when taking the address of a function
19838 template, as in [temp.deduct.funcaddr]. */
19839
19840 tree
19841 fn_type_unification (tree fn,
19842 tree explicit_targs,
19843 tree targs,
19844 const tree *args,
19845 unsigned int nargs,
19846 tree return_type,
19847 unification_kind_t strict,
19848 int flags,
19849 struct conversion **convs,
19850 bool explain_p,
19851 bool decltype_p)
19852 {
19853 tree parms;
19854 tree fntype;
19855 tree decl = NULL_TREE;
19856 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19857 bool ok;
19858 static int deduction_depth;
19859 /* type_unification_real will pass back any access checks from default
19860 template argument substitution. */
19861 vec<deferred_access_check, va_gc> *checks = NULL;
19862 /* We don't have all the template args yet. */
19863 bool incomplete = true;
19864
19865 tree orig_fn = fn;
19866 if (flag_new_inheriting_ctors)
19867 fn = strip_inheriting_ctors (fn);
19868
19869 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19870 tree r = error_mark_node;
19871
19872 tree full_targs = targs;
19873 if (TMPL_ARGS_DEPTH (targs)
19874 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19875 full_targs = (add_outermost_template_args
19876 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19877 targs));
19878
19879 if (decltype_p)
19880 complain |= tf_decltype;
19881
19882 /* In C++0x, it's possible to have a function template whose type depends
19883 on itself recursively. This is most obvious with decltype, but can also
19884 occur with enumeration scope (c++/48969). So we need to catch infinite
19885 recursion and reject the substitution at deduction time; this function
19886 will return error_mark_node for any repeated substitution.
19887
19888 This also catches excessive recursion such as when f<N> depends on
19889 f<N-1> across all integers, and returns error_mark_node for all the
19890 substitutions back up to the initial one.
19891
19892 This is, of course, not reentrant. */
19893 if (excessive_deduction_depth)
19894 return error_mark_node;
19895 ++deduction_depth;
19896
19897 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19898
19899 fntype = TREE_TYPE (fn);
19900 if (explicit_targs)
19901 {
19902 /* [temp.deduct]
19903
19904 The specified template arguments must match the template
19905 parameters in kind (i.e., type, nontype, template), and there
19906 must not be more arguments than there are parameters;
19907 otherwise type deduction fails.
19908
19909 Nontype arguments must match the types of the corresponding
19910 nontype template parameters, or must be convertible to the
19911 types of the corresponding nontype parameters as specified in
19912 _temp.arg.nontype_, otherwise type deduction fails.
19913
19914 All references in the function type of the function template
19915 to the corresponding template parameters are replaced by the
19916 specified template argument values. If a substitution in a
19917 template parameter or in the function type of the function
19918 template results in an invalid type, type deduction fails. */
19919 int i, len = TREE_VEC_LENGTH (tparms);
19920 location_t loc = input_location;
19921 incomplete = false;
19922
19923 if (explicit_targs == error_mark_node)
19924 goto fail;
19925
19926 if (TMPL_ARGS_DEPTH (explicit_targs)
19927 < TMPL_ARGS_DEPTH (full_targs))
19928 explicit_targs = add_outermost_template_args (full_targs,
19929 explicit_targs);
19930
19931 /* Adjust any explicit template arguments before entering the
19932 substitution context. */
19933 explicit_targs
19934 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19935 complain,
19936 /*require_all_args=*/false,
19937 /*use_default_args=*/false));
19938 if (explicit_targs == error_mark_node)
19939 goto fail;
19940
19941 /* Substitute the explicit args into the function type. This is
19942 necessary so that, for instance, explicitly declared function
19943 arguments can match null pointed constants. If we were given
19944 an incomplete set of explicit args, we must not do semantic
19945 processing during substitution as we could create partial
19946 instantiations. */
19947 for (i = 0; i < len; i++)
19948 {
19949 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19950 bool parameter_pack = false;
19951 tree targ = TREE_VEC_ELT (explicit_targs, i);
19952
19953 /* Dig out the actual parm. */
19954 if (TREE_CODE (parm) == TYPE_DECL
19955 || TREE_CODE (parm) == TEMPLATE_DECL)
19956 {
19957 parm = TREE_TYPE (parm);
19958 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19959 }
19960 else if (TREE_CODE (parm) == PARM_DECL)
19961 {
19962 parm = DECL_INITIAL (parm);
19963 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19964 }
19965
19966 if (!parameter_pack && targ == NULL_TREE)
19967 /* No explicit argument for this template parameter. */
19968 incomplete = true;
19969
19970 if (parameter_pack && pack_deducible_p (parm, fn))
19971 {
19972 /* Mark the argument pack as "incomplete". We could
19973 still deduce more arguments during unification.
19974 We remove this mark in type_unification_real. */
19975 if (targ)
19976 {
19977 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19978 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19979 = ARGUMENT_PACK_ARGS (targ);
19980 }
19981
19982 /* We have some incomplete argument packs. */
19983 incomplete = true;
19984 }
19985 }
19986
19987 if (incomplete)
19988 {
19989 if (!push_tinst_level (fn, explicit_targs))
19990 {
19991 excessive_deduction_depth = true;
19992 goto fail;
19993 }
19994 ++processing_template_decl;
19995 input_location = DECL_SOURCE_LOCATION (fn);
19996 /* Ignore any access checks; we'll see them again in
19997 instantiate_template and they might have the wrong
19998 access path at this point. */
19999 push_deferring_access_checks (dk_deferred);
20000 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20001 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20002 pop_deferring_access_checks ();
20003 input_location = loc;
20004 --processing_template_decl;
20005 pop_tinst_level ();
20006
20007 if (fntype == error_mark_node)
20008 goto fail;
20009 }
20010
20011 /* Place the explicitly specified arguments in TARGS. */
20012 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20013 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20014 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20015 if (!incomplete && CHECKING_P
20016 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20017 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20018 (targs, NUM_TMPL_ARGS (explicit_targs));
20019 }
20020
20021 if (return_type && strict != DEDUCE_CALL)
20022 {
20023 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20024 new_args[0] = return_type;
20025 memcpy (new_args + 1, args, nargs * sizeof (tree));
20026 args = new_args;
20027 ++nargs;
20028 }
20029
20030 if (!incomplete)
20031 goto deduced;
20032
20033 /* Never do unification on the 'this' parameter. */
20034 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20035
20036 if (return_type && strict == DEDUCE_CALL)
20037 {
20038 /* We're deducing for a call to the result of a template conversion
20039 function. The parms we really want are in return_type. */
20040 if (INDIRECT_TYPE_P (return_type))
20041 return_type = TREE_TYPE (return_type);
20042 parms = TYPE_ARG_TYPES (return_type);
20043 }
20044 else if (return_type)
20045 {
20046 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20047 }
20048
20049 /* We allow incomplete unification without an error message here
20050 because the standard doesn't seem to explicitly prohibit it. Our
20051 callers must be ready to deal with unification failures in any
20052 event. */
20053
20054 /* If we aren't explaining yet, push tinst context so we can see where
20055 any errors (e.g. from class instantiations triggered by instantiation
20056 of default template arguments) come from. If we are explaining, this
20057 context is redundant. */
20058 if (!explain_p && !push_tinst_level (fn, targs))
20059 {
20060 excessive_deduction_depth = true;
20061 goto fail;
20062 }
20063
20064 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20065 full_targs, parms, args, nargs, /*subr=*/0,
20066 strict, &checks, explain_p);
20067 if (!explain_p)
20068 pop_tinst_level ();
20069 if (!ok)
20070 goto fail;
20071
20072 /* Now that we have bindings for all of the template arguments,
20073 ensure that the arguments deduced for the template template
20074 parameters have compatible template parameter lists. We cannot
20075 check this property before we have deduced all template
20076 arguments, because the template parameter types of a template
20077 template parameter might depend on prior template parameters
20078 deduced after the template template parameter. The following
20079 ill-formed example illustrates this issue:
20080
20081 template<typename T, template<T> class C> void f(C<5>, T);
20082
20083 template<int N> struct X {};
20084
20085 void g() {
20086 f(X<5>(), 5l); // error: template argument deduction fails
20087 }
20088
20089 The template parameter list of 'C' depends on the template type
20090 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20091 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20092 time that we deduce 'C'. */
20093 if (!template_template_parm_bindings_ok_p
20094 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20095 {
20096 unify_inconsistent_template_template_parameters (explain_p);
20097 goto fail;
20098 }
20099
20100 /* DR 1391: All parameters have args, now check non-dependent parms for
20101 convertibility. */
20102 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20103 convs, explain_p))
20104 goto fail;
20105
20106 deduced:
20107 /* All is well so far. Now, check:
20108
20109 [temp.deduct]
20110
20111 When all template arguments have been deduced, all uses of
20112 template parameters in nondeduced contexts are replaced with
20113 the corresponding deduced argument values. If the
20114 substitution results in an invalid type, as described above,
20115 type deduction fails. */
20116 if (!push_tinst_level (fn, targs))
20117 {
20118 excessive_deduction_depth = true;
20119 goto fail;
20120 }
20121
20122 /* Also collect access checks from the instantiation. */
20123 reopen_deferring_access_checks (checks);
20124
20125 decl = instantiate_template (fn, targs, complain);
20126
20127 checks = get_deferred_access_checks ();
20128 pop_deferring_access_checks ();
20129
20130 pop_tinst_level ();
20131
20132 if (decl == error_mark_node)
20133 goto fail;
20134
20135 /* Now perform any access checks encountered during substitution. */
20136 push_access_scope (decl);
20137 ok = perform_access_checks (checks, complain);
20138 pop_access_scope (decl);
20139 if (!ok)
20140 goto fail;
20141
20142 /* If we're looking for an exact match, check that what we got
20143 is indeed an exact match. It might not be if some template
20144 parameters are used in non-deduced contexts. But don't check
20145 for an exact match if we have dependent template arguments;
20146 in that case we're doing partial ordering, and we already know
20147 that we have two candidates that will provide the actual type. */
20148 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20149 {
20150 tree substed = TREE_TYPE (decl);
20151 unsigned int i;
20152
20153 tree sarg
20154 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20155 if (return_type)
20156 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20157 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20158 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20159 {
20160 unify_type_mismatch (explain_p, args[i],
20161 TREE_VALUE (sarg));
20162 goto fail;
20163 }
20164 }
20165
20166 /* After doing deduction with the inherited constructor, actually return an
20167 instantiation of the inheriting constructor. */
20168 if (orig_fn != fn)
20169 decl = instantiate_template (orig_fn, targs, complain);
20170
20171 r = decl;
20172
20173 fail:
20174 --deduction_depth;
20175 if (excessive_deduction_depth)
20176 {
20177 if (deduction_depth == 0)
20178 /* Reset once we're all the way out. */
20179 excessive_deduction_depth = false;
20180 }
20181
20182 return r;
20183 }
20184
20185 /* Adjust types before performing type deduction, as described in
20186 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20187 sections are symmetric. PARM is the type of a function parameter
20188 or the return type of the conversion function. ARG is the type of
20189 the argument passed to the call, or the type of the value
20190 initialized with the result of the conversion function.
20191 ARG_EXPR is the original argument expression, which may be null. */
20192
20193 static int
20194 maybe_adjust_types_for_deduction (unification_kind_t strict,
20195 tree* parm,
20196 tree* arg,
20197 tree arg_expr)
20198 {
20199 int result = 0;
20200
20201 switch (strict)
20202 {
20203 case DEDUCE_CALL:
20204 break;
20205
20206 case DEDUCE_CONV:
20207 /* Swap PARM and ARG throughout the remainder of this
20208 function; the handling is precisely symmetric since PARM
20209 will initialize ARG rather than vice versa. */
20210 std::swap (parm, arg);
20211 break;
20212
20213 case DEDUCE_EXACT:
20214 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20215 too, but here handle it by stripping the reference from PARM
20216 rather than by adding it to ARG. */
20217 if (TYPE_REF_P (*parm)
20218 && TYPE_REF_IS_RVALUE (*parm)
20219 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20220 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20221 && TYPE_REF_P (*arg)
20222 && !TYPE_REF_IS_RVALUE (*arg))
20223 *parm = TREE_TYPE (*parm);
20224 /* Nothing else to do in this case. */
20225 return 0;
20226
20227 default:
20228 gcc_unreachable ();
20229 }
20230
20231 if (!TYPE_REF_P (*parm))
20232 {
20233 /* [temp.deduct.call]
20234
20235 If P is not a reference type:
20236
20237 --If A is an array type, the pointer type produced by the
20238 array-to-pointer standard conversion (_conv.array_) is
20239 used in place of A for type deduction; otherwise,
20240
20241 --If A is a function type, the pointer type produced by
20242 the function-to-pointer standard conversion
20243 (_conv.func_) is used in place of A for type deduction;
20244 otherwise,
20245
20246 --If A is a cv-qualified type, the top level
20247 cv-qualifiers of A's type are ignored for type
20248 deduction. */
20249 if (TREE_CODE (*arg) == ARRAY_TYPE)
20250 *arg = build_pointer_type (TREE_TYPE (*arg));
20251 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20252 *arg = build_pointer_type (*arg);
20253 else
20254 *arg = TYPE_MAIN_VARIANT (*arg);
20255 }
20256
20257 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20258 reference to a cv-unqualified template parameter that does not represent a
20259 template parameter of a class template (during class template argument
20260 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20261 an lvalue, the type "lvalue reference to A" is used in place of A for type
20262 deduction. */
20263 if (TYPE_REF_P (*parm)
20264 && TYPE_REF_IS_RVALUE (*parm)
20265 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20266 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20267 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20268 && (arg_expr ? lvalue_p (arg_expr)
20269 /* try_one_overload doesn't provide an arg_expr, but
20270 functions are always lvalues. */
20271 : TREE_CODE (*arg) == FUNCTION_TYPE))
20272 *arg = build_reference_type (*arg);
20273
20274 /* [temp.deduct.call]
20275
20276 If P is a cv-qualified type, the top level cv-qualifiers
20277 of P's type are ignored for type deduction. If P is a
20278 reference type, the type referred to by P is used for
20279 type deduction. */
20280 *parm = TYPE_MAIN_VARIANT (*parm);
20281 if (TYPE_REF_P (*parm))
20282 {
20283 *parm = TREE_TYPE (*parm);
20284 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20285 }
20286
20287 /* DR 322. For conversion deduction, remove a reference type on parm
20288 too (which has been swapped into ARG). */
20289 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20290 *arg = TREE_TYPE (*arg);
20291
20292 return result;
20293 }
20294
20295 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20296 template which doesn't contain any deducible template parameters; check if
20297 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20298 unify_one_argument. */
20299
20300 static int
20301 check_non_deducible_conversion (tree parm, tree arg, int strict,
20302 int flags, struct conversion **conv_p,
20303 bool explain_p)
20304 {
20305 tree type;
20306
20307 if (!TYPE_P (arg))
20308 type = TREE_TYPE (arg);
20309 else
20310 type = arg;
20311
20312 if (same_type_p (parm, type))
20313 return unify_success (explain_p);
20314
20315 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20316 if (strict == DEDUCE_CONV)
20317 {
20318 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20319 return unify_success (explain_p);
20320 }
20321 else if (strict != DEDUCE_EXACT)
20322 {
20323 bool ok = false;
20324 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20325 if (conv_p)
20326 /* Avoid recalculating this in add_function_candidate. */
20327 ok = (*conv_p
20328 = good_conversion (parm, type, conv_arg, flags, complain));
20329 else
20330 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20331 if (ok)
20332 return unify_success (explain_p);
20333 }
20334
20335 if (strict == DEDUCE_EXACT)
20336 return unify_type_mismatch (explain_p, parm, arg);
20337 else
20338 return unify_arg_conversion (explain_p, parm, type, arg);
20339 }
20340
20341 static bool uses_deducible_template_parms (tree type);
20342
20343 /* Returns true iff the expression EXPR is one from which a template
20344 argument can be deduced. In other words, if it's an undecorated
20345 use of a template non-type parameter. */
20346
20347 static bool
20348 deducible_expression (tree expr)
20349 {
20350 /* Strip implicit conversions. */
20351 while (CONVERT_EXPR_P (expr))
20352 expr = TREE_OPERAND (expr, 0);
20353 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20354 }
20355
20356 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20357 deducible way; that is, if it has a max value of <PARM> - 1. */
20358
20359 static bool
20360 deducible_array_bound (tree domain)
20361 {
20362 if (domain == NULL_TREE)
20363 return false;
20364
20365 tree max = TYPE_MAX_VALUE (domain);
20366 if (TREE_CODE (max) != MINUS_EXPR)
20367 return false;
20368
20369 return deducible_expression (TREE_OPERAND (max, 0));
20370 }
20371
20372 /* Returns true iff the template arguments ARGS use a template parameter
20373 in a deducible way. */
20374
20375 static bool
20376 deducible_template_args (tree args)
20377 {
20378 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20379 {
20380 bool deducible;
20381 tree elt = TREE_VEC_ELT (args, i);
20382 if (ARGUMENT_PACK_P (elt))
20383 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20384 else
20385 {
20386 if (PACK_EXPANSION_P (elt))
20387 elt = PACK_EXPANSION_PATTERN (elt);
20388 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20389 deducible = true;
20390 else if (TYPE_P (elt))
20391 deducible = uses_deducible_template_parms (elt);
20392 else
20393 deducible = deducible_expression (elt);
20394 }
20395 if (deducible)
20396 return true;
20397 }
20398 return false;
20399 }
20400
20401 /* Returns true iff TYPE contains any deducible references to template
20402 parameters, as per 14.8.2.5. */
20403
20404 static bool
20405 uses_deducible_template_parms (tree type)
20406 {
20407 if (PACK_EXPANSION_P (type))
20408 type = PACK_EXPANSION_PATTERN (type);
20409
20410 /* T
20411 cv-list T
20412 TT<T>
20413 TT<i>
20414 TT<> */
20415 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20416 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20417 return true;
20418
20419 /* T*
20420 T&
20421 T&& */
20422 if (INDIRECT_TYPE_P (type))
20423 return uses_deducible_template_parms (TREE_TYPE (type));
20424
20425 /* T[integer-constant ]
20426 type [i] */
20427 if (TREE_CODE (type) == ARRAY_TYPE)
20428 return (uses_deducible_template_parms (TREE_TYPE (type))
20429 || deducible_array_bound (TYPE_DOMAIN (type)));
20430
20431 /* T type ::*
20432 type T::*
20433 T T::*
20434 T (type ::*)()
20435 type (T::*)()
20436 type (type ::*)(T)
20437 type (T::*)(T)
20438 T (type ::*)(T)
20439 T (T::*)()
20440 T (T::*)(T) */
20441 if (TYPE_PTRMEM_P (type))
20442 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20443 || (uses_deducible_template_parms
20444 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20445
20446 /* template-name <T> (where template-name refers to a class template)
20447 template-name <i> (where template-name refers to a class template) */
20448 if (CLASS_TYPE_P (type)
20449 && CLASSTYPE_TEMPLATE_INFO (type)
20450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20451 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20452 (CLASSTYPE_TI_ARGS (type)));
20453
20454 /* type (T)
20455 T()
20456 T(T) */
20457 if (TREE_CODE (type) == FUNCTION_TYPE
20458 || TREE_CODE (type) == METHOD_TYPE)
20459 {
20460 if (uses_deducible_template_parms (TREE_TYPE (type)))
20461 return true;
20462 tree parm = TYPE_ARG_TYPES (type);
20463 if (TREE_CODE (type) == METHOD_TYPE)
20464 parm = TREE_CHAIN (parm);
20465 for (; parm; parm = TREE_CHAIN (parm))
20466 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20467 return true;
20468 }
20469
20470 return false;
20471 }
20472
20473 /* Subroutine of type_unification_real and unify_pack_expansion to
20474 handle unification of a single P/A pair. Parameters are as
20475 for those functions. */
20476
20477 static int
20478 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20479 int subr, unification_kind_t strict,
20480 bool explain_p)
20481 {
20482 tree arg_expr = NULL_TREE;
20483 int arg_strict;
20484
20485 if (arg == error_mark_node || parm == error_mark_node)
20486 return unify_invalid (explain_p);
20487 if (arg == unknown_type_node)
20488 /* We can't deduce anything from this, but we might get all the
20489 template args from other function args. */
20490 return unify_success (explain_p);
20491
20492 /* Implicit conversions (Clause 4) will be performed on a function
20493 argument to convert it to the type of the corresponding function
20494 parameter if the parameter type contains no template-parameters that
20495 participate in template argument deduction. */
20496 if (strict != DEDUCE_EXACT
20497 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20498 /* For function parameters with no deducible template parameters,
20499 just return. We'll check non-dependent conversions later. */
20500 return unify_success (explain_p);
20501
20502 switch (strict)
20503 {
20504 case DEDUCE_CALL:
20505 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20506 | UNIFY_ALLOW_MORE_CV_QUAL
20507 | UNIFY_ALLOW_DERIVED);
20508 break;
20509
20510 case DEDUCE_CONV:
20511 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20512 break;
20513
20514 case DEDUCE_EXACT:
20515 arg_strict = UNIFY_ALLOW_NONE;
20516 break;
20517
20518 default:
20519 gcc_unreachable ();
20520 }
20521
20522 /* We only do these transformations if this is the top-level
20523 parameter_type_list in a call or declaration matching; in other
20524 situations (nested function declarators, template argument lists) we
20525 won't be comparing a type to an expression, and we don't do any type
20526 adjustments. */
20527 if (!subr)
20528 {
20529 if (!TYPE_P (arg))
20530 {
20531 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20532 if (type_unknown_p (arg))
20533 {
20534 /* [temp.deduct.type] A template-argument can be
20535 deduced from a pointer to function or pointer
20536 to member function argument if the set of
20537 overloaded functions does not contain function
20538 templates and at most one of a set of
20539 overloaded functions provides a unique
20540 match. */
20541 resolve_overloaded_unification (tparms, targs, parm,
20542 arg, strict,
20543 arg_strict, explain_p);
20544 /* If a unique match was not found, this is a
20545 non-deduced context, so we still succeed. */
20546 return unify_success (explain_p);
20547 }
20548
20549 arg_expr = arg;
20550 arg = unlowered_expr_type (arg);
20551 if (arg == error_mark_node)
20552 return unify_invalid (explain_p);
20553 }
20554
20555 arg_strict |=
20556 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20557 }
20558 else
20559 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20560 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20561 return unify_template_argument_mismatch (explain_p, parm, arg);
20562
20563 /* For deduction from an init-list we need the actual list. */
20564 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20565 arg = arg_expr;
20566 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20567 }
20568
20569 /* for_each_template_parm callback that always returns 0. */
20570
20571 static int
20572 zero_r (tree, void *)
20573 {
20574 return 0;
20575 }
20576
20577 /* for_each_template_parm any_fn callback to handle deduction of a template
20578 type argument from the type of an array bound. */
20579
20580 static int
20581 array_deduction_r (tree t, void *data)
20582 {
20583 tree_pair_p d = (tree_pair_p)data;
20584 tree &tparms = d->purpose;
20585 tree &targs = d->value;
20586
20587 if (TREE_CODE (t) == ARRAY_TYPE)
20588 if (tree dom = TYPE_DOMAIN (t))
20589 if (tree max = TYPE_MAX_VALUE (dom))
20590 {
20591 if (TREE_CODE (max) == MINUS_EXPR)
20592 max = TREE_OPERAND (max, 0);
20593 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20594 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20595 UNIFY_ALLOW_NONE, /*explain*/false);
20596 }
20597
20598 /* Keep walking. */
20599 return 0;
20600 }
20601
20602 /* Try to deduce any not-yet-deduced template type arguments from the type of
20603 an array bound. This is handled separately from unify because 14.8.2.5 says
20604 "The type of a type parameter is only deduced from an array bound if it is
20605 not otherwise deduced." */
20606
20607 static void
20608 try_array_deduction (tree tparms, tree targs, tree parm)
20609 {
20610 tree_pair_s data = { tparms, targs };
20611 hash_set<tree> visited;
20612 for_each_template_parm (parm, zero_r, &data, &visited,
20613 /*nondeduced*/false, array_deduction_r);
20614 }
20615
20616 /* Most parms like fn_type_unification.
20617
20618 If SUBR is 1, we're being called recursively (to unify the
20619 arguments of a function or method parameter of a function
20620 template).
20621
20622 CHECKS is a pointer to a vector of access checks encountered while
20623 substituting default template arguments. */
20624
20625 static int
20626 type_unification_real (tree tparms,
20627 tree full_targs,
20628 tree xparms,
20629 const tree *xargs,
20630 unsigned int xnargs,
20631 int subr,
20632 unification_kind_t strict,
20633 vec<deferred_access_check, va_gc> **checks,
20634 bool explain_p)
20635 {
20636 tree parm, arg;
20637 int i;
20638 int ntparms = TREE_VEC_LENGTH (tparms);
20639 int saw_undeduced = 0;
20640 tree parms;
20641 const tree *args;
20642 unsigned int nargs;
20643 unsigned int ia;
20644
20645 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20646 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20647 gcc_assert (ntparms > 0);
20648
20649 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20650
20651 /* Reset the number of non-defaulted template arguments contained
20652 in TARGS. */
20653 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20654
20655 again:
20656 parms = xparms;
20657 args = xargs;
20658 nargs = xnargs;
20659
20660 ia = 0;
20661 while (parms && parms != void_list_node
20662 && ia < nargs)
20663 {
20664 parm = TREE_VALUE (parms);
20665
20666 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20667 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20668 /* For a function parameter pack that occurs at the end of the
20669 parameter-declaration-list, the type A of each remaining
20670 argument of the call is compared with the type P of the
20671 declarator-id of the function parameter pack. */
20672 break;
20673
20674 parms = TREE_CHAIN (parms);
20675
20676 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20677 /* For a function parameter pack that does not occur at the
20678 end of the parameter-declaration-list, the type of the
20679 parameter pack is a non-deduced context. */
20680 continue;
20681
20682 arg = args[ia];
20683 ++ia;
20684
20685 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20686 explain_p))
20687 return 1;
20688 }
20689
20690 if (parms
20691 && parms != void_list_node
20692 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20693 {
20694 /* Unify the remaining arguments with the pack expansion type. */
20695 tree argvec;
20696 tree parmvec = make_tree_vec (1);
20697
20698 /* Allocate a TREE_VEC and copy in all of the arguments */
20699 argvec = make_tree_vec (nargs - ia);
20700 for (i = 0; ia < nargs; ++ia, ++i)
20701 TREE_VEC_ELT (argvec, i) = args[ia];
20702
20703 /* Copy the parameter into parmvec. */
20704 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20705 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20706 /*subr=*/subr, explain_p))
20707 return 1;
20708
20709 /* Advance to the end of the list of parameters. */
20710 parms = TREE_CHAIN (parms);
20711 }
20712
20713 /* Fail if we've reached the end of the parm list, and more args
20714 are present, and the parm list isn't variadic. */
20715 if (ia < nargs && parms == void_list_node)
20716 return unify_too_many_arguments (explain_p, nargs, ia);
20717 /* Fail if parms are left and they don't have default values and
20718 they aren't all deduced as empty packs (c++/57397). This is
20719 consistent with sufficient_parms_p. */
20720 if (parms && parms != void_list_node
20721 && TREE_PURPOSE (parms) == NULL_TREE)
20722 {
20723 unsigned int count = nargs;
20724 tree p = parms;
20725 bool type_pack_p;
20726 do
20727 {
20728 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20729 if (!type_pack_p)
20730 count++;
20731 p = TREE_CHAIN (p);
20732 }
20733 while (p && p != void_list_node);
20734 if (count != nargs)
20735 return unify_too_few_arguments (explain_p, ia, count,
20736 type_pack_p);
20737 }
20738
20739 if (!subr)
20740 {
20741 tsubst_flags_t complain = (explain_p
20742 ? tf_warning_or_error
20743 : tf_none);
20744 bool tried_array_deduction = (cxx_dialect < cxx17);
20745
20746 for (i = 0; i < ntparms; i++)
20747 {
20748 tree targ = TREE_VEC_ELT (targs, i);
20749 tree tparm = TREE_VEC_ELT (tparms, i);
20750
20751 /* Clear the "incomplete" flags on all argument packs now so that
20752 substituting them into later default arguments works. */
20753 if (targ && ARGUMENT_PACK_P (targ))
20754 {
20755 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20756 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20757 }
20758
20759 if (targ || tparm == error_mark_node)
20760 continue;
20761 tparm = TREE_VALUE (tparm);
20762
20763 if (TREE_CODE (tparm) == TYPE_DECL
20764 && !tried_array_deduction)
20765 {
20766 try_array_deduction (tparms, targs, xparms);
20767 tried_array_deduction = true;
20768 if (TREE_VEC_ELT (targs, i))
20769 continue;
20770 }
20771
20772 /* If this is an undeduced nontype parameter that depends on
20773 a type parameter, try another pass; its type may have been
20774 deduced from a later argument than the one from which
20775 this parameter can be deduced. */
20776 if (TREE_CODE (tparm) == PARM_DECL
20777 && uses_template_parms (TREE_TYPE (tparm))
20778 && saw_undeduced < 2)
20779 {
20780 saw_undeduced = 1;
20781 continue;
20782 }
20783
20784 /* Core issue #226 (C++0x) [temp.deduct]:
20785
20786 If a template argument has not been deduced, its
20787 default template argument, if any, is used.
20788
20789 When we are in C++98 mode, TREE_PURPOSE will either
20790 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20791 to explicitly check cxx_dialect here. */
20792 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20793 /* OK, there is a default argument. Wait until after the
20794 conversion check to do substitution. */
20795 continue;
20796
20797 /* If the type parameter is a parameter pack, then it will
20798 be deduced to an empty parameter pack. */
20799 if (template_parameter_pack_p (tparm))
20800 {
20801 tree arg;
20802
20803 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20804 {
20805 arg = make_node (NONTYPE_ARGUMENT_PACK);
20806 TREE_CONSTANT (arg) = 1;
20807 }
20808 else
20809 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
20810
20811 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
20812
20813 TREE_VEC_ELT (targs, i) = arg;
20814 continue;
20815 }
20816
20817 return unify_parameter_deduction_failure (explain_p, tparm);
20818 }
20819
20820 /* Now substitute into the default template arguments. */
20821 for (i = 0; i < ntparms; i++)
20822 {
20823 tree targ = TREE_VEC_ELT (targs, i);
20824 tree tparm = TREE_VEC_ELT (tparms, i);
20825
20826 if (targ || tparm == error_mark_node)
20827 continue;
20828 tree parm = TREE_VALUE (tparm);
20829 tree arg = TREE_PURPOSE (tparm);
20830 reopen_deferring_access_checks (*checks);
20831 location_t save_loc = input_location;
20832 if (DECL_P (parm))
20833 input_location = DECL_SOURCE_LOCATION (parm);
20834
20835 if (saw_undeduced == 1
20836 && TREE_CODE (parm) == PARM_DECL
20837 && uses_template_parms (TREE_TYPE (parm)))
20838 {
20839 /* The type of this non-type parameter depends on undeduced
20840 parameters. Don't try to use its default argument yet,
20841 since we might deduce an argument for it on the next pass,
20842 but do check whether the arguments we already have cause
20843 substitution failure, so that that happens before we try
20844 later default arguments (78489). */
20845 ++processing_template_decl;
20846 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20847 NULL_TREE);
20848 --processing_template_decl;
20849 if (type == error_mark_node)
20850 arg = error_mark_node;
20851 else
20852 arg = NULL_TREE;
20853 }
20854 else
20855 {
20856 tree substed = NULL_TREE;
20857 if (saw_undeduced == 1 && processing_template_decl == 0)
20858 {
20859 /* First instatiate in template context, in case we still
20860 depend on undeduced template parameters. */
20861 ++processing_template_decl;
20862 substed = tsubst_template_arg (arg, full_targs, complain,
20863 NULL_TREE);
20864 --processing_template_decl;
20865 if (substed != error_mark_node
20866 && !uses_template_parms (substed))
20867 /* We replaced all the tparms, substitute again out of
20868 template context. */
20869 substed = NULL_TREE;
20870 }
20871 if (!substed)
20872 substed = tsubst_template_arg (arg, full_targs, complain,
20873 NULL_TREE);
20874
20875 if (!uses_template_parms (substed))
20876 arg = convert_template_argument (parm, substed, full_targs,
20877 complain, i, NULL_TREE);
20878 else if (saw_undeduced == 1)
20879 arg = NULL_TREE;
20880 else
20881 arg = error_mark_node;
20882 }
20883
20884 input_location = save_loc;
20885 *checks = get_deferred_access_checks ();
20886 pop_deferring_access_checks ();
20887
20888 if (arg == error_mark_node)
20889 return 1;
20890 else if (arg)
20891 {
20892 TREE_VEC_ELT (targs, i) = arg;
20893 /* The position of the first default template argument,
20894 is also the number of non-defaulted arguments in TARGS.
20895 Record that. */
20896 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20897 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20898 }
20899 }
20900
20901 if (saw_undeduced++ == 1)
20902 goto again;
20903 }
20904
20905 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20907
20908 return unify_success (explain_p);
20909 }
20910
20911 /* Subroutine of type_unification_real. Args are like the variables
20912 at the call site. ARG is an overloaded function (or template-id);
20913 we try deducing template args from each of the overloads, and if
20914 only one succeeds, we go with that. Modifies TARGS and returns
20915 true on success. */
20916
20917 static bool
20918 resolve_overloaded_unification (tree tparms,
20919 tree targs,
20920 tree parm,
20921 tree arg,
20922 unification_kind_t strict,
20923 int sub_strict,
20924 bool explain_p)
20925 {
20926 tree tempargs = copy_node (targs);
20927 int good = 0;
20928 tree goodfn = NULL_TREE;
20929 bool addr_p;
20930
20931 if (TREE_CODE (arg) == ADDR_EXPR)
20932 {
20933 arg = TREE_OPERAND (arg, 0);
20934 addr_p = true;
20935 }
20936 else
20937 addr_p = false;
20938
20939 if (TREE_CODE (arg) == COMPONENT_REF)
20940 /* Handle `&x' where `x' is some static or non-static member
20941 function name. */
20942 arg = TREE_OPERAND (arg, 1);
20943
20944 if (TREE_CODE (arg) == OFFSET_REF)
20945 arg = TREE_OPERAND (arg, 1);
20946
20947 /* Strip baselink information. */
20948 if (BASELINK_P (arg))
20949 arg = BASELINK_FUNCTIONS (arg);
20950
20951 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20952 {
20953 /* If we got some explicit template args, we need to plug them into
20954 the affected templates before we try to unify, in case the
20955 explicit args will completely resolve the templates in question. */
20956
20957 int ok = 0;
20958 tree expl_subargs = TREE_OPERAND (arg, 1);
20959 arg = TREE_OPERAND (arg, 0);
20960
20961 for (lkp_iterator iter (arg); iter; ++iter)
20962 {
20963 tree fn = *iter;
20964 tree subargs, elem;
20965
20966 if (TREE_CODE (fn) != TEMPLATE_DECL)
20967 continue;
20968
20969 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20970 expl_subargs, NULL_TREE, tf_none,
20971 /*require_all_args=*/true,
20972 /*use_default_args=*/true);
20973 if (subargs != error_mark_node
20974 && !any_dependent_template_arguments_p (subargs))
20975 {
20976 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20977 if (try_one_overload (tparms, targs, tempargs, parm,
20978 elem, strict, sub_strict, addr_p, explain_p)
20979 && (!goodfn || !same_type_p (goodfn, elem)))
20980 {
20981 goodfn = elem;
20982 ++good;
20983 }
20984 }
20985 else if (subargs)
20986 ++ok;
20987 }
20988 /* If no templates (or more than one) are fully resolved by the
20989 explicit arguments, this template-id is a non-deduced context; it
20990 could still be OK if we deduce all template arguments for the
20991 enclosing call through other arguments. */
20992 if (good != 1)
20993 good = ok;
20994 }
20995 else if (TREE_CODE (arg) != OVERLOAD
20996 && TREE_CODE (arg) != FUNCTION_DECL)
20997 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20998 -- but the deduction does not succeed because the expression is
20999 not just the function on its own. */
21000 return false;
21001 else
21002 for (lkp_iterator iter (arg); iter; ++iter)
21003 {
21004 tree fn = *iter;
21005 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21006 strict, sub_strict, addr_p, explain_p)
21007 && (!goodfn || !decls_match (goodfn, fn)))
21008 {
21009 goodfn = fn;
21010 ++good;
21011 }
21012 }
21013
21014 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21015 to function or pointer to member function argument if the set of
21016 overloaded functions does not contain function templates and at most
21017 one of a set of overloaded functions provides a unique match.
21018
21019 So if we found multiple possibilities, we return success but don't
21020 deduce anything. */
21021
21022 if (good == 1)
21023 {
21024 int i = TREE_VEC_LENGTH (targs);
21025 for (; i--; )
21026 if (TREE_VEC_ELT (tempargs, i))
21027 {
21028 tree old = TREE_VEC_ELT (targs, i);
21029 tree new_ = TREE_VEC_ELT (tempargs, i);
21030 if (new_ && old && ARGUMENT_PACK_P (old)
21031 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21032 /* Don't forget explicit template arguments in a pack. */
21033 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21034 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21035 TREE_VEC_ELT (targs, i) = new_;
21036 }
21037 }
21038 if (good)
21039 return true;
21040
21041 return false;
21042 }
21043
21044 /* Core DR 115: In contexts where deduction is done and fails, or in
21045 contexts where deduction is not done, if a template argument list is
21046 specified and it, along with any default template arguments, identifies
21047 a single function template specialization, then the template-id is an
21048 lvalue for the function template specialization. */
21049
21050 tree
21051 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21052 {
21053 tree expr, offset, baselink;
21054 bool addr;
21055
21056 if (!type_unknown_p (orig_expr))
21057 return orig_expr;
21058
21059 expr = orig_expr;
21060 addr = false;
21061 offset = NULL_TREE;
21062 baselink = NULL_TREE;
21063
21064 if (TREE_CODE (expr) == ADDR_EXPR)
21065 {
21066 expr = TREE_OPERAND (expr, 0);
21067 addr = true;
21068 }
21069 if (TREE_CODE (expr) == OFFSET_REF)
21070 {
21071 offset = expr;
21072 expr = TREE_OPERAND (expr, 1);
21073 }
21074 if (BASELINK_P (expr))
21075 {
21076 baselink = expr;
21077 expr = BASELINK_FUNCTIONS (expr);
21078 }
21079
21080 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21081 {
21082 int good = 0;
21083 tree goodfn = NULL_TREE;
21084
21085 /* If we got some explicit template args, we need to plug them into
21086 the affected templates before we try to unify, in case the
21087 explicit args will completely resolve the templates in question. */
21088
21089 tree expl_subargs = TREE_OPERAND (expr, 1);
21090 tree arg = TREE_OPERAND (expr, 0);
21091 tree badfn = NULL_TREE;
21092 tree badargs = NULL_TREE;
21093
21094 for (lkp_iterator iter (arg); iter; ++iter)
21095 {
21096 tree fn = *iter;
21097 tree subargs, elem;
21098
21099 if (TREE_CODE (fn) != TEMPLATE_DECL)
21100 continue;
21101
21102 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21103 expl_subargs, NULL_TREE, tf_none,
21104 /*require_all_args=*/true,
21105 /*use_default_args=*/true);
21106 if (subargs != error_mark_node
21107 && !any_dependent_template_arguments_p (subargs))
21108 {
21109 elem = instantiate_template (fn, subargs, tf_none);
21110 if (elem == error_mark_node)
21111 {
21112 badfn = fn;
21113 badargs = subargs;
21114 }
21115 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21116 {
21117 goodfn = elem;
21118 ++good;
21119 }
21120 }
21121 }
21122 if (good == 1)
21123 {
21124 mark_used (goodfn);
21125 expr = goodfn;
21126 if (baselink)
21127 expr = build_baselink (BASELINK_BINFO (baselink),
21128 BASELINK_ACCESS_BINFO (baselink),
21129 expr, BASELINK_OPTYPE (baselink));
21130 if (offset)
21131 {
21132 tree base
21133 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21134 expr = build_offset_ref (base, expr, addr, complain);
21135 }
21136 if (addr)
21137 expr = cp_build_addr_expr (expr, complain);
21138 return expr;
21139 }
21140 else if (good == 0 && badargs && (complain & tf_error))
21141 /* There were no good options and at least one bad one, so let the
21142 user know what the problem is. */
21143 instantiate_template (badfn, badargs, complain);
21144 }
21145 return orig_expr;
21146 }
21147
21148 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21149 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21150 different overloads deduce different arguments for a given parm.
21151 ADDR_P is true if the expression for which deduction is being
21152 performed was of the form "& fn" rather than simply "fn".
21153
21154 Returns 1 on success. */
21155
21156 static int
21157 try_one_overload (tree tparms,
21158 tree orig_targs,
21159 tree targs,
21160 tree parm,
21161 tree arg,
21162 unification_kind_t strict,
21163 int sub_strict,
21164 bool addr_p,
21165 bool explain_p)
21166 {
21167 int nargs;
21168 tree tempargs;
21169 int i;
21170
21171 if (arg == error_mark_node)
21172 return 0;
21173
21174 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21175 to function or pointer to member function argument if the set of
21176 overloaded functions does not contain function templates and at most
21177 one of a set of overloaded functions provides a unique match.
21178
21179 So if this is a template, just return success. */
21180
21181 if (uses_template_parms (arg))
21182 return 1;
21183
21184 if (TREE_CODE (arg) == METHOD_TYPE)
21185 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21186 else if (addr_p)
21187 arg = build_pointer_type (arg);
21188
21189 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21190
21191 /* We don't copy orig_targs for this because if we have already deduced
21192 some template args from previous args, unify would complain when we
21193 try to deduce a template parameter for the same argument, even though
21194 there isn't really a conflict. */
21195 nargs = TREE_VEC_LENGTH (targs);
21196 tempargs = make_tree_vec (nargs);
21197
21198 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21199 return 0;
21200
21201 /* First make sure we didn't deduce anything that conflicts with
21202 explicitly specified args. */
21203 for (i = nargs; i--; )
21204 {
21205 tree elt = TREE_VEC_ELT (tempargs, i);
21206 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21207
21208 if (!elt)
21209 /*NOP*/;
21210 else if (uses_template_parms (elt))
21211 /* Since we're unifying against ourselves, we will fill in
21212 template args used in the function parm list with our own
21213 template parms. Discard them. */
21214 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21215 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21216 {
21217 /* Check that the argument at each index of the deduced argument pack
21218 is equivalent to the corresponding explicitly specified argument.
21219 We may have deduced more arguments than were explicitly specified,
21220 and that's OK. */
21221
21222 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21223 that's wrong if we deduce the same argument pack from multiple
21224 function arguments: it's only incomplete the first time. */
21225
21226 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21227 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21228
21229 if (TREE_VEC_LENGTH (deduced_pack)
21230 < TREE_VEC_LENGTH (explicit_pack))
21231 return 0;
21232
21233 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21234 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21235 TREE_VEC_ELT (deduced_pack, j)))
21236 return 0;
21237 }
21238 else if (oldelt && !template_args_equal (oldelt, elt))
21239 return 0;
21240 }
21241
21242 for (i = nargs; i--; )
21243 {
21244 tree elt = TREE_VEC_ELT (tempargs, i);
21245
21246 if (elt)
21247 TREE_VEC_ELT (targs, i) = elt;
21248 }
21249
21250 return 1;
21251 }
21252
21253 /* PARM is a template class (perhaps with unbound template
21254 parameters). ARG is a fully instantiated type. If ARG can be
21255 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21256 TARGS are as for unify. */
21257
21258 static tree
21259 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21260 bool explain_p)
21261 {
21262 tree copy_of_targs;
21263
21264 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21265 return NULL_TREE;
21266 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21267 /* Matches anything. */;
21268 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21269 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21270 return NULL_TREE;
21271
21272 /* We need to make a new template argument vector for the call to
21273 unify. If we used TARGS, we'd clutter it up with the result of
21274 the attempted unification, even if this class didn't work out.
21275 We also don't want to commit ourselves to all the unifications
21276 we've already done, since unification is supposed to be done on
21277 an argument-by-argument basis. In other words, consider the
21278 following pathological case:
21279
21280 template <int I, int J, int K>
21281 struct S {};
21282
21283 template <int I, int J>
21284 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21285
21286 template <int I, int J, int K>
21287 void f(S<I, J, K>, S<I, I, I>);
21288
21289 void g() {
21290 S<0, 0, 0> s0;
21291 S<0, 1, 2> s2;
21292
21293 f(s0, s2);
21294 }
21295
21296 Now, by the time we consider the unification involving `s2', we
21297 already know that we must have `f<0, 0, 0>'. But, even though
21298 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21299 because there are two ways to unify base classes of S<0, 1, 2>
21300 with S<I, I, I>. If we kept the already deduced knowledge, we
21301 would reject the possibility I=1. */
21302 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21303
21304 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21305 {
21306 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21307 return NULL_TREE;
21308 return arg;
21309 }
21310
21311 /* If unification failed, we're done. */
21312 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21313 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21314 return NULL_TREE;
21315
21316 return arg;
21317 }
21318
21319 /* Given a template type PARM and a class type ARG, find the unique
21320 base type in ARG that is an instance of PARM. We do not examine
21321 ARG itself; only its base-classes. If there is not exactly one
21322 appropriate base class, return NULL_TREE. PARM may be the type of
21323 a partial specialization, as well as a plain template type. Used
21324 by unify. */
21325
21326 static enum template_base_result
21327 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21328 bool explain_p, tree *result)
21329 {
21330 tree rval = NULL_TREE;
21331 tree binfo;
21332
21333 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21334
21335 binfo = TYPE_BINFO (complete_type (arg));
21336 if (!binfo)
21337 {
21338 /* The type could not be completed. */
21339 *result = NULL_TREE;
21340 return tbr_incomplete_type;
21341 }
21342
21343 /* Walk in inheritance graph order. The search order is not
21344 important, and this avoids multiple walks of virtual bases. */
21345 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21346 {
21347 tree r = try_class_unification (tparms, targs, parm,
21348 BINFO_TYPE (binfo), explain_p);
21349
21350 if (r)
21351 {
21352 /* If there is more than one satisfactory baseclass, then:
21353
21354 [temp.deduct.call]
21355
21356 If they yield more than one possible deduced A, the type
21357 deduction fails.
21358
21359 applies. */
21360 if (rval && !same_type_p (r, rval))
21361 {
21362 *result = NULL_TREE;
21363 return tbr_ambiguous_baseclass;
21364 }
21365
21366 rval = r;
21367 }
21368 }
21369
21370 *result = rval;
21371 return tbr_success;
21372 }
21373
21374 /* Returns the level of DECL, which declares a template parameter. */
21375
21376 static int
21377 template_decl_level (tree decl)
21378 {
21379 switch (TREE_CODE (decl))
21380 {
21381 case TYPE_DECL:
21382 case TEMPLATE_DECL:
21383 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21384
21385 case PARM_DECL:
21386 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21387
21388 default:
21389 gcc_unreachable ();
21390 }
21391 return 0;
21392 }
21393
21394 /* Decide whether ARG can be unified with PARM, considering only the
21395 cv-qualifiers of each type, given STRICT as documented for unify.
21396 Returns nonzero iff the unification is OK on that basis. */
21397
21398 static int
21399 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21400 {
21401 int arg_quals = cp_type_quals (arg);
21402 int parm_quals = cp_type_quals (parm);
21403
21404 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21405 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21406 {
21407 /* Although a CVR qualifier is ignored when being applied to a
21408 substituted template parameter ([8.3.2]/1 for example), that
21409 does not allow us to unify "const T" with "int&" because both
21410 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21411 It is ok when we're allowing additional CV qualifiers
21412 at the outer level [14.8.2.1]/3,1st bullet. */
21413 if ((TYPE_REF_P (arg)
21414 || TREE_CODE (arg) == FUNCTION_TYPE
21415 || TREE_CODE (arg) == METHOD_TYPE)
21416 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21417 return 0;
21418
21419 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21420 && (parm_quals & TYPE_QUAL_RESTRICT))
21421 return 0;
21422 }
21423
21424 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21425 && (arg_quals & parm_quals) != parm_quals)
21426 return 0;
21427
21428 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21429 && (parm_quals & arg_quals) != arg_quals)
21430 return 0;
21431
21432 return 1;
21433 }
21434
21435 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21436 void
21437 template_parm_level_and_index (tree parm, int* level, int* index)
21438 {
21439 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21440 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21441 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21442 {
21443 *index = TEMPLATE_TYPE_IDX (parm);
21444 *level = TEMPLATE_TYPE_LEVEL (parm);
21445 }
21446 else
21447 {
21448 *index = TEMPLATE_PARM_IDX (parm);
21449 *level = TEMPLATE_PARM_LEVEL (parm);
21450 }
21451 }
21452
21453 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21454 do { \
21455 if (unify (TP, TA, P, A, S, EP)) \
21456 return 1; \
21457 } while (0)
21458
21459 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21460 expansion at the end of PACKED_PARMS. Returns 0 if the type
21461 deduction succeeds, 1 otherwise. STRICT is the same as in
21462 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21463 function call argument list. We'll need to adjust the arguments to make them
21464 types. SUBR tells us if this is from a recursive call to
21465 type_unification_real, or for comparing two template argument
21466 lists. */
21467
21468 static int
21469 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21470 tree packed_args, unification_kind_t strict,
21471 bool subr, bool explain_p)
21472 {
21473 tree parm
21474 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21475 tree pattern = PACK_EXPANSION_PATTERN (parm);
21476 tree pack, packs = NULL_TREE;
21477 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21478
21479 /* Add in any args remembered from an earlier partial instantiation. */
21480 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21481 int levels = TMPL_ARGS_DEPTH (targs);
21482
21483 packed_args = expand_template_argument_pack (packed_args);
21484
21485 int len = TREE_VEC_LENGTH (packed_args);
21486
21487 /* Determine the parameter packs we will be deducing from the
21488 pattern, and record their current deductions. */
21489 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21490 pack; pack = TREE_CHAIN (pack))
21491 {
21492 tree parm_pack = TREE_VALUE (pack);
21493 int idx, level;
21494
21495 /* Only template parameter packs can be deduced, not e.g. function
21496 parameter packs or __bases or __integer_pack. */
21497 if (!TEMPLATE_PARM_P (parm_pack))
21498 continue;
21499
21500 /* Determine the index and level of this parameter pack. */
21501 template_parm_level_and_index (parm_pack, &level, &idx);
21502 if (level < levels)
21503 continue;
21504
21505 /* Keep track of the parameter packs and their corresponding
21506 argument packs. */
21507 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21508 TREE_TYPE (packs) = make_tree_vec (len - start);
21509 }
21510
21511 /* Loop through all of the arguments that have not yet been
21512 unified and unify each with the pattern. */
21513 for (i = start; i < len; i++)
21514 {
21515 tree parm;
21516 bool any_explicit = false;
21517 tree arg = TREE_VEC_ELT (packed_args, i);
21518
21519 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21520 or the element of its argument pack at the current index if
21521 this argument was explicitly specified. */
21522 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21523 {
21524 int idx, level;
21525 tree arg, pargs;
21526 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21527
21528 arg = NULL_TREE;
21529 if (TREE_VALUE (pack)
21530 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21531 && (i - start < TREE_VEC_LENGTH (pargs)))
21532 {
21533 any_explicit = true;
21534 arg = TREE_VEC_ELT (pargs, i - start);
21535 }
21536 TMPL_ARG (targs, level, idx) = arg;
21537 }
21538
21539 /* If we had explicit template arguments, substitute them into the
21540 pattern before deduction. */
21541 if (any_explicit)
21542 {
21543 /* Some arguments might still be unspecified or dependent. */
21544 bool dependent;
21545 ++processing_template_decl;
21546 dependent = any_dependent_template_arguments_p (targs);
21547 if (!dependent)
21548 --processing_template_decl;
21549 parm = tsubst (pattern, targs,
21550 explain_p ? tf_warning_or_error : tf_none,
21551 NULL_TREE);
21552 if (dependent)
21553 --processing_template_decl;
21554 if (parm == error_mark_node)
21555 return 1;
21556 }
21557 else
21558 parm = pattern;
21559
21560 /* Unify the pattern with the current argument. */
21561 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21562 explain_p))
21563 return 1;
21564
21565 /* For each parameter pack, collect the deduced value. */
21566 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21567 {
21568 int idx, level;
21569 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21570
21571 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21572 TMPL_ARG (targs, level, idx);
21573 }
21574 }
21575
21576 /* Verify that the results of unification with the parameter packs
21577 produce results consistent with what we've seen before, and make
21578 the deduced argument packs available. */
21579 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21580 {
21581 tree old_pack = TREE_VALUE (pack);
21582 tree new_args = TREE_TYPE (pack);
21583 int i, len = TREE_VEC_LENGTH (new_args);
21584 int idx, level;
21585 bool nondeduced_p = false;
21586
21587 /* By default keep the original deduced argument pack.
21588 If necessary, more specific code is going to update the
21589 resulting deduced argument later down in this function. */
21590 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21591 TMPL_ARG (targs, level, idx) = old_pack;
21592
21593 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21594 actually deduce anything. */
21595 for (i = 0; i < len && !nondeduced_p; ++i)
21596 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21597 nondeduced_p = true;
21598 if (nondeduced_p)
21599 continue;
21600
21601 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21602 {
21603 /* If we had fewer function args than explicit template args,
21604 just use the explicits. */
21605 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21606 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21607 if (len < explicit_len)
21608 new_args = explicit_args;
21609 }
21610
21611 if (!old_pack)
21612 {
21613 tree result;
21614 /* Build the deduced *_ARGUMENT_PACK. */
21615 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21616 {
21617 result = make_node (NONTYPE_ARGUMENT_PACK);
21618 TREE_CONSTANT (result) = 1;
21619 }
21620 else
21621 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21622
21623 SET_ARGUMENT_PACK_ARGS (result, new_args);
21624
21625 /* Note the deduced argument packs for this parameter
21626 pack. */
21627 TMPL_ARG (targs, level, idx) = result;
21628 }
21629 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21630 && (ARGUMENT_PACK_ARGS (old_pack)
21631 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21632 {
21633 /* We only had the explicitly-provided arguments before, but
21634 now we have a complete set of arguments. */
21635 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21636
21637 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21638 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21639 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21640 }
21641 else
21642 {
21643 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21644 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21645
21646 if (!comp_template_args (old_args, new_args,
21647 &bad_old_arg, &bad_new_arg))
21648 /* Inconsistent unification of this parameter pack. */
21649 return unify_parameter_pack_inconsistent (explain_p,
21650 bad_old_arg,
21651 bad_new_arg);
21652 }
21653 }
21654
21655 return unify_success (explain_p);
21656 }
21657
21658 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21659 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21660 parameters and return value are as for unify. */
21661
21662 static int
21663 unify_array_domain (tree tparms, tree targs,
21664 tree parm_dom, tree arg_dom,
21665 bool explain_p)
21666 {
21667 tree parm_max;
21668 tree arg_max;
21669 bool parm_cst;
21670 bool arg_cst;
21671
21672 /* Our representation of array types uses "N - 1" as the
21673 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21674 not an integer constant. We cannot unify arbitrarily
21675 complex expressions, so we eliminate the MINUS_EXPRs
21676 here. */
21677 parm_max = TYPE_MAX_VALUE (parm_dom);
21678 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21679 if (!parm_cst)
21680 {
21681 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21682 parm_max = TREE_OPERAND (parm_max, 0);
21683 }
21684 arg_max = TYPE_MAX_VALUE (arg_dom);
21685 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21686 if (!arg_cst)
21687 {
21688 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21689 trying to unify the type of a variable with the type
21690 of a template parameter. For example:
21691
21692 template <unsigned int N>
21693 void f (char (&) [N]);
21694 int g();
21695 void h(int i) {
21696 char a[g(i)];
21697 f(a);
21698 }
21699
21700 Here, the type of the ARG will be "int [g(i)]", and
21701 may be a SAVE_EXPR, etc. */
21702 if (TREE_CODE (arg_max) != MINUS_EXPR)
21703 return unify_vla_arg (explain_p, arg_dom);
21704 arg_max = TREE_OPERAND (arg_max, 0);
21705 }
21706
21707 /* If only one of the bounds used a MINUS_EXPR, compensate
21708 by adding one to the other bound. */
21709 if (parm_cst && !arg_cst)
21710 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21711 integer_type_node,
21712 parm_max,
21713 integer_one_node);
21714 else if (arg_cst && !parm_cst)
21715 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21716 integer_type_node,
21717 arg_max,
21718 integer_one_node);
21719
21720 return unify (tparms, targs, parm_max, arg_max,
21721 UNIFY_ALLOW_INTEGER, explain_p);
21722 }
21723
21724 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21725
21726 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21727
21728 static pa_kind_t
21729 pa_kind (tree t)
21730 {
21731 if (PACK_EXPANSION_P (t))
21732 t = PACK_EXPANSION_PATTERN (t);
21733 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21734 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21735 || DECL_TYPE_TEMPLATE_P (t))
21736 return pa_tmpl;
21737 else if (TYPE_P (t))
21738 return pa_type;
21739 else
21740 return pa_expr;
21741 }
21742
21743 /* Deduce the value of template parameters. TPARMS is the (innermost)
21744 set of template parameters to a template. TARGS is the bindings
21745 for those template parameters, as determined thus far; TARGS may
21746 include template arguments for outer levels of template parameters
21747 as well. PARM is a parameter to a template function, or a
21748 subcomponent of that parameter; ARG is the corresponding argument.
21749 This function attempts to match PARM with ARG in a manner
21750 consistent with the existing assignments in TARGS. If more values
21751 are deduced, then TARGS is updated.
21752
21753 Returns 0 if the type deduction succeeds, 1 otherwise. The
21754 parameter STRICT is a bitwise or of the following flags:
21755
21756 UNIFY_ALLOW_NONE:
21757 Require an exact match between PARM and ARG.
21758 UNIFY_ALLOW_MORE_CV_QUAL:
21759 Allow the deduced ARG to be more cv-qualified (by qualification
21760 conversion) than ARG.
21761 UNIFY_ALLOW_LESS_CV_QUAL:
21762 Allow the deduced ARG to be less cv-qualified than ARG.
21763 UNIFY_ALLOW_DERIVED:
21764 Allow the deduced ARG to be a template base class of ARG,
21765 or a pointer to a template base class of the type pointed to by
21766 ARG.
21767 UNIFY_ALLOW_INTEGER:
21768 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21769 case for more information.
21770 UNIFY_ALLOW_OUTER_LEVEL:
21771 This is the outermost level of a deduction. Used to determine validity
21772 of qualification conversions. A valid qualification conversion must
21773 have const qualified pointers leading up to the inner type which
21774 requires additional CV quals, except at the outer level, where const
21775 is not required [conv.qual]. It would be normal to set this flag in
21776 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21777 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21778 This is the outermost level of a deduction, and PARM can be more CV
21779 qualified at this point.
21780 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21781 This is the outermost level of a deduction, and PARM can be less CV
21782 qualified at this point. */
21783
21784 static int
21785 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
21786 bool explain_p)
21787 {
21788 int idx;
21789 tree targ;
21790 tree tparm;
21791 int strict_in = strict;
21792 tsubst_flags_t complain = (explain_p
21793 ? tf_warning_or_error
21794 : tf_none);
21795
21796 /* I don't think this will do the right thing with respect to types.
21797 But the only case I've seen it in so far has been array bounds, where
21798 signedness is the only information lost, and I think that will be
21799 okay. */
21800 while (CONVERT_EXPR_P (parm))
21801 parm = TREE_OPERAND (parm, 0);
21802
21803 if (arg == error_mark_node)
21804 return unify_invalid (explain_p);
21805 if (arg == unknown_type_node
21806 || arg == init_list_type_node)
21807 /* We can't deduce anything from this, but we might get all the
21808 template args from other function args. */
21809 return unify_success (explain_p);
21810
21811 if (parm == any_targ_node || arg == any_targ_node)
21812 return unify_success (explain_p);
21813
21814 /* If PARM uses template parameters, then we can't bail out here,
21815 even if ARG == PARM, since we won't record unifications for the
21816 template parameters. We might need them if we're trying to
21817 figure out which of two things is more specialized. */
21818 if (arg == parm && !uses_template_parms (parm))
21819 return unify_success (explain_p);
21820
21821 /* Handle init lists early, so the rest of the function can assume
21822 we're dealing with a type. */
21823 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21824 {
21825 tree elt, elttype;
21826 unsigned i;
21827 tree orig_parm = parm;
21828
21829 /* Replace T with std::initializer_list<T> for deduction. */
21830 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21831 && flag_deduce_init_list)
21832 parm = listify (parm);
21833
21834 if (!is_std_init_list (parm)
21835 && TREE_CODE (parm) != ARRAY_TYPE)
21836 /* We can only deduce from an initializer list argument if the
21837 parameter is std::initializer_list or an array; otherwise this
21838 is a non-deduced context. */
21839 return unify_success (explain_p);
21840
21841 if (TREE_CODE (parm) == ARRAY_TYPE)
21842 elttype = TREE_TYPE (parm);
21843 else
21844 {
21845 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21846 /* Deduction is defined in terms of a single type, so just punt
21847 on the (bizarre) std::initializer_list<T...>. */
21848 if (PACK_EXPANSION_P (elttype))
21849 return unify_success (explain_p);
21850 }
21851
21852 if (strict != DEDUCE_EXACT
21853 && TYPE_P (elttype)
21854 && !uses_deducible_template_parms (elttype))
21855 /* If ELTTYPE has no deducible template parms, skip deduction from
21856 the list elements. */;
21857 else
21858 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21859 {
21860 int elt_strict = strict;
21861
21862 if (elt == error_mark_node)
21863 return unify_invalid (explain_p);
21864
21865 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21866 {
21867 tree type = TREE_TYPE (elt);
21868 if (type == error_mark_node)
21869 return unify_invalid (explain_p);
21870 /* It should only be possible to get here for a call. */
21871 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21872 elt_strict |= maybe_adjust_types_for_deduction
21873 (DEDUCE_CALL, &elttype, &type, elt);
21874 elt = type;
21875 }
21876
21877 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21878 explain_p);
21879 }
21880
21881 if (TREE_CODE (parm) == ARRAY_TYPE
21882 && deducible_array_bound (TYPE_DOMAIN (parm)))
21883 {
21884 /* Also deduce from the length of the initializer list. */
21885 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21886 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21887 if (idx == error_mark_node)
21888 return unify_invalid (explain_p);
21889 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21890 idx, explain_p);
21891 }
21892
21893 /* If the std::initializer_list<T> deduction worked, replace the
21894 deduced A with std::initializer_list<A>. */
21895 if (orig_parm != parm)
21896 {
21897 idx = TEMPLATE_TYPE_IDX (orig_parm);
21898 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21899 targ = listify (targ);
21900 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21901 }
21902 return unify_success (explain_p);
21903 }
21904
21905 /* If parm and arg aren't the same kind of thing (template, type, or
21906 expression), fail early. */
21907 if (pa_kind (parm) != pa_kind (arg))
21908 return unify_invalid (explain_p);
21909
21910 /* Immediately reject some pairs that won't unify because of
21911 cv-qualification mismatches. */
21912 if (TREE_CODE (arg) == TREE_CODE (parm)
21913 && TYPE_P (arg)
21914 /* It is the elements of the array which hold the cv quals of an array
21915 type, and the elements might be template type parms. We'll check
21916 when we recurse. */
21917 && TREE_CODE (arg) != ARRAY_TYPE
21918 /* We check the cv-qualifiers when unifying with template type
21919 parameters below. We want to allow ARG `const T' to unify with
21920 PARM `T' for example, when computing which of two templates
21921 is more specialized, for example. */
21922 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21923 && !check_cv_quals_for_unify (strict_in, arg, parm))
21924 return unify_cv_qual_mismatch (explain_p, parm, arg);
21925
21926 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21927 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21928 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21929 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21930 strict &= ~UNIFY_ALLOW_DERIVED;
21931 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21932 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21933
21934 switch (TREE_CODE (parm))
21935 {
21936 case TYPENAME_TYPE:
21937 case SCOPE_REF:
21938 case UNBOUND_CLASS_TEMPLATE:
21939 /* In a type which contains a nested-name-specifier, template
21940 argument values cannot be deduced for template parameters used
21941 within the nested-name-specifier. */
21942 return unify_success (explain_p);
21943
21944 case TEMPLATE_TYPE_PARM:
21945 case TEMPLATE_TEMPLATE_PARM:
21946 case BOUND_TEMPLATE_TEMPLATE_PARM:
21947 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21948 if (error_operand_p (tparm))
21949 return unify_invalid (explain_p);
21950
21951 if (TEMPLATE_TYPE_LEVEL (parm)
21952 != template_decl_level (tparm))
21953 /* The PARM is not one we're trying to unify. Just check
21954 to see if it matches ARG. */
21955 {
21956 if (TREE_CODE (arg) == TREE_CODE (parm)
21957 && (is_auto (parm) ? is_auto (arg)
21958 : same_type_p (parm, arg)))
21959 return unify_success (explain_p);
21960 else
21961 return unify_type_mismatch (explain_p, parm, arg);
21962 }
21963 idx = TEMPLATE_TYPE_IDX (parm);
21964 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21965 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21966 if (error_operand_p (tparm))
21967 return unify_invalid (explain_p);
21968
21969 /* Check for mixed types and values. */
21970 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21971 && TREE_CODE (tparm) != TYPE_DECL)
21972 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21973 && TREE_CODE (tparm) != TEMPLATE_DECL))
21974 gcc_unreachable ();
21975
21976 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21977 {
21978 if ((strict_in & UNIFY_ALLOW_DERIVED)
21979 && CLASS_TYPE_P (arg))
21980 {
21981 /* First try to match ARG directly. */
21982 tree t = try_class_unification (tparms, targs, parm, arg,
21983 explain_p);
21984 if (!t)
21985 {
21986 /* Otherwise, look for a suitable base of ARG, as below. */
21987 enum template_base_result r;
21988 r = get_template_base (tparms, targs, parm, arg,
21989 explain_p, &t);
21990 if (!t)
21991 return unify_no_common_base (explain_p, r, parm, arg);
21992 arg = t;
21993 }
21994 }
21995 /* ARG must be constructed from a template class or a template
21996 template parameter. */
21997 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21998 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21999 return unify_template_deduction_failure (explain_p, parm, arg);
22000
22001 /* Deduce arguments T, i from TT<T> or TT<i>. */
22002 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22003 return 1;
22004
22005 arg = TYPE_TI_TEMPLATE (arg);
22006
22007 /* Fall through to deduce template name. */
22008 }
22009
22010 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22011 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22012 {
22013 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22014
22015 /* Simple cases: Value already set, does match or doesn't. */
22016 if (targ != NULL_TREE && template_args_equal (targ, arg))
22017 return unify_success (explain_p);
22018 else if (targ)
22019 return unify_inconsistency (explain_p, parm, targ, arg);
22020 }
22021 else
22022 {
22023 /* If PARM is `const T' and ARG is only `int', we don't have
22024 a match unless we are allowing additional qualification.
22025 If ARG is `const int' and PARM is just `T' that's OK;
22026 that binds `const int' to `T'. */
22027 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22028 arg, parm))
22029 return unify_cv_qual_mismatch (explain_p, parm, arg);
22030
22031 /* Consider the case where ARG is `const volatile int' and
22032 PARM is `const T'. Then, T should be `volatile int'. */
22033 arg = cp_build_qualified_type_real
22034 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22035 if (arg == error_mark_node)
22036 return unify_invalid (explain_p);
22037
22038 /* Simple cases: Value already set, does match or doesn't. */
22039 if (targ != NULL_TREE && same_type_p (targ, arg))
22040 return unify_success (explain_p);
22041 else if (targ)
22042 return unify_inconsistency (explain_p, parm, targ, arg);
22043
22044 /* Make sure that ARG is not a variable-sized array. (Note
22045 that were talking about variable-sized arrays (like
22046 `int[n]'), rather than arrays of unknown size (like
22047 `int[]').) We'll get very confused by such a type since
22048 the bound of the array is not constant, and therefore
22049 not mangleable. Besides, such types are not allowed in
22050 ISO C++, so we can do as we please here. We do allow
22051 them for 'auto' deduction, since that isn't ABI-exposed. */
22052 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22053 return unify_vla_arg (explain_p, arg);
22054
22055 /* Strip typedefs as in convert_template_argument. */
22056 arg = canonicalize_type_argument (arg, tf_none);
22057 }
22058
22059 /* If ARG is a parameter pack or an expansion, we cannot unify
22060 against it unless PARM is also a parameter pack. */
22061 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22062 && !template_parameter_pack_p (parm))
22063 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22064
22065 /* If the argument deduction results is a METHOD_TYPE,
22066 then there is a problem.
22067 METHOD_TYPE doesn't map to any real C++ type the result of
22068 the deduction cannot be of that type. */
22069 if (TREE_CODE (arg) == METHOD_TYPE)
22070 return unify_method_type_error (explain_p, arg);
22071
22072 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22073 return unify_success (explain_p);
22074
22075 case TEMPLATE_PARM_INDEX:
22076 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22077 if (error_operand_p (tparm))
22078 return unify_invalid (explain_p);
22079
22080 if (TEMPLATE_PARM_LEVEL (parm)
22081 != template_decl_level (tparm))
22082 {
22083 /* The PARM is not one we're trying to unify. Just check
22084 to see if it matches ARG. */
22085 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22086 && cp_tree_equal (parm, arg));
22087 if (result)
22088 unify_expression_unequal (explain_p, parm, arg);
22089 return result;
22090 }
22091
22092 idx = TEMPLATE_PARM_IDX (parm);
22093 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22094
22095 if (targ)
22096 {
22097 if ((strict & UNIFY_ALLOW_INTEGER)
22098 && TREE_TYPE (targ) && TREE_TYPE (arg)
22099 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22100 /* We're deducing from an array bound, the type doesn't matter. */
22101 arg = fold_convert (TREE_TYPE (targ), arg);
22102 int x = !cp_tree_equal (targ, arg);
22103 if (x)
22104 unify_inconsistency (explain_p, parm, targ, arg);
22105 return x;
22106 }
22107
22108 /* [temp.deduct.type] If, in the declaration of a function template
22109 with a non-type template-parameter, the non-type
22110 template-parameter is used in an expression in the function
22111 parameter-list and, if the corresponding template-argument is
22112 deduced, the template-argument type shall match the type of the
22113 template-parameter exactly, except that a template-argument
22114 deduced from an array bound may be of any integral type.
22115 The non-type parameter might use already deduced type parameters. */
22116 tparm = TREE_TYPE (parm);
22117 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22118 /* We don't have enough levels of args to do any substitution. This
22119 can happen in the context of -fnew-ttp-matching. */;
22120 else
22121 {
22122 ++processing_template_decl;
22123 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22124 --processing_template_decl;
22125
22126 if (tree a = type_uses_auto (tparm))
22127 {
22128 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22129 if (tparm == error_mark_node)
22130 return 1;
22131 }
22132 }
22133
22134 if (!TREE_TYPE (arg))
22135 /* Template-parameter dependent expression. Just accept it for now.
22136 It will later be processed in convert_template_argument. */
22137 ;
22138 else if (same_type_p (non_reference (TREE_TYPE (arg)),
22139 non_reference (tparm)))
22140 /* OK */;
22141 else if ((strict & UNIFY_ALLOW_INTEGER)
22142 && CP_INTEGRAL_TYPE_P (tparm))
22143 /* Convert the ARG to the type of PARM; the deduced non-type
22144 template argument must exactly match the types of the
22145 corresponding parameter. */
22146 arg = fold (build_nop (tparm, arg));
22147 else if (uses_template_parms (tparm))
22148 {
22149 /* We haven't deduced the type of this parameter yet. */
22150 if (cxx_dialect >= cxx17
22151 /* We deduce from array bounds in try_array_deduction. */
22152 && !(strict & UNIFY_ALLOW_INTEGER))
22153 {
22154 /* Deduce it from the non-type argument. */
22155 tree atype = TREE_TYPE (arg);
22156 RECUR_AND_CHECK_FAILURE (tparms, targs,
22157 tparm, atype,
22158 UNIFY_ALLOW_NONE, explain_p);
22159 }
22160 else
22161 /* Try again later. */
22162 return unify_success (explain_p);
22163 }
22164 else
22165 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
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_PARM_PARAMETER_PACK (parm))
22171 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22172
22173 {
22174 bool removed_attr = false;
22175 arg = strip_typedefs_expr (arg, &removed_attr);
22176 }
22177 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22178 return unify_success (explain_p);
22179
22180 case PTRMEM_CST:
22181 {
22182 /* A pointer-to-member constant can be unified only with
22183 another constant. */
22184 if (TREE_CODE (arg) != PTRMEM_CST)
22185 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22186
22187 /* Just unify the class member. It would be useless (and possibly
22188 wrong, depending on the strict flags) to unify also
22189 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22190 arg refer to the same variable, even if through different
22191 classes. For instance:
22192
22193 struct A { int x; };
22194 struct B : A { };
22195
22196 Unification of &A::x and &B::x must succeed. */
22197 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22198 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22199 }
22200
22201 case POINTER_TYPE:
22202 {
22203 if (!TYPE_PTR_P (arg))
22204 return unify_type_mismatch (explain_p, parm, arg);
22205
22206 /* [temp.deduct.call]
22207
22208 A can be another pointer or pointer to member type that can
22209 be converted to the deduced A via a qualification
22210 conversion (_conv.qual_).
22211
22212 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22213 This will allow for additional cv-qualification of the
22214 pointed-to types if appropriate. */
22215
22216 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22217 /* The derived-to-base conversion only persists through one
22218 level of pointers. */
22219 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22220
22221 return unify (tparms, targs, TREE_TYPE (parm),
22222 TREE_TYPE (arg), strict, explain_p);
22223 }
22224
22225 case REFERENCE_TYPE:
22226 if (!TYPE_REF_P (arg))
22227 return unify_type_mismatch (explain_p, parm, arg);
22228 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22229 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22230
22231 case ARRAY_TYPE:
22232 if (TREE_CODE (arg) != ARRAY_TYPE)
22233 return unify_type_mismatch (explain_p, parm, arg);
22234 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22235 != (TYPE_DOMAIN (arg) == NULL_TREE))
22236 return unify_type_mismatch (explain_p, parm, arg);
22237 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22238 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22239 if (TYPE_DOMAIN (parm) != NULL_TREE)
22240 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22241 TYPE_DOMAIN (arg), explain_p);
22242 return unify_success (explain_p);
22243
22244 case REAL_TYPE:
22245 case COMPLEX_TYPE:
22246 case VECTOR_TYPE:
22247 case INTEGER_TYPE:
22248 case BOOLEAN_TYPE:
22249 case ENUMERAL_TYPE:
22250 case VOID_TYPE:
22251 case NULLPTR_TYPE:
22252 if (TREE_CODE (arg) != TREE_CODE (parm))
22253 return unify_type_mismatch (explain_p, parm, arg);
22254
22255 /* We have already checked cv-qualification at the top of the
22256 function. */
22257 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22258 return unify_type_mismatch (explain_p, parm, arg);
22259
22260 /* As far as unification is concerned, this wins. Later checks
22261 will invalidate it if necessary. */
22262 return unify_success (explain_p);
22263
22264 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22265 /* Type INTEGER_CST can come from ordinary constant template args. */
22266 case INTEGER_CST:
22267 while (CONVERT_EXPR_P (arg))
22268 arg = TREE_OPERAND (arg, 0);
22269
22270 if (TREE_CODE (arg) != INTEGER_CST)
22271 return unify_template_argument_mismatch (explain_p, parm, arg);
22272 return (tree_int_cst_equal (parm, arg)
22273 ? unify_success (explain_p)
22274 : unify_template_argument_mismatch (explain_p, parm, arg));
22275
22276 case TREE_VEC:
22277 {
22278 int i, len, argslen;
22279 int parm_variadic_p = 0;
22280
22281 if (TREE_CODE (arg) != TREE_VEC)
22282 return unify_template_argument_mismatch (explain_p, parm, arg);
22283
22284 len = TREE_VEC_LENGTH (parm);
22285 argslen = TREE_VEC_LENGTH (arg);
22286
22287 /* Check for pack expansions in the parameters. */
22288 for (i = 0; i < len; ++i)
22289 {
22290 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22291 {
22292 if (i == len - 1)
22293 /* We can unify against something with a trailing
22294 parameter pack. */
22295 parm_variadic_p = 1;
22296 else
22297 /* [temp.deduct.type]/9: If the template argument list of
22298 P contains a pack expansion that is not the last
22299 template argument, the entire template argument list
22300 is a non-deduced context. */
22301 return unify_success (explain_p);
22302 }
22303 }
22304
22305 /* If we don't have enough arguments to satisfy the parameters
22306 (not counting the pack expression at the end), or we have
22307 too many arguments for a parameter list that doesn't end in
22308 a pack expression, we can't unify. */
22309 if (parm_variadic_p
22310 ? argslen < len - parm_variadic_p
22311 : argslen != len)
22312 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22313
22314 /* Unify all of the parameters that precede the (optional)
22315 pack expression. */
22316 for (i = 0; i < len - parm_variadic_p; ++i)
22317 {
22318 RECUR_AND_CHECK_FAILURE (tparms, targs,
22319 TREE_VEC_ELT (parm, i),
22320 TREE_VEC_ELT (arg, i),
22321 UNIFY_ALLOW_NONE, explain_p);
22322 }
22323 if (parm_variadic_p)
22324 return unify_pack_expansion (tparms, targs, parm, arg,
22325 DEDUCE_EXACT,
22326 /*subr=*/true, explain_p);
22327 return unify_success (explain_p);
22328 }
22329
22330 case RECORD_TYPE:
22331 case UNION_TYPE:
22332 if (TREE_CODE (arg) != TREE_CODE (parm))
22333 return unify_type_mismatch (explain_p, parm, arg);
22334
22335 if (TYPE_PTRMEMFUNC_P (parm))
22336 {
22337 if (!TYPE_PTRMEMFUNC_P (arg))
22338 return unify_type_mismatch (explain_p, parm, arg);
22339
22340 return unify (tparms, targs,
22341 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22342 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22343 strict, explain_p);
22344 }
22345 else if (TYPE_PTRMEMFUNC_P (arg))
22346 return unify_type_mismatch (explain_p, parm, arg);
22347
22348 if (CLASSTYPE_TEMPLATE_INFO (parm))
22349 {
22350 tree t = NULL_TREE;
22351
22352 if (strict_in & UNIFY_ALLOW_DERIVED)
22353 {
22354 /* First, we try to unify the PARM and ARG directly. */
22355 t = try_class_unification (tparms, targs,
22356 parm, arg, explain_p);
22357
22358 if (!t)
22359 {
22360 /* Fallback to the special case allowed in
22361 [temp.deduct.call]:
22362
22363 If P is a class, and P has the form
22364 template-id, then A can be a derived class of
22365 the deduced A. Likewise, if P is a pointer to
22366 a class of the form template-id, A can be a
22367 pointer to a derived class pointed to by the
22368 deduced A. */
22369 enum template_base_result r;
22370 r = get_template_base (tparms, targs, parm, arg,
22371 explain_p, &t);
22372
22373 if (!t)
22374 {
22375 /* Don't give the derived diagnostic if we're
22376 already dealing with the same template. */
22377 bool same_template
22378 = (CLASSTYPE_TEMPLATE_INFO (arg)
22379 && (CLASSTYPE_TI_TEMPLATE (parm)
22380 == CLASSTYPE_TI_TEMPLATE (arg)));
22381 return unify_no_common_base (explain_p && !same_template,
22382 r, parm, arg);
22383 }
22384 }
22385 }
22386 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22387 && (CLASSTYPE_TI_TEMPLATE (parm)
22388 == CLASSTYPE_TI_TEMPLATE (arg)))
22389 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22390 Then, we should unify `int' and `U'. */
22391 t = arg;
22392 else
22393 /* There's no chance of unification succeeding. */
22394 return unify_type_mismatch (explain_p, parm, arg);
22395
22396 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22397 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22398 }
22399 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22400 return unify_type_mismatch (explain_p, parm, arg);
22401 return unify_success (explain_p);
22402
22403 case METHOD_TYPE:
22404 case FUNCTION_TYPE:
22405 {
22406 unsigned int nargs;
22407 tree *args;
22408 tree a;
22409 unsigned int i;
22410
22411 if (TREE_CODE (arg) != TREE_CODE (parm))
22412 return unify_type_mismatch (explain_p, parm, arg);
22413
22414 /* CV qualifications for methods can never be deduced, they must
22415 match exactly. We need to check them explicitly here,
22416 because type_unification_real treats them as any other
22417 cv-qualified parameter. */
22418 if (TREE_CODE (parm) == METHOD_TYPE
22419 && (!check_cv_quals_for_unify
22420 (UNIFY_ALLOW_NONE,
22421 class_of_this_parm (arg),
22422 class_of_this_parm (parm))))
22423 return unify_cv_qual_mismatch (explain_p, parm, arg);
22424 if (TREE_CODE (arg) == FUNCTION_TYPE
22425 && type_memfn_quals (parm) != type_memfn_quals (arg))
22426 return unify_cv_qual_mismatch (explain_p, parm, arg);
22427 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22428 return unify_type_mismatch (explain_p, parm, arg);
22429
22430 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22431 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22432
22433 nargs = list_length (TYPE_ARG_TYPES (arg));
22434 args = XALLOCAVEC (tree, nargs);
22435 for (a = TYPE_ARG_TYPES (arg), i = 0;
22436 a != NULL_TREE && a != void_list_node;
22437 a = TREE_CHAIN (a), ++i)
22438 args[i] = TREE_VALUE (a);
22439 nargs = i;
22440
22441 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22442 args, nargs, 1, DEDUCE_EXACT,
22443 NULL, explain_p))
22444 return 1;
22445
22446 if (flag_noexcept_type)
22447 {
22448 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22449 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22450 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22451 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22452 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22453 && uses_template_parms (TREE_PURPOSE (pspec)))
22454 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22455 TREE_PURPOSE (aspec),
22456 UNIFY_ALLOW_NONE, explain_p);
22457 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22458 return unify_type_mismatch (explain_p, parm, arg);
22459 }
22460
22461 return 0;
22462 }
22463
22464 case OFFSET_TYPE:
22465 /* Unify a pointer to member with a pointer to member function, which
22466 deduces the type of the member as a function type. */
22467 if (TYPE_PTRMEMFUNC_P (arg))
22468 {
22469 /* Check top-level cv qualifiers */
22470 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22471 return unify_cv_qual_mismatch (explain_p, parm, arg);
22472
22473 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22474 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22475 UNIFY_ALLOW_NONE, explain_p);
22476
22477 /* Determine the type of the function we are unifying against. */
22478 tree fntype = static_fn_type (arg);
22479
22480 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22481 }
22482
22483 if (TREE_CODE (arg) != OFFSET_TYPE)
22484 return unify_type_mismatch (explain_p, parm, arg);
22485 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22486 TYPE_OFFSET_BASETYPE (arg),
22487 UNIFY_ALLOW_NONE, explain_p);
22488 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22489 strict, explain_p);
22490
22491 case CONST_DECL:
22492 if (DECL_TEMPLATE_PARM_P (parm))
22493 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22494 if (arg != scalar_constant_value (parm))
22495 return unify_template_argument_mismatch (explain_p, parm, arg);
22496 return unify_success (explain_p);
22497
22498 case FIELD_DECL:
22499 case TEMPLATE_DECL:
22500 /* Matched cases are handled by the ARG == PARM test above. */
22501 return unify_template_argument_mismatch (explain_p, parm, arg);
22502
22503 case VAR_DECL:
22504 /* We might get a variable as a non-type template argument in parm if the
22505 corresponding parameter is type-dependent. Make any necessary
22506 adjustments based on whether arg is a reference. */
22507 if (CONSTANT_CLASS_P (arg))
22508 parm = fold_non_dependent_expr (parm, complain);
22509 else if (REFERENCE_REF_P (arg))
22510 {
22511 tree sub = TREE_OPERAND (arg, 0);
22512 STRIP_NOPS (sub);
22513 if (TREE_CODE (sub) == ADDR_EXPR)
22514 arg = TREE_OPERAND (sub, 0);
22515 }
22516 /* Now use the normal expression code to check whether they match. */
22517 goto expr;
22518
22519 case TYPE_ARGUMENT_PACK:
22520 case NONTYPE_ARGUMENT_PACK:
22521 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22522 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22523
22524 case TYPEOF_TYPE:
22525 case DECLTYPE_TYPE:
22526 case UNDERLYING_TYPE:
22527 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22528 or UNDERLYING_TYPE nodes. */
22529 return unify_success (explain_p);
22530
22531 case ERROR_MARK:
22532 /* Unification fails if we hit an error node. */
22533 return unify_invalid (explain_p);
22534
22535 case INDIRECT_REF:
22536 if (REFERENCE_REF_P (parm))
22537 {
22538 bool pexp = PACK_EXPANSION_P (arg);
22539 if (pexp)
22540 arg = PACK_EXPANSION_PATTERN (arg);
22541 if (REFERENCE_REF_P (arg))
22542 arg = TREE_OPERAND (arg, 0);
22543 if (pexp)
22544 arg = make_pack_expansion (arg, complain);
22545 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22546 strict, explain_p);
22547 }
22548 /* FALLTHRU */
22549
22550 default:
22551 /* An unresolved overload is a nondeduced context. */
22552 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22553 return unify_success (explain_p);
22554 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22555 expr:
22556 /* We must be looking at an expression. This can happen with
22557 something like:
22558
22559 template <int I>
22560 void foo(S<I>, S<I + 2>);
22561
22562 This is a "nondeduced context":
22563
22564 [deduct.type]
22565
22566 The nondeduced contexts are:
22567
22568 --A type that is a template-id in which one or more of
22569 the template-arguments is an expression that references
22570 a template-parameter.
22571
22572 In these cases, we assume deduction succeeded, but don't
22573 actually infer any unifications. */
22574
22575 if (!uses_template_parms (parm)
22576 && !template_args_equal (parm, arg))
22577 return unify_expression_unequal (explain_p, parm, arg);
22578 else
22579 return unify_success (explain_p);
22580 }
22581 }
22582 #undef RECUR_AND_CHECK_FAILURE
22583 \f
22584 /* Note that DECL can be defined in this translation unit, if
22585 required. */
22586
22587 static void
22588 mark_definable (tree decl)
22589 {
22590 tree clone;
22591 DECL_NOT_REALLY_EXTERN (decl) = 1;
22592 FOR_EACH_CLONE (clone, decl)
22593 DECL_NOT_REALLY_EXTERN (clone) = 1;
22594 }
22595
22596 /* Called if RESULT is explicitly instantiated, or is a member of an
22597 explicitly instantiated class. */
22598
22599 void
22600 mark_decl_instantiated (tree result, int extern_p)
22601 {
22602 SET_DECL_EXPLICIT_INSTANTIATION (result);
22603
22604 /* If this entity has already been written out, it's too late to
22605 make any modifications. */
22606 if (TREE_ASM_WRITTEN (result))
22607 return;
22608
22609 /* For anonymous namespace we don't need to do anything. */
22610 if (decl_anon_ns_mem_p (result))
22611 {
22612 gcc_assert (!TREE_PUBLIC (result));
22613 return;
22614 }
22615
22616 if (TREE_CODE (result) != FUNCTION_DECL)
22617 /* The TREE_PUBLIC flag for function declarations will have been
22618 set correctly by tsubst. */
22619 TREE_PUBLIC (result) = 1;
22620
22621 /* This might have been set by an earlier implicit instantiation. */
22622 DECL_COMDAT (result) = 0;
22623
22624 if (extern_p)
22625 DECL_NOT_REALLY_EXTERN (result) = 0;
22626 else
22627 {
22628 mark_definable (result);
22629 mark_needed (result);
22630 /* Always make artificials weak. */
22631 if (DECL_ARTIFICIAL (result) && flag_weak)
22632 comdat_linkage (result);
22633 /* For WIN32 we also want to put explicit instantiations in
22634 linkonce sections. */
22635 else if (TREE_PUBLIC (result))
22636 maybe_make_one_only (result);
22637 if (TREE_CODE (result) == FUNCTION_DECL
22638 && DECL_TEMPLATE_INSTANTIATED (result))
22639 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22640 since start_preparsed_function wouldn't have if we had an earlier
22641 extern explicit instantiation. */
22642 DECL_EXTERNAL (result) = 0;
22643 }
22644
22645 /* If EXTERN_P, then this function will not be emitted -- unless
22646 followed by an explicit instantiation, at which point its linkage
22647 will be adjusted. If !EXTERN_P, then this function will be
22648 emitted here. In neither circumstance do we want
22649 import_export_decl to adjust the linkage. */
22650 DECL_INTERFACE_KNOWN (result) = 1;
22651 }
22652
22653 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22654 important template arguments. If any are missing, we check whether
22655 they're important by using error_mark_node for substituting into any
22656 args that were used for partial ordering (the ones between ARGS and END)
22657 and seeing if it bubbles up. */
22658
22659 static bool
22660 check_undeduced_parms (tree targs, tree args, tree end)
22661 {
22662 bool found = false;
22663 int i;
22664 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22665 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22666 {
22667 found = true;
22668 TREE_VEC_ELT (targs, i) = error_mark_node;
22669 }
22670 if (found)
22671 {
22672 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22673 if (substed == error_mark_node)
22674 return true;
22675 }
22676 return false;
22677 }
22678
22679 /* Given two function templates PAT1 and PAT2, return:
22680
22681 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22682 -1 if PAT2 is more specialized than PAT1.
22683 0 if neither is more specialized.
22684
22685 LEN indicates the number of parameters we should consider
22686 (defaulted parameters should not be considered).
22687
22688 The 1998 std underspecified function template partial ordering, and
22689 DR214 addresses the issue. We take pairs of arguments, one from
22690 each of the templates, and deduce them against each other. One of
22691 the templates will be more specialized if all the *other*
22692 template's arguments deduce against its arguments and at least one
22693 of its arguments *does* *not* deduce against the other template's
22694 corresponding argument. Deduction is done as for class templates.
22695 The arguments used in deduction have reference and top level cv
22696 qualifiers removed. Iff both arguments were originally reference
22697 types *and* deduction succeeds in both directions, an lvalue reference
22698 wins against an rvalue reference and otherwise the template
22699 with the more cv-qualified argument wins for that pairing (if
22700 neither is more cv-qualified, they both are equal). Unlike regular
22701 deduction, after all the arguments have been deduced in this way,
22702 we do *not* verify the deduced template argument values can be
22703 substituted into non-deduced contexts.
22704
22705 The logic can be a bit confusing here, because we look at deduce1 and
22706 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22707 can find template arguments for pat1 to make arg1 look like arg2, that
22708 means that arg2 is at least as specialized as arg1. */
22709
22710 int
22711 more_specialized_fn (tree pat1, tree pat2, int len)
22712 {
22713 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22714 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22715 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22716 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22717 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22718 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22719 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22720 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22721 tree origs1, origs2;
22722 bool lose1 = false;
22723 bool lose2 = false;
22724
22725 /* Remove the this parameter from non-static member functions. If
22726 one is a non-static member function and the other is not a static
22727 member function, remove the first parameter from that function
22728 also. This situation occurs for operator functions where we
22729 locate both a member function (with this pointer) and non-member
22730 operator (with explicit first operand). */
22731 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22732 {
22733 len--; /* LEN is the number of significant arguments for DECL1 */
22734 args1 = TREE_CHAIN (args1);
22735 if (!DECL_STATIC_FUNCTION_P (decl2))
22736 args2 = TREE_CHAIN (args2);
22737 }
22738 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22739 {
22740 args2 = TREE_CHAIN (args2);
22741 if (!DECL_STATIC_FUNCTION_P (decl1))
22742 {
22743 len--;
22744 args1 = TREE_CHAIN (args1);
22745 }
22746 }
22747
22748 /* If only one is a conversion operator, they are unordered. */
22749 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22750 return 0;
22751
22752 /* Consider the return type for a conversion function */
22753 if (DECL_CONV_FN_P (decl1))
22754 {
22755 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22756 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22757 len++;
22758 }
22759
22760 processing_template_decl++;
22761
22762 origs1 = args1;
22763 origs2 = args2;
22764
22765 while (len--
22766 /* Stop when an ellipsis is seen. */
22767 && args1 != NULL_TREE && args2 != NULL_TREE)
22768 {
22769 tree arg1 = TREE_VALUE (args1);
22770 tree arg2 = TREE_VALUE (args2);
22771 int deduce1, deduce2;
22772 int quals1 = -1;
22773 int quals2 = -1;
22774 int ref1 = 0;
22775 int ref2 = 0;
22776
22777 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22778 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22779 {
22780 /* When both arguments are pack expansions, we need only
22781 unify the patterns themselves. */
22782 arg1 = PACK_EXPANSION_PATTERN (arg1);
22783 arg2 = PACK_EXPANSION_PATTERN (arg2);
22784
22785 /* This is the last comparison we need to do. */
22786 len = 0;
22787 }
22788
22789 /* DR 1847: If a particular P contains no template-parameters that
22790 participate in template argument deduction, that P is not used to
22791 determine the ordering. */
22792 if (!uses_deducible_template_parms (arg1)
22793 && !uses_deducible_template_parms (arg2))
22794 goto next;
22795
22796 if (TYPE_REF_P (arg1))
22797 {
22798 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
22799 arg1 = TREE_TYPE (arg1);
22800 quals1 = cp_type_quals (arg1);
22801 }
22802
22803 if (TYPE_REF_P (arg2))
22804 {
22805 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22806 arg2 = TREE_TYPE (arg2);
22807 quals2 = cp_type_quals (arg2);
22808 }
22809
22810 arg1 = TYPE_MAIN_VARIANT (arg1);
22811 arg2 = TYPE_MAIN_VARIANT (arg2);
22812
22813 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22814 {
22815 int i, len2 = remaining_arguments (args2);
22816 tree parmvec = make_tree_vec (1);
22817 tree argvec = make_tree_vec (len2);
22818 tree ta = args2;
22819
22820 /* Setup the parameter vector, which contains only ARG1. */
22821 TREE_VEC_ELT (parmvec, 0) = arg1;
22822
22823 /* Setup the argument vector, which contains the remaining
22824 arguments. */
22825 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22826 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22827
22828 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22829 argvec, DEDUCE_EXACT,
22830 /*subr=*/true, /*explain_p=*/false)
22831 == 0);
22832
22833 /* We cannot deduce in the other direction, because ARG1 is
22834 a pack expansion but ARG2 is not. */
22835 deduce2 = 0;
22836 }
22837 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22838 {
22839 int i, len1 = remaining_arguments (args1);
22840 tree parmvec = make_tree_vec (1);
22841 tree argvec = make_tree_vec (len1);
22842 tree ta = args1;
22843
22844 /* Setup the parameter vector, which contains only ARG1. */
22845 TREE_VEC_ELT (parmvec, 0) = arg2;
22846
22847 /* Setup the argument vector, which contains the remaining
22848 arguments. */
22849 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22850 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22851
22852 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22853 argvec, DEDUCE_EXACT,
22854 /*subr=*/true, /*explain_p=*/false)
22855 == 0);
22856
22857 /* We cannot deduce in the other direction, because ARG2 is
22858 a pack expansion but ARG1 is not.*/
22859 deduce1 = 0;
22860 }
22861
22862 else
22863 {
22864 /* The normal case, where neither argument is a pack
22865 expansion. */
22866 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22867 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22868 == 0);
22869 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22870 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22871 == 0);
22872 }
22873
22874 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22875 arg2, then arg2 is not as specialized as arg1. */
22876 if (!deduce1)
22877 lose2 = true;
22878 if (!deduce2)
22879 lose1 = true;
22880
22881 /* "If, for a given type, deduction succeeds in both directions
22882 (i.e., the types are identical after the transformations above)
22883 and both P and A were reference types (before being replaced with
22884 the type referred to above):
22885 - if the type from the argument template was an lvalue reference and
22886 the type from the parameter template was not, the argument type is
22887 considered to be more specialized than the other; otherwise,
22888 - if the type from the argument template is more cv-qualified
22889 than the type from the parameter template (as described above),
22890 the argument type is considered to be more specialized than the other;
22891 otherwise,
22892 - neither type is more specialized than the other." */
22893
22894 if (deduce1 && deduce2)
22895 {
22896 if (ref1 && ref2 && ref1 != ref2)
22897 {
22898 if (ref1 > ref2)
22899 lose1 = true;
22900 else
22901 lose2 = true;
22902 }
22903 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22904 {
22905 if ((quals1 & quals2) == quals2)
22906 lose2 = true;
22907 if ((quals1 & quals2) == quals1)
22908 lose1 = true;
22909 }
22910 }
22911
22912 if (lose1 && lose2)
22913 /* We've failed to deduce something in either direction.
22914 These must be unordered. */
22915 break;
22916
22917 next:
22918
22919 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22920 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22921 /* We have already processed all of the arguments in our
22922 handing of the pack expansion type. */
22923 len = 0;
22924
22925 args1 = TREE_CHAIN (args1);
22926 args2 = TREE_CHAIN (args2);
22927 }
22928
22929 /* "In most cases, all template parameters must have values in order for
22930 deduction to succeed, but for partial ordering purposes a template
22931 parameter may remain without a value provided it is not used in the
22932 types being used for partial ordering."
22933
22934 Thus, if we are missing any of the targs1 we need to substitute into
22935 origs1, then pat2 is not as specialized as pat1. This can happen when
22936 there is a nondeduced context. */
22937 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22938 lose2 = true;
22939 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22940 lose1 = true;
22941
22942 processing_template_decl--;
22943
22944 /* If both deductions succeed, the partial ordering selects the more
22945 constrained template. */
22946 if (!lose1 && !lose2)
22947 {
22948 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22949 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22950 lose1 = !subsumes_constraints (c1, c2);
22951 lose2 = !subsumes_constraints (c2, c1);
22952 }
22953
22954 /* All things being equal, if the next argument is a pack expansion
22955 for one function but not for the other, prefer the
22956 non-variadic function. FIXME this is bogus; see c++/41958. */
22957 if (lose1 == lose2
22958 && args1 && TREE_VALUE (args1)
22959 && args2 && TREE_VALUE (args2))
22960 {
22961 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22962 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22963 }
22964
22965 if (lose1 == lose2)
22966 return 0;
22967 else if (!lose1)
22968 return 1;
22969 else
22970 return -1;
22971 }
22972
22973 /* Determine which of two partial specializations of TMPL is more
22974 specialized.
22975
22976 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22977 to the first partial specialization. The TREE_PURPOSE is the
22978 innermost set of template parameters for the partial
22979 specialization. PAT2 is similar, but for the second template.
22980
22981 Return 1 if the first partial specialization is more specialized;
22982 -1 if the second is more specialized; 0 if neither is more
22983 specialized.
22984
22985 See [temp.class.order] for information about determining which of
22986 two templates is more specialized. */
22987
22988 static int
22989 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22990 {
22991 tree targs;
22992 int winner = 0;
22993 bool any_deductions = false;
22994
22995 tree tmpl1 = TREE_VALUE (pat1);
22996 tree tmpl2 = TREE_VALUE (pat2);
22997 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22998 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22999
23000 /* Just like what happens for functions, if we are ordering between
23001 different template specializations, we may encounter dependent
23002 types in the arguments, and we need our dependency check functions
23003 to behave correctly. */
23004 ++processing_template_decl;
23005 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23006 if (targs)
23007 {
23008 --winner;
23009 any_deductions = true;
23010 }
23011
23012 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23013 if (targs)
23014 {
23015 ++winner;
23016 any_deductions = true;
23017 }
23018 --processing_template_decl;
23019
23020 /* If both deductions succeed, the partial ordering selects the more
23021 constrained template. */
23022 if (!winner && any_deductions)
23023 return more_constrained (tmpl1, tmpl2);
23024
23025 /* In the case of a tie where at least one of the templates
23026 has a parameter pack at the end, the template with the most
23027 non-packed parameters wins. */
23028 if (winner == 0
23029 && any_deductions
23030 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23031 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23032 {
23033 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23034 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23035 int len1 = TREE_VEC_LENGTH (args1);
23036 int len2 = TREE_VEC_LENGTH (args2);
23037
23038 /* We don't count the pack expansion at the end. */
23039 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23040 --len1;
23041 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23042 --len2;
23043
23044 if (len1 > len2)
23045 return 1;
23046 else if (len1 < len2)
23047 return -1;
23048 }
23049
23050 return winner;
23051 }
23052
23053 /* Return the template arguments that will produce the function signature
23054 DECL from the function template FN, with the explicit template
23055 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23056 also match. Return NULL_TREE if no satisfactory arguments could be
23057 found. */
23058
23059 static tree
23060 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23061 {
23062 int ntparms = DECL_NTPARMS (fn);
23063 tree targs = make_tree_vec (ntparms);
23064 tree decl_type = TREE_TYPE (decl);
23065 tree decl_arg_types;
23066 tree *args;
23067 unsigned int nargs, ix;
23068 tree arg;
23069
23070 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23071
23072 /* Never do unification on the 'this' parameter. */
23073 decl_arg_types = skip_artificial_parms_for (decl,
23074 TYPE_ARG_TYPES (decl_type));
23075
23076 nargs = list_length (decl_arg_types);
23077 args = XALLOCAVEC (tree, nargs);
23078 for (arg = decl_arg_types, ix = 0;
23079 arg != NULL_TREE && arg != void_list_node;
23080 arg = TREE_CHAIN (arg), ++ix)
23081 args[ix] = TREE_VALUE (arg);
23082
23083 if (fn_type_unification (fn, explicit_args, targs,
23084 args, ix,
23085 (check_rettype || DECL_CONV_FN_P (fn)
23086 ? TREE_TYPE (decl_type) : NULL_TREE),
23087 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23088 /*explain_p=*/false,
23089 /*decltype*/false)
23090 == error_mark_node)
23091 return NULL_TREE;
23092
23093 return targs;
23094 }
23095
23096 /* Return the innermost template arguments that, when applied to a partial
23097 specialization SPEC_TMPL of TMPL, yield the ARGS.
23098
23099 For example, suppose we have:
23100
23101 template <class T, class U> struct S {};
23102 template <class T> struct S<T*, int> {};
23103
23104 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23105 partial specialization and the ARGS will be {double*, int}. The resulting
23106 vector will be {double}, indicating that `T' is bound to `double'. */
23107
23108 static tree
23109 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23110 {
23111 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23112 tree spec_args
23113 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23114 int i, ntparms = TREE_VEC_LENGTH (tparms);
23115 tree deduced_args;
23116 tree innermost_deduced_args;
23117
23118 innermost_deduced_args = make_tree_vec (ntparms);
23119 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23120 {
23121 deduced_args = copy_node (args);
23122 SET_TMPL_ARGS_LEVEL (deduced_args,
23123 TMPL_ARGS_DEPTH (deduced_args),
23124 innermost_deduced_args);
23125 }
23126 else
23127 deduced_args = innermost_deduced_args;
23128
23129 bool tried_array_deduction = (cxx_dialect < cxx17);
23130 again:
23131 if (unify (tparms, deduced_args,
23132 INNERMOST_TEMPLATE_ARGS (spec_args),
23133 INNERMOST_TEMPLATE_ARGS (args),
23134 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23135 return NULL_TREE;
23136
23137 for (i = 0; i < ntparms; ++i)
23138 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23139 {
23140 if (!tried_array_deduction)
23141 {
23142 try_array_deduction (tparms, innermost_deduced_args,
23143 INNERMOST_TEMPLATE_ARGS (spec_args));
23144 tried_array_deduction = true;
23145 if (TREE_VEC_ELT (innermost_deduced_args, i))
23146 goto again;
23147 }
23148 return NULL_TREE;
23149 }
23150
23151 if (!push_tinst_level (spec_tmpl, deduced_args))
23152 {
23153 excessive_deduction_depth = true;
23154 return NULL_TREE;
23155 }
23156
23157 /* Verify that nondeduced template arguments agree with the type
23158 obtained from argument deduction.
23159
23160 For example:
23161
23162 struct A { typedef int X; };
23163 template <class T, class U> struct C {};
23164 template <class T> struct C<T, typename T::X> {};
23165
23166 Then with the instantiation `C<A, int>', we can deduce that
23167 `T' is `A' but unify () does not check whether `typename T::X'
23168 is `int'. */
23169 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23170
23171 if (spec_args != error_mark_node)
23172 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23173 INNERMOST_TEMPLATE_ARGS (spec_args),
23174 tmpl, tf_none, false, false);
23175
23176 pop_tinst_level ();
23177
23178 if (spec_args == error_mark_node
23179 /* We only need to check the innermost arguments; the other
23180 arguments will always agree. */
23181 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23182 INNERMOST_TEMPLATE_ARGS (args)))
23183 return NULL_TREE;
23184
23185 /* Now that we have bindings for all of the template arguments,
23186 ensure that the arguments deduced for the template template
23187 parameters have compatible template parameter lists. See the use
23188 of template_template_parm_bindings_ok_p in fn_type_unification
23189 for more information. */
23190 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23191 return NULL_TREE;
23192
23193 return deduced_args;
23194 }
23195
23196 // Compare two function templates T1 and T2 by deducing bindings
23197 // from one against the other. If both deductions succeed, compare
23198 // constraints to see which is more constrained.
23199 static int
23200 more_specialized_inst (tree t1, tree t2)
23201 {
23202 int fate = 0;
23203 int count = 0;
23204
23205 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23206 {
23207 --fate;
23208 ++count;
23209 }
23210
23211 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23212 {
23213 ++fate;
23214 ++count;
23215 }
23216
23217 // If both deductions succeed, then one may be more constrained.
23218 if (count == 2 && fate == 0)
23219 fate = more_constrained (t1, t2);
23220
23221 return fate;
23222 }
23223
23224 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23225 Return the TREE_LIST node with the most specialized template, if
23226 any. If there is no most specialized template, the error_mark_node
23227 is returned.
23228
23229 Note that this function does not look at, or modify, the
23230 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23231 returned is one of the elements of INSTANTIATIONS, callers may
23232 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23233 and retrieve it from the value returned. */
23234
23235 tree
23236 most_specialized_instantiation (tree templates)
23237 {
23238 tree fn, champ;
23239
23240 ++processing_template_decl;
23241
23242 champ = templates;
23243 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23244 {
23245 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23246 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23247 if (fate == -1)
23248 champ = fn;
23249 else if (!fate)
23250 {
23251 /* Equally specialized, move to next function. If there
23252 is no next function, nothing's most specialized. */
23253 fn = TREE_CHAIN (fn);
23254 champ = fn;
23255 if (!fn)
23256 break;
23257 }
23258 }
23259
23260 if (champ)
23261 /* Now verify that champ is better than everything earlier in the
23262 instantiation list. */
23263 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23264 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23265 {
23266 champ = NULL_TREE;
23267 break;
23268 }
23269 }
23270
23271 processing_template_decl--;
23272
23273 if (!champ)
23274 return error_mark_node;
23275
23276 return champ;
23277 }
23278
23279 /* If DECL is a specialization of some template, return the most
23280 general such template. Otherwise, returns NULL_TREE.
23281
23282 For example, given:
23283
23284 template <class T> struct S { template <class U> void f(U); };
23285
23286 if TMPL is `template <class U> void S<int>::f(U)' this will return
23287 the full template. This function will not trace past partial
23288 specializations, however. For example, given in addition:
23289
23290 template <class T> struct S<T*> { template <class U> void f(U); };
23291
23292 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23293 `template <class T> template <class U> S<T*>::f(U)'. */
23294
23295 tree
23296 most_general_template (tree decl)
23297 {
23298 if (TREE_CODE (decl) != TEMPLATE_DECL)
23299 {
23300 if (tree tinfo = get_template_info (decl))
23301 decl = TI_TEMPLATE (tinfo);
23302 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23303 template friend, or a FIELD_DECL for a capture pack. */
23304 if (TREE_CODE (decl) != TEMPLATE_DECL)
23305 return NULL_TREE;
23306 }
23307
23308 /* Look for more and more general templates. */
23309 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23310 {
23311 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23312 (See cp-tree.h for details.) */
23313 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23314 break;
23315
23316 if (CLASS_TYPE_P (TREE_TYPE (decl))
23317 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23318 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23319 break;
23320
23321 /* Stop if we run into an explicitly specialized class template. */
23322 if (!DECL_NAMESPACE_SCOPE_P (decl)
23323 && DECL_CONTEXT (decl)
23324 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23325 break;
23326
23327 decl = DECL_TI_TEMPLATE (decl);
23328 }
23329
23330 return decl;
23331 }
23332
23333 /* Return the most specialized of the template partial specializations
23334 which can produce TARGET, a specialization of some class or variable
23335 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23336 a TEMPLATE_DECL node corresponding to the partial specialization, while
23337 the TREE_PURPOSE is the set of template arguments that must be
23338 substituted into the template pattern in order to generate TARGET.
23339
23340 If the choice of partial specialization is ambiguous, a diagnostic
23341 is issued, and the error_mark_node is returned. If there are no
23342 partial specializations matching TARGET, then NULL_TREE is
23343 returned, indicating that the primary template should be used. */
23344
23345 static tree
23346 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23347 {
23348 tree list = NULL_TREE;
23349 tree t;
23350 tree champ;
23351 int fate;
23352 bool ambiguous_p;
23353 tree outer_args = NULL_TREE;
23354 tree tmpl, args;
23355
23356 if (TYPE_P (target))
23357 {
23358 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23359 tmpl = TI_TEMPLATE (tinfo);
23360 args = TI_ARGS (tinfo);
23361 }
23362 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23363 {
23364 tmpl = TREE_OPERAND (target, 0);
23365 args = TREE_OPERAND (target, 1);
23366 }
23367 else if (VAR_P (target))
23368 {
23369 tree tinfo = DECL_TEMPLATE_INFO (target);
23370 tmpl = TI_TEMPLATE (tinfo);
23371 args = TI_ARGS (tinfo);
23372 }
23373 else
23374 gcc_unreachable ();
23375
23376 tree main_tmpl = most_general_template (tmpl);
23377
23378 /* For determining which partial specialization to use, only the
23379 innermost args are interesting. */
23380 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23381 {
23382 outer_args = strip_innermost_template_args (args, 1);
23383 args = INNERMOST_TEMPLATE_ARGS (args);
23384 }
23385
23386 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23387 {
23388 tree spec_args;
23389 tree spec_tmpl = TREE_VALUE (t);
23390
23391 if (outer_args)
23392 {
23393 /* Substitute in the template args from the enclosing class. */
23394 ++processing_template_decl;
23395 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23396 --processing_template_decl;
23397 }
23398
23399 if (spec_tmpl == error_mark_node)
23400 return error_mark_node;
23401
23402 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23403 if (spec_args)
23404 {
23405 if (outer_args)
23406 spec_args = add_to_template_args (outer_args, spec_args);
23407
23408 /* Keep the candidate only if the constraints are satisfied,
23409 or if we're not compiling with concepts. */
23410 if (!flag_concepts
23411 || constraints_satisfied_p (spec_tmpl, spec_args))
23412 {
23413 list = tree_cons (spec_args, TREE_VALUE (t), list);
23414 TREE_TYPE (list) = TREE_TYPE (t);
23415 }
23416 }
23417 }
23418
23419 if (! list)
23420 return NULL_TREE;
23421
23422 ambiguous_p = false;
23423 t = list;
23424 champ = t;
23425 t = TREE_CHAIN (t);
23426 for (; t; t = TREE_CHAIN (t))
23427 {
23428 fate = more_specialized_partial_spec (tmpl, champ, t);
23429 if (fate == 1)
23430 ;
23431 else
23432 {
23433 if (fate == 0)
23434 {
23435 t = TREE_CHAIN (t);
23436 if (! t)
23437 {
23438 ambiguous_p = true;
23439 break;
23440 }
23441 }
23442 champ = t;
23443 }
23444 }
23445
23446 if (!ambiguous_p)
23447 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23448 {
23449 fate = more_specialized_partial_spec (tmpl, champ, t);
23450 if (fate != 1)
23451 {
23452 ambiguous_p = true;
23453 break;
23454 }
23455 }
23456
23457 if (ambiguous_p)
23458 {
23459 const char *str;
23460 char *spaces = NULL;
23461 if (!(complain & tf_error))
23462 return error_mark_node;
23463 if (TYPE_P (target))
23464 error ("ambiguous template instantiation for %q#T", target);
23465 else
23466 error ("ambiguous template instantiation for %q#D", target);
23467 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23468 for (t = list; t; t = TREE_CHAIN (t))
23469 {
23470 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23471 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23472 "%s %#qS", spaces ? spaces : str, subst);
23473 spaces = spaces ? spaces : get_spaces (str);
23474 }
23475 free (spaces);
23476 return error_mark_node;
23477 }
23478
23479 return champ;
23480 }
23481
23482 /* Explicitly instantiate DECL. */
23483
23484 void
23485 do_decl_instantiation (tree decl, tree storage)
23486 {
23487 tree result = NULL_TREE;
23488 int extern_p = 0;
23489
23490 if (!decl || decl == error_mark_node)
23491 /* An error occurred, for which grokdeclarator has already issued
23492 an appropriate message. */
23493 return;
23494 else if (! DECL_LANG_SPECIFIC (decl))
23495 {
23496 error ("explicit instantiation of non-template %q#D", decl);
23497 return;
23498 }
23499 else if (DECL_DECLARED_CONCEPT_P (decl))
23500 {
23501 if (VAR_P (decl))
23502 error ("explicit instantiation of variable concept %q#D", decl);
23503 else
23504 error ("explicit instantiation of function concept %q#D", decl);
23505 return;
23506 }
23507
23508 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23509 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23510
23511 if (VAR_P (decl) && !var_templ)
23512 {
23513 /* There is an asymmetry here in the way VAR_DECLs and
23514 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23515 the latter, the DECL we get back will be marked as a
23516 template instantiation, and the appropriate
23517 DECL_TEMPLATE_INFO will be set up. This does not happen for
23518 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23519 should handle VAR_DECLs as it currently handles
23520 FUNCTION_DECLs. */
23521 if (!DECL_CLASS_SCOPE_P (decl))
23522 {
23523 error ("%qD is not a static data member of a class template", decl);
23524 return;
23525 }
23526 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23527 if (!result || !VAR_P (result))
23528 {
23529 error ("no matching template for %qD found", decl);
23530 return;
23531 }
23532 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23533 {
23534 error ("type %qT for explicit instantiation %qD does not match "
23535 "declared type %qT", TREE_TYPE (result), decl,
23536 TREE_TYPE (decl));
23537 return;
23538 }
23539 }
23540 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23541 {
23542 error ("explicit instantiation of %q#D", decl);
23543 return;
23544 }
23545 else
23546 result = decl;
23547
23548 /* Check for various error cases. Note that if the explicit
23549 instantiation is valid the RESULT will currently be marked as an
23550 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23551 until we get here. */
23552
23553 if (DECL_TEMPLATE_SPECIALIZATION (result))
23554 {
23555 /* DR 259 [temp.spec].
23556
23557 Both an explicit instantiation and a declaration of an explicit
23558 specialization shall not appear in a program unless the explicit
23559 instantiation follows a declaration of the explicit specialization.
23560
23561 For a given set of template parameters, if an explicit
23562 instantiation of a template appears after a declaration of an
23563 explicit specialization for that template, the explicit
23564 instantiation has no effect. */
23565 return;
23566 }
23567 else if (DECL_EXPLICIT_INSTANTIATION (result))
23568 {
23569 /* [temp.spec]
23570
23571 No program shall explicitly instantiate any template more
23572 than once.
23573
23574 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23575 the first instantiation was `extern' and the second is not,
23576 and EXTERN_P for the opposite case. */
23577 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23578 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23579 /* If an "extern" explicit instantiation follows an ordinary
23580 explicit instantiation, the template is instantiated. */
23581 if (extern_p)
23582 return;
23583 }
23584 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23585 {
23586 error ("no matching template for %qD found", result);
23587 return;
23588 }
23589 else if (!DECL_TEMPLATE_INFO (result))
23590 {
23591 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23592 return;
23593 }
23594
23595 if (storage == NULL_TREE)
23596 ;
23597 else if (storage == ridpointers[(int) RID_EXTERN])
23598 {
23599 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23600 pedwarn (input_location, OPT_Wpedantic,
23601 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23602 "instantiations");
23603 extern_p = 1;
23604 }
23605 else
23606 error ("storage class %qD applied to template instantiation", storage);
23607
23608 check_explicit_instantiation_namespace (result);
23609 mark_decl_instantiated (result, extern_p);
23610 if (! extern_p)
23611 instantiate_decl (result, /*defer_ok=*/true,
23612 /*expl_inst_class_mem_p=*/false);
23613 }
23614
23615 static void
23616 mark_class_instantiated (tree t, int extern_p)
23617 {
23618 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23619 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23620 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23621 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23622 if (! extern_p)
23623 {
23624 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23625 rest_of_type_compilation (t, 1);
23626 }
23627 }
23628
23629 /* Called from do_type_instantiation through binding_table_foreach to
23630 do recursive instantiation for the type bound in ENTRY. */
23631 static void
23632 bt_instantiate_type_proc (binding_entry entry, void *data)
23633 {
23634 tree storage = *(tree *) data;
23635
23636 if (MAYBE_CLASS_TYPE_P (entry->type)
23637 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23638 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23639 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23640 }
23641
23642 /* Perform an explicit instantiation of template class T. STORAGE, if
23643 non-null, is the RID for extern, inline or static. COMPLAIN is
23644 nonzero if this is called from the parser, zero if called recursively,
23645 since the standard is unclear (as detailed below). */
23646
23647 void
23648 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23649 {
23650 int extern_p = 0;
23651 int nomem_p = 0;
23652 int static_p = 0;
23653 int previous_instantiation_extern_p = 0;
23654
23655 if (TREE_CODE (t) == TYPE_DECL)
23656 t = TREE_TYPE (t);
23657
23658 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23659 {
23660 tree tmpl =
23661 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23662 if (tmpl)
23663 error ("explicit instantiation of non-class template %qD", tmpl);
23664 else
23665 error ("explicit instantiation of non-template type %qT", t);
23666 return;
23667 }
23668
23669 complete_type (t);
23670
23671 if (!COMPLETE_TYPE_P (t))
23672 {
23673 if (complain & tf_error)
23674 error ("explicit instantiation of %q#T before definition of template",
23675 t);
23676 return;
23677 }
23678
23679 if (storage != NULL_TREE)
23680 {
23681 if (!in_system_header_at (input_location))
23682 {
23683 if (storage == ridpointers[(int) RID_EXTERN])
23684 {
23685 if (cxx_dialect == cxx98)
23686 pedwarn (input_location, OPT_Wpedantic,
23687 "ISO C++ 1998 forbids the use of %<extern%> on "
23688 "explicit instantiations");
23689 }
23690 else
23691 pedwarn (input_location, OPT_Wpedantic,
23692 "ISO C++ forbids the use of %qE"
23693 " on explicit instantiations", storage);
23694 }
23695
23696 if (storage == ridpointers[(int) RID_INLINE])
23697 nomem_p = 1;
23698 else if (storage == ridpointers[(int) RID_EXTERN])
23699 extern_p = 1;
23700 else if (storage == ridpointers[(int) RID_STATIC])
23701 static_p = 1;
23702 else
23703 {
23704 error ("storage class %qD applied to template instantiation",
23705 storage);
23706 extern_p = 0;
23707 }
23708 }
23709
23710 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23711 {
23712 /* DR 259 [temp.spec].
23713
23714 Both an explicit instantiation and a declaration of an explicit
23715 specialization shall not appear in a program unless the explicit
23716 instantiation follows a declaration of the explicit specialization.
23717
23718 For a given set of template parameters, if an explicit
23719 instantiation of a template appears after a declaration of an
23720 explicit specialization for that template, the explicit
23721 instantiation has no effect. */
23722 return;
23723 }
23724 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23725 {
23726 /* [temp.spec]
23727
23728 No program shall explicitly instantiate any template more
23729 than once.
23730
23731 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23732 instantiation was `extern'. If EXTERN_P then the second is.
23733 These cases are OK. */
23734 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23735
23736 if (!previous_instantiation_extern_p && !extern_p
23737 && (complain & tf_error))
23738 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23739
23740 /* If we've already instantiated the template, just return now. */
23741 if (!CLASSTYPE_INTERFACE_ONLY (t))
23742 return;
23743 }
23744
23745 check_explicit_instantiation_namespace (TYPE_NAME (t));
23746 mark_class_instantiated (t, extern_p);
23747
23748 if (nomem_p)
23749 return;
23750
23751 /* In contrast to implicit instantiation, where only the
23752 declarations, and not the definitions, of members are
23753 instantiated, we have here:
23754
23755 [temp.explicit]
23756
23757 The explicit instantiation of a class template specialization
23758 implies the instantiation of all of its members not
23759 previously explicitly specialized in the translation unit
23760 containing the explicit instantiation.
23761
23762 Of course, we can't instantiate member template classes, since we
23763 don't have any arguments for them. Note that the standard is
23764 unclear on whether the instantiation of the members are
23765 *explicit* instantiations or not. However, the most natural
23766 interpretation is that it should be an explicit
23767 instantiation. */
23768 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23769 if ((VAR_P (fld)
23770 || (TREE_CODE (fld) == FUNCTION_DECL
23771 && !static_p
23772 && user_provided_p (fld)))
23773 && DECL_TEMPLATE_INSTANTIATION (fld))
23774 {
23775 mark_decl_instantiated (fld, extern_p);
23776 if (! extern_p)
23777 instantiate_decl (fld, /*defer_ok=*/true,
23778 /*expl_inst_class_mem_p=*/true);
23779 }
23780
23781 if (CLASSTYPE_NESTED_UTDS (t))
23782 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
23783 bt_instantiate_type_proc, &storage);
23784 }
23785
23786 /* Given a function DECL, which is a specialization of TMPL, modify
23787 DECL to be a re-instantiation of TMPL with the same template
23788 arguments. TMPL should be the template into which tsubst'ing
23789 should occur for DECL, not the most general template.
23790
23791 One reason for doing this is a scenario like this:
23792
23793 template <class T>
23794 void f(const T&, int i);
23795
23796 void g() { f(3, 7); }
23797
23798 template <class T>
23799 void f(const T& t, const int i) { }
23800
23801 Note that when the template is first instantiated, with
23802 instantiate_template, the resulting DECL will have no name for the
23803 first parameter, and the wrong type for the second. So, when we go
23804 to instantiate the DECL, we regenerate it. */
23805
23806 static void
23807 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
23808 {
23809 /* The arguments used to instantiate DECL, from the most general
23810 template. */
23811 tree code_pattern;
23812
23813 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
23814
23815 /* Make sure that we can see identifiers, and compute access
23816 correctly. */
23817 push_access_scope (decl);
23818
23819 if (TREE_CODE (decl) == FUNCTION_DECL)
23820 {
23821 tree decl_parm;
23822 tree pattern_parm;
23823 tree specs;
23824 int args_depth;
23825 int parms_depth;
23826
23827 args_depth = TMPL_ARGS_DEPTH (args);
23828 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23829 if (args_depth > parms_depth)
23830 args = get_innermost_template_args (args, parms_depth);
23831
23832 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23833 args, tf_error, NULL_TREE,
23834 /*defer_ok*/false);
23835 if (specs && specs != error_mark_node)
23836 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23837 specs);
23838
23839 /* Merge parameter declarations. */
23840 decl_parm = skip_artificial_parms_for (decl,
23841 DECL_ARGUMENTS (decl));
23842 pattern_parm
23843 = skip_artificial_parms_for (code_pattern,
23844 DECL_ARGUMENTS (code_pattern));
23845 while (decl_parm && !DECL_PACK_P (pattern_parm))
23846 {
23847 tree parm_type;
23848 tree attributes;
23849
23850 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23851 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23852 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23853 NULL_TREE);
23854 parm_type = type_decays_to (parm_type);
23855 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23856 TREE_TYPE (decl_parm) = parm_type;
23857 attributes = DECL_ATTRIBUTES (pattern_parm);
23858 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23859 {
23860 DECL_ATTRIBUTES (decl_parm) = attributes;
23861 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23862 }
23863 decl_parm = DECL_CHAIN (decl_parm);
23864 pattern_parm = DECL_CHAIN (pattern_parm);
23865 }
23866 /* Merge any parameters that match with the function parameter
23867 pack. */
23868 if (pattern_parm && DECL_PACK_P (pattern_parm))
23869 {
23870 int i, len;
23871 tree expanded_types;
23872 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23873 the parameters in this function parameter pack. */
23874 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23875 args, tf_error, NULL_TREE);
23876 len = TREE_VEC_LENGTH (expanded_types);
23877 for (i = 0; i < len; i++)
23878 {
23879 tree parm_type;
23880 tree attributes;
23881
23882 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23883 /* Rename the parameter to include the index. */
23884 DECL_NAME (decl_parm) =
23885 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23886 parm_type = TREE_VEC_ELT (expanded_types, i);
23887 parm_type = type_decays_to (parm_type);
23888 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23889 TREE_TYPE (decl_parm) = parm_type;
23890 attributes = DECL_ATTRIBUTES (pattern_parm);
23891 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23892 {
23893 DECL_ATTRIBUTES (decl_parm) = attributes;
23894 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23895 }
23896 decl_parm = DECL_CHAIN (decl_parm);
23897 }
23898 }
23899 /* Merge additional specifiers from the CODE_PATTERN. */
23900 if (DECL_DECLARED_INLINE_P (code_pattern)
23901 && !DECL_DECLARED_INLINE_P (decl))
23902 DECL_DECLARED_INLINE_P (decl) = 1;
23903 }
23904 else if (VAR_P (decl))
23905 {
23906 start_lambda_scope (decl);
23907 DECL_INITIAL (decl) =
23908 tsubst_expr (DECL_INITIAL (code_pattern), args,
23909 tf_error, DECL_TI_TEMPLATE (decl),
23910 /*integral_constant_expression_p=*/false);
23911 finish_lambda_scope ();
23912 if (VAR_HAD_UNKNOWN_BOUND (decl))
23913 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23914 tf_error, DECL_TI_TEMPLATE (decl));
23915 }
23916 else
23917 gcc_unreachable ();
23918
23919 pop_access_scope (decl);
23920 }
23921
23922 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23923 substituted to get DECL. */
23924
23925 tree
23926 template_for_substitution (tree decl)
23927 {
23928 tree tmpl = DECL_TI_TEMPLATE (decl);
23929
23930 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23931 for the instantiation. This is not always the most general
23932 template. Consider, for example:
23933
23934 template <class T>
23935 struct S { template <class U> void f();
23936 template <> void f<int>(); };
23937
23938 and an instantiation of S<double>::f<int>. We want TD to be the
23939 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23940 while (/* An instantiation cannot have a definition, so we need a
23941 more general template. */
23942 DECL_TEMPLATE_INSTANTIATION (tmpl)
23943 /* We must also deal with friend templates. Given:
23944
23945 template <class T> struct S {
23946 template <class U> friend void f() {};
23947 };
23948
23949 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23950 so far as the language is concerned, but that's still
23951 where we get the pattern for the instantiation from. On
23952 other hand, if the definition comes outside the class, say:
23953
23954 template <class T> struct S {
23955 template <class U> friend void f();
23956 };
23957 template <class U> friend void f() {}
23958
23959 we don't need to look any further. That's what the check for
23960 DECL_INITIAL is for. */
23961 || (TREE_CODE (decl) == FUNCTION_DECL
23962 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23963 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23964 {
23965 /* The present template, TD, should not be a definition. If it
23966 were a definition, we should be using it! Note that we
23967 cannot restructure the loop to just keep going until we find
23968 a template with a definition, since that might go too far if
23969 a specialization was declared, but not defined. */
23970
23971 /* Fetch the more general template. */
23972 tmpl = DECL_TI_TEMPLATE (tmpl);
23973 }
23974
23975 return tmpl;
23976 }
23977
23978 /* Returns true if we need to instantiate this template instance even if we
23979 know we aren't going to emit it. */
23980
23981 bool
23982 always_instantiate_p (tree decl)
23983 {
23984 /* We always instantiate inline functions so that we can inline them. An
23985 explicit instantiation declaration prohibits implicit instantiation of
23986 non-inline functions. With high levels of optimization, we would
23987 normally inline non-inline functions -- but we're not allowed to do
23988 that for "extern template" functions. Therefore, we check
23989 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23990 return ((TREE_CODE (decl) == FUNCTION_DECL
23991 && (DECL_DECLARED_INLINE_P (decl)
23992 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23993 /* And we need to instantiate static data members so that
23994 their initializers are available in integral constant
23995 expressions. */
23996 || (VAR_P (decl)
23997 && decl_maybe_constant_var_p (decl)));
23998 }
23999
24000 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24001 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24002 error, true otherwise. */
24003
24004 bool
24005 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24006 {
24007 tree fntype, spec, noex, clone;
24008
24009 /* Don't instantiate a noexcept-specification from template context. */
24010 if (processing_template_decl
24011 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24012 return true;
24013
24014 if (DECL_CLONED_FUNCTION_P (fn))
24015 fn = DECL_CLONED_FUNCTION (fn);
24016 fntype = TREE_TYPE (fn);
24017 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24018
24019 if (!spec || !TREE_PURPOSE (spec))
24020 return true;
24021
24022 noex = TREE_PURPOSE (spec);
24023
24024 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24025 {
24026 static hash_set<tree>* fns = new hash_set<tree>;
24027 bool added = false;
24028 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24029 spec = get_defaulted_eh_spec (fn, complain);
24030 else if (!(added = !fns->add (fn)))
24031 {
24032 /* If hash_set::add returns true, the element was already there. */
24033 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24034 DECL_SOURCE_LOCATION (fn));
24035 error_at (loc,
24036 "exception specification of %qD depends on itself",
24037 fn);
24038 spec = noexcept_false_spec;
24039 }
24040 else if (push_tinst_level (fn))
24041 {
24042 push_access_scope (fn);
24043 push_deferring_access_checks (dk_no_deferred);
24044 input_location = DECL_SOURCE_LOCATION (fn);
24045 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24046 DEFERRED_NOEXCEPT_ARGS (noex),
24047 tf_warning_or_error, fn,
24048 /*function_p=*/false,
24049 /*integral_constant_expression_p=*/true);
24050 spec = build_noexcept_spec (noex, tf_warning_or_error);
24051 pop_deferring_access_checks ();
24052 pop_access_scope (fn);
24053 pop_tinst_level ();
24054 if (spec == error_mark_node)
24055 spec = noexcept_false_spec;
24056 }
24057 else
24058 spec = noexcept_false_spec;
24059
24060 if (added)
24061 fns->remove (fn);
24062
24063 if (spec == error_mark_node)
24064 return false;
24065
24066 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24067 }
24068
24069 FOR_EACH_CLONE (clone, fn)
24070 {
24071 if (TREE_TYPE (clone) == fntype)
24072 TREE_TYPE (clone) = TREE_TYPE (fn);
24073 else
24074 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24075 }
24076
24077 return true;
24078 }
24079
24080 /* We're starting to process the function INST, an instantiation of PATTERN;
24081 add their parameters to local_specializations. */
24082
24083 static void
24084 register_parameter_specializations (tree pattern, tree inst)
24085 {
24086 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24087 tree spec_parm = DECL_ARGUMENTS (inst);
24088 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24089 {
24090 register_local_specialization (spec_parm, tmpl_parm);
24091 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24092 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24093 }
24094 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24095 {
24096 if (!DECL_PACK_P (tmpl_parm))
24097 {
24098 register_local_specialization (spec_parm, tmpl_parm);
24099 spec_parm = DECL_CHAIN (spec_parm);
24100 }
24101 else
24102 {
24103 /* Register the (value) argument pack as a specialization of
24104 TMPL_PARM, then move on. */
24105 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24106 register_local_specialization (argpack, tmpl_parm);
24107 }
24108 }
24109 gcc_assert (!spec_parm);
24110 }
24111
24112 /* Produce the definition of D, a _DECL generated from a template. If
24113 DEFER_OK is true, then we don't have to actually do the
24114 instantiation now; we just have to do it sometime. Normally it is
24115 an error if this is an explicit instantiation but D is undefined.
24116 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24117 instantiated class template. */
24118
24119 tree
24120 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24121 {
24122 tree tmpl = DECL_TI_TEMPLATE (d);
24123 tree gen_args;
24124 tree args;
24125 tree td;
24126 tree code_pattern;
24127 tree spec;
24128 tree gen_tmpl;
24129 bool pattern_defined;
24130 location_t saved_loc = input_location;
24131 int saved_unevaluated_operand = cp_unevaluated_operand;
24132 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24133 bool external_p;
24134 bool deleted_p;
24135
24136 /* This function should only be used to instantiate templates for
24137 functions and static member variables. */
24138 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24139
24140 /* A concept is never instantiated. */
24141 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24142
24143 /* Variables are never deferred; if instantiation is required, they
24144 are instantiated right away. That allows for better code in the
24145 case that an expression refers to the value of the variable --
24146 if the variable has a constant value the referring expression can
24147 take advantage of that fact. */
24148 if (VAR_P (d))
24149 defer_ok = false;
24150
24151 /* Don't instantiate cloned functions. Instead, instantiate the
24152 functions they cloned. */
24153 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24154 d = DECL_CLONED_FUNCTION (d);
24155
24156 if (DECL_TEMPLATE_INSTANTIATED (d)
24157 || (TREE_CODE (d) == FUNCTION_DECL
24158 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24159 || DECL_TEMPLATE_SPECIALIZATION (d))
24160 /* D has already been instantiated or explicitly specialized, so
24161 there's nothing for us to do here.
24162
24163 It might seem reasonable to check whether or not D is an explicit
24164 instantiation, and, if so, stop here. But when an explicit
24165 instantiation is deferred until the end of the compilation,
24166 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24167 the instantiation. */
24168 return d;
24169
24170 /* Check to see whether we know that this template will be
24171 instantiated in some other file, as with "extern template"
24172 extension. */
24173 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24174
24175 /* In general, we do not instantiate such templates. */
24176 if (external_p && !always_instantiate_p (d))
24177 return d;
24178
24179 gen_tmpl = most_general_template (tmpl);
24180 gen_args = DECL_TI_ARGS (d);
24181
24182 if (tmpl != gen_tmpl)
24183 /* We should already have the extra args. */
24184 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24185 == TMPL_ARGS_DEPTH (gen_args));
24186 /* And what's in the hash table should match D. */
24187 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24188 || spec == NULL_TREE);
24189
24190 /* This needs to happen before any tsubsting. */
24191 if (! push_tinst_level (d))
24192 return d;
24193
24194 timevar_push (TV_TEMPLATE_INST);
24195
24196 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24197 for the instantiation. */
24198 td = template_for_substitution (d);
24199 args = gen_args;
24200
24201 if (VAR_P (d))
24202 {
24203 /* Look up an explicit specialization, if any. */
24204 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24205 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24206 if (elt && elt != error_mark_node)
24207 {
24208 td = TREE_VALUE (elt);
24209 args = TREE_PURPOSE (elt);
24210 }
24211 }
24212
24213 code_pattern = DECL_TEMPLATE_RESULT (td);
24214
24215 /* We should never be trying to instantiate a member of a class
24216 template or partial specialization. */
24217 gcc_assert (d != code_pattern);
24218
24219 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24220 || DECL_TEMPLATE_SPECIALIZATION (td))
24221 /* In the case of a friend template whose definition is provided
24222 outside the class, we may have too many arguments. Drop the
24223 ones we don't need. The same is true for specializations. */
24224 args = get_innermost_template_args
24225 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24226
24227 if (TREE_CODE (d) == FUNCTION_DECL)
24228 {
24229 deleted_p = DECL_DELETED_FN (code_pattern);
24230 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24231 && DECL_INITIAL (code_pattern) != error_mark_node)
24232 || DECL_DEFAULTED_FN (code_pattern)
24233 || deleted_p);
24234 }
24235 else
24236 {
24237 deleted_p = false;
24238 if (DECL_CLASS_SCOPE_P (code_pattern))
24239 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
24240 || DECL_INLINE_VAR_P (code_pattern));
24241 else
24242 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24243 }
24244
24245 /* We may be in the middle of deferred access check. Disable it now. */
24246 push_deferring_access_checks (dk_no_deferred);
24247
24248 /* Unless an explicit instantiation directive has already determined
24249 the linkage of D, remember that a definition is available for
24250 this entity. */
24251 if (pattern_defined
24252 && !DECL_INTERFACE_KNOWN (d)
24253 && !DECL_NOT_REALLY_EXTERN (d))
24254 mark_definable (d);
24255
24256 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24257 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24258 input_location = DECL_SOURCE_LOCATION (d);
24259
24260 /* If D is a member of an explicitly instantiated class template,
24261 and no definition is available, treat it like an implicit
24262 instantiation. */
24263 if (!pattern_defined && expl_inst_class_mem_p
24264 && DECL_EXPLICIT_INSTANTIATION (d))
24265 {
24266 /* Leave linkage flags alone on instantiations with anonymous
24267 visibility. */
24268 if (TREE_PUBLIC (d))
24269 {
24270 DECL_NOT_REALLY_EXTERN (d) = 0;
24271 DECL_INTERFACE_KNOWN (d) = 0;
24272 }
24273 SET_DECL_IMPLICIT_INSTANTIATION (d);
24274 }
24275
24276 /* Defer all other templates, unless we have been explicitly
24277 forbidden from doing so. */
24278 if (/* If there is no definition, we cannot instantiate the
24279 template. */
24280 ! pattern_defined
24281 /* If it's OK to postpone instantiation, do so. */
24282 || defer_ok
24283 /* If this is a static data member that will be defined
24284 elsewhere, we don't want to instantiate the entire data
24285 member, but we do want to instantiate the initializer so that
24286 we can substitute that elsewhere. */
24287 || (external_p && VAR_P (d))
24288 /* Handle here a deleted function too, avoid generating
24289 its body (c++/61080). */
24290 || deleted_p)
24291 {
24292 /* The definition of the static data member is now required so
24293 we must substitute the initializer. */
24294 if (VAR_P (d)
24295 && !DECL_INITIAL (d)
24296 && DECL_INITIAL (code_pattern))
24297 {
24298 tree ns;
24299 tree init;
24300 bool const_init = false;
24301 bool enter_context = DECL_CLASS_SCOPE_P (d);
24302
24303 ns = decl_namespace_context (d);
24304 push_nested_namespace (ns);
24305 if (enter_context)
24306 push_nested_class (DECL_CONTEXT (d));
24307 init = tsubst_expr (DECL_INITIAL (code_pattern),
24308 args,
24309 tf_warning_or_error, NULL_TREE,
24310 /*integral_constant_expression_p=*/false);
24311 /* If instantiating the initializer involved instantiating this
24312 again, don't call cp_finish_decl twice. */
24313 if (!DECL_INITIAL (d))
24314 {
24315 /* Make sure the initializer is still constant, in case of
24316 circular dependency (template/instantiate6.C). */
24317 const_init
24318 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24319 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24320 /*asmspec_tree=*/NULL_TREE,
24321 LOOKUP_ONLYCONVERTING);
24322 }
24323 if (enter_context)
24324 pop_nested_class ();
24325 pop_nested_namespace (ns);
24326 }
24327
24328 /* We restore the source position here because it's used by
24329 add_pending_template. */
24330 input_location = saved_loc;
24331
24332 if (at_eof && !pattern_defined
24333 && DECL_EXPLICIT_INSTANTIATION (d)
24334 && DECL_NOT_REALLY_EXTERN (d))
24335 /* [temp.explicit]
24336
24337 The definition of a non-exported function template, a
24338 non-exported member function template, or a non-exported
24339 member function or static data member of a class template
24340 shall be present in every translation unit in which it is
24341 explicitly instantiated. */
24342 permerror (input_location, "explicit instantiation of %qD "
24343 "but no definition available", d);
24344
24345 /* If we're in unevaluated context, we just wanted to get the
24346 constant value; this isn't an odr use, so don't queue
24347 a full instantiation. */
24348 if (cp_unevaluated_operand != 0)
24349 goto out;
24350 /* ??? Historically, we have instantiated inline functions, even
24351 when marked as "extern template". */
24352 if (!(external_p && VAR_P (d)))
24353 add_pending_template (d);
24354 goto out;
24355 }
24356 /* Tell the repository that D is available in this translation unit
24357 -- and see if it is supposed to be instantiated here. */
24358 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24359 {
24360 /* In a PCH file, despite the fact that the repository hasn't
24361 requested instantiation in the PCH it is still possible that
24362 an instantiation will be required in a file that includes the
24363 PCH. */
24364 if (pch_file)
24365 add_pending_template (d);
24366 /* Instantiate inline functions so that the inliner can do its
24367 job, even though we'll not be emitting a copy of this
24368 function. */
24369 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24370 goto out;
24371 }
24372
24373 bool push_to_top, nested;
24374 tree fn_context;
24375 fn_context = decl_function_context (d);
24376 if (LAMBDA_FUNCTION_P (d))
24377 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24378 fn_context = NULL_TREE;
24379 nested = current_function_decl != NULL_TREE;
24380 push_to_top = !(nested && fn_context == current_function_decl);
24381
24382 vec<tree> omp_privatization_save;
24383 if (nested)
24384 save_omp_privatization_clauses (omp_privatization_save);
24385
24386 if (push_to_top)
24387 push_to_top_level ();
24388 else
24389 {
24390 gcc_assert (!processing_template_decl);
24391 push_function_context ();
24392 cp_unevaluated_operand = 0;
24393 c_inhibit_evaluation_warnings = 0;
24394 }
24395
24396 /* Mark D as instantiated so that recursive calls to
24397 instantiate_decl do not try to instantiate it again. */
24398 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24399
24400 /* Regenerate the declaration in case the template has been modified
24401 by a subsequent redeclaration. */
24402 regenerate_decl_from_template (d, td, args);
24403
24404 /* We already set the file and line above. Reset them now in case
24405 they changed as a result of calling regenerate_decl_from_template. */
24406 input_location = DECL_SOURCE_LOCATION (d);
24407
24408 if (VAR_P (d))
24409 {
24410 tree init;
24411 bool const_init = false;
24412
24413 /* Clear out DECL_RTL; whatever was there before may not be right
24414 since we've reset the type of the declaration. */
24415 SET_DECL_RTL (d, NULL);
24416 DECL_IN_AGGR_P (d) = 0;
24417
24418 /* The initializer is placed in DECL_INITIAL by
24419 regenerate_decl_from_template so we don't need to
24420 push/pop_access_scope again here. Pull it out so that
24421 cp_finish_decl can process it. */
24422 init = DECL_INITIAL (d);
24423 DECL_INITIAL (d) = NULL_TREE;
24424 DECL_INITIALIZED_P (d) = 0;
24425
24426 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24427 initializer. That function will defer actual emission until
24428 we have a chance to determine linkage. */
24429 DECL_EXTERNAL (d) = 0;
24430
24431 /* Enter the scope of D so that access-checking works correctly. */
24432 bool enter_context = DECL_CLASS_SCOPE_P (d);
24433 if (enter_context)
24434 push_nested_class (DECL_CONTEXT (d));
24435
24436 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24437 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24438
24439 if (enter_context)
24440 pop_nested_class ();
24441
24442 if (variable_template_p (gen_tmpl))
24443 note_variable_template_instantiation (d);
24444 }
24445 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24446 synthesize_method (d);
24447 else if (TREE_CODE (d) == FUNCTION_DECL)
24448 {
24449 /* Set up the list of local specializations. */
24450 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24451 tree block = NULL_TREE;
24452
24453 /* Set up context. */
24454 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24455 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24456 block = push_stmt_list ();
24457 else
24458 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24459
24460 /* Some typedefs referenced from within the template code need to be
24461 access checked at template instantiation time, i.e now. These
24462 types were added to the template at parsing time. Let's get those
24463 and perform the access checks then. */
24464 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24465 args);
24466
24467 /* Create substitution entries for the parameters. */
24468 register_parameter_specializations (code_pattern, d);
24469
24470 /* Substitute into the body of the function. */
24471 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24472 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24473 tf_warning_or_error, tmpl);
24474 else
24475 {
24476 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24477 tf_warning_or_error, tmpl,
24478 /*integral_constant_expression_p=*/false);
24479
24480 /* Set the current input_location to the end of the function
24481 so that finish_function knows where we are. */
24482 input_location
24483 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24484
24485 /* Remember if we saw an infinite loop in the template. */
24486 current_function_infinite_loop
24487 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24488 }
24489
24490 /* Finish the function. */
24491 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24492 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24493 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24494 else
24495 {
24496 d = finish_function (/*inline_p=*/false);
24497 expand_or_defer_fn (d);
24498 }
24499
24500 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24501 cp_check_omp_declare_reduction (d);
24502 }
24503
24504 /* We're not deferring instantiation any more. */
24505 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24506
24507 if (push_to_top)
24508 pop_from_top_level ();
24509 else
24510 pop_function_context ();
24511
24512 if (nested)
24513 restore_omp_privatization_clauses (omp_privatization_save);
24514
24515 out:
24516 pop_deferring_access_checks ();
24517 timevar_pop (TV_TEMPLATE_INST);
24518 pop_tinst_level ();
24519 input_location = saved_loc;
24520 cp_unevaluated_operand = saved_unevaluated_operand;
24521 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24522
24523 return d;
24524 }
24525
24526 /* Run through the list of templates that we wish we could
24527 instantiate, and instantiate any we can. RETRIES is the
24528 number of times we retry pending template instantiation. */
24529
24530 void
24531 instantiate_pending_templates (int retries)
24532 {
24533 int reconsider;
24534 location_t saved_loc = input_location;
24535
24536 /* Instantiating templates may trigger vtable generation. This in turn
24537 may require further template instantiations. We place a limit here
24538 to avoid infinite loop. */
24539 if (pending_templates && retries >= max_tinst_depth)
24540 {
24541 tree decl = pending_templates->tinst->maybe_get_node ();
24542
24543 fatal_error (input_location,
24544 "template instantiation depth exceeds maximum of %d"
24545 " instantiating %q+D, possibly from virtual table generation"
24546 " (use -ftemplate-depth= to increase the maximum)",
24547 max_tinst_depth, decl);
24548 if (TREE_CODE (decl) == FUNCTION_DECL)
24549 /* Pretend that we defined it. */
24550 DECL_INITIAL (decl) = error_mark_node;
24551 return;
24552 }
24553
24554 do
24555 {
24556 struct pending_template **t = &pending_templates;
24557 struct pending_template *last = NULL;
24558 reconsider = 0;
24559 while (*t)
24560 {
24561 tree instantiation = reopen_tinst_level ((*t)->tinst);
24562 bool complete = false;
24563
24564 if (TYPE_P (instantiation))
24565 {
24566 if (!COMPLETE_TYPE_P (instantiation))
24567 {
24568 instantiate_class_template (instantiation);
24569 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24570 for (tree fld = TYPE_FIELDS (instantiation);
24571 fld; fld = TREE_CHAIN (fld))
24572 if ((VAR_P (fld)
24573 || (TREE_CODE (fld) == FUNCTION_DECL
24574 && !DECL_ARTIFICIAL (fld)))
24575 && DECL_TEMPLATE_INSTANTIATION (fld))
24576 instantiate_decl (fld,
24577 /*defer_ok=*/false,
24578 /*expl_inst_class_mem_p=*/false);
24579
24580 if (COMPLETE_TYPE_P (instantiation))
24581 reconsider = 1;
24582 }
24583
24584 complete = COMPLETE_TYPE_P (instantiation);
24585 }
24586 else
24587 {
24588 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24589 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24590 {
24591 instantiation
24592 = instantiate_decl (instantiation,
24593 /*defer_ok=*/false,
24594 /*expl_inst_class_mem_p=*/false);
24595 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24596 reconsider = 1;
24597 }
24598
24599 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24600 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24601 }
24602
24603 if (complete)
24604 {
24605 /* If INSTANTIATION has been instantiated, then we don't
24606 need to consider it again in the future. */
24607 struct pending_template *drop = *t;
24608 *t = (*t)->next;
24609 set_refcount_ptr (drop->tinst);
24610 pending_template_freelist ().free (drop);
24611 }
24612 else
24613 {
24614 last = *t;
24615 t = &(*t)->next;
24616 }
24617 tinst_depth = 0;
24618 set_refcount_ptr (current_tinst_level);
24619 }
24620 last_pending_template = last;
24621 }
24622 while (reconsider);
24623
24624 input_location = saved_loc;
24625 }
24626
24627 /* Substitute ARGVEC into T, which is a list of initializers for
24628 either base class or a non-static data member. The TREE_PURPOSEs
24629 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24630 instantiate_decl. */
24631
24632 static tree
24633 tsubst_initializer_list (tree t, tree argvec)
24634 {
24635 tree inits = NULL_TREE;
24636 tree target_ctor = error_mark_node;
24637
24638 for (; t; t = TREE_CHAIN (t))
24639 {
24640 tree decl;
24641 tree init;
24642 tree expanded_bases = NULL_TREE;
24643 tree expanded_arguments = NULL_TREE;
24644 int i, len = 1;
24645
24646 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24647 {
24648 tree expr;
24649 tree arg;
24650
24651 /* Expand the base class expansion type into separate base
24652 classes. */
24653 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24654 tf_warning_or_error,
24655 NULL_TREE);
24656 if (expanded_bases == error_mark_node)
24657 continue;
24658
24659 /* We'll be building separate TREE_LISTs of arguments for
24660 each base. */
24661 len = TREE_VEC_LENGTH (expanded_bases);
24662 expanded_arguments = make_tree_vec (len);
24663 for (i = 0; i < len; i++)
24664 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24665
24666 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24667 expand each argument in the TREE_VALUE of t. */
24668 expr = make_node (EXPR_PACK_EXPANSION);
24669 PACK_EXPANSION_LOCAL_P (expr) = true;
24670 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24671 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24672
24673 if (TREE_VALUE (t) == void_type_node)
24674 /* VOID_TYPE_NODE is used to indicate
24675 value-initialization. */
24676 {
24677 for (i = 0; i < len; i++)
24678 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24679 }
24680 else
24681 {
24682 /* Substitute parameter packs into each argument in the
24683 TREE_LIST. */
24684 in_base_initializer = 1;
24685 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24686 {
24687 tree expanded_exprs;
24688
24689 /* Expand the argument. */
24690 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24691 expanded_exprs
24692 = tsubst_pack_expansion (expr, argvec,
24693 tf_warning_or_error,
24694 NULL_TREE);
24695 if (expanded_exprs == error_mark_node)
24696 continue;
24697
24698 /* Prepend each of the expanded expressions to the
24699 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24700 for (i = 0; i < len; i++)
24701 {
24702 TREE_VEC_ELT (expanded_arguments, i) =
24703 tree_cons (NULL_TREE,
24704 TREE_VEC_ELT (expanded_exprs, i),
24705 TREE_VEC_ELT (expanded_arguments, i));
24706 }
24707 }
24708 in_base_initializer = 0;
24709
24710 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24711 since we built them backwards. */
24712 for (i = 0; i < len; i++)
24713 {
24714 TREE_VEC_ELT (expanded_arguments, i) =
24715 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24716 }
24717 }
24718 }
24719
24720 for (i = 0; i < len; ++i)
24721 {
24722 if (expanded_bases)
24723 {
24724 decl = TREE_VEC_ELT (expanded_bases, i);
24725 decl = expand_member_init (decl);
24726 init = TREE_VEC_ELT (expanded_arguments, i);
24727 }
24728 else
24729 {
24730 tree tmp;
24731 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
24732 tf_warning_or_error, NULL_TREE);
24733
24734 decl = expand_member_init (decl);
24735 if (decl && !DECL_P (decl))
24736 in_base_initializer = 1;
24737
24738 init = TREE_VALUE (t);
24739 tmp = init;
24740 if (init != void_type_node)
24741 init = tsubst_expr (init, argvec,
24742 tf_warning_or_error, NULL_TREE,
24743 /*integral_constant_expression_p=*/false);
24744 if (init == NULL_TREE && tmp != NULL_TREE)
24745 /* If we had an initializer but it instantiated to nothing,
24746 value-initialize the object. This will only occur when
24747 the initializer was a pack expansion where the parameter
24748 packs used in that expansion were of length zero. */
24749 init = void_type_node;
24750 in_base_initializer = 0;
24751 }
24752
24753 if (target_ctor != error_mark_node
24754 && init != error_mark_node)
24755 {
24756 error ("mem-initializer for %qD follows constructor delegation",
24757 decl);
24758 return inits;
24759 }
24760 /* Look for a target constructor. */
24761 if (init != error_mark_node
24762 && decl && CLASS_TYPE_P (decl)
24763 && same_type_p (decl, current_class_type))
24764 {
24765 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
24766 if (inits)
24767 {
24768 error ("constructor delegation follows mem-initializer for %qD",
24769 TREE_PURPOSE (inits));
24770 continue;
24771 }
24772 target_ctor = init;
24773 }
24774
24775 if (decl)
24776 {
24777 init = build_tree_list (decl, init);
24778 TREE_CHAIN (init) = inits;
24779 inits = init;
24780 }
24781 }
24782 }
24783 return inits;
24784 }
24785
24786 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24787
24788 static void
24789 set_current_access_from_decl (tree decl)
24790 {
24791 if (TREE_PRIVATE (decl))
24792 current_access_specifier = access_private_node;
24793 else if (TREE_PROTECTED (decl))
24794 current_access_specifier = access_protected_node;
24795 else
24796 current_access_specifier = access_public_node;
24797 }
24798
24799 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24800 is the instantiation (which should have been created with
24801 start_enum) and ARGS are the template arguments to use. */
24802
24803 static void
24804 tsubst_enum (tree tag, tree newtag, tree args)
24805 {
24806 tree e;
24807
24808 if (SCOPED_ENUM_P (newtag))
24809 begin_scope (sk_scoped_enum, newtag);
24810
24811 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
24812 {
24813 tree value;
24814 tree decl;
24815
24816 decl = TREE_VALUE (e);
24817 /* Note that in a template enum, the TREE_VALUE is the
24818 CONST_DECL, not the corresponding INTEGER_CST. */
24819 value = tsubst_expr (DECL_INITIAL (decl),
24820 args, tf_warning_or_error, NULL_TREE,
24821 /*integral_constant_expression_p=*/true);
24822
24823 /* Give this enumeration constant the correct access. */
24824 set_current_access_from_decl (decl);
24825
24826 /* Actually build the enumerator itself. Here we're assuming that
24827 enumerators can't have dependent attributes. */
24828 build_enumerator (DECL_NAME (decl), value, newtag,
24829 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24830 }
24831
24832 if (SCOPED_ENUM_P (newtag))
24833 finish_scope ();
24834
24835 finish_enum_value_list (newtag);
24836 finish_enum (newtag);
24837
24838 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24839 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24840 }
24841
24842 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24843 its type -- but without substituting the innermost set of template
24844 arguments. So, innermost set of template parameters will appear in
24845 the type. */
24846
24847 tree
24848 get_mostly_instantiated_function_type (tree decl)
24849 {
24850 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24851 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24852 }
24853
24854 /* Return truthvalue if we're processing a template different from
24855 the last one involved in diagnostics. */
24856 bool
24857 problematic_instantiation_changed (void)
24858 {
24859 return current_tinst_level != last_error_tinst_level;
24860 }
24861
24862 /* Remember current template involved in diagnostics. */
24863 void
24864 record_last_problematic_instantiation (void)
24865 {
24866 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
24867 }
24868
24869 struct tinst_level *
24870 current_instantiation (void)
24871 {
24872 return current_tinst_level;
24873 }
24874
24875 /* Return TRUE if current_function_decl is being instantiated, false
24876 otherwise. */
24877
24878 bool
24879 instantiating_current_function_p (void)
24880 {
24881 return (current_instantiation ()
24882 && (current_instantiation ()->maybe_get_node ()
24883 == current_function_decl));
24884 }
24885
24886 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24887 type. Return false for ok, true for disallowed. Issue error and
24888 inform messages under control of COMPLAIN. */
24889
24890 static bool
24891 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24892 {
24893 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24894 return false;
24895 else if (TYPE_PTR_P (type))
24896 return false;
24897 else if (TYPE_REF_P (type)
24898 && !TYPE_REF_IS_RVALUE (type))
24899 return false;
24900 else if (TYPE_PTRMEM_P (type))
24901 return false;
24902 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24903 return false;
24904 else if (TREE_CODE (type) == TYPENAME_TYPE)
24905 return false;
24906 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24907 return false;
24908 else if (TREE_CODE (type) == NULLPTR_TYPE)
24909 return false;
24910 /* A bound template template parm could later be instantiated to have a valid
24911 nontype parm type via an alias template. */
24912 else if (cxx_dialect >= cxx11
24913 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24914 return false;
24915 else if (CLASS_TYPE_P (type))
24916 {
24917 if (cxx_dialect < cxx2a)
24918 {
24919 error ("non-type template parameters of class type only available "
24920 "with -std=c++2a or -std=gnu++2a");
24921 return true;
24922 }
24923 if (!complete_type_or_else (type, NULL_TREE))
24924 return true;
24925 if (!literal_type_p (type))
24926 {
24927 error ("%qT is not a valid type for a template non-type parameter "
24928 "because it is not literal", type);
24929 explain_non_literal_class (type);
24930 return true;
24931 }
24932 if (cp_has_mutable_p (type))
24933 {
24934 error ("%qT is not a valid type for a template non-type parameter "
24935 "because it has a mutable member", type);
24936 return true;
24937 }
24938 /* FIXME check op<=> and strong structural equality once spaceship is
24939 implemented. */
24940 return false;
24941 }
24942
24943 if (complain & tf_error)
24944 {
24945 if (type == error_mark_node)
24946 inform (input_location, "invalid template non-type parameter");
24947 else
24948 error ("%q#T is not a valid type for a template non-type parameter",
24949 type);
24950 }
24951 return true;
24952 }
24953
24954 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24955 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24956
24957 static bool
24958 dependent_type_p_r (tree type)
24959 {
24960 tree scope;
24961
24962 /* [temp.dep.type]
24963
24964 A type is dependent if it is:
24965
24966 -- a template parameter. Template template parameters are types
24967 for us (since TYPE_P holds true for them) so we handle
24968 them here. */
24969 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24970 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24971 return true;
24972 /* -- a qualified-id with a nested-name-specifier which contains a
24973 class-name that names a dependent type or whose unqualified-id
24974 names a dependent type. */
24975 if (TREE_CODE (type) == TYPENAME_TYPE)
24976 return true;
24977
24978 /* An alias template specialization can be dependent even if the
24979 resulting type is not. */
24980 if (dependent_alias_template_spec_p (type))
24981 return true;
24982
24983 /* -- a cv-qualified type where the cv-unqualified type is
24984 dependent.
24985 No code is necessary for this bullet; the code below handles
24986 cv-qualified types, and we don't want to strip aliases with
24987 TYPE_MAIN_VARIANT because of DR 1558. */
24988 /* -- a compound type constructed from any dependent type. */
24989 if (TYPE_PTRMEM_P (type))
24990 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24991 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24992 (type)));
24993 else if (INDIRECT_TYPE_P (type))
24994 return dependent_type_p (TREE_TYPE (type));
24995 else if (TREE_CODE (type) == FUNCTION_TYPE
24996 || TREE_CODE (type) == METHOD_TYPE)
24997 {
24998 tree arg_type;
24999
25000 if (dependent_type_p (TREE_TYPE (type)))
25001 return true;
25002 for (arg_type = TYPE_ARG_TYPES (type);
25003 arg_type;
25004 arg_type = TREE_CHAIN (arg_type))
25005 if (dependent_type_p (TREE_VALUE (arg_type)))
25006 return true;
25007 if (cxx_dialect >= cxx17)
25008 /* A value-dependent noexcept-specifier makes the type dependent. */
25009 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25010 if (tree noex = TREE_PURPOSE (spec))
25011 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25012 affect overload resolution and treating it as dependent breaks
25013 things. */
25014 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25015 && value_dependent_expression_p (noex))
25016 return true;
25017 return false;
25018 }
25019 /* -- an array type constructed from any dependent type or whose
25020 size is specified by a constant expression that is
25021 value-dependent.
25022
25023 We checked for type- and value-dependence of the bounds in
25024 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25025 if (TREE_CODE (type) == ARRAY_TYPE)
25026 {
25027 if (TYPE_DOMAIN (type)
25028 && dependent_type_p (TYPE_DOMAIN (type)))
25029 return true;
25030 return dependent_type_p (TREE_TYPE (type));
25031 }
25032
25033 /* -- a template-id in which either the template name is a template
25034 parameter ... */
25035 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25036 return true;
25037 /* ... or any of the template arguments is a dependent type or
25038 an expression that is type-dependent or value-dependent. */
25039 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25040 && (any_dependent_template_arguments_p
25041 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25042 return true;
25043
25044 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25045 dependent; if the argument of the `typeof' expression is not
25046 type-dependent, then it should already been have resolved. */
25047 if (TREE_CODE (type) == TYPEOF_TYPE
25048 || TREE_CODE (type) == DECLTYPE_TYPE
25049 || TREE_CODE (type) == UNDERLYING_TYPE)
25050 return true;
25051
25052 /* A template argument pack is dependent if any of its packed
25053 arguments are. */
25054 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25055 {
25056 tree args = ARGUMENT_PACK_ARGS (type);
25057 int i, len = TREE_VEC_LENGTH (args);
25058 for (i = 0; i < len; ++i)
25059 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25060 return true;
25061 }
25062
25063 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25064 be template parameters. */
25065 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25066 return true;
25067
25068 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25069 return true;
25070
25071 /* The standard does not specifically mention types that are local
25072 to template functions or local classes, but they should be
25073 considered dependent too. For example:
25074
25075 template <int I> void f() {
25076 enum E { a = I };
25077 S<sizeof (E)> s;
25078 }
25079
25080 The size of `E' cannot be known until the value of `I' has been
25081 determined. Therefore, `E' must be considered dependent. */
25082 scope = TYPE_CONTEXT (type);
25083 if (scope && TYPE_P (scope))
25084 return dependent_type_p (scope);
25085 /* Don't use type_dependent_expression_p here, as it can lead
25086 to infinite recursion trying to determine whether a lambda
25087 nested in a lambda is dependent (c++/47687). */
25088 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25089 && DECL_LANG_SPECIFIC (scope)
25090 && DECL_TEMPLATE_INFO (scope)
25091 && (any_dependent_template_arguments_p
25092 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25093 return true;
25094
25095 /* Other types are non-dependent. */
25096 return false;
25097 }
25098
25099 /* Returns TRUE if TYPE is dependent, in the sense of
25100 [temp.dep.type]. Note that a NULL type is considered dependent. */
25101
25102 bool
25103 dependent_type_p (tree type)
25104 {
25105 /* If there are no template parameters in scope, then there can't be
25106 any dependent types. */
25107 if (!processing_template_decl)
25108 {
25109 /* If we are not processing a template, then nobody should be
25110 providing us with a dependent type. */
25111 gcc_assert (type);
25112 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25113 return false;
25114 }
25115
25116 /* If the type is NULL, we have not computed a type for the entity
25117 in question; in that case, the type is dependent. */
25118 if (!type)
25119 return true;
25120
25121 /* Erroneous types can be considered non-dependent. */
25122 if (type == error_mark_node)
25123 return false;
25124
25125 /* Getting here with global_type_node means we improperly called this
25126 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25127 gcc_checking_assert (type != global_type_node);
25128
25129 /* If we have not already computed the appropriate value for TYPE,
25130 do so now. */
25131 if (!TYPE_DEPENDENT_P_VALID (type))
25132 {
25133 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25134 TYPE_DEPENDENT_P_VALID (type) = 1;
25135 }
25136
25137 return TYPE_DEPENDENT_P (type);
25138 }
25139
25140 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25141 lookup. In other words, a dependent type that is not the current
25142 instantiation. */
25143
25144 bool
25145 dependent_scope_p (tree scope)
25146 {
25147 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25148 && !currently_open_class (scope));
25149 }
25150
25151 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25152 an unknown base of 'this' (and is therefore instantiation-dependent). */
25153
25154 static bool
25155 unknown_base_ref_p (tree t)
25156 {
25157 if (!current_class_ptr)
25158 return false;
25159
25160 tree mem = TREE_OPERAND (t, 1);
25161 if (shared_member_p (mem))
25162 return false;
25163
25164 tree cur = current_nonlambda_class_type ();
25165 if (!any_dependent_bases_p (cur))
25166 return false;
25167
25168 tree ctx = TREE_OPERAND (t, 0);
25169 if (DERIVED_FROM_P (ctx, cur))
25170 return false;
25171
25172 return true;
25173 }
25174
25175 /* T is a SCOPE_REF; return whether we need to consider it
25176 instantiation-dependent so that we can check access at instantiation
25177 time even though we know which member it resolves to. */
25178
25179 static bool
25180 instantiation_dependent_scope_ref_p (tree t)
25181 {
25182 if (DECL_P (TREE_OPERAND (t, 1))
25183 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25184 && !unknown_base_ref_p (t)
25185 && accessible_in_template_p (TREE_OPERAND (t, 0),
25186 TREE_OPERAND (t, 1)))
25187 return false;
25188 else
25189 return true;
25190 }
25191
25192 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25193 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25194 expression. */
25195
25196 /* Note that this predicate is not appropriate for general expressions;
25197 only constant expressions (that satisfy potential_constant_expression)
25198 can be tested for value dependence. */
25199
25200 bool
25201 value_dependent_expression_p (tree expression)
25202 {
25203 if (!processing_template_decl || expression == NULL_TREE)
25204 return false;
25205
25206 /* A type-dependent expression is also value-dependent. */
25207 if (type_dependent_expression_p (expression))
25208 return true;
25209
25210 switch (TREE_CODE (expression))
25211 {
25212 case BASELINK:
25213 /* A dependent member function of the current instantiation. */
25214 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25215
25216 case FUNCTION_DECL:
25217 /* A dependent member function of the current instantiation. */
25218 if (DECL_CLASS_SCOPE_P (expression)
25219 && dependent_type_p (DECL_CONTEXT (expression)))
25220 return true;
25221 break;
25222
25223 case IDENTIFIER_NODE:
25224 /* A name that has not been looked up -- must be dependent. */
25225 return true;
25226
25227 case TEMPLATE_PARM_INDEX:
25228 /* A non-type template parm. */
25229 return true;
25230
25231 case CONST_DECL:
25232 /* A non-type template parm. */
25233 if (DECL_TEMPLATE_PARM_P (expression))
25234 return true;
25235 return value_dependent_expression_p (DECL_INITIAL (expression));
25236
25237 case VAR_DECL:
25238 /* A constant with literal type and is initialized
25239 with an expression that is value-dependent. */
25240 if (DECL_DEPENDENT_INIT_P (expression)
25241 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25242 || TYPE_REF_P (TREE_TYPE (expression)))
25243 return true;
25244 if (DECL_HAS_VALUE_EXPR_P (expression))
25245 {
25246 tree value_expr = DECL_VALUE_EXPR (expression);
25247 if (value_dependent_expression_p (value_expr))
25248 return true;
25249 }
25250 return false;
25251
25252 case DYNAMIC_CAST_EXPR:
25253 case STATIC_CAST_EXPR:
25254 case CONST_CAST_EXPR:
25255 case REINTERPRET_CAST_EXPR:
25256 case CAST_EXPR:
25257 case IMPLICIT_CONV_EXPR:
25258 /* These expressions are value-dependent if the type to which
25259 the cast occurs is dependent or the expression being casted
25260 is value-dependent. */
25261 {
25262 tree type = TREE_TYPE (expression);
25263
25264 if (dependent_type_p (type))
25265 return true;
25266
25267 /* A functional cast has a list of operands. */
25268 expression = TREE_OPERAND (expression, 0);
25269 if (!expression)
25270 {
25271 /* If there are no operands, it must be an expression such
25272 as "int()". This should not happen for aggregate types
25273 because it would form non-constant expressions. */
25274 gcc_assert (cxx_dialect >= cxx11
25275 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25276
25277 return false;
25278 }
25279
25280 if (TREE_CODE (expression) == TREE_LIST)
25281 return any_value_dependent_elements_p (expression);
25282
25283 return value_dependent_expression_p (expression);
25284 }
25285
25286 case SIZEOF_EXPR:
25287 if (SIZEOF_EXPR_TYPE_P (expression))
25288 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25289 /* FALLTHRU */
25290 case ALIGNOF_EXPR:
25291 case TYPEID_EXPR:
25292 /* A `sizeof' expression is value-dependent if the operand is
25293 type-dependent or is a pack expansion. */
25294 expression = TREE_OPERAND (expression, 0);
25295 if (PACK_EXPANSION_P (expression))
25296 return true;
25297 else if (TYPE_P (expression))
25298 return dependent_type_p (expression);
25299 return instantiation_dependent_uneval_expression_p (expression);
25300
25301 case AT_ENCODE_EXPR:
25302 /* An 'encode' expression is value-dependent if the operand is
25303 type-dependent. */
25304 expression = TREE_OPERAND (expression, 0);
25305 return dependent_type_p (expression);
25306
25307 case NOEXCEPT_EXPR:
25308 expression = TREE_OPERAND (expression, 0);
25309 return instantiation_dependent_uneval_expression_p (expression);
25310
25311 case SCOPE_REF:
25312 /* All instantiation-dependent expressions should also be considered
25313 value-dependent. */
25314 return instantiation_dependent_scope_ref_p (expression);
25315
25316 case COMPONENT_REF:
25317 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25318 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25319
25320 case NONTYPE_ARGUMENT_PACK:
25321 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25322 is value-dependent. */
25323 {
25324 tree values = ARGUMENT_PACK_ARGS (expression);
25325 int i, len = TREE_VEC_LENGTH (values);
25326
25327 for (i = 0; i < len; ++i)
25328 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25329 return true;
25330
25331 return false;
25332 }
25333
25334 case TRAIT_EXPR:
25335 {
25336 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25337
25338 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25339 return true;
25340
25341 if (!type2)
25342 return false;
25343
25344 if (TREE_CODE (type2) != TREE_LIST)
25345 return dependent_type_p (type2);
25346
25347 for (; type2; type2 = TREE_CHAIN (type2))
25348 if (dependent_type_p (TREE_VALUE (type2)))
25349 return true;
25350
25351 return false;
25352 }
25353
25354 case MODOP_EXPR:
25355 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25356 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25357
25358 case ARRAY_REF:
25359 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25360 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25361
25362 case ADDR_EXPR:
25363 {
25364 tree op = TREE_OPERAND (expression, 0);
25365 return (value_dependent_expression_p (op)
25366 || has_value_dependent_address (op));
25367 }
25368
25369 case REQUIRES_EXPR:
25370 /* Treat all requires-expressions as value-dependent so
25371 we don't try to fold them. */
25372 return true;
25373
25374 case TYPE_REQ:
25375 return dependent_type_p (TREE_OPERAND (expression, 0));
25376
25377 case CALL_EXPR:
25378 {
25379 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25380 return true;
25381 tree fn = get_callee_fndecl (expression);
25382 int i, nargs;
25383 nargs = call_expr_nargs (expression);
25384 for (i = 0; i < nargs; ++i)
25385 {
25386 tree op = CALL_EXPR_ARG (expression, i);
25387 /* In a call to a constexpr member function, look through the
25388 implicit ADDR_EXPR on the object argument so that it doesn't
25389 cause the call to be considered value-dependent. We also
25390 look through it in potential_constant_expression. */
25391 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25392 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25393 && TREE_CODE (op) == ADDR_EXPR)
25394 op = TREE_OPERAND (op, 0);
25395 if (value_dependent_expression_p (op))
25396 return true;
25397 }
25398 return false;
25399 }
25400
25401 case TEMPLATE_ID_EXPR:
25402 return variable_concept_p (TREE_OPERAND (expression, 0));
25403
25404 case CONSTRUCTOR:
25405 {
25406 unsigned ix;
25407 tree val;
25408 if (dependent_type_p (TREE_TYPE (expression)))
25409 return true;
25410 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25411 if (value_dependent_expression_p (val))
25412 return true;
25413 return false;
25414 }
25415
25416 case STMT_EXPR:
25417 /* Treat a GNU statement expression as dependent to avoid crashing
25418 under instantiate_non_dependent_expr; it can't be constant. */
25419 return true;
25420
25421 default:
25422 /* A constant expression is value-dependent if any subexpression is
25423 value-dependent. */
25424 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25425 {
25426 case tcc_reference:
25427 case tcc_unary:
25428 case tcc_comparison:
25429 case tcc_binary:
25430 case tcc_expression:
25431 case tcc_vl_exp:
25432 {
25433 int i, len = cp_tree_operand_length (expression);
25434
25435 for (i = 0; i < len; i++)
25436 {
25437 tree t = TREE_OPERAND (expression, i);
25438
25439 /* In some cases, some of the operands may be missing.
25440 (For example, in the case of PREDECREMENT_EXPR, the
25441 amount to increment by may be missing.) That doesn't
25442 make the expression dependent. */
25443 if (t && value_dependent_expression_p (t))
25444 return true;
25445 }
25446 }
25447 break;
25448 default:
25449 break;
25450 }
25451 break;
25452 }
25453
25454 /* The expression is not value-dependent. */
25455 return false;
25456 }
25457
25458 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25459 [temp.dep.expr]. Note that an expression with no type is
25460 considered dependent. Other parts of the compiler arrange for an
25461 expression with type-dependent subexpressions to have no type, so
25462 this function doesn't have to be fully recursive. */
25463
25464 bool
25465 type_dependent_expression_p (tree expression)
25466 {
25467 if (!processing_template_decl)
25468 return false;
25469
25470 if (expression == NULL_TREE || expression == error_mark_node)
25471 return false;
25472
25473 STRIP_ANY_LOCATION_WRAPPER (expression);
25474
25475 /* An unresolved name is always dependent. */
25476 if (identifier_p (expression)
25477 || TREE_CODE (expression) == USING_DECL
25478 || TREE_CODE (expression) == WILDCARD_DECL)
25479 return true;
25480
25481 /* A fold expression is type-dependent. */
25482 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25483 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25484 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25485 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25486 return true;
25487
25488 /* Some expression forms are never type-dependent. */
25489 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25490 || TREE_CODE (expression) == SIZEOF_EXPR
25491 || TREE_CODE (expression) == ALIGNOF_EXPR
25492 || TREE_CODE (expression) == AT_ENCODE_EXPR
25493 || TREE_CODE (expression) == NOEXCEPT_EXPR
25494 || TREE_CODE (expression) == TRAIT_EXPR
25495 || TREE_CODE (expression) == TYPEID_EXPR
25496 || TREE_CODE (expression) == DELETE_EXPR
25497 || TREE_CODE (expression) == VEC_DELETE_EXPR
25498 || TREE_CODE (expression) == THROW_EXPR
25499 || TREE_CODE (expression) == REQUIRES_EXPR)
25500 return false;
25501
25502 /* The types of these expressions depends only on the type to which
25503 the cast occurs. */
25504 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25505 || TREE_CODE (expression) == STATIC_CAST_EXPR
25506 || TREE_CODE (expression) == CONST_CAST_EXPR
25507 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25508 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25509 || TREE_CODE (expression) == CAST_EXPR)
25510 return dependent_type_p (TREE_TYPE (expression));
25511
25512 /* The types of these expressions depends only on the type created
25513 by the expression. */
25514 if (TREE_CODE (expression) == NEW_EXPR
25515 || TREE_CODE (expression) == VEC_NEW_EXPR)
25516 {
25517 /* For NEW_EXPR tree nodes created inside a template, either
25518 the object type itself or a TREE_LIST may appear as the
25519 operand 1. */
25520 tree type = TREE_OPERAND (expression, 1);
25521 if (TREE_CODE (type) == TREE_LIST)
25522 /* This is an array type. We need to check array dimensions
25523 as well. */
25524 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25525 || value_dependent_expression_p
25526 (TREE_OPERAND (TREE_VALUE (type), 1));
25527 else
25528 return dependent_type_p (type);
25529 }
25530
25531 if (TREE_CODE (expression) == SCOPE_REF)
25532 {
25533 tree scope = TREE_OPERAND (expression, 0);
25534 tree name = TREE_OPERAND (expression, 1);
25535
25536 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25537 contains an identifier associated by name lookup with one or more
25538 declarations declared with a dependent type, or...a
25539 nested-name-specifier or qualified-id that names a member of an
25540 unknown specialization. */
25541 return (type_dependent_expression_p (name)
25542 || dependent_scope_p (scope));
25543 }
25544
25545 if (TREE_CODE (expression) == TEMPLATE_DECL
25546 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25547 return uses_outer_template_parms (expression);
25548
25549 if (TREE_CODE (expression) == STMT_EXPR)
25550 expression = stmt_expr_value_expr (expression);
25551
25552 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25553 {
25554 tree elt;
25555 unsigned i;
25556
25557 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25558 {
25559 if (type_dependent_expression_p (elt))
25560 return true;
25561 }
25562 return false;
25563 }
25564
25565 /* A static data member of the current instantiation with incomplete
25566 array type is type-dependent, as the definition and specializations
25567 can have different bounds. */
25568 if (VAR_P (expression)
25569 && DECL_CLASS_SCOPE_P (expression)
25570 && dependent_type_p (DECL_CONTEXT (expression))
25571 && VAR_HAD_UNKNOWN_BOUND (expression))
25572 return true;
25573
25574 /* An array of unknown bound depending on a variadic parameter, eg:
25575
25576 template<typename... Args>
25577 void foo (Args... args)
25578 {
25579 int arr[] = { args... };
25580 }
25581
25582 template<int... vals>
25583 void bar ()
25584 {
25585 int arr[] = { vals... };
25586 }
25587
25588 If the array has no length and has an initializer, it must be that
25589 we couldn't determine its length in cp_complete_array_type because
25590 it is dependent. */
25591 if (VAR_P (expression)
25592 && TREE_TYPE (expression) != NULL_TREE
25593 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25594 && !TYPE_DOMAIN (TREE_TYPE (expression))
25595 && DECL_INITIAL (expression))
25596 return true;
25597
25598 /* A function or variable template-id is type-dependent if it has any
25599 dependent template arguments. */
25600 if (VAR_OR_FUNCTION_DECL_P (expression)
25601 && DECL_LANG_SPECIFIC (expression)
25602 && DECL_TEMPLATE_INFO (expression))
25603 {
25604 /* Consider the innermost template arguments, since those are the ones
25605 that come from the template-id; the template arguments for the
25606 enclosing class do not make it type-dependent unless they are used in
25607 the type of the decl. */
25608 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
25609 && (any_dependent_template_arguments_p
25610 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25611 return true;
25612 }
25613
25614 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25615 type-dependent. Checking this is important for functions with auto return
25616 type, which looks like a dependent type. */
25617 if (TREE_CODE (expression) == FUNCTION_DECL
25618 && !(DECL_CLASS_SCOPE_P (expression)
25619 && dependent_type_p (DECL_CONTEXT (expression)))
25620 && !(DECL_LANG_SPECIFIC (expression)
25621 && DECL_FRIEND_P (expression)
25622 && (!DECL_FRIEND_CONTEXT (expression)
25623 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25624 && !DECL_LOCAL_FUNCTION_P (expression))
25625 {
25626 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25627 || undeduced_auto_decl (expression));
25628 return false;
25629 }
25630
25631 /* Always dependent, on the number of arguments if nothing else. */
25632 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25633 return true;
25634
25635 if (TREE_TYPE (expression) == unknown_type_node)
25636 {
25637 if (TREE_CODE (expression) == ADDR_EXPR)
25638 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25639 if (TREE_CODE (expression) == COMPONENT_REF
25640 || TREE_CODE (expression) == OFFSET_REF)
25641 {
25642 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25643 return true;
25644 expression = TREE_OPERAND (expression, 1);
25645 if (identifier_p (expression))
25646 return false;
25647 }
25648 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25649 if (TREE_CODE (expression) == SCOPE_REF)
25650 return false;
25651
25652 if (BASELINK_P (expression))
25653 {
25654 if (BASELINK_OPTYPE (expression)
25655 && dependent_type_p (BASELINK_OPTYPE (expression)))
25656 return true;
25657 expression = BASELINK_FUNCTIONS (expression);
25658 }
25659
25660 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25661 {
25662 if (any_dependent_template_arguments_p
25663 (TREE_OPERAND (expression, 1)))
25664 return true;
25665 expression = TREE_OPERAND (expression, 0);
25666 if (identifier_p (expression))
25667 return true;
25668 }
25669
25670 gcc_assert (TREE_CODE (expression) == OVERLOAD
25671 || TREE_CODE (expression) == FUNCTION_DECL);
25672
25673 for (lkp_iterator iter (expression); iter; ++iter)
25674 if (type_dependent_expression_p (*iter))
25675 return true;
25676
25677 return false;
25678 }
25679
25680 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25681
25682 /* Dependent type attributes might not have made it from the decl to
25683 the type yet. */
25684 if (DECL_P (expression)
25685 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25686 return true;
25687
25688 return (dependent_type_p (TREE_TYPE (expression)));
25689 }
25690
25691 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25692 type-dependent if the expression refers to a member of the current
25693 instantiation and the type of the referenced member is dependent, or the
25694 class member access expression refers to a member of an unknown
25695 specialization.
25696
25697 This function returns true if the OBJECT in such a class member access
25698 expression is of an unknown specialization. */
25699
25700 bool
25701 type_dependent_object_expression_p (tree object)
25702 {
25703 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25704 dependent. */
25705 if (TREE_CODE (object) == IDENTIFIER_NODE)
25706 return true;
25707 tree scope = TREE_TYPE (object);
25708 return (!scope || dependent_scope_p (scope));
25709 }
25710
25711 /* walk_tree callback function for instantiation_dependent_expression_p,
25712 below. Returns non-zero if a dependent subexpression is found. */
25713
25714 static tree
25715 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25716 void * /*data*/)
25717 {
25718 if (TYPE_P (*tp))
25719 {
25720 /* We don't have to worry about decltype currently because decltype
25721 of an instantiation-dependent expr is a dependent type. This
25722 might change depending on the resolution of DR 1172. */
25723 *walk_subtrees = false;
25724 return NULL_TREE;
25725 }
25726 enum tree_code code = TREE_CODE (*tp);
25727 switch (code)
25728 {
25729 /* Don't treat an argument list as dependent just because it has no
25730 TREE_TYPE. */
25731 case TREE_LIST:
25732 case TREE_VEC:
25733 case NONTYPE_ARGUMENT_PACK:
25734 return NULL_TREE;
25735
25736 case TEMPLATE_PARM_INDEX:
25737 if (dependent_type_p (TREE_TYPE (*tp)))
25738 return *tp;
25739 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
25740 return *tp;
25741 /* We'll check value-dependence separately. */
25742 return NULL_TREE;
25743
25744 /* Handle expressions with type operands. */
25745 case SIZEOF_EXPR:
25746 case ALIGNOF_EXPR:
25747 case TYPEID_EXPR:
25748 case AT_ENCODE_EXPR:
25749 {
25750 tree op = TREE_OPERAND (*tp, 0);
25751 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
25752 op = TREE_TYPE (op);
25753 if (TYPE_P (op))
25754 {
25755 if (dependent_type_p (op))
25756 return *tp;
25757 else
25758 {
25759 *walk_subtrees = false;
25760 return NULL_TREE;
25761 }
25762 }
25763 break;
25764 }
25765
25766 case COMPONENT_REF:
25767 if (identifier_p (TREE_OPERAND (*tp, 1)))
25768 /* In a template, finish_class_member_access_expr creates a
25769 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25770 type-dependent, so that we can check access control at
25771 instantiation time (PR 42277). See also Core issue 1273. */
25772 return *tp;
25773 break;
25774
25775 case SCOPE_REF:
25776 if (instantiation_dependent_scope_ref_p (*tp))
25777 return *tp;
25778 else
25779 break;
25780
25781 /* Treat statement-expressions as dependent. */
25782 case BIND_EXPR:
25783 return *tp;
25784
25785 /* Treat requires-expressions as dependent. */
25786 case REQUIRES_EXPR:
25787 return *tp;
25788
25789 case CALL_EXPR:
25790 /* Treat calls to function concepts as dependent. */
25791 if (function_concept_check_p (*tp))
25792 return *tp;
25793 break;
25794
25795 case TEMPLATE_ID_EXPR:
25796 /* And variable concepts. */
25797 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
25798 return *tp;
25799 break;
25800
25801 default:
25802 break;
25803 }
25804
25805 if (type_dependent_expression_p (*tp))
25806 return *tp;
25807 else
25808 return NULL_TREE;
25809 }
25810
25811 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25812 sense defined by the ABI:
25813
25814 "An expression is instantiation-dependent if it is type-dependent
25815 or value-dependent, or it has a subexpression that is type-dependent
25816 or value-dependent."
25817
25818 Except don't actually check value-dependence for unevaluated expressions,
25819 because in sizeof(i) we don't care about the value of i. Checking
25820 type-dependence will in turn check value-dependence of array bounds/template
25821 arguments as needed. */
25822
25823 bool
25824 instantiation_dependent_uneval_expression_p (tree expression)
25825 {
25826 tree result;
25827
25828 if (!processing_template_decl)
25829 return false;
25830
25831 if (expression == error_mark_node)
25832 return false;
25833
25834 result = cp_walk_tree_without_duplicates (&expression,
25835 instantiation_dependent_r, NULL);
25836 return result != NULL_TREE;
25837 }
25838
25839 /* As above, but also check value-dependence of the expression as a whole. */
25840
25841 bool
25842 instantiation_dependent_expression_p (tree expression)
25843 {
25844 return (instantiation_dependent_uneval_expression_p (expression)
25845 || value_dependent_expression_p (expression));
25846 }
25847
25848 /* Like type_dependent_expression_p, but it also works while not processing
25849 a template definition, i.e. during substitution or mangling. */
25850
25851 bool
25852 type_dependent_expression_p_push (tree expr)
25853 {
25854 bool b;
25855 ++processing_template_decl;
25856 b = type_dependent_expression_p (expr);
25857 --processing_template_decl;
25858 return b;
25859 }
25860
25861 /* Returns TRUE if ARGS contains a type-dependent expression. */
25862
25863 bool
25864 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25865 {
25866 unsigned int i;
25867 tree arg;
25868
25869 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25870 {
25871 if (type_dependent_expression_p (arg))
25872 return true;
25873 }
25874 return false;
25875 }
25876
25877 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25878 expressions) contains any type-dependent expressions. */
25879
25880 bool
25881 any_type_dependent_elements_p (const_tree list)
25882 {
25883 for (; list; list = TREE_CHAIN (list))
25884 if (type_dependent_expression_p (TREE_VALUE (list)))
25885 return true;
25886
25887 return false;
25888 }
25889
25890 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25891 expressions) contains any value-dependent expressions. */
25892
25893 bool
25894 any_value_dependent_elements_p (const_tree list)
25895 {
25896 for (; list; list = TREE_CHAIN (list))
25897 if (value_dependent_expression_p (TREE_VALUE (list)))
25898 return true;
25899
25900 return false;
25901 }
25902
25903 /* Returns TRUE if the ARG (a template argument) is dependent. */
25904
25905 bool
25906 dependent_template_arg_p (tree arg)
25907 {
25908 if (!processing_template_decl)
25909 return false;
25910
25911 /* Assume a template argument that was wrongly written by the user
25912 is dependent. This is consistent with what
25913 any_dependent_template_arguments_p [that calls this function]
25914 does. */
25915 if (!arg || arg == error_mark_node)
25916 return true;
25917
25918 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25919 arg = argument_pack_select_arg (arg);
25920
25921 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25922 return true;
25923 if (TREE_CODE (arg) == TEMPLATE_DECL)
25924 {
25925 if (DECL_TEMPLATE_PARM_P (arg))
25926 return true;
25927 /* A member template of a dependent class is not necessarily
25928 type-dependent, but it is a dependent template argument because it
25929 will be a member of an unknown specialization to that template. */
25930 tree scope = CP_DECL_CONTEXT (arg);
25931 return TYPE_P (scope) && dependent_type_p (scope);
25932 }
25933 else if (ARGUMENT_PACK_P (arg))
25934 {
25935 tree args = ARGUMENT_PACK_ARGS (arg);
25936 int i, len = TREE_VEC_LENGTH (args);
25937 for (i = 0; i < len; ++i)
25938 {
25939 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25940 return true;
25941 }
25942
25943 return false;
25944 }
25945 else if (TYPE_P (arg))
25946 return dependent_type_p (arg);
25947 else
25948 return (type_dependent_expression_p (arg)
25949 || value_dependent_expression_p (arg));
25950 }
25951
25952 /* Returns true if ARGS (a collection of template arguments) contains
25953 any types that require structural equality testing. */
25954
25955 bool
25956 any_template_arguments_need_structural_equality_p (tree args)
25957 {
25958 int i;
25959 int j;
25960
25961 if (!args)
25962 return false;
25963 if (args == error_mark_node)
25964 return true;
25965
25966 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25967 {
25968 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25969 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25970 {
25971 tree arg = TREE_VEC_ELT (level, j);
25972 tree packed_args = NULL_TREE;
25973 int k, len = 1;
25974
25975 if (ARGUMENT_PACK_P (arg))
25976 {
25977 /* Look inside the argument pack. */
25978 packed_args = ARGUMENT_PACK_ARGS (arg);
25979 len = TREE_VEC_LENGTH (packed_args);
25980 }
25981
25982 for (k = 0; k < len; ++k)
25983 {
25984 if (packed_args)
25985 arg = TREE_VEC_ELT (packed_args, k);
25986
25987 if (error_operand_p (arg))
25988 return true;
25989 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25990 continue;
25991 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25992 return true;
25993 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25994 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25995 return true;
25996 }
25997 }
25998 }
25999
26000 return false;
26001 }
26002
26003 /* Returns true if ARGS (a collection of template arguments) contains
26004 any dependent arguments. */
26005
26006 bool
26007 any_dependent_template_arguments_p (const_tree args)
26008 {
26009 int i;
26010 int j;
26011
26012 if (!args)
26013 return false;
26014 if (args == error_mark_node)
26015 return true;
26016
26017 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26018 {
26019 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26020 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26021 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26022 return true;
26023 }
26024
26025 return false;
26026 }
26027
26028 /* Returns true if ARGS contains any errors. */
26029
26030 bool
26031 any_erroneous_template_args_p (const_tree args)
26032 {
26033 int i;
26034 int j;
26035
26036 if (args == error_mark_node)
26037 return true;
26038
26039 if (args && TREE_CODE (args) != TREE_VEC)
26040 {
26041 if (tree ti = get_template_info (args))
26042 args = TI_ARGS (ti);
26043 else
26044 args = NULL_TREE;
26045 }
26046
26047 if (!args)
26048 return false;
26049
26050 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26051 {
26052 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26053 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26054 if (error_operand_p (TREE_VEC_ELT (level, j)))
26055 return true;
26056 }
26057
26058 return false;
26059 }
26060
26061 /* Returns TRUE if the template TMPL is type-dependent. */
26062
26063 bool
26064 dependent_template_p (tree tmpl)
26065 {
26066 if (TREE_CODE (tmpl) == OVERLOAD)
26067 {
26068 for (lkp_iterator iter (tmpl); iter; ++iter)
26069 if (dependent_template_p (*iter))
26070 return true;
26071 return false;
26072 }
26073
26074 /* Template template parameters are dependent. */
26075 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26076 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26077 return true;
26078 /* So are names that have not been looked up. */
26079 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26080 return true;
26081 return false;
26082 }
26083
26084 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26085
26086 bool
26087 dependent_template_id_p (tree tmpl, tree args)
26088 {
26089 return (dependent_template_p (tmpl)
26090 || any_dependent_template_arguments_p (args));
26091 }
26092
26093 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26094 are dependent. */
26095
26096 bool
26097 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26098 {
26099 int i;
26100
26101 if (!processing_template_decl)
26102 return false;
26103
26104 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26105 {
26106 tree decl = TREE_VEC_ELT (declv, i);
26107 tree init = TREE_VEC_ELT (initv, i);
26108 tree cond = TREE_VEC_ELT (condv, i);
26109 tree incr = TREE_VEC_ELT (incrv, i);
26110
26111 if (type_dependent_expression_p (decl)
26112 || TREE_CODE (decl) == SCOPE_REF)
26113 return true;
26114
26115 if (init && type_dependent_expression_p (init))
26116 return true;
26117
26118 if (cond == global_namespace)
26119 return true;
26120
26121 if (type_dependent_expression_p (cond))
26122 return true;
26123
26124 if (COMPARISON_CLASS_P (cond)
26125 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26126 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26127 return true;
26128
26129 if (TREE_CODE (incr) == MODOP_EXPR)
26130 {
26131 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26132 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26133 return true;
26134 }
26135 else if (type_dependent_expression_p (incr))
26136 return true;
26137 else if (TREE_CODE (incr) == MODIFY_EXPR)
26138 {
26139 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26140 return true;
26141 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26142 {
26143 tree t = TREE_OPERAND (incr, 1);
26144 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26145 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26146 return true;
26147
26148 /* If this loop has a class iterator with != comparison
26149 with increment other than i++/++i/i--/--i, make sure the
26150 increment is constant. */
26151 if (CLASS_TYPE_P (TREE_TYPE (decl))
26152 && TREE_CODE (cond) == NE_EXPR)
26153 {
26154 if (TREE_OPERAND (t, 0) == decl)
26155 t = TREE_OPERAND (t, 1);
26156 else
26157 t = TREE_OPERAND (t, 0);
26158 if (TREE_CODE (t) != INTEGER_CST)
26159 return true;
26160 }
26161 }
26162 }
26163 }
26164
26165 return false;
26166 }
26167
26168 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26169 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26170 no such TYPE can be found. Note that this function peers inside
26171 uninstantiated templates and therefore should be used only in
26172 extremely limited situations. ONLY_CURRENT_P restricts this
26173 peering to the currently open classes hierarchy (which is required
26174 when comparing types). */
26175
26176 tree
26177 resolve_typename_type (tree type, bool only_current_p)
26178 {
26179 tree scope;
26180 tree name;
26181 tree decl;
26182 int quals;
26183 tree pushed_scope;
26184 tree result;
26185
26186 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26187
26188 scope = TYPE_CONTEXT (type);
26189 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26190 gcc_checking_assert (uses_template_parms (scope));
26191
26192 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26193 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26194 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26195 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26196 identifier of the TYPENAME_TYPE anymore.
26197 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26198 TYPENAME_TYPE instead, we avoid messing up with a possible
26199 typedef variant case. */
26200 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26201
26202 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26203 it first before we can figure out what NAME refers to. */
26204 if (TREE_CODE (scope) == TYPENAME_TYPE)
26205 {
26206 if (TYPENAME_IS_RESOLVING_P (scope))
26207 /* Given a class template A with a dependent base with nested type C,
26208 typedef typename A::C::C C will land us here, as trying to resolve
26209 the initial A::C leads to the local C typedef, which leads back to
26210 A::C::C. So we break the recursion now. */
26211 return type;
26212 else
26213 scope = resolve_typename_type (scope, only_current_p);
26214 }
26215 /* If we don't know what SCOPE refers to, then we cannot resolve the
26216 TYPENAME_TYPE. */
26217 if (!CLASS_TYPE_P (scope))
26218 return type;
26219 /* If this is a typedef, we don't want to look inside (c++/11987). */
26220 if (typedef_variant_p (type))
26221 return type;
26222 /* If SCOPE isn't the template itself, it will not have a valid
26223 TYPE_FIELDS list. */
26224 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26225 /* scope is either the template itself or a compatible instantiation
26226 like X<T>, so look up the name in the original template. */
26227 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26228 /* If scope has no fields, it can't be a current instantiation. Check this
26229 before currently_open_class to avoid infinite recursion (71515). */
26230 if (!TYPE_FIELDS (scope))
26231 return type;
26232 /* If the SCOPE is not the current instantiation, there's no reason
26233 to look inside it. */
26234 if (only_current_p && !currently_open_class (scope))
26235 return type;
26236 /* Enter the SCOPE so that name lookup will be resolved as if we
26237 were in the class definition. In particular, SCOPE will no
26238 longer be considered a dependent type. */
26239 pushed_scope = push_scope (scope);
26240 /* Look up the declaration. */
26241 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26242 tf_warning_or_error);
26243
26244 result = NULL_TREE;
26245
26246 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26247 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26248 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26249 if (!decl)
26250 /*nop*/;
26251 else if (identifier_p (fullname)
26252 && TREE_CODE (decl) == TYPE_DECL)
26253 {
26254 result = TREE_TYPE (decl);
26255 if (result == error_mark_node)
26256 result = NULL_TREE;
26257 }
26258 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26259 && DECL_CLASS_TEMPLATE_P (decl))
26260 {
26261 /* Obtain the template and the arguments. */
26262 tree tmpl = TREE_OPERAND (fullname, 0);
26263 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26264 {
26265 /* We get here with a plain identifier because a previous tentative
26266 parse of the nested-name-specifier as part of a ptr-operator saw
26267 ::template X<A>. The use of ::template is necessary in a
26268 ptr-operator, but wrong in a declarator-id.
26269
26270 [temp.names]: In a qualified-id of a declarator-id, the keyword
26271 template shall not appear at the top level. */
26272 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26273 "keyword %<template%> not allowed in declarator-id");
26274 tmpl = decl;
26275 }
26276 tree args = TREE_OPERAND (fullname, 1);
26277 /* Instantiate the template. */
26278 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26279 /*entering_scope=*/true,
26280 tf_error | tf_user);
26281 if (result == error_mark_node)
26282 result = NULL_TREE;
26283 }
26284
26285 /* Leave the SCOPE. */
26286 if (pushed_scope)
26287 pop_scope (pushed_scope);
26288
26289 /* If we failed to resolve it, return the original typename. */
26290 if (!result)
26291 return type;
26292
26293 /* If lookup found a typename type, resolve that too. */
26294 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26295 {
26296 /* Ill-formed programs can cause infinite recursion here, so we
26297 must catch that. */
26298 TYPENAME_IS_RESOLVING_P (result) = 1;
26299 result = resolve_typename_type (result, only_current_p);
26300 TYPENAME_IS_RESOLVING_P (result) = 0;
26301 }
26302
26303 /* Qualify the resulting type. */
26304 quals = cp_type_quals (type);
26305 if (quals)
26306 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26307
26308 return result;
26309 }
26310
26311 /* EXPR is an expression which is not type-dependent. Return a proxy
26312 for EXPR that can be used to compute the types of larger
26313 expressions containing EXPR. */
26314
26315 tree
26316 build_non_dependent_expr (tree expr)
26317 {
26318 tree orig_expr = expr;
26319 tree inner_expr;
26320
26321 /* When checking, try to get a constant value for all non-dependent
26322 expressions in order to expose bugs in *_dependent_expression_p
26323 and constexpr. This can affect code generation, see PR70704, so
26324 only do this for -fchecking=2. */
26325 if (flag_checking > 1
26326 && cxx_dialect >= cxx11
26327 /* Don't do this during nsdmi parsing as it can lead to
26328 unexpected recursive instantiations. */
26329 && !parsing_nsdmi ()
26330 /* Don't do this during concept expansion either and for
26331 the same reason. */
26332 && !expanding_concept ())
26333 fold_non_dependent_expr (expr, tf_none);
26334
26335 STRIP_ANY_LOCATION_WRAPPER (expr);
26336
26337 /* Preserve OVERLOADs; the functions must be available to resolve
26338 types. */
26339 inner_expr = expr;
26340 if (TREE_CODE (inner_expr) == STMT_EXPR)
26341 inner_expr = stmt_expr_value_expr (inner_expr);
26342 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26343 inner_expr = TREE_OPERAND (inner_expr, 0);
26344 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26345 inner_expr = TREE_OPERAND (inner_expr, 1);
26346 if (is_overloaded_fn (inner_expr)
26347 || TREE_CODE (inner_expr) == OFFSET_REF)
26348 return orig_expr;
26349 /* There is no need to return a proxy for a variable or enumerator. */
26350 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26351 return orig_expr;
26352 /* Preserve string constants; conversions from string constants to
26353 "char *" are allowed, even though normally a "const char *"
26354 cannot be used to initialize a "char *". */
26355 if (TREE_CODE (expr) == STRING_CST)
26356 return orig_expr;
26357 /* Preserve void and arithmetic constants, as an optimization -- there is no
26358 reason to create a new node. */
26359 if (TREE_CODE (expr) == VOID_CST
26360 || TREE_CODE (expr) == INTEGER_CST
26361 || TREE_CODE (expr) == REAL_CST)
26362 return orig_expr;
26363 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26364 There is at least one place where we want to know that a
26365 particular expression is a throw-expression: when checking a ?:
26366 expression, there are special rules if the second or third
26367 argument is a throw-expression. */
26368 if (TREE_CODE (expr) == THROW_EXPR)
26369 return orig_expr;
26370
26371 /* Don't wrap an initializer list, we need to be able to look inside. */
26372 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26373 return orig_expr;
26374
26375 /* Don't wrap a dummy object, we need to be able to test for it. */
26376 if (is_dummy_object (expr))
26377 return orig_expr;
26378
26379 if (TREE_CODE (expr) == COND_EXPR)
26380 return build3 (COND_EXPR,
26381 TREE_TYPE (expr),
26382 TREE_OPERAND (expr, 0),
26383 (TREE_OPERAND (expr, 1)
26384 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26385 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26386 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26387 if (TREE_CODE (expr) == COMPOUND_EXPR
26388 && !COMPOUND_EXPR_OVERLOADED (expr))
26389 return build2 (COMPOUND_EXPR,
26390 TREE_TYPE (expr),
26391 TREE_OPERAND (expr, 0),
26392 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26393
26394 /* If the type is unknown, it can't really be non-dependent */
26395 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26396
26397 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26398 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26399 TREE_TYPE (expr), expr);
26400 }
26401
26402 /* ARGS is a vector of expressions as arguments to a function call.
26403 Replace the arguments with equivalent non-dependent expressions.
26404 This modifies ARGS in place. */
26405
26406 void
26407 make_args_non_dependent (vec<tree, va_gc> *args)
26408 {
26409 unsigned int ix;
26410 tree arg;
26411
26412 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26413 {
26414 tree newarg = build_non_dependent_expr (arg);
26415 if (newarg != arg)
26416 (*args)[ix] = newarg;
26417 }
26418 }
26419
26420 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26421 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26422 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26423
26424 static tree
26425 make_auto_1 (tree name, bool set_canonical)
26426 {
26427 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26428 TYPE_NAME (au) = build_decl (input_location,
26429 TYPE_DECL, name, au);
26430 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26431 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26432 (0, processing_template_decl + 1, processing_template_decl + 1,
26433 TYPE_NAME (au), NULL_TREE);
26434 if (set_canonical)
26435 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26436 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26437 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26438
26439 return au;
26440 }
26441
26442 tree
26443 make_decltype_auto (void)
26444 {
26445 return make_auto_1 (decltype_auto_identifier, true);
26446 }
26447
26448 tree
26449 make_auto (void)
26450 {
26451 return make_auto_1 (auto_identifier, true);
26452 }
26453
26454 /* Return a C++17 deduction placeholder for class template TMPL. */
26455
26456 tree
26457 make_template_placeholder (tree tmpl)
26458 {
26459 tree t = make_auto_1 (DECL_NAME (tmpl), true);
26460 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26461 return t;
26462 }
26463
26464 /* True iff T is a C++17 class template deduction placeholder. */
26465
26466 bool
26467 template_placeholder_p (tree t)
26468 {
26469 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26470 }
26471
26472 /* Make a "constrained auto" type-specifier. This is an
26473 auto type with constraints that must be associated after
26474 deduction. The constraint is formed from the given
26475 CONC and its optional sequence of arguments, which are
26476 non-null if written as partial-concept-id. */
26477
26478 tree
26479 make_constrained_auto (tree con, tree args)
26480 {
26481 tree type = make_auto_1 (auto_identifier, false);
26482
26483 /* Build the constraint. */
26484 tree tmpl = DECL_TI_TEMPLATE (con);
26485 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26486 expr = build_concept_check (expr, type, args);
26487
26488 tree constr = normalize_expression (expr);
26489 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26490
26491 /* Our canonical type depends on the constraint. */
26492 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26493
26494 /* Attach the constraint to the type declaration. */
26495 tree decl = TYPE_NAME (type);
26496 return decl;
26497 }
26498
26499 /* Given type ARG, return std::initializer_list<ARG>. */
26500
26501 static tree
26502 listify (tree arg)
26503 {
26504 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26505
26506 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26507 {
26508 gcc_rich_location richloc (input_location);
26509 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26510 error_at (&richloc,
26511 "deducing from brace-enclosed initializer list"
26512 " requires %<#include <initializer_list>%>");
26513
26514 return error_mark_node;
26515 }
26516 tree argvec = make_tree_vec (1);
26517 TREE_VEC_ELT (argvec, 0) = arg;
26518
26519 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26520 NULL_TREE, 0, tf_warning_or_error);
26521 }
26522
26523 /* Replace auto in TYPE with std::initializer_list<auto>. */
26524
26525 static tree
26526 listify_autos (tree type, tree auto_node)
26527 {
26528 tree init_auto = listify (auto_node);
26529 tree argvec = make_tree_vec (1);
26530 TREE_VEC_ELT (argvec, 0) = init_auto;
26531 if (processing_template_decl)
26532 argvec = add_to_template_args (current_template_args (), argvec);
26533 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26534 }
26535
26536 /* Hash traits for hashing possibly constrained 'auto'
26537 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26538
26539 struct auto_hash : default_hash_traits<tree>
26540 {
26541 static inline hashval_t hash (tree);
26542 static inline bool equal (tree, tree);
26543 };
26544
26545 /* Hash the 'auto' T. */
26546
26547 inline hashval_t
26548 auto_hash::hash (tree t)
26549 {
26550 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26551 /* Matching constrained-type-specifiers denote the same template
26552 parameter, so hash the constraint. */
26553 return hash_placeholder_constraint (c);
26554 else
26555 /* But unconstrained autos are all separate, so just hash the pointer. */
26556 return iterative_hash_object (t, 0);
26557 }
26558
26559 /* Compare two 'auto's. */
26560
26561 inline bool
26562 auto_hash::equal (tree t1, tree t2)
26563 {
26564 if (t1 == t2)
26565 return true;
26566
26567 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26568 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26569
26570 /* Two unconstrained autos are distinct. */
26571 if (!c1 || !c2)
26572 return false;
26573
26574 return equivalent_placeholder_constraints (c1, c2);
26575 }
26576
26577 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26578 constrained) auto, add it to the vector. */
26579
26580 static int
26581 extract_autos_r (tree t, void *data)
26582 {
26583 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26584 if (is_auto (t))
26585 {
26586 /* All the autos were built with index 0; fix that up now. */
26587 tree *p = hash.find_slot (t, INSERT);
26588 unsigned idx;
26589 if (*p)
26590 /* If this is a repeated constrained-type-specifier, use the index we
26591 chose before. */
26592 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26593 else
26594 {
26595 /* Otherwise this is new, so use the current count. */
26596 *p = t;
26597 idx = hash.elements () - 1;
26598 }
26599 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26600 }
26601
26602 /* Always keep walking. */
26603 return 0;
26604 }
26605
26606 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26607 says they can appear anywhere in the type. */
26608
26609 static tree
26610 extract_autos (tree type)
26611 {
26612 hash_set<tree> visited;
26613 hash_table<auto_hash> hash (2);
26614
26615 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26616
26617 tree tree_vec = make_tree_vec (hash.elements());
26618 for (hash_table<auto_hash>::iterator iter = hash.begin();
26619 iter != hash.end(); ++iter)
26620 {
26621 tree elt = *iter;
26622 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26623 TREE_VEC_ELT (tree_vec, i)
26624 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26625 }
26626
26627 return tree_vec;
26628 }
26629
26630 /* The stem for deduction guide names. */
26631 const char *const dguide_base = "__dguide_";
26632
26633 /* Return the name for a deduction guide for class template TMPL. */
26634
26635 tree
26636 dguide_name (tree tmpl)
26637 {
26638 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26639 tree tname = TYPE_IDENTIFIER (type);
26640 char *buf = (char *) alloca (1 + strlen (dguide_base)
26641 + IDENTIFIER_LENGTH (tname));
26642 memcpy (buf, dguide_base, strlen (dguide_base));
26643 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26644 IDENTIFIER_LENGTH (tname) + 1);
26645 tree dname = get_identifier (buf);
26646 TREE_TYPE (dname) = type;
26647 return dname;
26648 }
26649
26650 /* True if NAME is the name of a deduction guide. */
26651
26652 bool
26653 dguide_name_p (tree name)
26654 {
26655 return (TREE_CODE (name) == IDENTIFIER_NODE
26656 && TREE_TYPE (name)
26657 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26658 strlen (dguide_base)));
26659 }
26660
26661 /* True if FN is a deduction guide. */
26662
26663 bool
26664 deduction_guide_p (const_tree fn)
26665 {
26666 if (DECL_P (fn))
26667 if (tree name = DECL_NAME (fn))
26668 return dguide_name_p (name);
26669 return false;
26670 }
26671
26672 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26673
26674 bool
26675 copy_guide_p (const_tree fn)
26676 {
26677 gcc_assert (deduction_guide_p (fn));
26678 if (!DECL_ARTIFICIAL (fn))
26679 return false;
26680 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26681 return (TREE_CHAIN (parms) == void_list_node
26682 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26683 }
26684
26685 /* True if FN is a guide generated from a constructor template. */
26686
26687 bool
26688 template_guide_p (const_tree fn)
26689 {
26690 gcc_assert (deduction_guide_p (fn));
26691 if (!DECL_ARTIFICIAL (fn))
26692 return false;
26693 tree tmpl = DECL_TI_TEMPLATE (fn);
26694 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26695 return PRIMARY_TEMPLATE_P (org);
26696 return false;
26697 }
26698
26699 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26700 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26701 template parameter types. Note that the handling of template template
26702 parameters relies on current_template_parms being set appropriately for the
26703 new template. */
26704
26705 static tree
26706 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26707 tree tsubst_args, tsubst_flags_t complain)
26708 {
26709 if (olddecl == error_mark_node)
26710 return error_mark_node;
26711
26712 tree oldidx = get_template_parm_index (olddecl);
26713
26714 tree newtype;
26715 if (TREE_CODE (olddecl) == TYPE_DECL
26716 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26717 {
26718 tree oldtype = TREE_TYPE (olddecl);
26719 newtype = cxx_make_type (TREE_CODE (oldtype));
26720 TYPE_MAIN_VARIANT (newtype) = newtype;
26721 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
26722 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
26723 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
26724 }
26725 else
26726 {
26727 newtype = TREE_TYPE (olddecl);
26728 if (type_uses_auto (newtype))
26729 {
26730 // Substitute once to fix references to other template parameters.
26731 newtype = tsubst (newtype, tsubst_args,
26732 complain|tf_partial, NULL_TREE);
26733 // Now substitute again to reduce the level of the auto.
26734 newtype = tsubst (newtype, current_template_args (),
26735 complain, NULL_TREE);
26736 }
26737 else
26738 newtype = tsubst (newtype, tsubst_args,
26739 complain, NULL_TREE);
26740 }
26741
26742 tree newdecl
26743 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
26744 DECL_NAME (olddecl), newtype);
26745 SET_DECL_TEMPLATE_PARM_P (newdecl);
26746
26747 tree newidx;
26748 if (TREE_CODE (olddecl) == TYPE_DECL
26749 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26750 {
26751 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
26752 = build_template_parm_index (index, level, level,
26753 newdecl, newtype);
26754 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26755 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26756 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
26757 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
26758
26759 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
26760 {
26761 DECL_TEMPLATE_RESULT (newdecl)
26762 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
26763 DECL_NAME (olddecl), newtype);
26764 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
26765 // First create a copy (ttargs) of tsubst_args with an
26766 // additional level for the template template parameter's own
26767 // template parameters (ttparms).
26768 tree ttparms = (INNERMOST_TEMPLATE_PARMS
26769 (DECL_TEMPLATE_PARMS (olddecl)));
26770 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
26771 tree ttargs = make_tree_vec (depth + 1);
26772 for (int i = 0; i < depth; ++i)
26773 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
26774 TREE_VEC_ELT (ttargs, depth)
26775 = template_parms_level_to_args (ttparms);
26776 // Substitute ttargs into ttparms to fix references to
26777 // other template parameters.
26778 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26779 complain|tf_partial);
26780 // Now substitute again with args based on tparms, to reduce
26781 // the level of the ttparms.
26782 ttargs = current_template_args ();
26783 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26784 complain);
26785 // Finally, tack the adjusted parms onto tparms.
26786 ttparms = tree_cons (size_int (depth), ttparms,
26787 current_template_parms);
26788 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
26789 }
26790 }
26791 else
26792 {
26793 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
26794 tree newconst
26795 = build_decl (DECL_SOURCE_LOCATION (oldconst),
26796 TREE_CODE (oldconst),
26797 DECL_NAME (oldconst), newtype);
26798 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
26799 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
26800 SET_DECL_TEMPLATE_PARM_P (newconst);
26801 newidx = build_template_parm_index (index, level, level,
26802 newconst, newtype);
26803 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26804 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26805 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
26806 }
26807
26808 return newdecl;
26809 }
26810
26811 /* Returns a C++17 class deduction guide template based on the constructor
26812 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26813 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26814
26815 static tree
26816 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
26817 {
26818 tree type, tparms, targs, fparms, fargs, ci;
26819 bool memtmpl = false;
26820 bool explicit_p;
26821 location_t loc;
26822 tree fn_tmpl = NULL_TREE;
26823
26824 if (TYPE_P (ctor))
26825 {
26826 type = ctor;
26827 bool copy_p = TYPE_REF_P (type);
26828 if (copy_p)
26829 {
26830 type = TREE_TYPE (type);
26831 fparms = tree_cons (NULL_TREE, type, void_list_node);
26832 }
26833 else
26834 fparms = void_list_node;
26835
26836 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
26837 tparms = DECL_TEMPLATE_PARMS (ctmpl);
26838 targs = CLASSTYPE_TI_ARGS (type);
26839 ci = NULL_TREE;
26840 fargs = NULL_TREE;
26841 loc = DECL_SOURCE_LOCATION (ctmpl);
26842 explicit_p = false;
26843 }
26844 else
26845 {
26846 ++processing_template_decl;
26847 bool ok = true;
26848
26849 fn_tmpl
26850 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
26851 : DECL_TI_TEMPLATE (ctor));
26852 if (outer_args)
26853 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
26854 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
26855
26856 type = DECL_CONTEXT (ctor);
26857
26858 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
26859 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26860 fully specialized args for the enclosing class. Strip those off, as
26861 the deduction guide won't have those template parameters. */
26862 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
26863 TMPL_PARMS_DEPTH (tparms));
26864 /* Discard the 'this' parameter. */
26865 fparms = FUNCTION_ARG_CHAIN (ctor);
26866 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
26867 ci = get_constraints (ctor);
26868 loc = DECL_SOURCE_LOCATION (ctor);
26869 explicit_p = DECL_NONCONVERTING_P (ctor);
26870
26871 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26872 {
26873 memtmpl = true;
26874
26875 /* For a member template constructor, we need to flatten the two
26876 template parameter lists into one, and then adjust the function
26877 signature accordingly. This gets...complicated. */
26878 tree save_parms = current_template_parms;
26879
26880 /* For a member template we should have two levels of parms/args, one
26881 for the class and one for the constructor. We stripped
26882 specialized args for further enclosing classes above. */
26883 const int depth = 2;
26884 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26885
26886 /* Template args for translating references to the two-level template
26887 parameters into references to the one-level template parameters we
26888 are creating. */
26889 tree tsubst_args = copy_node (targs);
26890 TMPL_ARGS_LEVEL (tsubst_args, depth)
26891 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
26892
26893 /* Template parms for the constructor template. */
26894 tree ftparms = TREE_VALUE (tparms);
26895 unsigned flen = TREE_VEC_LENGTH (ftparms);
26896 /* Template parms for the class template. */
26897 tparms = TREE_CHAIN (tparms);
26898 tree ctparms = TREE_VALUE (tparms);
26899 unsigned clen = TREE_VEC_LENGTH (ctparms);
26900 /* Template parms for the deduction guide start as a copy of the
26901 template parms for the class. We set current_template_parms for
26902 lookup_template_class_1. */
26903 current_template_parms = tparms = copy_node (tparms);
26904 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
26905 for (unsigned i = 0; i < clen; ++i)
26906 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
26907
26908 /* Now we need to rewrite the constructor parms to append them to the
26909 class parms. */
26910 for (unsigned i = 0; i < flen; ++i)
26911 {
26912 unsigned index = i + clen;
26913 unsigned level = 1;
26914 tree oldelt = TREE_VEC_ELT (ftparms, i);
26915 tree olddecl = TREE_VALUE (oldelt);
26916 tree newdecl = rewrite_template_parm (olddecl, index, level,
26917 tsubst_args, complain);
26918 if (newdecl == error_mark_node)
26919 ok = false;
26920 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
26921 tsubst_args, complain, ctor);
26922 tree list = build_tree_list (newdef, newdecl);
26923 TEMPLATE_PARM_CONSTRAINTS (list)
26924 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
26925 tsubst_args, complain, ctor);
26926 TREE_VEC_ELT (new_vec, index) = list;
26927 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26928 }
26929
26930 /* Now we have a final set of template parms to substitute into the
26931 function signature. */
26932 targs = template_parms_to_args (tparms);
26933 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26934 complain, ctor);
26935 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26936 if (ci)
26937 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26938
26939 current_template_parms = save_parms;
26940 }
26941
26942 --processing_template_decl;
26943 if (!ok)
26944 return error_mark_node;
26945 }
26946
26947 if (!memtmpl)
26948 {
26949 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26950 tparms = copy_node (tparms);
26951 INNERMOST_TEMPLATE_PARMS (tparms)
26952 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26953 }
26954
26955 tree fntype = build_function_type (type, fparms);
26956 tree ded_fn = build_lang_decl_loc (loc,
26957 FUNCTION_DECL,
26958 dguide_name (type), fntype);
26959 DECL_ARGUMENTS (ded_fn) = fargs;
26960 DECL_ARTIFICIAL (ded_fn) = true;
26961 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26962 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26963 DECL_ARTIFICIAL (ded_tmpl) = true;
26964 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26965 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26966 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26967 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26968 if (DECL_P (ctor))
26969 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26970 if (ci)
26971 set_constraints (ded_tmpl, ci);
26972
26973 return ded_tmpl;
26974 }
26975
26976 /* Deduce template arguments for the class template placeholder PTYPE for
26977 template TMPL based on the initializer INIT, and return the resulting
26978 type. */
26979
26980 static tree
26981 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26982 tsubst_flags_t complain)
26983 {
26984 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26985 {
26986 /* We should have handled this in the caller. */
26987 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26988 return ptype;
26989 if (complain & tf_error)
26990 error ("non-class template %qT used without template arguments", tmpl);
26991 return error_mark_node;
26992 }
26993
26994 tree type = TREE_TYPE (tmpl);
26995
26996 bool try_list_ctor = false;
26997
26998 vec<tree,va_gc> *args;
26999 if (init == NULL_TREE
27000 || TREE_CODE (init) == TREE_LIST)
27001 args = make_tree_vector_from_list (init);
27002 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27003 {
27004 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27005 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27006 {
27007 /* As an exception, the first phase in 16.3.1.7 (considering the
27008 initializer list as a single argument) is omitted if the
27009 initializer list consists of a single expression of type cv U,
27010 where U is a specialization of C or a class derived from a
27011 specialization of C. */
27012 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27013 tree etype = TREE_TYPE (elt);
27014
27015 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
27016 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27017 int err = unify (tparms, targs, type, etype,
27018 UNIFY_ALLOW_DERIVED, /*explain*/false);
27019 if (err == 0)
27020 try_list_ctor = false;
27021 ggc_free (targs);
27022 }
27023 if (try_list_ctor || is_std_init_list (type))
27024 args = make_tree_vector_single (init);
27025 else
27026 args = make_tree_vector_from_ctor (init);
27027 }
27028 else
27029 args = make_tree_vector_single (init);
27030
27031 tree dname = dguide_name (tmpl);
27032 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27033 /*type*/false, /*complain*/false,
27034 /*hidden*/false);
27035 bool elided = false;
27036 if (cands == error_mark_node)
27037 cands = NULL_TREE;
27038
27039 /* Prune explicit deduction guides in copy-initialization context. */
27040 if (flags & LOOKUP_ONLYCONVERTING)
27041 {
27042 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27043 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27044 elided = true;
27045
27046 if (elided)
27047 {
27048 /* Found a nonconverting guide, prune the candidates. */
27049 tree pruned = NULL_TREE;
27050 for (lkp_iterator iter (cands); iter; ++iter)
27051 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27052 pruned = lookup_add (*iter, pruned);
27053
27054 cands = pruned;
27055 }
27056 }
27057
27058 tree outer_args = NULL_TREE;
27059 if (DECL_CLASS_SCOPE_P (tmpl)
27060 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
27061 {
27062 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27063 type = TREE_TYPE (most_general_template (tmpl));
27064 }
27065
27066 bool saw_ctor = false;
27067 // FIXME cache artificial deduction guides
27068 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27069 {
27070 /* Skip inherited constructors. */
27071 if (iter.using_p ())
27072 continue;
27073
27074 tree guide = build_deduction_guide (*iter, outer_args, complain);
27075 if (guide == error_mark_node)
27076 return error_mark_node;
27077 if ((flags & LOOKUP_ONLYCONVERTING)
27078 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27079 elided = true;
27080 else
27081 cands = lookup_add (guide, cands);
27082
27083 saw_ctor = true;
27084 }
27085
27086 tree call = error_mark_node;
27087
27088 /* If this is list-initialization and the class has a list constructor, first
27089 try deducing from the list as a single argument, as [over.match.list]. */
27090 tree list_cands = NULL_TREE;
27091 if (try_list_ctor && cands)
27092 for (lkp_iterator iter (cands); iter; ++iter)
27093 {
27094 tree dg = *iter;
27095 if (is_list_ctor (dg))
27096 list_cands = lookup_add (dg, list_cands);
27097 }
27098 if (list_cands)
27099 {
27100 ++cp_unevaluated_operand;
27101 call = build_new_function_call (list_cands, &args, tf_decltype);
27102 --cp_unevaluated_operand;
27103
27104 if (call == error_mark_node)
27105 {
27106 /* That didn't work, now try treating the list as a sequence of
27107 arguments. */
27108 release_tree_vector (args);
27109 args = make_tree_vector_from_ctor (init);
27110 }
27111 }
27112
27113 /* Maybe generate an implicit deduction guide. */
27114 if (call == error_mark_node && args->length () < 2)
27115 {
27116 tree gtype = NULL_TREE;
27117
27118 if (args->length () == 1)
27119 /* Generate a copy guide. */
27120 gtype = build_reference_type (type);
27121 else if (!saw_ctor)
27122 /* Generate a default guide. */
27123 gtype = type;
27124
27125 if (gtype)
27126 {
27127 tree guide = build_deduction_guide (gtype, outer_args, complain);
27128 if (guide == error_mark_node)
27129 return error_mark_node;
27130 cands = lookup_add (guide, cands);
27131 }
27132 }
27133
27134 if (elided && !cands)
27135 {
27136 error ("cannot deduce template arguments for copy-initialization"
27137 " of %qT, as it has no non-explicit deduction guides or "
27138 "user-declared constructors", type);
27139 return error_mark_node;
27140 }
27141 else if (!cands && call == error_mark_node)
27142 {
27143 error ("cannot deduce template arguments of %qT, as it has no viable "
27144 "deduction guides", type);
27145 return error_mark_node;
27146 }
27147
27148 if (call == error_mark_node)
27149 {
27150 ++cp_unevaluated_operand;
27151 call = build_new_function_call (cands, &args, tf_decltype);
27152 --cp_unevaluated_operand;
27153 }
27154
27155 if (call == error_mark_node && (complain & tf_warning_or_error))
27156 {
27157 error ("class template argument deduction failed:");
27158
27159 ++cp_unevaluated_operand;
27160 call = build_new_function_call (cands, &args, complain | tf_decltype);
27161 --cp_unevaluated_operand;
27162
27163 if (elided)
27164 inform (input_location, "explicit deduction guides not considered "
27165 "for copy-initialization");
27166 }
27167
27168 release_tree_vector (args);
27169
27170 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27171 }
27172
27173 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27174 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27175 The CONTEXT determines the context in which auto deduction is performed
27176 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27177 OUTER_TARGS are used during template argument deduction
27178 (context == adc_unify) to properly substitute the result, and is ignored
27179 in other contexts.
27180
27181 For partial-concept-ids, extra args may be appended to the list of deduced
27182 template arguments prior to determining constraint satisfaction. */
27183
27184 tree
27185 do_auto_deduction (tree type, tree init, tree auto_node,
27186 tsubst_flags_t complain, auto_deduction_context context,
27187 tree outer_targs, int flags)
27188 {
27189 tree targs;
27190
27191 if (init == error_mark_node)
27192 return error_mark_node;
27193
27194 if (init && type_dependent_expression_p (init)
27195 && context != adc_unify)
27196 /* Defining a subset of type-dependent expressions that we can deduce
27197 from ahead of time isn't worth the trouble. */
27198 return type;
27199
27200 /* Similarly, we can't deduce from another undeduced decl. */
27201 if (init && undeduced_auto_decl (init))
27202 return type;
27203
27204 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27205 /* C++17 class template argument deduction. */
27206 return do_class_deduction (type, tmpl, init, flags, complain);
27207
27208 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27209 /* Nothing we can do with this, even in deduction context. */
27210 return type;
27211
27212 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27213 with either a new invented type template parameter U or, if the
27214 initializer is a braced-init-list (8.5.4), with
27215 std::initializer_list<U>. */
27216 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27217 {
27218 if (!DIRECT_LIST_INIT_P (init))
27219 type = listify_autos (type, auto_node);
27220 else if (CONSTRUCTOR_NELTS (init) == 1)
27221 init = CONSTRUCTOR_ELT (init, 0)->value;
27222 else
27223 {
27224 if (complain & tf_warning_or_error)
27225 {
27226 if (permerror (input_location, "direct-list-initialization of "
27227 "%<auto%> requires exactly one element"))
27228 inform (input_location,
27229 "for deduction to %<std::initializer_list%>, use copy-"
27230 "list-initialization (i.e. add %<=%> before the %<{%>)");
27231 }
27232 type = listify_autos (type, auto_node);
27233 }
27234 }
27235
27236 if (type == error_mark_node)
27237 return error_mark_node;
27238
27239 init = resolve_nondeduced_context (init, complain);
27240
27241 if (context == adc_decomp_type
27242 && auto_node == type
27243 && init != error_mark_node
27244 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27245 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27246 and initializer has array type, deduce cv-qualified array type. */
27247 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27248 complain);
27249 else if (AUTO_IS_DECLTYPE (auto_node))
27250 {
27251 tree stripped_init = tree_strip_any_location_wrapper (init);
27252 bool id = (DECL_P (stripped_init)
27253 || ((TREE_CODE (init) == COMPONENT_REF
27254 || TREE_CODE (init) == SCOPE_REF)
27255 && !REF_PARENTHESIZED_P (init)));
27256 targs = make_tree_vec (1);
27257 TREE_VEC_ELT (targs, 0)
27258 = finish_decltype_type (init, id, tf_warning_or_error);
27259 if (type != auto_node)
27260 {
27261 if (complain & tf_error)
27262 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27263 return error_mark_node;
27264 }
27265 }
27266 else
27267 {
27268 tree parms = build_tree_list (NULL_TREE, type);
27269 tree tparms;
27270
27271 if (flag_concepts)
27272 tparms = extract_autos (type);
27273 else
27274 {
27275 tparms = make_tree_vec (1);
27276 TREE_VEC_ELT (tparms, 0)
27277 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27278 }
27279
27280 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27281 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27282 DEDUCE_CALL,
27283 NULL, /*explain_p=*/false);
27284 if (val > 0)
27285 {
27286 if (processing_template_decl)
27287 /* Try again at instantiation time. */
27288 return type;
27289 if (type && type != error_mark_node
27290 && (complain & tf_error))
27291 /* If type is error_mark_node a diagnostic must have been
27292 emitted by now. Also, having a mention to '<type error>'
27293 in the diagnostic is not really useful to the user. */
27294 {
27295 if (cfun && auto_node == current_function_auto_return_pattern
27296 && LAMBDA_FUNCTION_P (current_function_decl))
27297 error ("unable to deduce lambda return type from %qE", init);
27298 else
27299 error ("unable to deduce %qT from %qE", type, init);
27300 type_unification_real (tparms, targs, parms, &init, 1, 0,
27301 DEDUCE_CALL,
27302 NULL, /*explain_p=*/true);
27303 }
27304 return error_mark_node;
27305 }
27306 }
27307
27308 /* Check any placeholder constraints against the deduced type. */
27309 if (flag_concepts && !processing_template_decl)
27310 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27311 {
27312 /* Use the deduced type to check the associated constraints. If we
27313 have a partial-concept-id, rebuild the argument list so that
27314 we check using the extra arguments. */
27315 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27316 tree cargs = CHECK_CONSTR_ARGS (constr);
27317 if (TREE_VEC_LENGTH (cargs) > 1)
27318 {
27319 cargs = copy_node (cargs);
27320 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27321 }
27322 else
27323 cargs = targs;
27324 if (!constraints_satisfied_p (constr, cargs))
27325 {
27326 if (complain & tf_warning_or_error)
27327 {
27328 auto_diagnostic_group d;
27329 switch (context)
27330 {
27331 case adc_unspecified:
27332 case adc_unify:
27333 error("placeholder constraints not satisfied");
27334 break;
27335 case adc_variable_type:
27336 case adc_decomp_type:
27337 error ("deduced initializer does not satisfy "
27338 "placeholder constraints");
27339 break;
27340 case adc_return_type:
27341 error ("deduced return type does not satisfy "
27342 "placeholder constraints");
27343 break;
27344 case adc_requirement:
27345 error ("deduced expression type does not satisfy "
27346 "placeholder constraints");
27347 break;
27348 }
27349 diagnose_constraints (input_location, constr, targs);
27350 }
27351 return error_mark_node;
27352 }
27353 }
27354
27355 if (processing_template_decl && context != adc_unify)
27356 outer_targs = current_template_args ();
27357 targs = add_to_template_args (outer_targs, targs);
27358 return tsubst (type, targs, complain, NULL_TREE);
27359 }
27360
27361 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27362 result. */
27363
27364 tree
27365 splice_late_return_type (tree type, tree late_return_type)
27366 {
27367 if (is_auto (type))
27368 {
27369 if (late_return_type)
27370 return late_return_type;
27371
27372 tree idx = get_template_parm_index (type);
27373 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27374 /* In an abbreviated function template we didn't know we were dealing
27375 with a function template when we saw the auto return type, so update
27376 it to have the correct level. */
27377 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27378 }
27379 return type;
27380 }
27381
27382 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27383 'decltype(auto)' or a deduced class template. */
27384
27385 bool
27386 is_auto (const_tree type)
27387 {
27388 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27389 && (TYPE_IDENTIFIER (type) == auto_identifier
27390 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
27391 || CLASS_PLACEHOLDER_TEMPLATE (type)))
27392 return true;
27393 else
27394 return false;
27395 }
27396
27397 /* for_each_template_parm callback for type_uses_auto. */
27398
27399 int
27400 is_auto_r (tree tp, void */*data*/)
27401 {
27402 return is_auto (tp);
27403 }
27404
27405 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27406 a use of `auto'. Returns NULL_TREE otherwise. */
27407
27408 tree
27409 type_uses_auto (tree type)
27410 {
27411 if (type == NULL_TREE)
27412 return NULL_TREE;
27413 else if (flag_concepts)
27414 {
27415 /* The Concepts TS allows multiple autos in one type-specifier; just
27416 return the first one we find, do_auto_deduction will collect all of
27417 them. */
27418 if (uses_template_parms (type))
27419 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27420 /*visited*/NULL, /*nondeduced*/false);
27421 else
27422 return NULL_TREE;
27423 }
27424 else
27425 return find_type_usage (type, is_auto);
27426 }
27427
27428 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27429 concepts are enabled, auto is acceptable in template arguments, but
27430 only when TEMPL identifies a template class. Return TRUE if any
27431 such errors were reported. */
27432
27433 bool
27434 check_auto_in_tmpl_args (tree tmpl, tree args)
27435 {
27436 /* If there were previous errors, nevermind. */
27437 if (!args || TREE_CODE (args) != TREE_VEC)
27438 return false;
27439
27440 /* If TMPL is an identifier, we're parsing and we can't tell yet
27441 whether TMPL is supposed to be a type, a function or a variable.
27442 We'll only be able to tell during template substitution, so we
27443 expect to be called again then. If concepts are enabled and we
27444 know we have a type, we're ok. */
27445 if (flag_concepts
27446 && (identifier_p (tmpl)
27447 || (DECL_P (tmpl)
27448 && (DECL_TYPE_TEMPLATE_P (tmpl)
27449 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27450 return false;
27451
27452 /* Quickly search for any occurrences of auto; usually there won't
27453 be any, and then we'll avoid allocating the vector. */
27454 if (!type_uses_auto (args))
27455 return false;
27456
27457 bool errors = false;
27458
27459 tree vec = extract_autos (args);
27460 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27461 {
27462 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27463 error_at (DECL_SOURCE_LOCATION (xauto),
27464 "invalid use of %qT in template argument", xauto);
27465 errors = true;
27466 }
27467
27468 return errors;
27469 }
27470
27471 /* For a given template T, return the vector of typedefs referenced
27472 in T for which access check is needed at T instantiation time.
27473 T is either a FUNCTION_DECL or a RECORD_TYPE.
27474 Those typedefs were added to T by the function
27475 append_type_to_template_for_access_check. */
27476
27477 vec<qualified_typedef_usage_t, va_gc> *
27478 get_types_needing_access_check (tree t)
27479 {
27480 tree ti;
27481 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27482
27483 if (!t || t == error_mark_node)
27484 return NULL;
27485
27486 if (!(ti = get_template_info (t)))
27487 return NULL;
27488
27489 if (CLASS_TYPE_P (t)
27490 || TREE_CODE (t) == FUNCTION_DECL)
27491 {
27492 if (!TI_TEMPLATE (ti))
27493 return NULL;
27494
27495 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27496 }
27497
27498 return result;
27499 }
27500
27501 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27502 tied to T. That list of typedefs will be access checked at
27503 T instantiation time.
27504 T is either a FUNCTION_DECL or a RECORD_TYPE.
27505 TYPE_DECL is a TYPE_DECL node representing a typedef.
27506 SCOPE is the scope through which TYPE_DECL is accessed.
27507 LOCATION is the location of the usage point of TYPE_DECL.
27508
27509 This function is a subroutine of
27510 append_type_to_template_for_access_check. */
27511
27512 static void
27513 append_type_to_template_for_access_check_1 (tree t,
27514 tree type_decl,
27515 tree scope,
27516 location_t location)
27517 {
27518 qualified_typedef_usage_t typedef_usage;
27519 tree ti;
27520
27521 if (!t || t == error_mark_node)
27522 return;
27523
27524 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27525 || CLASS_TYPE_P (t))
27526 && type_decl
27527 && TREE_CODE (type_decl) == TYPE_DECL
27528 && scope);
27529
27530 if (!(ti = get_template_info (t)))
27531 return;
27532
27533 gcc_assert (TI_TEMPLATE (ti));
27534
27535 typedef_usage.typedef_decl = type_decl;
27536 typedef_usage.context = scope;
27537 typedef_usage.locus = location;
27538
27539 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27540 }
27541
27542 /* Append TYPE_DECL to the template TEMPL.
27543 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27544 At TEMPL instanciation time, TYPE_DECL will be checked to see
27545 if it can be accessed through SCOPE.
27546 LOCATION is the location of the usage point of TYPE_DECL.
27547
27548 e.g. consider the following code snippet:
27549
27550 class C
27551 {
27552 typedef int myint;
27553 };
27554
27555 template<class U> struct S
27556 {
27557 C::myint mi; // <-- usage point of the typedef C::myint
27558 };
27559
27560 S<char> s;
27561
27562 At S<char> instantiation time, we need to check the access of C::myint
27563 In other words, we need to check the access of the myint typedef through
27564 the C scope. For that purpose, this function will add the myint typedef
27565 and the scope C through which its being accessed to a list of typedefs
27566 tied to the template S. That list will be walked at template instantiation
27567 time and access check performed on each typedefs it contains.
27568 Note that this particular code snippet should yield an error because
27569 myint is private to C. */
27570
27571 void
27572 append_type_to_template_for_access_check (tree templ,
27573 tree type_decl,
27574 tree scope,
27575 location_t location)
27576 {
27577 qualified_typedef_usage_t *iter;
27578 unsigned i;
27579
27580 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27581
27582 /* Make sure we don't append the type to the template twice. */
27583 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27584 if (iter->typedef_decl == type_decl && scope == iter->context)
27585 return;
27586
27587 append_type_to_template_for_access_check_1 (templ, type_decl,
27588 scope, location);
27589 }
27590
27591 /* Convert the generic type parameters in PARM that match the types given in the
27592 range [START_IDX, END_IDX) from the current_template_parms into generic type
27593 packs. */
27594
27595 tree
27596 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27597 {
27598 tree current = current_template_parms;
27599 int depth = TMPL_PARMS_DEPTH (current);
27600 current = INNERMOST_TEMPLATE_PARMS (current);
27601 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27602
27603 for (int i = 0; i < start_idx; ++i)
27604 TREE_VEC_ELT (replacement, i)
27605 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27606
27607 for (int i = start_idx; i < end_idx; ++i)
27608 {
27609 /* Create a distinct parameter pack type from the current parm and add it
27610 to the replacement args to tsubst below into the generic function
27611 parameter. */
27612
27613 tree o = TREE_TYPE (TREE_VALUE
27614 (TREE_VEC_ELT (current, i)));
27615 tree t = copy_type (o);
27616 TEMPLATE_TYPE_PARM_INDEX (t)
27617 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27618 o, 0, 0, tf_none);
27619 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27620 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27621 TYPE_MAIN_VARIANT (t) = t;
27622 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27623 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27624 TREE_VEC_ELT (replacement, i) = t;
27625 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27626 }
27627
27628 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27629 TREE_VEC_ELT (replacement, i)
27630 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27631
27632 /* If there are more levels then build up the replacement with the outer
27633 template parms. */
27634 if (depth > 1)
27635 replacement = add_to_template_args (template_parms_to_args
27636 (TREE_CHAIN (current_template_parms)),
27637 replacement);
27638
27639 return tsubst (parm, replacement, tf_none, NULL_TREE);
27640 }
27641
27642 /* Entries in the decl_constraint hash table. */
27643 struct GTY((for_user)) constr_entry
27644 {
27645 tree decl;
27646 tree ci;
27647 };
27648
27649 /* Hashing function and equality for constraint entries. */
27650 struct constr_hasher : ggc_ptr_hash<constr_entry>
27651 {
27652 static hashval_t hash (constr_entry *e)
27653 {
27654 return (hashval_t)DECL_UID (e->decl);
27655 }
27656
27657 static bool equal (constr_entry *e1, constr_entry *e2)
27658 {
27659 return e1->decl == e2->decl;
27660 }
27661 };
27662
27663 /* A mapping from declarations to constraint information. Note that
27664 both templates and their underlying declarations are mapped to the
27665 same constraint information.
27666
27667 FIXME: This is defined in pt.c because garbage collection
27668 code is not being generated for constraint.cc. */
27669
27670 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27671
27672 /* Returns the template constraints of declaration T. If T is not
27673 constrained, return NULL_TREE. Note that T must be non-null. */
27674
27675 tree
27676 get_constraints (tree t)
27677 {
27678 if (!flag_concepts)
27679 return NULL_TREE;
27680
27681 gcc_assert (DECL_P (t));
27682 if (TREE_CODE (t) == TEMPLATE_DECL)
27683 t = DECL_TEMPLATE_RESULT (t);
27684 constr_entry elt = { t, NULL_TREE };
27685 constr_entry* found = decl_constraints->find (&elt);
27686 if (found)
27687 return found->ci;
27688 else
27689 return NULL_TREE;
27690 }
27691
27692 /* Associate the given constraint information CI with the declaration
27693 T. If T is a template, then the constraints are associated with
27694 its underlying declaration. Don't build associations if CI is
27695 NULL_TREE. */
27696
27697 void
27698 set_constraints (tree t, tree ci)
27699 {
27700 if (!ci)
27701 return;
27702 gcc_assert (t && flag_concepts);
27703 if (TREE_CODE (t) == TEMPLATE_DECL)
27704 t = DECL_TEMPLATE_RESULT (t);
27705 gcc_assert (!get_constraints (t));
27706 constr_entry elt = {t, ci};
27707 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
27708 constr_entry* entry = ggc_alloc<constr_entry> ();
27709 *entry = elt;
27710 *slot = entry;
27711 }
27712
27713 /* Remove the associated constraints of the declaration T. */
27714
27715 void
27716 remove_constraints (tree t)
27717 {
27718 gcc_assert (DECL_P (t));
27719 if (TREE_CODE (t) == TEMPLATE_DECL)
27720 t = DECL_TEMPLATE_RESULT (t);
27721
27722 constr_entry elt = {t, NULL_TREE};
27723 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
27724 if (slot)
27725 decl_constraints->clear_slot (slot);
27726 }
27727
27728 /* Memoized satisfaction results for declarations. This
27729 maps the pair (constraint_info, arguments) to the result computed
27730 by constraints_satisfied_p. */
27731
27732 struct GTY((for_user)) constraint_sat_entry
27733 {
27734 tree ci;
27735 tree args;
27736 tree result;
27737 };
27738
27739 /* Hashing function and equality for constraint entries. */
27740
27741 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
27742 {
27743 static hashval_t hash (constraint_sat_entry *e)
27744 {
27745 hashval_t val = iterative_hash_object(e->ci, 0);
27746 return iterative_hash_template_arg (e->args, val);
27747 }
27748
27749 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
27750 {
27751 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
27752 }
27753 };
27754
27755 /* Memoized satisfaction results for concept checks. */
27756
27757 struct GTY((for_user)) concept_spec_entry
27758 {
27759 tree tmpl;
27760 tree args;
27761 tree result;
27762 };
27763
27764 /* Hashing function and equality for constraint entries. */
27765
27766 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
27767 {
27768 static hashval_t hash (concept_spec_entry *e)
27769 {
27770 return hash_tmpl_and_args (e->tmpl, e->args);
27771 }
27772
27773 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
27774 {
27775 ++comparing_specializations;
27776 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
27777 --comparing_specializations;
27778 return eq;
27779 }
27780 };
27781
27782 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
27783 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
27784
27785 /* Search for a memoized satisfaction result. Returns one of the
27786 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27787
27788 tree
27789 lookup_constraint_satisfaction (tree ci, tree args)
27790 {
27791 constraint_sat_entry elt = { ci, args, NULL_TREE };
27792 constraint_sat_entry* found = constraint_memos->find (&elt);
27793 if (found)
27794 return found->result;
27795 else
27796 return NULL_TREE;
27797 }
27798
27799 /* Memoize the result of a satisfication test. Returns the saved result. */
27800
27801 tree
27802 memoize_constraint_satisfaction (tree ci, tree args, tree result)
27803 {
27804 constraint_sat_entry elt = {ci, args, result};
27805 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
27806 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
27807 *entry = elt;
27808 *slot = entry;
27809 return result;
27810 }
27811
27812 /* Search for a memoized satisfaction result for a concept. */
27813
27814 tree
27815 lookup_concept_satisfaction (tree tmpl, tree args)
27816 {
27817 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27818 concept_spec_entry* found = concept_memos->find (&elt);
27819 if (found)
27820 return found->result;
27821 else
27822 return NULL_TREE;
27823 }
27824
27825 /* Memoize the result of a concept check. Returns the saved result. */
27826
27827 tree
27828 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
27829 {
27830 concept_spec_entry elt = {tmpl, args, result};
27831 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
27832 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27833 *entry = elt;
27834 *slot = entry;
27835 return result;
27836 }
27837
27838 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
27839
27840 /* Returns a prior concept specialization. This returns the substituted
27841 and normalized constraints defined by the concept. */
27842
27843 tree
27844 get_concept_expansion (tree tmpl, tree args)
27845 {
27846 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27847 concept_spec_entry* found = concept_expansions->find (&elt);
27848 if (found)
27849 return found->result;
27850 else
27851 return NULL_TREE;
27852 }
27853
27854 /* Save a concept expansion for later. */
27855
27856 tree
27857 save_concept_expansion (tree tmpl, tree args, tree def)
27858 {
27859 concept_spec_entry elt = {tmpl, args, def};
27860 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
27861 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27862 *entry = elt;
27863 *slot = entry;
27864 return def;
27865 }
27866
27867 static hashval_t
27868 hash_subsumption_args (tree t1, tree t2)
27869 {
27870 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
27871 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
27872 int val = 0;
27873 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
27874 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
27875 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
27876 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
27877 return val;
27878 }
27879
27880 /* Compare the constraints of two subsumption entries. The LEFT1 and
27881 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27882 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27883
27884 static bool
27885 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
27886 {
27887 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
27888 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
27889 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
27890 CHECK_CONSTR_ARGS (right1)))
27891 return comp_template_args (CHECK_CONSTR_ARGS (left2),
27892 CHECK_CONSTR_ARGS (right2));
27893 return false;
27894 }
27895
27896 /* Key/value pair for learning and memoizing subsumption results. This
27897 associates a pair of check constraints (including arguments) with
27898 a boolean value indicating the result. */
27899
27900 struct GTY((for_user)) subsumption_entry
27901 {
27902 tree t1;
27903 tree t2;
27904 bool result;
27905 };
27906
27907 /* Hashing function and equality for constraint entries. */
27908
27909 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
27910 {
27911 static hashval_t hash (subsumption_entry *e)
27912 {
27913 return hash_subsumption_args (e->t1, e->t2);
27914 }
27915
27916 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
27917 {
27918 ++comparing_specializations;
27919 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
27920 --comparing_specializations;
27921 return eq;
27922 }
27923 };
27924
27925 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
27926
27927 /* Search for a previously cached subsumption result. */
27928
27929 bool*
27930 lookup_subsumption_result (tree t1, tree t2)
27931 {
27932 subsumption_entry elt = { t1, t2, false };
27933 subsumption_entry* found = subsumption_table->find (&elt);
27934 if (found)
27935 return &found->result;
27936 else
27937 return 0;
27938 }
27939
27940 /* Save a subsumption result. */
27941
27942 bool
27943 save_subsumption_result (tree t1, tree t2, bool result)
27944 {
27945 subsumption_entry elt = {t1, t2, result};
27946 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
27947 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
27948 *entry = elt;
27949 *slot = entry;
27950 return result;
27951 }
27952
27953 /* Set up the hash table for constraint association. */
27954
27955 void
27956 init_constraint_processing (void)
27957 {
27958 if (!flag_concepts)
27959 return;
27960
27961 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
27962 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
27963 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
27964 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
27965 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
27966 }
27967
27968 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27969 0..N-1. */
27970
27971 void
27972 declare_integer_pack (void)
27973 {
27974 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
27975 build_function_type_list (integer_type_node,
27976 integer_type_node,
27977 NULL_TREE),
27978 NULL_TREE, ECF_CONST);
27979 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
27980 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
27981 DECL_FUNCTION_CODE (ipfn)
27982 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
27983 }
27984
27985 /* Set up the hash tables for template instantiations. */
27986
27987 void
27988 init_template_processing (void)
27989 {
27990 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27991 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27992
27993 if (cxx_dialect >= cxx11)
27994 declare_integer_pack ();
27995 }
27996
27997 /* Print stats about the template hash tables for -fstats. */
27998
27999 void
28000 print_template_statistics (void)
28001 {
28002 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28003 "%f collisions\n", (long) decl_specializations->size (),
28004 (long) decl_specializations->elements (),
28005 decl_specializations->collisions ());
28006 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28007 "%f collisions\n", (long) type_specializations->size (),
28008 (long) type_specializations->elements (),
28009 type_specializations->collisions ());
28010 }
28011
28012 #if CHECKING_P
28013
28014 namespace selftest {
28015
28016 /* Verify that build_non_dependent_expr () works, for various expressions,
28017 and that location wrappers don't affect the results. */
28018
28019 static void
28020 test_build_non_dependent_expr ()
28021 {
28022 location_t loc = BUILTINS_LOCATION;
28023
28024 /* Verify constants, without and with location wrappers. */
28025 tree int_cst = build_int_cst (integer_type_node, 42);
28026 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28027
28028 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28029 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28030 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28031
28032 tree string_lit = build_string (4, "foo");
28033 TREE_TYPE (string_lit) = char_array_type_node;
28034 string_lit = fix_string_type (string_lit);
28035 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28036
28037 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28038 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28039 ASSERT_EQ (wrapped_string_lit,
28040 build_non_dependent_expr (wrapped_string_lit));
28041 }
28042
28043 /* Verify that type_dependent_expression_p () works correctly, even
28044 in the presence of location wrapper nodes. */
28045
28046 static void
28047 test_type_dependent_expression_p ()
28048 {
28049 location_t loc = BUILTINS_LOCATION;
28050
28051 tree name = get_identifier ("foo");
28052
28053 /* If no templates are involved, nothing is type-dependent. */
28054 gcc_assert (!processing_template_decl);
28055 ASSERT_FALSE (type_dependent_expression_p (name));
28056
28057 ++processing_template_decl;
28058
28059 /* Within a template, an unresolved name is always type-dependent. */
28060 ASSERT_TRUE (type_dependent_expression_p (name));
28061
28062 /* Ensure it copes with NULL_TREE and errors. */
28063 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28064 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28065
28066 /* A USING_DECL in a template should be type-dependent, even if wrapped
28067 with a location wrapper (PR c++/83799). */
28068 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28069 TREE_TYPE (using_decl) = integer_type_node;
28070 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28071 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28072 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28073 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28074
28075 --processing_template_decl;
28076 }
28077
28078 /* Run all of the selftests within this file. */
28079
28080 void
28081 cp_pt_c_tests ()
28082 {
28083 test_build_non_dependent_expr ();
28084 test_type_dependent_expression_p ();
28085 }
28086
28087 } // namespace selftest
28088
28089 #endif /* #if CHECKING_P */
28090
28091 #include "gt-cp-pt.h"