Fix template/inherit4.C.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 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 struct pending_template *next;
55 struct tinst_level *tinst;
56 };
57
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
60
61 int processing_template_parmlist;
62 static int template_header_count;
63
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
66
67 static GTY(()) struct tinst_level *current_tinst_level;
68
69 static GTY(()) tree saved_access_scope;
70
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
75
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
78 //
79 // Implementation of the RAII helper for creating new local
80 // specializations.
81 local_specialization_stack::local_specialization_stack (lss_policy policy)
82 : saved (local_specializations)
83 {
84 if (policy == lss_blank || !saved)
85 local_specializations = new hash_map<tree, tree>;
86 else
87 local_specializations = new hash_map<tree, tree>(*saved);
88 }
89
90 local_specialization_stack::~local_specialization_stack ()
91 {
92 delete local_specializations;
93 local_specializations = saved;
94 }
95
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth;
98
99 struct GTY((for_user)) spec_entry
100 {
101 tree tmpl;
102 tree args;
103 tree spec;
104 };
105
106 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 {
108 static hashval_t hash (spec_entry *);
109 static bool equal (spec_entry *, spec_entry *);
110 };
111
112 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113
114 static GTY (()) hash_table<spec_hasher> *type_specializations;
115
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
135 };
136
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t, int,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static void template_parm_level_and_index (tree, int*, int*);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
191 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
192 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
193 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
194 static bool check_specialization_scope (void);
195 static tree process_partial_specialization (tree);
196 static void set_current_access_from_decl (tree);
197 static enum template_base_result get_template_base (tree, tree, tree, tree,
198 bool , tree *);
199 static tree try_class_unification (tree, tree, tree, tree, bool);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_typedefs_access_check (tree tmpl, tree targs);
214 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
215 location_t);
216 static tree listify (tree);
217 static tree listify_autos (tree, tree);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220 static bool complex_alias_template_p (const_tree tmpl);
221 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
222 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
223 static tree make_argument_pack (tree);
224 static void register_parameter_specializations (tree, tree);
225
226 /* Make the current scope suitable for access checking when we are
227 processing T. T can be FUNCTION_DECL for instantiated function
228 template, VAR_DECL for static member variable, or TYPE_DECL for
229 alias template (needed by instantiate_decl). */
230
231 static void
232 push_access_scope (tree t)
233 {
234 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
235 || TREE_CODE (t) == TYPE_DECL);
236
237 if (DECL_FRIEND_CONTEXT (t))
238 push_nested_class (DECL_FRIEND_CONTEXT (t));
239 else if (DECL_CLASS_SCOPE_P (t))
240 push_nested_class (DECL_CONTEXT (t));
241 else
242 push_to_top_level ();
243
244 if (TREE_CODE (t) == FUNCTION_DECL)
245 {
246 saved_access_scope = tree_cons
247 (NULL_TREE, current_function_decl, saved_access_scope);
248 current_function_decl = t;
249 }
250 }
251
252 /* Restore the scope set up by push_access_scope. T is the node we
253 are processing. */
254
255 static void
256 pop_access_scope (tree t)
257 {
258 if (TREE_CODE (t) == FUNCTION_DECL)
259 {
260 current_function_decl = TREE_VALUE (saved_access_scope);
261 saved_access_scope = TREE_CHAIN (saved_access_scope);
262 }
263
264 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
265 pop_nested_class ();
266 else
267 pop_from_top_level ();
268 }
269
270 /* Do any processing required when DECL (a member template
271 declaration) is finished. Returns the TEMPLATE_DECL corresponding
272 to DECL, unless it is a specialization, in which case the DECL
273 itself is returned. */
274
275 tree
276 finish_member_template_decl (tree decl)
277 {
278 if (decl == error_mark_node)
279 return error_mark_node;
280
281 gcc_assert (DECL_P (decl));
282
283 if (TREE_CODE (decl) == TYPE_DECL)
284 {
285 tree type;
286
287 type = TREE_TYPE (decl);
288 if (type == error_mark_node)
289 return error_mark_node;
290 if (MAYBE_CLASS_TYPE_P (type)
291 && CLASSTYPE_TEMPLATE_INFO (type)
292 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
293 {
294 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
295 check_member_template (tmpl);
296 return tmpl;
297 }
298 return NULL_TREE;
299 }
300 else if (TREE_CODE (decl) == FIELD_DECL)
301 error ("data member %qD cannot be a member template", decl);
302 else if (DECL_TEMPLATE_INFO (decl))
303 {
304 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
305 {
306 check_member_template (DECL_TI_TEMPLATE (decl));
307 return DECL_TI_TEMPLATE (decl);
308 }
309 else
310 return decl;
311 }
312 else
313 error ("invalid member template declaration %qD", decl);
314
315 return error_mark_node;
316 }
317
318 /* Create a template info node. */
319
320 tree
321 build_template_info (tree template_decl, tree template_args)
322 {
323 tree result = make_node (TEMPLATE_INFO);
324 TI_TEMPLATE (result) = template_decl;
325 TI_ARGS (result) = template_args;
326 return result;
327 }
328
329 /* Return the template info node corresponding to T, whatever T is. */
330
331 tree
332 get_template_info (const_tree t)
333 {
334 tree tinfo = NULL_TREE;
335
336 if (!t || t == error_mark_node)
337 return NULL;
338
339 if (TREE_CODE (t) == NAMESPACE_DECL
340 || TREE_CODE (t) == PARM_DECL)
341 return NULL;
342
343 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
344 tinfo = DECL_TEMPLATE_INFO (t);
345
346 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
347 t = TREE_TYPE (t);
348
349 if (OVERLOAD_TYPE_P (t))
350 tinfo = TYPE_TEMPLATE_INFO (t);
351 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
352 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
353
354 return tinfo;
355 }
356
357 /* Returns the template nesting level of the indicated class TYPE.
358
359 For example, in:
360 template <class T>
361 struct A
362 {
363 template <class U>
364 struct B {};
365 };
366
367 A<T>::B<U> has depth two, while A<T> has depth one.
368 Both A<T>::B<int> and A<int>::B<U> have depth one, if
369 they are instantiations, not specializations.
370
371 This function is guaranteed to return 0 if passed NULL_TREE so
372 that, for example, `template_class_depth (current_class_type)' is
373 always safe. */
374
375 int
376 template_class_depth (tree type)
377 {
378 int depth;
379
380 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
381 {
382 tree tinfo = get_template_info (type);
383
384 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
385 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
386 ++depth;
387
388 if (DECL_P (type))
389 type = CP_DECL_CONTEXT (type);
390 else if (LAMBDA_TYPE_P (type))
391 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
392 else
393 type = CP_TYPE_CONTEXT (type);
394 }
395
396 return depth;
397 }
398
399 /* Subroutine of maybe_begin_member_template_processing.
400 Returns true if processing DECL needs us to push template parms. */
401
402 static bool
403 inline_needs_template_parms (tree decl, bool nsdmi)
404 {
405 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
406 return false;
407
408 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
409 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
410 }
411
412 /* Subroutine of maybe_begin_member_template_processing.
413 Push the template parms in PARMS, starting from LEVELS steps into the
414 chain, and ending at the beginning, since template parms are listed
415 innermost first. */
416
417 static void
418 push_inline_template_parms_recursive (tree parmlist, int levels)
419 {
420 tree parms = TREE_VALUE (parmlist);
421 int i;
422
423 if (levels > 1)
424 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
425
426 ++processing_template_decl;
427 current_template_parms
428 = tree_cons (size_int (processing_template_decl),
429 parms, current_template_parms);
430 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
431
432 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
433 NULL);
434 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
435 {
436 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
437
438 if (error_operand_p (parm))
439 continue;
440
441 gcc_assert (DECL_P (parm));
442
443 switch (TREE_CODE (parm))
444 {
445 case TYPE_DECL:
446 case TEMPLATE_DECL:
447 pushdecl (parm);
448 break;
449
450 case PARM_DECL:
451 /* Push the CONST_DECL. */
452 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
453 break;
454
455 default:
456 gcc_unreachable ();
457 }
458 }
459 }
460
461 /* Restore the template parameter context for a member template, a
462 friend template defined in a class definition, or a non-template
463 member of template class. */
464
465 void
466 maybe_begin_member_template_processing (tree decl)
467 {
468 tree parms;
469 int levels = 0;
470 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
471
472 if (nsdmi)
473 {
474 tree ctx = DECL_CONTEXT (decl);
475 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
476 /* Disregard full specializations (c++/60999). */
477 && uses_template_parms (ctx)
478 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
479 }
480
481 if (inline_needs_template_parms (decl, nsdmi))
482 {
483 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
484 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485
486 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 {
488 --levels;
489 parms = TREE_CHAIN (parms);
490 }
491
492 push_inline_template_parms_recursive (parms, levels);
493 }
494
495 /* Remember how many levels of template parameters we pushed so that
496 we can pop them later. */
497 inline_parm_levels.safe_push (levels);
498 }
499
500 /* Undo the effects of maybe_begin_member_template_processing. */
501
502 void
503 maybe_end_member_template_processing (void)
504 {
505 int i;
506 int last;
507
508 if (inline_parm_levels.length () == 0)
509 return;
510
511 last = inline_parm_levels.pop ();
512 for (i = 0; i < last; ++i)
513 {
514 --processing_template_decl;
515 current_template_parms = TREE_CHAIN (current_template_parms);
516 poplevel (0, 0, 0);
517 }
518 }
519
520 /* Return a new template argument vector which contains all of ARGS,
521 but has as its innermost set of arguments the EXTRA_ARGS. */
522
523 static tree
524 add_to_template_args (tree args, tree extra_args)
525 {
526 tree new_args;
527 int extra_depth;
528 int i;
529 int j;
530
531 if (args == NULL_TREE || extra_args == error_mark_node)
532 return extra_args;
533
534 extra_depth = TMPL_ARGS_DEPTH (extra_args);
535 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536
537 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
538 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539
540 for (j = 1; j <= extra_depth; ++j, ++i)
541 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542
543 return new_args;
544 }
545
546 /* Like add_to_template_args, but only the outermost ARGS are added to
547 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
548 (EXTRA_ARGS) levels are added. This function is used to combine
549 the template arguments from a partial instantiation with the
550 template arguments used to attain the full instantiation from the
551 partial instantiation. */
552
553 static tree
554 add_outermost_template_args (tree args, tree extra_args)
555 {
556 tree new_args;
557
558 /* If there are more levels of EXTRA_ARGS than there are ARGS,
559 something very fishy is going on. */
560 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561
562 /* If *all* the new arguments will be the EXTRA_ARGS, just return
563 them. */
564 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
565 return extra_args;
566
567 /* For the moment, we make ARGS look like it contains fewer levels. */
568 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569
570 new_args = add_to_template_args (args, extra_args);
571
572 /* Now, we restore ARGS to its full dimensions. */
573 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574
575 return new_args;
576 }
577
578 /* Return the N levels of innermost template arguments from the ARGS. */
579
580 tree
581 get_innermost_template_args (tree args, int n)
582 {
583 tree new_args;
584 int extra_levels;
585 int i;
586
587 gcc_assert (n >= 0);
588
589 /* If N is 1, just return the innermost set of template arguments. */
590 if (n == 1)
591 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592
593 /* If we're not removing anything, just return the arguments we were
594 given. */
595 extra_levels = TMPL_ARGS_DEPTH (args) - n;
596 gcc_assert (extra_levels >= 0);
597 if (extra_levels == 0)
598 return args;
599
600 /* Make a new set of arguments, not containing the outer arguments. */
601 new_args = make_tree_vec (n);
602 for (i = 1; i <= n; ++i)
603 SET_TMPL_ARGS_LEVEL (new_args, i,
604 TMPL_ARGS_LEVEL (args, i + extra_levels));
605
606 return new_args;
607 }
608
609 /* The inverse of get_innermost_template_args: Return all but the innermost
610 EXTRA_LEVELS levels of template arguments from the ARGS. */
611
612 static tree
613 strip_innermost_template_args (tree args, int extra_levels)
614 {
615 tree new_args;
616 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
617 int i;
618
619 gcc_assert (n >= 0);
620
621 /* If N is 1, just return the outermost set of template arguments. */
622 if (n == 1)
623 return TMPL_ARGS_LEVEL (args, 1);
624
625 /* If we're not removing anything, just return the arguments we were
626 given. */
627 gcc_assert (extra_levels >= 0);
628 if (extra_levels == 0)
629 return args;
630
631 /* Make a new set of arguments, not containing the inner arguments. */
632 new_args = make_tree_vec (n);
633 for (i = 1; i <= n; ++i)
634 SET_TMPL_ARGS_LEVEL (new_args, i,
635 TMPL_ARGS_LEVEL (args, i));
636
637 return new_args;
638 }
639
640 /* We've got a template header coming up; push to a new level for storing
641 the parms. */
642
643 void
644 begin_template_parm_list (void)
645 {
646 /* We use a non-tag-transparent scope here, which causes pushtag to
647 put tags in this scope, rather than in the enclosing class or
648 namespace scope. This is the right thing, since we want
649 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
650 global template class, push_template_decl handles putting the
651 TEMPLATE_DECL into top-level scope. For a nested template class,
652 e.g.:
653
654 template <class T> struct S1 {
655 template <class T> struct S2 {};
656 };
657
658 pushtag contains special code to insert the TEMPLATE_DECL for S2
659 at the right scope. */
660 begin_scope (sk_template_parms, NULL);
661 ++processing_template_decl;
662 ++processing_template_parmlist;
663 note_template_header (0);
664
665 /* Add a dummy parameter level while we process the parameter list. */
666 current_template_parms
667 = tree_cons (size_int (processing_template_decl),
668 make_tree_vec (0),
669 current_template_parms);
670 }
671
672 /* This routine is called when a specialization is declared. If it is
673 invalid to declare a specialization here, an error is reported and
674 false is returned, otherwise this routine will return true. */
675
676 static bool
677 check_specialization_scope (void)
678 {
679 tree scope = current_scope ();
680
681 /* [temp.expl.spec]
682
683 An explicit specialization shall be declared in the namespace of
684 which the template is a member, or, for member templates, in the
685 namespace of which the enclosing class or enclosing class
686 template is a member. An explicit specialization of a member
687 function, member class or static data member of a class template
688 shall be declared in the namespace of which the class template
689 is a member. */
690 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
691 {
692 error ("explicit specialization in non-namespace scope %qD", scope);
693 return false;
694 }
695
696 /* [temp.expl.spec]
697
698 In an explicit specialization declaration for a member of a class
699 template or a member template that appears in namespace scope,
700 the member template and some of its enclosing class templates may
701 remain unspecialized, except that the declaration shall not
702 explicitly specialize a class member template if its enclosing
703 class templates are not explicitly specialized as well. */
704 if (current_template_parms)
705 {
706 error ("enclosing class templates are not explicitly specialized");
707 return false;
708 }
709
710 return true;
711 }
712
713 /* We've just seen template <>. */
714
715 bool
716 begin_specialization (void)
717 {
718 begin_scope (sk_template_spec, NULL);
719 note_template_header (1);
720 return check_specialization_scope ();
721 }
722
723 /* Called at then end of processing a declaration preceded by
724 template<>. */
725
726 void
727 end_specialization (void)
728 {
729 finish_scope ();
730 reset_specialization ();
731 }
732
733 /* Any template <>'s that we have seen thus far are not referring to a
734 function specialization. */
735
736 void
737 reset_specialization (void)
738 {
739 processing_specialization = 0;
740 template_header_count = 0;
741 }
742
743 /* We've just seen a template header. If SPECIALIZATION is nonzero,
744 it was of the form template <>. */
745
746 static void
747 note_template_header (int specialization)
748 {
749 processing_specialization = specialization;
750 template_header_count++;
751 }
752
753 /* We're beginning an explicit instantiation. */
754
755 void
756 begin_explicit_instantiation (void)
757 {
758 gcc_assert (!processing_explicit_instantiation);
759 processing_explicit_instantiation = true;
760 }
761
762
763 void
764 end_explicit_instantiation (void)
765 {
766 gcc_assert (processing_explicit_instantiation);
767 processing_explicit_instantiation = false;
768 }
769
770 /* An explicit specialization or partial specialization of TMPL is being
771 declared. Check that the namespace in which the specialization is
772 occurring is permissible. Returns false iff it is invalid to
773 specialize TMPL in the current namespace. */
774
775 static bool
776 check_specialization_namespace (tree tmpl)
777 {
778 tree tpl_ns = decl_namespace_context (tmpl);
779
780 /* [tmpl.expl.spec]
781
782 An explicit specialization shall be declared in a namespace enclosing the
783 specialized template. An explicit specialization whose declarator-id is
784 not qualified shall be declared in the nearest enclosing namespace of the
785 template, or, if the namespace is inline (7.3.1), any namespace from its
786 enclosing namespace set. */
787 if (current_scope() != DECL_CONTEXT (tmpl)
788 && !at_namespace_scope_p ())
789 {
790 error ("specialization of %qD must appear at namespace scope", tmpl);
791 return false;
792 }
793
794 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
795 /* Same or enclosing namespace. */
796 return true;
797 else
798 {
799 permerror (input_location,
800 "specialization of %qD in different namespace", tmpl);
801 inform (DECL_SOURCE_LOCATION (tmpl),
802 " from definition of %q#D", tmpl);
803 return false;
804 }
805 }
806
807 /* SPEC is an explicit instantiation. Check that it is valid to
808 perform this explicit instantiation in the current namespace. */
809
810 static void
811 check_explicit_instantiation_namespace (tree spec)
812 {
813 tree ns;
814
815 /* DR 275: An explicit instantiation shall appear in an enclosing
816 namespace of its template. */
817 ns = decl_namespace_context (spec);
818 if (!is_nested_namespace (current_namespace, ns))
819 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
820 "(which does not enclose namespace %qD)",
821 spec, current_namespace, ns);
822 }
823
824 // Returns the type of a template specialization only if that
825 // specialization needs to be defined. Otherwise (e.g., if the type has
826 // already been defined), the function returns NULL_TREE.
827 static tree
828 maybe_new_partial_specialization (tree type)
829 {
830 // An implicit instantiation of an incomplete type implies
831 // the definition of a new class template.
832 //
833 // template<typename T>
834 // struct S;
835 //
836 // template<typename T>
837 // struct S<T*>;
838 //
839 // Here, S<T*> is an implicit instantiation of S whose type
840 // is incomplete.
841 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
842 return type;
843
844 // It can also be the case that TYPE is a completed specialization.
845 // Continuing the previous example, suppose we also declare:
846 //
847 // template<typename T>
848 // requires Integral<T>
849 // struct S<T*>;
850 //
851 // Here, S<T*> refers to the specialization S<T*> defined
852 // above. However, we need to differentiate definitions because
853 // we intend to define a new partial specialization. In this case,
854 // we rely on the fact that the constraints are different for
855 // this declaration than that above.
856 //
857 // Note that we also get here for injected class names and
858 // late-parsed template definitions. We must ensure that we
859 // do not create new type declarations for those cases.
860 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
861 {
862 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
863 tree args = CLASSTYPE_TI_ARGS (type);
864
865 // If there are no template parameters, this cannot be a new
866 // partial template specializtion?
867 if (!current_template_parms)
868 return NULL_TREE;
869
870 // The injected-class-name is not a new partial specialization.
871 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
872 return NULL_TREE;
873
874 // If the constraints are not the same as those of the primary
875 // then, we can probably create a new specialization.
876 tree type_constr = current_template_constraints ();
877
878 if (type == TREE_TYPE (tmpl))
879 {
880 tree main_constr = get_constraints (tmpl);
881 if (equivalent_constraints (type_constr, main_constr))
882 return NULL_TREE;
883 }
884
885 // Also, if there's a pre-existing specialization with matching
886 // constraints, then this also isn't new.
887 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
888 while (specs)
889 {
890 tree spec_tmpl = TREE_VALUE (specs);
891 tree spec_args = TREE_PURPOSE (specs);
892 tree spec_constr = get_constraints (spec_tmpl);
893 if (comp_template_args (args, spec_args)
894 && equivalent_constraints (type_constr, spec_constr))
895 return NULL_TREE;
896 specs = TREE_CHAIN (specs);
897 }
898
899 // Create a new type node (and corresponding type decl)
900 // for the newly declared specialization.
901 tree t = make_class_type (TREE_CODE (type));
902 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
903 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
904
905 /* We only need a separate type node for storing the definition of this
906 partial specialization; uses of S<T*> are unconstrained, so all are
907 equivalent. So keep TYPE_CANONICAL the same. */
908 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
909
910 // Build the corresponding type decl.
911 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
912 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
913 DECL_SOURCE_LOCATION (d) = input_location;
914
915 return t;
916 }
917
918 return NULL_TREE;
919 }
920
921 /* The TYPE is being declared. If it is a template type, that means it
922 is a partial specialization. Do appropriate error-checking. */
923
924 tree
925 maybe_process_partial_specialization (tree type)
926 {
927 tree context;
928
929 if (type == error_mark_node)
930 return error_mark_node;
931
932 /* A lambda that appears in specialization context is not itself a
933 specialization. */
934 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
935 return type;
936
937 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
938 {
939 error ("name of class shadows template template parameter %qD",
940 TYPE_NAME (type));
941 return error_mark_node;
942 }
943
944 context = TYPE_CONTEXT (type);
945
946 if (TYPE_ALIAS_P (type))
947 {
948 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
949
950 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
951 error ("specialization of alias template %qD",
952 TI_TEMPLATE (tinfo));
953 else
954 error ("explicit specialization of non-template %qT", type);
955 return error_mark_node;
956 }
957 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
958 {
959 /* This is for ordinary explicit specialization and partial
960 specialization of a template class such as:
961
962 template <> class C<int>;
963
964 or:
965
966 template <class T> class C<T*>;
967
968 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
969
970 if (tree t = maybe_new_partial_specialization (type))
971 {
972 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
973 && !at_namespace_scope_p ())
974 return error_mark_node;
975 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
976 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
977 if (processing_template_decl)
978 {
979 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
980 if (decl == error_mark_node)
981 return error_mark_node;
982 return TREE_TYPE (decl);
983 }
984 }
985 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
986 error ("specialization of %qT after instantiation", type);
987 else if (errorcount && !processing_specialization
988 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
989 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
990 /* Trying to define a specialization either without a template<> header
991 or in an inappropriate place. We've already given an error, so just
992 bail now so we don't actually define the specialization. */
993 return error_mark_node;
994 }
995 else if (CLASS_TYPE_P (type)
996 && !CLASSTYPE_USE_TEMPLATE (type)
997 && CLASSTYPE_TEMPLATE_INFO (type)
998 && context && CLASS_TYPE_P (context)
999 && CLASSTYPE_TEMPLATE_INFO (context))
1000 {
1001 /* This is for an explicit specialization of member class
1002 template according to [temp.expl.spec/18]:
1003
1004 template <> template <class U> class C<int>::D;
1005
1006 The context `C<int>' must be an implicit instantiation.
1007 Otherwise this is just a member class template declared
1008 earlier like:
1009
1010 template <> class C<int> { template <class U> class D; };
1011 template <> template <class U> class C<int>::D;
1012
1013 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1014 while in the second case, `C<int>::D' is a primary template
1015 and `C<T>::D' may not exist. */
1016
1017 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1018 && !COMPLETE_TYPE_P (type))
1019 {
1020 tree t;
1021 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1022
1023 if (current_namespace
1024 != decl_namespace_context (tmpl))
1025 {
1026 permerror (input_location,
1027 "specializing %q#T in different namespace", type);
1028 permerror (DECL_SOURCE_LOCATION (tmpl),
1029 " from definition of %q#D", tmpl);
1030 }
1031
1032 /* Check for invalid specialization after instantiation:
1033
1034 template <> template <> class C<int>::D<int>;
1035 template <> template <class U> class C<int>::D; */
1036
1037 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1038 t; t = TREE_CHAIN (t))
1039 {
1040 tree inst = TREE_VALUE (t);
1041 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1042 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1043 {
1044 /* We already have a full specialization of this partial
1045 instantiation, or a full specialization has been
1046 looked up but not instantiated. Reassign it to the
1047 new member specialization template. */
1048 spec_entry elt;
1049 spec_entry *entry;
1050
1051 elt.tmpl = most_general_template (tmpl);
1052 elt.args = CLASSTYPE_TI_ARGS (inst);
1053 elt.spec = inst;
1054
1055 type_specializations->remove_elt (&elt);
1056
1057 elt.tmpl = tmpl;
1058 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1059
1060 spec_entry **slot
1061 = type_specializations->find_slot (&elt, INSERT);
1062 entry = ggc_alloc<spec_entry> ();
1063 *entry = elt;
1064 *slot = entry;
1065 }
1066 else
1067 /* But if we've had an implicit instantiation, that's a
1068 problem ([temp.expl.spec]/6). */
1069 error ("specialization %qT after instantiation %qT",
1070 type, inst);
1071 }
1072
1073 /* Mark TYPE as a specialization. And as a result, we only
1074 have one level of template argument for the innermost
1075 class template. */
1076 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1077 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1078 CLASSTYPE_TI_ARGS (type)
1079 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1080 }
1081 }
1082 else if (processing_specialization)
1083 {
1084 /* Someday C++0x may allow for enum template specialization. */
1085 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1086 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1087 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1088 "of %qD not allowed by ISO C++", type);
1089 else
1090 {
1091 error ("explicit specialization of non-template %qT", type);
1092 return error_mark_node;
1093 }
1094 }
1095
1096 return type;
1097 }
1098
1099 /* Returns nonzero if we can optimize the retrieval of specializations
1100 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1101 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1102
1103 static inline bool
1104 optimize_specialization_lookup_p (tree tmpl)
1105 {
1106 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1107 && DECL_CLASS_SCOPE_P (tmpl)
1108 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1109 parameter. */
1110 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1111 /* The optimized lookup depends on the fact that the
1112 template arguments for the member function template apply
1113 purely to the containing class, which is not true if the
1114 containing class is an explicit or partial
1115 specialization. */
1116 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1117 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1118 && !DECL_CONV_FN_P (tmpl)
1119 /* It is possible to have a template that is not a member
1120 template and is not a member of a template class:
1121
1122 template <typename T>
1123 struct S { friend A::f(); };
1124
1125 Here, the friend function is a template, but the context does
1126 not have template information. The optimized lookup relies
1127 on having ARGS be the template arguments for both the class
1128 and the function template. */
1129 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1130 }
1131
1132 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1133 gone through coerce_template_parms by now. */
1134
1135 static void
1136 verify_unstripped_args (tree args)
1137 {
1138 ++processing_template_decl;
1139 if (!any_dependent_template_arguments_p (args))
1140 {
1141 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1142 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1143 {
1144 tree arg = TREE_VEC_ELT (inner, i);
1145 if (TREE_CODE (arg) == TEMPLATE_DECL)
1146 /* OK */;
1147 else if (TYPE_P (arg))
1148 gcc_assert (strip_typedefs (arg, NULL) == arg);
1149 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1150 /* Allow typedefs on the type of a non-type argument, since a
1151 parameter can have them. */;
1152 else
1153 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1154 }
1155 }
1156 --processing_template_decl;
1157 }
1158
1159 /* Retrieve the specialization (in the sense of [temp.spec] - a
1160 specialization is either an instantiation or an explicit
1161 specialization) of TMPL for the given template ARGS. If there is
1162 no such specialization, return NULL_TREE. The ARGS are a vector of
1163 arguments, or a vector of vectors of arguments, in the case of
1164 templates with more than one level of parameters.
1165
1166 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1167 then we search for a partial specialization matching ARGS. This
1168 parameter is ignored if TMPL is not a class template.
1169
1170 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1171 result is a NONTYPE_ARGUMENT_PACK. */
1172
1173 static tree
1174 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1175 {
1176 if (tmpl == NULL_TREE)
1177 return NULL_TREE;
1178
1179 if (args == error_mark_node)
1180 return NULL_TREE;
1181
1182 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1183 || TREE_CODE (tmpl) == FIELD_DECL);
1184
1185 /* There should be as many levels of arguments as there are
1186 levels of parameters. */
1187 gcc_assert (TMPL_ARGS_DEPTH (args)
1188 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1189 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1190 : template_class_depth (DECL_CONTEXT (tmpl))));
1191
1192 if (flag_checking)
1193 verify_unstripped_args (args);
1194
1195 /* Lambda functions in templates aren't instantiated normally, but through
1196 tsubst_lambda_expr. */
1197 if (lambda_fn_in_template_p (tmpl))
1198 return NULL_TREE;
1199
1200 if (optimize_specialization_lookup_p (tmpl))
1201 {
1202 /* The template arguments actually apply to the containing
1203 class. Find the class specialization with those
1204 arguments. */
1205 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1206 tree class_specialization
1207 = retrieve_specialization (class_template, args, 0);
1208 if (!class_specialization)
1209 return NULL_TREE;
1210
1211 /* Find the instance of TMPL. */
1212 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1213 for (ovl_iterator iter (fns); iter; ++iter)
1214 {
1215 tree fn = *iter;
1216 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1217 /* using-declarations can add base methods to the method vec,
1218 and we don't want those here. */
1219 && DECL_CONTEXT (fn) == class_specialization)
1220 return fn;
1221 }
1222 return NULL_TREE;
1223 }
1224 else
1225 {
1226 spec_entry *found;
1227 spec_entry elt;
1228 hash_table<spec_hasher> *specializations;
1229
1230 elt.tmpl = tmpl;
1231 elt.args = args;
1232 elt.spec = NULL_TREE;
1233
1234 if (DECL_CLASS_TEMPLATE_P (tmpl))
1235 specializations = type_specializations;
1236 else
1237 specializations = decl_specializations;
1238
1239 if (hash == 0)
1240 hash = spec_hasher::hash (&elt);
1241 found = specializations->find_with_hash (&elt, hash);
1242 if (found)
1243 return found->spec;
1244 }
1245
1246 return NULL_TREE;
1247 }
1248
1249 /* Like retrieve_specialization, but for local declarations. */
1250
1251 tree
1252 retrieve_local_specialization (tree tmpl)
1253 {
1254 if (local_specializations == NULL)
1255 return NULL_TREE;
1256
1257 tree *slot = local_specializations->get (tmpl);
1258 return slot ? *slot : NULL_TREE;
1259 }
1260
1261 /* Returns nonzero iff DECL is a specialization of TMPL. */
1262
1263 int
1264 is_specialization_of (tree decl, tree tmpl)
1265 {
1266 tree t;
1267
1268 if (TREE_CODE (decl) == FUNCTION_DECL)
1269 {
1270 for (t = decl;
1271 t != NULL_TREE;
1272 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1273 if (t == tmpl)
1274 return 1;
1275 }
1276 else
1277 {
1278 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1279
1280 for (t = TREE_TYPE (decl);
1281 t != NULL_TREE;
1282 t = CLASSTYPE_USE_TEMPLATE (t)
1283 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1284 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1285 return 1;
1286 }
1287
1288 return 0;
1289 }
1290
1291 /* Returns nonzero iff DECL is a specialization of friend declaration
1292 FRIEND_DECL according to [temp.friend]. */
1293
1294 bool
1295 is_specialization_of_friend (tree decl, tree friend_decl)
1296 {
1297 bool need_template = true;
1298 int template_depth;
1299
1300 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1301 || TREE_CODE (decl) == TYPE_DECL);
1302
1303 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1304 of a template class, we want to check if DECL is a specialization
1305 if this. */
1306 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1307 && DECL_TEMPLATE_INFO (friend_decl)
1308 && !DECL_USE_TEMPLATE (friend_decl))
1309 {
1310 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1311 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1312 need_template = false;
1313 }
1314 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1315 && !PRIMARY_TEMPLATE_P (friend_decl))
1316 need_template = false;
1317
1318 /* There is nothing to do if this is not a template friend. */
1319 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1320 return false;
1321
1322 if (is_specialization_of (decl, friend_decl))
1323 return true;
1324
1325 /* [temp.friend/6]
1326 A member of a class template may be declared to be a friend of a
1327 non-template class. In this case, the corresponding member of
1328 every specialization of the class template is a friend of the
1329 class granting friendship.
1330
1331 For example, given a template friend declaration
1332
1333 template <class T> friend void A<T>::f();
1334
1335 the member function below is considered a friend
1336
1337 template <> struct A<int> {
1338 void f();
1339 };
1340
1341 For this type of template friend, TEMPLATE_DEPTH below will be
1342 nonzero. To determine if DECL is a friend of FRIEND, we first
1343 check if the enclosing class is a specialization of another. */
1344
1345 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1346 if (template_depth
1347 && DECL_CLASS_SCOPE_P (decl)
1348 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1349 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1350 {
1351 /* Next, we check the members themselves. In order to handle
1352 a few tricky cases, such as when FRIEND_DECL's are
1353
1354 template <class T> friend void A<T>::g(T t);
1355 template <class T> template <T t> friend void A<T>::h();
1356
1357 and DECL's are
1358
1359 void A<int>::g(int);
1360 template <int> void A<int>::h();
1361
1362 we need to figure out ARGS, the template arguments from
1363 the context of DECL. This is required for template substitution
1364 of `T' in the function parameter of `g' and template parameter
1365 of `h' in the above examples. Here ARGS corresponds to `int'. */
1366
1367 tree context = DECL_CONTEXT (decl);
1368 tree args = NULL_TREE;
1369 int current_depth = 0;
1370
1371 while (current_depth < template_depth)
1372 {
1373 if (CLASSTYPE_TEMPLATE_INFO (context))
1374 {
1375 if (current_depth == 0)
1376 args = TYPE_TI_ARGS (context);
1377 else
1378 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1379 current_depth++;
1380 }
1381 context = TYPE_CONTEXT (context);
1382 }
1383
1384 if (TREE_CODE (decl) == FUNCTION_DECL)
1385 {
1386 bool is_template;
1387 tree friend_type;
1388 tree decl_type;
1389 tree friend_args_type;
1390 tree decl_args_type;
1391
1392 /* Make sure that both DECL and FRIEND_DECL are templates or
1393 non-templates. */
1394 is_template = DECL_TEMPLATE_INFO (decl)
1395 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1396 if (need_template ^ is_template)
1397 return false;
1398 else if (is_template)
1399 {
1400 /* If both are templates, check template parameter list. */
1401 tree friend_parms
1402 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1403 args, tf_none);
1404 if (!comp_template_parms
1405 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1406 friend_parms))
1407 return false;
1408
1409 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1410 }
1411 else
1412 decl_type = TREE_TYPE (decl);
1413
1414 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1415 tf_none, NULL_TREE);
1416 if (friend_type == error_mark_node)
1417 return false;
1418
1419 /* Check if return types match. */
1420 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1421 return false;
1422
1423 /* Check if function parameter types match, ignoring the
1424 `this' parameter. */
1425 friend_args_type = TYPE_ARG_TYPES (friend_type);
1426 decl_args_type = TYPE_ARG_TYPES (decl_type);
1427 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1428 friend_args_type = TREE_CHAIN (friend_args_type);
1429 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1430 decl_args_type = TREE_CHAIN (decl_args_type);
1431
1432 return compparms (decl_args_type, friend_args_type);
1433 }
1434 else
1435 {
1436 /* DECL is a TYPE_DECL */
1437 bool is_template;
1438 tree decl_type = TREE_TYPE (decl);
1439
1440 /* Make sure that both DECL and FRIEND_DECL are templates or
1441 non-templates. */
1442 is_template
1443 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1444 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1445
1446 if (need_template ^ is_template)
1447 return false;
1448 else if (is_template)
1449 {
1450 tree friend_parms;
1451 /* If both are templates, check the name of the two
1452 TEMPLATE_DECL's first because is_friend didn't. */
1453 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1454 != DECL_NAME (friend_decl))
1455 return false;
1456
1457 /* Now check template parameter list. */
1458 friend_parms
1459 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1460 args, tf_none);
1461 return comp_template_parms
1462 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1463 friend_parms);
1464 }
1465 else
1466 return (DECL_NAME (decl)
1467 == DECL_NAME (friend_decl));
1468 }
1469 }
1470 return false;
1471 }
1472
1473 /* Register the specialization SPEC as a specialization of TMPL with
1474 the indicated ARGS. IS_FRIEND indicates whether the specialization
1475 is actually just a friend declaration. Returns SPEC, or an
1476 equivalent prior declaration, if available.
1477
1478 We also store instantiations of field packs in the hash table, even
1479 though they are not themselves templates, to make lookup easier. */
1480
1481 static tree
1482 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1483 hashval_t hash)
1484 {
1485 tree fn;
1486 spec_entry **slot = NULL;
1487 spec_entry elt;
1488
1489 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1490 || (TREE_CODE (tmpl) == FIELD_DECL
1491 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1492
1493 if (TREE_CODE (spec) == FUNCTION_DECL
1494 && uses_template_parms (DECL_TI_ARGS (spec)))
1495 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1496 register it; we want the corresponding TEMPLATE_DECL instead.
1497 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1498 the more obvious `uses_template_parms (spec)' to avoid problems
1499 with default function arguments. In particular, given
1500 something like this:
1501
1502 template <class T> void f(T t1, T t = T())
1503
1504 the default argument expression is not substituted for in an
1505 instantiation unless and until it is actually needed. */
1506 return spec;
1507
1508 if (optimize_specialization_lookup_p (tmpl))
1509 /* We don't put these specializations in the hash table, but we might
1510 want to give an error about a mismatch. */
1511 fn = retrieve_specialization (tmpl, args, 0);
1512 else
1513 {
1514 elt.tmpl = tmpl;
1515 elt.args = args;
1516 elt.spec = spec;
1517
1518 if (hash == 0)
1519 hash = spec_hasher::hash (&elt);
1520
1521 slot =
1522 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1523 if (*slot)
1524 fn = ((spec_entry *) *slot)->spec;
1525 else
1526 fn = NULL_TREE;
1527 }
1528
1529 /* We can sometimes try to re-register a specialization that we've
1530 already got. In particular, regenerate_decl_from_template calls
1531 duplicate_decls which will update the specialization list. But,
1532 we'll still get called again here anyhow. It's more convenient
1533 to simply allow this than to try to prevent it. */
1534 if (fn == spec)
1535 return spec;
1536 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1537 {
1538 if (DECL_TEMPLATE_INSTANTIATION (fn))
1539 {
1540 if (DECL_ODR_USED (fn)
1541 || DECL_EXPLICIT_INSTANTIATION (fn))
1542 {
1543 error ("specialization of %qD after instantiation",
1544 fn);
1545 return error_mark_node;
1546 }
1547 else
1548 {
1549 tree clone;
1550 /* This situation should occur only if the first
1551 specialization is an implicit instantiation, the
1552 second is an explicit specialization, and the
1553 implicit instantiation has not yet been used. That
1554 situation can occur if we have implicitly
1555 instantiated a member function and then specialized
1556 it later.
1557
1558 We can also wind up here if a friend declaration that
1559 looked like an instantiation turns out to be a
1560 specialization:
1561
1562 template <class T> void foo(T);
1563 class S { friend void foo<>(int) };
1564 template <> void foo(int);
1565
1566 We transform the existing DECL in place so that any
1567 pointers to it become pointers to the updated
1568 declaration.
1569
1570 If there was a definition for the template, but not
1571 for the specialization, we want this to look as if
1572 there were no definition, and vice versa. */
1573 DECL_INITIAL (fn) = NULL_TREE;
1574 duplicate_decls (spec, fn, is_friend);
1575 /* The call to duplicate_decls will have applied
1576 [temp.expl.spec]:
1577
1578 An explicit specialization of a function template
1579 is inline only if it is explicitly declared to be,
1580 and independently of whether its function template
1581 is.
1582
1583 to the primary function; now copy the inline bits to
1584 the various clones. */
1585 FOR_EACH_CLONE (clone, fn)
1586 {
1587 DECL_DECLARED_INLINE_P (clone)
1588 = DECL_DECLARED_INLINE_P (fn);
1589 DECL_SOURCE_LOCATION (clone)
1590 = DECL_SOURCE_LOCATION (fn);
1591 DECL_DELETED_FN (clone)
1592 = DECL_DELETED_FN (fn);
1593 }
1594 check_specialization_namespace (tmpl);
1595
1596 return fn;
1597 }
1598 }
1599 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1600 {
1601 tree dd = duplicate_decls (spec, fn, is_friend);
1602 if (dd == error_mark_node)
1603 /* We've already complained in duplicate_decls. */
1604 return error_mark_node;
1605
1606 if (dd == NULL_TREE && DECL_INITIAL (spec))
1607 /* Dup decl failed, but this is a new definition. Set the
1608 line number so any errors match this new
1609 definition. */
1610 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1611
1612 return fn;
1613 }
1614 }
1615 else if (fn)
1616 return duplicate_decls (spec, fn, is_friend);
1617
1618 /* A specialization must be declared in the same namespace as the
1619 template it is specializing. */
1620 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1621 && !check_specialization_namespace (tmpl))
1622 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1623
1624 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1625 {
1626 spec_entry *entry = ggc_alloc<spec_entry> ();
1627 gcc_assert (tmpl && args && spec);
1628 *entry = elt;
1629 *slot = entry;
1630 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1631 && PRIMARY_TEMPLATE_P (tmpl)
1632 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1633 || variable_template_p (tmpl))
1634 /* If TMPL is a forward declaration of a template function, keep a list
1635 of all specializations in case we need to reassign them to a friend
1636 template later in tsubst_friend_function.
1637
1638 Also keep a list of all variable template instantiations so that
1639 process_partial_specialization can check whether a later partial
1640 specialization would have used it. */
1641 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1642 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1643 }
1644
1645 return spec;
1646 }
1647
1648 /* Returns true iff two spec_entry nodes are equivalent. */
1649
1650 int comparing_specializations;
1651
1652 bool
1653 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1654 {
1655 int equal;
1656
1657 ++comparing_specializations;
1658 equal = (e1->tmpl == e2->tmpl
1659 && comp_template_args (e1->args, e2->args));
1660 if (equal && flag_concepts
1661 /* tmpl could be a FIELD_DECL for a capture pack. */
1662 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1663 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1664 && uses_template_parms (e1->args))
1665 {
1666 /* Partial specializations of a variable template can be distinguished by
1667 constraints. */
1668 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1669 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1670 equal = equivalent_constraints (c1, c2);
1671 }
1672 --comparing_specializations;
1673
1674 return equal;
1675 }
1676
1677 /* Returns a hash for a template TMPL and template arguments ARGS. */
1678
1679 static hashval_t
1680 hash_tmpl_and_args (tree tmpl, tree args)
1681 {
1682 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1683 return iterative_hash_template_arg (args, val);
1684 }
1685
1686 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1687 ignoring SPEC. */
1688
1689 hashval_t
1690 spec_hasher::hash (spec_entry *e)
1691 {
1692 return hash_tmpl_and_args (e->tmpl, e->args);
1693 }
1694
1695 /* Recursively calculate a hash value for a template argument ARG, for use
1696 in the hash tables of template specializations. */
1697
1698 hashval_t
1699 iterative_hash_template_arg (tree arg, hashval_t val)
1700 {
1701 unsigned HOST_WIDE_INT i;
1702 enum tree_code code;
1703 char tclass;
1704
1705 if (arg == NULL_TREE)
1706 return iterative_hash_object (arg, val);
1707
1708 if (!TYPE_P (arg))
1709 STRIP_NOPS (arg);
1710
1711 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1712 gcc_unreachable ();
1713
1714 code = TREE_CODE (arg);
1715 tclass = TREE_CODE_CLASS (code);
1716
1717 val = iterative_hash_object (code, val);
1718
1719 switch (code)
1720 {
1721 case ERROR_MARK:
1722 return val;
1723
1724 case IDENTIFIER_NODE:
1725 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1726
1727 case TREE_VEC:
1728 {
1729 int i, len = TREE_VEC_LENGTH (arg);
1730 for (i = 0; i < len; ++i)
1731 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1732 return val;
1733 }
1734
1735 case TYPE_PACK_EXPANSION:
1736 case EXPR_PACK_EXPANSION:
1737 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1738 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1739
1740 case TYPE_ARGUMENT_PACK:
1741 case NONTYPE_ARGUMENT_PACK:
1742 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1743
1744 case TREE_LIST:
1745 for (; arg; arg = TREE_CHAIN (arg))
1746 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1747 return val;
1748
1749 case OVERLOAD:
1750 for (lkp_iterator iter (arg); iter; ++iter)
1751 val = iterative_hash_template_arg (*iter, val);
1752 return val;
1753
1754 case CONSTRUCTOR:
1755 {
1756 tree field, value;
1757 iterative_hash_template_arg (TREE_TYPE (arg), val);
1758 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1759 {
1760 val = iterative_hash_template_arg (field, val);
1761 val = iterative_hash_template_arg (value, val);
1762 }
1763 return val;
1764 }
1765
1766 case PARM_DECL:
1767 if (!DECL_ARTIFICIAL (arg))
1768 {
1769 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1770 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1771 }
1772 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1773
1774 case TARGET_EXPR:
1775 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1776
1777 case PTRMEM_CST:
1778 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1779 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1780
1781 case TEMPLATE_PARM_INDEX:
1782 val = iterative_hash_template_arg
1783 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1784 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1785 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1786
1787 case TRAIT_EXPR:
1788 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1789 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1790 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1791
1792 case BASELINK:
1793 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1794 val);
1795 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1796 val);
1797
1798 case MODOP_EXPR:
1799 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1800 code = TREE_CODE (TREE_OPERAND (arg, 1));
1801 val = iterative_hash_object (code, val);
1802 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1803
1804 case LAMBDA_EXPR:
1805 /* A lambda can't appear in a template arg, but don't crash on
1806 erroneous input. */
1807 gcc_assert (seen_error ());
1808 return val;
1809
1810 case CAST_EXPR:
1811 case IMPLICIT_CONV_EXPR:
1812 case STATIC_CAST_EXPR:
1813 case REINTERPRET_CAST_EXPR:
1814 case CONST_CAST_EXPR:
1815 case DYNAMIC_CAST_EXPR:
1816 case NEW_EXPR:
1817 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1818 /* Now hash operands as usual. */
1819 break;
1820
1821 default:
1822 break;
1823 }
1824
1825 switch (tclass)
1826 {
1827 case tcc_type:
1828 if (alias_template_specialization_p (arg))
1829 {
1830 // We want an alias specialization that survived strip_typedefs
1831 // to hash differently from its TYPE_CANONICAL, to avoid hash
1832 // collisions that compare as different in template_args_equal.
1833 // These could be dependent specializations that strip_typedefs
1834 // left alone, or untouched specializations because
1835 // coerce_template_parms returns the unconverted template
1836 // arguments if it sees incomplete argument packs.
1837 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1838 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1839 }
1840 if (TYPE_CANONICAL (arg))
1841 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1842 val);
1843 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1844 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1845 /* Otherwise just compare the types during lookup. */
1846 return val;
1847
1848 case tcc_declaration:
1849 case tcc_constant:
1850 return iterative_hash_expr (arg, val);
1851
1852 default:
1853 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1854 {
1855 unsigned n = cp_tree_operand_length (arg);
1856 for (i = 0; i < n; ++i)
1857 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1858 return val;
1859 }
1860 }
1861 gcc_unreachable ();
1862 return 0;
1863 }
1864
1865 /* Unregister the specialization SPEC as a specialization of TMPL.
1866 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1867 if the SPEC was listed as a specialization of TMPL.
1868
1869 Note that SPEC has been ggc_freed, so we can't look inside it. */
1870
1871 bool
1872 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1873 {
1874 spec_entry *entry;
1875 spec_entry elt;
1876
1877 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1878 elt.args = TI_ARGS (tinfo);
1879 elt.spec = NULL_TREE;
1880
1881 entry = decl_specializations->find (&elt);
1882 if (entry != NULL)
1883 {
1884 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1885 gcc_assert (new_spec != NULL_TREE);
1886 entry->spec = new_spec;
1887 return 1;
1888 }
1889
1890 return 0;
1891 }
1892
1893 /* Like register_specialization, but for local declarations. We are
1894 registering SPEC, an instantiation of TMPL. */
1895
1896 void
1897 register_local_specialization (tree spec, tree tmpl)
1898 {
1899 gcc_assert (tmpl != spec);
1900 local_specializations->put (tmpl, spec);
1901 }
1902
1903 /* TYPE is a class type. Returns true if TYPE is an explicitly
1904 specialized class. */
1905
1906 bool
1907 explicit_class_specialization_p (tree type)
1908 {
1909 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1910 return false;
1911 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1912 }
1913
1914 /* Print the list of functions at FNS, going through all the overloads
1915 for each element of the list. Alternatively, FNS can not be a
1916 TREE_LIST, in which case it will be printed together with all the
1917 overloads.
1918
1919 MORE and *STR should respectively be FALSE and NULL when the function
1920 is called from the outside. They are used internally on recursive
1921 calls. print_candidates manages the two parameters and leaves NULL
1922 in *STR when it ends. */
1923
1924 static void
1925 print_candidates_1 (tree fns, char **str, bool more = false)
1926 {
1927 if (TREE_CODE (fns) == TREE_LIST)
1928 for (; fns; fns = TREE_CHAIN (fns))
1929 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1930 else
1931 for (lkp_iterator iter (fns); iter;)
1932 {
1933 tree cand = *iter;
1934 ++iter;
1935
1936 const char *pfx = *str;
1937 if (!pfx)
1938 {
1939 if (more || iter)
1940 pfx = _("candidates are:");
1941 else
1942 pfx = _("candidate is:");
1943 *str = get_spaces (pfx);
1944 }
1945 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1946 }
1947 }
1948
1949 /* Print the list of candidate FNS in an error message. FNS can also
1950 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1951
1952 void
1953 print_candidates (tree fns)
1954 {
1955 char *str = NULL;
1956 print_candidates_1 (fns, &str);
1957 free (str);
1958 }
1959
1960 /* Get a (possibly) constrained template declaration for the
1961 purpose of ordering candidates. */
1962 static tree
1963 get_template_for_ordering (tree list)
1964 {
1965 gcc_assert (TREE_CODE (list) == TREE_LIST);
1966 tree f = TREE_VALUE (list);
1967 if (tree ti = DECL_TEMPLATE_INFO (f))
1968 return TI_TEMPLATE (ti);
1969 return f;
1970 }
1971
1972 /* Among candidates having the same signature, return the
1973 most constrained or NULL_TREE if there is no best candidate.
1974 If the signatures of candidates vary (e.g., template
1975 specialization vs. member function), then there can be no
1976 most constrained.
1977
1978 Note that we don't compare constraints on the functions
1979 themselves, but rather those of their templates. */
1980 static tree
1981 most_constrained_function (tree candidates)
1982 {
1983 // Try to find the best candidate in a first pass.
1984 tree champ = candidates;
1985 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1986 {
1987 int winner = more_constrained (get_template_for_ordering (champ),
1988 get_template_for_ordering (c));
1989 if (winner == -1)
1990 champ = c; // The candidate is more constrained
1991 else if (winner == 0)
1992 return NULL_TREE; // Neither is more constrained
1993 }
1994
1995 // Verify that the champ is better than previous candidates.
1996 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1997 if (!more_constrained (get_template_for_ordering (champ),
1998 get_template_for_ordering (c)))
1999 return NULL_TREE;
2000 }
2001
2002 return champ;
2003 }
2004
2005
2006 /* Returns the template (one of the functions given by TEMPLATE_ID)
2007 which can be specialized to match the indicated DECL with the
2008 explicit template args given in TEMPLATE_ID. The DECL may be
2009 NULL_TREE if none is available. In that case, the functions in
2010 TEMPLATE_ID are non-members.
2011
2012 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2013 specialization of a member template.
2014
2015 The TEMPLATE_COUNT is the number of references to qualifying
2016 template classes that appeared in the name of the function. See
2017 check_explicit_specialization for a more accurate description.
2018
2019 TSK indicates what kind of template declaration (if any) is being
2020 declared. TSK_TEMPLATE indicates that the declaration given by
2021 DECL, though a FUNCTION_DECL, has template parameters, and is
2022 therefore a template function.
2023
2024 The template args (those explicitly specified and those deduced)
2025 are output in a newly created vector *TARGS_OUT.
2026
2027 If it is impossible to determine the result, an error message is
2028 issued. The error_mark_node is returned to indicate failure. */
2029
2030 static tree
2031 determine_specialization (tree template_id,
2032 tree decl,
2033 tree* targs_out,
2034 int need_member_template,
2035 int template_count,
2036 tmpl_spec_kind tsk)
2037 {
2038 tree fns;
2039 tree targs;
2040 tree explicit_targs;
2041 tree candidates = NULL_TREE;
2042
2043 /* A TREE_LIST of templates of which DECL may be a specialization.
2044 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2045 corresponding TREE_PURPOSE is the set of template arguments that,
2046 when used to instantiate the template, would produce a function
2047 with the signature of DECL. */
2048 tree templates = NULL_TREE;
2049 int header_count;
2050 cp_binding_level *b;
2051
2052 *targs_out = NULL_TREE;
2053
2054 if (template_id == error_mark_node || decl == error_mark_node)
2055 return error_mark_node;
2056
2057 /* We shouldn't be specializing a member template of an
2058 unspecialized class template; we already gave an error in
2059 check_specialization_scope, now avoid crashing. */
2060 if (template_count && DECL_CLASS_SCOPE_P (decl)
2061 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2062 {
2063 gcc_assert (errorcount);
2064 return error_mark_node;
2065 }
2066
2067 fns = TREE_OPERAND (template_id, 0);
2068 explicit_targs = TREE_OPERAND (template_id, 1);
2069
2070 if (fns == error_mark_node)
2071 return error_mark_node;
2072
2073 /* Check for baselinks. */
2074 if (BASELINK_P (fns))
2075 fns = BASELINK_FUNCTIONS (fns);
2076
2077 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2078 {
2079 error ("%qD is not a function template", fns);
2080 return error_mark_node;
2081 }
2082 else if (VAR_P (decl) && !variable_template_p (fns))
2083 {
2084 error ("%qD is not a variable template", fns);
2085 return error_mark_node;
2086 }
2087
2088 /* Count the number of template headers specified for this
2089 specialization. */
2090 header_count = 0;
2091 for (b = current_binding_level;
2092 b->kind == sk_template_parms;
2093 b = b->level_chain)
2094 ++header_count;
2095
2096 tree orig_fns = fns;
2097
2098 if (variable_template_p (fns))
2099 {
2100 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2101 targs = coerce_template_parms (parms, explicit_targs, fns,
2102 tf_warning_or_error,
2103 /*req_all*/true, /*use_defarg*/true);
2104 if (targs != error_mark_node)
2105 templates = tree_cons (targs, fns, templates);
2106 }
2107 else for (lkp_iterator iter (fns); iter; ++iter)
2108 {
2109 tree fn = *iter;
2110
2111 if (TREE_CODE (fn) == TEMPLATE_DECL)
2112 {
2113 tree decl_arg_types;
2114 tree fn_arg_types;
2115 tree insttype;
2116
2117 /* In case of explicit specialization, we need to check if
2118 the number of template headers appearing in the specialization
2119 is correct. This is usually done in check_explicit_specialization,
2120 but the check done there cannot be exhaustive when specializing
2121 member functions. Consider the following code:
2122
2123 template <> void A<int>::f(int);
2124 template <> template <> void A<int>::f(int);
2125
2126 Assuming that A<int> is not itself an explicit specialization
2127 already, the first line specializes "f" which is a non-template
2128 member function, whilst the second line specializes "f" which
2129 is a template member function. So both lines are syntactically
2130 correct, and check_explicit_specialization does not reject
2131 them.
2132
2133 Here, we can do better, as we are matching the specialization
2134 against the declarations. We count the number of template
2135 headers, and we check if they match TEMPLATE_COUNT + 1
2136 (TEMPLATE_COUNT is the number of qualifying template classes,
2137 plus there must be another header for the member template
2138 itself).
2139
2140 Notice that if header_count is zero, this is not a
2141 specialization but rather a template instantiation, so there
2142 is no check we can perform here. */
2143 if (header_count && header_count != template_count + 1)
2144 continue;
2145
2146 /* Check that the number of template arguments at the
2147 innermost level for DECL is the same as for FN. */
2148 if (current_binding_level->kind == sk_template_parms
2149 && !current_binding_level->explicit_spec_p
2150 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2151 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2152 (current_template_parms))))
2153 continue;
2154
2155 /* DECL might be a specialization of FN. */
2156 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2157 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2158
2159 /* For a non-static member function, we need to make sure
2160 that the const qualification is the same. Since
2161 get_bindings does not try to merge the "this" parameter,
2162 we must do the comparison explicitly. */
2163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2164 && !same_type_p (TREE_VALUE (fn_arg_types),
2165 TREE_VALUE (decl_arg_types)))
2166 continue;
2167
2168 /* Skip the "this" parameter and, for constructors of
2169 classes with virtual bases, the VTT parameter. A
2170 full specialization of a constructor will have a VTT
2171 parameter, but a template never will. */
2172 decl_arg_types
2173 = skip_artificial_parms_for (decl, decl_arg_types);
2174 fn_arg_types
2175 = skip_artificial_parms_for (fn, fn_arg_types);
2176
2177 /* Function templates cannot be specializations; there are
2178 no partial specializations of functions. Therefore, if
2179 the type of DECL does not match FN, there is no
2180 match.
2181
2182 Note that it should never be the case that we have both
2183 candidates added here, and for regular member functions
2184 below. */
2185 if (tsk == tsk_template)
2186 {
2187 if (compparms (fn_arg_types, decl_arg_types))
2188 candidates = tree_cons (NULL_TREE, fn, candidates);
2189 continue;
2190 }
2191
2192 /* See whether this function might be a specialization of this
2193 template. Suppress access control because we might be trying
2194 to make this specialization a friend, and we have already done
2195 access control for the declaration of the specialization. */
2196 push_deferring_access_checks (dk_no_check);
2197 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2198 pop_deferring_access_checks ();
2199
2200 if (!targs)
2201 /* We cannot deduce template arguments that when used to
2202 specialize TMPL will produce DECL. */
2203 continue;
2204
2205 /* Remove, from the set of candidates, all those functions
2206 whose constraints are not satisfied. */
2207 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2208 continue;
2209
2210 // Then, try to form the new function type.
2211 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2212 if (insttype == error_mark_node)
2213 continue;
2214 fn_arg_types
2215 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2216 if (!compparms (fn_arg_types, decl_arg_types))
2217 continue;
2218
2219 /* Save this template, and the arguments deduced. */
2220 templates = tree_cons (targs, fn, templates);
2221 }
2222 else if (need_member_template)
2223 /* FN is an ordinary member function, and we need a
2224 specialization of a member template. */
2225 ;
2226 else if (TREE_CODE (fn) != FUNCTION_DECL)
2227 /* We can get IDENTIFIER_NODEs here in certain erroneous
2228 cases. */
2229 ;
2230 else if (!DECL_FUNCTION_MEMBER_P (fn))
2231 /* This is just an ordinary non-member function. Nothing can
2232 be a specialization of that. */
2233 ;
2234 else if (DECL_ARTIFICIAL (fn))
2235 /* Cannot specialize functions that are created implicitly. */
2236 ;
2237 else
2238 {
2239 tree decl_arg_types;
2240
2241 /* This is an ordinary member function. However, since
2242 we're here, we can assume its enclosing class is a
2243 template class. For example,
2244
2245 template <typename T> struct S { void f(); };
2246 template <> void S<int>::f() {}
2247
2248 Here, S<int>::f is a non-template, but S<int> is a
2249 template class. If FN has the same type as DECL, we
2250 might be in business. */
2251
2252 if (!DECL_TEMPLATE_INFO (fn))
2253 /* Its enclosing class is an explicit specialization
2254 of a template class. This is not a candidate. */
2255 continue;
2256
2257 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2258 TREE_TYPE (TREE_TYPE (fn))))
2259 /* The return types differ. */
2260 continue;
2261
2262 /* Adjust the type of DECL in case FN is a static member. */
2263 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2264 if (DECL_STATIC_FUNCTION_P (fn)
2265 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2266 decl_arg_types = TREE_CHAIN (decl_arg_types);
2267
2268 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2269 decl_arg_types))
2270 continue;
2271
2272 // If the deduced arguments do not satisfy the constraints,
2273 // this is not a candidate.
2274 if (flag_concepts && !constraints_satisfied_p (fn))
2275 continue;
2276
2277 // Add the candidate.
2278 candidates = tree_cons (NULL_TREE, fn, candidates);
2279 }
2280 }
2281
2282 if (templates && TREE_CHAIN (templates))
2283 {
2284 /* We have:
2285
2286 [temp.expl.spec]
2287
2288 It is possible for a specialization with a given function
2289 signature to be instantiated from more than one function
2290 template. In such cases, explicit specification of the
2291 template arguments must be used to uniquely identify the
2292 function template specialization being specialized.
2293
2294 Note that here, there's no suggestion that we're supposed to
2295 determine which of the candidate templates is most
2296 specialized. However, we, also have:
2297
2298 [temp.func.order]
2299
2300 Partial ordering of overloaded function template
2301 declarations is used in the following contexts to select
2302 the function template to which a function template
2303 specialization refers:
2304
2305 -- when an explicit specialization refers to a function
2306 template.
2307
2308 So, we do use the partial ordering rules, at least for now.
2309 This extension can only serve to make invalid programs valid,
2310 so it's safe. And, there is strong anecdotal evidence that
2311 the committee intended the partial ordering rules to apply;
2312 the EDG front end has that behavior, and John Spicer claims
2313 that the committee simply forgot to delete the wording in
2314 [temp.expl.spec]. */
2315 tree tmpl = most_specialized_instantiation (templates);
2316 if (tmpl != error_mark_node)
2317 {
2318 templates = tmpl;
2319 TREE_CHAIN (templates) = NULL_TREE;
2320 }
2321 }
2322
2323 // Concepts allows multiple declarations of member functions
2324 // with the same signature. Like above, we need to rely on
2325 // on the partial ordering of those candidates to determine which
2326 // is the best.
2327 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2328 {
2329 if (tree cand = most_constrained_function (candidates))
2330 {
2331 candidates = cand;
2332 TREE_CHAIN (cand) = NULL_TREE;
2333 }
2334 }
2335
2336 if (templates == NULL_TREE && candidates == NULL_TREE)
2337 {
2338 error ("template-id %qD for %q+D does not match any template "
2339 "declaration", template_id, decl);
2340 if (header_count && header_count != template_count + 1)
2341 inform (input_location, "saw %d %<template<>%>, need %d for "
2342 "specializing a member function template",
2343 header_count, template_count + 1);
2344 else
2345 print_candidates (orig_fns);
2346 return error_mark_node;
2347 }
2348 else if ((templates && TREE_CHAIN (templates))
2349 || (candidates && TREE_CHAIN (candidates))
2350 || (templates && candidates))
2351 {
2352 error ("ambiguous template specialization %qD for %q+D",
2353 template_id, decl);
2354 candidates = chainon (candidates, templates);
2355 print_candidates (candidates);
2356 return error_mark_node;
2357 }
2358
2359 /* We have one, and exactly one, match. */
2360 if (candidates)
2361 {
2362 tree fn = TREE_VALUE (candidates);
2363 *targs_out = copy_node (DECL_TI_ARGS (fn));
2364
2365 // Propagate the candidate's constraints to the declaration.
2366 set_constraints (decl, get_constraints (fn));
2367
2368 /* DECL is a re-declaration or partial instantiation of a template
2369 function. */
2370 if (TREE_CODE (fn) == TEMPLATE_DECL)
2371 return fn;
2372 /* It was a specialization of an ordinary member function in a
2373 template class. */
2374 return DECL_TI_TEMPLATE (fn);
2375 }
2376
2377 /* It was a specialization of a template. */
2378 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2379 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2380 {
2381 *targs_out = copy_node (targs);
2382 SET_TMPL_ARGS_LEVEL (*targs_out,
2383 TMPL_ARGS_DEPTH (*targs_out),
2384 TREE_PURPOSE (templates));
2385 }
2386 else
2387 *targs_out = TREE_PURPOSE (templates);
2388 return TREE_VALUE (templates);
2389 }
2390
2391 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2392 but with the default argument values filled in from those in the
2393 TMPL_TYPES. */
2394
2395 static tree
2396 copy_default_args_to_explicit_spec_1 (tree spec_types,
2397 tree tmpl_types)
2398 {
2399 tree new_spec_types;
2400
2401 if (!spec_types)
2402 return NULL_TREE;
2403
2404 if (spec_types == void_list_node)
2405 return void_list_node;
2406
2407 /* Substitute into the rest of the list. */
2408 new_spec_types =
2409 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2410 TREE_CHAIN (tmpl_types));
2411
2412 /* Add the default argument for this parameter. */
2413 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2414 TREE_VALUE (spec_types),
2415 new_spec_types);
2416 }
2417
2418 /* DECL is an explicit specialization. Replicate default arguments
2419 from the template it specializes. (That way, code like:
2420
2421 template <class T> void f(T = 3);
2422 template <> void f(double);
2423 void g () { f (); }
2424
2425 works, as required.) An alternative approach would be to look up
2426 the correct default arguments at the call-site, but this approach
2427 is consistent with how implicit instantiations are handled. */
2428
2429 static void
2430 copy_default_args_to_explicit_spec (tree decl)
2431 {
2432 tree tmpl;
2433 tree spec_types;
2434 tree tmpl_types;
2435 tree new_spec_types;
2436 tree old_type;
2437 tree new_type;
2438 tree t;
2439 tree object_type = NULL_TREE;
2440 tree in_charge = NULL_TREE;
2441 tree vtt = NULL_TREE;
2442
2443 /* See if there's anything we need to do. */
2444 tmpl = DECL_TI_TEMPLATE (decl);
2445 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2446 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2447 if (TREE_PURPOSE (t))
2448 break;
2449 if (!t)
2450 return;
2451
2452 old_type = TREE_TYPE (decl);
2453 spec_types = TYPE_ARG_TYPES (old_type);
2454
2455 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2456 {
2457 /* Remove the this pointer, but remember the object's type for
2458 CV quals. */
2459 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2460 spec_types = TREE_CHAIN (spec_types);
2461 tmpl_types = TREE_CHAIN (tmpl_types);
2462
2463 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2464 {
2465 /* DECL may contain more parameters than TMPL due to the extra
2466 in-charge parameter in constructors and destructors. */
2467 in_charge = spec_types;
2468 spec_types = TREE_CHAIN (spec_types);
2469 }
2470 if (DECL_HAS_VTT_PARM_P (decl))
2471 {
2472 vtt = spec_types;
2473 spec_types = TREE_CHAIN (spec_types);
2474 }
2475 }
2476
2477 /* Compute the merged default arguments. */
2478 new_spec_types =
2479 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2480
2481 /* Compute the new FUNCTION_TYPE. */
2482 if (object_type)
2483 {
2484 if (vtt)
2485 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2486 TREE_VALUE (vtt),
2487 new_spec_types);
2488
2489 if (in_charge)
2490 /* Put the in-charge parameter back. */
2491 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2492 TREE_VALUE (in_charge),
2493 new_spec_types);
2494
2495 new_type = build_method_type_directly (object_type,
2496 TREE_TYPE (old_type),
2497 new_spec_types);
2498 }
2499 else
2500 new_type = build_function_type (TREE_TYPE (old_type),
2501 new_spec_types);
2502 new_type = cp_build_type_attribute_variant (new_type,
2503 TYPE_ATTRIBUTES (old_type));
2504 new_type = build_exception_variant (new_type,
2505 TYPE_RAISES_EXCEPTIONS (old_type));
2506
2507 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2508 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2509
2510 TREE_TYPE (decl) = new_type;
2511 }
2512
2513 /* Return the number of template headers we expect to see for a definition
2514 or specialization of CTYPE or one of its non-template members. */
2515
2516 int
2517 num_template_headers_for_class (tree ctype)
2518 {
2519 int num_templates = 0;
2520
2521 while (ctype && CLASS_TYPE_P (ctype))
2522 {
2523 /* You're supposed to have one `template <...>' for every
2524 template class, but you don't need one for a full
2525 specialization. For example:
2526
2527 template <class T> struct S{};
2528 template <> struct S<int> { void f(); };
2529 void S<int>::f () {}
2530
2531 is correct; there shouldn't be a `template <>' for the
2532 definition of `S<int>::f'. */
2533 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2534 /* If CTYPE does not have template information of any
2535 kind, then it is not a template, nor is it nested
2536 within a template. */
2537 break;
2538 if (explicit_class_specialization_p (ctype))
2539 break;
2540 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2541 ++num_templates;
2542
2543 ctype = TYPE_CONTEXT (ctype);
2544 }
2545
2546 return num_templates;
2547 }
2548
2549 /* Do a simple sanity check on the template headers that precede the
2550 variable declaration DECL. */
2551
2552 void
2553 check_template_variable (tree decl)
2554 {
2555 tree ctx = CP_DECL_CONTEXT (decl);
2556 int wanted = num_template_headers_for_class (ctx);
2557 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2558 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2559 {
2560 if (cxx_dialect < cxx14)
2561 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2562 "variable templates only available with "
2563 "-std=c++14 or -std=gnu++14");
2564
2565 // Namespace-scope variable templates should have a template header.
2566 ++wanted;
2567 }
2568 if (template_header_count > wanted)
2569 {
2570 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2571 "too many template headers for %qD "
2572 "(should be %d)",
2573 decl, wanted);
2574 if (warned && CLASS_TYPE_P (ctx)
2575 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2576 inform (DECL_SOURCE_LOCATION (decl),
2577 "members of an explicitly specialized class are defined "
2578 "without a template header");
2579 }
2580 }
2581
2582 /* An explicit specialization whose declarator-id or class-head-name is not
2583 qualified shall be declared in the nearest enclosing namespace of the
2584 template, or, if the namespace is inline (7.3.1), any namespace from its
2585 enclosing namespace set.
2586
2587 If the name declared in the explicit instantiation is an unqualified name,
2588 the explicit instantiation shall appear in the namespace where its template
2589 is declared or, if that namespace is inline (7.3.1), any namespace from its
2590 enclosing namespace set. */
2591
2592 void
2593 check_unqualified_spec_or_inst (tree t, location_t loc)
2594 {
2595 tree tmpl = most_general_template (t);
2596 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2597 && !is_nested_namespace (current_namespace,
2598 CP_DECL_CONTEXT (tmpl), true))
2599 {
2600 if (processing_specialization)
2601 permerror (loc, "explicit specialization of %qD outside its "
2602 "namespace must use a nested-name-specifier", tmpl);
2603 else if (processing_explicit_instantiation
2604 && cxx_dialect >= cxx11)
2605 /* This was allowed in C++98, so only pedwarn. */
2606 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2607 "outside its namespace must use a nested-name-"
2608 "specifier", tmpl);
2609 }
2610 }
2611
2612 /* Check to see if the function just declared, as indicated in
2613 DECLARATOR, and in DECL, is a specialization of a function
2614 template. We may also discover that the declaration is an explicit
2615 instantiation at this point.
2616
2617 Returns DECL, or an equivalent declaration that should be used
2618 instead if all goes well. Issues an error message if something is
2619 amiss. Returns error_mark_node if the error is not easily
2620 recoverable.
2621
2622 FLAGS is a bitmask consisting of the following flags:
2623
2624 2: The function has a definition.
2625 4: The function is a friend.
2626
2627 The TEMPLATE_COUNT is the number of references to qualifying
2628 template classes that appeared in the name of the function. For
2629 example, in
2630
2631 template <class T> struct S { void f(); };
2632 void S<int>::f();
2633
2634 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2635 classes are not counted in the TEMPLATE_COUNT, so that in
2636
2637 template <class T> struct S {};
2638 template <> struct S<int> { void f(); }
2639 template <> void S<int>::f();
2640
2641 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2642 invalid; there should be no template <>.)
2643
2644 If the function is a specialization, it is marked as such via
2645 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2646 is set up correctly, and it is added to the list of specializations
2647 for that template. */
2648
2649 tree
2650 check_explicit_specialization (tree declarator,
2651 tree decl,
2652 int template_count,
2653 int flags)
2654 {
2655 int have_def = flags & 2;
2656 int is_friend = flags & 4;
2657 bool is_concept = flags & 8;
2658 int specialization = 0;
2659 int explicit_instantiation = 0;
2660 int member_specialization = 0;
2661 tree ctype = DECL_CLASS_CONTEXT (decl);
2662 tree dname = DECL_NAME (decl);
2663 tmpl_spec_kind tsk;
2664
2665 if (is_friend)
2666 {
2667 if (!processing_specialization)
2668 tsk = tsk_none;
2669 else
2670 tsk = tsk_excessive_parms;
2671 }
2672 else
2673 tsk = current_tmpl_spec_kind (template_count);
2674
2675 switch (tsk)
2676 {
2677 case tsk_none:
2678 if (processing_specialization && !VAR_P (decl))
2679 {
2680 specialization = 1;
2681 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2682 }
2683 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2684 {
2685 if (is_friend)
2686 /* This could be something like:
2687
2688 template <class T> void f(T);
2689 class S { friend void f<>(int); } */
2690 specialization = 1;
2691 else
2692 {
2693 /* This case handles bogus declarations like template <>
2694 template <class T> void f<int>(); */
2695
2696 error ("template-id %qD in declaration of primary template",
2697 declarator);
2698 return decl;
2699 }
2700 }
2701 break;
2702
2703 case tsk_invalid_member_spec:
2704 /* The error has already been reported in
2705 check_specialization_scope. */
2706 return error_mark_node;
2707
2708 case tsk_invalid_expl_inst:
2709 error ("template parameter list used in explicit instantiation");
2710
2711 /* Fall through. */
2712
2713 case tsk_expl_inst:
2714 if (have_def)
2715 error ("definition provided for explicit instantiation");
2716
2717 explicit_instantiation = 1;
2718 break;
2719
2720 case tsk_excessive_parms:
2721 case tsk_insufficient_parms:
2722 if (tsk == tsk_excessive_parms)
2723 error ("too many template parameter lists in declaration of %qD",
2724 decl);
2725 else if (template_header_count)
2726 error("too few template parameter lists in declaration of %qD", decl);
2727 else
2728 error("explicit specialization of %qD must be introduced by "
2729 "%<template <>%>", decl);
2730
2731 /* Fall through. */
2732 case tsk_expl_spec:
2733 if (is_concept)
2734 error ("explicit specialization declared %<concept%>");
2735
2736 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2737 /* In cases like template<> constexpr bool v = true;
2738 We'll give an error in check_template_variable. */
2739 break;
2740
2741 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2742 if (ctype)
2743 member_specialization = 1;
2744 else
2745 specialization = 1;
2746 break;
2747
2748 case tsk_template:
2749 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2750 {
2751 /* This case handles bogus declarations like template <>
2752 template <class T> void f<int>(); */
2753
2754 if (!uses_template_parms (declarator))
2755 error ("template-id %qD in declaration of primary template",
2756 declarator);
2757 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2758 {
2759 /* Partial specialization of variable template. */
2760 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2761 specialization = 1;
2762 goto ok;
2763 }
2764 else if (cxx_dialect < cxx14)
2765 error ("non-type partial specialization %qD "
2766 "is not allowed", declarator);
2767 else
2768 error ("non-class, non-variable partial specialization %qD "
2769 "is not allowed", declarator);
2770 return decl;
2771 ok:;
2772 }
2773
2774 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2775 /* This is a specialization of a member template, without
2776 specialization the containing class. Something like:
2777
2778 template <class T> struct S {
2779 template <class U> void f (U);
2780 };
2781 template <> template <class U> void S<int>::f(U) {}
2782
2783 That's a specialization -- but of the entire template. */
2784 specialization = 1;
2785 break;
2786
2787 default:
2788 gcc_unreachable ();
2789 }
2790
2791 if ((specialization || member_specialization)
2792 /* This doesn't apply to variable templates. */
2793 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2794 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2795 {
2796 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2797 for (; t; t = TREE_CHAIN (t))
2798 if (TREE_PURPOSE (t))
2799 {
2800 permerror (input_location,
2801 "default argument specified in explicit specialization");
2802 break;
2803 }
2804 }
2805
2806 if (specialization || member_specialization || explicit_instantiation)
2807 {
2808 tree tmpl = NULL_TREE;
2809 tree targs = NULL_TREE;
2810 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2811
2812 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2813 if (!was_template_id)
2814 {
2815 tree fns;
2816
2817 gcc_assert (identifier_p (declarator));
2818 if (ctype)
2819 fns = dname;
2820 else
2821 {
2822 /* If there is no class context, the explicit instantiation
2823 must be at namespace scope. */
2824 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2825
2826 /* Find the namespace binding, using the declaration
2827 context. */
2828 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2829 false, true);
2830 if (fns == error_mark_node)
2831 /* If lookup fails, look for a friend declaration so we can
2832 give a better diagnostic. */
2833 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2834 /*type*/false, /*complain*/true,
2835 /*hidden*/true);
2836
2837 if (fns == error_mark_node || !is_overloaded_fn (fns))
2838 {
2839 error ("%qD is not a template function", dname);
2840 fns = error_mark_node;
2841 }
2842 }
2843
2844 declarator = lookup_template_function (fns, NULL_TREE);
2845 }
2846
2847 if (declarator == error_mark_node)
2848 return error_mark_node;
2849
2850 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2851 {
2852 if (!explicit_instantiation)
2853 /* A specialization in class scope. This is invalid,
2854 but the error will already have been flagged by
2855 check_specialization_scope. */
2856 return error_mark_node;
2857 else
2858 {
2859 /* It's not valid to write an explicit instantiation in
2860 class scope, e.g.:
2861
2862 class C { template void f(); }
2863
2864 This case is caught by the parser. However, on
2865 something like:
2866
2867 template class C { void f(); };
2868
2869 (which is invalid) we can get here. The error will be
2870 issued later. */
2871 ;
2872 }
2873
2874 return decl;
2875 }
2876 else if (ctype != NULL_TREE
2877 && (identifier_p (TREE_OPERAND (declarator, 0))))
2878 {
2879 // We'll match variable templates in start_decl.
2880 if (VAR_P (decl))
2881 return decl;
2882
2883 /* Find the list of functions in ctype that have the same
2884 name as the declared function. */
2885 tree name = TREE_OPERAND (declarator, 0);
2886
2887 if (constructor_name_p (name, ctype))
2888 {
2889 if (DECL_CONSTRUCTOR_P (decl)
2890 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2891 : !CLASSTYPE_DESTRUCTOR (ctype))
2892 {
2893 /* From [temp.expl.spec]:
2894
2895 If such an explicit specialization for the member
2896 of a class template names an implicitly-declared
2897 special member function (clause _special_), the
2898 program is ill-formed.
2899
2900 Similar language is found in [temp.explicit]. */
2901 error ("specialization of implicitly-declared special member function");
2902 return error_mark_node;
2903 }
2904
2905 name = DECL_NAME (decl);
2906 }
2907
2908 /* For a type-conversion operator, We might be looking for
2909 `operator int' which will be a specialization of
2910 `operator T'. Grab all the conversion operators, and
2911 then select from them. */
2912 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2913 ? conv_op_identifier : name);
2914
2915 if (fns == NULL_TREE)
2916 {
2917 error ("no member function %qD declared in %qT", name, ctype);
2918 return error_mark_node;
2919 }
2920 else
2921 TREE_OPERAND (declarator, 0) = fns;
2922 }
2923
2924 /* Figure out what exactly is being specialized at this point.
2925 Note that for an explicit instantiation, even one for a
2926 member function, we cannot tell a priori whether the
2927 instantiation is for a member template, or just a member
2928 function of a template class. Even if a member template is
2929 being instantiated, the member template arguments may be
2930 elided if they can be deduced from the rest of the
2931 declaration. */
2932 tmpl = determine_specialization (declarator, decl,
2933 &targs,
2934 member_specialization,
2935 template_count,
2936 tsk);
2937
2938 if (!tmpl || tmpl == error_mark_node)
2939 /* We couldn't figure out what this declaration was
2940 specializing. */
2941 return error_mark_node;
2942 else
2943 {
2944 if (TREE_CODE (decl) == FUNCTION_DECL
2945 && DECL_HIDDEN_FRIEND_P (tmpl))
2946 {
2947 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2948 "friend declaration %qD is not visible to "
2949 "explicit specialization", tmpl))
2950 inform (DECL_SOURCE_LOCATION (tmpl),
2951 "friend declaration here");
2952 }
2953 else if (!ctype && !is_friend
2954 && CP_DECL_CONTEXT (decl) == current_namespace)
2955 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2956
2957 tree gen_tmpl = most_general_template (tmpl);
2958
2959 if (explicit_instantiation)
2960 {
2961 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2962 is done by do_decl_instantiation later. */
2963
2964 int arg_depth = TMPL_ARGS_DEPTH (targs);
2965 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2966
2967 if (arg_depth > parm_depth)
2968 {
2969 /* If TMPL is not the most general template (for
2970 example, if TMPL is a friend template that is
2971 injected into namespace scope), then there will
2972 be too many levels of TARGS. Remove some of them
2973 here. */
2974 int i;
2975 tree new_targs;
2976
2977 new_targs = make_tree_vec (parm_depth);
2978 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2979 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2980 = TREE_VEC_ELT (targs, i);
2981 targs = new_targs;
2982 }
2983
2984 return instantiate_template (tmpl, targs, tf_error);
2985 }
2986
2987 /* If we thought that the DECL was a member function, but it
2988 turns out to be specializing a static member function,
2989 make DECL a static member function as well. */
2990 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2991 && DECL_STATIC_FUNCTION_P (tmpl)
2992 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2993 revert_static_member_fn (decl);
2994
2995 /* If this is a specialization of a member template of a
2996 template class, we want to return the TEMPLATE_DECL, not
2997 the specialization of it. */
2998 if (tsk == tsk_template && !was_template_id)
2999 {
3000 tree result = DECL_TEMPLATE_RESULT (tmpl);
3001 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3002 DECL_INITIAL (result) = NULL_TREE;
3003 if (have_def)
3004 {
3005 tree parm;
3006 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3007 DECL_SOURCE_LOCATION (result)
3008 = DECL_SOURCE_LOCATION (decl);
3009 /* We want to use the argument list specified in the
3010 definition, not in the original declaration. */
3011 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3012 for (parm = DECL_ARGUMENTS (result); parm;
3013 parm = DECL_CHAIN (parm))
3014 DECL_CONTEXT (parm) = result;
3015 }
3016 return register_specialization (tmpl, gen_tmpl, targs,
3017 is_friend, 0);
3018 }
3019
3020 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3021 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3022
3023 if (was_template_id)
3024 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3025
3026 /* Inherit default function arguments from the template
3027 DECL is specializing. */
3028 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3029 copy_default_args_to_explicit_spec (decl);
3030
3031 /* This specialization has the same protection as the
3032 template it specializes. */
3033 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3034 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3035
3036 /* 7.1.1-1 [dcl.stc]
3037
3038 A storage-class-specifier shall not be specified in an
3039 explicit specialization...
3040
3041 The parser rejects these, so unless action is taken here,
3042 explicit function specializations will always appear with
3043 global linkage.
3044
3045 The action recommended by the C++ CWG in response to C++
3046 defect report 605 is to make the storage class and linkage
3047 of the explicit specialization match the templated function:
3048
3049 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3050 */
3051 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3052 {
3053 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3054 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3055
3056 /* A concept cannot be specialized. */
3057 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3058 {
3059 error ("explicit specialization of function concept %qD",
3060 gen_tmpl);
3061 return error_mark_node;
3062 }
3063
3064 /* This specialization has the same linkage and visibility as
3065 the function template it specializes. */
3066 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3067 if (! TREE_PUBLIC (decl))
3068 {
3069 DECL_INTERFACE_KNOWN (decl) = 1;
3070 DECL_NOT_REALLY_EXTERN (decl) = 1;
3071 }
3072 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3073 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3074 {
3075 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3076 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3077 }
3078 }
3079
3080 /* If DECL is a friend declaration, declared using an
3081 unqualified name, the namespace associated with DECL may
3082 have been set incorrectly. For example, in:
3083
3084 template <typename T> void f(T);
3085 namespace N {
3086 struct S { friend void f<int>(int); }
3087 }
3088
3089 we will have set the DECL_CONTEXT for the friend
3090 declaration to N, rather than to the global namespace. */
3091 if (DECL_NAMESPACE_SCOPE_P (decl))
3092 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3093
3094 if (is_friend && !have_def)
3095 /* This is not really a declaration of a specialization.
3096 It's just the name of an instantiation. But, it's not
3097 a request for an instantiation, either. */
3098 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3099 else if (TREE_CODE (decl) == FUNCTION_DECL)
3100 /* A specialization is not necessarily COMDAT. */
3101 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3102 && DECL_DECLARED_INLINE_P (decl));
3103 else if (VAR_P (decl))
3104 DECL_COMDAT (decl) = false;
3105
3106 /* If this is a full specialization, register it so that we can find
3107 it again. Partial specializations will be registered in
3108 process_partial_specialization. */
3109 if (!processing_template_decl)
3110 decl = register_specialization (decl, gen_tmpl, targs,
3111 is_friend, 0);
3112
3113 /* A 'structor should already have clones. */
3114 gcc_assert (decl == error_mark_node
3115 || variable_template_p (tmpl)
3116 || !(DECL_CONSTRUCTOR_P (decl)
3117 || DECL_DESTRUCTOR_P (decl))
3118 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3119 }
3120 }
3121
3122 return decl;
3123 }
3124
3125 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3126 parameters. These are represented in the same format used for
3127 DECL_TEMPLATE_PARMS. */
3128
3129 int
3130 comp_template_parms (const_tree parms1, const_tree parms2)
3131 {
3132 const_tree p1;
3133 const_tree p2;
3134
3135 if (parms1 == parms2)
3136 return 1;
3137
3138 for (p1 = parms1, p2 = parms2;
3139 p1 != NULL_TREE && p2 != NULL_TREE;
3140 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3141 {
3142 tree t1 = TREE_VALUE (p1);
3143 tree t2 = TREE_VALUE (p2);
3144 int i;
3145
3146 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3147 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3148
3149 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3150 return 0;
3151
3152 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3153 {
3154 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3155 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3156
3157 /* If either of the template parameters are invalid, assume
3158 they match for the sake of error recovery. */
3159 if (error_operand_p (parm1) || error_operand_p (parm2))
3160 return 1;
3161
3162 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3163 return 0;
3164
3165 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3166 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3167 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3168 continue;
3169 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3170 return 0;
3171 }
3172 }
3173
3174 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3175 /* One set of parameters has more parameters lists than the
3176 other. */
3177 return 0;
3178
3179 return 1;
3180 }
3181
3182 /* Determine whether PARM is a parameter pack. */
3183
3184 bool
3185 template_parameter_pack_p (const_tree parm)
3186 {
3187 /* Determine if we have a non-type template parameter pack. */
3188 if (TREE_CODE (parm) == PARM_DECL)
3189 return (DECL_TEMPLATE_PARM_P (parm)
3190 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3191 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3192 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3193
3194 /* If this is a list of template parameters, we could get a
3195 TYPE_DECL or a TEMPLATE_DECL. */
3196 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3197 parm = TREE_TYPE (parm);
3198
3199 /* Otherwise it must be a type template parameter. */
3200 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3201 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3202 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3203 }
3204
3205 /* Determine if T is a function parameter pack. */
3206
3207 bool
3208 function_parameter_pack_p (const_tree t)
3209 {
3210 if (t && TREE_CODE (t) == PARM_DECL)
3211 return DECL_PACK_P (t);
3212 return false;
3213 }
3214
3215 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3216 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3217
3218 tree
3219 get_function_template_decl (const_tree primary_func_tmpl_inst)
3220 {
3221 if (! primary_func_tmpl_inst
3222 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3223 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3224 return NULL;
3225
3226 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3227 }
3228
3229 /* Return true iff the function parameter PARAM_DECL was expanded
3230 from the function parameter pack PACK. */
3231
3232 bool
3233 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3234 {
3235 if (DECL_ARTIFICIAL (param_decl)
3236 || !function_parameter_pack_p (pack))
3237 return false;
3238
3239 /* The parameter pack and its pack arguments have the same
3240 DECL_PARM_INDEX. */
3241 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3242 }
3243
3244 /* Determine whether ARGS describes a variadic template args list,
3245 i.e., one that is terminated by a template argument pack. */
3246
3247 static bool
3248 template_args_variadic_p (tree args)
3249 {
3250 int nargs;
3251 tree last_parm;
3252
3253 if (args == NULL_TREE)
3254 return false;
3255
3256 args = INNERMOST_TEMPLATE_ARGS (args);
3257 nargs = TREE_VEC_LENGTH (args);
3258
3259 if (nargs == 0)
3260 return false;
3261
3262 last_parm = TREE_VEC_ELT (args, nargs - 1);
3263
3264 return ARGUMENT_PACK_P (last_parm);
3265 }
3266
3267 /* Generate a new name for the parameter pack name NAME (an
3268 IDENTIFIER_NODE) that incorporates its */
3269
3270 static tree
3271 make_ith_pack_parameter_name (tree name, int i)
3272 {
3273 /* Munge the name to include the parameter index. */
3274 #define NUMBUF_LEN 128
3275 char numbuf[NUMBUF_LEN];
3276 char* newname;
3277 int newname_len;
3278
3279 if (name == NULL_TREE)
3280 return name;
3281 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3282 newname_len = IDENTIFIER_LENGTH (name)
3283 + strlen (numbuf) + 2;
3284 newname = (char*)alloca (newname_len);
3285 snprintf (newname, newname_len,
3286 "%s#%i", IDENTIFIER_POINTER (name), i);
3287 return get_identifier (newname);
3288 }
3289
3290 /* Return true if T is a primary function, class or alias template
3291 specialization, not including the template pattern. */
3292
3293 bool
3294 primary_template_specialization_p (const_tree t)
3295 {
3296 if (!t)
3297 return false;
3298
3299 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3300 return (DECL_LANG_SPECIFIC (t)
3301 && DECL_USE_TEMPLATE (t)
3302 && DECL_TEMPLATE_INFO (t)
3303 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3304 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3305 return (CLASSTYPE_TEMPLATE_INFO (t)
3306 && CLASSTYPE_USE_TEMPLATE (t)
3307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3308 else if (alias_template_specialization_p (t))
3309 return true;
3310 return false;
3311 }
3312
3313 /* Return true if PARM is a template template parameter. */
3314
3315 bool
3316 template_template_parameter_p (const_tree parm)
3317 {
3318 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3319 }
3320
3321 /* Return true iff PARM is a DECL representing a type template
3322 parameter. */
3323
3324 bool
3325 template_type_parameter_p (const_tree parm)
3326 {
3327 return (parm
3328 && (TREE_CODE (parm) == TYPE_DECL
3329 || TREE_CODE (parm) == TEMPLATE_DECL)
3330 && DECL_TEMPLATE_PARM_P (parm));
3331 }
3332
3333 /* Return the template parameters of T if T is a
3334 primary template instantiation, NULL otherwise. */
3335
3336 tree
3337 get_primary_template_innermost_parameters (const_tree t)
3338 {
3339 tree parms = NULL, template_info = NULL;
3340
3341 if ((template_info = get_template_info (t))
3342 && primary_template_specialization_p (t))
3343 parms = INNERMOST_TEMPLATE_PARMS
3344 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3345
3346 return parms;
3347 }
3348
3349 /* Return the template parameters of the LEVELth level from the full list
3350 of template parameters PARMS. */
3351
3352 tree
3353 get_template_parms_at_level (tree parms, int level)
3354 {
3355 tree p;
3356 if (!parms
3357 || TREE_CODE (parms) != TREE_LIST
3358 || level > TMPL_PARMS_DEPTH (parms))
3359 return NULL_TREE;
3360
3361 for (p = parms; p; p = TREE_CHAIN (p))
3362 if (TMPL_PARMS_DEPTH (p) == level)
3363 return p;
3364
3365 return NULL_TREE;
3366 }
3367
3368 /* Returns the template arguments of T if T is a template instantiation,
3369 NULL otherwise. */
3370
3371 tree
3372 get_template_innermost_arguments (const_tree t)
3373 {
3374 tree args = NULL, template_info = NULL;
3375
3376 if ((template_info = get_template_info (t))
3377 && TI_ARGS (template_info))
3378 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3379
3380 return args;
3381 }
3382
3383 /* Return the argument pack elements of T if T is a template argument pack,
3384 NULL otherwise. */
3385
3386 tree
3387 get_template_argument_pack_elems (const_tree t)
3388 {
3389 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3390 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3391 return NULL;
3392
3393 return ARGUMENT_PACK_ARGS (t);
3394 }
3395
3396 /* True iff FN is a function representing a built-in variadic parameter
3397 pack. */
3398
3399 bool
3400 builtin_pack_fn_p (tree fn)
3401 {
3402 if (!fn
3403 || TREE_CODE (fn) != FUNCTION_DECL
3404 || !DECL_IS_BUILTIN (fn))
3405 return false;
3406
3407 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3408 return true;
3409
3410 return false;
3411 }
3412
3413 /* True iff CALL is a call to a function representing a built-in variadic
3414 parameter pack. */
3415
3416 static bool
3417 builtin_pack_call_p (tree call)
3418 {
3419 if (TREE_CODE (call) != CALL_EXPR)
3420 return false;
3421 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3422 }
3423
3424 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3425
3426 static tree
3427 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3428 tree in_decl)
3429 {
3430 tree ohi = CALL_EXPR_ARG (call, 0);
3431 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3432 false/*fn*/, true/*int_cst*/);
3433
3434 if (value_dependent_expression_p (hi))
3435 {
3436 if (hi != ohi)
3437 {
3438 call = copy_node (call);
3439 CALL_EXPR_ARG (call, 0) = hi;
3440 }
3441 tree ex = make_pack_expansion (call, complain);
3442 tree vec = make_tree_vec (1);
3443 TREE_VEC_ELT (vec, 0) = ex;
3444 return vec;
3445 }
3446 else
3447 {
3448 hi = cxx_constant_value (hi);
3449 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3450
3451 /* Calculate the largest value of len that won't make the size of the vec
3452 overflow an int. The compiler will exceed resource limits long before
3453 this, but it seems a decent place to diagnose. */
3454 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3455
3456 if (len < 0 || len > max)
3457 {
3458 if ((complain & tf_error)
3459 && hi != error_mark_node)
3460 error ("argument to __integer_pack must be between 0 and %d", max);
3461 return error_mark_node;
3462 }
3463
3464 tree vec = make_tree_vec (len);
3465
3466 for (int i = 0; i < len; ++i)
3467 TREE_VEC_ELT (vec, i) = size_int (i);
3468
3469 return vec;
3470 }
3471 }
3472
3473 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3474 CALL. */
3475
3476 static tree
3477 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3478 tree in_decl)
3479 {
3480 if (!builtin_pack_call_p (call))
3481 return NULL_TREE;
3482
3483 tree fn = CALL_EXPR_FN (call);
3484
3485 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3486 return expand_integer_pack (call, args, complain, in_decl);
3487
3488 return NULL_TREE;
3489 }
3490
3491 /* Structure used to track the progress of find_parameter_packs_r. */
3492 struct find_parameter_pack_data
3493 {
3494 /* TREE_LIST that will contain all of the parameter packs found by
3495 the traversal. */
3496 tree* parameter_packs;
3497
3498 /* Set of AST nodes that have been visited by the traversal. */
3499 hash_set<tree> *visited;
3500
3501 /* True iff we're making a type pack expansion. */
3502 bool type_pack_expansion_p;
3503 };
3504
3505 /* Identifies all of the argument packs that occur in a template
3506 argument and appends them to the TREE_LIST inside DATA, which is a
3507 find_parameter_pack_data structure. This is a subroutine of
3508 make_pack_expansion and uses_parameter_packs. */
3509 static tree
3510 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3511 {
3512 tree t = *tp;
3513 struct find_parameter_pack_data* ppd =
3514 (struct find_parameter_pack_data*)data;
3515 bool parameter_pack_p = false;
3516
3517 /* Handle type aliases/typedefs. */
3518 if (TYPE_ALIAS_P (t))
3519 {
3520 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3521 cp_walk_tree (&TI_ARGS (tinfo),
3522 &find_parameter_packs_r,
3523 ppd, ppd->visited);
3524 *walk_subtrees = 0;
3525 return NULL_TREE;
3526 }
3527
3528 /* Identify whether this is a parameter pack or not. */
3529 switch (TREE_CODE (t))
3530 {
3531 case TEMPLATE_PARM_INDEX:
3532 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3533 parameter_pack_p = true;
3534 break;
3535
3536 case TEMPLATE_TYPE_PARM:
3537 t = TYPE_MAIN_VARIANT (t);
3538 /* FALLTHRU */
3539 case TEMPLATE_TEMPLATE_PARM:
3540 /* If the placeholder appears in the decl-specifier-seq of a function
3541 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3542 is a pack expansion, the invented template parameter is a template
3543 parameter pack. */
3544 if (ppd->type_pack_expansion_p && is_auto (t))
3545 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3546 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3547 parameter_pack_p = true;
3548 break;
3549
3550 case FIELD_DECL:
3551 case PARM_DECL:
3552 if (DECL_PACK_P (t))
3553 {
3554 /* We don't want to walk into the type of a PARM_DECL,
3555 because we don't want to see the type parameter pack. */
3556 *walk_subtrees = 0;
3557 parameter_pack_p = true;
3558 }
3559 break;
3560
3561 /* Look through a lambda capture proxy to the field pack. */
3562 case VAR_DECL:
3563 if (DECL_HAS_VALUE_EXPR_P (t))
3564 {
3565 tree v = DECL_VALUE_EXPR (t);
3566 cp_walk_tree (&v,
3567 &find_parameter_packs_r,
3568 ppd, ppd->visited);
3569 *walk_subtrees = 0;
3570 }
3571 else if (variable_template_specialization_p (t))
3572 {
3573 cp_walk_tree (&DECL_TI_ARGS (t),
3574 find_parameter_packs_r,
3575 ppd, ppd->visited);
3576 *walk_subtrees = 0;
3577 }
3578 break;
3579
3580 case CALL_EXPR:
3581 if (builtin_pack_call_p (t))
3582 parameter_pack_p = true;
3583 break;
3584
3585 case BASES:
3586 parameter_pack_p = true;
3587 break;
3588 default:
3589 /* Not a parameter pack. */
3590 break;
3591 }
3592
3593 if (parameter_pack_p)
3594 {
3595 /* Add this parameter pack to the list. */
3596 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3597 }
3598
3599 if (TYPE_P (t))
3600 cp_walk_tree (&TYPE_CONTEXT (t),
3601 &find_parameter_packs_r, ppd, ppd->visited);
3602
3603 /* This switch statement will return immediately if we don't find a
3604 parameter pack. */
3605 switch (TREE_CODE (t))
3606 {
3607 case TEMPLATE_PARM_INDEX:
3608 return NULL_TREE;
3609
3610 case BOUND_TEMPLATE_TEMPLATE_PARM:
3611 /* Check the template itself. */
3612 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3613 &find_parameter_packs_r, ppd, ppd->visited);
3614 /* Check the template arguments. */
3615 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3616 ppd->visited);
3617 *walk_subtrees = 0;
3618 return NULL_TREE;
3619
3620 case TEMPLATE_TYPE_PARM:
3621 case TEMPLATE_TEMPLATE_PARM:
3622 return NULL_TREE;
3623
3624 case PARM_DECL:
3625 return NULL_TREE;
3626
3627 case DECL_EXPR:
3628 /* Ignore the declaration of a capture proxy for a parameter pack. */
3629 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3630 *walk_subtrees = 0;
3631 return NULL_TREE;
3632
3633 case RECORD_TYPE:
3634 if (TYPE_PTRMEMFUNC_P (t))
3635 return NULL_TREE;
3636 /* Fall through. */
3637
3638 case UNION_TYPE:
3639 case ENUMERAL_TYPE:
3640 if (TYPE_TEMPLATE_INFO (t))
3641 cp_walk_tree (&TYPE_TI_ARGS (t),
3642 &find_parameter_packs_r, ppd, ppd->visited);
3643
3644 *walk_subtrees = 0;
3645 return NULL_TREE;
3646
3647 case TEMPLATE_DECL:
3648 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3649 return NULL_TREE;
3650 gcc_fallthrough();
3651
3652 case CONSTRUCTOR:
3653 cp_walk_tree (&TREE_TYPE (t),
3654 &find_parameter_packs_r, ppd, ppd->visited);
3655 return NULL_TREE;
3656
3657 case TYPENAME_TYPE:
3658 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3659 ppd, ppd->visited);
3660 *walk_subtrees = 0;
3661 return NULL_TREE;
3662
3663 case TYPE_PACK_EXPANSION:
3664 case EXPR_PACK_EXPANSION:
3665 *walk_subtrees = 0;
3666 return NULL_TREE;
3667
3668 case INTEGER_TYPE:
3669 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3670 ppd, ppd->visited);
3671 *walk_subtrees = 0;
3672 return NULL_TREE;
3673
3674 case IDENTIFIER_NODE:
3675 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3676 ppd->visited);
3677 *walk_subtrees = 0;
3678 return NULL_TREE;
3679
3680 case LAMBDA_EXPR:
3681 {
3682 tree fn = lambda_function (t);
3683 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3684 ppd->visited);
3685 *walk_subtrees = 0;
3686 return NULL_TREE;
3687 }
3688
3689 case DECLTYPE_TYPE:
3690 {
3691 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3692 type_pack_expansion_p to false so that any placeholders
3693 within the expression don't get marked as parameter packs. */
3694 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3695 ppd->type_pack_expansion_p = false;
3696 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3697 ppd, ppd->visited);
3698 ppd->type_pack_expansion_p = type_pack_expansion_p;
3699 *walk_subtrees = 0;
3700 return NULL_TREE;
3701 }
3702
3703 default:
3704 return NULL_TREE;
3705 }
3706
3707 return NULL_TREE;
3708 }
3709
3710 /* Determines if the expression or type T uses any parameter packs. */
3711 bool
3712 uses_parameter_packs (tree t)
3713 {
3714 tree parameter_packs = NULL_TREE;
3715 struct find_parameter_pack_data ppd;
3716 ppd.parameter_packs = &parameter_packs;
3717 ppd.visited = new hash_set<tree>;
3718 ppd.type_pack_expansion_p = false;
3719 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3720 delete ppd.visited;
3721 return parameter_packs != NULL_TREE;
3722 }
3723
3724 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3725 representation a base-class initializer into a parameter pack
3726 expansion. If all goes well, the resulting node will be an
3727 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3728 respectively. */
3729 tree
3730 make_pack_expansion (tree arg, tsubst_flags_t complain)
3731 {
3732 tree result;
3733 tree parameter_packs = NULL_TREE;
3734 bool for_types = false;
3735 struct find_parameter_pack_data ppd;
3736
3737 if (!arg || arg == error_mark_node)
3738 return arg;
3739
3740 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3741 {
3742 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3743 class initializer. In this case, the TREE_PURPOSE will be a
3744 _TYPE node (representing the base class expansion we're
3745 initializing) and the TREE_VALUE will be a TREE_LIST
3746 containing the initialization arguments.
3747
3748 The resulting expansion looks somewhat different from most
3749 expansions. Rather than returning just one _EXPANSION, we
3750 return a TREE_LIST whose TREE_PURPOSE is a
3751 TYPE_PACK_EXPANSION containing the bases that will be
3752 initialized. The TREE_VALUE will be identical to the
3753 original TREE_VALUE, which is a list of arguments that will
3754 be passed to each base. We do not introduce any new pack
3755 expansion nodes into the TREE_VALUE (although it is possible
3756 that some already exist), because the TREE_PURPOSE and
3757 TREE_VALUE all need to be expanded together with the same
3758 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3759 resulting TREE_PURPOSE will mention the parameter packs in
3760 both the bases and the arguments to the bases. */
3761 tree purpose;
3762 tree value;
3763 tree parameter_packs = NULL_TREE;
3764
3765 /* Determine which parameter packs will be used by the base
3766 class expansion. */
3767 ppd.visited = new hash_set<tree>;
3768 ppd.parameter_packs = &parameter_packs;
3769 ppd.type_pack_expansion_p = true;
3770 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3771 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3772 &ppd, ppd.visited);
3773
3774 if (parameter_packs == NULL_TREE)
3775 {
3776 if (complain & tf_error)
3777 error ("base initializer expansion %qT contains no parameter packs",
3778 arg);
3779 delete ppd.visited;
3780 return error_mark_node;
3781 }
3782
3783 if (TREE_VALUE (arg) != void_type_node)
3784 {
3785 /* Collect the sets of parameter packs used in each of the
3786 initialization arguments. */
3787 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3788 {
3789 /* Determine which parameter packs will be expanded in this
3790 argument. */
3791 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3792 &ppd, ppd.visited);
3793 }
3794 }
3795
3796 delete ppd.visited;
3797
3798 /* Create the pack expansion type for the base type. */
3799 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3800 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3801 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3802 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3803
3804 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3805 they will rarely be compared to anything. */
3806 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3807
3808 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3809 }
3810
3811 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3812 for_types = true;
3813
3814 /* Build the PACK_EXPANSION_* node. */
3815 result = for_types
3816 ? cxx_make_type (TYPE_PACK_EXPANSION)
3817 : make_node (EXPR_PACK_EXPANSION);
3818 SET_PACK_EXPANSION_PATTERN (result, arg);
3819 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3820 {
3821 /* Propagate type and const-expression information. */
3822 TREE_TYPE (result) = TREE_TYPE (arg);
3823 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3824 /* Mark this read now, since the expansion might be length 0. */
3825 mark_exp_read (arg);
3826 }
3827 else
3828 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3829 they will rarely be compared to anything. */
3830 SET_TYPE_STRUCTURAL_EQUALITY (result);
3831
3832 /* Determine which parameter packs will be expanded. */
3833 ppd.parameter_packs = &parameter_packs;
3834 ppd.visited = new hash_set<tree>;
3835 ppd.type_pack_expansion_p = TYPE_P (arg);
3836 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3837 delete ppd.visited;
3838
3839 /* Make sure we found some parameter packs. */
3840 if (parameter_packs == NULL_TREE)
3841 {
3842 if (complain & tf_error)
3843 {
3844 if (TYPE_P (arg))
3845 error ("expansion pattern %qT contains no argument packs", arg);
3846 else
3847 error ("expansion pattern %qE contains no argument packs", arg);
3848 }
3849 return error_mark_node;
3850 }
3851 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3852
3853 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3854
3855 return result;
3856 }
3857
3858 /* Checks T for any "bare" parameter packs, which have not yet been
3859 expanded, and issues an error if any are found. This operation can
3860 only be done on full expressions or types (e.g., an expression
3861 statement, "if" condition, etc.), because we could have expressions like:
3862
3863 foo(f(g(h(args)))...)
3864
3865 where "args" is a parameter pack. check_for_bare_parameter_packs
3866 should not be called for the subexpressions args, h(args),
3867 g(h(args)), or f(g(h(args))), because we would produce erroneous
3868 error messages.
3869
3870 Returns TRUE and emits an error if there were bare parameter packs,
3871 returns FALSE otherwise. */
3872 bool
3873 check_for_bare_parameter_packs (tree t)
3874 {
3875 tree parameter_packs = NULL_TREE;
3876 struct find_parameter_pack_data ppd;
3877
3878 if (!processing_template_decl || !t || t == error_mark_node)
3879 return false;
3880
3881 /* A lambda might use a parameter pack from the containing context. */
3882 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3883 return false;
3884
3885 if (TREE_CODE (t) == TYPE_DECL)
3886 t = TREE_TYPE (t);
3887
3888 ppd.parameter_packs = &parameter_packs;
3889 ppd.visited = new hash_set<tree>;
3890 ppd.type_pack_expansion_p = false;
3891 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3892 delete ppd.visited;
3893
3894 if (parameter_packs)
3895 {
3896 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3897 error_at (loc, "parameter packs not expanded with %<...%>:");
3898 while (parameter_packs)
3899 {
3900 tree pack = TREE_VALUE (parameter_packs);
3901 tree name = NULL_TREE;
3902
3903 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3904 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3905 name = TYPE_NAME (pack);
3906 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3907 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3908 else if (TREE_CODE (pack) == CALL_EXPR)
3909 name = DECL_NAME (CALL_EXPR_FN (pack));
3910 else
3911 name = DECL_NAME (pack);
3912
3913 if (name)
3914 inform (loc, " %qD", name);
3915 else
3916 inform (loc, " <anonymous>");
3917
3918 parameter_packs = TREE_CHAIN (parameter_packs);
3919 }
3920
3921 return true;
3922 }
3923
3924 return false;
3925 }
3926
3927 /* Expand any parameter packs that occur in the template arguments in
3928 ARGS. */
3929 tree
3930 expand_template_argument_pack (tree args)
3931 {
3932 if (args == error_mark_node)
3933 return error_mark_node;
3934
3935 tree result_args = NULL_TREE;
3936 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3937 int num_result_args = -1;
3938 int non_default_args_count = -1;
3939
3940 /* First, determine if we need to expand anything, and the number of
3941 slots we'll need. */
3942 for (in_arg = 0; in_arg < nargs; ++in_arg)
3943 {
3944 tree arg = TREE_VEC_ELT (args, in_arg);
3945 if (arg == NULL_TREE)
3946 return args;
3947 if (ARGUMENT_PACK_P (arg))
3948 {
3949 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3950 if (num_result_args < 0)
3951 num_result_args = in_arg + num_packed;
3952 else
3953 num_result_args += num_packed;
3954 }
3955 else
3956 {
3957 if (num_result_args >= 0)
3958 num_result_args++;
3959 }
3960 }
3961
3962 /* If no expansion is necessary, we're done. */
3963 if (num_result_args < 0)
3964 return args;
3965
3966 /* Expand arguments. */
3967 result_args = make_tree_vec (num_result_args);
3968 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3969 non_default_args_count =
3970 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3971 for (in_arg = 0; in_arg < nargs; ++in_arg)
3972 {
3973 tree arg = TREE_VEC_ELT (args, in_arg);
3974 if (ARGUMENT_PACK_P (arg))
3975 {
3976 tree packed = ARGUMENT_PACK_ARGS (arg);
3977 int i, num_packed = TREE_VEC_LENGTH (packed);
3978 for (i = 0; i < num_packed; ++i, ++out_arg)
3979 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3980 if (non_default_args_count > 0)
3981 non_default_args_count += num_packed - 1;
3982 }
3983 else
3984 {
3985 TREE_VEC_ELT (result_args, out_arg) = arg;
3986 ++out_arg;
3987 }
3988 }
3989 if (non_default_args_count >= 0)
3990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3991 return result_args;
3992 }
3993
3994 /* Checks if DECL shadows a template parameter.
3995
3996 [temp.local]: A template-parameter shall not be redeclared within its
3997 scope (including nested scopes).
3998
3999 Emits an error and returns TRUE if the DECL shadows a parameter,
4000 returns FALSE otherwise. */
4001
4002 bool
4003 check_template_shadow (tree decl)
4004 {
4005 tree olddecl;
4006
4007 /* If we're not in a template, we can't possibly shadow a template
4008 parameter. */
4009 if (!current_template_parms)
4010 return true;
4011
4012 /* Figure out what we're shadowing. */
4013 decl = OVL_FIRST (decl);
4014 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4015
4016 /* If there's no previous binding for this name, we're not shadowing
4017 anything, let alone a template parameter. */
4018 if (!olddecl)
4019 return true;
4020
4021 /* If we're not shadowing a template parameter, we're done. Note
4022 that OLDDECL might be an OVERLOAD (or perhaps even an
4023 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4024 node. */
4025 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4026 return true;
4027
4028 /* We check for decl != olddecl to avoid bogus errors for using a
4029 name inside a class. We check TPFI to avoid duplicate errors for
4030 inline member templates. */
4031 if (decl == olddecl
4032 || (DECL_TEMPLATE_PARM_P (decl)
4033 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4034 return true;
4035
4036 /* Don't complain about the injected class name, as we've already
4037 complained about the class itself. */
4038 if (DECL_SELF_REFERENCE_P (decl))
4039 return false;
4040
4041 if (DECL_TEMPLATE_PARM_P (decl))
4042 error ("declaration of template parameter %q+D shadows "
4043 "template parameter", decl);
4044 else
4045 error ("declaration of %q+#D shadows template parameter", decl);
4046 inform (DECL_SOURCE_LOCATION (olddecl),
4047 "template parameter %qD declared here", olddecl);
4048 return false;
4049 }
4050
4051 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4052 ORIG_LEVEL, DECL, and TYPE. */
4053
4054 static tree
4055 build_template_parm_index (int index,
4056 int level,
4057 int orig_level,
4058 tree decl,
4059 tree type)
4060 {
4061 tree t = make_node (TEMPLATE_PARM_INDEX);
4062 TEMPLATE_PARM_IDX (t) = index;
4063 TEMPLATE_PARM_LEVEL (t) = level;
4064 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4065 TEMPLATE_PARM_DECL (t) = decl;
4066 TREE_TYPE (t) = type;
4067 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4068 TREE_READONLY (t) = TREE_READONLY (decl);
4069
4070 return t;
4071 }
4072
4073 /* Find the canonical type parameter for the given template type
4074 parameter. Returns the canonical type parameter, which may be TYPE
4075 if no such parameter existed. */
4076
4077 static tree
4078 canonical_type_parameter (tree type)
4079 {
4080 tree list;
4081 int idx = TEMPLATE_TYPE_IDX (type);
4082 if (!canonical_template_parms)
4083 vec_alloc (canonical_template_parms, idx + 1);
4084
4085 if (canonical_template_parms->length () <= (unsigned) idx)
4086 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4087
4088 list = (*canonical_template_parms)[idx];
4089 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4090 list = TREE_CHAIN (list);
4091
4092 if (list)
4093 return TREE_VALUE (list);
4094 else
4095 {
4096 (*canonical_template_parms)[idx]
4097 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4098 return type;
4099 }
4100 }
4101
4102 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4103 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4104 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4105 new one is created. */
4106
4107 static tree
4108 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4109 tsubst_flags_t complain)
4110 {
4111 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4112 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4113 != TEMPLATE_PARM_LEVEL (index) - levels)
4114 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4115 {
4116 tree orig_decl = TEMPLATE_PARM_DECL (index);
4117 tree decl, t;
4118
4119 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4120 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4121 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4122 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4123 DECL_ARTIFICIAL (decl) = 1;
4124 SET_DECL_TEMPLATE_PARM_P (decl);
4125
4126 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4127 TEMPLATE_PARM_LEVEL (index) - levels,
4128 TEMPLATE_PARM_ORIG_LEVEL (index),
4129 decl, type);
4130 TEMPLATE_PARM_DESCENDANTS (index) = t;
4131 TEMPLATE_PARM_PARAMETER_PACK (t)
4132 = TEMPLATE_PARM_PARAMETER_PACK (index);
4133
4134 /* Template template parameters need this. */
4135 if (TREE_CODE (decl) == TEMPLATE_DECL)
4136 {
4137 DECL_TEMPLATE_RESULT (decl)
4138 = build_decl (DECL_SOURCE_LOCATION (decl),
4139 TYPE_DECL, DECL_NAME (decl), type);
4140 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4141 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4142 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4143 }
4144 }
4145
4146 return TEMPLATE_PARM_DESCENDANTS (index);
4147 }
4148
4149 /* Process information from new template parameter PARM and append it
4150 to the LIST being built. This new parameter is a non-type
4151 parameter iff IS_NON_TYPE is true. This new parameter is a
4152 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4153 is in PARM_LOC. */
4154
4155 tree
4156 process_template_parm (tree list, location_t parm_loc, tree parm,
4157 bool is_non_type, bool is_parameter_pack)
4158 {
4159 tree decl = 0;
4160 int idx = 0;
4161
4162 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4163 tree defval = TREE_PURPOSE (parm);
4164 tree constr = TREE_TYPE (parm);
4165
4166 if (list)
4167 {
4168 tree p = tree_last (list);
4169
4170 if (p && TREE_VALUE (p) != error_mark_node)
4171 {
4172 p = TREE_VALUE (p);
4173 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4174 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4175 else
4176 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4177 }
4178
4179 ++idx;
4180 }
4181
4182 if (is_non_type)
4183 {
4184 parm = TREE_VALUE (parm);
4185
4186 SET_DECL_TEMPLATE_PARM_P (parm);
4187
4188 if (TREE_TYPE (parm) != error_mark_node)
4189 {
4190 /* [temp.param]
4191
4192 The top-level cv-qualifiers on the template-parameter are
4193 ignored when determining its type. */
4194 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4195 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4196 TREE_TYPE (parm) = error_mark_node;
4197 else if (uses_parameter_packs (TREE_TYPE (parm))
4198 && !is_parameter_pack
4199 /* If we're in a nested template parameter list, the template
4200 template parameter could be a parameter pack. */
4201 && processing_template_parmlist == 1)
4202 {
4203 /* This template parameter is not a parameter pack, but it
4204 should be. Complain about "bare" parameter packs. */
4205 check_for_bare_parameter_packs (TREE_TYPE (parm));
4206
4207 /* Recover by calling this a parameter pack. */
4208 is_parameter_pack = true;
4209 }
4210 }
4211
4212 /* A template parameter is not modifiable. */
4213 TREE_CONSTANT (parm) = 1;
4214 TREE_READONLY (parm) = 1;
4215 decl = build_decl (parm_loc,
4216 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4217 TREE_CONSTANT (decl) = 1;
4218 TREE_READONLY (decl) = 1;
4219 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4220 = build_template_parm_index (idx, processing_template_decl,
4221 processing_template_decl,
4222 decl, TREE_TYPE (parm));
4223
4224 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4225 = is_parameter_pack;
4226 }
4227 else
4228 {
4229 tree t;
4230 parm = TREE_VALUE (TREE_VALUE (parm));
4231
4232 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4233 {
4234 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4235 /* This is for distinguishing between real templates and template
4236 template parameters */
4237 TREE_TYPE (parm) = t;
4238 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4239 decl = parm;
4240 }
4241 else
4242 {
4243 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4244 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4245 decl = build_decl (parm_loc,
4246 TYPE_DECL, parm, t);
4247 }
4248
4249 TYPE_NAME (t) = decl;
4250 TYPE_STUB_DECL (t) = decl;
4251 parm = decl;
4252 TEMPLATE_TYPE_PARM_INDEX (t)
4253 = build_template_parm_index (idx, processing_template_decl,
4254 processing_template_decl,
4255 decl, TREE_TYPE (parm));
4256 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4257 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4258 }
4259 DECL_ARTIFICIAL (decl) = 1;
4260 SET_DECL_TEMPLATE_PARM_P (decl);
4261
4262 /* Build requirements for the type/template parameter.
4263 This must be done after SET_DECL_TEMPLATE_PARM_P or
4264 process_template_parm could fail. */
4265 tree reqs = finish_shorthand_constraint (parm, constr);
4266
4267 pushdecl (decl);
4268
4269 /* Build the parameter node linking the parameter declaration,
4270 its default argument (if any), and its constraints (if any). */
4271 parm = build_tree_list (defval, parm);
4272 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4273
4274 return chainon (list, parm);
4275 }
4276
4277 /* The end of a template parameter list has been reached. Process the
4278 tree list into a parameter vector, converting each parameter into a more
4279 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4280 as PARM_DECLs. */
4281
4282 tree
4283 end_template_parm_list (tree parms)
4284 {
4285 int nparms;
4286 tree parm, next;
4287 tree saved_parmlist = make_tree_vec (list_length (parms));
4288
4289 /* Pop the dummy parameter level and add the real one. */
4290 current_template_parms = TREE_CHAIN (current_template_parms);
4291
4292 current_template_parms
4293 = tree_cons (size_int (processing_template_decl),
4294 saved_parmlist, current_template_parms);
4295
4296 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4297 {
4298 next = TREE_CHAIN (parm);
4299 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4300 TREE_CHAIN (parm) = NULL_TREE;
4301 }
4302
4303 --processing_template_parmlist;
4304
4305 return saved_parmlist;
4306 }
4307
4308 // Explicitly indicate the end of the template parameter list. We assume
4309 // that the current template parameters have been constructed and/or
4310 // managed explicitly, as when creating new template template parameters
4311 // from a shorthand constraint.
4312 void
4313 end_template_parm_list ()
4314 {
4315 --processing_template_parmlist;
4316 }
4317
4318 /* end_template_decl is called after a template declaration is seen. */
4319
4320 void
4321 end_template_decl (void)
4322 {
4323 reset_specialization ();
4324
4325 if (! processing_template_decl)
4326 return;
4327
4328 /* This matches the pushlevel in begin_template_parm_list. */
4329 finish_scope ();
4330
4331 --processing_template_decl;
4332 current_template_parms = TREE_CHAIN (current_template_parms);
4333 }
4334
4335 /* Takes a TREE_LIST representing a template parameter and convert it
4336 into an argument suitable to be passed to the type substitution
4337 functions. Note that If the TREE_LIST contains an error_mark
4338 node, the returned argument is error_mark_node. */
4339
4340 tree
4341 template_parm_to_arg (tree t)
4342 {
4343
4344 if (t == NULL_TREE
4345 || TREE_CODE (t) != TREE_LIST)
4346 return t;
4347
4348 if (error_operand_p (TREE_VALUE (t)))
4349 return error_mark_node;
4350
4351 t = TREE_VALUE (t);
4352
4353 if (TREE_CODE (t) == TYPE_DECL
4354 || TREE_CODE (t) == TEMPLATE_DECL)
4355 {
4356 t = TREE_TYPE (t);
4357
4358 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4359 {
4360 /* Turn this argument into a TYPE_ARGUMENT_PACK
4361 with a single element, which expands T. */
4362 tree vec = make_tree_vec (1);
4363 if (CHECKING_P)
4364 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4365
4366 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4367
4368 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4369 SET_ARGUMENT_PACK_ARGS (t, vec);
4370 }
4371 }
4372 else
4373 {
4374 t = DECL_INITIAL (t);
4375
4376 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4377 {
4378 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4379 with a single element, which expands T. */
4380 tree vec = make_tree_vec (1);
4381 if (CHECKING_P)
4382 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4383
4384 t = convert_from_reference (t);
4385 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4386
4387 t = make_node (NONTYPE_ARGUMENT_PACK);
4388 SET_ARGUMENT_PACK_ARGS (t, vec);
4389 }
4390 else
4391 t = convert_from_reference (t);
4392 }
4393 return t;
4394 }
4395
4396 /* Given a single level of template parameters (a TREE_VEC), return it
4397 as a set of template arguments. */
4398
4399 static tree
4400 template_parms_level_to_args (tree parms)
4401 {
4402 tree a = copy_node (parms);
4403 TREE_TYPE (a) = NULL_TREE;
4404 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4405 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4406
4407 if (CHECKING_P)
4408 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4409
4410 return a;
4411 }
4412
4413 /* Given a set of template parameters, return them as a set of template
4414 arguments. The template parameters are represented as a TREE_VEC, in
4415 the form documented in cp-tree.h for template arguments. */
4416
4417 static tree
4418 template_parms_to_args (tree parms)
4419 {
4420 tree header;
4421 tree args = NULL_TREE;
4422 int length = TMPL_PARMS_DEPTH (parms);
4423 int l = length;
4424
4425 /* If there is only one level of template parameters, we do not
4426 create a TREE_VEC of TREE_VECs. Instead, we return a single
4427 TREE_VEC containing the arguments. */
4428 if (length > 1)
4429 args = make_tree_vec (length);
4430
4431 for (header = parms; header; header = TREE_CHAIN (header))
4432 {
4433 tree a = template_parms_level_to_args (TREE_VALUE (header));
4434
4435 if (length > 1)
4436 TREE_VEC_ELT (args, --l) = a;
4437 else
4438 args = a;
4439 }
4440
4441 return args;
4442 }
4443
4444 /* Within the declaration of a template, return the currently active
4445 template parameters as an argument TREE_VEC. */
4446
4447 static tree
4448 current_template_args (void)
4449 {
4450 return template_parms_to_args (current_template_parms);
4451 }
4452
4453 /* Update the declared TYPE by doing any lookups which were thought to be
4454 dependent, but are not now that we know the SCOPE of the declarator. */
4455
4456 tree
4457 maybe_update_decl_type (tree orig_type, tree scope)
4458 {
4459 tree type = orig_type;
4460
4461 if (type == NULL_TREE)
4462 return type;
4463
4464 if (TREE_CODE (orig_type) == TYPE_DECL)
4465 type = TREE_TYPE (type);
4466
4467 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4468 && dependent_type_p (type)
4469 /* Don't bother building up the args in this case. */
4470 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4471 {
4472 /* tsubst in the args corresponding to the template parameters,
4473 including auto if present. Most things will be unchanged, but
4474 make_typename_type and tsubst_qualified_id will resolve
4475 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4476 tree args = current_template_args ();
4477 tree auto_node = type_uses_auto (type);
4478 tree pushed;
4479 if (auto_node)
4480 {
4481 tree auto_vec = make_tree_vec (1);
4482 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4483 args = add_to_template_args (args, auto_vec);
4484 }
4485 pushed = push_scope (scope);
4486 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4487 if (pushed)
4488 pop_scope (scope);
4489 }
4490
4491 if (type == error_mark_node)
4492 return orig_type;
4493
4494 if (TREE_CODE (orig_type) == TYPE_DECL)
4495 {
4496 if (same_type_p (type, TREE_TYPE (orig_type)))
4497 type = orig_type;
4498 else
4499 type = TYPE_NAME (type);
4500 }
4501 return type;
4502 }
4503
4504 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4505 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4506 the new template is a member template. */
4507
4508 tree
4509 build_template_decl (tree decl, tree parms, bool member_template_p)
4510 {
4511 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4512 DECL_TEMPLATE_PARMS (tmpl) = parms;
4513 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4514 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4515 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4516
4517 return tmpl;
4518 }
4519
4520 struct template_parm_data
4521 {
4522 /* The level of the template parameters we are currently
4523 processing. */
4524 int level;
4525
4526 /* The index of the specialization argument we are currently
4527 processing. */
4528 int current_arg;
4529
4530 /* An array whose size is the number of template parameters. The
4531 elements are nonzero if the parameter has been used in any one
4532 of the arguments processed so far. */
4533 int* parms;
4534
4535 /* An array whose size is the number of template arguments. The
4536 elements are nonzero if the argument makes use of template
4537 parameters of this level. */
4538 int* arg_uses_template_parms;
4539 };
4540
4541 /* Subroutine of push_template_decl used to see if each template
4542 parameter in a partial specialization is used in the explicit
4543 argument list. If T is of the LEVEL given in DATA (which is
4544 treated as a template_parm_data*), then DATA->PARMS is marked
4545 appropriately. */
4546
4547 static int
4548 mark_template_parm (tree t, void* data)
4549 {
4550 int level;
4551 int idx;
4552 struct template_parm_data* tpd = (struct template_parm_data*) data;
4553
4554 template_parm_level_and_index (t, &level, &idx);
4555
4556 if (level == tpd->level)
4557 {
4558 tpd->parms[idx] = 1;
4559 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4560 }
4561
4562 /* In C++17 the type of a non-type argument is a deduced context. */
4563 if (cxx_dialect >= cxx17
4564 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4565 for_each_template_parm (TREE_TYPE (t),
4566 &mark_template_parm,
4567 data,
4568 NULL,
4569 /*include_nondeduced_p=*/false);
4570
4571 /* Return zero so that for_each_template_parm will continue the
4572 traversal of the tree; we want to mark *every* template parm. */
4573 return 0;
4574 }
4575
4576 /* Process the partial specialization DECL. */
4577
4578 static tree
4579 process_partial_specialization (tree decl)
4580 {
4581 tree type = TREE_TYPE (decl);
4582 tree tinfo = get_template_info (decl);
4583 tree maintmpl = TI_TEMPLATE (tinfo);
4584 tree specargs = TI_ARGS (tinfo);
4585 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4586 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4587 tree inner_parms;
4588 tree inst;
4589 int nargs = TREE_VEC_LENGTH (inner_args);
4590 int ntparms;
4591 int i;
4592 bool did_error_intro = false;
4593 struct template_parm_data tpd;
4594 struct template_parm_data tpd2;
4595
4596 gcc_assert (current_template_parms);
4597
4598 /* A concept cannot be specialized. */
4599 if (flag_concepts && variable_concept_p (maintmpl))
4600 {
4601 error ("specialization of variable concept %q#D", maintmpl);
4602 return error_mark_node;
4603 }
4604
4605 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4606 ntparms = TREE_VEC_LENGTH (inner_parms);
4607
4608 /* We check that each of the template parameters given in the
4609 partial specialization is used in the argument list to the
4610 specialization. For example:
4611
4612 template <class T> struct S;
4613 template <class T> struct S<T*>;
4614
4615 The second declaration is OK because `T*' uses the template
4616 parameter T, whereas
4617
4618 template <class T> struct S<int>;
4619
4620 is no good. Even trickier is:
4621
4622 template <class T>
4623 struct S1
4624 {
4625 template <class U>
4626 struct S2;
4627 template <class U>
4628 struct S2<T>;
4629 };
4630
4631 The S2<T> declaration is actually invalid; it is a
4632 full-specialization. Of course,
4633
4634 template <class U>
4635 struct S2<T (*)(U)>;
4636
4637 or some such would have been OK. */
4638 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4639 tpd.parms = XALLOCAVEC (int, ntparms);
4640 memset (tpd.parms, 0, sizeof (int) * ntparms);
4641
4642 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4643 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4644 for (i = 0; i < nargs; ++i)
4645 {
4646 tpd.current_arg = i;
4647 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4648 &mark_template_parm,
4649 &tpd,
4650 NULL,
4651 /*include_nondeduced_p=*/false);
4652 }
4653 for (i = 0; i < ntparms; ++i)
4654 if (tpd.parms[i] == 0)
4655 {
4656 /* One of the template parms was not used in a deduced context in the
4657 specialization. */
4658 if (!did_error_intro)
4659 {
4660 error ("template parameters not deducible in "
4661 "partial specialization:");
4662 did_error_intro = true;
4663 }
4664
4665 inform (input_location, " %qD",
4666 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4667 }
4668
4669 if (did_error_intro)
4670 return error_mark_node;
4671
4672 /* [temp.class.spec]
4673
4674 The argument list of the specialization shall not be identical to
4675 the implicit argument list of the primary template. */
4676 tree main_args
4677 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4678 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4679 && (!flag_concepts
4680 || !strictly_subsumes (current_template_constraints (),
4681 get_constraints (maintmpl))))
4682 {
4683 if (!flag_concepts)
4684 error ("partial specialization %q+D does not specialize "
4685 "any template arguments", decl);
4686 else
4687 error ("partial specialization %q+D does not specialize any "
4688 "template arguments and is not more constrained than", decl);
4689 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4690 }
4691
4692 /* A partial specialization that replaces multiple parameters of the
4693 primary template with a pack expansion is less specialized for those
4694 parameters. */
4695 if (nargs < DECL_NTPARMS (maintmpl))
4696 {
4697 error ("partial specialization is not more specialized than the "
4698 "primary template because it replaces multiple parameters "
4699 "with a pack expansion");
4700 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4701 /* Avoid crash in process_partial_specialization. */
4702 return decl;
4703 }
4704
4705 /* If we aren't in a dependent class, we can actually try deduction. */
4706 else if (tpd.level == 1
4707 /* FIXME we should be able to handle a partial specialization of a
4708 partial instantiation, but currently we can't (c++/41727). */
4709 && TMPL_ARGS_DEPTH (specargs) == 1
4710 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4711 {
4712 if (permerror (input_location, "partial specialization %qD is not "
4713 "more specialized than", decl))
4714 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4715 maintmpl);
4716 }
4717
4718 /* [temp.class.spec]
4719
4720 A partially specialized non-type argument expression shall not
4721 involve template parameters of the partial specialization except
4722 when the argument expression is a simple identifier.
4723
4724 The type of a template parameter corresponding to a specialized
4725 non-type argument shall not be dependent on a parameter of the
4726 specialization.
4727
4728 Also, we verify that pack expansions only occur at the
4729 end of the argument list. */
4730 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4731 tpd2.parms = 0;
4732 for (i = 0; i < nargs; ++i)
4733 {
4734 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4735 tree arg = TREE_VEC_ELT (inner_args, i);
4736 tree packed_args = NULL_TREE;
4737 int j, len = 1;
4738
4739 if (ARGUMENT_PACK_P (arg))
4740 {
4741 /* Extract the arguments from the argument pack. We'll be
4742 iterating over these in the following loop. */
4743 packed_args = ARGUMENT_PACK_ARGS (arg);
4744 len = TREE_VEC_LENGTH (packed_args);
4745 }
4746
4747 for (j = 0; j < len; j++)
4748 {
4749 if (packed_args)
4750 /* Get the Jth argument in the parameter pack. */
4751 arg = TREE_VEC_ELT (packed_args, j);
4752
4753 if (PACK_EXPANSION_P (arg))
4754 {
4755 /* Pack expansions must come at the end of the
4756 argument list. */
4757 if ((packed_args && j < len - 1)
4758 || (!packed_args && i < nargs - 1))
4759 {
4760 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4761 error ("parameter pack argument %qE must be at the "
4762 "end of the template argument list", arg);
4763 else
4764 error ("parameter pack argument %qT must be at the "
4765 "end of the template argument list", arg);
4766 }
4767 }
4768
4769 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4770 /* We only care about the pattern. */
4771 arg = PACK_EXPANSION_PATTERN (arg);
4772
4773 if (/* These first two lines are the `non-type' bit. */
4774 !TYPE_P (arg)
4775 && TREE_CODE (arg) != TEMPLATE_DECL
4776 /* This next two lines are the `argument expression is not just a
4777 simple identifier' condition and also the `specialized
4778 non-type argument' bit. */
4779 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4780 && !(REFERENCE_REF_P (arg)
4781 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4782 {
4783 if ((!packed_args && tpd.arg_uses_template_parms[i])
4784 || (packed_args && uses_template_parms (arg)))
4785 error ("template argument %qE involves template parameter(s)",
4786 arg);
4787 else
4788 {
4789 /* Look at the corresponding template parameter,
4790 marking which template parameters its type depends
4791 upon. */
4792 tree type = TREE_TYPE (parm);
4793
4794 if (!tpd2.parms)
4795 {
4796 /* We haven't yet initialized TPD2. Do so now. */
4797 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4798 /* The number of parameters here is the number in the
4799 main template, which, as checked in the assertion
4800 above, is NARGS. */
4801 tpd2.parms = XALLOCAVEC (int, nargs);
4802 tpd2.level =
4803 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4804 }
4805
4806 /* Mark the template parameters. But this time, we're
4807 looking for the template parameters of the main
4808 template, not in the specialization. */
4809 tpd2.current_arg = i;
4810 tpd2.arg_uses_template_parms[i] = 0;
4811 memset (tpd2.parms, 0, sizeof (int) * nargs);
4812 for_each_template_parm (type,
4813 &mark_template_parm,
4814 &tpd2,
4815 NULL,
4816 /*include_nondeduced_p=*/false);
4817
4818 if (tpd2.arg_uses_template_parms [i])
4819 {
4820 /* The type depended on some template parameters.
4821 If they are fully specialized in the
4822 specialization, that's OK. */
4823 int j;
4824 int count = 0;
4825 for (j = 0; j < nargs; ++j)
4826 if (tpd2.parms[j] != 0
4827 && tpd.arg_uses_template_parms [j])
4828 ++count;
4829 if (count != 0)
4830 error_n (input_location, count,
4831 "type %qT of template argument %qE depends "
4832 "on a template parameter",
4833 "type %qT of template argument %qE depends "
4834 "on template parameters",
4835 type,
4836 arg);
4837 }
4838 }
4839 }
4840 }
4841 }
4842
4843 /* We should only get here once. */
4844 if (TREE_CODE (decl) == TYPE_DECL)
4845 gcc_assert (!COMPLETE_TYPE_P (type));
4846
4847 // Build the template decl.
4848 tree tmpl = build_template_decl (decl, current_template_parms,
4849 DECL_MEMBER_TEMPLATE_P (maintmpl));
4850 TREE_TYPE (tmpl) = type;
4851 DECL_TEMPLATE_RESULT (tmpl) = decl;
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4853 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4854 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4855
4856 /* Give template template parms a DECL_CONTEXT of the template
4857 for which they are a parameter. */
4858 for (i = 0; i < ntparms; ++i)
4859 {
4860 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4861 if (TREE_CODE (parm) == TEMPLATE_DECL)
4862 DECL_CONTEXT (parm) = tmpl;
4863 }
4864
4865 if (VAR_P (decl))
4866 /* We didn't register this in check_explicit_specialization so we could
4867 wait until the constraints were set. */
4868 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4869 else
4870 associate_classtype_constraints (type);
4871
4872 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4873 = tree_cons (specargs, tmpl,
4874 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4875 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4876
4877 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4878 inst = TREE_CHAIN (inst))
4879 {
4880 tree instance = TREE_VALUE (inst);
4881 if (TYPE_P (instance)
4882 ? (COMPLETE_TYPE_P (instance)
4883 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4884 : DECL_TEMPLATE_INSTANTIATION (instance))
4885 {
4886 tree spec = most_specialized_partial_spec (instance, tf_none);
4887 tree inst_decl = (DECL_P (instance)
4888 ? instance : TYPE_NAME (instance));
4889 if (!spec)
4890 /* OK */;
4891 else if (spec == error_mark_node)
4892 permerror (input_location,
4893 "declaration of %qD ambiguates earlier template "
4894 "instantiation for %qD", decl, inst_decl);
4895 else if (TREE_VALUE (spec) == tmpl)
4896 permerror (input_location,
4897 "partial specialization of %qD after instantiation "
4898 "of %qD", decl, inst_decl);
4899 }
4900 }
4901
4902 return decl;
4903 }
4904
4905 /* PARM is a template parameter of some form; return the corresponding
4906 TEMPLATE_PARM_INDEX. */
4907
4908 static tree
4909 get_template_parm_index (tree parm)
4910 {
4911 if (TREE_CODE (parm) == PARM_DECL
4912 || TREE_CODE (parm) == CONST_DECL)
4913 parm = DECL_INITIAL (parm);
4914 else if (TREE_CODE (parm) == TYPE_DECL
4915 || TREE_CODE (parm) == TEMPLATE_DECL)
4916 parm = TREE_TYPE (parm);
4917 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4918 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4919 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4920 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4921 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4922 return parm;
4923 }
4924
4925 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4926 parameter packs used by the template parameter PARM. */
4927
4928 static void
4929 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4930 {
4931 /* A type parm can't refer to another parm. */
4932 if (TREE_CODE (parm) == TYPE_DECL)
4933 return;
4934 else if (TREE_CODE (parm) == PARM_DECL)
4935 {
4936 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4937 ppd, ppd->visited);
4938 return;
4939 }
4940
4941 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4942
4943 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4944 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4945 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4946 }
4947
4948 /* PARM is a template parameter pack. Return any parameter packs used in
4949 its type or the type of any of its template parameters. If there are
4950 any such packs, it will be instantiated into a fixed template parameter
4951 list by partial instantiation rather than be fully deduced. */
4952
4953 tree
4954 fixed_parameter_pack_p (tree parm)
4955 {
4956 /* This can only be true in a member template. */
4957 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4958 return NULL_TREE;
4959 /* This can only be true for a parameter pack. */
4960 if (!template_parameter_pack_p (parm))
4961 return NULL_TREE;
4962 /* A type parm can't refer to another parm. */
4963 if (TREE_CODE (parm) == TYPE_DECL)
4964 return NULL_TREE;
4965
4966 tree parameter_packs = NULL_TREE;
4967 struct find_parameter_pack_data ppd;
4968 ppd.parameter_packs = &parameter_packs;
4969 ppd.visited = new hash_set<tree>;
4970 ppd.type_pack_expansion_p = false;
4971
4972 fixed_parameter_pack_p_1 (parm, &ppd);
4973
4974 delete ppd.visited;
4975 return parameter_packs;
4976 }
4977
4978 /* Check that a template declaration's use of default arguments and
4979 parameter packs is not invalid. Here, PARMS are the template
4980 parameters. IS_PRIMARY is true if DECL is the thing declared by
4981 a primary template. IS_PARTIAL is true if DECL is a partial
4982 specialization.
4983
4984 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
4985 function template declaration or a friend class template
4986 declaration. In the function case, 1 indicates a declaration, 2
4987 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4988 emitted for extraneous default arguments.
4989
4990 Returns TRUE if there were no errors found, FALSE otherwise. */
4991
4992 bool
4993 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4994 bool is_partial, int is_friend_decl)
4995 {
4996 const char *msg;
4997 int last_level_to_check;
4998 tree parm_level;
4999 bool no_errors = true;
5000
5001 /* [temp.param]
5002
5003 A default template-argument shall not be specified in a
5004 function template declaration or a function template definition, nor
5005 in the template-parameter-list of the definition of a member of a
5006 class template. */
5007
5008 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5009 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5010 /* You can't have a function template declaration in a local
5011 scope, nor you can you define a member of a class template in a
5012 local scope. */
5013 return true;
5014
5015 if ((TREE_CODE (decl) == TYPE_DECL
5016 && TREE_TYPE (decl)
5017 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5018 || (TREE_CODE (decl) == FUNCTION_DECL
5019 && LAMBDA_FUNCTION_P (decl)))
5020 /* A lambda doesn't have an explicit declaration; don't complain
5021 about the parms of the enclosing class. */
5022 return true;
5023
5024 if (current_class_type
5025 && !TYPE_BEING_DEFINED (current_class_type)
5026 && DECL_LANG_SPECIFIC (decl)
5027 && DECL_DECLARES_FUNCTION_P (decl)
5028 /* If this is either a friend defined in the scope of the class
5029 or a member function. */
5030 && (DECL_FUNCTION_MEMBER_P (decl)
5031 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5032 : DECL_FRIEND_CONTEXT (decl)
5033 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5034 : false)
5035 /* And, if it was a member function, it really was defined in
5036 the scope of the class. */
5037 && (!DECL_FUNCTION_MEMBER_P (decl)
5038 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5039 /* We already checked these parameters when the template was
5040 declared, so there's no need to do it again now. This function
5041 was defined in class scope, but we're processing its body now
5042 that the class is complete. */
5043 return true;
5044
5045 /* Core issue 226 (C++0x only): the following only applies to class
5046 templates. */
5047 if (is_primary
5048 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5049 {
5050 /* [temp.param]
5051
5052 If a template-parameter has a default template-argument, all
5053 subsequent template-parameters shall have a default
5054 template-argument supplied. */
5055 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5056 {
5057 tree inner_parms = TREE_VALUE (parm_level);
5058 int ntparms = TREE_VEC_LENGTH (inner_parms);
5059 int seen_def_arg_p = 0;
5060 int i;
5061
5062 for (i = 0; i < ntparms; ++i)
5063 {
5064 tree parm = TREE_VEC_ELT (inner_parms, i);
5065
5066 if (parm == error_mark_node)
5067 continue;
5068
5069 if (TREE_PURPOSE (parm))
5070 seen_def_arg_p = 1;
5071 else if (seen_def_arg_p
5072 && !template_parameter_pack_p (TREE_VALUE (parm)))
5073 {
5074 error ("no default argument for %qD", TREE_VALUE (parm));
5075 /* For better subsequent error-recovery, we indicate that
5076 there should have been a default argument. */
5077 TREE_PURPOSE (parm) = error_mark_node;
5078 no_errors = false;
5079 }
5080 else if (!is_partial
5081 && !is_friend_decl
5082 /* Don't complain about an enclosing partial
5083 specialization. */
5084 && parm_level == parms
5085 && TREE_CODE (decl) == TYPE_DECL
5086 && i < ntparms - 1
5087 && template_parameter_pack_p (TREE_VALUE (parm))
5088 /* A fixed parameter pack will be partially
5089 instantiated into a fixed length list. */
5090 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5091 {
5092 /* A primary class template can only have one
5093 parameter pack, at the end of the template
5094 parameter list. */
5095
5096 error ("parameter pack %q+D must be at the end of the"
5097 " template parameter list", TREE_VALUE (parm));
5098
5099 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5100 = error_mark_node;
5101 no_errors = false;
5102 }
5103 }
5104 }
5105 }
5106
5107 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5108 || is_partial
5109 || !is_primary
5110 || is_friend_decl)
5111 /* For an ordinary class template, default template arguments are
5112 allowed at the innermost level, e.g.:
5113 template <class T = int>
5114 struct S {};
5115 but, in a partial specialization, they're not allowed even
5116 there, as we have in [temp.class.spec]:
5117
5118 The template parameter list of a specialization shall not
5119 contain default template argument values.
5120
5121 So, for a partial specialization, or for a function template
5122 (in C++98/C++03), we look at all of them. */
5123 ;
5124 else
5125 /* But, for a primary class template that is not a partial
5126 specialization we look at all template parameters except the
5127 innermost ones. */
5128 parms = TREE_CHAIN (parms);
5129
5130 /* Figure out what error message to issue. */
5131 if (is_friend_decl == 2)
5132 msg = G_("default template arguments may not be used in function template "
5133 "friend re-declaration");
5134 else if (is_friend_decl)
5135 msg = G_("default template arguments may not be used in template "
5136 "friend declarations");
5137 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5138 msg = G_("default template arguments may not be used in function templates "
5139 "without -std=c++11 or -std=gnu++11");
5140 else if (is_partial)
5141 msg = G_("default template arguments may not be used in "
5142 "partial specializations");
5143 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5144 msg = G_("default argument for template parameter for class enclosing %qD");
5145 else
5146 /* Per [temp.param]/9, "A default template-argument shall not be
5147 specified in the template-parameter-lists of the definition of
5148 a member of a class template that appears outside of the member's
5149 class.", thus if we aren't handling a member of a class template
5150 there is no need to examine the parameters. */
5151 return true;
5152
5153 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5154 /* If we're inside a class definition, there's no need to
5155 examine the parameters to the class itself. On the one
5156 hand, they will be checked when the class is defined, and,
5157 on the other, default arguments are valid in things like:
5158 template <class T = double>
5159 struct S { template <class U> void f(U); };
5160 Here the default argument for `S' has no bearing on the
5161 declaration of `f'. */
5162 last_level_to_check = template_class_depth (current_class_type) + 1;
5163 else
5164 /* Check everything. */
5165 last_level_to_check = 0;
5166
5167 for (parm_level = parms;
5168 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5169 parm_level = TREE_CHAIN (parm_level))
5170 {
5171 tree inner_parms = TREE_VALUE (parm_level);
5172 int i;
5173 int ntparms;
5174
5175 ntparms = TREE_VEC_LENGTH (inner_parms);
5176 for (i = 0; i < ntparms; ++i)
5177 {
5178 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5179 continue;
5180
5181 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5182 {
5183 if (msg)
5184 {
5185 no_errors = false;
5186 if (is_friend_decl == 2)
5187 return no_errors;
5188
5189 error (msg, decl);
5190 msg = 0;
5191 }
5192
5193 /* Clear out the default argument so that we are not
5194 confused later. */
5195 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5196 }
5197 }
5198
5199 /* At this point, if we're still interested in issuing messages,
5200 they must apply to classes surrounding the object declared. */
5201 if (msg)
5202 msg = G_("default argument for template parameter for class "
5203 "enclosing %qD");
5204 }
5205
5206 return no_errors;
5207 }
5208
5209 /* Worker for push_template_decl_real, called via
5210 for_each_template_parm. DATA is really an int, indicating the
5211 level of the parameters we are interested in. If T is a template
5212 parameter of that level, return nonzero. */
5213
5214 static int
5215 template_parm_this_level_p (tree t, void* data)
5216 {
5217 int this_level = *(int *)data;
5218 int level;
5219
5220 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5221 level = TEMPLATE_PARM_LEVEL (t);
5222 else
5223 level = TEMPLATE_TYPE_LEVEL (t);
5224 return level == this_level;
5225 }
5226
5227 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5228 DATA is really an int, indicating the innermost outer level of parameters.
5229 If T is a template parameter of that level or further out, return
5230 nonzero. */
5231
5232 static int
5233 template_parm_outer_level (tree t, void *data)
5234 {
5235 int this_level = *(int *)data;
5236 int level;
5237
5238 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5239 level = TEMPLATE_PARM_LEVEL (t);
5240 else
5241 level = TEMPLATE_TYPE_LEVEL (t);
5242 return level <= this_level;
5243 }
5244
5245 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5246 parameters given by current_template_args, or reuses a
5247 previously existing one, if appropriate. Returns the DECL, or an
5248 equivalent one, if it is replaced via a call to duplicate_decls.
5249
5250 If IS_FRIEND is true, DECL is a friend declaration. */
5251
5252 tree
5253 push_template_decl_real (tree decl, bool is_friend)
5254 {
5255 tree tmpl;
5256 tree args;
5257 tree info;
5258 tree ctx;
5259 bool is_primary;
5260 bool is_partial;
5261 int new_template_p = 0;
5262 /* True if the template is a member template, in the sense of
5263 [temp.mem]. */
5264 bool member_template_p = false;
5265
5266 if (decl == error_mark_node || !current_template_parms)
5267 return error_mark_node;
5268
5269 /* See if this is a partial specialization. */
5270 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5271 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5272 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5273 || (VAR_P (decl)
5274 && DECL_LANG_SPECIFIC (decl)
5275 && DECL_TEMPLATE_SPECIALIZATION (decl)
5276 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5277
5278 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5279 is_friend = true;
5280
5281 if (is_friend)
5282 /* For a friend, we want the context of the friend, not
5283 the type of which it is a friend. */
5284 ctx = CP_DECL_CONTEXT (decl);
5285 else if (CP_DECL_CONTEXT (decl)
5286 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5287 /* In the case of a virtual function, we want the class in which
5288 it is defined. */
5289 ctx = CP_DECL_CONTEXT (decl);
5290 else
5291 /* Otherwise, if we're currently defining some class, the DECL
5292 is assumed to be a member of the class. */
5293 ctx = current_scope ();
5294
5295 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5296 ctx = NULL_TREE;
5297
5298 if (!DECL_CONTEXT (decl))
5299 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5300
5301 /* See if this is a primary template. */
5302 if (is_friend && ctx
5303 && uses_template_parms_level (ctx, processing_template_decl))
5304 /* A friend template that specifies a class context, i.e.
5305 template <typename T> friend void A<T>::f();
5306 is not primary. */
5307 is_primary = false;
5308 else if (TREE_CODE (decl) == TYPE_DECL
5309 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5310 is_primary = false;
5311 else
5312 is_primary = template_parm_scope_p ();
5313
5314 if (is_primary)
5315 {
5316 warning (OPT_Wtemplates, "template %qD declared", decl);
5317
5318 if (DECL_CLASS_SCOPE_P (decl))
5319 member_template_p = true;
5320 if (TREE_CODE (decl) == TYPE_DECL
5321 && anon_aggrname_p (DECL_NAME (decl)))
5322 {
5323 error ("template class without a name");
5324 return error_mark_node;
5325 }
5326 else if (TREE_CODE (decl) == FUNCTION_DECL)
5327 {
5328 if (member_template_p)
5329 {
5330 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5331 error ("member template %qD may not have virt-specifiers", decl);
5332 }
5333 if (DECL_DESTRUCTOR_P (decl))
5334 {
5335 /* [temp.mem]
5336
5337 A destructor shall not be a member template. */
5338 error ("destructor %qD declared as member template", decl);
5339 return error_mark_node;
5340 }
5341 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5342 && (!prototype_p (TREE_TYPE (decl))
5343 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5344 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5345 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5346 == void_list_node)))
5347 {
5348 /* [basic.stc.dynamic.allocation]
5349
5350 An allocation function can be a function
5351 template. ... Template allocation functions shall
5352 have two or more parameters. */
5353 error ("invalid template declaration of %qD", decl);
5354 return error_mark_node;
5355 }
5356 }
5357 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5358 && CLASS_TYPE_P (TREE_TYPE (decl)))
5359 {
5360 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5361 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5362 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5363 {
5364 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5365 if (TREE_CODE (t) == TYPE_DECL)
5366 t = TREE_TYPE (t);
5367 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5368 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5369 }
5370 }
5371 else if (TREE_CODE (decl) == TYPE_DECL
5372 && TYPE_DECL_ALIAS_P (decl))
5373 /* alias-declaration */
5374 gcc_assert (!DECL_ARTIFICIAL (decl));
5375 else if (VAR_P (decl))
5376 /* C++14 variable template. */;
5377 else
5378 {
5379 error ("template declaration of %q#D", decl);
5380 return error_mark_node;
5381 }
5382 }
5383
5384 /* Check to see that the rules regarding the use of default
5385 arguments are not being violated. We check args for a friend
5386 functions when we know whether it's a definition, introducing
5387 declaration or re-declaration. */
5388 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5389 check_default_tmpl_args (decl, current_template_parms,
5390 is_primary, is_partial, is_friend);
5391
5392 /* Ensure that there are no parameter packs in the type of this
5393 declaration that have not been expanded. */
5394 if (TREE_CODE (decl) == FUNCTION_DECL)
5395 {
5396 /* Check each of the arguments individually to see if there are
5397 any bare parameter packs. */
5398 tree type = TREE_TYPE (decl);
5399 tree arg = DECL_ARGUMENTS (decl);
5400 tree argtype = TYPE_ARG_TYPES (type);
5401
5402 while (arg && argtype)
5403 {
5404 if (!DECL_PACK_P (arg)
5405 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5406 {
5407 /* This is a PARM_DECL that contains unexpanded parameter
5408 packs. We have already complained about this in the
5409 check_for_bare_parameter_packs call, so just replace
5410 these types with ERROR_MARK_NODE. */
5411 TREE_TYPE (arg) = error_mark_node;
5412 TREE_VALUE (argtype) = error_mark_node;
5413 }
5414
5415 arg = DECL_CHAIN (arg);
5416 argtype = TREE_CHAIN (argtype);
5417 }
5418
5419 /* Check for bare parameter packs in the return type and the
5420 exception specifiers. */
5421 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5422 /* Errors were already issued, set return type to int
5423 as the frontend doesn't expect error_mark_node as
5424 the return type. */
5425 TREE_TYPE (type) = integer_type_node;
5426 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5427 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5428 }
5429 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5430 && TYPE_DECL_ALIAS_P (decl))
5431 ? DECL_ORIGINAL_TYPE (decl)
5432 : TREE_TYPE (decl)))
5433 {
5434 TREE_TYPE (decl) = error_mark_node;
5435 return error_mark_node;
5436 }
5437
5438 if (is_partial)
5439 return process_partial_specialization (decl);
5440
5441 args = current_template_args ();
5442
5443 if (!ctx
5444 || TREE_CODE (ctx) == FUNCTION_DECL
5445 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5446 || (TREE_CODE (decl) == TYPE_DECL
5447 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5448 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5449 {
5450 if (DECL_LANG_SPECIFIC (decl)
5451 && DECL_TEMPLATE_INFO (decl)
5452 && DECL_TI_TEMPLATE (decl))
5453 tmpl = DECL_TI_TEMPLATE (decl);
5454 /* If DECL is a TYPE_DECL for a class-template, then there won't
5455 be DECL_LANG_SPECIFIC. The information equivalent to
5456 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5457 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5458 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5459 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5460 {
5461 /* Since a template declaration already existed for this
5462 class-type, we must be redeclaring it here. Make sure
5463 that the redeclaration is valid. */
5464 redeclare_class_template (TREE_TYPE (decl),
5465 current_template_parms,
5466 current_template_constraints ());
5467 /* We don't need to create a new TEMPLATE_DECL; just use the
5468 one we already had. */
5469 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5470 }
5471 else
5472 {
5473 tmpl = build_template_decl (decl, current_template_parms,
5474 member_template_p);
5475 new_template_p = 1;
5476
5477 if (DECL_LANG_SPECIFIC (decl)
5478 && DECL_TEMPLATE_SPECIALIZATION (decl))
5479 {
5480 /* A specialization of a member template of a template
5481 class. */
5482 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5483 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5484 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5485 }
5486 }
5487 }
5488 else
5489 {
5490 tree a, t, current, parms;
5491 int i;
5492 tree tinfo = get_template_info (decl);
5493
5494 if (!tinfo)
5495 {
5496 error ("template definition of non-template %q#D", decl);
5497 return error_mark_node;
5498 }
5499
5500 tmpl = TI_TEMPLATE (tinfo);
5501
5502 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5503 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5504 && DECL_TEMPLATE_SPECIALIZATION (decl)
5505 && DECL_MEMBER_TEMPLATE_P (tmpl))
5506 {
5507 tree new_tmpl;
5508
5509 /* The declaration is a specialization of a member
5510 template, declared outside the class. Therefore, the
5511 innermost template arguments will be NULL, so we
5512 replace them with the arguments determined by the
5513 earlier call to check_explicit_specialization. */
5514 args = DECL_TI_ARGS (decl);
5515
5516 new_tmpl
5517 = build_template_decl (decl, current_template_parms,
5518 member_template_p);
5519 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5520 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5521 DECL_TI_TEMPLATE (decl) = new_tmpl;
5522 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5523 DECL_TEMPLATE_INFO (new_tmpl)
5524 = build_template_info (tmpl, args);
5525
5526 register_specialization (new_tmpl,
5527 most_general_template (tmpl),
5528 args,
5529 is_friend, 0);
5530 return decl;
5531 }
5532
5533 /* Make sure the template headers we got make sense. */
5534
5535 parms = DECL_TEMPLATE_PARMS (tmpl);
5536 i = TMPL_PARMS_DEPTH (parms);
5537 if (TMPL_ARGS_DEPTH (args) != i)
5538 {
5539 error ("expected %d levels of template parms for %q#D, got %d",
5540 i, decl, TMPL_ARGS_DEPTH (args));
5541 DECL_INTERFACE_KNOWN (decl) = 1;
5542 return error_mark_node;
5543 }
5544 else
5545 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5546 {
5547 a = TMPL_ARGS_LEVEL (args, i);
5548 t = INNERMOST_TEMPLATE_PARMS (parms);
5549
5550 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5551 {
5552 if (current == decl)
5553 error ("got %d template parameters for %q#D",
5554 TREE_VEC_LENGTH (a), decl);
5555 else
5556 error ("got %d template parameters for %q#T",
5557 TREE_VEC_LENGTH (a), current);
5558 error (" but %d required", TREE_VEC_LENGTH (t));
5559 /* Avoid crash in import_export_decl. */
5560 DECL_INTERFACE_KNOWN (decl) = 1;
5561 return error_mark_node;
5562 }
5563
5564 if (current == decl)
5565 current = ctx;
5566 else if (current == NULL_TREE)
5567 /* Can happen in erroneous input. */
5568 break;
5569 else
5570 current = get_containing_scope (current);
5571 }
5572
5573 /* Check that the parms are used in the appropriate qualifying scopes
5574 in the declarator. */
5575 if (!comp_template_args
5576 (TI_ARGS (tinfo),
5577 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5578 {
5579 error ("template arguments to %qD do not match original "
5580 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5581 if (!uses_template_parms (TI_ARGS (tinfo)))
5582 inform (input_location, "use %<template<>%> for"
5583 " an explicit specialization");
5584 /* Avoid crash in import_export_decl. */
5585 DECL_INTERFACE_KNOWN (decl) = 1;
5586 return error_mark_node;
5587 }
5588 }
5589
5590 DECL_TEMPLATE_RESULT (tmpl) = decl;
5591 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5592
5593 /* Push template declarations for global functions and types. Note
5594 that we do not try to push a global template friend declared in a
5595 template class; such a thing may well depend on the template
5596 parameters of the class. */
5597 if (new_template_p && !ctx
5598 && !(is_friend && template_class_depth (current_class_type) > 0))
5599 {
5600 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5601 if (tmpl == error_mark_node)
5602 return error_mark_node;
5603
5604 /* Hide template friend classes that haven't been declared yet. */
5605 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5606 {
5607 DECL_ANTICIPATED (tmpl) = 1;
5608 DECL_FRIEND_P (tmpl) = 1;
5609 }
5610 }
5611
5612 if (is_primary)
5613 {
5614 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5615
5616 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5617
5618 /* Give template template parms a DECL_CONTEXT of the template
5619 for which they are a parameter. */
5620 parms = INNERMOST_TEMPLATE_PARMS (parms);
5621 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5622 {
5623 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5624 if (TREE_CODE (parm) == TEMPLATE_DECL)
5625 DECL_CONTEXT (parm) = tmpl;
5626 }
5627
5628 if (TREE_CODE (decl) == TYPE_DECL
5629 && TYPE_DECL_ALIAS_P (decl)
5630 && complex_alias_template_p (tmpl))
5631 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5632 }
5633
5634 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5635 back to its most general template. If TMPL is a specialization,
5636 ARGS may only have the innermost set of arguments. Add the missing
5637 argument levels if necessary. */
5638 if (DECL_TEMPLATE_INFO (tmpl))
5639 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5640
5641 info = build_template_info (tmpl, args);
5642
5643 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5644 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5645 else
5646 {
5647 if (is_primary)
5648 retrofit_lang_decl (decl);
5649 if (DECL_LANG_SPECIFIC (decl))
5650 DECL_TEMPLATE_INFO (decl) = info;
5651 }
5652
5653 if (flag_implicit_templates
5654 && !is_friend
5655 && TREE_PUBLIC (decl)
5656 && VAR_OR_FUNCTION_DECL_P (decl))
5657 /* Set DECL_COMDAT on template instantiations; if we force
5658 them to be emitted by explicit instantiation or -frepo,
5659 mark_needed will tell cgraph to do the right thing. */
5660 DECL_COMDAT (decl) = true;
5661
5662 return DECL_TEMPLATE_RESULT (tmpl);
5663 }
5664
5665 tree
5666 push_template_decl (tree decl)
5667 {
5668 return push_template_decl_real (decl, false);
5669 }
5670
5671 /* FN is an inheriting constructor that inherits from the constructor
5672 template INHERITED; turn FN into a constructor template with a matching
5673 template header. */
5674
5675 tree
5676 add_inherited_template_parms (tree fn, tree inherited)
5677 {
5678 tree inner_parms
5679 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5680 inner_parms = copy_node (inner_parms);
5681 tree parms
5682 = tree_cons (size_int (processing_template_decl + 1),
5683 inner_parms, current_template_parms);
5684 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5685 tree args = template_parms_to_args (parms);
5686 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5687 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5688 DECL_TEMPLATE_RESULT (tmpl) = fn;
5689 DECL_ARTIFICIAL (tmpl) = true;
5690 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5691 return tmpl;
5692 }
5693
5694 /* Called when a class template TYPE is redeclared with the indicated
5695 template PARMS, e.g.:
5696
5697 template <class T> struct S;
5698 template <class T> struct S {}; */
5699
5700 bool
5701 redeclare_class_template (tree type, tree parms, tree cons)
5702 {
5703 tree tmpl;
5704 tree tmpl_parms;
5705 int i;
5706
5707 if (!TYPE_TEMPLATE_INFO (type))
5708 {
5709 error ("%qT is not a template type", type);
5710 return false;
5711 }
5712
5713 tmpl = TYPE_TI_TEMPLATE (type);
5714 if (!PRIMARY_TEMPLATE_P (tmpl))
5715 /* The type is nested in some template class. Nothing to worry
5716 about here; there are no new template parameters for the nested
5717 type. */
5718 return true;
5719
5720 if (!parms)
5721 {
5722 error ("template specifiers not specified in declaration of %qD",
5723 tmpl);
5724 return false;
5725 }
5726
5727 parms = INNERMOST_TEMPLATE_PARMS (parms);
5728 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5729
5730 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5731 {
5732 error_n (input_location, TREE_VEC_LENGTH (parms),
5733 "redeclared with %d template parameter",
5734 "redeclared with %d template parameters",
5735 TREE_VEC_LENGTH (parms));
5736 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5737 "previous declaration %qD used %d template parameter",
5738 "previous declaration %qD used %d template parameters",
5739 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5740 return false;
5741 }
5742
5743 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5744 {
5745 tree tmpl_parm;
5746 tree parm;
5747 tree tmpl_default;
5748 tree parm_default;
5749
5750 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5751 || TREE_VEC_ELT (parms, i) == error_mark_node)
5752 continue;
5753
5754 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5755 if (error_operand_p (tmpl_parm))
5756 return false;
5757
5758 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5759 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5760 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5761
5762 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5763 TEMPLATE_DECL. */
5764 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5765 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5766 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5767 || (TREE_CODE (tmpl_parm) != PARM_DECL
5768 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5769 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5770 || (TREE_CODE (tmpl_parm) == PARM_DECL
5771 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5772 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5773 {
5774 error ("template parameter %q+#D", tmpl_parm);
5775 error ("redeclared here as %q#D", parm);
5776 return false;
5777 }
5778
5779 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5780 {
5781 /* We have in [temp.param]:
5782
5783 A template-parameter may not be given default arguments
5784 by two different declarations in the same scope. */
5785 error_at (input_location, "redefinition of default argument for %q#D", parm);
5786 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5787 "original definition appeared here");
5788 return false;
5789 }
5790
5791 if (parm_default != NULL_TREE)
5792 /* Update the previous template parameters (which are the ones
5793 that will really count) with the new default value. */
5794 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5795 else if (tmpl_default != NULL_TREE)
5796 /* Update the new parameters, too; they'll be used as the
5797 parameters for any members. */
5798 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5799
5800 /* Give each template template parm in this redeclaration a
5801 DECL_CONTEXT of the template for which they are a parameter. */
5802 if (TREE_CODE (parm) == TEMPLATE_DECL)
5803 {
5804 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5805 DECL_CONTEXT (parm) = tmpl;
5806 }
5807
5808 if (TREE_CODE (parm) == TYPE_DECL)
5809 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5810 }
5811
5812 // Cannot redeclare a class template with a different set of constraints.
5813 if (!equivalent_constraints (get_constraints (tmpl), cons))
5814 {
5815 error_at (input_location, "redeclaration %q#D with different "
5816 "constraints", tmpl);
5817 inform (DECL_SOURCE_LOCATION (tmpl),
5818 "original declaration appeared here");
5819 }
5820
5821 return true;
5822 }
5823
5824 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5825 to be used when the caller has already checked
5826 (processing_template_decl
5827 && !instantiation_dependent_expression_p (expr)
5828 && potential_constant_expression (expr))
5829 and cleared processing_template_decl. */
5830
5831 tree
5832 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5833 {
5834 return tsubst_copy_and_build (expr,
5835 /*args=*/NULL_TREE,
5836 complain,
5837 /*in_decl=*/NULL_TREE,
5838 /*function_p=*/false,
5839 /*integral_constant_expression_p=*/true);
5840 }
5841
5842 /* Simplify EXPR if it is a non-dependent expression. Returns the
5843 (possibly simplified) expression. */
5844
5845 tree
5846 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5847 {
5848 if (expr == NULL_TREE)
5849 return NULL_TREE;
5850
5851 /* If we're in a template, but EXPR isn't value dependent, simplify
5852 it. We're supposed to treat:
5853
5854 template <typename T> void f(T[1 + 1]);
5855 template <typename T> void f(T[2]);
5856
5857 as two declarations of the same function, for example. */
5858 if (processing_template_decl
5859 && is_nondependent_constant_expression (expr))
5860 {
5861 processing_template_decl_sentinel s;
5862 expr = instantiate_non_dependent_expr_internal (expr, complain);
5863 }
5864 return expr;
5865 }
5866
5867 tree
5868 instantiate_non_dependent_expr (tree expr)
5869 {
5870 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5871 }
5872
5873 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5874 an uninstantiated expression. */
5875
5876 tree
5877 instantiate_non_dependent_or_null (tree expr)
5878 {
5879 if (expr == NULL_TREE)
5880 return NULL_TREE;
5881 if (processing_template_decl)
5882 {
5883 if (!is_nondependent_constant_expression (expr))
5884 expr = NULL_TREE;
5885 else
5886 {
5887 processing_template_decl_sentinel s;
5888 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5889 }
5890 }
5891 return expr;
5892 }
5893
5894 /* True iff T is a specialization of a variable template. */
5895
5896 bool
5897 variable_template_specialization_p (tree t)
5898 {
5899 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5900 return false;
5901 tree tmpl = DECL_TI_TEMPLATE (t);
5902 return variable_template_p (tmpl);
5903 }
5904
5905 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5906 template declaration, or a TYPE_DECL for an alias declaration. */
5907
5908 bool
5909 alias_type_or_template_p (tree t)
5910 {
5911 if (t == NULL_TREE)
5912 return false;
5913 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5914 || (TYPE_P (t)
5915 && TYPE_NAME (t)
5916 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5917 || DECL_ALIAS_TEMPLATE_P (t));
5918 }
5919
5920 /* Return TRUE iff T is a specialization of an alias template. */
5921
5922 bool
5923 alias_template_specialization_p (const_tree t)
5924 {
5925 /* It's an alias template specialization if it's an alias and its
5926 TYPE_NAME is a specialization of a primary template. */
5927 if (TYPE_ALIAS_P (t))
5928 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5929 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5930
5931 return false;
5932 }
5933
5934 /* An alias template is complex from a SFINAE perspective if a template-id
5935 using that alias can be ill-formed when the expansion is not, as with
5936 the void_t template. We determine this by checking whether the
5937 expansion for the alias template uses all its template parameters. */
5938
5939 struct uses_all_template_parms_data
5940 {
5941 int level;
5942 bool *seen;
5943 };
5944
5945 static int
5946 uses_all_template_parms_r (tree t, void *data_)
5947 {
5948 struct uses_all_template_parms_data &data
5949 = *(struct uses_all_template_parms_data*)data_;
5950 tree idx = get_template_parm_index (t);
5951
5952 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5953 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5954 return 0;
5955 }
5956
5957 static bool
5958 complex_alias_template_p (const_tree tmpl)
5959 {
5960 struct uses_all_template_parms_data data;
5961 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5962 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5963 data.level = TMPL_PARMS_DEPTH (parms);
5964 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5965 data.seen = XALLOCAVEC (bool, len);
5966 for (int i = 0; i < len; ++i)
5967 data.seen[i] = false;
5968
5969 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5970 for (int i = 0; i < len; ++i)
5971 if (!data.seen[i])
5972 return true;
5973 return false;
5974 }
5975
5976 /* Return TRUE iff T is a specialization of a complex alias template with
5977 dependent template-arguments. */
5978
5979 bool
5980 dependent_alias_template_spec_p (const_tree t)
5981 {
5982 if (!alias_template_specialization_p (t))
5983 return false;
5984
5985 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5986 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5987 return false;
5988
5989 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5990 if (!any_dependent_template_arguments_p (args))
5991 return false;
5992
5993 return true;
5994 }
5995
5996 /* Return the number of innermost template parameters in TMPL. */
5997
5998 static int
5999 num_innermost_template_parms (tree tmpl)
6000 {
6001 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6002 return TREE_VEC_LENGTH (parms);
6003 }
6004
6005 /* Return either TMPL or another template that it is equivalent to under DR
6006 1286: An alias that just changes the name of a template is equivalent to
6007 the other template. */
6008
6009 static tree
6010 get_underlying_template (tree tmpl)
6011 {
6012 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6013 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6014 {
6015 /* Determine if the alias is equivalent to an underlying template. */
6016 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6017 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6018 if (!tinfo)
6019 break;
6020
6021 tree underlying = TI_TEMPLATE (tinfo);
6022 if (!PRIMARY_TEMPLATE_P (underlying)
6023 || (num_innermost_template_parms (tmpl)
6024 != num_innermost_template_parms (underlying)))
6025 break;
6026
6027 tree alias_args = INNERMOST_TEMPLATE_ARGS
6028 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6029 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6030 break;
6031
6032 /* Alias is equivalent. Strip it and repeat. */
6033 tmpl = underlying;
6034 }
6035
6036 return tmpl;
6037 }
6038
6039 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6040 must be a reference-to-function or a pointer-to-function type, as specified
6041 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6042 and check that the resulting function has external linkage. */
6043
6044 static tree
6045 convert_nontype_argument_function (tree type, tree expr,
6046 tsubst_flags_t complain)
6047 {
6048 tree fns = expr;
6049 tree fn, fn_no_ptr;
6050 linkage_kind linkage;
6051
6052 fn = instantiate_type (type, fns, tf_none);
6053 if (fn == error_mark_node)
6054 return error_mark_node;
6055
6056 if (value_dependent_expression_p (fn))
6057 goto accept;
6058
6059 fn_no_ptr = strip_fnptr_conv (fn);
6060 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6061 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6062 if (BASELINK_P (fn_no_ptr))
6063 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6064
6065 /* [temp.arg.nontype]/1
6066
6067 A template-argument for a non-type, non-template template-parameter
6068 shall be one of:
6069 [...]
6070 -- the address of an object or function with external [C++11: or
6071 internal] linkage. */
6072
6073 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6074 {
6075 if (complain & tf_error)
6076 {
6077 error ("%qE is not a valid template argument for type %qT",
6078 expr, type);
6079 if (TYPE_PTR_P (type))
6080 inform (input_location, "it must be the address of a function "
6081 "with external linkage");
6082 else
6083 inform (input_location, "it must be the name of a function with "
6084 "external linkage");
6085 }
6086 return NULL_TREE;
6087 }
6088
6089 linkage = decl_linkage (fn_no_ptr);
6090 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6091 {
6092 if (complain & tf_error)
6093 {
6094 if (cxx_dialect >= cxx11)
6095 error ("%qE is not a valid template argument for type %qT "
6096 "because %qD has no linkage",
6097 expr, type, fn_no_ptr);
6098 else
6099 error ("%qE is not a valid template argument for type %qT "
6100 "because %qD does not have external linkage",
6101 expr, type, fn_no_ptr);
6102 }
6103 return NULL_TREE;
6104 }
6105
6106 accept:
6107 if (TREE_CODE (type) == REFERENCE_TYPE)
6108 fn = build_address (fn);
6109 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6110 fn = build_nop (type, fn);
6111
6112 return fn;
6113 }
6114
6115 /* Subroutine of convert_nontype_argument.
6116 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6117 Emit an error otherwise. */
6118
6119 static bool
6120 check_valid_ptrmem_cst_expr (tree type, tree expr,
6121 tsubst_flags_t complain)
6122 {
6123 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6124 tree orig_expr = expr;
6125 STRIP_NOPS (expr);
6126 if (null_ptr_cst_p (expr))
6127 return true;
6128 if (TREE_CODE (expr) == PTRMEM_CST
6129 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6130 PTRMEM_CST_CLASS (expr)))
6131 return true;
6132 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6133 return true;
6134 if (processing_template_decl
6135 && TREE_CODE (expr) == ADDR_EXPR
6136 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6137 return true;
6138 if (complain & tf_error)
6139 {
6140 error_at (loc, "%qE is not a valid template argument for type %qT",
6141 orig_expr, type);
6142 if (TREE_CODE (expr) != PTRMEM_CST)
6143 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6144 else
6145 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6146 }
6147 return false;
6148 }
6149
6150 /* Returns TRUE iff the address of OP is value-dependent.
6151
6152 14.6.2.4 [temp.dep.temp]:
6153 A non-integral non-type template-argument is dependent if its type is
6154 dependent or it has either of the following forms
6155 qualified-id
6156 & qualified-id
6157 and contains a nested-name-specifier which specifies a class-name that
6158 names a dependent type.
6159
6160 We generalize this to just say that the address of a member of a
6161 dependent class is value-dependent; the above doesn't cover the
6162 address of a static data member named with an unqualified-id. */
6163
6164 static bool
6165 has_value_dependent_address (tree op)
6166 {
6167 /* We could use get_inner_reference here, but there's no need;
6168 this is only relevant for template non-type arguments, which
6169 can only be expressed as &id-expression. */
6170 if (DECL_P (op))
6171 {
6172 tree ctx = CP_DECL_CONTEXT (op);
6173 if (TYPE_P (ctx) && dependent_type_p (ctx))
6174 return true;
6175 }
6176
6177 return false;
6178 }
6179
6180 /* The next set of functions are used for providing helpful explanatory
6181 diagnostics for failed overload resolution. Their messages should be
6182 indented by two spaces for consistency with the messages in
6183 call.c */
6184
6185 static int
6186 unify_success (bool /*explain_p*/)
6187 {
6188 return 0;
6189 }
6190
6191 /* Other failure functions should call this one, to provide a single function
6192 for setting a breakpoint on. */
6193
6194 static int
6195 unify_invalid (bool /*explain_p*/)
6196 {
6197 return 1;
6198 }
6199
6200 static int
6201 unify_parameter_deduction_failure (bool explain_p, tree parm)
6202 {
6203 if (explain_p)
6204 inform (input_location,
6205 " couldn't deduce template parameter %qD", parm);
6206 return unify_invalid (explain_p);
6207 }
6208
6209 static int
6210 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6211 {
6212 if (explain_p)
6213 inform (input_location,
6214 " types %qT and %qT have incompatible cv-qualifiers",
6215 parm, arg);
6216 return unify_invalid (explain_p);
6217 }
6218
6219 static int
6220 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6221 {
6222 if (explain_p)
6223 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6224 return unify_invalid (explain_p);
6225 }
6226
6227 static int
6228 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6229 {
6230 if (explain_p)
6231 inform (input_location,
6232 " template parameter %qD is not a parameter pack, but "
6233 "argument %qD is",
6234 parm, arg);
6235 return unify_invalid (explain_p);
6236 }
6237
6238 static int
6239 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6240 {
6241 if (explain_p)
6242 inform (input_location,
6243 " template argument %qE does not match "
6244 "pointer-to-member constant %qE",
6245 arg, parm);
6246 return unify_invalid (explain_p);
6247 }
6248
6249 static int
6250 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6251 {
6252 if (explain_p)
6253 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6254 return unify_invalid (explain_p);
6255 }
6256
6257 static int
6258 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6259 {
6260 if (explain_p)
6261 inform (input_location,
6262 " inconsistent parameter pack deduction with %qT and %qT",
6263 old_arg, new_arg);
6264 return unify_invalid (explain_p);
6265 }
6266
6267 static int
6268 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6269 {
6270 if (explain_p)
6271 {
6272 if (TYPE_P (parm))
6273 inform (input_location,
6274 " deduced conflicting types for parameter %qT (%qT and %qT)",
6275 parm, first, second);
6276 else
6277 inform (input_location,
6278 " deduced conflicting values for non-type parameter "
6279 "%qE (%qE and %qE)", parm, first, second);
6280 }
6281 return unify_invalid (explain_p);
6282 }
6283
6284 static int
6285 unify_vla_arg (bool explain_p, tree arg)
6286 {
6287 if (explain_p)
6288 inform (input_location,
6289 " variable-sized array type %qT is not "
6290 "a valid template argument",
6291 arg);
6292 return unify_invalid (explain_p);
6293 }
6294
6295 static int
6296 unify_method_type_error (bool explain_p, tree arg)
6297 {
6298 if (explain_p)
6299 inform (input_location,
6300 " member function type %qT is not a valid template argument",
6301 arg);
6302 return unify_invalid (explain_p);
6303 }
6304
6305 static int
6306 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6307 {
6308 if (explain_p)
6309 {
6310 if (least_p)
6311 inform_n (input_location, wanted,
6312 " candidate expects at least %d argument, %d provided",
6313 " candidate expects at least %d arguments, %d provided",
6314 wanted, have);
6315 else
6316 inform_n (input_location, wanted,
6317 " candidate expects %d argument, %d provided",
6318 " candidate expects %d arguments, %d provided",
6319 wanted, have);
6320 }
6321 return unify_invalid (explain_p);
6322 }
6323
6324 static int
6325 unify_too_many_arguments (bool explain_p, int have, int wanted)
6326 {
6327 return unify_arity (explain_p, have, wanted);
6328 }
6329
6330 static int
6331 unify_too_few_arguments (bool explain_p, int have, int wanted,
6332 bool least_p = false)
6333 {
6334 return unify_arity (explain_p, have, wanted, least_p);
6335 }
6336
6337 static int
6338 unify_arg_conversion (bool explain_p, tree to_type,
6339 tree from_type, tree arg)
6340 {
6341 if (explain_p)
6342 inform (EXPR_LOC_OR_LOC (arg, input_location),
6343 " cannot convert %qE (type %qT) to type %qT",
6344 arg, from_type, to_type);
6345 return unify_invalid (explain_p);
6346 }
6347
6348 static int
6349 unify_no_common_base (bool explain_p, enum template_base_result r,
6350 tree parm, tree arg)
6351 {
6352 if (explain_p)
6353 switch (r)
6354 {
6355 case tbr_ambiguous_baseclass:
6356 inform (input_location, " %qT is an ambiguous base class of %qT",
6357 parm, arg);
6358 break;
6359 default:
6360 inform (input_location, " %qT is not derived from %qT", arg, parm);
6361 break;
6362 }
6363 return unify_invalid (explain_p);
6364 }
6365
6366 static int
6367 unify_inconsistent_template_template_parameters (bool explain_p)
6368 {
6369 if (explain_p)
6370 inform (input_location,
6371 " template parameters of a template template argument are "
6372 "inconsistent with other deduced template arguments");
6373 return unify_invalid (explain_p);
6374 }
6375
6376 static int
6377 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6378 {
6379 if (explain_p)
6380 inform (input_location,
6381 " can't deduce a template for %qT from non-template type %qT",
6382 parm, arg);
6383 return unify_invalid (explain_p);
6384 }
6385
6386 static int
6387 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6388 {
6389 if (explain_p)
6390 inform (input_location,
6391 " template argument %qE does not match %qE", arg, parm);
6392 return unify_invalid (explain_p);
6393 }
6394
6395 /* Attempt to convert the non-type template parameter EXPR to the
6396 indicated TYPE. If the conversion is successful, return the
6397 converted value. If the conversion is unsuccessful, return
6398 NULL_TREE if we issued an error message, or error_mark_node if we
6399 did not. We issue error messages for out-and-out bad template
6400 parameters, but not simply because the conversion failed, since we
6401 might be just trying to do argument deduction. Both TYPE and EXPR
6402 must be non-dependent.
6403
6404 The conversion follows the special rules described in
6405 [temp.arg.nontype], and it is much more strict than an implicit
6406 conversion.
6407
6408 This function is called twice for each template argument (see
6409 lookup_template_class for a more accurate description of this
6410 problem). This means that we need to handle expressions which
6411 are not valid in a C++ source, but can be created from the
6412 first call (for instance, casts to perform conversions). These
6413 hacks can go away after we fix the double coercion problem. */
6414
6415 static tree
6416 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6417 {
6418 tree expr_type;
6419 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6420 tree orig_expr = expr;
6421
6422 /* Detect immediately string literals as invalid non-type argument.
6423 This special-case is not needed for correctness (we would easily
6424 catch this later), but only to provide better diagnostic for this
6425 common user mistake. As suggested by DR 100, we do not mention
6426 linkage issues in the diagnostic as this is not the point. */
6427 /* FIXME we're making this OK. */
6428 if (TREE_CODE (expr) == STRING_CST)
6429 {
6430 if (complain & tf_error)
6431 error ("%qE is not a valid template argument for type %qT "
6432 "because string literals can never be used in this context",
6433 expr, type);
6434 return NULL_TREE;
6435 }
6436
6437 /* Add the ADDR_EXPR now for the benefit of
6438 value_dependent_expression_p. */
6439 if (TYPE_PTROBV_P (type)
6440 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6441 {
6442 expr = decay_conversion (expr, complain);
6443 if (expr == error_mark_node)
6444 return error_mark_node;
6445 }
6446
6447 /* If we are in a template, EXPR may be non-dependent, but still
6448 have a syntactic, rather than semantic, form. For example, EXPR
6449 might be a SCOPE_REF, rather than the VAR_DECL to which the
6450 SCOPE_REF refers. Preserving the qualifying scope is necessary
6451 so that access checking can be performed when the template is
6452 instantiated -- but here we need the resolved form so that we can
6453 convert the argument. */
6454 bool non_dep = false;
6455 if (TYPE_REF_OBJ_P (type)
6456 && has_value_dependent_address (expr))
6457 /* If we want the address and it's value-dependent, don't fold. */;
6458 else if (processing_template_decl
6459 && is_nondependent_constant_expression (expr))
6460 non_dep = true;
6461 if (error_operand_p (expr))
6462 return error_mark_node;
6463 expr_type = TREE_TYPE (expr);
6464
6465 /* If the argument is non-dependent, perform any conversions in
6466 non-dependent context as well. */
6467 processing_template_decl_sentinel s (non_dep);
6468 if (non_dep)
6469 expr = instantiate_non_dependent_expr_internal (expr, complain);
6470
6471 if (value_dependent_expression_p (expr))
6472 expr = canonicalize_expr_argument (expr, complain);
6473
6474 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6475 to a non-type argument of "nullptr". */
6476 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6477 expr = fold_simple (convert (type, expr));
6478
6479 /* In C++11, integral or enumeration non-type template arguments can be
6480 arbitrary constant expressions. Pointer and pointer to
6481 member arguments can be general constant expressions that evaluate
6482 to a null value, but otherwise still need to be of a specific form. */
6483 if (cxx_dialect >= cxx11)
6484 {
6485 if (TREE_CODE (expr) == PTRMEM_CST)
6486 /* A PTRMEM_CST is already constant, and a valid template
6487 argument for a parameter of pointer to member type, we just want
6488 to leave it in that form rather than lower it to a
6489 CONSTRUCTOR. */;
6490 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6491 || cxx_dialect >= cxx17)
6492 {
6493 /* C++17: A template-argument for a non-type template-parameter shall
6494 be a converted constant expression (8.20) of the type of the
6495 template-parameter. */
6496 expr = build_converted_constant_expr (type, expr, complain);
6497 if (expr == error_mark_node)
6498 return error_mark_node;
6499 expr = maybe_constant_value (expr);
6500 expr = convert_from_reference (expr);
6501 }
6502 else if (TYPE_PTR_OR_PTRMEM_P (type))
6503 {
6504 tree folded = maybe_constant_value (expr);
6505 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6506 : null_member_pointer_value_p (folded))
6507 expr = folded;
6508 }
6509 }
6510
6511 if (TREE_CODE (type) == REFERENCE_TYPE)
6512 expr = mark_lvalue_use (expr);
6513 else
6514 expr = mark_rvalue_use (expr);
6515
6516 /* HACK: Due to double coercion, we can get a
6517 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6518 which is the tree that we built on the first call (see
6519 below when coercing to reference to object or to reference to
6520 function). We just strip everything and get to the arg.
6521 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6522 for examples. */
6523 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6524 {
6525 tree probe_type, probe = expr;
6526 if (REFERENCE_REF_P (probe))
6527 probe = TREE_OPERAND (probe, 0);
6528 probe_type = TREE_TYPE (probe);
6529 if (TREE_CODE (probe) == NOP_EXPR)
6530 {
6531 /* ??? Maybe we could use convert_from_reference here, but we
6532 would need to relax its constraints because the NOP_EXPR
6533 could actually change the type to something more cv-qualified,
6534 and this is not folded by convert_from_reference. */
6535 tree addr = TREE_OPERAND (probe, 0);
6536 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6537 && TREE_CODE (addr) == ADDR_EXPR
6538 && TYPE_PTR_P (TREE_TYPE (addr))
6539 && (same_type_ignoring_top_level_qualifiers_p
6540 (TREE_TYPE (probe_type),
6541 TREE_TYPE (TREE_TYPE (addr)))))
6542 {
6543 expr = TREE_OPERAND (addr, 0);
6544 expr_type = TREE_TYPE (probe_type);
6545 }
6546 }
6547 }
6548
6549 /* [temp.arg.nontype]/5, bullet 1
6550
6551 For a non-type template-parameter of integral or enumeration type,
6552 integral promotions (_conv.prom_) and integral conversions
6553 (_conv.integral_) are applied. */
6554 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6555 {
6556 if (cxx_dialect < cxx11)
6557 {
6558 tree t = build_converted_constant_expr (type, expr, complain);
6559 t = maybe_constant_value (t);
6560 if (t != error_mark_node)
6561 expr = t;
6562 }
6563
6564 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6565 return error_mark_node;
6566
6567 /* Notice that there are constant expressions like '4 % 0' which
6568 do not fold into integer constants. */
6569 if (TREE_CODE (expr) != INTEGER_CST
6570 && !value_dependent_expression_p (expr))
6571 {
6572 if (complain & tf_error)
6573 {
6574 int errs = errorcount, warns = warningcount + werrorcount;
6575 if (!require_potential_constant_expression (expr))
6576 expr = error_mark_node;
6577 else
6578 expr = cxx_constant_value (expr);
6579 if (errorcount > errs || warningcount + werrorcount > warns)
6580 inform (loc, "in template argument for type %qT ", type);
6581 if (expr == error_mark_node)
6582 return NULL_TREE;
6583 /* else cxx_constant_value complained but gave us
6584 a real constant, so go ahead. */
6585 if (TREE_CODE (expr) != INTEGER_CST)
6586 {
6587 /* Some assemble time constant expressions like
6588 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6589 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6590 as we can emit them into .rodata initializers of
6591 variables, yet they can't fold into an INTEGER_CST at
6592 compile time. Refuse them here. */
6593 gcc_checking_assert (reduced_constant_expression_p (expr));
6594 error_at (loc, "template argument %qE for type %qT not "
6595 "a constant integer", expr, type);
6596 return NULL_TREE;
6597 }
6598 }
6599 else
6600 return NULL_TREE;
6601 }
6602
6603 /* Avoid typedef problems. */
6604 if (TREE_TYPE (expr) != type)
6605 expr = fold_convert (type, expr);
6606 }
6607 /* [temp.arg.nontype]/5, bullet 2
6608
6609 For a non-type template-parameter of type pointer to object,
6610 qualification conversions (_conv.qual_) and the array-to-pointer
6611 conversion (_conv.array_) are applied. */
6612 else if (TYPE_PTROBV_P (type))
6613 {
6614 tree decayed = expr;
6615
6616 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6617 decay_conversion or an explicit cast. If it's a problematic cast,
6618 we'll complain about it below. */
6619 if (TREE_CODE (expr) == NOP_EXPR)
6620 {
6621 tree probe = expr;
6622 STRIP_NOPS (probe);
6623 if (TREE_CODE (probe) == ADDR_EXPR
6624 && TYPE_PTR_P (TREE_TYPE (probe)))
6625 {
6626 expr = probe;
6627 expr_type = TREE_TYPE (expr);
6628 }
6629 }
6630
6631 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6632
6633 A template-argument for a non-type, non-template template-parameter
6634 shall be one of: [...]
6635
6636 -- the name of a non-type template-parameter;
6637 -- the address of an object or function with external linkage, [...]
6638 expressed as "& id-expression" where the & is optional if the name
6639 refers to a function or array, or if the corresponding
6640 template-parameter is a reference.
6641
6642 Here, we do not care about functions, as they are invalid anyway
6643 for a parameter of type pointer-to-object. */
6644
6645 if (value_dependent_expression_p (expr))
6646 /* Non-type template parameters are OK. */
6647 ;
6648 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6649 /* Null pointer values are OK in C++11. */;
6650 else if (TREE_CODE (expr) != ADDR_EXPR)
6651 {
6652 if (VAR_P (expr))
6653 {
6654 if (complain & tf_error)
6655 error ("%qD is not a valid template argument "
6656 "because %qD is a variable, not the address of "
6657 "a variable", orig_expr, expr);
6658 return NULL_TREE;
6659 }
6660 if (POINTER_TYPE_P (expr_type))
6661 {
6662 if (complain & tf_error)
6663 error ("%qE is not a valid template argument for %qT "
6664 "because it is not the address of a variable",
6665 orig_expr, type);
6666 return NULL_TREE;
6667 }
6668 /* Other values, like integer constants, might be valid
6669 non-type arguments of some other type. */
6670 return error_mark_node;
6671 }
6672 else
6673 {
6674 tree decl = TREE_OPERAND (expr, 0);
6675
6676 if (!VAR_P (decl))
6677 {
6678 if (complain & tf_error)
6679 error ("%qE is not a valid template argument of type %qT "
6680 "because %qE is not a variable", orig_expr, type, decl);
6681 return NULL_TREE;
6682 }
6683 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6684 {
6685 if (complain & tf_error)
6686 error ("%qE is not a valid template argument of type %qT "
6687 "because %qD does not have external linkage",
6688 orig_expr, type, decl);
6689 return NULL_TREE;
6690 }
6691 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6692 && decl_linkage (decl) == lk_none)
6693 {
6694 if (complain & tf_error)
6695 error ("%qE is not a valid template argument of type %qT "
6696 "because %qD has no linkage", orig_expr, type, decl);
6697 return NULL_TREE;
6698 }
6699 /* C++17: For a non-type template-parameter of reference or pointer
6700 type, the value of the constant expression shall not refer to (or
6701 for a pointer type, shall not be the address of):
6702 * a subobject (4.5),
6703 * a temporary object (15.2),
6704 * a string literal (5.13.5),
6705 * the result of a typeid expression (8.2.8), or
6706 * a predefined __func__ variable (11.4.1). */
6707 else if (DECL_ARTIFICIAL (decl))
6708 {
6709 if (complain & tf_error)
6710 error ("the address of %qD is not a valid template argument",
6711 decl);
6712 return NULL_TREE;
6713 }
6714 else if (!same_type_ignoring_top_level_qualifiers_p
6715 (strip_array_types (TREE_TYPE (type)),
6716 strip_array_types (TREE_TYPE (decl))))
6717 {
6718 if (complain & tf_error)
6719 error ("the address of the %qT subobject of %qD is not a "
6720 "valid template argument", TREE_TYPE (type), decl);
6721 return NULL_TREE;
6722 }
6723 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6724 {
6725 if (complain & tf_error)
6726 error ("the address of %qD is not a valid template argument "
6727 "because it does not have static storage duration",
6728 decl);
6729 return NULL_TREE;
6730 }
6731 }
6732
6733 expr = decayed;
6734
6735 expr = perform_qualification_conversions (type, expr);
6736 if (expr == error_mark_node)
6737 return error_mark_node;
6738 }
6739 /* [temp.arg.nontype]/5, bullet 3
6740
6741 For a non-type template-parameter of type reference to object, no
6742 conversions apply. The type referred to by the reference may be more
6743 cv-qualified than the (otherwise identical) type of the
6744 template-argument. The template-parameter is bound directly to the
6745 template-argument, which must be an lvalue. */
6746 else if (TYPE_REF_OBJ_P (type))
6747 {
6748 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6749 expr_type))
6750 return error_mark_node;
6751
6752 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6753 {
6754 if (complain & tf_error)
6755 error ("%qE is not a valid template argument for type %qT "
6756 "because of conflicts in cv-qualification", expr, type);
6757 return NULL_TREE;
6758 }
6759
6760 if (!lvalue_p (expr))
6761 {
6762 if (complain & tf_error)
6763 error ("%qE is not a valid template argument for type %qT "
6764 "because it is not an lvalue", expr, type);
6765 return NULL_TREE;
6766 }
6767
6768 /* [temp.arg.nontype]/1
6769
6770 A template-argument for a non-type, non-template template-parameter
6771 shall be one of: [...]
6772
6773 -- the address of an object or function with external linkage. */
6774 if (INDIRECT_REF_P (expr)
6775 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6776 {
6777 expr = TREE_OPERAND (expr, 0);
6778 if (DECL_P (expr))
6779 {
6780 if (complain & tf_error)
6781 error ("%q#D is not a valid template argument for type %qT "
6782 "because a reference variable does not have a constant "
6783 "address", expr, type);
6784 return NULL_TREE;
6785 }
6786 }
6787
6788 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6789 && value_dependent_expression_p (expr))
6790 /* OK, dependent reference. We don't want to ask whether a DECL is
6791 itself value-dependent, since what we want here is its address. */;
6792 else
6793 {
6794 if (!DECL_P (expr))
6795 {
6796 if (complain & tf_error)
6797 error ("%qE is not a valid template argument for type %qT "
6798 "because it is not an object with linkage",
6799 expr, type);
6800 return NULL_TREE;
6801 }
6802
6803 /* DR 1155 allows internal linkage in C++11 and up. */
6804 linkage_kind linkage = decl_linkage (expr);
6805 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6806 {
6807 if (complain & tf_error)
6808 error ("%qE is not a valid template argument for type %qT "
6809 "because object %qD does not have linkage",
6810 expr, type, expr);
6811 return NULL_TREE;
6812 }
6813
6814 expr = build_address (expr);
6815 }
6816
6817 if (!same_type_p (type, TREE_TYPE (expr)))
6818 expr = build_nop (type, expr);
6819 }
6820 /* [temp.arg.nontype]/5, bullet 4
6821
6822 For a non-type template-parameter of type pointer to function, only
6823 the function-to-pointer conversion (_conv.func_) is applied. If the
6824 template-argument represents a set of overloaded functions (or a
6825 pointer to such), the matching function is selected from the set
6826 (_over.over_). */
6827 else if (TYPE_PTRFN_P (type))
6828 {
6829 /* If the argument is a template-id, we might not have enough
6830 context information to decay the pointer. */
6831 if (!type_unknown_p (expr_type))
6832 {
6833 expr = decay_conversion (expr, complain);
6834 if (expr == error_mark_node)
6835 return error_mark_node;
6836 }
6837
6838 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6839 /* Null pointer values are OK in C++11. */
6840 return perform_qualification_conversions (type, expr);
6841
6842 expr = convert_nontype_argument_function (type, expr, complain);
6843 if (!expr || expr == error_mark_node)
6844 return expr;
6845 }
6846 /* [temp.arg.nontype]/5, bullet 5
6847
6848 For a non-type template-parameter of type reference to function, no
6849 conversions apply. If the template-argument represents a set of
6850 overloaded functions, the matching function is selected from the set
6851 (_over.over_). */
6852 else if (TYPE_REFFN_P (type))
6853 {
6854 if (TREE_CODE (expr) == ADDR_EXPR)
6855 {
6856 if (complain & tf_error)
6857 {
6858 error ("%qE is not a valid template argument for type %qT "
6859 "because it is a pointer", expr, type);
6860 inform (input_location, "try using %qE instead",
6861 TREE_OPERAND (expr, 0));
6862 }
6863 return NULL_TREE;
6864 }
6865
6866 expr = convert_nontype_argument_function (type, expr, complain);
6867 if (!expr || expr == error_mark_node)
6868 return expr;
6869 }
6870 /* [temp.arg.nontype]/5, bullet 6
6871
6872 For a non-type template-parameter of type pointer to member function,
6873 no conversions apply. If the template-argument represents a set of
6874 overloaded member functions, the matching member function is selected
6875 from the set (_over.over_). */
6876 else if (TYPE_PTRMEMFUNC_P (type))
6877 {
6878 expr = instantiate_type (type, expr, tf_none);
6879 if (expr == error_mark_node)
6880 return error_mark_node;
6881
6882 /* [temp.arg.nontype] bullet 1 says the pointer to member
6883 expression must be a pointer-to-member constant. */
6884 if (!value_dependent_expression_p (expr)
6885 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6886 return NULL_TREE;
6887
6888 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6889 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6890 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6891 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6892 }
6893 /* [temp.arg.nontype]/5, bullet 7
6894
6895 For a non-type template-parameter of type pointer to data member,
6896 qualification conversions (_conv.qual_) are applied. */
6897 else if (TYPE_PTRDATAMEM_P (type))
6898 {
6899 /* [temp.arg.nontype] bullet 1 says the pointer to member
6900 expression must be a pointer-to-member constant. */
6901 if (!value_dependent_expression_p (expr)
6902 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6903 return NULL_TREE;
6904
6905 expr = perform_qualification_conversions (type, expr);
6906 if (expr == error_mark_node)
6907 return expr;
6908 }
6909 else if (NULLPTR_TYPE_P (type))
6910 {
6911 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6912 {
6913 if (complain & tf_error)
6914 error ("%qE is not a valid template argument for type %qT "
6915 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6916 return NULL_TREE;
6917 }
6918 return expr;
6919 }
6920 /* A template non-type parameter must be one of the above. */
6921 else
6922 gcc_unreachable ();
6923
6924 /* Sanity check: did we actually convert the argument to the
6925 right type? */
6926 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6927 (type, TREE_TYPE (expr)));
6928 return convert_from_reference (expr);
6929 }
6930
6931 /* Subroutine of coerce_template_template_parms, which returns 1 if
6932 PARM_PARM and ARG_PARM match using the rule for the template
6933 parameters of template template parameters. Both PARM and ARG are
6934 template parameters; the rest of the arguments are the same as for
6935 coerce_template_template_parms.
6936 */
6937 static int
6938 coerce_template_template_parm (tree parm,
6939 tree arg,
6940 tsubst_flags_t complain,
6941 tree in_decl,
6942 tree outer_args)
6943 {
6944 if (arg == NULL_TREE || error_operand_p (arg)
6945 || parm == NULL_TREE || error_operand_p (parm))
6946 return 0;
6947
6948 if (TREE_CODE (arg) != TREE_CODE (parm))
6949 return 0;
6950
6951 switch (TREE_CODE (parm))
6952 {
6953 case TEMPLATE_DECL:
6954 /* We encounter instantiations of templates like
6955 template <template <template <class> class> class TT>
6956 class C; */
6957 {
6958 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6959 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6960
6961 if (!coerce_template_template_parms
6962 (parmparm, argparm, complain, in_decl, outer_args))
6963 return 0;
6964 }
6965 /* Fall through. */
6966
6967 case TYPE_DECL:
6968 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6969 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6970 /* Argument is a parameter pack but parameter is not. */
6971 return 0;
6972 break;
6973
6974 case PARM_DECL:
6975 /* The tsubst call is used to handle cases such as
6976
6977 template <int> class C {};
6978 template <class T, template <T> class TT> class D {};
6979 D<int, C> d;
6980
6981 i.e. the parameter list of TT depends on earlier parameters. */
6982 if (!uses_template_parms (TREE_TYPE (arg)))
6983 {
6984 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6985 if (!uses_template_parms (t)
6986 && !same_type_p (t, TREE_TYPE (arg)))
6987 return 0;
6988 }
6989
6990 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6991 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6992 /* Argument is a parameter pack but parameter is not. */
6993 return 0;
6994
6995 break;
6996
6997 default:
6998 gcc_unreachable ();
6999 }
7000
7001 return 1;
7002 }
7003
7004 /* Coerce template argument list ARGLIST for use with template
7005 template-parameter TEMPL. */
7006
7007 static tree
7008 coerce_template_args_for_ttp (tree templ, tree arglist,
7009 tsubst_flags_t complain)
7010 {
7011 /* Consider an example where a template template parameter declared as
7012
7013 template <class T, class U = std::allocator<T> > class TT
7014
7015 The template parameter level of T and U are one level larger than
7016 of TT. To proper process the default argument of U, say when an
7017 instantiation `TT<int>' is seen, we need to build the full
7018 arguments containing {int} as the innermost level. Outer levels,
7019 available when not appearing as default template argument, can be
7020 obtained from the arguments of the enclosing template.
7021
7022 Suppose that TT is later substituted with std::vector. The above
7023 instantiation is `TT<int, std::allocator<T> >' with TT at
7024 level 1, and T at level 2, while the template arguments at level 1
7025 becomes {std::vector} and the inner level 2 is {int}. */
7026
7027 tree outer = DECL_CONTEXT (templ);
7028 if (outer)
7029 {
7030 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7031 /* We want arguments for the partial specialization, not arguments for
7032 the primary template. */
7033 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7034 else
7035 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7036 }
7037 else if (current_template_parms)
7038 {
7039 /* This is an argument of the current template, so we haven't set
7040 DECL_CONTEXT yet. */
7041 tree relevant_template_parms;
7042
7043 /* Parameter levels that are greater than the level of the given
7044 template template parm are irrelevant. */
7045 relevant_template_parms = current_template_parms;
7046 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7047 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7048 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7049
7050 outer = template_parms_to_args (relevant_template_parms);
7051 }
7052
7053 if (outer)
7054 arglist = add_to_template_args (outer, arglist);
7055
7056 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7057 return coerce_template_parms (parmlist, arglist, templ,
7058 complain,
7059 /*require_all_args=*/true,
7060 /*use_default_args=*/true);
7061 }
7062
7063 /* A cache of template template parameters with match-all default
7064 arguments. */
7065 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7066 static void
7067 store_defaulted_ttp (tree v, tree t)
7068 {
7069 if (!defaulted_ttp_cache)
7070 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7071 defaulted_ttp_cache->put (v, t);
7072 }
7073 static tree
7074 lookup_defaulted_ttp (tree v)
7075 {
7076 if (defaulted_ttp_cache)
7077 if (tree *p = defaulted_ttp_cache->get (v))
7078 return *p;
7079 return NULL_TREE;
7080 }
7081
7082 /* T is a bound template template-parameter. Copy its arguments into default
7083 arguments of the template template-parameter's template parameters. */
7084
7085 static tree
7086 add_defaults_to_ttp (tree otmpl)
7087 {
7088 if (tree c = lookup_defaulted_ttp (otmpl))
7089 return c;
7090
7091 tree ntmpl = copy_node (otmpl);
7092
7093 tree ntype = copy_node (TREE_TYPE (otmpl));
7094 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7095 TYPE_MAIN_VARIANT (ntype) = ntype;
7096 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7097 TYPE_NAME (ntype) = ntmpl;
7098 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7099
7100 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7101 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7102 TEMPLATE_PARM_DECL (idx) = ntmpl;
7103 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7104
7105 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7106 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7107 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7108 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7109 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7110 {
7111 tree o = TREE_VEC_ELT (vec, i);
7112 if (!template_parameter_pack_p (TREE_VALUE (o)))
7113 {
7114 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7115 TREE_PURPOSE (n) = any_targ_node;
7116 }
7117 }
7118
7119 store_defaulted_ttp (otmpl, ntmpl);
7120 return ntmpl;
7121 }
7122
7123 /* ARG is a bound potential template template-argument, and PARGS is a list
7124 of arguments for the corresponding template template-parameter. Adjust
7125 PARGS as appropriate for application to ARG's template, and if ARG is a
7126 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7127 arguments to the template template parameter. */
7128
7129 static tree
7130 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7131 {
7132 ++processing_template_decl;
7133 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7134 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7135 {
7136 /* When comparing two template template-parameters in partial ordering,
7137 rewrite the one currently being used as an argument to have default
7138 arguments for all parameters. */
7139 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7140 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7141 if (pargs != error_mark_node)
7142 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7143 TYPE_TI_ARGS (arg));
7144 }
7145 else
7146 {
7147 tree aparms
7148 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7149 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7150 /*require_all*/true,
7151 /*use_default*/true);
7152 }
7153 --processing_template_decl;
7154 return pargs;
7155 }
7156
7157 /* Subroutine of unify for the case when PARM is a
7158 BOUND_TEMPLATE_TEMPLATE_PARM. */
7159
7160 static int
7161 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7162 bool explain_p)
7163 {
7164 tree parmvec = TYPE_TI_ARGS (parm);
7165 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7166
7167 /* The template template parm might be variadic and the argument
7168 not, so flatten both argument lists. */
7169 parmvec = expand_template_argument_pack (parmvec);
7170 argvec = expand_template_argument_pack (argvec);
7171
7172 if (flag_new_ttp)
7173 {
7174 /* In keeping with P0522R0, adjust P's template arguments
7175 to apply to A's template; then flatten it again. */
7176 tree nparmvec = parmvec;
7177 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7178 nparmvec = expand_template_argument_pack (nparmvec);
7179
7180 if (unify (tparms, targs, nparmvec, argvec,
7181 UNIFY_ALLOW_NONE, explain_p))
7182 return 1;
7183
7184 /* If the P0522 adjustment eliminated a pack expansion, deduce
7185 empty packs. */
7186 if (flag_new_ttp
7187 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7188 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7189 DEDUCE_EXACT, /*sub*/true, explain_p))
7190 return 1;
7191 }
7192 else
7193 {
7194 /* Deduce arguments T, i from TT<T> or TT<i>.
7195 We check each element of PARMVEC and ARGVEC individually
7196 rather than the whole TREE_VEC since they can have
7197 different number of elements, which is allowed under N2555. */
7198
7199 int len = TREE_VEC_LENGTH (parmvec);
7200
7201 /* Check if the parameters end in a pack, making them
7202 variadic. */
7203 int parm_variadic_p = 0;
7204 if (len > 0
7205 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7206 parm_variadic_p = 1;
7207
7208 for (int i = 0; i < len - parm_variadic_p; ++i)
7209 /* If the template argument list of P contains a pack
7210 expansion that is not the last template argument, the
7211 entire template argument list is a non-deduced
7212 context. */
7213 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7214 return unify_success (explain_p);
7215
7216 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7217 return unify_too_few_arguments (explain_p,
7218 TREE_VEC_LENGTH (argvec), len);
7219
7220 for (int i = 0; i < len - parm_variadic_p; ++i)
7221 if (unify (tparms, targs,
7222 TREE_VEC_ELT (parmvec, i),
7223 TREE_VEC_ELT (argvec, i),
7224 UNIFY_ALLOW_NONE, explain_p))
7225 return 1;
7226
7227 if (parm_variadic_p
7228 && unify_pack_expansion (tparms, targs,
7229 parmvec, argvec,
7230 DEDUCE_EXACT,
7231 /*subr=*/true, explain_p))
7232 return 1;
7233 }
7234
7235 return 0;
7236 }
7237
7238 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7239 template template parameters. Both PARM_PARMS and ARG_PARMS are
7240 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7241 or PARM_DECL.
7242
7243 Consider the example:
7244 template <class T> class A;
7245 template<template <class U> class TT> class B;
7246
7247 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7248 the parameters to A, and OUTER_ARGS contains A. */
7249
7250 static int
7251 coerce_template_template_parms (tree parm_parms,
7252 tree arg_parms,
7253 tsubst_flags_t complain,
7254 tree in_decl,
7255 tree outer_args)
7256 {
7257 int nparms, nargs, i;
7258 tree parm, arg;
7259 int variadic_p = 0;
7260
7261 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7262 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7263
7264 nparms = TREE_VEC_LENGTH (parm_parms);
7265 nargs = TREE_VEC_LENGTH (arg_parms);
7266
7267 if (flag_new_ttp)
7268 {
7269 /* P0522R0: A template template-parameter P is at least as specialized as
7270 a template template-argument A if, given the following rewrite to two
7271 function templates, the function template corresponding to P is at
7272 least as specialized as the function template corresponding to A
7273 according to the partial ordering rules for function templates
7274 ([temp.func.order]). Given an invented class template X with the
7275 template parameter list of A (including default arguments):
7276
7277 * Each of the two function templates has the same template parameters,
7278 respectively, as P or A.
7279
7280 * Each function template has a single function parameter whose type is
7281 a specialization of X with template arguments corresponding to the
7282 template parameters from the respective function template where, for
7283 each template parameter PP in the template parameter list of the
7284 function template, a corresponding template argument AA is formed. If
7285 PP declares a parameter pack, then AA is the pack expansion
7286 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7287
7288 If the rewrite produces an invalid type, then P is not at least as
7289 specialized as A. */
7290
7291 /* So coerce P's args to apply to A's parms, and then deduce between A's
7292 args and the converted args. If that succeeds, A is at least as
7293 specialized as P, so they match.*/
7294 tree pargs = template_parms_level_to_args (parm_parms);
7295 ++processing_template_decl;
7296 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7297 /*require_all*/true, /*use_default*/true);
7298 --processing_template_decl;
7299 if (pargs != error_mark_node)
7300 {
7301 tree targs = make_tree_vec (nargs);
7302 tree aargs = template_parms_level_to_args (arg_parms);
7303 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7304 /*explain*/false))
7305 return 1;
7306 }
7307 }
7308
7309 /* Determine whether we have a parameter pack at the end of the
7310 template template parameter's template parameter list. */
7311 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7312 {
7313 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7314
7315 if (error_operand_p (parm))
7316 return 0;
7317
7318 switch (TREE_CODE (parm))
7319 {
7320 case TEMPLATE_DECL:
7321 case TYPE_DECL:
7322 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7323 variadic_p = 1;
7324 break;
7325
7326 case PARM_DECL:
7327 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7328 variadic_p = 1;
7329 break;
7330
7331 default:
7332 gcc_unreachable ();
7333 }
7334 }
7335
7336 if (nargs != nparms
7337 && !(variadic_p && nargs >= nparms - 1))
7338 return 0;
7339
7340 /* Check all of the template parameters except the parameter pack at
7341 the end (if any). */
7342 for (i = 0; i < nparms - variadic_p; ++i)
7343 {
7344 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7345 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7346 continue;
7347
7348 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7349 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7350
7351 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7352 outer_args))
7353 return 0;
7354
7355 }
7356
7357 if (variadic_p)
7358 {
7359 /* Check each of the template parameters in the template
7360 argument against the template parameter pack at the end of
7361 the template template parameter. */
7362 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7363 return 0;
7364
7365 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7366
7367 for (; i < nargs; ++i)
7368 {
7369 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7370 continue;
7371
7372 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7373
7374 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7375 outer_args))
7376 return 0;
7377 }
7378 }
7379
7380 return 1;
7381 }
7382
7383 /* Verifies that the deduced template arguments (in TARGS) for the
7384 template template parameters (in TPARMS) represent valid bindings,
7385 by comparing the template parameter list of each template argument
7386 to the template parameter list of its corresponding template
7387 template parameter, in accordance with DR150. This
7388 routine can only be called after all template arguments have been
7389 deduced. It will return TRUE if all of the template template
7390 parameter bindings are okay, FALSE otherwise. */
7391 bool
7392 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7393 {
7394 int i, ntparms = TREE_VEC_LENGTH (tparms);
7395 bool ret = true;
7396
7397 /* We're dealing with template parms in this process. */
7398 ++processing_template_decl;
7399
7400 targs = INNERMOST_TEMPLATE_ARGS (targs);
7401
7402 for (i = 0; i < ntparms; ++i)
7403 {
7404 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7405 tree targ = TREE_VEC_ELT (targs, i);
7406
7407 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7408 {
7409 tree packed_args = NULL_TREE;
7410 int idx, len = 1;
7411
7412 if (ARGUMENT_PACK_P (targ))
7413 {
7414 /* Look inside the argument pack. */
7415 packed_args = ARGUMENT_PACK_ARGS (targ);
7416 len = TREE_VEC_LENGTH (packed_args);
7417 }
7418
7419 for (idx = 0; idx < len; ++idx)
7420 {
7421 tree targ_parms = NULL_TREE;
7422
7423 if (packed_args)
7424 /* Extract the next argument from the argument
7425 pack. */
7426 targ = TREE_VEC_ELT (packed_args, idx);
7427
7428 if (PACK_EXPANSION_P (targ))
7429 /* Look at the pattern of the pack expansion. */
7430 targ = PACK_EXPANSION_PATTERN (targ);
7431
7432 /* Extract the template parameters from the template
7433 argument. */
7434 if (TREE_CODE (targ) == TEMPLATE_DECL)
7435 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7436 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7437 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7438
7439 /* Verify that we can coerce the template template
7440 parameters from the template argument to the template
7441 parameter. This requires an exact match. */
7442 if (targ_parms
7443 && !coerce_template_template_parms
7444 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7445 targ_parms,
7446 tf_none,
7447 tparm,
7448 targs))
7449 {
7450 ret = false;
7451 goto out;
7452 }
7453 }
7454 }
7455 }
7456
7457 out:
7458
7459 --processing_template_decl;
7460 return ret;
7461 }
7462
7463 /* Since type attributes aren't mangled, we need to strip them from
7464 template type arguments. */
7465
7466 static tree
7467 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7468 {
7469 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7470 return arg;
7471 bool removed_attributes = false;
7472 tree canon = strip_typedefs (arg, &removed_attributes);
7473 if (removed_attributes
7474 && (complain & tf_warning))
7475 warning (OPT_Wignored_attributes,
7476 "ignoring attributes on template argument %qT", arg);
7477 return canon;
7478 }
7479
7480 /* And from inside dependent non-type arguments like sizeof(Type). */
7481
7482 static tree
7483 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7484 {
7485 if (!arg || arg == error_mark_node)
7486 return arg;
7487 bool removed_attributes = false;
7488 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7489 if (removed_attributes
7490 && (complain & tf_warning))
7491 warning (OPT_Wignored_attributes,
7492 "ignoring attributes in template argument %qE", arg);
7493 return canon;
7494 }
7495
7496 // A template declaration can be substituted for a constrained
7497 // template template parameter only when the argument is more
7498 // constrained than the parameter.
7499 static bool
7500 is_compatible_template_arg (tree parm, tree arg)
7501 {
7502 tree parm_cons = get_constraints (parm);
7503
7504 /* For now, allow constrained template template arguments
7505 and unconstrained template template parameters. */
7506 if (parm_cons == NULL_TREE)
7507 return true;
7508
7509 tree arg_cons = get_constraints (arg);
7510
7511 // If the template parameter is constrained, we need to rewrite its
7512 // constraints in terms of the ARG's template parameters. This ensures
7513 // that all of the template parameter types will have the same depth.
7514 //
7515 // Note that this is only valid when coerce_template_template_parm is
7516 // true for the innermost template parameters of PARM and ARG. In other
7517 // words, because coercion is successful, this conversion will be valid.
7518 if (parm_cons)
7519 {
7520 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7521 parm_cons = tsubst_constraint_info (parm_cons,
7522 INNERMOST_TEMPLATE_ARGS (args),
7523 tf_none, NULL_TREE);
7524 if (parm_cons == error_mark_node)
7525 return false;
7526 }
7527
7528 return subsumes (parm_cons, arg_cons);
7529 }
7530
7531 // Convert a placeholder argument into a binding to the original
7532 // parameter. The original parameter is saved as the TREE_TYPE of
7533 // ARG.
7534 static inline tree
7535 convert_wildcard_argument (tree parm, tree arg)
7536 {
7537 TREE_TYPE (arg) = parm;
7538 return arg;
7539 }
7540
7541 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7542 because one of them is dependent. But we need to represent the
7543 conversion for the benefit of cp_tree_equal. */
7544
7545 static tree
7546 maybe_convert_nontype_argument (tree type, tree arg)
7547 {
7548 /* Auto parms get no conversion. */
7549 if (type_uses_auto (type))
7550 return arg;
7551 /* We don't need or want to add this conversion now if we're going to use the
7552 argument for deduction. */
7553 if (value_dependent_expression_p (arg))
7554 return arg;
7555
7556 type = cv_unqualified (type);
7557 tree argtype = TREE_TYPE (arg);
7558 if (same_type_p (type, argtype))
7559 return arg;
7560
7561 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7562 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7563 return arg;
7564 }
7565
7566 /* Convert the indicated template ARG as necessary to match the
7567 indicated template PARM. Returns the converted ARG, or
7568 error_mark_node if the conversion was unsuccessful. Error and
7569 warning messages are issued under control of COMPLAIN. This
7570 conversion is for the Ith parameter in the parameter list. ARGS is
7571 the full set of template arguments deduced so far. */
7572
7573 static tree
7574 convert_template_argument (tree parm,
7575 tree arg,
7576 tree args,
7577 tsubst_flags_t complain,
7578 int i,
7579 tree in_decl)
7580 {
7581 tree orig_arg;
7582 tree val;
7583 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7584
7585 if (parm == error_mark_node)
7586 return error_mark_node;
7587
7588 /* Trivially convert placeholders. */
7589 if (TREE_CODE (arg) == WILDCARD_DECL)
7590 return convert_wildcard_argument (parm, arg);
7591
7592 if (arg == any_targ_node)
7593 return arg;
7594
7595 if (TREE_CODE (arg) == TREE_LIST
7596 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7597 {
7598 /* The template argument was the name of some
7599 member function. That's usually
7600 invalid, but static members are OK. In any
7601 case, grab the underlying fields/functions
7602 and issue an error later if required. */
7603 orig_arg = TREE_VALUE (arg);
7604 TREE_TYPE (arg) = unknown_type_node;
7605 }
7606
7607 orig_arg = arg;
7608
7609 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7610 requires_type = (TREE_CODE (parm) == TYPE_DECL
7611 || requires_tmpl_type);
7612
7613 /* When determining whether an argument pack expansion is a template,
7614 look at the pattern. */
7615 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7616 arg = PACK_EXPANSION_PATTERN (arg);
7617
7618 /* Deal with an injected-class-name used as a template template arg. */
7619 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7620 {
7621 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7622 if (TREE_CODE (t) == TEMPLATE_DECL)
7623 {
7624 if (cxx_dialect >= cxx11)
7625 /* OK under DR 1004. */;
7626 else if (complain & tf_warning_or_error)
7627 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7628 " used as template template argument", TYPE_NAME (arg));
7629 else if (flag_pedantic_errors)
7630 t = arg;
7631
7632 arg = t;
7633 }
7634 }
7635
7636 is_tmpl_type =
7637 ((TREE_CODE (arg) == TEMPLATE_DECL
7638 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7639 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7640 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7641 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7642
7643 if (is_tmpl_type
7644 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7645 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7646 arg = TYPE_STUB_DECL (arg);
7647
7648 is_type = TYPE_P (arg) || is_tmpl_type;
7649
7650 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7651 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7652 {
7653 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7654 {
7655 if (complain & tf_error)
7656 error ("invalid use of destructor %qE as a type", orig_arg);
7657 return error_mark_node;
7658 }
7659
7660 permerror (input_location,
7661 "to refer to a type member of a template parameter, "
7662 "use %<typename %E%>", orig_arg);
7663
7664 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7665 TREE_OPERAND (arg, 1),
7666 typename_type,
7667 complain);
7668 arg = orig_arg;
7669 is_type = 1;
7670 }
7671 if (is_type != requires_type)
7672 {
7673 if (in_decl)
7674 {
7675 if (complain & tf_error)
7676 {
7677 error ("type/value mismatch at argument %d in template "
7678 "parameter list for %qD",
7679 i + 1, in_decl);
7680 if (is_type)
7681 inform (input_location,
7682 " expected a constant of type %qT, got %qT",
7683 TREE_TYPE (parm),
7684 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7685 else if (requires_tmpl_type)
7686 inform (input_location,
7687 " expected a class template, got %qE", orig_arg);
7688 else
7689 inform (input_location,
7690 " expected a type, got %qE", orig_arg);
7691 }
7692 }
7693 return error_mark_node;
7694 }
7695 if (is_tmpl_type ^ requires_tmpl_type)
7696 {
7697 if (in_decl && (complain & tf_error))
7698 {
7699 error ("type/value mismatch at argument %d in template "
7700 "parameter list for %qD",
7701 i + 1, in_decl);
7702 if (is_tmpl_type)
7703 inform (input_location,
7704 " expected a type, got %qT", DECL_NAME (arg));
7705 else
7706 inform (input_location,
7707 " expected a class template, got %qT", orig_arg);
7708 }
7709 return error_mark_node;
7710 }
7711
7712 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7713 /* We already did the appropriate conversion when packing args. */
7714 val = orig_arg;
7715 else if (is_type)
7716 {
7717 if (requires_tmpl_type)
7718 {
7719 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7720 /* The number of argument required is not known yet.
7721 Just accept it for now. */
7722 val = orig_arg;
7723 else
7724 {
7725 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7726 tree argparm;
7727
7728 /* Strip alias templates that are equivalent to another
7729 template. */
7730 arg = get_underlying_template (arg);
7731 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7732
7733 if (coerce_template_template_parms (parmparm, argparm,
7734 complain, in_decl,
7735 args))
7736 {
7737 val = arg;
7738
7739 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7740 TEMPLATE_DECL. */
7741 if (val != error_mark_node)
7742 {
7743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7744 val = TREE_TYPE (val);
7745 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7746 val = make_pack_expansion (val, complain);
7747 }
7748 }
7749 else
7750 {
7751 if (in_decl && (complain & tf_error))
7752 {
7753 error ("type/value mismatch at argument %d in "
7754 "template parameter list for %qD",
7755 i + 1, in_decl);
7756 inform (input_location,
7757 " expected a template of type %qD, got %qT",
7758 parm, orig_arg);
7759 }
7760
7761 val = error_mark_node;
7762 }
7763
7764 // Check that the constraints are compatible before allowing the
7765 // substitution.
7766 if (val != error_mark_node)
7767 if (!is_compatible_template_arg (parm, arg))
7768 {
7769 if (in_decl && (complain & tf_error))
7770 {
7771 error ("constraint mismatch at argument %d in "
7772 "template parameter list for %qD",
7773 i + 1, in_decl);
7774 inform (input_location, " expected %qD but got %qD",
7775 parm, arg);
7776 }
7777 val = error_mark_node;
7778 }
7779 }
7780 }
7781 else
7782 val = orig_arg;
7783 /* We only form one instance of each template specialization.
7784 Therefore, if we use a non-canonical variant (i.e., a
7785 typedef), any future messages referring to the type will use
7786 the typedef, which is confusing if those future uses do not
7787 themselves also use the typedef. */
7788 if (TYPE_P (val))
7789 val = canonicalize_type_argument (val, complain);
7790 }
7791 else
7792 {
7793 tree t = TREE_TYPE (parm);
7794
7795 if (tree a = type_uses_auto (t))
7796 {
7797 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7798 if (t == error_mark_node)
7799 return error_mark_node;
7800 }
7801 else
7802 t = tsubst (t, args, complain, in_decl);
7803
7804 if (invalid_nontype_parm_type_p (t, complain))
7805 return error_mark_node;
7806
7807 if (!type_dependent_expression_p (orig_arg)
7808 && !uses_template_parms (t))
7809 /* We used to call digest_init here. However, digest_init
7810 will report errors, which we don't want when complain
7811 is zero. More importantly, digest_init will try too
7812 hard to convert things: for example, `0' should not be
7813 converted to pointer type at this point according to
7814 the standard. Accepting this is not merely an
7815 extension, since deciding whether or not these
7816 conversions can occur is part of determining which
7817 function template to call, or whether a given explicit
7818 argument specification is valid. */
7819 val = convert_nontype_argument (t, orig_arg, complain);
7820 else
7821 {
7822 val = canonicalize_expr_argument (orig_arg, complain);
7823 val = maybe_convert_nontype_argument (t, val);
7824 }
7825
7826
7827 if (val == NULL_TREE)
7828 val = error_mark_node;
7829 else if (val == error_mark_node && (complain & tf_error))
7830 error ("could not convert template argument %qE from %qT to %qT",
7831 orig_arg, TREE_TYPE (orig_arg), t);
7832
7833 if (INDIRECT_REF_P (val))
7834 {
7835 /* Reject template arguments that are references to built-in
7836 functions with no library fallbacks. */
7837 const_tree inner = TREE_OPERAND (val, 0);
7838 const_tree innertype = TREE_TYPE (inner);
7839 if (innertype
7840 && TREE_CODE (innertype) == REFERENCE_TYPE
7841 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7842 && TREE_OPERAND_LENGTH (inner) > 0
7843 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7844 return error_mark_node;
7845 }
7846
7847 if (TREE_CODE (val) == SCOPE_REF)
7848 {
7849 /* Strip typedefs from the SCOPE_REF. */
7850 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7851 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7852 complain);
7853 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7854 QUALIFIED_NAME_IS_TEMPLATE (val));
7855 }
7856 }
7857
7858 return val;
7859 }
7860
7861 /* Coerces the remaining template arguments in INNER_ARGS (from
7862 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7863 Returns the coerced argument pack. PARM_IDX is the position of this
7864 parameter in the template parameter list. ARGS is the original
7865 template argument list. */
7866 static tree
7867 coerce_template_parameter_pack (tree parms,
7868 int parm_idx,
7869 tree args,
7870 tree inner_args,
7871 int arg_idx,
7872 tree new_args,
7873 int* lost,
7874 tree in_decl,
7875 tsubst_flags_t complain)
7876 {
7877 tree parm = TREE_VEC_ELT (parms, parm_idx);
7878 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7879 tree packed_args;
7880 tree argument_pack;
7881 tree packed_parms = NULL_TREE;
7882
7883 if (arg_idx > nargs)
7884 arg_idx = nargs;
7885
7886 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7887 {
7888 /* When the template parameter is a non-type template parameter pack
7889 or template template parameter pack whose type or template
7890 parameters use parameter packs, we know exactly how many arguments
7891 we are looking for. Build a vector of the instantiated decls for
7892 these template parameters in PACKED_PARMS. */
7893 /* We can't use make_pack_expansion here because it would interpret a
7894 _DECL as a use rather than a declaration. */
7895 tree decl = TREE_VALUE (parm);
7896 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7897 SET_PACK_EXPANSION_PATTERN (exp, decl);
7898 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7899 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7900
7901 TREE_VEC_LENGTH (args)--;
7902 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7903 TREE_VEC_LENGTH (args)++;
7904
7905 if (packed_parms == error_mark_node)
7906 return error_mark_node;
7907
7908 /* If we're doing a partial instantiation of a member template,
7909 verify that all of the types used for the non-type
7910 template parameter pack are, in fact, valid for non-type
7911 template parameters. */
7912 if (arg_idx < nargs
7913 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7914 {
7915 int j, len = TREE_VEC_LENGTH (packed_parms);
7916 for (j = 0; j < len; ++j)
7917 {
7918 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7919 if (invalid_nontype_parm_type_p (t, complain))
7920 return error_mark_node;
7921 }
7922 /* We don't know how many args we have yet, just
7923 use the unconverted ones for now. */
7924 return NULL_TREE;
7925 }
7926
7927 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7928 }
7929 /* Check if we have a placeholder pack, which indicates we're
7930 in the context of a introduction list. In that case we want
7931 to match this pack to the single placeholder. */
7932 else if (arg_idx < nargs
7933 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7934 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7935 {
7936 nargs = arg_idx + 1;
7937 packed_args = make_tree_vec (1);
7938 }
7939 else
7940 packed_args = make_tree_vec (nargs - arg_idx);
7941
7942 /* Convert the remaining arguments, which will be a part of the
7943 parameter pack "parm". */
7944 int first_pack_arg = arg_idx;
7945 for (; arg_idx < nargs; ++arg_idx)
7946 {
7947 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7948 tree actual_parm = TREE_VALUE (parm);
7949 int pack_idx = arg_idx - first_pack_arg;
7950
7951 if (packed_parms)
7952 {
7953 /* Once we've packed as many args as we have types, stop. */
7954 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7955 break;
7956 else if (PACK_EXPANSION_P (arg))
7957 /* We don't know how many args we have yet, just
7958 use the unconverted ones for now. */
7959 return NULL_TREE;
7960 else
7961 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7962 }
7963
7964 if (arg == error_mark_node)
7965 {
7966 if (complain & tf_error)
7967 error ("template argument %d is invalid", arg_idx + 1);
7968 }
7969 else
7970 arg = convert_template_argument (actual_parm,
7971 arg, new_args, complain, parm_idx,
7972 in_decl);
7973 if (arg == error_mark_node)
7974 (*lost)++;
7975 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7976 }
7977
7978 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7979 && TREE_VEC_LENGTH (packed_args) > 0)
7980 {
7981 if (complain & tf_error)
7982 error ("wrong number of template arguments (%d, should be %d)",
7983 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7984 return error_mark_node;
7985 }
7986
7987 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7988 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7989 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7990 else
7991 {
7992 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7993 TREE_CONSTANT (argument_pack) = 1;
7994 }
7995
7996 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7997 if (CHECKING_P)
7998 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7999 TREE_VEC_LENGTH (packed_args));
8000 return argument_pack;
8001 }
8002
8003 /* Returns the number of pack expansions in the template argument vector
8004 ARGS. */
8005
8006 static int
8007 pack_expansion_args_count (tree args)
8008 {
8009 int i;
8010 int count = 0;
8011 if (args)
8012 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8013 {
8014 tree elt = TREE_VEC_ELT (args, i);
8015 if (elt && PACK_EXPANSION_P (elt))
8016 ++count;
8017 }
8018 return count;
8019 }
8020
8021 /* Convert all template arguments to their appropriate types, and
8022 return a vector containing the innermost resulting template
8023 arguments. If any error occurs, return error_mark_node. Error and
8024 warning messages are issued under control of COMPLAIN.
8025
8026 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8027 for arguments not specified in ARGS. Otherwise, if
8028 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8029 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8030 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8031 ARGS. */
8032
8033 static tree
8034 coerce_template_parms (tree parms,
8035 tree args,
8036 tree in_decl,
8037 tsubst_flags_t complain,
8038 bool require_all_args,
8039 bool use_default_args)
8040 {
8041 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8042 tree orig_inner_args;
8043 tree inner_args;
8044 tree new_args;
8045 tree new_inner_args;
8046 int saved_unevaluated_operand;
8047 int saved_inhibit_evaluation_warnings;
8048
8049 /* When used as a boolean value, indicates whether this is a
8050 variadic template parameter list. Since it's an int, we can also
8051 subtract it from nparms to get the number of non-variadic
8052 parameters. */
8053 int variadic_p = 0;
8054 int variadic_args_p = 0;
8055 int post_variadic_parms = 0;
8056
8057 /* Likewise for parameters with default arguments. */
8058 int default_p = 0;
8059
8060 if (args == error_mark_node)
8061 return error_mark_node;
8062
8063 nparms = TREE_VEC_LENGTH (parms);
8064
8065 /* Determine if there are any parameter packs or default arguments. */
8066 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8067 {
8068 tree parm = TREE_VEC_ELT (parms, parm_idx);
8069 if (variadic_p)
8070 ++post_variadic_parms;
8071 if (template_parameter_pack_p (TREE_VALUE (parm)))
8072 ++variadic_p;
8073 if (TREE_PURPOSE (parm))
8074 ++default_p;
8075 }
8076
8077 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8078 /* If there are no parameters that follow a parameter pack, we need to
8079 expand any argument packs so that we can deduce a parameter pack from
8080 some non-packed args followed by an argument pack, as in variadic85.C.
8081 If there are such parameters, we need to leave argument packs intact
8082 so the arguments are assigned properly. This can happen when dealing
8083 with a nested class inside a partial specialization of a class
8084 template, as in variadic92.C, or when deducing a template parameter pack
8085 from a sub-declarator, as in variadic114.C. */
8086 if (!post_variadic_parms)
8087 inner_args = expand_template_argument_pack (inner_args);
8088
8089 /* Count any pack expansion args. */
8090 variadic_args_p = pack_expansion_args_count (inner_args);
8091
8092 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8093 if ((nargs - variadic_args_p > nparms && !variadic_p)
8094 || (nargs < nparms - variadic_p
8095 && require_all_args
8096 && !variadic_args_p
8097 && (!use_default_args
8098 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8099 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8100 {
8101 if (complain & tf_error)
8102 {
8103 if (variadic_p || default_p)
8104 {
8105 nparms -= variadic_p + default_p;
8106 error ("wrong number of template arguments "
8107 "(%d, should be at least %d)", nargs, nparms);
8108 }
8109 else
8110 error ("wrong number of template arguments "
8111 "(%d, should be %d)", nargs, nparms);
8112
8113 if (in_decl)
8114 inform (DECL_SOURCE_LOCATION (in_decl),
8115 "provided for %qD", in_decl);
8116 }
8117
8118 return error_mark_node;
8119 }
8120 /* We can't pass a pack expansion to a non-pack parameter of an alias
8121 template (DR 1430). */
8122 else if (in_decl
8123 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8124 || concept_template_p (in_decl))
8125 && variadic_args_p
8126 && nargs - variadic_args_p < nparms - variadic_p)
8127 {
8128 if (complain & tf_error)
8129 {
8130 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8131 {
8132 tree arg = TREE_VEC_ELT (inner_args, i);
8133 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8134
8135 if (PACK_EXPANSION_P (arg)
8136 && !template_parameter_pack_p (parm))
8137 {
8138 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8139 error_at (location_of (arg),
8140 "pack expansion argument for non-pack parameter "
8141 "%qD of alias template %qD", parm, in_decl);
8142 else
8143 error_at (location_of (arg),
8144 "pack expansion argument for non-pack parameter "
8145 "%qD of concept %qD", parm, in_decl);
8146 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8147 goto found;
8148 }
8149 }
8150 gcc_unreachable ();
8151 found:;
8152 }
8153 return error_mark_node;
8154 }
8155
8156 /* We need to evaluate the template arguments, even though this
8157 template-id may be nested within a "sizeof". */
8158 saved_unevaluated_operand = cp_unevaluated_operand;
8159 cp_unevaluated_operand = 0;
8160 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8161 c_inhibit_evaluation_warnings = 0;
8162 new_inner_args = make_tree_vec (nparms);
8163 new_args = add_outermost_template_args (args, new_inner_args);
8164 int pack_adjust = 0;
8165 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8166 {
8167 tree arg;
8168 tree parm;
8169
8170 /* Get the Ith template parameter. */
8171 parm = TREE_VEC_ELT (parms, parm_idx);
8172
8173 if (parm == error_mark_node)
8174 {
8175 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8176 continue;
8177 }
8178
8179 /* Calculate the next argument. */
8180 if (arg_idx < nargs)
8181 arg = TREE_VEC_ELT (inner_args, arg_idx);
8182 else
8183 arg = NULL_TREE;
8184
8185 if (template_parameter_pack_p (TREE_VALUE (parm))
8186 && !(arg && ARGUMENT_PACK_P (arg)))
8187 {
8188 /* Some arguments will be placed in the
8189 template parameter pack PARM. */
8190 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8191 inner_args, arg_idx,
8192 new_args, &lost,
8193 in_decl, complain);
8194
8195 if (arg == NULL_TREE)
8196 {
8197 /* We don't know how many args we have yet, just use the
8198 unconverted (and still packed) ones for now. */
8199 new_inner_args = orig_inner_args;
8200 arg_idx = nargs;
8201 break;
8202 }
8203
8204 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8205
8206 /* Store this argument. */
8207 if (arg == error_mark_node)
8208 {
8209 lost++;
8210 /* We are done with all of the arguments. */
8211 arg_idx = nargs;
8212 }
8213 else
8214 {
8215 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8216 arg_idx += pack_adjust;
8217 }
8218
8219 continue;
8220 }
8221 else if (arg)
8222 {
8223 if (PACK_EXPANSION_P (arg))
8224 {
8225 /* "If every valid specialization of a variadic template
8226 requires an empty template parameter pack, the template is
8227 ill-formed, no diagnostic required." So check that the
8228 pattern works with this parameter. */
8229 tree pattern = PACK_EXPANSION_PATTERN (arg);
8230 tree conv = convert_template_argument (TREE_VALUE (parm),
8231 pattern, new_args,
8232 complain, parm_idx,
8233 in_decl);
8234 if (conv == error_mark_node)
8235 {
8236 if (complain & tf_error)
8237 inform (input_location, "so any instantiation with a "
8238 "non-empty parameter pack would be ill-formed");
8239 ++lost;
8240 }
8241 else if (TYPE_P (conv) && !TYPE_P (pattern))
8242 /* Recover from missing typename. */
8243 TREE_VEC_ELT (inner_args, arg_idx)
8244 = make_pack_expansion (conv, complain);
8245
8246 /* We don't know how many args we have yet, just
8247 use the unconverted ones for now. */
8248 new_inner_args = inner_args;
8249 arg_idx = nargs;
8250 break;
8251 }
8252 }
8253 else if (require_all_args)
8254 {
8255 /* There must be a default arg in this case. */
8256 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8257 complain, in_decl);
8258 /* The position of the first default template argument,
8259 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8260 Record that. */
8261 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8262 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8263 arg_idx - pack_adjust);
8264 }
8265 else
8266 break;
8267
8268 if (arg == error_mark_node)
8269 {
8270 if (complain & tf_error)
8271 error ("template argument %d is invalid", arg_idx + 1);
8272 }
8273 else if (!arg)
8274 /* This only occurs if there was an error in the template
8275 parameter list itself (which we would already have
8276 reported) that we are trying to recover from, e.g., a class
8277 template with a parameter list such as
8278 template<typename..., typename>. */
8279 ++lost;
8280 else
8281 arg = convert_template_argument (TREE_VALUE (parm),
8282 arg, new_args, complain,
8283 parm_idx, in_decl);
8284
8285 if (arg == error_mark_node)
8286 lost++;
8287 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8288 }
8289 cp_unevaluated_operand = saved_unevaluated_operand;
8290 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8291
8292 if (variadic_p && arg_idx < nargs)
8293 {
8294 if (complain & tf_error)
8295 {
8296 error ("wrong number of template arguments "
8297 "(%d, should be %d)", nargs, arg_idx);
8298 if (in_decl)
8299 error ("provided for %q+D", in_decl);
8300 }
8301 return error_mark_node;
8302 }
8303
8304 if (lost)
8305 return error_mark_node;
8306
8307 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8309 TREE_VEC_LENGTH (new_inner_args));
8310
8311 return new_inner_args;
8312 }
8313
8314 /* Convert all template arguments to their appropriate types, and
8315 return a vector containing the innermost resulting template
8316 arguments. If any error occurs, return error_mark_node. Error and
8317 warning messages are not issued.
8318
8319 Note that no function argument deduction is performed, and default
8320 arguments are used to fill in unspecified arguments. */
8321 tree
8322 coerce_template_parms (tree parms, tree args, tree in_decl)
8323 {
8324 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8325 }
8326
8327 /* Convert all template arguments to their appropriate type, and
8328 instantiate default arguments as needed. This returns a vector
8329 containing the innermost resulting template arguments, or
8330 error_mark_node if unsuccessful. */
8331 tree
8332 coerce_template_parms (tree parms, tree args, tree in_decl,
8333 tsubst_flags_t complain)
8334 {
8335 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8336 }
8337
8338 /* Like coerce_template_parms. If PARMS represents all template
8339 parameters levels, this function returns a vector of vectors
8340 representing all the resulting argument levels. Note that in this
8341 case, only the innermost arguments are coerced because the
8342 outermost ones are supposed to have been coerced already.
8343
8344 Otherwise, if PARMS represents only (the innermost) vector of
8345 parameters, this function returns a vector containing just the
8346 innermost resulting arguments. */
8347
8348 static tree
8349 coerce_innermost_template_parms (tree parms,
8350 tree args,
8351 tree in_decl,
8352 tsubst_flags_t complain,
8353 bool require_all_args,
8354 bool use_default_args)
8355 {
8356 int parms_depth = TMPL_PARMS_DEPTH (parms);
8357 int args_depth = TMPL_ARGS_DEPTH (args);
8358 tree coerced_args;
8359
8360 if (parms_depth > 1)
8361 {
8362 coerced_args = make_tree_vec (parms_depth);
8363 tree level;
8364 int cur_depth;
8365
8366 for (level = parms, cur_depth = parms_depth;
8367 parms_depth > 0 && level != NULL_TREE;
8368 level = TREE_CHAIN (level), --cur_depth)
8369 {
8370 tree l;
8371 if (cur_depth == args_depth)
8372 l = coerce_template_parms (TREE_VALUE (level),
8373 args, in_decl, complain,
8374 require_all_args,
8375 use_default_args);
8376 else
8377 l = TMPL_ARGS_LEVEL (args, cur_depth);
8378
8379 if (l == error_mark_node)
8380 return error_mark_node;
8381
8382 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8383 }
8384 }
8385 else
8386 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8387 args, in_decl, complain,
8388 require_all_args,
8389 use_default_args);
8390 return coerced_args;
8391 }
8392
8393 /* Returns 1 if template args OT and NT are equivalent. */
8394
8395 int
8396 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8397 {
8398 if (nt == ot)
8399 return 1;
8400 if (nt == NULL_TREE || ot == NULL_TREE)
8401 return false;
8402 if (nt == any_targ_node || ot == any_targ_node)
8403 return true;
8404
8405 if (TREE_CODE (nt) == TREE_VEC)
8406 /* For member templates */
8407 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8408 else if (PACK_EXPANSION_P (ot))
8409 return (PACK_EXPANSION_P (nt)
8410 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8411 PACK_EXPANSION_PATTERN (nt))
8412 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8413 PACK_EXPANSION_EXTRA_ARGS (nt)));
8414 else if (ARGUMENT_PACK_P (ot))
8415 {
8416 int i, len;
8417 tree opack, npack;
8418
8419 if (!ARGUMENT_PACK_P (nt))
8420 return 0;
8421
8422 opack = ARGUMENT_PACK_ARGS (ot);
8423 npack = ARGUMENT_PACK_ARGS (nt);
8424 len = TREE_VEC_LENGTH (opack);
8425 if (TREE_VEC_LENGTH (npack) != len)
8426 return 0;
8427 for (i = 0; i < len; ++i)
8428 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8429 TREE_VEC_ELT (npack, i)))
8430 return 0;
8431 return 1;
8432 }
8433 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8434 gcc_unreachable ();
8435 else if (TYPE_P (nt))
8436 {
8437 if (!TYPE_P (ot))
8438 return false;
8439 /* Don't treat an alias template specialization with dependent
8440 arguments as equivalent to its underlying type when used as a
8441 template argument; we need them to be distinct so that we
8442 substitute into the specialization arguments at instantiation
8443 time. And aliases can't be equivalent without being ==, so
8444 we don't need to look any deeper.
8445
8446 During partial ordering, however, we need to treat them normally so
8447 that we can order uses of the same alias with different
8448 cv-qualification (79960). */
8449 if (!partial_order
8450 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8451 return false;
8452 else
8453 return same_type_p (ot, nt);
8454 }
8455 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8456 return 0;
8457 else
8458 {
8459 /* Try to treat a template non-type argument that has been converted
8460 to the parameter type as equivalent to one that hasn't yet. */
8461 for (enum tree_code code1 = TREE_CODE (ot);
8462 CONVERT_EXPR_CODE_P (code1)
8463 || code1 == NON_LVALUE_EXPR;
8464 code1 = TREE_CODE (ot))
8465 ot = TREE_OPERAND (ot, 0);
8466 for (enum tree_code code2 = TREE_CODE (nt);
8467 CONVERT_EXPR_CODE_P (code2)
8468 || code2 == NON_LVALUE_EXPR;
8469 code2 = TREE_CODE (nt))
8470 nt = TREE_OPERAND (nt, 0);
8471
8472 return cp_tree_equal (ot, nt);
8473 }
8474 }
8475
8476 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8477 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8478 NEWARG_PTR with the offending arguments if they are non-NULL. */
8479
8480 int
8481 comp_template_args (tree oldargs, tree newargs,
8482 tree *oldarg_ptr, tree *newarg_ptr,
8483 bool partial_order)
8484 {
8485 int i;
8486
8487 if (oldargs == newargs)
8488 return 1;
8489
8490 if (!oldargs || !newargs)
8491 return 0;
8492
8493 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8494 return 0;
8495
8496 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8497 {
8498 tree nt = TREE_VEC_ELT (newargs, i);
8499 tree ot = TREE_VEC_ELT (oldargs, i);
8500
8501 if (! template_args_equal (ot, nt, partial_order))
8502 {
8503 if (oldarg_ptr != NULL)
8504 *oldarg_ptr = ot;
8505 if (newarg_ptr != NULL)
8506 *newarg_ptr = nt;
8507 return 0;
8508 }
8509 }
8510 return 1;
8511 }
8512
8513 inline bool
8514 comp_template_args_porder (tree oargs, tree nargs)
8515 {
8516 return comp_template_args (oargs, nargs, NULL, NULL, true);
8517 }
8518
8519 static void
8520 add_pending_template (tree d)
8521 {
8522 tree ti = (TYPE_P (d)
8523 ? CLASSTYPE_TEMPLATE_INFO (d)
8524 : DECL_TEMPLATE_INFO (d));
8525 struct pending_template *pt;
8526 int level;
8527
8528 if (TI_PENDING_TEMPLATE_FLAG (ti))
8529 return;
8530
8531 /* We are called both from instantiate_decl, where we've already had a
8532 tinst_level pushed, and instantiate_template, where we haven't.
8533 Compensate. */
8534 level = !current_tinst_level || current_tinst_level->decl != d;
8535
8536 if (level)
8537 push_tinst_level (d);
8538
8539 pt = ggc_alloc<pending_template> ();
8540 pt->next = NULL;
8541 pt->tinst = current_tinst_level;
8542 if (last_pending_template)
8543 last_pending_template->next = pt;
8544 else
8545 pending_templates = pt;
8546
8547 last_pending_template = pt;
8548
8549 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8550
8551 if (level)
8552 pop_tinst_level ();
8553 }
8554
8555
8556 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8557 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8558 documentation for TEMPLATE_ID_EXPR. */
8559
8560 tree
8561 lookup_template_function (tree fns, tree arglist)
8562 {
8563 tree type;
8564
8565 if (fns == error_mark_node || arglist == error_mark_node)
8566 return error_mark_node;
8567
8568 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8569
8570 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8571 {
8572 error ("%q#D is not a function template", fns);
8573 return error_mark_node;
8574 }
8575
8576 if (BASELINK_P (fns))
8577 {
8578 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8579 unknown_type_node,
8580 BASELINK_FUNCTIONS (fns),
8581 arglist);
8582 return fns;
8583 }
8584
8585 type = TREE_TYPE (fns);
8586 if (TREE_CODE (fns) == OVERLOAD || !type)
8587 type = unknown_type_node;
8588
8589 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8590 }
8591
8592 /* Within the scope of a template class S<T>, the name S gets bound
8593 (in build_self_reference) to a TYPE_DECL for the class, not a
8594 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8595 or one of its enclosing classes, and that type is a template,
8596 return the associated TEMPLATE_DECL. Otherwise, the original
8597 DECL is returned.
8598
8599 Also handle the case when DECL is a TREE_LIST of ambiguous
8600 injected-class-names from different bases. */
8601
8602 tree
8603 maybe_get_template_decl_from_type_decl (tree decl)
8604 {
8605 if (decl == NULL_TREE)
8606 return decl;
8607
8608 /* DR 176: A lookup that finds an injected-class-name (10.2
8609 [class.member.lookup]) can result in an ambiguity in certain cases
8610 (for example, if it is found in more than one base class). If all of
8611 the injected-class-names that are found refer to specializations of
8612 the same class template, and if the name is followed by a
8613 template-argument-list, the reference refers to the class template
8614 itself and not a specialization thereof, and is not ambiguous. */
8615 if (TREE_CODE (decl) == TREE_LIST)
8616 {
8617 tree t, tmpl = NULL_TREE;
8618 for (t = decl; t; t = TREE_CHAIN (t))
8619 {
8620 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8621 if (!tmpl)
8622 tmpl = elt;
8623 else if (tmpl != elt)
8624 break;
8625 }
8626 if (tmpl && t == NULL_TREE)
8627 return tmpl;
8628 else
8629 return decl;
8630 }
8631
8632 return (decl != NULL_TREE
8633 && DECL_SELF_REFERENCE_P (decl)
8634 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8635 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8636 }
8637
8638 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8639 parameters, find the desired type.
8640
8641 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8642
8643 IN_DECL, if non-NULL, is the template declaration we are trying to
8644 instantiate.
8645
8646 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8647 the class we are looking up.
8648
8649 Issue error and warning messages under control of COMPLAIN.
8650
8651 If the template class is really a local class in a template
8652 function, then the FUNCTION_CONTEXT is the function in which it is
8653 being instantiated.
8654
8655 ??? Note that this function is currently called *twice* for each
8656 template-id: the first time from the parser, while creating the
8657 incomplete type (finish_template_type), and the second type during the
8658 real instantiation (instantiate_template_class). This is surely something
8659 that we want to avoid. It also causes some problems with argument
8660 coercion (see convert_nontype_argument for more information on this). */
8661
8662 static tree
8663 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8664 int entering_scope, tsubst_flags_t complain)
8665 {
8666 tree templ = NULL_TREE, parmlist;
8667 tree t;
8668 spec_entry **slot;
8669 spec_entry *entry;
8670 spec_entry elt;
8671 hashval_t hash;
8672
8673 if (identifier_p (d1))
8674 {
8675 tree value = innermost_non_namespace_value (d1);
8676 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8677 templ = value;
8678 else
8679 {
8680 if (context)
8681 push_decl_namespace (context);
8682 templ = lookup_name (d1);
8683 templ = maybe_get_template_decl_from_type_decl (templ);
8684 if (context)
8685 pop_decl_namespace ();
8686 }
8687 if (templ)
8688 context = DECL_CONTEXT (templ);
8689 }
8690 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8691 {
8692 tree type = TREE_TYPE (d1);
8693
8694 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8695 an implicit typename for the second A. Deal with it. */
8696 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8697 type = TREE_TYPE (type);
8698
8699 if (CLASSTYPE_TEMPLATE_INFO (type))
8700 {
8701 templ = CLASSTYPE_TI_TEMPLATE (type);
8702 d1 = DECL_NAME (templ);
8703 }
8704 }
8705 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8706 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8707 {
8708 templ = TYPE_TI_TEMPLATE (d1);
8709 d1 = DECL_NAME (templ);
8710 }
8711 else if (DECL_TYPE_TEMPLATE_P (d1))
8712 {
8713 templ = d1;
8714 d1 = DECL_NAME (templ);
8715 context = DECL_CONTEXT (templ);
8716 }
8717 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8718 {
8719 templ = d1;
8720 d1 = DECL_NAME (templ);
8721 }
8722
8723 /* Issue an error message if we didn't find a template. */
8724 if (! templ)
8725 {
8726 if (complain & tf_error)
8727 error ("%qT is not a template", d1);
8728 return error_mark_node;
8729 }
8730
8731 if (TREE_CODE (templ) != TEMPLATE_DECL
8732 /* Make sure it's a user visible template, if it was named by
8733 the user. */
8734 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8735 && !PRIMARY_TEMPLATE_P (templ)))
8736 {
8737 if (complain & tf_error)
8738 {
8739 error ("non-template type %qT used as a template", d1);
8740 if (in_decl)
8741 error ("for template declaration %q+D", in_decl);
8742 }
8743 return error_mark_node;
8744 }
8745
8746 complain &= ~tf_user;
8747
8748 /* An alias that just changes the name of a template is equivalent to the
8749 other template, so if any of the arguments are pack expansions, strip
8750 the alias to avoid problems with a pack expansion passed to a non-pack
8751 alias template parameter (DR 1430). */
8752 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8753 templ = get_underlying_template (templ);
8754
8755 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8756 {
8757 tree parm;
8758 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8759 if (arglist2 == error_mark_node
8760 || (!uses_template_parms (arglist2)
8761 && check_instantiated_args (templ, arglist2, complain)))
8762 return error_mark_node;
8763
8764 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8765 return parm;
8766 }
8767 else
8768 {
8769 tree template_type = TREE_TYPE (templ);
8770 tree gen_tmpl;
8771 tree type_decl;
8772 tree found = NULL_TREE;
8773 int arg_depth;
8774 int parm_depth;
8775 int is_dependent_type;
8776 int use_partial_inst_tmpl = false;
8777
8778 if (template_type == error_mark_node)
8779 /* An error occurred while building the template TEMPL, and a
8780 diagnostic has most certainly been emitted for that
8781 already. Let's propagate that error. */
8782 return error_mark_node;
8783
8784 gen_tmpl = most_general_template (templ);
8785 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8786 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8787 arg_depth = TMPL_ARGS_DEPTH (arglist);
8788
8789 if (arg_depth == 1 && parm_depth > 1)
8790 {
8791 /* We've been given an incomplete set of template arguments.
8792 For example, given:
8793
8794 template <class T> struct S1 {
8795 template <class U> struct S2 {};
8796 template <class U> struct S2<U*> {};
8797 };
8798
8799 we will be called with an ARGLIST of `U*', but the
8800 TEMPLATE will be `template <class T> template
8801 <class U> struct S1<T>::S2'. We must fill in the missing
8802 arguments. */
8803 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8804 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8805 arg_depth = TMPL_ARGS_DEPTH (arglist);
8806 }
8807
8808 /* Now we should have enough arguments. */
8809 gcc_assert (parm_depth == arg_depth);
8810
8811 /* From here on, we're only interested in the most general
8812 template. */
8813
8814 /* Calculate the BOUND_ARGS. These will be the args that are
8815 actually tsubst'd into the definition to create the
8816 instantiation. */
8817 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8818 complain,
8819 /*require_all_args=*/true,
8820 /*use_default_args=*/true);
8821
8822 if (arglist == error_mark_node)
8823 /* We were unable to bind the arguments. */
8824 return error_mark_node;
8825
8826 /* In the scope of a template class, explicit references to the
8827 template class refer to the type of the template, not any
8828 instantiation of it. For example, in:
8829
8830 template <class T> class C { void f(C<T>); }
8831
8832 the `C<T>' is just the same as `C'. Outside of the
8833 class, however, such a reference is an instantiation. */
8834 if (entering_scope
8835 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8836 || currently_open_class (template_type))
8837 {
8838 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8839
8840 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8841 return template_type;
8842 }
8843
8844 /* If we already have this specialization, return it. */
8845 elt.tmpl = gen_tmpl;
8846 elt.args = arglist;
8847 elt.spec = NULL_TREE;
8848 hash = spec_hasher::hash (&elt);
8849 entry = type_specializations->find_with_hash (&elt, hash);
8850
8851 if (entry)
8852 return entry->spec;
8853
8854 /* If the the template's constraints are not satisfied,
8855 then we cannot form a valid type.
8856
8857 Note that the check is deferred until after the hash
8858 lookup. This prevents redundant checks on previously
8859 instantiated specializations. */
8860 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8861 {
8862 if (complain & tf_error)
8863 {
8864 error ("template constraint failure");
8865 diagnose_constraints (input_location, gen_tmpl, arglist);
8866 }
8867 return error_mark_node;
8868 }
8869
8870 is_dependent_type = uses_template_parms (arglist);
8871
8872 /* If the deduced arguments are invalid, then the binding
8873 failed. */
8874 if (!is_dependent_type
8875 && check_instantiated_args (gen_tmpl,
8876 INNERMOST_TEMPLATE_ARGS (arglist),
8877 complain))
8878 return error_mark_node;
8879
8880 if (!is_dependent_type
8881 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8882 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8883 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8884 {
8885 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8886 DECL_NAME (gen_tmpl),
8887 /*tag_scope=*/ts_global);
8888 return found;
8889 }
8890
8891 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8892 complain, in_decl);
8893 if (context == error_mark_node)
8894 return error_mark_node;
8895
8896 if (!context)
8897 context = global_namespace;
8898
8899 /* Create the type. */
8900 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8901 {
8902 /* The user referred to a specialization of an alias
8903 template represented by GEN_TMPL.
8904
8905 [temp.alias]/2 says:
8906
8907 When a template-id refers to the specialization of an
8908 alias template, it is equivalent to the associated
8909 type obtained by substitution of its
8910 template-arguments for the template-parameters in the
8911 type-id of the alias template. */
8912
8913 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8914 /* Note that the call above (by indirectly calling
8915 register_specialization in tsubst_decl) registers the
8916 TYPE_DECL representing the specialization of the alias
8917 template. So next time someone substitutes ARGLIST for
8918 the template parms into the alias template (GEN_TMPL),
8919 she'll get that TYPE_DECL back. */
8920
8921 if (t == error_mark_node)
8922 return t;
8923 }
8924 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8925 {
8926 if (!is_dependent_type)
8927 {
8928 set_current_access_from_decl (TYPE_NAME (template_type));
8929 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8930 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8931 arglist, complain, in_decl),
8932 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8933 arglist, complain, in_decl),
8934 SCOPED_ENUM_P (template_type), NULL);
8935
8936 if (t == error_mark_node)
8937 return t;
8938 }
8939 else
8940 {
8941 /* We don't want to call start_enum for this type, since
8942 the values for the enumeration constants may involve
8943 template parameters. And, no one should be interested
8944 in the enumeration constants for such a type. */
8945 t = cxx_make_type (ENUMERAL_TYPE);
8946 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8947 }
8948 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8949 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8950 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8951 }
8952 else if (CLASS_TYPE_P (template_type))
8953 {
8954 t = make_class_type (TREE_CODE (template_type));
8955 CLASSTYPE_DECLARED_CLASS (t)
8956 = CLASSTYPE_DECLARED_CLASS (template_type);
8957 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8958
8959 /* A local class. Make sure the decl gets registered properly. */
8960 if (context == current_function_decl)
8961 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8962
8963 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8964 /* This instantiation is another name for the primary
8965 template type. Set the TYPE_CANONICAL field
8966 appropriately. */
8967 TYPE_CANONICAL (t) = template_type;
8968 else if (any_template_arguments_need_structural_equality_p (arglist))
8969 /* Some of the template arguments require structural
8970 equality testing, so this template class requires
8971 structural equality testing. */
8972 SET_TYPE_STRUCTURAL_EQUALITY (t);
8973 }
8974 else
8975 gcc_unreachable ();
8976
8977 /* If we called start_enum or pushtag above, this information
8978 will already be set up. */
8979 if (!TYPE_NAME (t))
8980 {
8981 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8982
8983 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8984 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8985 DECL_SOURCE_LOCATION (type_decl)
8986 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8987 }
8988 else
8989 type_decl = TYPE_NAME (t);
8990
8991 if (CLASS_TYPE_P (template_type))
8992 {
8993 TREE_PRIVATE (type_decl)
8994 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8995 TREE_PROTECTED (type_decl)
8996 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8997 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8998 {
8999 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9000 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9001 }
9002 }
9003
9004 if (OVERLOAD_TYPE_P (t)
9005 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9006 {
9007 static const char *tags[] = {"abi_tag", "may_alias"};
9008
9009 for (unsigned ix = 0; ix != 2; ix++)
9010 {
9011 tree attributes
9012 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9013
9014 if (attributes)
9015 TYPE_ATTRIBUTES (t)
9016 = tree_cons (TREE_PURPOSE (attributes),
9017 TREE_VALUE (attributes),
9018 TYPE_ATTRIBUTES (t));
9019 }
9020 }
9021
9022 /* Let's consider the explicit specialization of a member
9023 of a class template specialization that is implicitly instantiated,
9024 e.g.:
9025 template<class T>
9026 struct S
9027 {
9028 template<class U> struct M {}; //#0
9029 };
9030
9031 template<>
9032 template<>
9033 struct S<int>::M<char> //#1
9034 {
9035 int i;
9036 };
9037 [temp.expl.spec]/4 says this is valid.
9038
9039 In this case, when we write:
9040 S<int>::M<char> m;
9041
9042 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9043 the one of #0.
9044
9045 When we encounter #1, we want to store the partial instantiation
9046 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9047
9048 For all cases other than this "explicit specialization of member of a
9049 class template", we just want to store the most general template into
9050 the CLASSTYPE_TI_TEMPLATE of M.
9051
9052 This case of "explicit specialization of member of a class template"
9053 only happens when:
9054 1/ the enclosing class is an instantiation of, and therefore not
9055 the same as, the context of the most general template, and
9056 2/ we aren't looking at the partial instantiation itself, i.e.
9057 the innermost arguments are not the same as the innermost parms of
9058 the most general template.
9059
9060 So it's only when 1/ and 2/ happens that we want to use the partial
9061 instantiation of the member template in lieu of its most general
9062 template. */
9063
9064 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9065 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9066 /* the enclosing class must be an instantiation... */
9067 && CLASS_TYPE_P (context)
9068 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9069 {
9070 TREE_VEC_LENGTH (arglist)--;
9071 ++processing_template_decl;
9072 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9073 tree partial_inst_args =
9074 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9075 arglist, complain, NULL_TREE);
9076 --processing_template_decl;
9077 TREE_VEC_LENGTH (arglist)++;
9078 if (partial_inst_args == error_mark_node)
9079 return error_mark_node;
9080 use_partial_inst_tmpl =
9081 /*...and we must not be looking at the partial instantiation
9082 itself. */
9083 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9084 partial_inst_args);
9085 }
9086
9087 if (!use_partial_inst_tmpl)
9088 /* This case is easy; there are no member templates involved. */
9089 found = gen_tmpl;
9090 else
9091 {
9092 /* This is a full instantiation of a member template. Find
9093 the partial instantiation of which this is an instance. */
9094
9095 /* Temporarily reduce by one the number of levels in the ARGLIST
9096 so as to avoid comparing the last set of arguments. */
9097 TREE_VEC_LENGTH (arglist)--;
9098 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9099 TREE_VEC_LENGTH (arglist)++;
9100 /* FOUND is either a proper class type, or an alias
9101 template specialization. In the later case, it's a
9102 TYPE_DECL, resulting from the substituting of arguments
9103 for parameters in the TYPE_DECL of the alias template
9104 done earlier. So be careful while getting the template
9105 of FOUND. */
9106 found = (TREE_CODE (found) == TEMPLATE_DECL
9107 ? found
9108 : (TREE_CODE (found) == TYPE_DECL
9109 ? DECL_TI_TEMPLATE (found)
9110 : CLASSTYPE_TI_TEMPLATE (found)));
9111 }
9112
9113 // Build template info for the new specialization.
9114 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9115
9116 elt.spec = t;
9117 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9118 entry = ggc_alloc<spec_entry> ();
9119 *entry = elt;
9120 *slot = entry;
9121
9122 /* Note this use of the partial instantiation so we can check it
9123 later in maybe_process_partial_specialization. */
9124 DECL_TEMPLATE_INSTANTIATIONS (found)
9125 = tree_cons (arglist, t,
9126 DECL_TEMPLATE_INSTANTIATIONS (found));
9127
9128 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9129 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9130 /* Now that the type has been registered on the instantiations
9131 list, we set up the enumerators. Because the enumeration
9132 constants may involve the enumeration type itself, we make
9133 sure to register the type first, and then create the
9134 constants. That way, doing tsubst_expr for the enumeration
9135 constants won't result in recursive calls here; we'll find
9136 the instantiation and exit above. */
9137 tsubst_enum (template_type, t, arglist);
9138
9139 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9140 /* If the type makes use of template parameters, the
9141 code that generates debugging information will crash. */
9142 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9143
9144 /* Possibly limit visibility based on template args. */
9145 TREE_PUBLIC (type_decl) = 1;
9146 determine_visibility (type_decl);
9147
9148 inherit_targ_abi_tags (t);
9149
9150 return t;
9151 }
9152 }
9153
9154 /* Wrapper for lookup_template_class_1. */
9155
9156 tree
9157 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9158 int entering_scope, tsubst_flags_t complain)
9159 {
9160 tree ret;
9161 timevar_push (TV_TEMPLATE_INST);
9162 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9163 entering_scope, complain);
9164 timevar_pop (TV_TEMPLATE_INST);
9165 return ret;
9166 }
9167
9168 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9169
9170 tree
9171 lookup_template_variable (tree templ, tree arglist)
9172 {
9173 /* The type of the expression is NULL_TREE since the template-id could refer
9174 to an explicit or partial specialization. */
9175 tree type = NULL_TREE;
9176 if (flag_concepts && variable_concept_p (templ))
9177 /* Except that concepts are always bool. */
9178 type = boolean_type_node;
9179 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9180 }
9181
9182 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9183
9184 tree
9185 finish_template_variable (tree var, tsubst_flags_t complain)
9186 {
9187 tree templ = TREE_OPERAND (var, 0);
9188 tree arglist = TREE_OPERAND (var, 1);
9189
9190 /* We never want to return a VAR_DECL for a variable concept, since they
9191 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9192 bool concept_p = flag_concepts && variable_concept_p (templ);
9193 if (concept_p && processing_template_decl)
9194 return var;
9195
9196 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9197 arglist = add_outermost_template_args (tmpl_args, arglist);
9198
9199 templ = most_general_template (templ);
9200 tree parms = DECL_TEMPLATE_PARMS (templ);
9201 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9202 /*req_all*/true,
9203 /*use_default*/true);
9204
9205 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9206 {
9207 if (complain & tf_error)
9208 {
9209 error ("use of invalid variable template %qE", var);
9210 diagnose_constraints (location_of (var), templ, arglist);
9211 }
9212 return error_mark_node;
9213 }
9214
9215 /* If a template-id refers to a specialization of a variable
9216 concept, then the expression is true if and only if the
9217 concept's constraints are satisfied by the given template
9218 arguments.
9219
9220 NOTE: This is an extension of Concepts Lite TS that
9221 allows constraints to be used in expressions. */
9222 if (concept_p)
9223 {
9224 tree decl = DECL_TEMPLATE_RESULT (templ);
9225 return evaluate_variable_concept (decl, arglist);
9226 }
9227
9228 return instantiate_template (templ, arglist, complain);
9229 }
9230
9231 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9232 TARGS template args, and instantiate it if it's not dependent. */
9233
9234 tree
9235 lookup_and_finish_template_variable (tree templ, tree targs,
9236 tsubst_flags_t complain)
9237 {
9238 templ = lookup_template_variable (templ, targs);
9239 if (!any_dependent_template_arguments_p (targs))
9240 {
9241 templ = finish_template_variable (templ, complain);
9242 mark_used (templ);
9243 }
9244
9245 return convert_from_reference (templ);
9246 }
9247
9248 \f
9249 struct pair_fn_data
9250 {
9251 tree_fn_t fn;
9252 tree_fn_t any_fn;
9253 void *data;
9254 /* True when we should also visit template parameters that occur in
9255 non-deduced contexts. */
9256 bool include_nondeduced_p;
9257 hash_set<tree> *visited;
9258 };
9259
9260 /* Called from for_each_template_parm via walk_tree. */
9261
9262 static tree
9263 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9264 {
9265 tree t = *tp;
9266 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9267 tree_fn_t fn = pfd->fn;
9268 void *data = pfd->data;
9269 tree result = NULL_TREE;
9270
9271 #define WALK_SUBTREE(NODE) \
9272 do \
9273 { \
9274 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9275 pfd->include_nondeduced_p, \
9276 pfd->any_fn); \
9277 if (result) goto out; \
9278 } \
9279 while (0)
9280
9281 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9282 return t;
9283
9284 if (TYPE_P (t)
9285 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9286 WALK_SUBTREE (TYPE_CONTEXT (t));
9287
9288 switch (TREE_CODE (t))
9289 {
9290 case RECORD_TYPE:
9291 if (TYPE_PTRMEMFUNC_P (t))
9292 break;
9293 /* Fall through. */
9294
9295 case UNION_TYPE:
9296 case ENUMERAL_TYPE:
9297 if (!TYPE_TEMPLATE_INFO (t))
9298 *walk_subtrees = 0;
9299 else
9300 WALK_SUBTREE (TYPE_TI_ARGS (t));
9301 break;
9302
9303 case INTEGER_TYPE:
9304 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9305 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9306 break;
9307
9308 case METHOD_TYPE:
9309 /* Since we're not going to walk subtrees, we have to do this
9310 explicitly here. */
9311 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9312 /* Fall through. */
9313
9314 case FUNCTION_TYPE:
9315 /* Check the return type. */
9316 WALK_SUBTREE (TREE_TYPE (t));
9317
9318 /* Check the parameter types. Since default arguments are not
9319 instantiated until they are needed, the TYPE_ARG_TYPES may
9320 contain expressions that involve template parameters. But,
9321 no-one should be looking at them yet. And, once they're
9322 instantiated, they don't contain template parameters, so
9323 there's no point in looking at them then, either. */
9324 {
9325 tree parm;
9326
9327 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9328 WALK_SUBTREE (TREE_VALUE (parm));
9329
9330 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9331 want walk_tree walking into them itself. */
9332 *walk_subtrees = 0;
9333 }
9334
9335 if (flag_noexcept_type)
9336 {
9337 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9338 if (spec)
9339 WALK_SUBTREE (TREE_PURPOSE (spec));
9340 }
9341 break;
9342
9343 case TYPEOF_TYPE:
9344 case UNDERLYING_TYPE:
9345 if (pfd->include_nondeduced_p
9346 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9347 pfd->visited,
9348 pfd->include_nondeduced_p,
9349 pfd->any_fn))
9350 return error_mark_node;
9351 break;
9352
9353 case FUNCTION_DECL:
9354 case VAR_DECL:
9355 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9356 WALK_SUBTREE (DECL_TI_ARGS (t));
9357 /* Fall through. */
9358
9359 case PARM_DECL:
9360 case CONST_DECL:
9361 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9362 WALK_SUBTREE (DECL_INITIAL (t));
9363 if (DECL_CONTEXT (t)
9364 && pfd->include_nondeduced_p)
9365 WALK_SUBTREE (DECL_CONTEXT (t));
9366 break;
9367
9368 case BOUND_TEMPLATE_TEMPLATE_PARM:
9369 /* Record template parameters such as `T' inside `TT<T>'. */
9370 WALK_SUBTREE (TYPE_TI_ARGS (t));
9371 /* Fall through. */
9372
9373 case TEMPLATE_TEMPLATE_PARM:
9374 case TEMPLATE_TYPE_PARM:
9375 case TEMPLATE_PARM_INDEX:
9376 if (fn && (*fn)(t, data))
9377 return t;
9378 else if (!fn)
9379 return t;
9380 break;
9381
9382 case TEMPLATE_DECL:
9383 /* A template template parameter is encountered. */
9384 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9385 WALK_SUBTREE (TREE_TYPE (t));
9386
9387 /* Already substituted template template parameter */
9388 *walk_subtrees = 0;
9389 break;
9390
9391 case TYPENAME_TYPE:
9392 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9393 partial instantiation. */
9394 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9395 break;
9396
9397 case CONSTRUCTOR:
9398 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9399 && pfd->include_nondeduced_p)
9400 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9401 break;
9402
9403 case INDIRECT_REF:
9404 case COMPONENT_REF:
9405 /* If there's no type, then this thing must be some expression
9406 involving template parameters. */
9407 if (!fn && !TREE_TYPE (t))
9408 return error_mark_node;
9409 break;
9410
9411 case MODOP_EXPR:
9412 case CAST_EXPR:
9413 case IMPLICIT_CONV_EXPR:
9414 case REINTERPRET_CAST_EXPR:
9415 case CONST_CAST_EXPR:
9416 case STATIC_CAST_EXPR:
9417 case DYNAMIC_CAST_EXPR:
9418 case ARROW_EXPR:
9419 case DOTSTAR_EXPR:
9420 case TYPEID_EXPR:
9421 case PSEUDO_DTOR_EXPR:
9422 if (!fn)
9423 return error_mark_node;
9424 break;
9425
9426 default:
9427 break;
9428 }
9429
9430 #undef WALK_SUBTREE
9431
9432 /* We didn't find any template parameters we liked. */
9433 out:
9434 return result;
9435 }
9436
9437 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9438 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9439 call FN with the parameter and the DATA.
9440 If FN returns nonzero, the iteration is terminated, and
9441 for_each_template_parm returns 1. Otherwise, the iteration
9442 continues. If FN never returns a nonzero value, the value
9443 returned by for_each_template_parm is 0. If FN is NULL, it is
9444 considered to be the function which always returns 1.
9445
9446 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9447 parameters that occur in non-deduced contexts. When false, only
9448 visits those template parameters that can be deduced. */
9449
9450 static tree
9451 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9452 hash_set<tree> *visited,
9453 bool include_nondeduced_p,
9454 tree_fn_t any_fn)
9455 {
9456 struct pair_fn_data pfd;
9457 tree result;
9458
9459 /* Set up. */
9460 pfd.fn = fn;
9461 pfd.any_fn = any_fn;
9462 pfd.data = data;
9463 pfd.include_nondeduced_p = include_nondeduced_p;
9464
9465 /* Walk the tree. (Conceptually, we would like to walk without
9466 duplicates, but for_each_template_parm_r recursively calls
9467 for_each_template_parm, so we would need to reorganize a fair
9468 bit to use walk_tree_without_duplicates, so we keep our own
9469 visited list.) */
9470 if (visited)
9471 pfd.visited = visited;
9472 else
9473 pfd.visited = new hash_set<tree>;
9474 result = cp_walk_tree (&t,
9475 for_each_template_parm_r,
9476 &pfd,
9477 pfd.visited);
9478
9479 /* Clean up. */
9480 if (!visited)
9481 {
9482 delete pfd.visited;
9483 pfd.visited = 0;
9484 }
9485
9486 return result;
9487 }
9488
9489 /* Returns true if T depends on any template parameter. */
9490
9491 int
9492 uses_template_parms (tree t)
9493 {
9494 if (t == NULL_TREE)
9495 return false;
9496
9497 bool dependent_p;
9498 int saved_processing_template_decl;
9499
9500 saved_processing_template_decl = processing_template_decl;
9501 if (!saved_processing_template_decl)
9502 processing_template_decl = 1;
9503 if (TYPE_P (t))
9504 dependent_p = dependent_type_p (t);
9505 else if (TREE_CODE (t) == TREE_VEC)
9506 dependent_p = any_dependent_template_arguments_p (t);
9507 else if (TREE_CODE (t) == TREE_LIST)
9508 dependent_p = (uses_template_parms (TREE_VALUE (t))
9509 || uses_template_parms (TREE_CHAIN (t)));
9510 else if (TREE_CODE (t) == TYPE_DECL)
9511 dependent_p = dependent_type_p (TREE_TYPE (t));
9512 else if (DECL_P (t)
9513 || EXPR_P (t)
9514 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9515 || TREE_CODE (t) == OVERLOAD
9516 || BASELINK_P (t)
9517 || identifier_p (t)
9518 || TREE_CODE (t) == TRAIT_EXPR
9519 || TREE_CODE (t) == CONSTRUCTOR
9520 || CONSTANT_CLASS_P (t))
9521 dependent_p = (type_dependent_expression_p (t)
9522 || value_dependent_expression_p (t));
9523 else
9524 {
9525 gcc_assert (t == error_mark_node);
9526 dependent_p = false;
9527 }
9528
9529 processing_template_decl = saved_processing_template_decl;
9530
9531 return dependent_p;
9532 }
9533
9534 /* Returns true iff current_function_decl is an incompletely instantiated
9535 template. Useful instead of processing_template_decl because the latter
9536 is set to 0 during instantiate_non_dependent_expr. */
9537
9538 bool
9539 in_template_function (void)
9540 {
9541 tree fn = current_function_decl;
9542 bool ret;
9543 ++processing_template_decl;
9544 ret = (fn && DECL_LANG_SPECIFIC (fn)
9545 && DECL_TEMPLATE_INFO (fn)
9546 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9547 --processing_template_decl;
9548 return ret;
9549 }
9550
9551 /* Returns true if T depends on any template parameter with level LEVEL. */
9552
9553 bool
9554 uses_template_parms_level (tree t, int level)
9555 {
9556 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9557 /*include_nondeduced_p=*/true);
9558 }
9559
9560 /* Returns true if the signature of DECL depends on any template parameter from
9561 its enclosing class. */
9562
9563 bool
9564 uses_outer_template_parms (tree decl)
9565 {
9566 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9567 if (depth == 0)
9568 return false;
9569 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9570 &depth, NULL, /*include_nondeduced_p=*/true))
9571 return true;
9572 if (PRIMARY_TEMPLATE_P (decl)
9573 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9574 (DECL_TEMPLATE_PARMS (decl)),
9575 template_parm_outer_level,
9576 &depth, NULL, /*include_nondeduced_p=*/true))
9577 return true;
9578 tree ci = get_constraints (decl);
9579 if (ci)
9580 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9581 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9582 &depth, NULL, /*nondeduced*/true))
9583 return true;
9584 return false;
9585 }
9586
9587 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9588 ill-formed translation unit, i.e. a variable or function that isn't
9589 usable in a constant expression. */
9590
9591 static inline bool
9592 neglectable_inst_p (tree d)
9593 {
9594 return (DECL_P (d)
9595 && !undeduced_auto_decl (d)
9596 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9597 : decl_maybe_constant_var_p (d)));
9598 }
9599
9600 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9601 neglectable and instantiated from within an erroneous instantiation. */
9602
9603 static bool
9604 limit_bad_template_recursion (tree decl)
9605 {
9606 struct tinst_level *lev = current_tinst_level;
9607 int errs = errorcount + sorrycount;
9608 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9609 return false;
9610
9611 for (; lev; lev = lev->next)
9612 if (neglectable_inst_p (lev->decl))
9613 break;
9614
9615 return (lev && errs > lev->errors);
9616 }
9617
9618 static int tinst_depth;
9619 extern int max_tinst_depth;
9620 int depth_reached;
9621
9622 static GTY(()) struct tinst_level *last_error_tinst_level;
9623
9624 /* We're starting to instantiate D; record the template instantiation context
9625 for diagnostics and to restore it later. */
9626
9627 bool
9628 push_tinst_level (tree d)
9629 {
9630 return push_tinst_level_loc (d, input_location);
9631 }
9632
9633 /* We're starting to instantiate D; record the template instantiation context
9634 at LOC for diagnostics and to restore it later. */
9635
9636 bool
9637 push_tinst_level_loc (tree d, location_t loc)
9638 {
9639 struct tinst_level *new_level;
9640
9641 if (tinst_depth >= max_tinst_depth)
9642 {
9643 /* Tell error.c not to try to instantiate any templates. */
9644 at_eof = 2;
9645 fatal_error (input_location,
9646 "template instantiation depth exceeds maximum of %d"
9647 " (use -ftemplate-depth= to increase the maximum)",
9648 max_tinst_depth);
9649 return false;
9650 }
9651
9652 /* If the current instantiation caused problems, don't let it instantiate
9653 anything else. Do allow deduction substitution and decls usable in
9654 constant expressions. */
9655 if (limit_bad_template_recursion (d))
9656 return false;
9657
9658 /* When not -quiet, dump template instantiations other than functions, since
9659 announce_function will take care of those. */
9660 if (!quiet_flag
9661 && TREE_CODE (d) != TREE_LIST
9662 && TREE_CODE (d) != FUNCTION_DECL)
9663 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9664
9665 new_level = ggc_alloc<tinst_level> ();
9666 new_level->decl = d;
9667 new_level->locus = loc;
9668 new_level->errors = errorcount+sorrycount;
9669 new_level->in_system_header_p = in_system_header_at (input_location);
9670 new_level->next = current_tinst_level;
9671 current_tinst_level = new_level;
9672
9673 ++tinst_depth;
9674 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9675 depth_reached = tinst_depth;
9676
9677 return true;
9678 }
9679
9680 /* We're done instantiating this template; return to the instantiation
9681 context. */
9682
9683 void
9684 pop_tinst_level (void)
9685 {
9686 /* Restore the filename and line number stashed away when we started
9687 this instantiation. */
9688 input_location = current_tinst_level->locus;
9689 current_tinst_level = current_tinst_level->next;
9690 --tinst_depth;
9691 }
9692
9693 /* We're instantiating a deferred template; restore the template
9694 instantiation context in which the instantiation was requested, which
9695 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9696
9697 static tree
9698 reopen_tinst_level (struct tinst_level *level)
9699 {
9700 struct tinst_level *t;
9701
9702 tinst_depth = 0;
9703 for (t = level; t; t = t->next)
9704 ++tinst_depth;
9705
9706 current_tinst_level = level;
9707 pop_tinst_level ();
9708 if (current_tinst_level)
9709 current_tinst_level->errors = errorcount+sorrycount;
9710 return level->decl;
9711 }
9712
9713 /* Returns the TINST_LEVEL which gives the original instantiation
9714 context. */
9715
9716 struct tinst_level *
9717 outermost_tinst_level (void)
9718 {
9719 struct tinst_level *level = current_tinst_level;
9720 if (level)
9721 while (level->next)
9722 level = level->next;
9723 return level;
9724 }
9725
9726 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9727 vector of template arguments, as for tsubst.
9728
9729 Returns an appropriate tsubst'd friend declaration. */
9730
9731 static tree
9732 tsubst_friend_function (tree decl, tree args)
9733 {
9734 tree new_friend;
9735
9736 if (TREE_CODE (decl) == FUNCTION_DECL
9737 && DECL_TEMPLATE_INSTANTIATION (decl)
9738 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9739 /* This was a friend declared with an explicit template
9740 argument list, e.g.:
9741
9742 friend void f<>(T);
9743
9744 to indicate that f was a template instantiation, not a new
9745 function declaration. Now, we have to figure out what
9746 instantiation of what template. */
9747 {
9748 tree template_id, arglist, fns;
9749 tree new_args;
9750 tree tmpl;
9751 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9752
9753 /* Friend functions are looked up in the containing namespace scope.
9754 We must enter that scope, to avoid finding member functions of the
9755 current class with same name. */
9756 push_nested_namespace (ns);
9757 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9758 tf_warning_or_error, NULL_TREE,
9759 /*integral_constant_expression_p=*/false);
9760 pop_nested_namespace (ns);
9761 arglist = tsubst (DECL_TI_ARGS (decl), args,
9762 tf_warning_or_error, NULL_TREE);
9763 template_id = lookup_template_function (fns, arglist);
9764
9765 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9766 tmpl = determine_specialization (template_id, new_friend,
9767 &new_args,
9768 /*need_member_template=*/0,
9769 TREE_VEC_LENGTH (args),
9770 tsk_none);
9771 return instantiate_template (tmpl, new_args, tf_error);
9772 }
9773
9774 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9775
9776 /* The NEW_FRIEND will look like an instantiation, to the
9777 compiler, but is not an instantiation from the point of view of
9778 the language. For example, we might have had:
9779
9780 template <class T> struct S {
9781 template <class U> friend void f(T, U);
9782 };
9783
9784 Then, in S<int>, template <class U> void f(int, U) is not an
9785 instantiation of anything. */
9786 if (new_friend == error_mark_node)
9787 return error_mark_node;
9788
9789 DECL_USE_TEMPLATE (new_friend) = 0;
9790 if (TREE_CODE (decl) == TEMPLATE_DECL)
9791 {
9792 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9793 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9794 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9795 }
9796
9797 /* The mangled name for the NEW_FRIEND is incorrect. The function
9798 is not a template instantiation and should not be mangled like
9799 one. Therefore, we forget the mangling here; we'll recompute it
9800 later if we need it. */
9801 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9802 {
9803 SET_DECL_RTL (new_friend, NULL);
9804 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9805 }
9806
9807 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9808 {
9809 tree old_decl;
9810 tree new_friend_template_info;
9811 tree new_friend_result_template_info;
9812 tree ns;
9813 int new_friend_is_defn;
9814
9815 /* We must save some information from NEW_FRIEND before calling
9816 duplicate decls since that function will free NEW_FRIEND if
9817 possible. */
9818 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9819 new_friend_is_defn =
9820 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9821 (template_for_substitution (new_friend)))
9822 != NULL_TREE);
9823 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9824 {
9825 /* This declaration is a `primary' template. */
9826 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9827
9828 new_friend_result_template_info
9829 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9830 }
9831 else
9832 new_friend_result_template_info = NULL_TREE;
9833
9834 /* Inside pushdecl_namespace_level, we will push into the
9835 current namespace. However, the friend function should go
9836 into the namespace of the template. */
9837 ns = decl_namespace_context (new_friend);
9838 push_nested_namespace (ns);
9839 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9840 pop_nested_namespace (ns);
9841
9842 if (old_decl == error_mark_node)
9843 return error_mark_node;
9844
9845 if (old_decl != new_friend)
9846 {
9847 /* This new friend declaration matched an existing
9848 declaration. For example, given:
9849
9850 template <class T> void f(T);
9851 template <class U> class C {
9852 template <class T> friend void f(T) {}
9853 };
9854
9855 the friend declaration actually provides the definition
9856 of `f', once C has been instantiated for some type. So,
9857 old_decl will be the out-of-class template declaration,
9858 while new_friend is the in-class definition.
9859
9860 But, if `f' was called before this point, the
9861 instantiation of `f' will have DECL_TI_ARGS corresponding
9862 to `T' but not to `U', references to which might appear
9863 in the definition of `f'. Previously, the most general
9864 template for an instantiation of `f' was the out-of-class
9865 version; now it is the in-class version. Therefore, we
9866 run through all specialization of `f', adding to their
9867 DECL_TI_ARGS appropriately. In particular, they need a
9868 new set of outer arguments, corresponding to the
9869 arguments for this class instantiation.
9870
9871 The same situation can arise with something like this:
9872
9873 friend void f(int);
9874 template <class T> class C {
9875 friend void f(T) {}
9876 };
9877
9878 when `C<int>' is instantiated. Now, `f(int)' is defined
9879 in the class. */
9880
9881 if (!new_friend_is_defn)
9882 /* On the other hand, if the in-class declaration does
9883 *not* provide a definition, then we don't want to alter
9884 existing definitions. We can just leave everything
9885 alone. */
9886 ;
9887 else
9888 {
9889 tree new_template = TI_TEMPLATE (new_friend_template_info);
9890 tree new_args = TI_ARGS (new_friend_template_info);
9891
9892 /* Overwrite whatever template info was there before, if
9893 any, with the new template information pertaining to
9894 the declaration. */
9895 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9896
9897 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9898 {
9899 /* We should have called reregister_specialization in
9900 duplicate_decls. */
9901 gcc_assert (retrieve_specialization (new_template,
9902 new_args, 0)
9903 == old_decl);
9904
9905 /* Instantiate it if the global has already been used. */
9906 if (DECL_ODR_USED (old_decl))
9907 instantiate_decl (old_decl, /*defer_ok=*/true,
9908 /*expl_inst_class_mem_p=*/false);
9909 }
9910 else
9911 {
9912 tree t;
9913
9914 /* Indicate that the old function template is a partial
9915 instantiation. */
9916 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9917 = new_friend_result_template_info;
9918
9919 gcc_assert (new_template
9920 == most_general_template (new_template));
9921 gcc_assert (new_template != old_decl);
9922
9923 /* Reassign any specializations already in the hash table
9924 to the new more general template, and add the
9925 additional template args. */
9926 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9927 t != NULL_TREE;
9928 t = TREE_CHAIN (t))
9929 {
9930 tree spec = TREE_VALUE (t);
9931 spec_entry elt;
9932
9933 elt.tmpl = old_decl;
9934 elt.args = DECL_TI_ARGS (spec);
9935 elt.spec = NULL_TREE;
9936
9937 decl_specializations->remove_elt (&elt);
9938
9939 DECL_TI_ARGS (spec)
9940 = add_outermost_template_args (new_args,
9941 DECL_TI_ARGS (spec));
9942
9943 register_specialization
9944 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9945
9946 }
9947 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9948 }
9949 }
9950
9951 /* The information from NEW_FRIEND has been merged into OLD_DECL
9952 by duplicate_decls. */
9953 new_friend = old_decl;
9954 }
9955 }
9956 else
9957 {
9958 tree context = DECL_CONTEXT (new_friend);
9959 bool dependent_p;
9960
9961 /* In the code
9962 template <class T> class C {
9963 template <class U> friend void C1<U>::f (); // case 1
9964 friend void C2<T>::f (); // case 2
9965 };
9966 we only need to make sure CONTEXT is a complete type for
9967 case 2. To distinguish between the two cases, we note that
9968 CONTEXT of case 1 remains dependent type after tsubst while
9969 this isn't true for case 2. */
9970 ++processing_template_decl;
9971 dependent_p = dependent_type_p (context);
9972 --processing_template_decl;
9973
9974 if (!dependent_p
9975 && !complete_type_or_else (context, NULL_TREE))
9976 return error_mark_node;
9977
9978 if (COMPLETE_TYPE_P (context))
9979 {
9980 tree fn = new_friend;
9981 /* do_friend adds the TEMPLATE_DECL for any member friend
9982 template even if it isn't a member template, i.e.
9983 template <class T> friend A<T>::f();
9984 Look through it in that case. */
9985 if (TREE_CODE (fn) == TEMPLATE_DECL
9986 && !PRIMARY_TEMPLATE_P (fn))
9987 fn = DECL_TEMPLATE_RESULT (fn);
9988 /* Check to see that the declaration is really present, and,
9989 possibly obtain an improved declaration. */
9990 fn = check_classfn (context, fn, NULL_TREE);
9991
9992 if (fn)
9993 new_friend = fn;
9994 }
9995 }
9996
9997 return new_friend;
9998 }
9999
10000 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10001 template arguments, as for tsubst.
10002
10003 Returns an appropriate tsubst'd friend type or error_mark_node on
10004 failure. */
10005
10006 static tree
10007 tsubst_friend_class (tree friend_tmpl, tree args)
10008 {
10009 tree tmpl;
10010
10011 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10012 {
10013 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10014 return TREE_TYPE (tmpl);
10015 }
10016
10017 tree context = CP_DECL_CONTEXT (friend_tmpl);
10018 if (TREE_CODE (context) == NAMESPACE_DECL)
10019 push_nested_namespace (context);
10020 else
10021 push_nested_class (context);
10022
10023 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10024 /*non_class=*/false, /*block_p=*/false,
10025 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10026
10027 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10028 {
10029 /* The friend template has already been declared. Just
10030 check to see that the declarations match, and install any new
10031 default parameters. We must tsubst the default parameters,
10032 of course. We only need the innermost template parameters
10033 because that is all that redeclare_class_template will look
10034 at. */
10035 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10036 > TMPL_ARGS_DEPTH (args))
10037 {
10038 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10039 args, tf_warning_or_error);
10040 location_t saved_input_location = input_location;
10041 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10042 tree cons = get_constraints (tmpl);
10043 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10044 input_location = saved_input_location;
10045 }
10046 }
10047 else
10048 {
10049 /* The friend template has not already been declared. In this
10050 case, the instantiation of the template class will cause the
10051 injection of this template into the namespace scope. */
10052 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10053
10054 if (tmpl != error_mark_node)
10055 {
10056 /* The new TMPL is not an instantiation of anything, so we
10057 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10058 for the new type because that is supposed to be the
10059 corresponding template decl, i.e., TMPL. */
10060 DECL_USE_TEMPLATE (tmpl) = 0;
10061 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10062 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10063 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10064 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10065
10066 /* It is hidden. */
10067 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10068 DECL_ANTICIPATED (tmpl)
10069 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10070
10071 /* Inject this template into the enclosing namspace scope. */
10072 tmpl = pushdecl_namespace_level (tmpl, true);
10073 }
10074 }
10075
10076 if (TREE_CODE (context) == NAMESPACE_DECL)
10077 pop_nested_namespace (context);
10078 else
10079 pop_nested_class ();
10080
10081 return TREE_TYPE (tmpl);
10082 }
10083
10084 /* Returns zero if TYPE cannot be completed later due to circularity.
10085 Otherwise returns one. */
10086
10087 static int
10088 can_complete_type_without_circularity (tree type)
10089 {
10090 if (type == NULL_TREE || type == error_mark_node)
10091 return 0;
10092 else if (COMPLETE_TYPE_P (type))
10093 return 1;
10094 else if (TREE_CODE (type) == ARRAY_TYPE)
10095 return can_complete_type_without_circularity (TREE_TYPE (type));
10096 else if (CLASS_TYPE_P (type)
10097 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10098 return 0;
10099 else
10100 return 1;
10101 }
10102
10103 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10104 tsubst_flags_t, tree);
10105
10106 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10107 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10108
10109 static tree
10110 tsubst_attribute (tree t, tree *decl_p, tree args,
10111 tsubst_flags_t complain, tree in_decl)
10112 {
10113 gcc_assert (ATTR_IS_DEPENDENT (t));
10114
10115 tree val = TREE_VALUE (t);
10116 if (val == NULL_TREE)
10117 /* Nothing to do. */;
10118 else if ((flag_openmp || flag_openmp_simd)
10119 && is_attribute_p ("omp declare simd",
10120 get_attribute_name (t)))
10121 {
10122 tree clauses = TREE_VALUE (val);
10123 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10124 complain, in_decl);
10125 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10126 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10127 tree parms = DECL_ARGUMENTS (*decl_p);
10128 clauses
10129 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10130 if (clauses)
10131 val = build_tree_list (NULL_TREE, clauses);
10132 else
10133 val = NULL_TREE;
10134 }
10135 /* If the first attribute argument is an identifier, don't
10136 pass it through tsubst. Attributes like mode, format,
10137 cleanup and several target specific attributes expect it
10138 unmodified. */
10139 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10140 {
10141 tree chain
10142 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10143 /*integral_constant_expression_p=*/false);
10144 if (chain != TREE_CHAIN (val))
10145 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10146 }
10147 else if (PACK_EXPANSION_P (val))
10148 {
10149 /* An attribute pack expansion. */
10150 tree purp = TREE_PURPOSE (t);
10151 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10152 if (pack == error_mark_node)
10153 return error_mark_node;
10154 int len = TREE_VEC_LENGTH (pack);
10155 tree list = NULL_TREE;
10156 tree *q = &list;
10157 for (int i = 0; i < len; ++i)
10158 {
10159 tree elt = TREE_VEC_ELT (pack, i);
10160 *q = build_tree_list (purp, elt);
10161 q = &TREE_CHAIN (*q);
10162 }
10163 return list;
10164 }
10165 else
10166 val = tsubst_expr (val, args, complain, in_decl,
10167 /*integral_constant_expression_p=*/false);
10168
10169 if (val != TREE_VALUE (t))
10170 return build_tree_list (TREE_PURPOSE (t), val);
10171 return t;
10172 }
10173
10174 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10175 unchanged or a new TREE_LIST chain. */
10176
10177 static tree
10178 tsubst_attributes (tree attributes, tree args,
10179 tsubst_flags_t complain, tree in_decl)
10180 {
10181 tree last_dep = NULL_TREE;
10182
10183 for (tree t = attributes; t; t = TREE_CHAIN (t))
10184 if (ATTR_IS_DEPENDENT (t))
10185 {
10186 last_dep = t;
10187 attributes = copy_list (attributes);
10188 break;
10189 }
10190
10191 if (last_dep)
10192 for (tree *p = &attributes; *p; )
10193 {
10194 tree t = *p;
10195 if (ATTR_IS_DEPENDENT (t))
10196 {
10197 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10198 if (subst != t)
10199 {
10200 *p = subst;
10201 do
10202 p = &TREE_CHAIN (*p);
10203 while (*p);
10204 *p = TREE_CHAIN (t);
10205 continue;
10206 }
10207 }
10208 p = &TREE_CHAIN (*p);
10209 }
10210
10211 return attributes;
10212 }
10213
10214 /* Apply any attributes which had to be deferred until instantiation
10215 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10216 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10217
10218 static void
10219 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10220 tree args, tsubst_flags_t complain, tree in_decl)
10221 {
10222 tree last_dep = NULL_TREE;
10223 tree t;
10224 tree *p;
10225
10226 if (attributes == NULL_TREE)
10227 return;
10228
10229 if (DECL_P (*decl_p))
10230 {
10231 if (TREE_TYPE (*decl_p) == error_mark_node)
10232 return;
10233 p = &DECL_ATTRIBUTES (*decl_p);
10234 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10235 to our attributes parameter. */
10236 gcc_assert (*p == attributes);
10237 }
10238 else
10239 {
10240 p = &TYPE_ATTRIBUTES (*decl_p);
10241 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10242 lookup_template_class_1, and should be preserved. */
10243 gcc_assert (*p != attributes);
10244 while (*p)
10245 p = &TREE_CHAIN (*p);
10246 }
10247
10248 for (t = attributes; t; t = TREE_CHAIN (t))
10249 if (ATTR_IS_DEPENDENT (t))
10250 {
10251 last_dep = t;
10252 attributes = copy_list (attributes);
10253 break;
10254 }
10255
10256 *p = attributes;
10257 if (last_dep)
10258 {
10259 tree late_attrs = NULL_TREE;
10260 tree *q = &late_attrs;
10261
10262 for (; *p; )
10263 {
10264 t = *p;
10265 if (ATTR_IS_DEPENDENT (t))
10266 {
10267 *p = TREE_CHAIN (t);
10268 TREE_CHAIN (t) = NULL_TREE;
10269 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10270 do
10271 q = &TREE_CHAIN (*q);
10272 while (*q);
10273 }
10274 else
10275 p = &TREE_CHAIN (t);
10276 }
10277
10278 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10279 }
10280 }
10281
10282 /* Perform (or defer) access check for typedefs that were referenced
10283 from within the template TMPL code.
10284 This is a subroutine of instantiate_decl and instantiate_class_template.
10285 TMPL is the template to consider and TARGS is the list of arguments of
10286 that template. */
10287
10288 static void
10289 perform_typedefs_access_check (tree tmpl, tree targs)
10290 {
10291 location_t saved_location;
10292 unsigned i;
10293 qualified_typedef_usage_t *iter;
10294
10295 if (!tmpl
10296 || (!CLASS_TYPE_P (tmpl)
10297 && TREE_CODE (tmpl) != FUNCTION_DECL))
10298 return;
10299
10300 saved_location = input_location;
10301 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10302 {
10303 tree type_decl = iter->typedef_decl;
10304 tree type_scope = iter->context;
10305
10306 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10307 continue;
10308
10309 if (uses_template_parms (type_decl))
10310 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10311 if (uses_template_parms (type_scope))
10312 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10313
10314 /* Make access check error messages point to the location
10315 of the use of the typedef. */
10316 input_location = iter->locus;
10317 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10318 type_decl, type_decl,
10319 tf_warning_or_error);
10320 }
10321 input_location = saved_location;
10322 }
10323
10324 static tree
10325 instantiate_class_template_1 (tree type)
10326 {
10327 tree templ, args, pattern, t, member;
10328 tree typedecl;
10329 tree pbinfo;
10330 tree base_list;
10331 unsigned int saved_maximum_field_alignment;
10332 tree fn_context;
10333
10334 if (type == error_mark_node)
10335 return error_mark_node;
10336
10337 if (COMPLETE_OR_OPEN_TYPE_P (type)
10338 || uses_template_parms (type))
10339 return type;
10340
10341 /* Figure out which template is being instantiated. */
10342 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10343 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10344
10345 /* Determine what specialization of the original template to
10346 instantiate. */
10347 t = most_specialized_partial_spec (type, tf_warning_or_error);
10348 if (t == error_mark_node)
10349 {
10350 TYPE_BEING_DEFINED (type) = 1;
10351 return error_mark_node;
10352 }
10353 else if (t)
10354 {
10355 /* This TYPE is actually an instantiation of a partial
10356 specialization. We replace the innermost set of ARGS with
10357 the arguments appropriate for substitution. For example,
10358 given:
10359
10360 template <class T> struct S {};
10361 template <class T> struct S<T*> {};
10362
10363 and supposing that we are instantiating S<int*>, ARGS will
10364 presently be {int*} -- but we need {int}. */
10365 pattern = TREE_TYPE (t);
10366 args = TREE_PURPOSE (t);
10367 }
10368 else
10369 {
10370 pattern = TREE_TYPE (templ);
10371 args = CLASSTYPE_TI_ARGS (type);
10372 }
10373
10374 /* If the template we're instantiating is incomplete, then clearly
10375 there's nothing we can do. */
10376 if (!COMPLETE_TYPE_P (pattern))
10377 return type;
10378
10379 /* If we've recursively instantiated too many templates, stop. */
10380 if (! push_tinst_level (type))
10381 return type;
10382
10383 /* Now we're really doing the instantiation. Mark the type as in
10384 the process of being defined. */
10385 TYPE_BEING_DEFINED (type) = 1;
10386
10387 /* We may be in the middle of deferred access check. Disable
10388 it now. */
10389 push_deferring_access_checks (dk_no_deferred);
10390
10391 int saved_unevaluated_operand = cp_unevaluated_operand;
10392 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10393
10394 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10395 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10396 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10397 fn_context = error_mark_node;
10398 if (!fn_context)
10399 push_to_top_level ();
10400 else
10401 {
10402 cp_unevaluated_operand = 0;
10403 c_inhibit_evaluation_warnings = 0;
10404 }
10405 /* Use #pragma pack from the template context. */
10406 saved_maximum_field_alignment = maximum_field_alignment;
10407 maximum_field_alignment = TYPE_PRECISION (pattern);
10408
10409 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10410
10411 /* Set the input location to the most specialized template definition.
10412 This is needed if tsubsting causes an error. */
10413 typedecl = TYPE_MAIN_DECL (pattern);
10414 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10415 DECL_SOURCE_LOCATION (typedecl);
10416
10417 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10418 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10419 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10420 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10421 if (ANON_AGGR_TYPE_P (pattern))
10422 SET_ANON_AGGR_TYPE_P (type);
10423 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10424 {
10425 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10426 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10427 /* Adjust visibility for template arguments. */
10428 determine_visibility (TYPE_MAIN_DECL (type));
10429 }
10430 if (CLASS_TYPE_P (type))
10431 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10432
10433 pbinfo = TYPE_BINFO (pattern);
10434
10435 /* We should never instantiate a nested class before its enclosing
10436 class; we need to look up the nested class by name before we can
10437 instantiate it, and that lookup should instantiate the enclosing
10438 class. */
10439 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10440 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10441
10442 base_list = NULL_TREE;
10443 if (BINFO_N_BASE_BINFOS (pbinfo))
10444 {
10445 tree pbase_binfo;
10446 tree pushed_scope;
10447 int i;
10448
10449 /* We must enter the scope containing the type, as that is where
10450 the accessibility of types named in dependent bases are
10451 looked up from. */
10452 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10453
10454 /* Substitute into each of the bases to determine the actual
10455 basetypes. */
10456 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10457 {
10458 tree base;
10459 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10460 tree expanded_bases = NULL_TREE;
10461 int idx, len = 1;
10462
10463 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10464 {
10465 expanded_bases =
10466 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10467 args, tf_error, NULL_TREE);
10468 if (expanded_bases == error_mark_node)
10469 continue;
10470
10471 len = TREE_VEC_LENGTH (expanded_bases);
10472 }
10473
10474 for (idx = 0; idx < len; idx++)
10475 {
10476 if (expanded_bases)
10477 /* Extract the already-expanded base class. */
10478 base = TREE_VEC_ELT (expanded_bases, idx);
10479 else
10480 /* Substitute to figure out the base class. */
10481 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10482 NULL_TREE);
10483
10484 if (base == error_mark_node)
10485 continue;
10486
10487 base_list = tree_cons (access, base, base_list);
10488 if (BINFO_VIRTUAL_P (pbase_binfo))
10489 TREE_TYPE (base_list) = integer_type_node;
10490 }
10491 }
10492
10493 /* The list is now in reverse order; correct that. */
10494 base_list = nreverse (base_list);
10495
10496 if (pushed_scope)
10497 pop_scope (pushed_scope);
10498 }
10499 /* Now call xref_basetypes to set up all the base-class
10500 information. */
10501 xref_basetypes (type, base_list);
10502
10503 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10504 (int) ATTR_FLAG_TYPE_IN_PLACE,
10505 args, tf_error, NULL_TREE);
10506 fixup_attribute_variants (type);
10507
10508 /* Now that our base classes are set up, enter the scope of the
10509 class, so that name lookups into base classes, etc. will work
10510 correctly. This is precisely analogous to what we do in
10511 begin_class_definition when defining an ordinary non-template
10512 class, except we also need to push the enclosing classes. */
10513 push_nested_class (type);
10514
10515 /* Now members are processed in the order of declaration. */
10516 for (member = CLASSTYPE_DECL_LIST (pattern);
10517 member; member = TREE_CHAIN (member))
10518 {
10519 tree t = TREE_VALUE (member);
10520
10521 if (TREE_PURPOSE (member))
10522 {
10523 if (TYPE_P (t))
10524 {
10525 /* Build new CLASSTYPE_NESTED_UTDS. */
10526
10527 tree newtag;
10528 bool class_template_p;
10529
10530 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10531 && TYPE_LANG_SPECIFIC (t)
10532 && CLASSTYPE_IS_TEMPLATE (t));
10533 /* If the member is a class template, then -- even after
10534 substitution -- there may be dependent types in the
10535 template argument list for the class. We increment
10536 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10537 that function will assume that no types are dependent
10538 when outside of a template. */
10539 if (class_template_p)
10540 ++processing_template_decl;
10541 newtag = tsubst (t, args, tf_error, NULL_TREE);
10542 if (class_template_p)
10543 --processing_template_decl;
10544 if (newtag == error_mark_node)
10545 continue;
10546
10547 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10548 {
10549 tree name = TYPE_IDENTIFIER (t);
10550
10551 if (class_template_p)
10552 /* Unfortunately, lookup_template_class sets
10553 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10554 instantiation (i.e., for the type of a member
10555 template class nested within a template class.)
10556 This behavior is required for
10557 maybe_process_partial_specialization to work
10558 correctly, but is not accurate in this case;
10559 the TAG is not an instantiation of anything.
10560 (The corresponding TEMPLATE_DECL is an
10561 instantiation, but the TYPE is not.) */
10562 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10563
10564 /* Now, we call pushtag to put this NEWTAG into the scope of
10565 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10566 pushtag calling push_template_decl. We don't have to do
10567 this for enums because it will already have been done in
10568 tsubst_enum. */
10569 if (name)
10570 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10571 pushtag (name, newtag, /*tag_scope=*/ts_current);
10572 }
10573 }
10574 else if (DECL_DECLARES_FUNCTION_P (t))
10575 {
10576 tree r;
10577
10578 if (TREE_CODE (t) == TEMPLATE_DECL)
10579 ++processing_template_decl;
10580 r = tsubst (t, args, tf_error, NULL_TREE);
10581 if (TREE_CODE (t) == TEMPLATE_DECL)
10582 --processing_template_decl;
10583 set_current_access_from_decl (r);
10584 finish_member_declaration (r);
10585 /* Instantiate members marked with attribute used. */
10586 if (r != error_mark_node && DECL_PRESERVE_P (r))
10587 mark_used (r);
10588 if (TREE_CODE (r) == FUNCTION_DECL
10589 && DECL_OMP_DECLARE_REDUCTION_P (r))
10590 cp_check_omp_declare_reduction (r);
10591 }
10592 else if (DECL_CLASS_TEMPLATE_P (t)
10593 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10594 /* A closure type for a lambda in a default argument for a
10595 member template. Ignore it; it will be instantiated with
10596 the default argument. */;
10597 else
10598 {
10599 /* Build new TYPE_FIELDS. */
10600 if (TREE_CODE (t) == STATIC_ASSERT)
10601 {
10602 tree condition;
10603
10604 ++c_inhibit_evaluation_warnings;
10605 condition =
10606 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10607 tf_warning_or_error, NULL_TREE,
10608 /*integral_constant_expression_p=*/true);
10609 --c_inhibit_evaluation_warnings;
10610
10611 finish_static_assert (condition,
10612 STATIC_ASSERT_MESSAGE (t),
10613 STATIC_ASSERT_SOURCE_LOCATION (t),
10614 /*member_p=*/true);
10615 }
10616 else if (TREE_CODE (t) != CONST_DECL)
10617 {
10618 tree r;
10619 tree vec = NULL_TREE;
10620 int len = 1;
10621
10622 /* The file and line for this declaration, to
10623 assist in error message reporting. Since we
10624 called push_tinst_level above, we don't need to
10625 restore these. */
10626 input_location = DECL_SOURCE_LOCATION (t);
10627
10628 if (TREE_CODE (t) == TEMPLATE_DECL)
10629 ++processing_template_decl;
10630 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10631 if (TREE_CODE (t) == TEMPLATE_DECL)
10632 --processing_template_decl;
10633
10634 if (TREE_CODE (r) == TREE_VEC)
10635 {
10636 /* A capture pack became multiple fields. */
10637 vec = r;
10638 len = TREE_VEC_LENGTH (vec);
10639 }
10640
10641 for (int i = 0; i < len; ++i)
10642 {
10643 if (vec)
10644 r = TREE_VEC_ELT (vec, i);
10645 if (VAR_P (r))
10646 {
10647 /* In [temp.inst]:
10648
10649 [t]he initialization (and any associated
10650 side-effects) of a static data member does
10651 not occur unless the static data member is
10652 itself used in a way that requires the
10653 definition of the static data member to
10654 exist.
10655
10656 Therefore, we do not substitute into the
10657 initialized for the static data member here. */
10658 finish_static_data_member_decl
10659 (r,
10660 /*init=*/NULL_TREE,
10661 /*init_const_expr_p=*/false,
10662 /*asmspec_tree=*/NULL_TREE,
10663 /*flags=*/0);
10664 /* Instantiate members marked with attribute used. */
10665 if (r != error_mark_node && DECL_PRESERVE_P (r))
10666 mark_used (r);
10667 }
10668 else if (TREE_CODE (r) == FIELD_DECL)
10669 {
10670 /* Determine whether R has a valid type and can be
10671 completed later. If R is invalid, then its type
10672 is replaced by error_mark_node. */
10673 tree rtype = TREE_TYPE (r);
10674 if (can_complete_type_without_circularity (rtype))
10675 complete_type (rtype);
10676
10677 if (!complete_or_array_type_p (rtype))
10678 {
10679 /* If R's type couldn't be completed and
10680 it isn't a flexible array member (whose
10681 type is incomplete by definition) give
10682 an error. */
10683 cxx_incomplete_type_error (r, rtype);
10684 TREE_TYPE (r) = error_mark_node;
10685 }
10686 }
10687
10688 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10689 such a thing will already have been added to the field
10690 list by tsubst_enum in finish_member_declaration in the
10691 CLASSTYPE_NESTED_UTDS case above. */
10692 if (!(TREE_CODE (r) == TYPE_DECL
10693 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10694 && DECL_ARTIFICIAL (r)))
10695 {
10696 set_current_access_from_decl (r);
10697 finish_member_declaration (r);
10698 }
10699 }
10700 }
10701 }
10702 }
10703 else
10704 {
10705 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10706 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10707 {
10708 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10709
10710 tree friend_type = t;
10711 bool adjust_processing_template_decl = false;
10712
10713 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10714 {
10715 /* template <class T> friend class C; */
10716 friend_type = tsubst_friend_class (friend_type, args);
10717 adjust_processing_template_decl = true;
10718 }
10719 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10720 {
10721 /* template <class T> friend class C::D; */
10722 friend_type = tsubst (friend_type, args,
10723 tf_warning_or_error, NULL_TREE);
10724 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10725 friend_type = TREE_TYPE (friend_type);
10726 adjust_processing_template_decl = true;
10727 }
10728 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10729 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10730 {
10731 /* This could be either
10732
10733 friend class T::C;
10734
10735 when dependent_type_p is false or
10736
10737 template <class U> friend class T::C;
10738
10739 otherwise. */
10740 /* Bump processing_template_decl in case this is something like
10741 template <class T> friend struct A<T>::B. */
10742 ++processing_template_decl;
10743 friend_type = tsubst (friend_type, args,
10744 tf_warning_or_error, NULL_TREE);
10745 if (dependent_type_p (friend_type))
10746 adjust_processing_template_decl = true;
10747 --processing_template_decl;
10748 }
10749 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10750 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10751 && TYPE_HIDDEN_P (friend_type))
10752 {
10753 /* friend class C;
10754
10755 where C hasn't been declared yet. Let's lookup name
10756 from namespace scope directly, bypassing any name that
10757 come from dependent base class. */
10758 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10759
10760 /* The call to xref_tag_from_type does injection for friend
10761 classes. */
10762 push_nested_namespace (ns);
10763 friend_type =
10764 xref_tag_from_type (friend_type, NULL_TREE,
10765 /*tag_scope=*/ts_current);
10766 pop_nested_namespace (ns);
10767 }
10768 else if (uses_template_parms (friend_type))
10769 /* friend class C<T>; */
10770 friend_type = tsubst (friend_type, args,
10771 tf_warning_or_error, NULL_TREE);
10772 /* Otherwise it's
10773
10774 friend class C;
10775
10776 where C is already declared or
10777
10778 friend class C<int>;
10779
10780 We don't have to do anything in these cases. */
10781
10782 if (adjust_processing_template_decl)
10783 /* Trick make_friend_class into realizing that the friend
10784 we're adding is a template, not an ordinary class. It's
10785 important that we use make_friend_class since it will
10786 perform some error-checking and output cross-reference
10787 information. */
10788 ++processing_template_decl;
10789
10790 if (friend_type != error_mark_node)
10791 make_friend_class (type, friend_type, /*complain=*/false);
10792
10793 if (adjust_processing_template_decl)
10794 --processing_template_decl;
10795 }
10796 else
10797 {
10798 /* Build new DECL_FRIENDLIST. */
10799 tree r;
10800
10801 /* The file and line for this declaration, to
10802 assist in error message reporting. Since we
10803 called push_tinst_level above, we don't need to
10804 restore these. */
10805 input_location = DECL_SOURCE_LOCATION (t);
10806
10807 if (TREE_CODE (t) == TEMPLATE_DECL)
10808 {
10809 ++processing_template_decl;
10810 push_deferring_access_checks (dk_no_check);
10811 }
10812
10813 r = tsubst_friend_function (t, args);
10814 add_friend (type, r, /*complain=*/false);
10815 if (TREE_CODE (t) == TEMPLATE_DECL)
10816 {
10817 pop_deferring_access_checks ();
10818 --processing_template_decl;
10819 }
10820 }
10821 }
10822 }
10823
10824 if (fn_context)
10825 {
10826 /* Restore these before substituting into the lambda capture
10827 initializers. */
10828 cp_unevaluated_operand = saved_unevaluated_operand;
10829 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10830 }
10831
10832 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10833 {
10834 tree decl = lambda_function (type);
10835 if (decl)
10836 {
10837 if (cxx_dialect >= cxx17)
10838 CLASSTYPE_LITERAL_P (type) = true;
10839
10840 if (!DECL_TEMPLATE_INFO (decl)
10841 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10842 {
10843 /* Set function_depth to avoid garbage collection. */
10844 ++function_depth;
10845 instantiate_decl (decl, /*defer_ok=*/false, false);
10846 --function_depth;
10847 }
10848
10849 /* We need to instantiate the capture list from the template
10850 after we've instantiated the closure members, but before we
10851 consider adding the conversion op. Also keep any captures
10852 that may have been added during instantiation of the op(). */
10853 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10854 tree tmpl_cap
10855 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10856 args, tf_warning_or_error, NULL_TREE,
10857 false, false);
10858
10859 LAMBDA_EXPR_CAPTURE_LIST (expr)
10860 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10861
10862 maybe_add_lambda_conv_op (type);
10863 }
10864 else
10865 gcc_assert (errorcount);
10866 }
10867
10868 /* Set the file and line number information to whatever is given for
10869 the class itself. This puts error messages involving generated
10870 implicit functions at a predictable point, and the same point
10871 that would be used for non-template classes. */
10872 input_location = DECL_SOURCE_LOCATION (typedecl);
10873
10874 unreverse_member_declarations (type);
10875 finish_struct_1 (type);
10876 TYPE_BEING_DEFINED (type) = 0;
10877
10878 /* We don't instantiate default arguments for member functions. 14.7.1:
10879
10880 The implicit instantiation of a class template specialization causes
10881 the implicit instantiation of the declarations, but not of the
10882 definitions or default arguments, of the class member functions,
10883 member classes, static data members and member templates.... */
10884
10885 /* Some typedefs referenced from within the template code need to be access
10886 checked at template instantiation time, i.e now. These types were
10887 added to the template at parsing time. Let's get those and perform
10888 the access checks then. */
10889 perform_typedefs_access_check (pattern, args);
10890 perform_deferred_access_checks (tf_warning_or_error);
10891 pop_nested_class ();
10892 maximum_field_alignment = saved_maximum_field_alignment;
10893 if (!fn_context)
10894 pop_from_top_level ();
10895 pop_deferring_access_checks ();
10896 pop_tinst_level ();
10897
10898 /* The vtable for a template class can be emitted in any translation
10899 unit in which the class is instantiated. When there is no key
10900 method, however, finish_struct_1 will already have added TYPE to
10901 the keyed_classes. */
10902 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10903 vec_safe_push (keyed_classes, type);
10904
10905 return type;
10906 }
10907
10908 /* Wrapper for instantiate_class_template_1. */
10909
10910 tree
10911 instantiate_class_template (tree type)
10912 {
10913 tree ret;
10914 timevar_push (TV_TEMPLATE_INST);
10915 ret = instantiate_class_template_1 (type);
10916 timevar_pop (TV_TEMPLATE_INST);
10917 return ret;
10918 }
10919
10920 static tree
10921 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10922 {
10923 tree r;
10924
10925 if (!t)
10926 r = t;
10927 else if (TYPE_P (t))
10928 r = tsubst (t, args, complain, in_decl);
10929 else
10930 {
10931 if (!(complain & tf_warning))
10932 ++c_inhibit_evaluation_warnings;
10933 r = tsubst_expr (t, args, complain, in_decl,
10934 /*integral_constant_expression_p=*/true);
10935 if (!(complain & tf_warning))
10936 --c_inhibit_evaluation_warnings;
10937 }
10938 return r;
10939 }
10940
10941 /* Given a function parameter pack TMPL_PARM and some function parameters
10942 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10943 and set *SPEC_P to point at the next point in the list. */
10944
10945 tree
10946 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10947 {
10948 /* Collect all of the extra "packed" parameters into an
10949 argument pack. */
10950 tree parmvec;
10951 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10952 tree spec_parm = *spec_p;
10953 int i, len;
10954
10955 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10956 if (tmpl_parm
10957 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10958 break;
10959
10960 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10961 parmvec = make_tree_vec (len);
10962 spec_parm = *spec_p;
10963 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10964 TREE_VEC_ELT (parmvec, i) = spec_parm;
10965
10966 /* Build the argument packs. */
10967 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10968 *spec_p = spec_parm;
10969
10970 return argpack;
10971 }
10972
10973 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10974 NONTYPE_ARGUMENT_PACK. */
10975
10976 static tree
10977 make_fnparm_pack (tree spec_parm)
10978 {
10979 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10980 }
10981
10982 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10983 pack expansion with no extra args, 2 if it has extra args, or 0
10984 if it is not a pack expansion. */
10985
10986 static int
10987 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10988 {
10989 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10990 if (i >= TREE_VEC_LENGTH (vec))
10991 return 0;
10992 tree elt = TREE_VEC_ELT (vec, i);
10993 if (DECL_P (elt))
10994 /* A decl pack is itself an expansion. */
10995 elt = TREE_TYPE (elt);
10996 if (!PACK_EXPANSION_P (elt))
10997 return 0;
10998 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10999 return 2;
11000 return 1;
11001 }
11002
11003
11004 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11005
11006 static tree
11007 make_argument_pack_select (tree arg_pack, unsigned index)
11008 {
11009 tree aps = make_node (ARGUMENT_PACK_SELECT);
11010
11011 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11012 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11013
11014 return aps;
11015 }
11016
11017 /* This is a subroutine of tsubst_pack_expansion.
11018
11019 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11020 mechanism to store the (non complete list of) arguments of the
11021 substitution and return a non substituted pack expansion, in order
11022 to wait for when we have enough arguments to really perform the
11023 substitution. */
11024
11025 static bool
11026 use_pack_expansion_extra_args_p (tree parm_packs,
11027 int arg_pack_len,
11028 bool has_empty_arg)
11029 {
11030 /* If one pack has an expansion and another pack has a normal
11031 argument or if one pack has an empty argument and an another
11032 one hasn't then tsubst_pack_expansion cannot perform the
11033 substitution and need to fall back on the
11034 PACK_EXPANSION_EXTRA mechanism. */
11035 if (parm_packs == NULL_TREE)
11036 return false;
11037 else if (has_empty_arg)
11038 return true;
11039
11040 bool has_expansion_arg = false;
11041 for (int i = 0 ; i < arg_pack_len; ++i)
11042 {
11043 bool has_non_expansion_arg = false;
11044 for (tree parm_pack = parm_packs;
11045 parm_pack;
11046 parm_pack = TREE_CHAIN (parm_pack))
11047 {
11048 tree arg = TREE_VALUE (parm_pack);
11049
11050 int exp = argument_pack_element_is_expansion_p (arg, i);
11051 if (exp == 2)
11052 /* We can't substitute a pack expansion with extra args into
11053 our pattern. */
11054 return true;
11055 else if (exp)
11056 has_expansion_arg = true;
11057 else
11058 has_non_expansion_arg = true;
11059 }
11060
11061 if (has_expansion_arg && has_non_expansion_arg)
11062 return true;
11063 }
11064 return false;
11065 }
11066
11067 /* [temp.variadic]/6 says that:
11068
11069 The instantiation of a pack expansion [...]
11070 produces a list E1,E2, ..., En, where N is the number of elements
11071 in the pack expansion parameters.
11072
11073 This subroutine of tsubst_pack_expansion produces one of these Ei.
11074
11075 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11076 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11077 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11078 INDEX is the index 'i' of the element Ei to produce. ARGS,
11079 COMPLAIN, and IN_DECL are the same parameters as for the
11080 tsubst_pack_expansion function.
11081
11082 The function returns the resulting Ei upon successful completion,
11083 or error_mark_node.
11084
11085 Note that this function possibly modifies the ARGS parameter, so
11086 it's the responsibility of the caller to restore it. */
11087
11088 static tree
11089 gen_elem_of_pack_expansion_instantiation (tree pattern,
11090 tree parm_packs,
11091 unsigned index,
11092 tree args /* This parm gets
11093 modified. */,
11094 tsubst_flags_t complain,
11095 tree in_decl)
11096 {
11097 tree t;
11098 bool ith_elem_is_expansion = false;
11099
11100 /* For each parameter pack, change the substitution of the parameter
11101 pack to the ith argument in its argument pack, then expand the
11102 pattern. */
11103 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11104 {
11105 tree parm = TREE_PURPOSE (pack);
11106 tree arg_pack = TREE_VALUE (pack);
11107 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11108
11109 ith_elem_is_expansion |=
11110 argument_pack_element_is_expansion_p (arg_pack, index);
11111
11112 /* Select the Ith argument from the pack. */
11113 if (TREE_CODE (parm) == PARM_DECL
11114 || TREE_CODE (parm) == FIELD_DECL)
11115 {
11116 if (index == 0)
11117 {
11118 aps = make_argument_pack_select (arg_pack, index);
11119 if (!mark_used (parm, complain) && !(complain & tf_error))
11120 return error_mark_node;
11121 register_local_specialization (aps, parm);
11122 }
11123 else
11124 aps = retrieve_local_specialization (parm);
11125 }
11126 else
11127 {
11128 int idx, level;
11129 template_parm_level_and_index (parm, &level, &idx);
11130
11131 if (index == 0)
11132 {
11133 aps = make_argument_pack_select (arg_pack, index);
11134 /* Update the corresponding argument. */
11135 TMPL_ARG (args, level, idx) = aps;
11136 }
11137 else
11138 /* Re-use the ARGUMENT_PACK_SELECT. */
11139 aps = TMPL_ARG (args, level, idx);
11140 }
11141 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11142 }
11143
11144 /* Substitute into the PATTERN with the (possibly altered)
11145 arguments. */
11146 if (pattern == in_decl)
11147 /* Expanding a fixed parameter pack from
11148 coerce_template_parameter_pack. */
11149 t = tsubst_decl (pattern, args, complain);
11150 else if (pattern == error_mark_node)
11151 t = error_mark_node;
11152 else if (constraint_p (pattern))
11153 {
11154 if (processing_template_decl)
11155 t = tsubst_constraint (pattern, args, complain, in_decl);
11156 else
11157 t = (constraints_satisfied_p (pattern, args)
11158 ? boolean_true_node : boolean_false_node);
11159 }
11160 else if (!TYPE_P (pattern))
11161 t = tsubst_expr (pattern, args, complain, in_decl,
11162 /*integral_constant_expression_p=*/false);
11163 else
11164 t = tsubst (pattern, args, complain, in_decl);
11165
11166 /* If the Ith argument pack element is a pack expansion, then
11167 the Ith element resulting from the substituting is going to
11168 be a pack expansion as well. */
11169 if (ith_elem_is_expansion)
11170 t = make_pack_expansion (t, complain);
11171
11172 return t;
11173 }
11174
11175 /* When the unexpanded parameter pack in a fold expression expands to an empty
11176 sequence, the value of the expression is as follows; the program is
11177 ill-formed if the operator is not listed in this table.
11178
11179 && true
11180 || false
11181 , void() */
11182
11183 tree
11184 expand_empty_fold (tree t, tsubst_flags_t complain)
11185 {
11186 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11187 if (!FOLD_EXPR_MODIFY_P (t))
11188 switch (code)
11189 {
11190 case TRUTH_ANDIF_EXPR:
11191 return boolean_true_node;
11192 case TRUTH_ORIF_EXPR:
11193 return boolean_false_node;
11194 case COMPOUND_EXPR:
11195 return void_node;
11196 default:
11197 break;
11198 }
11199
11200 if (complain & tf_error)
11201 error_at (location_of (t),
11202 "fold of empty expansion over %O", code);
11203 return error_mark_node;
11204 }
11205
11206 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11207 form an expression that combines the two terms using the
11208 operator of T. */
11209
11210 static tree
11211 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11212 {
11213 tree op = FOLD_EXPR_OP (t);
11214 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11215
11216 // Handle compound assignment operators.
11217 if (FOLD_EXPR_MODIFY_P (t))
11218 return build_x_modify_expr (input_location, left, code, right, complain);
11219
11220 switch (code)
11221 {
11222 case COMPOUND_EXPR:
11223 return build_x_compound_expr (input_location, left, right, complain);
11224 case DOTSTAR_EXPR:
11225 return build_m_component_ref (left, right, complain);
11226 default:
11227 return build_x_binary_op (input_location, code,
11228 left, TREE_CODE (left),
11229 right, TREE_CODE (right),
11230 /*overload=*/NULL,
11231 complain);
11232 }
11233 }
11234
11235 /* Substitute ARGS into the pack of a fold expression T. */
11236
11237 static inline tree
11238 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11239 {
11240 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11241 }
11242
11243 /* Substitute ARGS into the pack of a fold expression T. */
11244
11245 static inline tree
11246 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11247 {
11248 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11249 }
11250
11251 /* Expand a PACK of arguments into a grouped as left fold.
11252 Given a pack containing elements A0, A1, ..., An and an
11253 operator @, this builds the expression:
11254
11255 ((A0 @ A1) @ A2) ... @ An
11256
11257 Note that PACK must not be empty.
11258
11259 The operator is defined by the original fold expression T. */
11260
11261 static tree
11262 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11263 {
11264 tree left = TREE_VEC_ELT (pack, 0);
11265 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11266 {
11267 tree right = TREE_VEC_ELT (pack, i);
11268 left = fold_expression (t, left, right, complain);
11269 }
11270 return left;
11271 }
11272
11273 /* Substitute into a unary left fold expression. */
11274
11275 static tree
11276 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11277 tree in_decl)
11278 {
11279 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11280 if (pack == error_mark_node)
11281 return error_mark_node;
11282 if (PACK_EXPANSION_P (pack))
11283 {
11284 tree r = copy_node (t);
11285 FOLD_EXPR_PACK (r) = pack;
11286 return r;
11287 }
11288 if (TREE_VEC_LENGTH (pack) == 0)
11289 return expand_empty_fold (t, complain);
11290 else
11291 return expand_left_fold (t, pack, complain);
11292 }
11293
11294 /* Substitute into a binary left fold expression.
11295
11296 Do ths by building a single (non-empty) vector of argumnts and
11297 building the expression from those elements. */
11298
11299 static tree
11300 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11301 tree in_decl)
11302 {
11303 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11304 if (pack == error_mark_node)
11305 return error_mark_node;
11306 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11307 if (init == error_mark_node)
11308 return error_mark_node;
11309
11310 if (PACK_EXPANSION_P (pack))
11311 {
11312 tree r = copy_node (t);
11313 FOLD_EXPR_PACK (r) = pack;
11314 FOLD_EXPR_INIT (r) = init;
11315 return r;
11316 }
11317
11318 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11319 TREE_VEC_ELT (vec, 0) = init;
11320 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11321 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11322
11323 return expand_left_fold (t, vec, complain);
11324 }
11325
11326 /* Expand a PACK of arguments into a grouped as right fold.
11327 Given a pack containing elementns A0, A1, ..., and an
11328 operator @, this builds the expression:
11329
11330 A0@ ... (An-2 @ (An-1 @ An))
11331
11332 Note that PACK must not be empty.
11333
11334 The operator is defined by the original fold expression T. */
11335
11336 tree
11337 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11338 {
11339 // Build the expression.
11340 int n = TREE_VEC_LENGTH (pack);
11341 tree right = TREE_VEC_ELT (pack, n - 1);
11342 for (--n; n != 0; --n)
11343 {
11344 tree left = TREE_VEC_ELT (pack, n - 1);
11345 right = fold_expression (t, left, right, complain);
11346 }
11347 return right;
11348 }
11349
11350 /* Substitute into a unary right fold expression. */
11351
11352 static tree
11353 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11354 tree in_decl)
11355 {
11356 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11357 if (pack == error_mark_node)
11358 return error_mark_node;
11359 if (PACK_EXPANSION_P (pack))
11360 {
11361 tree r = copy_node (t);
11362 FOLD_EXPR_PACK (r) = pack;
11363 return r;
11364 }
11365 if (TREE_VEC_LENGTH (pack) == 0)
11366 return expand_empty_fold (t, complain);
11367 else
11368 return expand_right_fold (t, pack, complain);
11369 }
11370
11371 /* Substitute into a binary right fold expression.
11372
11373 Do ths by building a single (non-empty) vector of arguments and
11374 building the expression from those elements. */
11375
11376 static tree
11377 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11378 tree in_decl)
11379 {
11380 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11381 if (pack == error_mark_node)
11382 return error_mark_node;
11383 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11384 if (init == error_mark_node)
11385 return error_mark_node;
11386
11387 if (PACK_EXPANSION_P (pack))
11388 {
11389 tree r = copy_node (t);
11390 FOLD_EXPR_PACK (r) = pack;
11391 FOLD_EXPR_INIT (r) = init;
11392 return r;
11393 }
11394
11395 int n = TREE_VEC_LENGTH (pack);
11396 tree vec = make_tree_vec (n + 1);
11397 for (int i = 0; i < n; ++i)
11398 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11399 TREE_VEC_ELT (vec, n) = init;
11400
11401 return expand_right_fold (t, vec, complain);
11402 }
11403
11404
11405 /* Substitute ARGS into T, which is an pack expansion
11406 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11407 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11408 (if only a partial substitution could be performed) or
11409 ERROR_MARK_NODE if there was an error. */
11410 tree
11411 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11412 tree in_decl)
11413 {
11414 tree pattern;
11415 tree pack, packs = NULL_TREE;
11416 bool unsubstituted_packs = false;
11417 int i, len = -1;
11418 tree result;
11419 hash_map<tree, tree> *saved_local_specializations = NULL;
11420 bool need_local_specializations = false;
11421 int levels;
11422
11423 gcc_assert (PACK_EXPANSION_P (t));
11424 pattern = PACK_EXPANSION_PATTERN (t);
11425
11426 /* Add in any args remembered from an earlier partial instantiation. */
11427 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11428
11429 levels = TMPL_ARGS_DEPTH (args);
11430
11431 /* Determine the argument packs that will instantiate the parameter
11432 packs used in the expansion expression. While we're at it,
11433 compute the number of arguments to be expanded and make sure it
11434 is consistent. */
11435 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11436 pack = TREE_CHAIN (pack))
11437 {
11438 tree parm_pack = TREE_VALUE (pack);
11439 tree arg_pack = NULL_TREE;
11440 tree orig_arg = NULL_TREE;
11441 int level = 0;
11442
11443 if (TREE_CODE (parm_pack) == BASES)
11444 {
11445 gcc_assert (parm_pack == pattern);
11446 if (BASES_DIRECT (parm_pack))
11447 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11448 args, complain, in_decl, false));
11449 else
11450 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11451 args, complain, in_decl, false));
11452 }
11453 else if (builtin_pack_call_p (parm_pack))
11454 {
11455 /* ??? Support use in other patterns. */
11456 gcc_assert (parm_pack == pattern);
11457 return expand_builtin_pack_call (parm_pack, args,
11458 complain, in_decl);
11459 }
11460 else if (TREE_CODE (parm_pack) == PARM_DECL)
11461 {
11462 /* We know we have correct local_specializations if this
11463 expansion is at function scope, or if we're dealing with a
11464 local parameter in a requires expression; for the latter,
11465 tsubst_requires_expr set it up appropriately. */
11466 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11467 arg_pack = retrieve_local_specialization (parm_pack);
11468 else
11469 /* We can't rely on local_specializations for a parameter
11470 name used later in a function declaration (such as in a
11471 late-specified return type). Even if it exists, it might
11472 have the wrong value for a recursive call. */
11473 need_local_specializations = true;
11474
11475 if (!arg_pack)
11476 {
11477 /* This parameter pack was used in an unevaluated context. Just
11478 make a dummy decl, since it's only used for its type. */
11479 arg_pack = tsubst_decl (parm_pack, args, complain);
11480 if (arg_pack && DECL_PACK_P (arg_pack))
11481 /* Partial instantiation of the parm_pack, we can't build
11482 up an argument pack yet. */
11483 arg_pack = NULL_TREE;
11484 else
11485 arg_pack = make_fnparm_pack (arg_pack);
11486 }
11487 }
11488 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11489 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11490 else
11491 {
11492 int idx;
11493 template_parm_level_and_index (parm_pack, &level, &idx);
11494
11495 if (level <= levels)
11496 arg_pack = TMPL_ARG (args, level, idx);
11497 }
11498
11499 orig_arg = arg_pack;
11500 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11501 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11502
11503 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11504 /* This can only happen if we forget to expand an argument
11505 pack somewhere else. Just return an error, silently. */
11506 {
11507 result = make_tree_vec (1);
11508 TREE_VEC_ELT (result, 0) = error_mark_node;
11509 return result;
11510 }
11511
11512 if (arg_pack)
11513 {
11514 int my_len =
11515 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11516
11517 /* Don't bother trying to do a partial substitution with
11518 incomplete packs; we'll try again after deduction. */
11519 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11520 return t;
11521
11522 if (len < 0)
11523 len = my_len;
11524 else if (len != my_len)
11525 {
11526 if (!(complain & tf_error))
11527 /* Fail quietly. */;
11528 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11529 error ("mismatched argument pack lengths while expanding %qT",
11530 pattern);
11531 else
11532 error ("mismatched argument pack lengths while expanding %qE",
11533 pattern);
11534 return error_mark_node;
11535 }
11536
11537 /* Keep track of the parameter packs and their corresponding
11538 argument packs. */
11539 packs = tree_cons (parm_pack, arg_pack, packs);
11540 TREE_TYPE (packs) = orig_arg;
11541 }
11542 else
11543 {
11544 /* We can't substitute for this parameter pack. We use a flag as
11545 well as the missing_level counter because function parameter
11546 packs don't have a level. */
11547 gcc_assert (processing_template_decl);
11548 unsubstituted_packs = true;
11549 }
11550 }
11551
11552 /* If the expansion is just T..., return the matching argument pack, unless
11553 we need to call convert_from_reference on all the elements. This is an
11554 important optimization; see c++/68422. */
11555 if (!unsubstituted_packs
11556 && TREE_PURPOSE (packs) == pattern)
11557 {
11558 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11559 /* Types need no adjustment, nor does sizeof..., and if we still have
11560 some pack expansion args we won't do anything yet. */
11561 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11562 || PACK_EXPANSION_SIZEOF_P (t)
11563 || pack_expansion_args_count (args))
11564 return args;
11565 /* Also optimize expression pack expansions if we can tell that the
11566 elements won't have reference type. */
11567 tree type = TREE_TYPE (pattern);
11568 if (type && TREE_CODE (type) != REFERENCE_TYPE
11569 && !PACK_EXPANSION_P (type)
11570 && !WILDCARD_TYPE_P (type))
11571 return args;
11572 /* Otherwise use the normal path so we get convert_from_reference. */
11573 }
11574
11575 /* We cannot expand this expansion expression, because we don't have
11576 all of the argument packs we need. */
11577 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11578 {
11579 /* We got some full packs, but we can't substitute them in until we
11580 have values for all the packs. So remember these until then. */
11581
11582 t = make_pack_expansion (pattern, complain);
11583 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11584 return t;
11585 }
11586 else if (unsubstituted_packs)
11587 {
11588 /* There were no real arguments, we're just replacing a parameter
11589 pack with another version of itself. Substitute into the
11590 pattern and return a PACK_EXPANSION_*. The caller will need to
11591 deal with that. */
11592 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11593 t = tsubst_expr (pattern, args, complain, in_decl,
11594 /*integral_constant_expression_p=*/false);
11595 else
11596 t = tsubst (pattern, args, complain, in_decl);
11597 t = make_pack_expansion (t, complain);
11598 return t;
11599 }
11600
11601 gcc_assert (len >= 0);
11602
11603 if (need_local_specializations)
11604 {
11605 /* We're in a late-specified return type, so create our own local
11606 specializations map; the current map is either NULL or (in the
11607 case of recursive unification) might have bindings that we don't
11608 want to use or alter. */
11609 saved_local_specializations = local_specializations;
11610 local_specializations = new hash_map<tree, tree>;
11611 }
11612
11613 /* For each argument in each argument pack, substitute into the
11614 pattern. */
11615 result = make_tree_vec (len);
11616 tree elem_args = copy_template_args (args);
11617 for (i = 0; i < len; ++i)
11618 {
11619 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11620 i,
11621 elem_args, complain,
11622 in_decl);
11623 TREE_VEC_ELT (result, i) = t;
11624 if (t == error_mark_node)
11625 {
11626 result = error_mark_node;
11627 break;
11628 }
11629 }
11630
11631 /* Update ARGS to restore the substitution from parameter packs to
11632 their argument packs. */
11633 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11634 {
11635 tree parm = TREE_PURPOSE (pack);
11636
11637 if (TREE_CODE (parm) == PARM_DECL
11638 || TREE_CODE (parm) == FIELD_DECL)
11639 register_local_specialization (TREE_TYPE (pack), parm);
11640 else
11641 {
11642 int idx, level;
11643
11644 if (TREE_VALUE (pack) == NULL_TREE)
11645 continue;
11646
11647 template_parm_level_and_index (parm, &level, &idx);
11648
11649 /* Update the corresponding argument. */
11650 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11651 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11652 TREE_TYPE (pack);
11653 else
11654 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11655 }
11656 }
11657
11658 if (need_local_specializations)
11659 {
11660 delete local_specializations;
11661 local_specializations = saved_local_specializations;
11662 }
11663
11664 /* If the dependent pack arguments were such that we end up with only a
11665 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11666 if (len == 1 && TREE_CODE (result) == TREE_VEC
11667 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11668 return TREE_VEC_ELT (result, 0);
11669
11670 return result;
11671 }
11672
11673 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11674 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11675 parameter packs; all parms generated from a function parameter pack will
11676 have the same DECL_PARM_INDEX. */
11677
11678 tree
11679 get_pattern_parm (tree parm, tree tmpl)
11680 {
11681 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11682 tree patparm;
11683
11684 if (DECL_ARTIFICIAL (parm))
11685 {
11686 for (patparm = DECL_ARGUMENTS (pattern);
11687 patparm; patparm = DECL_CHAIN (patparm))
11688 if (DECL_ARTIFICIAL (patparm)
11689 && DECL_NAME (parm) == DECL_NAME (patparm))
11690 break;
11691 }
11692 else
11693 {
11694 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11695 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11696 gcc_assert (DECL_PARM_INDEX (patparm)
11697 == DECL_PARM_INDEX (parm));
11698 }
11699
11700 return patparm;
11701 }
11702
11703 /* Make an argument pack out of the TREE_VEC VEC. */
11704
11705 static tree
11706 make_argument_pack (tree vec)
11707 {
11708 tree pack;
11709 tree elt = TREE_VEC_ELT (vec, 0);
11710 if (TYPE_P (elt))
11711 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11712 else
11713 {
11714 pack = make_node (NONTYPE_ARGUMENT_PACK);
11715 TREE_CONSTANT (pack) = 1;
11716 }
11717 SET_ARGUMENT_PACK_ARGS (pack, vec);
11718 return pack;
11719 }
11720
11721 /* Return an exact copy of template args T that can be modified
11722 independently. */
11723
11724 static tree
11725 copy_template_args (tree t)
11726 {
11727 if (t == error_mark_node)
11728 return t;
11729
11730 int len = TREE_VEC_LENGTH (t);
11731 tree new_vec = make_tree_vec (len);
11732
11733 for (int i = 0; i < len; ++i)
11734 {
11735 tree elt = TREE_VEC_ELT (t, i);
11736 if (elt && TREE_CODE (elt) == TREE_VEC)
11737 elt = copy_template_args (elt);
11738 TREE_VEC_ELT (new_vec, i) = elt;
11739 }
11740
11741 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11742 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11743
11744 return new_vec;
11745 }
11746
11747 /* Substitute ARGS into the vector or list of template arguments T. */
11748
11749 static tree
11750 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11751 {
11752 tree orig_t = t;
11753 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11754 tree *elts;
11755
11756 if (t == error_mark_node)
11757 return error_mark_node;
11758
11759 len = TREE_VEC_LENGTH (t);
11760 elts = XALLOCAVEC (tree, len);
11761
11762 for (i = 0; i < len; i++)
11763 {
11764 tree orig_arg = TREE_VEC_ELT (t, i);
11765 tree new_arg;
11766
11767 if (TREE_CODE (orig_arg) == TREE_VEC)
11768 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11769 else if (PACK_EXPANSION_P (orig_arg))
11770 {
11771 /* Substitute into an expansion expression. */
11772 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11773
11774 if (TREE_CODE (new_arg) == TREE_VEC)
11775 /* Add to the expanded length adjustment the number of
11776 expanded arguments. We subtract one from this
11777 measurement, because the argument pack expression
11778 itself is already counted as 1 in
11779 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11780 the argument pack is empty. */
11781 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11782 }
11783 else if (ARGUMENT_PACK_P (orig_arg))
11784 {
11785 /* Substitute into each of the arguments. */
11786 new_arg = TYPE_P (orig_arg)
11787 ? cxx_make_type (TREE_CODE (orig_arg))
11788 : make_node (TREE_CODE (orig_arg));
11789
11790 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11791 args, complain, in_decl);
11792 if (pack_args == error_mark_node)
11793 new_arg = error_mark_node;
11794 else
11795 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11796
11797 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11798 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11799 }
11800 else
11801 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11802
11803 if (new_arg == error_mark_node)
11804 return error_mark_node;
11805
11806 elts[i] = new_arg;
11807 if (new_arg != orig_arg)
11808 need_new = 1;
11809 }
11810
11811 if (!need_new)
11812 return t;
11813
11814 /* Make space for the expanded arguments coming from template
11815 argument packs. */
11816 t = make_tree_vec (len + expanded_len_adjust);
11817 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11818 arguments for a member template.
11819 In that case each TREE_VEC in ORIG_T represents a level of template
11820 arguments, and ORIG_T won't carry any non defaulted argument count.
11821 It will rather be the nested TREE_VECs that will carry one.
11822 In other words, ORIG_T carries a non defaulted argument count only
11823 if it doesn't contain any nested TREE_VEC. */
11824 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11825 {
11826 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11827 count += expanded_len_adjust;
11828 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11829 }
11830 for (i = 0, out = 0; i < len; i++)
11831 {
11832 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11833 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11834 && TREE_CODE (elts[i]) == TREE_VEC)
11835 {
11836 int idx;
11837
11838 /* Now expand the template argument pack "in place". */
11839 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11840 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11841 }
11842 else
11843 {
11844 TREE_VEC_ELT (t, out) = elts[i];
11845 out++;
11846 }
11847 }
11848
11849 return t;
11850 }
11851
11852 /* Substitute ARGS into one level PARMS of template parameters. */
11853
11854 static tree
11855 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11856 {
11857 if (parms == error_mark_node)
11858 return error_mark_node;
11859
11860 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11861
11862 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11863 {
11864 tree tuple = TREE_VEC_ELT (parms, i);
11865
11866 if (tuple == error_mark_node)
11867 continue;
11868
11869 TREE_VEC_ELT (new_vec, i) =
11870 tsubst_template_parm (tuple, args, complain);
11871 }
11872
11873 return new_vec;
11874 }
11875
11876 /* Return the result of substituting ARGS into the template parameters
11877 given by PARMS. If there are m levels of ARGS and m + n levels of
11878 PARMS, then the result will contain n levels of PARMS. For
11879 example, if PARMS is `template <class T> template <class U>
11880 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11881 result will be `template <int*, double, class V>'. */
11882
11883 static tree
11884 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11885 {
11886 tree r = NULL_TREE;
11887 tree* new_parms;
11888
11889 /* When substituting into a template, we must set
11890 PROCESSING_TEMPLATE_DECL as the template parameters may be
11891 dependent if they are based on one-another, and the dependency
11892 predicates are short-circuit outside of templates. */
11893 ++processing_template_decl;
11894
11895 for (new_parms = &r;
11896 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11897 new_parms = &(TREE_CHAIN (*new_parms)),
11898 parms = TREE_CHAIN (parms))
11899 {
11900 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11901 args, complain);
11902 *new_parms =
11903 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11904 - TMPL_ARGS_DEPTH (args)),
11905 new_vec, NULL_TREE);
11906 }
11907
11908 --processing_template_decl;
11909
11910 return r;
11911 }
11912
11913 /* Return the result of substituting ARGS into one template parameter
11914 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11915 parameter and which TREE_PURPOSE is the default argument of the
11916 template parameter. */
11917
11918 static tree
11919 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11920 {
11921 tree default_value, parm_decl;
11922
11923 if (args == NULL_TREE
11924 || t == NULL_TREE
11925 || t == error_mark_node)
11926 return t;
11927
11928 gcc_assert (TREE_CODE (t) == TREE_LIST);
11929
11930 default_value = TREE_PURPOSE (t);
11931 parm_decl = TREE_VALUE (t);
11932
11933 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11934 if (TREE_CODE (parm_decl) == PARM_DECL
11935 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11936 parm_decl = error_mark_node;
11937 default_value = tsubst_template_arg (default_value, args,
11938 complain, NULL_TREE);
11939
11940 return build_tree_list (default_value, parm_decl);
11941 }
11942
11943 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11944 type T. If T is not an aggregate or enumeration type, it is
11945 handled as if by tsubst. IN_DECL is as for tsubst. If
11946 ENTERING_SCOPE is nonzero, T is the context for a template which
11947 we are presently tsubst'ing. Return the substituted value. */
11948
11949 static tree
11950 tsubst_aggr_type (tree t,
11951 tree args,
11952 tsubst_flags_t complain,
11953 tree in_decl,
11954 int entering_scope)
11955 {
11956 if (t == NULL_TREE)
11957 return NULL_TREE;
11958
11959 switch (TREE_CODE (t))
11960 {
11961 case RECORD_TYPE:
11962 if (TYPE_PTRMEMFUNC_P (t))
11963 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11964
11965 /* Fall through. */
11966 case ENUMERAL_TYPE:
11967 case UNION_TYPE:
11968 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11969 {
11970 tree argvec;
11971 tree context;
11972 tree r;
11973 int saved_unevaluated_operand;
11974 int saved_inhibit_evaluation_warnings;
11975
11976 /* In "sizeof(X<I>)" we need to evaluate "I". */
11977 saved_unevaluated_operand = cp_unevaluated_operand;
11978 cp_unevaluated_operand = 0;
11979 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11980 c_inhibit_evaluation_warnings = 0;
11981
11982 /* First, determine the context for the type we are looking
11983 up. */
11984 context = TYPE_CONTEXT (t);
11985 if (context && TYPE_P (context))
11986 {
11987 context = tsubst_aggr_type (context, args, complain,
11988 in_decl, /*entering_scope=*/1);
11989 /* If context is a nested class inside a class template,
11990 it may still need to be instantiated (c++/33959). */
11991 context = complete_type (context);
11992 }
11993
11994 /* Then, figure out what arguments are appropriate for the
11995 type we are trying to find. For example, given:
11996
11997 template <class T> struct S;
11998 template <class T, class U> void f(T, U) { S<U> su; }
11999
12000 and supposing that we are instantiating f<int, double>,
12001 then our ARGS will be {int, double}, but, when looking up
12002 S we only want {double}. */
12003 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12004 complain, in_decl);
12005 if (argvec == error_mark_node)
12006 r = error_mark_node;
12007 else
12008 {
12009 r = lookup_template_class (t, argvec, in_decl, context,
12010 entering_scope, complain);
12011 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12012 }
12013
12014 cp_unevaluated_operand = saved_unevaluated_operand;
12015 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12016
12017 return r;
12018 }
12019 else
12020 /* This is not a template type, so there's nothing to do. */
12021 return t;
12022
12023 default:
12024 return tsubst (t, args, complain, in_decl);
12025 }
12026 }
12027
12028 static GTY((cache)) tree_cache_map *defarg_inst;
12029
12030 /* Substitute into the default argument ARG (a default argument for
12031 FN), which has the indicated TYPE. */
12032
12033 tree
12034 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12035 tsubst_flags_t complain)
12036 {
12037 tree saved_class_ptr = NULL_TREE;
12038 tree saved_class_ref = NULL_TREE;
12039 int errs = errorcount + sorrycount;
12040
12041 /* This can happen in invalid code. */
12042 if (TREE_CODE (arg) == DEFAULT_ARG)
12043 return arg;
12044
12045 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12046 parm = chain_index (parmnum, parm);
12047 tree parmtype = TREE_TYPE (parm);
12048 if (DECL_BY_REFERENCE (parm))
12049 parmtype = TREE_TYPE (parmtype);
12050 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12051
12052 tree *slot;
12053 if (defarg_inst && (slot = defarg_inst->get (parm)))
12054 return *slot;
12055
12056 /* This default argument came from a template. Instantiate the
12057 default argument here, not in tsubst. In the case of
12058 something like:
12059
12060 template <class T>
12061 struct S {
12062 static T t();
12063 void f(T = t());
12064 };
12065
12066 we must be careful to do name lookup in the scope of S<T>,
12067 rather than in the current class. */
12068 push_access_scope (fn);
12069 /* The "this" pointer is not valid in a default argument. */
12070 if (cfun)
12071 {
12072 saved_class_ptr = current_class_ptr;
12073 cp_function_chain->x_current_class_ptr = NULL_TREE;
12074 saved_class_ref = current_class_ref;
12075 cp_function_chain->x_current_class_ref = NULL_TREE;
12076 }
12077
12078 start_lambda_scope (parm);
12079
12080 push_deferring_access_checks(dk_no_deferred);
12081 /* The default argument expression may cause implicitly defined
12082 member functions to be synthesized, which will result in garbage
12083 collection. We must treat this situation as if we were within
12084 the body of function so as to avoid collecting live data on the
12085 stack. */
12086 ++function_depth;
12087 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12088 complain, NULL_TREE,
12089 /*integral_constant_expression_p=*/false);
12090 --function_depth;
12091 pop_deferring_access_checks();
12092
12093 finish_lambda_scope ();
12094
12095 /* Restore the "this" pointer. */
12096 if (cfun)
12097 {
12098 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12099 cp_function_chain->x_current_class_ref = saved_class_ref;
12100 }
12101
12102 if (errorcount+sorrycount > errs
12103 && (complain & tf_warning_or_error))
12104 inform (input_location,
12105 " when instantiating default argument for call to %qD", fn);
12106
12107 /* Make sure the default argument is reasonable. */
12108 arg = check_default_argument (type, arg, complain);
12109
12110 pop_access_scope (fn);
12111
12112 if (arg != error_mark_node && !cp_unevaluated_operand)
12113 {
12114 if (!defarg_inst)
12115 defarg_inst = tree_cache_map::create_ggc (37);
12116 defarg_inst->put (parm, arg);
12117 }
12118
12119 return arg;
12120 }
12121
12122 /* Substitute into all the default arguments for FN. */
12123
12124 static void
12125 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12126 {
12127 tree arg;
12128 tree tmpl_args;
12129
12130 tmpl_args = DECL_TI_ARGS (fn);
12131
12132 /* If this function is not yet instantiated, we certainly don't need
12133 its default arguments. */
12134 if (uses_template_parms (tmpl_args))
12135 return;
12136 /* Don't do this again for clones. */
12137 if (DECL_CLONED_FUNCTION_P (fn))
12138 return;
12139
12140 int i = 0;
12141 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12142 arg;
12143 arg = TREE_CHAIN (arg), ++i)
12144 if (TREE_PURPOSE (arg))
12145 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12146 TREE_VALUE (arg),
12147 TREE_PURPOSE (arg),
12148 complain);
12149 }
12150
12151 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12152
12153 static tree
12154 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12155 tree lambda_fntype)
12156 {
12157 tree gen_tmpl, argvec;
12158 hashval_t hash = 0;
12159 tree in_decl = t;
12160
12161 /* Nobody should be tsubst'ing into non-template functions. */
12162 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12163
12164 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12165 {
12166 /* If T is not dependent, just return it. */
12167 if (!uses_template_parms (DECL_TI_ARGS (t)))
12168 return t;
12169
12170 /* Calculate the most general template of which R is a
12171 specialization, and the complete set of arguments used to
12172 specialize R. */
12173 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12174 argvec = tsubst_template_args (DECL_TI_ARGS
12175 (DECL_TEMPLATE_RESULT
12176 (DECL_TI_TEMPLATE (t))),
12177 args, complain, in_decl);
12178 if (argvec == error_mark_node)
12179 return error_mark_node;
12180
12181 /* Check to see if we already have this specialization. */
12182 if (!lambda_fntype)
12183 {
12184 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12185 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12186 return spec;
12187 }
12188
12189 /* We can see more levels of arguments than parameters if
12190 there was a specialization of a member template, like
12191 this:
12192
12193 template <class T> struct S { template <class U> void f(); }
12194 template <> template <class U> void S<int>::f(U);
12195
12196 Here, we'll be substituting into the specialization,
12197 because that's where we can find the code we actually
12198 want to generate, but we'll have enough arguments for
12199 the most general template.
12200
12201 We also deal with the peculiar case:
12202
12203 template <class T> struct S {
12204 template <class U> friend void f();
12205 };
12206 template <class U> void f() {}
12207 template S<int>;
12208 template void f<double>();
12209
12210 Here, the ARGS for the instantiation of will be {int,
12211 double}. But, we only need as many ARGS as there are
12212 levels of template parameters in CODE_PATTERN. We are
12213 careful not to get fooled into reducing the ARGS in
12214 situations like:
12215
12216 template <class T> struct S { template <class U> void f(U); }
12217 template <class T> template <> void S<T>::f(int) {}
12218
12219 which we can spot because the pattern will be a
12220 specialization in this case. */
12221 int args_depth = TMPL_ARGS_DEPTH (args);
12222 int parms_depth =
12223 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12224
12225 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12226 args = get_innermost_template_args (args, parms_depth);
12227 }
12228 else
12229 {
12230 /* This special case arises when we have something like this:
12231
12232 template <class T> struct S {
12233 friend void f<int>(int, double);
12234 };
12235
12236 Here, the DECL_TI_TEMPLATE for the friend declaration
12237 will be an IDENTIFIER_NODE. We are being called from
12238 tsubst_friend_function, and we want only to create a
12239 new decl (R) with appropriate types so that we can call
12240 determine_specialization. */
12241 gen_tmpl = NULL_TREE;
12242 argvec = NULL_TREE;
12243 }
12244
12245 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12246 : NULL_TREE);
12247 tree ctx = closure ? closure : DECL_CONTEXT (t);
12248 bool member = ctx && TYPE_P (ctx);
12249
12250 if (member && !closure)
12251 ctx = tsubst_aggr_type (ctx, args,
12252 complain, t, /*entering_scope=*/1);
12253
12254 tree type = (lambda_fntype ? lambda_fntype
12255 : tsubst (TREE_TYPE (t), args,
12256 complain | tf_fndecl_type, in_decl));
12257 if (type == error_mark_node)
12258 return error_mark_node;
12259
12260 /* If we hit excessive deduction depth, the type is bogus even if
12261 it isn't error_mark_node, so don't build a decl. */
12262 if (excessive_deduction_depth)
12263 return error_mark_node;
12264
12265 /* We do NOT check for matching decls pushed separately at this
12266 point, as they may not represent instantiations of this
12267 template, and in any case are considered separate under the
12268 discrete model. */
12269 tree r = copy_decl (t);
12270 DECL_USE_TEMPLATE (r) = 0;
12271 TREE_TYPE (r) = type;
12272 /* Clear out the mangled name and RTL for the instantiation. */
12273 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12274 SET_DECL_RTL (r, NULL);
12275 /* Leave DECL_INITIAL set on deleted instantiations. */
12276 if (!DECL_DELETED_FN (r))
12277 DECL_INITIAL (r) = NULL_TREE;
12278 DECL_CONTEXT (r) = ctx;
12279
12280 /* OpenMP UDRs have the only argument a reference to the declared
12281 type. We want to diagnose if the declared type is a reference,
12282 which is invalid, but as references to references are usually
12283 quietly merged, diagnose it here. */
12284 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12285 {
12286 tree argtype
12287 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12288 argtype = tsubst (argtype, args, complain, in_decl);
12289 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12290 error_at (DECL_SOURCE_LOCATION (t),
12291 "reference type %qT in "
12292 "%<#pragma omp declare reduction%>", argtype);
12293 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12294 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12295 argtype);
12296 }
12297
12298 if (member && DECL_CONV_FN_P (r))
12299 /* Type-conversion operator. Reconstruct the name, in
12300 case it's the name of one of the template's parameters. */
12301 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12302
12303 tree parms = DECL_ARGUMENTS (t);
12304 if (closure)
12305 parms = DECL_CHAIN (parms);
12306 parms = tsubst (parms, args, complain, t);
12307 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12308 DECL_CONTEXT (parm) = r;
12309 if (closure)
12310 {
12311 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12312 DECL_CHAIN (tparm) = parms;
12313 parms = tparm;
12314 }
12315 DECL_ARGUMENTS (r) = parms;
12316 DECL_RESULT (r) = NULL_TREE;
12317
12318 TREE_STATIC (r) = 0;
12319 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12320 DECL_EXTERNAL (r) = 1;
12321 /* If this is an instantiation of a function with internal
12322 linkage, we already know what object file linkage will be
12323 assigned to the instantiation. */
12324 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12325 DECL_DEFER_OUTPUT (r) = 0;
12326 DECL_CHAIN (r) = NULL_TREE;
12327 DECL_PENDING_INLINE_INFO (r) = 0;
12328 DECL_PENDING_INLINE_P (r) = 0;
12329 DECL_SAVED_TREE (r) = NULL_TREE;
12330 DECL_STRUCT_FUNCTION (r) = NULL;
12331 TREE_USED (r) = 0;
12332 /* We'll re-clone as appropriate in instantiate_template. */
12333 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12334
12335 /* If we aren't complaining now, return on error before we register
12336 the specialization so that we'll complain eventually. */
12337 if ((complain & tf_error) == 0
12338 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12339 && !grok_op_properties (r, /*complain=*/false))
12340 return error_mark_node;
12341
12342 /* When instantiating a constrained member, substitute
12343 into the constraints to create a new constraint. */
12344 if (tree ci = get_constraints (t))
12345 if (member)
12346 {
12347 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12348 set_constraints (r, ci);
12349 }
12350
12351 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12352 this in the special friend case mentioned above where
12353 GEN_TMPL is NULL. */
12354 if (gen_tmpl && !closure)
12355 {
12356 DECL_TEMPLATE_INFO (r)
12357 = build_template_info (gen_tmpl, argvec);
12358 SET_DECL_IMPLICIT_INSTANTIATION (r);
12359
12360 tree new_r
12361 = register_specialization (r, gen_tmpl, argvec, false, hash);
12362 if (new_r != r)
12363 /* We instantiated this while substituting into
12364 the type earlier (template/friend54.C). */
12365 return new_r;
12366
12367 /* We're not supposed to instantiate default arguments
12368 until they are called, for a template. But, for a
12369 declaration like:
12370
12371 template <class T> void f ()
12372 { extern void g(int i = T()); }
12373
12374 we should do the substitution when the template is
12375 instantiated. We handle the member function case in
12376 instantiate_class_template since the default arguments
12377 might refer to other members of the class. */
12378 if (!member
12379 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12380 && !uses_template_parms (argvec))
12381 tsubst_default_arguments (r, complain);
12382 }
12383 else
12384 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12385
12386 /* Copy the list of befriending classes. */
12387 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12388 *friends;
12389 friends = &TREE_CHAIN (*friends))
12390 {
12391 *friends = copy_node (*friends);
12392 TREE_VALUE (*friends)
12393 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12394 }
12395
12396 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12397 {
12398 maybe_retrofit_in_chrg (r);
12399 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12400 return error_mark_node;
12401 /* If this is an instantiation of a member template, clone it.
12402 If it isn't, that'll be handled by
12403 clone_constructors_and_destructors. */
12404 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12405 clone_function_decl (r, /*update_methods=*/false);
12406 }
12407 else if ((complain & tf_error) != 0
12408 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12409 && !grok_op_properties (r, /*complain=*/true))
12410 return error_mark_node;
12411
12412 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12413 SET_DECL_FRIEND_CONTEXT (r,
12414 tsubst (DECL_FRIEND_CONTEXT (t),
12415 args, complain, in_decl));
12416
12417 /* Possibly limit visibility based on template args. */
12418 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12419 if (DECL_VISIBILITY_SPECIFIED (t))
12420 {
12421 DECL_VISIBILITY_SPECIFIED (r) = 0;
12422 DECL_ATTRIBUTES (r)
12423 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12424 }
12425 determine_visibility (r);
12426 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12427 && !processing_template_decl)
12428 defaulted_late_check (r);
12429
12430 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12431 args, complain, in_decl);
12432 return r;
12433 }
12434
12435 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12436
12437 static tree
12438 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12439 tree lambda_fntype)
12440 {
12441 /* We can get here when processing a member function template,
12442 member class template, or template template parameter. */
12443 tree decl = DECL_TEMPLATE_RESULT (t);
12444 tree in_decl = t;
12445 tree spec;
12446 tree tmpl_args;
12447 tree full_args;
12448 tree r;
12449 hashval_t hash = 0;
12450
12451 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12452 {
12453 /* Template template parameter is treated here. */
12454 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12455 if (new_type == error_mark_node)
12456 r = error_mark_node;
12457 /* If we get a real template back, return it. This can happen in
12458 the context of most_specialized_partial_spec. */
12459 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12460 r = new_type;
12461 else
12462 /* The new TEMPLATE_DECL was built in
12463 reduce_template_parm_level. */
12464 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12465 return r;
12466 }
12467
12468 if (!lambda_fntype)
12469 {
12470 /* We might already have an instance of this template.
12471 The ARGS are for the surrounding class type, so the
12472 full args contain the tsubst'd args for the context,
12473 plus the innermost args from the template decl. */
12474 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12475 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12476 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12477 /* Because this is a template, the arguments will still be
12478 dependent, even after substitution. If
12479 PROCESSING_TEMPLATE_DECL is not set, the dependency
12480 predicates will short-circuit. */
12481 ++processing_template_decl;
12482 full_args = tsubst_template_args (tmpl_args, args,
12483 complain, in_decl);
12484 --processing_template_decl;
12485 if (full_args == error_mark_node)
12486 return error_mark_node;
12487
12488 /* If this is a default template template argument,
12489 tsubst might not have changed anything. */
12490 if (full_args == tmpl_args)
12491 return t;
12492
12493 hash = hash_tmpl_and_args (t, full_args);
12494 spec = retrieve_specialization (t, full_args, hash);
12495 if (spec != NULL_TREE)
12496 return spec;
12497 }
12498
12499 /* Make a new template decl. It will be similar to the
12500 original, but will record the current template arguments.
12501 We also create a new function declaration, which is just
12502 like the old one, but points to this new template, rather
12503 than the old one. */
12504 r = copy_decl (t);
12505 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12506 DECL_CHAIN (r) = NULL_TREE;
12507
12508 // Build new template info linking to the original template decl.
12509 if (!lambda_fntype)
12510 {
12511 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12512 SET_DECL_IMPLICIT_INSTANTIATION (r);
12513 }
12514 else
12515 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12516
12517 /* The template parameters for this new template are all the
12518 template parameters for the old template, except the
12519 outermost level of parameters. */
12520 DECL_TEMPLATE_PARMS (r)
12521 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12522 complain);
12523
12524 if (TREE_CODE (decl) == TYPE_DECL
12525 && !TYPE_DECL_ALIAS_P (decl))
12526 {
12527 tree new_type;
12528 ++processing_template_decl;
12529 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12530 --processing_template_decl;
12531 if (new_type == error_mark_node)
12532 return error_mark_node;
12533
12534 TREE_TYPE (r) = new_type;
12535 /* For a partial specialization, we need to keep pointing to
12536 the primary template. */
12537 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12538 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12539 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12540 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12541 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12542 }
12543 else
12544 {
12545 tree new_decl;
12546 ++processing_template_decl;
12547 if (TREE_CODE (decl) == FUNCTION_DECL)
12548 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12549 else
12550 new_decl = tsubst (decl, args, complain, in_decl);
12551 --processing_template_decl;
12552 if (new_decl == error_mark_node)
12553 return error_mark_node;
12554
12555 DECL_TEMPLATE_RESULT (r) = new_decl;
12556 TREE_TYPE (r) = TREE_TYPE (new_decl);
12557 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12558 if (lambda_fntype)
12559 {
12560 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12561 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12562 }
12563 else
12564 {
12565 DECL_TI_TEMPLATE (new_decl) = r;
12566 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12567 }
12568 }
12569
12570 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12571 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12572
12573 if (PRIMARY_TEMPLATE_P (t))
12574 DECL_PRIMARY_TEMPLATE (r) = r;
12575
12576 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12577 && !lambda_fntype)
12578 /* Record this non-type partial instantiation. */
12579 register_specialization (r, t,
12580 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12581 false, hash);
12582
12583 return r;
12584 }
12585
12586 /* True if FN is the op() for a lambda in an uninstantiated template. */
12587
12588 bool
12589 lambda_fn_in_template_p (tree fn)
12590 {
12591 if (!fn || !LAMBDA_FUNCTION_P (fn))
12592 return false;
12593 tree closure = DECL_CONTEXT (fn);
12594 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12595 }
12596
12597 /* True if FN is the op() for a lambda regenerated from a lambda in an
12598 uninstantiated template. */
12599
12600 bool
12601 regenerated_lambda_fn_p (tree fn)
12602 {
12603 return (LAMBDA_FUNCTION_P (fn)
12604 && !DECL_TEMPLATE_INSTANTIATION (fn));
12605 }
12606
12607 /* We're instantiating a variable from template function TCTX. Return the
12608 corresponding current enclosing scope. This gets complicated because lambda
12609 functions in templates are regenerated rather than instantiated, but generic
12610 lambda functions are subsequently instantiated. */
12611
12612 static tree
12613 enclosing_instantiation_of (tree tctx)
12614 {
12615 tree fn = current_function_decl;
12616 int lambda_count = 0;
12617
12618 for (; tctx && lambda_fn_in_template_p (tctx);
12619 tctx = decl_function_context (tctx))
12620 ++lambda_count;
12621 for (; fn; fn = decl_function_context (fn))
12622 {
12623 tree lambda = fn;
12624 int flambda_count = 0;
12625 for (; fn && regenerated_lambda_fn_p (fn);
12626 fn = decl_function_context (fn))
12627 ++flambda_count;
12628 if (DECL_TEMPLATE_INFO (fn)
12629 ? most_general_template (fn) != most_general_template (tctx)
12630 : fn != tctx)
12631 continue;
12632 if (lambda_count)
12633 {
12634 fn = lambda;
12635 while (flambda_count-- > lambda_count)
12636 fn = decl_function_context (fn);
12637 }
12638 return fn;
12639 }
12640 gcc_unreachable ();
12641 }
12642
12643 /* Substitute the ARGS into the T, which is a _DECL. Return the
12644 result of the substitution. Issue error and warning messages under
12645 control of COMPLAIN. */
12646
12647 static tree
12648 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12649 {
12650 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12651 location_t saved_loc;
12652 tree r = NULL_TREE;
12653 tree in_decl = t;
12654 hashval_t hash = 0;
12655
12656 /* Set the filename and linenumber to improve error-reporting. */
12657 saved_loc = input_location;
12658 input_location = DECL_SOURCE_LOCATION (t);
12659
12660 switch (TREE_CODE (t))
12661 {
12662 case TEMPLATE_DECL:
12663 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12664 break;
12665
12666 case FUNCTION_DECL:
12667 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12668 break;
12669
12670 case PARM_DECL:
12671 {
12672 tree type = NULL_TREE;
12673 int i, len = 1;
12674 tree expanded_types = NULL_TREE;
12675 tree prev_r = NULL_TREE;
12676 tree first_r = NULL_TREE;
12677
12678 if (DECL_PACK_P (t))
12679 {
12680 /* If there is a local specialization that isn't a
12681 parameter pack, it means that we're doing a "simple"
12682 substitution from inside tsubst_pack_expansion. Just
12683 return the local specialization (which will be a single
12684 parm). */
12685 tree spec = retrieve_local_specialization (t);
12686 if (spec
12687 && TREE_CODE (spec) == PARM_DECL
12688 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12689 RETURN (spec);
12690
12691 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12692 the parameters in this function parameter pack. */
12693 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12694 complain, in_decl);
12695 if (TREE_CODE (expanded_types) == TREE_VEC)
12696 {
12697 len = TREE_VEC_LENGTH (expanded_types);
12698
12699 /* Zero-length parameter packs are boring. Just substitute
12700 into the chain. */
12701 if (len == 0)
12702 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12703 TREE_CHAIN (t)));
12704 }
12705 else
12706 {
12707 /* All we did was update the type. Make a note of that. */
12708 type = expanded_types;
12709 expanded_types = NULL_TREE;
12710 }
12711 }
12712
12713 /* Loop through all of the parameters we'll build. When T is
12714 a function parameter pack, LEN is the number of expanded
12715 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12716 r = NULL_TREE;
12717 for (i = 0; i < len; ++i)
12718 {
12719 prev_r = r;
12720 r = copy_node (t);
12721 if (DECL_TEMPLATE_PARM_P (t))
12722 SET_DECL_TEMPLATE_PARM_P (r);
12723
12724 if (expanded_types)
12725 /* We're on the Ith parameter of the function parameter
12726 pack. */
12727 {
12728 /* Get the Ith type. */
12729 type = TREE_VEC_ELT (expanded_types, i);
12730
12731 /* Rename the parameter to include the index. */
12732 DECL_NAME (r)
12733 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12734 }
12735 else if (!type)
12736 /* We're dealing with a normal parameter. */
12737 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12738
12739 type = type_decays_to (type);
12740 TREE_TYPE (r) = type;
12741 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12742
12743 if (DECL_INITIAL (r))
12744 {
12745 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12746 DECL_INITIAL (r) = TREE_TYPE (r);
12747 else
12748 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12749 complain, in_decl);
12750 }
12751
12752 DECL_CONTEXT (r) = NULL_TREE;
12753
12754 if (!DECL_TEMPLATE_PARM_P (r))
12755 DECL_ARG_TYPE (r) = type_passed_as (type);
12756
12757 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12758 args, complain, in_decl);
12759
12760 /* Keep track of the first new parameter we
12761 generate. That's what will be returned to the
12762 caller. */
12763 if (!first_r)
12764 first_r = r;
12765
12766 /* Build a proper chain of parameters when substituting
12767 into a function parameter pack. */
12768 if (prev_r)
12769 DECL_CHAIN (prev_r) = r;
12770 }
12771
12772 /* If cp_unevaluated_operand is set, we're just looking for a
12773 single dummy parameter, so don't keep going. */
12774 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12775 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12776 complain, DECL_CHAIN (t));
12777
12778 /* FIRST_R contains the start of the chain we've built. */
12779 r = first_r;
12780 }
12781 break;
12782
12783 case FIELD_DECL:
12784 {
12785 tree type = NULL_TREE;
12786 tree vec = NULL_TREE;
12787 tree expanded_types = NULL_TREE;
12788 int len = 1;
12789
12790 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12791 {
12792 /* This field is a lambda capture pack. Return a TREE_VEC of
12793 the expanded fields to instantiate_class_template_1 and
12794 store them in the specializations hash table as a
12795 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12796 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12797 complain, in_decl);
12798 if (TREE_CODE (expanded_types) == TREE_VEC)
12799 {
12800 len = TREE_VEC_LENGTH (expanded_types);
12801 vec = make_tree_vec (len);
12802 }
12803 else
12804 {
12805 /* All we did was update the type. Make a note of that. */
12806 type = expanded_types;
12807 expanded_types = NULL_TREE;
12808 }
12809 }
12810
12811 for (int i = 0; i < len; ++i)
12812 {
12813 r = copy_decl (t);
12814 if (expanded_types)
12815 {
12816 type = TREE_VEC_ELT (expanded_types, i);
12817 DECL_NAME (r)
12818 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12819 }
12820 else if (!type)
12821 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12822
12823 if (type == error_mark_node)
12824 RETURN (error_mark_node);
12825 TREE_TYPE (r) = type;
12826 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12827
12828 if (DECL_C_BIT_FIELD (r))
12829 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12830 number of bits. */
12831 DECL_BIT_FIELD_REPRESENTATIVE (r)
12832 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12833 complain, in_decl,
12834 /*integral_constant_expression_p=*/true);
12835 if (DECL_INITIAL (t))
12836 {
12837 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12838 NSDMI in perform_member_init. Still set DECL_INITIAL
12839 so that we know there is one. */
12840 DECL_INITIAL (r) = void_node;
12841 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12842 retrofit_lang_decl (r);
12843 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12844 }
12845 /* We don't have to set DECL_CONTEXT here; it is set by
12846 finish_member_declaration. */
12847 DECL_CHAIN (r) = NULL_TREE;
12848
12849 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12850 args, complain, in_decl);
12851
12852 if (vec)
12853 TREE_VEC_ELT (vec, i) = r;
12854 }
12855
12856 if (vec)
12857 {
12858 r = vec;
12859 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12860 SET_ARGUMENT_PACK_ARGS (pack, vec);
12861 register_specialization (pack, t, args, false, 0);
12862 }
12863 }
12864 break;
12865
12866 case USING_DECL:
12867 /* We reach here only for member using decls. We also need to check
12868 uses_template_parms because DECL_DEPENDENT_P is not set for a
12869 using-declaration that designates a member of the current
12870 instantiation (c++/53549). */
12871 if (DECL_DEPENDENT_P (t)
12872 || uses_template_parms (USING_DECL_SCOPE (t)))
12873 {
12874 tree scope = USING_DECL_SCOPE (t);
12875 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12876 if (PACK_EXPANSION_P (scope))
12877 {
12878 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12879 int len = TREE_VEC_LENGTH (vec);
12880 r = make_tree_vec (len);
12881 for (int i = 0; i < len; ++i)
12882 {
12883 tree escope = TREE_VEC_ELT (vec, i);
12884 tree elt = do_class_using_decl (escope, name);
12885 if (!elt)
12886 {
12887 r = error_mark_node;
12888 break;
12889 }
12890 else
12891 {
12892 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12893 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12894 }
12895 TREE_VEC_ELT (r, i) = elt;
12896 }
12897 }
12898 else
12899 {
12900 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12901 complain, in_decl);
12902 r = do_class_using_decl (inst_scope, name);
12903 if (!r)
12904 r = error_mark_node;
12905 else
12906 {
12907 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12908 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12909 }
12910 }
12911 }
12912 else
12913 {
12914 r = copy_node (t);
12915 DECL_CHAIN (r) = NULL_TREE;
12916 }
12917 break;
12918
12919 case TYPE_DECL:
12920 case VAR_DECL:
12921 {
12922 tree argvec = NULL_TREE;
12923 tree gen_tmpl = NULL_TREE;
12924 tree spec;
12925 tree tmpl = NULL_TREE;
12926 tree ctx;
12927 tree type = NULL_TREE;
12928 bool local_p;
12929
12930 if (TREE_TYPE (t) == error_mark_node)
12931 RETURN (error_mark_node);
12932
12933 if (TREE_CODE (t) == TYPE_DECL
12934 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12935 {
12936 /* If this is the canonical decl, we don't have to
12937 mess with instantiations, and often we can't (for
12938 typename, template type parms and such). Note that
12939 TYPE_NAME is not correct for the above test if
12940 we've copied the type for a typedef. */
12941 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12942 if (type == error_mark_node)
12943 RETURN (error_mark_node);
12944 r = TYPE_NAME (type);
12945 break;
12946 }
12947
12948 /* Check to see if we already have the specialization we
12949 need. */
12950 spec = NULL_TREE;
12951 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12952 {
12953 /* T is a static data member or namespace-scope entity.
12954 We have to substitute into namespace-scope variables
12955 (not just variable templates) because of cases like:
12956
12957 template <class T> void f() { extern T t; }
12958
12959 where the entity referenced is not known until
12960 instantiation time. */
12961 local_p = false;
12962 ctx = DECL_CONTEXT (t);
12963 if (DECL_CLASS_SCOPE_P (t))
12964 {
12965 ctx = tsubst_aggr_type (ctx, args,
12966 complain,
12967 in_decl, /*entering_scope=*/1);
12968 /* If CTX is unchanged, then T is in fact the
12969 specialization we want. That situation occurs when
12970 referencing a static data member within in its own
12971 class. We can use pointer equality, rather than
12972 same_type_p, because DECL_CONTEXT is always
12973 canonical... */
12974 if (ctx == DECL_CONTEXT (t)
12975 /* ... unless T is a member template; in which
12976 case our caller can be willing to create a
12977 specialization of that template represented
12978 by T. */
12979 && !(DECL_TI_TEMPLATE (t)
12980 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12981 spec = t;
12982 }
12983
12984 if (!spec)
12985 {
12986 tmpl = DECL_TI_TEMPLATE (t);
12987 gen_tmpl = most_general_template (tmpl);
12988 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12989 if (argvec != error_mark_node)
12990 argvec = (coerce_innermost_template_parms
12991 (DECL_TEMPLATE_PARMS (gen_tmpl),
12992 argvec, t, complain,
12993 /*all*/true, /*defarg*/true));
12994 if (argvec == error_mark_node)
12995 RETURN (error_mark_node);
12996 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12997 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12998 }
12999 }
13000 else
13001 {
13002 /* A local variable. */
13003 local_p = true;
13004 /* Subsequent calls to pushdecl will fill this in. */
13005 ctx = NULL_TREE;
13006 /* Unless this is a reference to a static variable from an
13007 enclosing function, in which case we need to fill it in now. */
13008 if (TREE_STATIC (t))
13009 {
13010 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13011 if (fn != current_function_decl)
13012 ctx = fn;
13013 }
13014 spec = retrieve_local_specialization (t);
13015 }
13016 /* If we already have the specialization we need, there is
13017 nothing more to do. */
13018 if (spec)
13019 {
13020 r = spec;
13021 break;
13022 }
13023
13024 /* Create a new node for the specialization we need. */
13025 r = copy_decl (t);
13026 if (type == NULL_TREE)
13027 {
13028 if (is_typedef_decl (t))
13029 type = DECL_ORIGINAL_TYPE (t);
13030 else
13031 type = TREE_TYPE (t);
13032 if (VAR_P (t)
13033 && VAR_HAD_UNKNOWN_BOUND (t)
13034 && type != error_mark_node)
13035 type = strip_array_domain (type);
13036 tree sub_args = args;
13037 if (tree auto_node = type_uses_auto (type))
13038 {
13039 /* Mask off any template args past the variable's context so we
13040 don't replace the auto with an unrelated argument. */
13041 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13042 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13043 if (extra > 0)
13044 /* This should never happen with the new lambda instantiation
13045 model, but keep the handling just in case. */
13046 gcc_assert (!CHECKING_P),
13047 sub_args = strip_innermost_template_args (args, extra);
13048 }
13049 type = tsubst (type, sub_args, complain, in_decl);
13050 }
13051 if (VAR_P (r))
13052 {
13053 /* Even if the original location is out of scope, the
13054 newly substituted one is not. */
13055 DECL_DEAD_FOR_LOCAL (r) = 0;
13056 DECL_INITIALIZED_P (r) = 0;
13057 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13058 if (type == error_mark_node)
13059 RETURN (error_mark_node);
13060 if (TREE_CODE (type) == FUNCTION_TYPE)
13061 {
13062 /* It may seem that this case cannot occur, since:
13063
13064 typedef void f();
13065 void g() { f x; }
13066
13067 declares a function, not a variable. However:
13068
13069 typedef void f();
13070 template <typename T> void g() { T t; }
13071 template void g<f>();
13072
13073 is an attempt to declare a variable with function
13074 type. */
13075 error ("variable %qD has function type",
13076 /* R is not yet sufficiently initialized, so we
13077 just use its name. */
13078 DECL_NAME (r));
13079 RETURN (error_mark_node);
13080 }
13081 type = complete_type (type);
13082 /* Wait until cp_finish_decl to set this again, to handle
13083 circular dependency (template/instantiate6.C). */
13084 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13085 type = check_var_type (DECL_NAME (r), type);
13086
13087 if (DECL_HAS_VALUE_EXPR_P (t))
13088 {
13089 tree ve = DECL_VALUE_EXPR (t);
13090 ve = tsubst_expr (ve, args, complain, in_decl,
13091 /*constant_expression_p=*/false);
13092 if (REFERENCE_REF_P (ve))
13093 {
13094 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13095 ve = TREE_OPERAND (ve, 0);
13096 }
13097 SET_DECL_VALUE_EXPR (r, ve);
13098 }
13099 if (CP_DECL_THREAD_LOCAL_P (r)
13100 && !processing_template_decl)
13101 set_decl_tls_model (r, decl_default_tls_model (r));
13102 }
13103 else if (DECL_SELF_REFERENCE_P (t))
13104 SET_DECL_SELF_REFERENCE_P (r);
13105 TREE_TYPE (r) = type;
13106 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13107 DECL_CONTEXT (r) = ctx;
13108 /* Clear out the mangled name and RTL for the instantiation. */
13109 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13110 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13111 SET_DECL_RTL (r, NULL);
13112 /* The initializer must not be expanded until it is required;
13113 see [temp.inst]. */
13114 DECL_INITIAL (r) = NULL_TREE;
13115 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13116 if (VAR_P (r))
13117 {
13118 if (DECL_LANG_SPECIFIC (r))
13119 SET_DECL_DEPENDENT_INIT_P (r, false);
13120
13121 SET_DECL_MODE (r, VOIDmode);
13122
13123 /* Possibly limit visibility based on template args. */
13124 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13125 if (DECL_VISIBILITY_SPECIFIED (t))
13126 {
13127 DECL_VISIBILITY_SPECIFIED (r) = 0;
13128 DECL_ATTRIBUTES (r)
13129 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13130 }
13131 determine_visibility (r);
13132 }
13133
13134 if (!local_p)
13135 {
13136 /* A static data member declaration is always marked
13137 external when it is declared in-class, even if an
13138 initializer is present. We mimic the non-template
13139 processing here. */
13140 DECL_EXTERNAL (r) = 1;
13141 if (DECL_NAMESPACE_SCOPE_P (t))
13142 DECL_NOT_REALLY_EXTERN (r) = 1;
13143
13144 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13145 SET_DECL_IMPLICIT_INSTANTIATION (r);
13146 register_specialization (r, gen_tmpl, argvec, false, hash);
13147 }
13148 else
13149 {
13150 if (DECL_LANG_SPECIFIC (r))
13151 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13152 if (!cp_unevaluated_operand)
13153 register_local_specialization (r, t);
13154 }
13155
13156 DECL_CHAIN (r) = NULL_TREE;
13157
13158 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13159 /*flags=*/0,
13160 args, complain, in_decl);
13161
13162 /* Preserve a typedef that names a type. */
13163 if (is_typedef_decl (r) && type != error_mark_node)
13164 {
13165 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13166 set_underlying_type (r);
13167 if (TYPE_DECL_ALIAS_P (r))
13168 /* An alias template specialization can be dependent
13169 even if its underlying type is not. */
13170 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13171 }
13172
13173 layout_decl (r, 0);
13174 }
13175 break;
13176
13177 default:
13178 gcc_unreachable ();
13179 }
13180 #undef RETURN
13181
13182 out:
13183 /* Restore the file and line information. */
13184 input_location = saved_loc;
13185
13186 return r;
13187 }
13188
13189 /* Substitute into the ARG_TYPES of a function type.
13190 If END is a TREE_CHAIN, leave it and any following types
13191 un-substituted. */
13192
13193 static tree
13194 tsubst_arg_types (tree arg_types,
13195 tree args,
13196 tree end,
13197 tsubst_flags_t complain,
13198 tree in_decl)
13199 {
13200 tree remaining_arg_types;
13201 tree type = NULL_TREE;
13202 int i = 1;
13203 tree expanded_args = NULL_TREE;
13204 tree default_arg;
13205
13206 if (!arg_types || arg_types == void_list_node || arg_types == end)
13207 return arg_types;
13208
13209 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13210 args, end, complain, in_decl);
13211 if (remaining_arg_types == error_mark_node)
13212 return error_mark_node;
13213
13214 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13215 {
13216 /* For a pack expansion, perform substitution on the
13217 entire expression. Later on, we'll handle the arguments
13218 one-by-one. */
13219 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13220 args, complain, in_decl);
13221
13222 if (TREE_CODE (expanded_args) == TREE_VEC)
13223 /* So that we'll spin through the parameters, one by one. */
13224 i = TREE_VEC_LENGTH (expanded_args);
13225 else
13226 {
13227 /* We only partially substituted into the parameter
13228 pack. Our type is TYPE_PACK_EXPANSION. */
13229 type = expanded_args;
13230 expanded_args = NULL_TREE;
13231 }
13232 }
13233
13234 while (i > 0) {
13235 --i;
13236
13237 if (expanded_args)
13238 type = TREE_VEC_ELT (expanded_args, i);
13239 else if (!type)
13240 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13241
13242 if (type == error_mark_node)
13243 return error_mark_node;
13244 if (VOID_TYPE_P (type))
13245 {
13246 if (complain & tf_error)
13247 {
13248 error ("invalid parameter type %qT", type);
13249 if (in_decl)
13250 error ("in declaration %q+D", in_decl);
13251 }
13252 return error_mark_node;
13253 }
13254 /* DR 657. */
13255 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13256 return error_mark_node;
13257
13258 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13259 top-level qualifiers as required. */
13260 type = cv_unqualified (type_decays_to (type));
13261
13262 /* We do not substitute into default arguments here. The standard
13263 mandates that they be instantiated only when needed, which is
13264 done in build_over_call. */
13265 default_arg = TREE_PURPOSE (arg_types);
13266
13267 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13268 since the new op() won't have any associated template arguments for us
13269 to refer to later. */
13270 if (lambda_fn_in_template_p (in_decl))
13271 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13272 false/*fn*/, false/*constexpr*/);
13273
13274 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13275 {
13276 /* We've instantiated a template before its default arguments
13277 have been parsed. This can happen for a nested template
13278 class, and is not an error unless we require the default
13279 argument in a call of this function. */
13280 remaining_arg_types =
13281 tree_cons (default_arg, type, remaining_arg_types);
13282 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13283 }
13284 else
13285 remaining_arg_types =
13286 hash_tree_cons (default_arg, type, remaining_arg_types);
13287 }
13288
13289 return remaining_arg_types;
13290 }
13291
13292 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13293 *not* handle the exception-specification for FNTYPE, because the
13294 initial substitution of explicitly provided template parameters
13295 during argument deduction forbids substitution into the
13296 exception-specification:
13297
13298 [temp.deduct]
13299
13300 All references in the function type of the function template to the
13301 corresponding template parameters are replaced by the specified tem-
13302 plate argument values. If a substitution in a template parameter or
13303 in the function type of the function template results in an invalid
13304 type, type deduction fails. [Note: The equivalent substitution in
13305 exception specifications is done only when the function is instanti-
13306 ated, at which point a program is ill-formed if the substitution
13307 results in an invalid type.] */
13308
13309 static tree
13310 tsubst_function_type (tree t,
13311 tree args,
13312 tsubst_flags_t complain,
13313 tree in_decl)
13314 {
13315 tree return_type;
13316 tree arg_types = NULL_TREE;
13317 tree fntype;
13318
13319 /* The TYPE_CONTEXT is not used for function/method types. */
13320 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13321
13322 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13323 failure. */
13324 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13325
13326 if (late_return_type_p)
13327 {
13328 /* Substitute the argument types. */
13329 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13330 complain, in_decl);
13331 if (arg_types == error_mark_node)
13332 return error_mark_node;
13333
13334 tree save_ccp = current_class_ptr;
13335 tree save_ccr = current_class_ref;
13336 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13337 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13338 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13339 if (do_inject)
13340 {
13341 /* DR 1207: 'this' is in scope in the trailing return type. */
13342 inject_this_parameter (this_type, cp_type_quals (this_type));
13343 }
13344
13345 /* Substitute the return type. */
13346 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13347
13348 if (do_inject)
13349 {
13350 current_class_ptr = save_ccp;
13351 current_class_ref = save_ccr;
13352 }
13353 }
13354 else
13355 /* Substitute the return type. */
13356 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13357
13358 if (return_type == error_mark_node)
13359 return error_mark_node;
13360 /* DR 486 clarifies that creation of a function type with an
13361 invalid return type is a deduction failure. */
13362 if (TREE_CODE (return_type) == ARRAY_TYPE
13363 || TREE_CODE (return_type) == FUNCTION_TYPE)
13364 {
13365 if (complain & tf_error)
13366 {
13367 if (TREE_CODE (return_type) == ARRAY_TYPE)
13368 error ("function returning an array");
13369 else
13370 error ("function returning a function");
13371 }
13372 return error_mark_node;
13373 }
13374 /* And DR 657. */
13375 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13376 return error_mark_node;
13377
13378 if (!late_return_type_p)
13379 {
13380 /* Substitute the argument types. */
13381 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13382 complain, in_decl);
13383 if (arg_types == error_mark_node)
13384 return error_mark_node;
13385 }
13386
13387 /* Construct a new type node and return it. */
13388 if (TREE_CODE (t) == FUNCTION_TYPE)
13389 {
13390 fntype = build_function_type (return_type, arg_types);
13391 fntype = apply_memfn_quals (fntype,
13392 type_memfn_quals (t),
13393 type_memfn_rqual (t));
13394 }
13395 else
13396 {
13397 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13398 /* Don't pick up extra function qualifiers from the basetype. */
13399 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13400 if (! MAYBE_CLASS_TYPE_P (r))
13401 {
13402 /* [temp.deduct]
13403
13404 Type deduction may fail for any of the following
13405 reasons:
13406
13407 -- Attempting to create "pointer to member of T" when T
13408 is not a class type. */
13409 if (complain & tf_error)
13410 error ("creating pointer to member function of non-class type %qT",
13411 r);
13412 return error_mark_node;
13413 }
13414
13415 fntype = build_method_type_directly (r, return_type,
13416 TREE_CHAIN (arg_types));
13417 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13418 }
13419 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13420
13421 if (late_return_type_p)
13422 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13423
13424 return fntype;
13425 }
13426
13427 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13428 ARGS into that specification, and return the substituted
13429 specification. If there is no specification, return NULL_TREE. */
13430
13431 static tree
13432 tsubst_exception_specification (tree fntype,
13433 tree args,
13434 tsubst_flags_t complain,
13435 tree in_decl,
13436 bool defer_ok)
13437 {
13438 tree specs;
13439 tree new_specs;
13440
13441 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13442 new_specs = NULL_TREE;
13443 if (specs && TREE_PURPOSE (specs))
13444 {
13445 /* A noexcept-specifier. */
13446 tree expr = TREE_PURPOSE (specs);
13447 if (TREE_CODE (expr) == INTEGER_CST)
13448 new_specs = expr;
13449 else if (defer_ok)
13450 {
13451 /* Defer instantiation of noexcept-specifiers to avoid
13452 excessive instantiations (c++/49107). */
13453 new_specs = make_node (DEFERRED_NOEXCEPT);
13454 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13455 {
13456 /* We already partially instantiated this member template,
13457 so combine the new args with the old. */
13458 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13459 = DEFERRED_NOEXCEPT_PATTERN (expr);
13460 DEFERRED_NOEXCEPT_ARGS (new_specs)
13461 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13462 }
13463 else
13464 {
13465 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13466 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13467 }
13468 }
13469 else
13470 new_specs = tsubst_copy_and_build
13471 (expr, args, complain, in_decl, /*function_p=*/false,
13472 /*integral_constant_expression_p=*/true);
13473 new_specs = build_noexcept_spec (new_specs, complain);
13474 }
13475 else if (specs)
13476 {
13477 if (! TREE_VALUE (specs))
13478 new_specs = specs;
13479 else
13480 while (specs)
13481 {
13482 tree spec;
13483 int i, len = 1;
13484 tree expanded_specs = NULL_TREE;
13485
13486 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13487 {
13488 /* Expand the pack expansion type. */
13489 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13490 args, complain,
13491 in_decl);
13492
13493 if (expanded_specs == error_mark_node)
13494 return error_mark_node;
13495 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13496 len = TREE_VEC_LENGTH (expanded_specs);
13497 else
13498 {
13499 /* We're substituting into a member template, so
13500 we got a TYPE_PACK_EXPANSION back. Add that
13501 expansion and move on. */
13502 gcc_assert (TREE_CODE (expanded_specs)
13503 == TYPE_PACK_EXPANSION);
13504 new_specs = add_exception_specifier (new_specs,
13505 expanded_specs,
13506 complain);
13507 specs = TREE_CHAIN (specs);
13508 continue;
13509 }
13510 }
13511
13512 for (i = 0; i < len; ++i)
13513 {
13514 if (expanded_specs)
13515 spec = TREE_VEC_ELT (expanded_specs, i);
13516 else
13517 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13518 if (spec == error_mark_node)
13519 return spec;
13520 new_specs = add_exception_specifier (new_specs, spec,
13521 complain);
13522 }
13523
13524 specs = TREE_CHAIN (specs);
13525 }
13526 }
13527 return new_specs;
13528 }
13529
13530 /* Take the tree structure T and replace template parameters used
13531 therein with the argument vector ARGS. IN_DECL is an associated
13532 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13533 Issue error and warning messages under control of COMPLAIN. Note
13534 that we must be relatively non-tolerant of extensions here, in
13535 order to preserve conformance; if we allow substitutions that
13536 should not be allowed, we may allow argument deductions that should
13537 not succeed, and therefore report ambiguous overload situations
13538 where there are none. In theory, we could allow the substitution,
13539 but indicate that it should have failed, and allow our caller to
13540 make sure that the right thing happens, but we don't try to do this
13541 yet.
13542
13543 This function is used for dealing with types, decls and the like;
13544 for expressions, use tsubst_expr or tsubst_copy. */
13545
13546 tree
13547 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13548 {
13549 enum tree_code code;
13550 tree type, r = NULL_TREE;
13551
13552 if (t == NULL_TREE || t == error_mark_node
13553 || t == integer_type_node
13554 || t == void_type_node
13555 || t == char_type_node
13556 || t == unknown_type_node
13557 || TREE_CODE (t) == NAMESPACE_DECL
13558 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13559 return t;
13560
13561 if (DECL_P (t))
13562 return tsubst_decl (t, args, complain);
13563
13564 if (args == NULL_TREE)
13565 return t;
13566
13567 code = TREE_CODE (t);
13568
13569 if (code == IDENTIFIER_NODE)
13570 type = IDENTIFIER_TYPE_VALUE (t);
13571 else
13572 type = TREE_TYPE (t);
13573
13574 gcc_assert (type != unknown_type_node);
13575
13576 /* Reuse typedefs. We need to do this to handle dependent attributes,
13577 such as attribute aligned. */
13578 if (TYPE_P (t)
13579 && typedef_variant_p (t))
13580 {
13581 tree decl = TYPE_NAME (t);
13582
13583 if (alias_template_specialization_p (t))
13584 {
13585 /* DECL represents an alias template and we want to
13586 instantiate it. */
13587 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13588 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13589 r = instantiate_alias_template (tmpl, gen_args, complain);
13590 }
13591 else if (DECL_CLASS_SCOPE_P (decl)
13592 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13593 && uses_template_parms (DECL_CONTEXT (decl)))
13594 {
13595 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13596 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13597 r = retrieve_specialization (tmpl, gen_args, 0);
13598 }
13599 else if (DECL_FUNCTION_SCOPE_P (decl)
13600 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13601 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13602 r = retrieve_local_specialization (decl);
13603 else
13604 /* The typedef is from a non-template context. */
13605 return t;
13606
13607 if (r)
13608 {
13609 r = TREE_TYPE (r);
13610 r = cp_build_qualified_type_real
13611 (r, cp_type_quals (t) | cp_type_quals (r),
13612 complain | tf_ignore_bad_quals);
13613 return r;
13614 }
13615 else
13616 {
13617 /* We don't have an instantiation yet, so drop the typedef. */
13618 int quals = cp_type_quals (t);
13619 t = DECL_ORIGINAL_TYPE (decl);
13620 t = cp_build_qualified_type_real (t, quals,
13621 complain | tf_ignore_bad_quals);
13622 }
13623 }
13624
13625 bool fndecl_type = (complain & tf_fndecl_type);
13626 complain &= ~tf_fndecl_type;
13627
13628 if (type
13629 && code != TYPENAME_TYPE
13630 && code != TEMPLATE_TYPE_PARM
13631 && code != TEMPLATE_PARM_INDEX
13632 && code != IDENTIFIER_NODE
13633 && code != FUNCTION_TYPE
13634 && code != METHOD_TYPE)
13635 type = tsubst (type, args, complain, in_decl);
13636 if (type == error_mark_node)
13637 return error_mark_node;
13638
13639 switch (code)
13640 {
13641 case RECORD_TYPE:
13642 case UNION_TYPE:
13643 case ENUMERAL_TYPE:
13644 return tsubst_aggr_type (t, args, complain, in_decl,
13645 /*entering_scope=*/0);
13646
13647 case ERROR_MARK:
13648 case IDENTIFIER_NODE:
13649 case VOID_TYPE:
13650 case REAL_TYPE:
13651 case COMPLEX_TYPE:
13652 case VECTOR_TYPE:
13653 case BOOLEAN_TYPE:
13654 case NULLPTR_TYPE:
13655 case LANG_TYPE:
13656 return t;
13657
13658 case INTEGER_TYPE:
13659 if (t == integer_type_node)
13660 return t;
13661
13662 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13663 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13664 return t;
13665
13666 {
13667 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13668
13669 max = tsubst_expr (omax, args, complain, in_decl,
13670 /*integral_constant_expression_p=*/false);
13671
13672 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13673 needed. */
13674 if (TREE_CODE (max) == NOP_EXPR
13675 && TREE_SIDE_EFFECTS (omax)
13676 && !TREE_TYPE (max))
13677 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13678
13679 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13680 with TREE_SIDE_EFFECTS that indicates this is not an integral
13681 constant expression. */
13682 if (processing_template_decl
13683 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13684 {
13685 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13686 TREE_SIDE_EFFECTS (max) = 1;
13687 }
13688
13689 return compute_array_index_type (NULL_TREE, max, complain);
13690 }
13691
13692 case TEMPLATE_TYPE_PARM:
13693 case TEMPLATE_TEMPLATE_PARM:
13694 case BOUND_TEMPLATE_TEMPLATE_PARM:
13695 case TEMPLATE_PARM_INDEX:
13696 {
13697 int idx;
13698 int level;
13699 int levels;
13700 tree arg = NULL_TREE;
13701
13702 /* Early in template argument deduction substitution, we don't
13703 want to reduce the level of 'auto', or it will be confused
13704 with a normal template parm in subsequent deduction. */
13705 if (is_auto (t) && (complain & tf_partial))
13706 return t;
13707
13708 r = NULL_TREE;
13709
13710 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13711 template_parm_level_and_index (t, &level, &idx);
13712
13713 levels = TMPL_ARGS_DEPTH (args);
13714 if (level <= levels
13715 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13716 {
13717 arg = TMPL_ARG (args, level, idx);
13718
13719 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13720 {
13721 /* See through ARGUMENT_PACK_SELECT arguments. */
13722 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13723 /* If the selected argument is an expansion E, that most
13724 likely means we were called from
13725 gen_elem_of_pack_expansion_instantiation during the
13726 substituting of pack an argument pack (which Ith
13727 element is a pack expansion, where I is
13728 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13729 In this case, the Ith element resulting from this
13730 substituting is going to be a pack expansion, which
13731 pattern is the pattern of E. Let's return the
13732 pattern of E, and
13733 gen_elem_of_pack_expansion_instantiation will
13734 build the resulting pack expansion from it. */
13735 if (PACK_EXPANSION_P (arg))
13736 {
13737 /* Make sure we aren't throwing away arg info. */
13738 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13739 arg = PACK_EXPANSION_PATTERN (arg);
13740 }
13741 }
13742 }
13743
13744 if (arg == error_mark_node)
13745 return error_mark_node;
13746 else if (arg != NULL_TREE)
13747 {
13748 if (ARGUMENT_PACK_P (arg))
13749 /* If ARG is an argument pack, we don't actually want to
13750 perform a substitution here, because substitutions
13751 for argument packs are only done
13752 element-by-element. We can get to this point when
13753 substituting the type of a non-type template
13754 parameter pack, when that type actually contains
13755 template parameter packs from an outer template, e.g.,
13756
13757 template<typename... Types> struct A {
13758 template<Types... Values> struct B { };
13759 }; */
13760 return t;
13761
13762 if (code == TEMPLATE_TYPE_PARM)
13763 {
13764 int quals;
13765 gcc_assert (TYPE_P (arg));
13766
13767 quals = cp_type_quals (arg) | cp_type_quals (t);
13768
13769 return cp_build_qualified_type_real
13770 (arg, quals, complain | tf_ignore_bad_quals);
13771 }
13772 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13773 {
13774 /* We are processing a type constructed from a
13775 template template parameter. */
13776 tree argvec = tsubst (TYPE_TI_ARGS (t),
13777 args, complain, in_decl);
13778 if (argvec == error_mark_node)
13779 return error_mark_node;
13780
13781 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13782 || TREE_CODE (arg) == TEMPLATE_DECL
13783 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13784
13785 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13786 /* Consider this code:
13787
13788 template <template <class> class Template>
13789 struct Internal {
13790 template <class Arg> using Bind = Template<Arg>;
13791 };
13792
13793 template <template <class> class Template, class Arg>
13794 using Instantiate = Template<Arg>; //#0
13795
13796 template <template <class> class Template,
13797 class Argument>
13798 using Bind =
13799 Instantiate<Internal<Template>::template Bind,
13800 Argument>; //#1
13801
13802 When #1 is parsed, the
13803 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13804 parameter `Template' in #0 matches the
13805 UNBOUND_CLASS_TEMPLATE representing the argument
13806 `Internal<Template>::template Bind'; We then want
13807 to assemble the type `Bind<Argument>' that can't
13808 be fully created right now, because
13809 `Internal<Template>' not being complete, the Bind
13810 template cannot be looked up in that context. So
13811 we need to "store" `Bind<Argument>' for later
13812 when the context of Bind becomes complete. Let's
13813 store that in a TYPENAME_TYPE. */
13814 return make_typename_type (TYPE_CONTEXT (arg),
13815 build_nt (TEMPLATE_ID_EXPR,
13816 TYPE_IDENTIFIER (arg),
13817 argvec),
13818 typename_type,
13819 complain);
13820
13821 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13822 are resolving nested-types in the signature of a
13823 member function templates. Otherwise ARG is a
13824 TEMPLATE_DECL and is the real template to be
13825 instantiated. */
13826 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13827 arg = TYPE_NAME (arg);
13828
13829 r = lookup_template_class (arg,
13830 argvec, in_decl,
13831 DECL_CONTEXT (arg),
13832 /*entering_scope=*/0,
13833 complain);
13834 return cp_build_qualified_type_real
13835 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13836 }
13837 else if (code == TEMPLATE_TEMPLATE_PARM)
13838 return arg;
13839 else
13840 /* TEMPLATE_PARM_INDEX. */
13841 return convert_from_reference (unshare_expr (arg));
13842 }
13843
13844 if (level == 1)
13845 /* This can happen during the attempted tsubst'ing in
13846 unify. This means that we don't yet have any information
13847 about the template parameter in question. */
13848 return t;
13849
13850 /* If we get here, we must have been looking at a parm for a
13851 more deeply nested template. Make a new version of this
13852 template parameter, but with a lower level. */
13853 switch (code)
13854 {
13855 case TEMPLATE_TYPE_PARM:
13856 case TEMPLATE_TEMPLATE_PARM:
13857 case BOUND_TEMPLATE_TEMPLATE_PARM:
13858 if (cp_type_quals (t))
13859 {
13860 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13861 r = cp_build_qualified_type_real
13862 (r, cp_type_quals (t),
13863 complain | (code == TEMPLATE_TYPE_PARM
13864 ? tf_ignore_bad_quals : 0));
13865 }
13866 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13867 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13868 && (r = (TEMPLATE_PARM_DESCENDANTS
13869 (TEMPLATE_TYPE_PARM_INDEX (t))))
13870 && (r = TREE_TYPE (r))
13871 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13872 /* Break infinite recursion when substituting the constraints
13873 of a constrained placeholder. */;
13874 else
13875 {
13876 r = copy_type (t);
13877 TEMPLATE_TYPE_PARM_INDEX (r)
13878 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13879 r, levels, args, complain);
13880 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13881 TYPE_MAIN_VARIANT (r) = r;
13882 TYPE_POINTER_TO (r) = NULL_TREE;
13883 TYPE_REFERENCE_TO (r) = NULL_TREE;
13884
13885 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13886 {
13887 /* Propagate constraints on placeholders. */
13888 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13889 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13890 = tsubst_constraint (constr, args, complain, in_decl);
13891 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13892 {
13893 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13894 pl = tsubst (pl, args, complain, in_decl);
13895 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13896 }
13897 }
13898
13899 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13900 /* We have reduced the level of the template
13901 template parameter, but not the levels of its
13902 template parameters, so canonical_type_parameter
13903 will not be able to find the canonical template
13904 template parameter for this level. Thus, we
13905 require structural equality checking to compare
13906 TEMPLATE_TEMPLATE_PARMs. */
13907 SET_TYPE_STRUCTURAL_EQUALITY (r);
13908 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13909 SET_TYPE_STRUCTURAL_EQUALITY (r);
13910 else
13911 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13912
13913 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13914 {
13915 tree tinfo = TYPE_TEMPLATE_INFO (t);
13916 /* We might need to substitute into the types of non-type
13917 template parameters. */
13918 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13919 complain, in_decl);
13920 if (tmpl == error_mark_node)
13921 return error_mark_node;
13922 tree argvec = tsubst (TI_ARGS (tinfo), args,
13923 complain, in_decl);
13924 if (argvec == error_mark_node)
13925 return error_mark_node;
13926
13927 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13928 = build_template_info (tmpl, argvec);
13929 }
13930 }
13931 break;
13932
13933 case TEMPLATE_PARM_INDEX:
13934 /* OK, now substitute the type of the non-type parameter. We
13935 couldn't do it earlier because it might be an auto parameter,
13936 and we wouldn't need to if we had an argument. */
13937 type = tsubst (type, args, complain, in_decl);
13938 if (type == error_mark_node)
13939 return error_mark_node;
13940 r = reduce_template_parm_level (t, type, levels, args, complain);
13941 break;
13942
13943 default:
13944 gcc_unreachable ();
13945 }
13946
13947 return r;
13948 }
13949
13950 case TREE_LIST:
13951 {
13952 tree purpose, value, chain;
13953
13954 if (t == void_list_node)
13955 return t;
13956
13957 purpose = TREE_PURPOSE (t);
13958 if (purpose)
13959 {
13960 purpose = tsubst (purpose, args, complain, in_decl);
13961 if (purpose == error_mark_node)
13962 return error_mark_node;
13963 }
13964 value = TREE_VALUE (t);
13965 if (value)
13966 {
13967 value = tsubst (value, args, complain, in_decl);
13968 if (value == error_mark_node)
13969 return error_mark_node;
13970 }
13971 chain = TREE_CHAIN (t);
13972 if (chain && chain != void_type_node)
13973 {
13974 chain = tsubst (chain, args, complain, in_decl);
13975 if (chain == error_mark_node)
13976 return error_mark_node;
13977 }
13978 if (purpose == TREE_PURPOSE (t)
13979 && value == TREE_VALUE (t)
13980 && chain == TREE_CHAIN (t))
13981 return t;
13982 return hash_tree_cons (purpose, value, chain);
13983 }
13984
13985 case TREE_BINFO:
13986 /* We should never be tsubsting a binfo. */
13987 gcc_unreachable ();
13988
13989 case TREE_VEC:
13990 /* A vector of template arguments. */
13991 gcc_assert (!type);
13992 return tsubst_template_args (t, args, complain, in_decl);
13993
13994 case POINTER_TYPE:
13995 case REFERENCE_TYPE:
13996 {
13997 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13998 return t;
13999
14000 /* [temp.deduct]
14001
14002 Type deduction may fail for any of the following
14003 reasons:
14004
14005 -- Attempting to create a pointer to reference type.
14006 -- Attempting to create a reference to a reference type or
14007 a reference to void.
14008
14009 Core issue 106 says that creating a reference to a reference
14010 during instantiation is no longer a cause for failure. We
14011 only enforce this check in strict C++98 mode. */
14012 if ((TREE_CODE (type) == REFERENCE_TYPE
14013 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14014 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14015 {
14016 static location_t last_loc;
14017
14018 /* We keep track of the last time we issued this error
14019 message to avoid spewing a ton of messages during a
14020 single bad template instantiation. */
14021 if (complain & tf_error
14022 && last_loc != input_location)
14023 {
14024 if (VOID_TYPE_P (type))
14025 error ("forming reference to void");
14026 else if (code == POINTER_TYPE)
14027 error ("forming pointer to reference type %qT", type);
14028 else
14029 error ("forming reference to reference type %qT", type);
14030 last_loc = input_location;
14031 }
14032
14033 return error_mark_node;
14034 }
14035 else if (TREE_CODE (type) == FUNCTION_TYPE
14036 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14037 || type_memfn_rqual (type) != REF_QUAL_NONE))
14038 {
14039 if (complain & tf_error)
14040 {
14041 if (code == POINTER_TYPE)
14042 error ("forming pointer to qualified function type %qT",
14043 type);
14044 else
14045 error ("forming reference to qualified function type %qT",
14046 type);
14047 }
14048 return error_mark_node;
14049 }
14050 else if (code == POINTER_TYPE)
14051 {
14052 r = build_pointer_type (type);
14053 if (TREE_CODE (type) == METHOD_TYPE)
14054 r = build_ptrmemfunc_type (r);
14055 }
14056 else if (TREE_CODE (type) == REFERENCE_TYPE)
14057 /* In C++0x, during template argument substitution, when there is an
14058 attempt to create a reference to a reference type, reference
14059 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14060
14061 "If a template-argument for a template-parameter T names a type
14062 that is a reference to a type A, an attempt to create the type
14063 'lvalue reference to cv T' creates the type 'lvalue reference to
14064 A,' while an attempt to create the type type rvalue reference to
14065 cv T' creates the type T"
14066 */
14067 r = cp_build_reference_type
14068 (TREE_TYPE (type),
14069 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14070 else
14071 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14072 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14073
14074 if (r != error_mark_node)
14075 /* Will this ever be needed for TYPE_..._TO values? */
14076 layout_type (r);
14077
14078 return r;
14079 }
14080 case OFFSET_TYPE:
14081 {
14082 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14083 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14084 {
14085 /* [temp.deduct]
14086
14087 Type deduction may fail for any of the following
14088 reasons:
14089
14090 -- Attempting to create "pointer to member of T" when T
14091 is not a class type. */
14092 if (complain & tf_error)
14093 error ("creating pointer to member of non-class type %qT", r);
14094 return error_mark_node;
14095 }
14096 if (TREE_CODE (type) == REFERENCE_TYPE)
14097 {
14098 if (complain & tf_error)
14099 error ("creating pointer to member reference type %qT", type);
14100 return error_mark_node;
14101 }
14102 if (VOID_TYPE_P (type))
14103 {
14104 if (complain & tf_error)
14105 error ("creating pointer to member of type void");
14106 return error_mark_node;
14107 }
14108 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14109 if (TREE_CODE (type) == FUNCTION_TYPE)
14110 {
14111 /* The type of the implicit object parameter gets its
14112 cv-qualifiers from the FUNCTION_TYPE. */
14113 tree memptr;
14114 tree method_type
14115 = build_memfn_type (type, r, type_memfn_quals (type),
14116 type_memfn_rqual (type));
14117 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14118 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14119 complain);
14120 }
14121 else
14122 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14123 cp_type_quals (t),
14124 complain);
14125 }
14126 case FUNCTION_TYPE:
14127 case METHOD_TYPE:
14128 {
14129 tree fntype;
14130 tree specs;
14131 fntype = tsubst_function_type (t, args, complain, in_decl);
14132 if (fntype == error_mark_node)
14133 return error_mark_node;
14134
14135 /* Substitute the exception specification. */
14136 specs = tsubst_exception_specification (t, args, complain, in_decl,
14137 /*defer_ok*/fndecl_type);
14138 if (specs == error_mark_node)
14139 return error_mark_node;
14140 if (specs)
14141 fntype = build_exception_variant (fntype, specs);
14142 return fntype;
14143 }
14144 case ARRAY_TYPE:
14145 {
14146 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14147 if (domain == error_mark_node)
14148 return error_mark_node;
14149
14150 /* As an optimization, we avoid regenerating the array type if
14151 it will obviously be the same as T. */
14152 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14153 return t;
14154
14155 /* These checks should match the ones in create_array_type_for_decl.
14156
14157 [temp.deduct]
14158
14159 The deduction may fail for any of the following reasons:
14160
14161 -- Attempting to create an array with an element type that
14162 is void, a function type, or a reference type, or [DR337]
14163 an abstract class type. */
14164 if (VOID_TYPE_P (type)
14165 || TREE_CODE (type) == FUNCTION_TYPE
14166 || (TREE_CODE (type) == ARRAY_TYPE
14167 && TYPE_DOMAIN (type) == NULL_TREE)
14168 || TREE_CODE (type) == REFERENCE_TYPE)
14169 {
14170 if (complain & tf_error)
14171 error ("creating array of %qT", type);
14172 return error_mark_node;
14173 }
14174
14175 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14176 return error_mark_node;
14177
14178 r = build_cplus_array_type (type, domain);
14179
14180 if (TYPE_USER_ALIGN (t))
14181 {
14182 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14183 TYPE_USER_ALIGN (r) = 1;
14184 }
14185
14186 return r;
14187 }
14188
14189 case TYPENAME_TYPE:
14190 {
14191 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14192 in_decl, /*entering_scope=*/1);
14193 if (ctx == error_mark_node)
14194 return error_mark_node;
14195
14196 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14197 complain, in_decl);
14198 if (f == error_mark_node)
14199 return error_mark_node;
14200
14201 if (!MAYBE_CLASS_TYPE_P (ctx))
14202 {
14203 if (complain & tf_error)
14204 error ("%qT is not a class, struct, or union type", ctx);
14205 return error_mark_node;
14206 }
14207 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14208 {
14209 /* Normally, make_typename_type does not require that the CTX
14210 have complete type in order to allow things like:
14211
14212 template <class T> struct S { typename S<T>::X Y; };
14213
14214 But, such constructs have already been resolved by this
14215 point, so here CTX really should have complete type, unless
14216 it's a partial instantiation. */
14217 ctx = complete_type (ctx);
14218 if (!COMPLETE_TYPE_P (ctx))
14219 {
14220 if (complain & tf_error)
14221 cxx_incomplete_type_error (NULL_TREE, ctx);
14222 return error_mark_node;
14223 }
14224 }
14225
14226 f = make_typename_type (ctx, f, typename_type,
14227 complain | tf_keep_type_decl);
14228 if (f == error_mark_node)
14229 return f;
14230 if (TREE_CODE (f) == TYPE_DECL)
14231 {
14232 complain |= tf_ignore_bad_quals;
14233 f = TREE_TYPE (f);
14234 }
14235
14236 if (TREE_CODE (f) != TYPENAME_TYPE)
14237 {
14238 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14239 {
14240 if (complain & tf_error)
14241 error ("%qT resolves to %qT, which is not an enumeration type",
14242 t, f);
14243 else
14244 return error_mark_node;
14245 }
14246 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14247 {
14248 if (complain & tf_error)
14249 error ("%qT resolves to %qT, which is is not a class type",
14250 t, f);
14251 else
14252 return error_mark_node;
14253 }
14254 }
14255
14256 return cp_build_qualified_type_real
14257 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14258 }
14259
14260 case UNBOUND_CLASS_TEMPLATE:
14261 {
14262 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14263 in_decl, /*entering_scope=*/1);
14264 tree name = TYPE_IDENTIFIER (t);
14265 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14266
14267 if (ctx == error_mark_node || name == error_mark_node)
14268 return error_mark_node;
14269
14270 if (parm_list)
14271 parm_list = tsubst_template_parms (parm_list, args, complain);
14272 return make_unbound_class_template (ctx, name, parm_list, complain);
14273 }
14274
14275 case TYPEOF_TYPE:
14276 {
14277 tree type;
14278
14279 ++cp_unevaluated_operand;
14280 ++c_inhibit_evaluation_warnings;
14281
14282 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14283 complain, in_decl,
14284 /*integral_constant_expression_p=*/false);
14285
14286 --cp_unevaluated_operand;
14287 --c_inhibit_evaluation_warnings;
14288
14289 type = finish_typeof (type);
14290 return cp_build_qualified_type_real (type,
14291 cp_type_quals (t)
14292 | cp_type_quals (type),
14293 complain);
14294 }
14295
14296 case DECLTYPE_TYPE:
14297 {
14298 tree type;
14299
14300 ++cp_unevaluated_operand;
14301 ++c_inhibit_evaluation_warnings;
14302
14303 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14304 complain|tf_decltype, in_decl,
14305 /*function_p*/false,
14306 /*integral_constant_expression*/false);
14307
14308 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14309 {
14310 if (type == NULL_TREE)
14311 {
14312 if (complain & tf_error)
14313 error ("empty initializer in lambda init-capture");
14314 type = error_mark_node;
14315 }
14316 else if (TREE_CODE (type) == TREE_LIST)
14317 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14318 }
14319
14320 --cp_unevaluated_operand;
14321 --c_inhibit_evaluation_warnings;
14322
14323 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14324 type = lambda_capture_field_type (type,
14325 DECLTYPE_FOR_INIT_CAPTURE (t),
14326 DECLTYPE_FOR_REF_CAPTURE (t));
14327 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14328 type = lambda_proxy_type (type);
14329 else
14330 {
14331 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14332 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14333 && EXPR_P (type))
14334 /* In a template ~id could be either a complement expression
14335 or an unqualified-id naming a destructor; if instantiating
14336 it produces an expression, it's not an id-expression or
14337 member access. */
14338 id = false;
14339 type = finish_decltype_type (type, id, complain);
14340 }
14341 return cp_build_qualified_type_real (type,
14342 cp_type_quals (t)
14343 | cp_type_quals (type),
14344 complain | tf_ignore_bad_quals);
14345 }
14346
14347 case UNDERLYING_TYPE:
14348 {
14349 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14350 complain, in_decl);
14351 return finish_underlying_type (type);
14352 }
14353
14354 case TYPE_ARGUMENT_PACK:
14355 case NONTYPE_ARGUMENT_PACK:
14356 {
14357 tree r;
14358
14359 if (code == NONTYPE_ARGUMENT_PACK)
14360 r = make_node (code);
14361 else
14362 r = cxx_make_type (code);
14363
14364 tree pack_args = ARGUMENT_PACK_ARGS (t);
14365 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14366 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14367
14368 return r;
14369 }
14370
14371 case VOID_CST:
14372 case INTEGER_CST:
14373 case REAL_CST:
14374 case STRING_CST:
14375 case PLUS_EXPR:
14376 case MINUS_EXPR:
14377 case NEGATE_EXPR:
14378 case NOP_EXPR:
14379 case INDIRECT_REF:
14380 case ADDR_EXPR:
14381 case CALL_EXPR:
14382 case ARRAY_REF:
14383 case SCOPE_REF:
14384 /* We should use one of the expression tsubsts for these codes. */
14385 gcc_unreachable ();
14386
14387 default:
14388 sorry ("use of %qs in template", get_tree_code_name (code));
14389 return error_mark_node;
14390 }
14391 }
14392
14393 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14394 expression on the left-hand side of the "." or "->" operator. We
14395 only do the lookup if we had a dependent BASELINK. Otherwise we
14396 adjust it onto the instantiated heirarchy. */
14397
14398 static tree
14399 tsubst_baselink (tree baselink, tree object_type,
14400 tree args, tsubst_flags_t complain, tree in_decl)
14401 {
14402 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14403 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14404 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14405
14406 tree optype = BASELINK_OPTYPE (baselink);
14407 optype = tsubst (optype, args, complain, in_decl);
14408
14409 tree template_args = NULL_TREE;
14410 bool template_id_p = false;
14411 tree fns = BASELINK_FUNCTIONS (baselink);
14412 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14413 {
14414 template_id_p = true;
14415 template_args = TREE_OPERAND (fns, 1);
14416 fns = TREE_OPERAND (fns, 0);
14417 if (template_args)
14418 template_args = tsubst_template_args (template_args, args,
14419 complain, in_decl);
14420 }
14421
14422 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14423 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14424 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14425
14426 if (dependent_p)
14427 {
14428 tree name = OVL_NAME (fns);
14429 if (IDENTIFIER_CONV_OP_P (name))
14430 name = make_conv_op_name (optype);
14431
14432 if (name == complete_dtor_identifier)
14433 /* Treat as-if non-dependent below. */
14434 dependent_p = false;
14435
14436 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14437 if (!baselink)
14438 {
14439 if ((complain & tf_error)
14440 && constructor_name_p (name, qualifying_scope))
14441 error ("cannot call constructor %<%T::%D%> directly",
14442 qualifying_scope, name);
14443 return error_mark_node;
14444 }
14445
14446 if (BASELINK_P (baselink))
14447 fns = BASELINK_FUNCTIONS (baselink);
14448 }
14449 else
14450 {
14451 gcc_assert (optype == BASELINK_OPTYPE (baselink));
14452 /* We're going to overwrite pieces below, make a duplicate. */
14453 baselink = copy_node (baselink);
14454 }
14455
14456 /* If lookup found a single function, mark it as used at this point.
14457 (If lookup found multiple functions the one selected later by
14458 overload resolution will be marked as used at that point.) */
14459 if (!template_id_p && !really_overloaded_fn (fns)
14460 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14461 return error_mark_node;
14462
14463 if (BASELINK_P (baselink))
14464 {
14465 /* Add back the template arguments, if present. */
14466 if (template_id_p)
14467 BASELINK_FUNCTIONS (baselink)
14468 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14469
14470 /* Update the conversion operator type. */
14471 BASELINK_OPTYPE (baselink) = optype;
14472 }
14473
14474 if (!object_type)
14475 object_type = current_class_type;
14476
14477 if (qualified_p || !dependent_p)
14478 {
14479 baselink = adjust_result_of_qualified_name_lookup (baselink,
14480 qualifying_scope,
14481 object_type);
14482 if (!qualified_p)
14483 /* We need to call adjust_result_of_qualified_name_lookup in case the
14484 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14485 so that we still get virtual function binding. */
14486 BASELINK_QUALIFIED_P (baselink) = false;
14487 }
14488
14489 return baselink;
14490 }
14491
14492 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14493 true if the qualified-id will be a postfix-expression in-and-of
14494 itself; false if more of the postfix-expression follows the
14495 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14496 of "&". */
14497
14498 static tree
14499 tsubst_qualified_id (tree qualified_id, tree args,
14500 tsubst_flags_t complain, tree in_decl,
14501 bool done, bool address_p)
14502 {
14503 tree expr;
14504 tree scope;
14505 tree name;
14506 bool is_template;
14507 tree template_args;
14508 location_t loc = UNKNOWN_LOCATION;
14509
14510 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14511
14512 /* Figure out what name to look up. */
14513 name = TREE_OPERAND (qualified_id, 1);
14514 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14515 {
14516 is_template = true;
14517 loc = EXPR_LOCATION (name);
14518 template_args = TREE_OPERAND (name, 1);
14519 if (template_args)
14520 template_args = tsubst_template_args (template_args, args,
14521 complain, in_decl);
14522 if (template_args == error_mark_node)
14523 return error_mark_node;
14524 name = TREE_OPERAND (name, 0);
14525 }
14526 else
14527 {
14528 is_template = false;
14529 template_args = NULL_TREE;
14530 }
14531
14532 /* Substitute into the qualifying scope. When there are no ARGS, we
14533 are just trying to simplify a non-dependent expression. In that
14534 case the qualifying scope may be dependent, and, in any case,
14535 substituting will not help. */
14536 scope = TREE_OPERAND (qualified_id, 0);
14537 if (args)
14538 {
14539 scope = tsubst (scope, args, complain, in_decl);
14540 expr = tsubst_copy (name, args, complain, in_decl);
14541 }
14542 else
14543 expr = name;
14544
14545 if (dependent_scope_p (scope))
14546 {
14547 if (is_template)
14548 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14549 tree r = build_qualified_name (NULL_TREE, scope, expr,
14550 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14551 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14552 return r;
14553 }
14554
14555 if (!BASELINK_P (name) && !DECL_P (expr))
14556 {
14557 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14558 {
14559 /* A BIT_NOT_EXPR is used to represent a destructor. */
14560 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14561 {
14562 error ("qualifying type %qT does not match destructor name ~%qT",
14563 scope, TREE_OPERAND (expr, 0));
14564 expr = error_mark_node;
14565 }
14566 else
14567 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14568 /*is_type_p=*/0, false);
14569 }
14570 else
14571 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14572 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14573 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14574 {
14575 if (complain & tf_error)
14576 {
14577 error ("dependent-name %qE is parsed as a non-type, but "
14578 "instantiation yields a type", qualified_id);
14579 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14580 }
14581 return error_mark_node;
14582 }
14583 }
14584
14585 if (DECL_P (expr))
14586 {
14587 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14588 scope);
14589 /* Remember that there was a reference to this entity. */
14590 if (!mark_used (expr, complain) && !(complain & tf_error))
14591 return error_mark_node;
14592 }
14593
14594 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14595 {
14596 if (complain & tf_error)
14597 qualified_name_lookup_error (scope,
14598 TREE_OPERAND (qualified_id, 1),
14599 expr, input_location);
14600 return error_mark_node;
14601 }
14602
14603 if (is_template)
14604 {
14605 if (variable_template_p (expr))
14606 expr = lookup_and_finish_template_variable (expr, template_args,
14607 complain);
14608 else
14609 expr = lookup_template_function (expr, template_args);
14610 }
14611
14612 if (expr == error_mark_node && complain & tf_error)
14613 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14614 expr, input_location);
14615 else if (TYPE_P (scope))
14616 {
14617 expr = (adjust_result_of_qualified_name_lookup
14618 (expr, scope, current_nonlambda_class_type ()));
14619 expr = (finish_qualified_id_expr
14620 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14621 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14622 /*template_arg_p=*/false, complain));
14623 }
14624
14625 /* Expressions do not generally have reference type. */
14626 if (TREE_CODE (expr) != SCOPE_REF
14627 /* However, if we're about to form a pointer-to-member, we just
14628 want the referenced member referenced. */
14629 && TREE_CODE (expr) != OFFSET_REF)
14630 expr = convert_from_reference (expr);
14631
14632 if (REF_PARENTHESIZED_P (qualified_id))
14633 expr = force_paren_expr (expr);
14634
14635 return expr;
14636 }
14637
14638 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14639 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14640 for tsubst. */
14641
14642 static tree
14643 tsubst_init (tree init, tree decl, tree args,
14644 tsubst_flags_t complain, tree in_decl)
14645 {
14646 if (!init)
14647 return NULL_TREE;
14648
14649 init = tsubst_expr (init, args, complain, in_decl, false);
14650
14651 if (!init && TREE_TYPE (decl) != error_mark_node)
14652 {
14653 /* If we had an initializer but it
14654 instantiated to nothing,
14655 value-initialize the object. This will
14656 only occur when the initializer was a
14657 pack expansion where the parameter packs
14658 used in that expansion were of length
14659 zero. */
14660 init = build_value_init (TREE_TYPE (decl),
14661 complain);
14662 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14663 init = get_target_expr_sfinae (init, complain);
14664 if (TREE_CODE (init) == TARGET_EXPR)
14665 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14666 }
14667
14668 return init;
14669 }
14670
14671 /* Like tsubst, but deals with expressions. This function just replaces
14672 template parms; to finish processing the resultant expression, use
14673 tsubst_copy_and_build or tsubst_expr. */
14674
14675 static tree
14676 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14677 {
14678 enum tree_code code;
14679 tree r;
14680
14681 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14682 return t;
14683
14684 code = TREE_CODE (t);
14685
14686 switch (code)
14687 {
14688 case PARM_DECL:
14689 r = retrieve_local_specialization (t);
14690
14691 if (r == NULL_TREE)
14692 {
14693 /* We get here for a use of 'this' in an NSDMI. */
14694 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14695 return current_class_ptr;
14696
14697 /* This can happen for a parameter name used later in a function
14698 declaration (such as in a late-specified return type). Just
14699 make a dummy decl, since it's only used for its type. */
14700 gcc_assert (cp_unevaluated_operand != 0);
14701 r = tsubst_decl (t, args, complain);
14702 /* Give it the template pattern as its context; its true context
14703 hasn't been instantiated yet and this is good enough for
14704 mangling. */
14705 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14706 }
14707
14708 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14709 r = ARGUMENT_PACK_SELECT_ARG (r);
14710 if (!mark_used (r, complain) && !(complain & tf_error))
14711 return error_mark_node;
14712 return r;
14713
14714 case CONST_DECL:
14715 {
14716 tree enum_type;
14717 tree v;
14718
14719 if (DECL_TEMPLATE_PARM_P (t))
14720 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14721 /* There is no need to substitute into namespace-scope
14722 enumerators. */
14723 if (DECL_NAMESPACE_SCOPE_P (t))
14724 return t;
14725 /* If ARGS is NULL, then T is known to be non-dependent. */
14726 if (args == NULL_TREE)
14727 return scalar_constant_value (t);
14728
14729 /* Unfortunately, we cannot just call lookup_name here.
14730 Consider:
14731
14732 template <int I> int f() {
14733 enum E { a = I };
14734 struct S { void g() { E e = a; } };
14735 };
14736
14737 When we instantiate f<7>::S::g(), say, lookup_name is not
14738 clever enough to find f<7>::a. */
14739 enum_type
14740 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14741 /*entering_scope=*/0);
14742
14743 for (v = TYPE_VALUES (enum_type);
14744 v != NULL_TREE;
14745 v = TREE_CHAIN (v))
14746 if (TREE_PURPOSE (v) == DECL_NAME (t))
14747 return TREE_VALUE (v);
14748
14749 /* We didn't find the name. That should never happen; if
14750 name-lookup found it during preliminary parsing, we
14751 should find it again here during instantiation. */
14752 gcc_unreachable ();
14753 }
14754 return t;
14755
14756 case FIELD_DECL:
14757 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14758 {
14759 /* Check for a local specialization set up by
14760 tsubst_pack_expansion. */
14761 if (tree r = retrieve_local_specialization (t))
14762 {
14763 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14764 r = ARGUMENT_PACK_SELECT_ARG (r);
14765 return r;
14766 }
14767
14768 /* When retrieving a capture pack from a generic lambda, remove the
14769 lambda call op's own template argument list from ARGS. Only the
14770 template arguments active for the closure type should be used to
14771 retrieve the pack specialization. */
14772 if (LAMBDA_FUNCTION_P (current_function_decl)
14773 && (template_class_depth (DECL_CONTEXT (t))
14774 != TMPL_ARGS_DEPTH (args)))
14775 args = strip_innermost_template_args (args, 1);
14776
14777 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14778 tsubst_decl put in the hash table. */
14779 return retrieve_specialization (t, args, 0);
14780 }
14781
14782 if (DECL_CONTEXT (t))
14783 {
14784 tree ctx;
14785
14786 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14787 /*entering_scope=*/1);
14788 if (ctx != DECL_CONTEXT (t))
14789 {
14790 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14791 if (!r)
14792 {
14793 if (complain & tf_error)
14794 error ("using invalid field %qD", t);
14795 return error_mark_node;
14796 }
14797 return r;
14798 }
14799 }
14800
14801 return t;
14802
14803 case VAR_DECL:
14804 case FUNCTION_DECL:
14805 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14806 r = tsubst (t, args, complain, in_decl);
14807 else if (local_variable_p (t)
14808 && uses_template_parms (DECL_CONTEXT (t)))
14809 {
14810 r = retrieve_local_specialization (t);
14811 if (r == NULL_TREE)
14812 {
14813 /* First try name lookup to find the instantiation. */
14814 r = lookup_name (DECL_NAME (t));
14815 if (r && !is_capture_proxy (r))
14816 {
14817 /* Make sure that the one we found is the one we want. */
14818 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14819 if (ctx != DECL_CONTEXT (r))
14820 r = NULL_TREE;
14821 }
14822
14823 if (r)
14824 /* OK */;
14825 else
14826 {
14827 /* This can happen for a variable used in a
14828 late-specified return type of a local lambda, or for a
14829 local static or constant. Building a new VAR_DECL
14830 should be OK in all those cases. */
14831 r = tsubst_decl (t, args, complain);
14832 if (local_specializations)
14833 /* Avoid infinite recursion (79640). */
14834 register_local_specialization (r, t);
14835 if (decl_maybe_constant_var_p (r))
14836 {
14837 /* We can't call cp_finish_decl, so handle the
14838 initializer by hand. */
14839 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14840 complain, in_decl);
14841 if (!processing_template_decl)
14842 init = maybe_constant_init (init);
14843 if (processing_template_decl
14844 ? potential_constant_expression (init)
14845 : reduced_constant_expression_p (init))
14846 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14847 = TREE_CONSTANT (r) = true;
14848 DECL_INITIAL (r) = init;
14849 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14850 TREE_TYPE (r)
14851 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14852 complain, adc_variable_type);
14853 }
14854 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14855 || decl_constant_var_p (r)
14856 || errorcount || sorrycount);
14857 if (!processing_template_decl
14858 && !TREE_STATIC (r))
14859 r = process_outer_var_ref (r, complain);
14860 }
14861 /* Remember this for subsequent uses. */
14862 if (local_specializations)
14863 register_local_specialization (r, t);
14864 }
14865 }
14866 else
14867 r = t;
14868 if (!mark_used (r, complain))
14869 return error_mark_node;
14870 return r;
14871
14872 case NAMESPACE_DECL:
14873 return t;
14874
14875 case OVERLOAD:
14876 /* An OVERLOAD will always be a non-dependent overload set; an
14877 overload set from function scope will just be represented with an
14878 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14879 gcc_assert (!uses_template_parms (t));
14880 /* We must have marked any lookups as persistent. */
14881 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14882 return t;
14883
14884 case BASELINK:
14885 return tsubst_baselink (t, current_nonlambda_class_type (),
14886 args, complain, in_decl);
14887
14888 case TEMPLATE_DECL:
14889 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14890 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14891 args, complain, in_decl);
14892 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14893 return tsubst (t, args, complain, in_decl);
14894 else if (DECL_CLASS_SCOPE_P (t)
14895 && uses_template_parms (DECL_CONTEXT (t)))
14896 {
14897 /* Template template argument like the following example need
14898 special treatment:
14899
14900 template <template <class> class TT> struct C {};
14901 template <class T> struct D {
14902 template <class U> struct E {};
14903 C<E> c; // #1
14904 };
14905 D<int> d; // #2
14906
14907 We are processing the template argument `E' in #1 for
14908 the template instantiation #2. Originally, `E' is a
14909 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14910 have to substitute this with one having context `D<int>'. */
14911
14912 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14913 if (dependent_scope_p (context))
14914 {
14915 /* When rewriting a constructor into a deduction guide, a
14916 non-dependent name can become dependent, so memtmpl<args>
14917 becomes context::template memtmpl<args>. */
14918 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14919 return build_qualified_name (type, context, DECL_NAME (t),
14920 /*template*/true);
14921 }
14922 return lookup_field (context, DECL_NAME(t), 0, false);
14923 }
14924 else
14925 /* Ordinary template template argument. */
14926 return t;
14927
14928 case NON_LVALUE_EXPR:
14929 case VIEW_CONVERT_EXPR:
14930 {
14931 /* Handle location wrappers by substituting the wrapped node
14932 first, *then* reusing the resulting type. Doing the type
14933 first ensures that we handle template parameters and
14934 parameter pack expansions. */
14935 gcc_assert (location_wrapper_p (t));
14936 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14937 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
14938 }
14939
14940 case CAST_EXPR:
14941 case REINTERPRET_CAST_EXPR:
14942 case CONST_CAST_EXPR:
14943 case STATIC_CAST_EXPR:
14944 case DYNAMIC_CAST_EXPR:
14945 case IMPLICIT_CONV_EXPR:
14946 case CONVERT_EXPR:
14947 case NOP_EXPR:
14948 {
14949 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14950 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14951 return build1 (code, type, op0);
14952 }
14953
14954 case SIZEOF_EXPR:
14955 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14956 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14957 {
14958 tree expanded, op = TREE_OPERAND (t, 0);
14959 int len = 0;
14960
14961 if (SIZEOF_EXPR_TYPE_P (t))
14962 op = TREE_TYPE (op);
14963
14964 ++cp_unevaluated_operand;
14965 ++c_inhibit_evaluation_warnings;
14966 /* We only want to compute the number of arguments. */
14967 if (PACK_EXPANSION_P (op))
14968 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14969 else
14970 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14971 args, complain, in_decl);
14972 --cp_unevaluated_operand;
14973 --c_inhibit_evaluation_warnings;
14974
14975 if (TREE_CODE (expanded) == TREE_VEC)
14976 {
14977 len = TREE_VEC_LENGTH (expanded);
14978 /* Set TREE_USED for the benefit of -Wunused. */
14979 for (int i = 0; i < len; i++)
14980 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14981 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14982 }
14983
14984 if (expanded == error_mark_node)
14985 return error_mark_node;
14986 else if (PACK_EXPANSION_P (expanded)
14987 || (TREE_CODE (expanded) == TREE_VEC
14988 && pack_expansion_args_count (expanded)))
14989
14990 {
14991 if (PACK_EXPANSION_P (expanded))
14992 /* OK. */;
14993 else if (TREE_VEC_LENGTH (expanded) == 1)
14994 expanded = TREE_VEC_ELT (expanded, 0);
14995 else
14996 expanded = make_argument_pack (expanded);
14997
14998 if (TYPE_P (expanded))
14999 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15000 complain & tf_error);
15001 else
15002 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15003 complain & tf_error);
15004 }
15005 else
15006 return build_int_cst (size_type_node, len);
15007 }
15008 if (SIZEOF_EXPR_TYPE_P (t))
15009 {
15010 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15011 args, complain, in_decl);
15012 r = build1 (NOP_EXPR, r, error_mark_node);
15013 r = build1 (SIZEOF_EXPR,
15014 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15015 SIZEOF_EXPR_TYPE_P (r) = 1;
15016 return r;
15017 }
15018 /* Fall through */
15019
15020 case INDIRECT_REF:
15021 case NEGATE_EXPR:
15022 case TRUTH_NOT_EXPR:
15023 case BIT_NOT_EXPR:
15024 case ADDR_EXPR:
15025 case UNARY_PLUS_EXPR: /* Unary + */
15026 case ALIGNOF_EXPR:
15027 case AT_ENCODE_EXPR:
15028 case ARROW_EXPR:
15029 case THROW_EXPR:
15030 case TYPEID_EXPR:
15031 case REALPART_EXPR:
15032 case IMAGPART_EXPR:
15033 case PAREN_EXPR:
15034 {
15035 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15036 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15037 return build1 (code, type, op0);
15038 }
15039
15040 case COMPONENT_REF:
15041 {
15042 tree object;
15043 tree name;
15044
15045 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15046 name = TREE_OPERAND (t, 1);
15047 if (TREE_CODE (name) == BIT_NOT_EXPR)
15048 {
15049 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15050 complain, in_decl);
15051 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15052 }
15053 else if (TREE_CODE (name) == SCOPE_REF
15054 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15055 {
15056 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15057 complain, in_decl);
15058 name = TREE_OPERAND (name, 1);
15059 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15060 complain, in_decl);
15061 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15062 name = build_qualified_name (/*type=*/NULL_TREE,
15063 base, name,
15064 /*template_p=*/false);
15065 }
15066 else if (BASELINK_P (name))
15067 name = tsubst_baselink (name,
15068 non_reference (TREE_TYPE (object)),
15069 args, complain,
15070 in_decl);
15071 else
15072 name = tsubst_copy (name, args, complain, in_decl);
15073 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15074 }
15075
15076 case PLUS_EXPR:
15077 case MINUS_EXPR:
15078 case MULT_EXPR:
15079 case TRUNC_DIV_EXPR:
15080 case CEIL_DIV_EXPR:
15081 case FLOOR_DIV_EXPR:
15082 case ROUND_DIV_EXPR:
15083 case EXACT_DIV_EXPR:
15084 case BIT_AND_EXPR:
15085 case BIT_IOR_EXPR:
15086 case BIT_XOR_EXPR:
15087 case TRUNC_MOD_EXPR:
15088 case FLOOR_MOD_EXPR:
15089 case TRUTH_ANDIF_EXPR:
15090 case TRUTH_ORIF_EXPR:
15091 case TRUTH_AND_EXPR:
15092 case TRUTH_OR_EXPR:
15093 case RSHIFT_EXPR:
15094 case LSHIFT_EXPR:
15095 case RROTATE_EXPR:
15096 case LROTATE_EXPR:
15097 case EQ_EXPR:
15098 case NE_EXPR:
15099 case MAX_EXPR:
15100 case MIN_EXPR:
15101 case LE_EXPR:
15102 case GE_EXPR:
15103 case LT_EXPR:
15104 case GT_EXPR:
15105 case COMPOUND_EXPR:
15106 case DOTSTAR_EXPR:
15107 case MEMBER_REF:
15108 case PREDECREMENT_EXPR:
15109 case PREINCREMENT_EXPR:
15110 case POSTDECREMENT_EXPR:
15111 case POSTINCREMENT_EXPR:
15112 {
15113 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15114 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15115 return build_nt (code, op0, op1);
15116 }
15117
15118 case SCOPE_REF:
15119 {
15120 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15121 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15122 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15123 QUALIFIED_NAME_IS_TEMPLATE (t));
15124 }
15125
15126 case ARRAY_REF:
15127 {
15128 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15129 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15130 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15131 }
15132
15133 case CALL_EXPR:
15134 {
15135 int n = VL_EXP_OPERAND_LENGTH (t);
15136 tree result = build_vl_exp (CALL_EXPR, n);
15137 int i;
15138 for (i = 0; i < n; i++)
15139 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15140 complain, in_decl);
15141 return result;
15142 }
15143
15144 case COND_EXPR:
15145 case MODOP_EXPR:
15146 case PSEUDO_DTOR_EXPR:
15147 case VEC_PERM_EXPR:
15148 {
15149 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15150 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15151 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15152 r = build_nt (code, op0, op1, op2);
15153 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15154 return r;
15155 }
15156
15157 case NEW_EXPR:
15158 {
15159 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15160 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15161 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15162 r = build_nt (code, op0, op1, op2);
15163 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15164 return r;
15165 }
15166
15167 case DELETE_EXPR:
15168 {
15169 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15170 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15171 r = build_nt (code, op0, op1);
15172 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15173 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15174 return r;
15175 }
15176
15177 case TEMPLATE_ID_EXPR:
15178 {
15179 /* Substituted template arguments */
15180 tree fn = TREE_OPERAND (t, 0);
15181 tree targs = TREE_OPERAND (t, 1);
15182
15183 fn = tsubst_copy (fn, args, complain, in_decl);
15184 if (targs)
15185 targs = tsubst_template_args (targs, args, complain, in_decl);
15186
15187 return lookup_template_function (fn, targs);
15188 }
15189
15190 case TREE_LIST:
15191 {
15192 tree purpose, value, chain;
15193
15194 if (t == void_list_node)
15195 return t;
15196
15197 purpose = TREE_PURPOSE (t);
15198 if (purpose)
15199 purpose = tsubst_copy (purpose, args, complain, in_decl);
15200 value = TREE_VALUE (t);
15201 if (value)
15202 value = tsubst_copy (value, args, complain, in_decl);
15203 chain = TREE_CHAIN (t);
15204 if (chain && chain != void_type_node)
15205 chain = tsubst_copy (chain, args, complain, in_decl);
15206 if (purpose == TREE_PURPOSE (t)
15207 && value == TREE_VALUE (t)
15208 && chain == TREE_CHAIN (t))
15209 return t;
15210 return tree_cons (purpose, value, chain);
15211 }
15212
15213 case RECORD_TYPE:
15214 case UNION_TYPE:
15215 case ENUMERAL_TYPE:
15216 case INTEGER_TYPE:
15217 case TEMPLATE_TYPE_PARM:
15218 case TEMPLATE_TEMPLATE_PARM:
15219 case BOUND_TEMPLATE_TEMPLATE_PARM:
15220 case TEMPLATE_PARM_INDEX:
15221 case POINTER_TYPE:
15222 case REFERENCE_TYPE:
15223 case OFFSET_TYPE:
15224 case FUNCTION_TYPE:
15225 case METHOD_TYPE:
15226 case ARRAY_TYPE:
15227 case TYPENAME_TYPE:
15228 case UNBOUND_CLASS_TEMPLATE:
15229 case TYPEOF_TYPE:
15230 case DECLTYPE_TYPE:
15231 case TYPE_DECL:
15232 return tsubst (t, args, complain, in_decl);
15233
15234 case USING_DECL:
15235 t = DECL_NAME (t);
15236 /* Fall through. */
15237 case IDENTIFIER_NODE:
15238 if (IDENTIFIER_CONV_OP_P (t))
15239 {
15240 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15241 return make_conv_op_name (new_type);
15242 }
15243 else
15244 return t;
15245
15246 case CONSTRUCTOR:
15247 /* This is handled by tsubst_copy_and_build. */
15248 gcc_unreachable ();
15249
15250 case VA_ARG_EXPR:
15251 {
15252 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15253 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15254 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15255 }
15256
15257 case CLEANUP_POINT_EXPR:
15258 /* We shouldn't have built any of these during initial template
15259 generation. Instead, they should be built during instantiation
15260 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15261 gcc_unreachable ();
15262
15263 case OFFSET_REF:
15264 {
15265 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15266 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15267 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15268 r = build2 (code, type, op0, op1);
15269 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15270 if (!mark_used (TREE_OPERAND (r, 1), complain)
15271 && !(complain & tf_error))
15272 return error_mark_node;
15273 return r;
15274 }
15275
15276 case EXPR_PACK_EXPANSION:
15277 error ("invalid use of pack expansion expression");
15278 return error_mark_node;
15279
15280 case NONTYPE_ARGUMENT_PACK:
15281 error ("use %<...%> to expand argument pack");
15282 return error_mark_node;
15283
15284 case VOID_CST:
15285 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15286 return t;
15287
15288 case INTEGER_CST:
15289 case REAL_CST:
15290 case STRING_CST:
15291 case COMPLEX_CST:
15292 {
15293 /* Instantiate any typedefs in the type. */
15294 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15295 r = fold_convert (type, t);
15296 gcc_assert (TREE_CODE (r) == code);
15297 return r;
15298 }
15299
15300 case PTRMEM_CST:
15301 /* These can sometimes show up in a partial instantiation, but never
15302 involve template parms. */
15303 gcc_assert (!uses_template_parms (t));
15304 return t;
15305
15306 case UNARY_LEFT_FOLD_EXPR:
15307 return tsubst_unary_left_fold (t, args, complain, in_decl);
15308 case UNARY_RIGHT_FOLD_EXPR:
15309 return tsubst_unary_right_fold (t, args, complain, in_decl);
15310 case BINARY_LEFT_FOLD_EXPR:
15311 return tsubst_binary_left_fold (t, args, complain, in_decl);
15312 case BINARY_RIGHT_FOLD_EXPR:
15313 return tsubst_binary_right_fold (t, args, complain, in_decl);
15314 case PREDICT_EXPR:
15315 return t;
15316
15317 case DEBUG_BEGIN_STMT:
15318 /* ??? There's no point in copying it for now, but maybe some
15319 day it will contain more information, such as a pointer back
15320 to the containing function, inlined copy or so. */
15321 return t;
15322
15323 default:
15324 /* We shouldn't get here, but keep going if !flag_checking. */
15325 if (flag_checking)
15326 gcc_unreachable ();
15327 return t;
15328 }
15329 }
15330
15331 /* Helper function for tsubst_omp_clauses, used for instantiation of
15332 OMP_CLAUSE_DECL of clauses. */
15333
15334 static tree
15335 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15336 tree in_decl)
15337 {
15338 if (decl == NULL_TREE)
15339 return NULL_TREE;
15340
15341 /* Handle an OpenMP array section represented as a TREE_LIST (or
15342 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15343 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15344 TREE_LIST. We can handle it exactly the same as an array section
15345 (purpose, value, and a chain), even though the nomenclature
15346 (low_bound, length, etc) is different. */
15347 if (TREE_CODE (decl) == TREE_LIST)
15348 {
15349 tree low_bound
15350 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15351 /*integral_constant_expression_p=*/false);
15352 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15353 /*integral_constant_expression_p=*/false);
15354 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15355 in_decl);
15356 if (TREE_PURPOSE (decl) == low_bound
15357 && TREE_VALUE (decl) == length
15358 && TREE_CHAIN (decl) == chain)
15359 return decl;
15360 tree ret = tree_cons (low_bound, length, chain);
15361 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15362 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15363 return ret;
15364 }
15365 tree ret = tsubst_expr (decl, args, complain, in_decl,
15366 /*integral_constant_expression_p=*/false);
15367 /* Undo convert_from_reference tsubst_expr could have called. */
15368 if (decl
15369 && REFERENCE_REF_P (ret)
15370 && !REFERENCE_REF_P (decl))
15371 ret = TREE_OPERAND (ret, 0);
15372 return ret;
15373 }
15374
15375 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15376
15377 static tree
15378 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15379 tree args, tsubst_flags_t complain, tree in_decl)
15380 {
15381 tree new_clauses = NULL_TREE, nc, oc;
15382 tree linear_no_step = NULL_TREE;
15383
15384 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15385 {
15386 nc = copy_node (oc);
15387 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15388 new_clauses = nc;
15389
15390 switch (OMP_CLAUSE_CODE (nc))
15391 {
15392 case OMP_CLAUSE_LASTPRIVATE:
15393 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15394 {
15395 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15396 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15397 in_decl, /*integral_constant_expression_p=*/false);
15398 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15399 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15400 }
15401 /* FALLTHRU */
15402 case OMP_CLAUSE_PRIVATE:
15403 case OMP_CLAUSE_SHARED:
15404 case OMP_CLAUSE_FIRSTPRIVATE:
15405 case OMP_CLAUSE_COPYIN:
15406 case OMP_CLAUSE_COPYPRIVATE:
15407 case OMP_CLAUSE_UNIFORM:
15408 case OMP_CLAUSE_DEPEND:
15409 case OMP_CLAUSE_FROM:
15410 case OMP_CLAUSE_TO:
15411 case OMP_CLAUSE_MAP:
15412 case OMP_CLAUSE_USE_DEVICE_PTR:
15413 case OMP_CLAUSE_IS_DEVICE_PTR:
15414 OMP_CLAUSE_DECL (nc)
15415 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15416 in_decl);
15417 break;
15418 case OMP_CLAUSE_TILE:
15419 case OMP_CLAUSE_IF:
15420 case OMP_CLAUSE_NUM_THREADS:
15421 case OMP_CLAUSE_SCHEDULE:
15422 case OMP_CLAUSE_COLLAPSE:
15423 case OMP_CLAUSE_FINAL:
15424 case OMP_CLAUSE_DEVICE:
15425 case OMP_CLAUSE_DIST_SCHEDULE:
15426 case OMP_CLAUSE_NUM_TEAMS:
15427 case OMP_CLAUSE_THREAD_LIMIT:
15428 case OMP_CLAUSE_SAFELEN:
15429 case OMP_CLAUSE_SIMDLEN:
15430 case OMP_CLAUSE_NUM_TASKS:
15431 case OMP_CLAUSE_GRAINSIZE:
15432 case OMP_CLAUSE_PRIORITY:
15433 case OMP_CLAUSE_ORDERED:
15434 case OMP_CLAUSE_HINT:
15435 case OMP_CLAUSE_NUM_GANGS:
15436 case OMP_CLAUSE_NUM_WORKERS:
15437 case OMP_CLAUSE_VECTOR_LENGTH:
15438 case OMP_CLAUSE_WORKER:
15439 case OMP_CLAUSE_VECTOR:
15440 case OMP_CLAUSE_ASYNC:
15441 case OMP_CLAUSE_WAIT:
15442 OMP_CLAUSE_OPERAND (nc, 0)
15443 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15444 in_decl, /*integral_constant_expression_p=*/false);
15445 break;
15446 case OMP_CLAUSE_REDUCTION:
15447 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15448 {
15449 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15450 if (TREE_CODE (placeholder) == SCOPE_REF)
15451 {
15452 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15453 complain, in_decl);
15454 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15455 = build_qualified_name (NULL_TREE, scope,
15456 TREE_OPERAND (placeholder, 1),
15457 false);
15458 }
15459 else
15460 gcc_assert (identifier_p (placeholder));
15461 }
15462 OMP_CLAUSE_DECL (nc)
15463 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15464 in_decl);
15465 break;
15466 case OMP_CLAUSE_GANG:
15467 case OMP_CLAUSE_ALIGNED:
15468 OMP_CLAUSE_DECL (nc)
15469 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15470 in_decl);
15471 OMP_CLAUSE_OPERAND (nc, 1)
15472 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15473 in_decl, /*integral_constant_expression_p=*/false);
15474 break;
15475 case OMP_CLAUSE_LINEAR:
15476 OMP_CLAUSE_DECL (nc)
15477 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15478 in_decl);
15479 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15480 {
15481 gcc_assert (!linear_no_step);
15482 linear_no_step = nc;
15483 }
15484 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15485 OMP_CLAUSE_LINEAR_STEP (nc)
15486 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15487 complain, in_decl);
15488 else
15489 OMP_CLAUSE_LINEAR_STEP (nc)
15490 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15491 in_decl,
15492 /*integral_constant_expression_p=*/false);
15493 break;
15494 case OMP_CLAUSE_NOWAIT:
15495 case OMP_CLAUSE_DEFAULT:
15496 case OMP_CLAUSE_UNTIED:
15497 case OMP_CLAUSE_MERGEABLE:
15498 case OMP_CLAUSE_INBRANCH:
15499 case OMP_CLAUSE_NOTINBRANCH:
15500 case OMP_CLAUSE_PROC_BIND:
15501 case OMP_CLAUSE_FOR:
15502 case OMP_CLAUSE_PARALLEL:
15503 case OMP_CLAUSE_SECTIONS:
15504 case OMP_CLAUSE_TASKGROUP:
15505 case OMP_CLAUSE_NOGROUP:
15506 case OMP_CLAUSE_THREADS:
15507 case OMP_CLAUSE_SIMD:
15508 case OMP_CLAUSE_DEFAULTMAP:
15509 case OMP_CLAUSE_INDEPENDENT:
15510 case OMP_CLAUSE_AUTO:
15511 case OMP_CLAUSE_SEQ:
15512 break;
15513 default:
15514 gcc_unreachable ();
15515 }
15516 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15517 switch (OMP_CLAUSE_CODE (nc))
15518 {
15519 case OMP_CLAUSE_SHARED:
15520 case OMP_CLAUSE_PRIVATE:
15521 case OMP_CLAUSE_FIRSTPRIVATE:
15522 case OMP_CLAUSE_LASTPRIVATE:
15523 case OMP_CLAUSE_COPYPRIVATE:
15524 case OMP_CLAUSE_LINEAR:
15525 case OMP_CLAUSE_REDUCTION:
15526 case OMP_CLAUSE_USE_DEVICE_PTR:
15527 case OMP_CLAUSE_IS_DEVICE_PTR:
15528 /* tsubst_expr on SCOPE_REF results in returning
15529 finish_non_static_data_member result. Undo that here. */
15530 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15531 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15532 == IDENTIFIER_NODE))
15533 {
15534 tree t = OMP_CLAUSE_DECL (nc);
15535 tree v = t;
15536 while (v)
15537 switch (TREE_CODE (v))
15538 {
15539 case COMPONENT_REF:
15540 case MEM_REF:
15541 case INDIRECT_REF:
15542 CASE_CONVERT:
15543 case POINTER_PLUS_EXPR:
15544 v = TREE_OPERAND (v, 0);
15545 continue;
15546 case PARM_DECL:
15547 if (DECL_CONTEXT (v) == current_function_decl
15548 && DECL_ARTIFICIAL (v)
15549 && DECL_NAME (v) == this_identifier)
15550 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15551 /* FALLTHRU */
15552 default:
15553 v = NULL_TREE;
15554 break;
15555 }
15556 }
15557 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15558 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15559 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15560 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15561 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15562 {
15563 tree decl = OMP_CLAUSE_DECL (nc);
15564 if (VAR_P (decl))
15565 {
15566 retrofit_lang_decl (decl);
15567 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15568 }
15569 }
15570 break;
15571 default:
15572 break;
15573 }
15574 }
15575
15576 new_clauses = nreverse (new_clauses);
15577 if (ort != C_ORT_OMP_DECLARE_SIMD)
15578 {
15579 new_clauses = finish_omp_clauses (new_clauses, ort);
15580 if (linear_no_step)
15581 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15582 if (nc == linear_no_step)
15583 {
15584 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15585 break;
15586 }
15587 }
15588 return new_clauses;
15589 }
15590
15591 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15592
15593 static tree
15594 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15595 tree in_decl)
15596 {
15597 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15598
15599 tree purpose, value, chain;
15600
15601 if (t == NULL)
15602 return t;
15603
15604 if (TREE_CODE (t) != TREE_LIST)
15605 return tsubst_copy_and_build (t, args, complain, in_decl,
15606 /*function_p=*/false,
15607 /*integral_constant_expression_p=*/false);
15608
15609 if (t == void_list_node)
15610 return t;
15611
15612 purpose = TREE_PURPOSE (t);
15613 if (purpose)
15614 purpose = RECUR (purpose);
15615 value = TREE_VALUE (t);
15616 if (value)
15617 {
15618 if (TREE_CODE (value) != LABEL_DECL)
15619 value = RECUR (value);
15620 else
15621 {
15622 value = lookup_label (DECL_NAME (value));
15623 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15624 TREE_USED (value) = 1;
15625 }
15626 }
15627 chain = TREE_CHAIN (t);
15628 if (chain && chain != void_type_node)
15629 chain = RECUR (chain);
15630 return tree_cons (purpose, value, chain);
15631 #undef RECUR
15632 }
15633
15634 /* Used to temporarily communicate the list of #pragma omp parallel
15635 clauses to #pragma omp for instantiation if they are combined
15636 together. */
15637
15638 static tree *omp_parallel_combined_clauses;
15639
15640 /* Substitute one OMP_FOR iterator. */
15641
15642 static void
15643 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15644 tree initv, tree condv, tree incrv, tree *clauses,
15645 tree args, tsubst_flags_t complain, tree in_decl,
15646 bool integral_constant_expression_p)
15647 {
15648 #define RECUR(NODE) \
15649 tsubst_expr ((NODE), args, complain, in_decl, \
15650 integral_constant_expression_p)
15651 tree decl, init, cond, incr;
15652
15653 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15654 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15655
15656 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15657 {
15658 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15659 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15660 }
15661
15662 decl = TREE_OPERAND (init, 0);
15663 init = TREE_OPERAND (init, 1);
15664 tree decl_expr = NULL_TREE;
15665 if (init && TREE_CODE (init) == DECL_EXPR)
15666 {
15667 /* We need to jump through some hoops to handle declarations in the
15668 init-statement, since we might need to handle auto deduction,
15669 but we need to keep control of initialization. */
15670 decl_expr = init;
15671 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15672 decl = tsubst_decl (decl, args, complain);
15673 }
15674 else
15675 {
15676 if (TREE_CODE (decl) == SCOPE_REF)
15677 {
15678 decl = RECUR (decl);
15679 if (TREE_CODE (decl) == COMPONENT_REF)
15680 {
15681 tree v = decl;
15682 while (v)
15683 switch (TREE_CODE (v))
15684 {
15685 case COMPONENT_REF:
15686 case MEM_REF:
15687 case INDIRECT_REF:
15688 CASE_CONVERT:
15689 case POINTER_PLUS_EXPR:
15690 v = TREE_OPERAND (v, 0);
15691 continue;
15692 case PARM_DECL:
15693 if (DECL_CONTEXT (v) == current_function_decl
15694 && DECL_ARTIFICIAL (v)
15695 && DECL_NAME (v) == this_identifier)
15696 {
15697 decl = TREE_OPERAND (decl, 1);
15698 decl = omp_privatize_field (decl, false);
15699 }
15700 /* FALLTHRU */
15701 default:
15702 v = NULL_TREE;
15703 break;
15704 }
15705 }
15706 }
15707 else
15708 decl = RECUR (decl);
15709 }
15710 init = RECUR (init);
15711
15712 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15713 if (auto_node && init)
15714 TREE_TYPE (decl)
15715 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15716
15717 gcc_assert (!type_dependent_expression_p (decl));
15718
15719 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15720 {
15721 if (decl_expr)
15722 {
15723 /* Declare the variable, but don't let that initialize it. */
15724 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15725 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15726 RECUR (decl_expr);
15727 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15728 }
15729
15730 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15731 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15732 if (TREE_CODE (incr) == MODIFY_EXPR)
15733 {
15734 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15735 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15736 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15737 NOP_EXPR, rhs, complain);
15738 }
15739 else
15740 incr = RECUR (incr);
15741 TREE_VEC_ELT (declv, i) = decl;
15742 TREE_VEC_ELT (initv, i) = init;
15743 TREE_VEC_ELT (condv, i) = cond;
15744 TREE_VEC_ELT (incrv, i) = incr;
15745 return;
15746 }
15747
15748 if (decl_expr)
15749 {
15750 /* Declare and initialize the variable. */
15751 RECUR (decl_expr);
15752 init = NULL_TREE;
15753 }
15754 else if (init)
15755 {
15756 tree *pc;
15757 int j;
15758 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15759 {
15760 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15761 {
15762 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15763 && OMP_CLAUSE_DECL (*pc) == decl)
15764 break;
15765 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15766 && OMP_CLAUSE_DECL (*pc) == decl)
15767 {
15768 if (j)
15769 break;
15770 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15771 tree c = *pc;
15772 *pc = OMP_CLAUSE_CHAIN (c);
15773 OMP_CLAUSE_CHAIN (c) = *clauses;
15774 *clauses = c;
15775 }
15776 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15777 && OMP_CLAUSE_DECL (*pc) == decl)
15778 {
15779 error ("iteration variable %qD should not be firstprivate",
15780 decl);
15781 *pc = OMP_CLAUSE_CHAIN (*pc);
15782 }
15783 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15784 && OMP_CLAUSE_DECL (*pc) == decl)
15785 {
15786 error ("iteration variable %qD should not be reduction",
15787 decl);
15788 *pc = OMP_CLAUSE_CHAIN (*pc);
15789 }
15790 else
15791 pc = &OMP_CLAUSE_CHAIN (*pc);
15792 }
15793 if (*pc)
15794 break;
15795 }
15796 if (*pc == NULL_TREE)
15797 {
15798 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15799 OMP_CLAUSE_DECL (c) = decl;
15800 c = finish_omp_clauses (c, C_ORT_OMP);
15801 if (c)
15802 {
15803 OMP_CLAUSE_CHAIN (c) = *clauses;
15804 *clauses = c;
15805 }
15806 }
15807 }
15808 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15809 if (COMPARISON_CLASS_P (cond))
15810 {
15811 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15812 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15813 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15814 }
15815 else
15816 cond = RECUR (cond);
15817 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15818 switch (TREE_CODE (incr))
15819 {
15820 case PREINCREMENT_EXPR:
15821 case PREDECREMENT_EXPR:
15822 case POSTINCREMENT_EXPR:
15823 case POSTDECREMENT_EXPR:
15824 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15825 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15826 break;
15827 case MODIFY_EXPR:
15828 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15829 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15830 {
15831 tree rhs = TREE_OPERAND (incr, 1);
15832 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15833 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15834 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15835 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15836 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15837 rhs0, rhs1));
15838 }
15839 else
15840 incr = RECUR (incr);
15841 break;
15842 case MODOP_EXPR:
15843 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15844 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15845 {
15846 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15847 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15848 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15849 TREE_TYPE (decl), lhs,
15850 RECUR (TREE_OPERAND (incr, 2))));
15851 }
15852 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15853 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15854 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15855 {
15856 tree rhs = TREE_OPERAND (incr, 2);
15857 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15858 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15859 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15860 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15861 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15862 rhs0, rhs1));
15863 }
15864 else
15865 incr = RECUR (incr);
15866 break;
15867 default:
15868 incr = RECUR (incr);
15869 break;
15870 }
15871
15872 TREE_VEC_ELT (declv, i) = decl;
15873 TREE_VEC_ELT (initv, i) = init;
15874 TREE_VEC_ELT (condv, i) = cond;
15875 TREE_VEC_ELT (incrv, i) = incr;
15876 #undef RECUR
15877 }
15878
15879 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15880 of OMP_TARGET's body. */
15881
15882 static tree
15883 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15884 {
15885 *walk_subtrees = 0;
15886 switch (TREE_CODE (*tp))
15887 {
15888 case OMP_TEAMS:
15889 return *tp;
15890 case BIND_EXPR:
15891 case STATEMENT_LIST:
15892 *walk_subtrees = 1;
15893 break;
15894 default:
15895 break;
15896 }
15897 return NULL_TREE;
15898 }
15899
15900 /* Helper function for tsubst_expr. For decomposition declaration
15901 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15902 also the corresponding decls representing the identifiers
15903 of the decomposition declaration. Return DECL if successful
15904 or error_mark_node otherwise, set *FIRST to the first decl
15905 in the list chained through DECL_CHAIN and *CNT to the number
15906 of such decls. */
15907
15908 static tree
15909 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15910 tsubst_flags_t complain, tree in_decl, tree *first,
15911 unsigned int *cnt)
15912 {
15913 tree decl2, decl3, prev = decl;
15914 *cnt = 0;
15915 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15916 for (decl2 = DECL_CHAIN (pattern_decl);
15917 decl2
15918 && VAR_P (decl2)
15919 && DECL_DECOMPOSITION_P (decl2)
15920 && DECL_NAME (decl2);
15921 decl2 = DECL_CHAIN (decl2))
15922 {
15923 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15924 {
15925 gcc_assert (errorcount);
15926 return error_mark_node;
15927 }
15928 (*cnt)++;
15929 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15930 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15931 tree v = DECL_VALUE_EXPR (decl2);
15932 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15933 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15934 decl3 = tsubst (decl2, args, complain, in_decl);
15935 SET_DECL_VALUE_EXPR (decl2, v);
15936 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15937 if (VAR_P (decl3))
15938 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15939 maybe_push_decl (decl3);
15940 if (error_operand_p (decl3))
15941 decl = error_mark_node;
15942 else if (decl != error_mark_node
15943 && DECL_CHAIN (decl3) != prev)
15944 {
15945 gcc_assert (errorcount);
15946 decl = error_mark_node;
15947 }
15948 else
15949 prev = decl3;
15950 }
15951 *first = prev;
15952 return decl;
15953 }
15954
15955 /* Like tsubst_copy for expressions, etc. but also does semantic
15956 processing. */
15957
15958 tree
15959 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15960 bool integral_constant_expression_p)
15961 {
15962 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15963 #define RECUR(NODE) \
15964 tsubst_expr ((NODE), args, complain, in_decl, \
15965 integral_constant_expression_p)
15966
15967 tree stmt, tmp;
15968 tree r;
15969 location_t loc;
15970
15971 if (t == NULL_TREE || t == error_mark_node)
15972 return t;
15973
15974 loc = input_location;
15975 if (EXPR_HAS_LOCATION (t))
15976 input_location = EXPR_LOCATION (t);
15977 if (STATEMENT_CODE_P (TREE_CODE (t)))
15978 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15979
15980 switch (TREE_CODE (t))
15981 {
15982 case STATEMENT_LIST:
15983 {
15984 tree_stmt_iterator i;
15985 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15986 RECUR (tsi_stmt (i));
15987 break;
15988 }
15989
15990 case CTOR_INITIALIZER:
15991 finish_mem_initializers (tsubst_initializer_list
15992 (TREE_OPERAND (t, 0), args));
15993 break;
15994
15995 case RETURN_EXPR:
15996 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15997 break;
15998
15999 case EXPR_STMT:
16000 tmp = RECUR (EXPR_STMT_EXPR (t));
16001 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16002 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16003 else
16004 finish_expr_stmt (tmp);
16005 break;
16006
16007 case USING_STMT:
16008 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16009 /*attribs=*/NULL_TREE);
16010 break;
16011
16012 case DECL_EXPR:
16013 {
16014 tree decl, pattern_decl;
16015 tree init;
16016
16017 pattern_decl = decl = DECL_EXPR_DECL (t);
16018 if (TREE_CODE (decl) == LABEL_DECL)
16019 finish_label_decl (DECL_NAME (decl));
16020 else if (TREE_CODE (decl) == USING_DECL)
16021 {
16022 tree scope = USING_DECL_SCOPE (decl);
16023 tree name = DECL_NAME (decl);
16024
16025 scope = tsubst (scope, args, complain, in_decl);
16026 decl = lookup_qualified_name (scope, name,
16027 /*is_type_p=*/false,
16028 /*complain=*/false);
16029 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16030 qualified_name_lookup_error (scope, name, decl, input_location);
16031 else
16032 finish_local_using_decl (decl, scope, name);
16033 }
16034 else if (DECL_PACK_P (decl))
16035 {
16036 /* Don't build up decls for a variadic capture proxy, we'll
16037 instantiate the elements directly as needed. */
16038 break;
16039 }
16040 else if (is_capture_proxy (decl)
16041 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16042 {
16043 /* We're in tsubst_lambda_expr, we've already inserted a new
16044 capture proxy, so look it up and register it. */
16045 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16046 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16047 gcc_assert (inst != decl && is_capture_proxy (inst));
16048 register_local_specialization (inst, decl);
16049 break;
16050 }
16051 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16052 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16053 /* Don't copy the old closure; we'll create a new one in
16054 tsubst_lambda_expr. */
16055 break;
16056 else
16057 {
16058 init = DECL_INITIAL (decl);
16059 decl = tsubst (decl, args, complain, in_decl);
16060 if (decl != error_mark_node)
16061 {
16062 /* By marking the declaration as instantiated, we avoid
16063 trying to instantiate it. Since instantiate_decl can't
16064 handle local variables, and since we've already done
16065 all that needs to be done, that's the right thing to
16066 do. */
16067 if (VAR_P (decl))
16068 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16069 if (VAR_P (decl)
16070 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16071 /* Anonymous aggregates are a special case. */
16072 finish_anon_union (decl);
16073 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16074 {
16075 DECL_CONTEXT (decl) = current_function_decl;
16076 if (DECL_NAME (decl) == this_identifier)
16077 {
16078 tree lam = DECL_CONTEXT (current_function_decl);
16079 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16080 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16081 }
16082 insert_capture_proxy (decl);
16083 }
16084 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16085 /* We already did a pushtag. */;
16086 else if (TREE_CODE (decl) == FUNCTION_DECL
16087 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16088 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16089 {
16090 DECL_CONTEXT (decl) = NULL_TREE;
16091 pushdecl (decl);
16092 DECL_CONTEXT (decl) = current_function_decl;
16093 cp_check_omp_declare_reduction (decl);
16094 }
16095 else
16096 {
16097 int const_init = false;
16098 maybe_push_decl (decl);
16099 if (VAR_P (decl)
16100 && DECL_PRETTY_FUNCTION_P (decl))
16101 {
16102 /* For __PRETTY_FUNCTION__ we have to adjust the
16103 initializer. */
16104 const char *const name
16105 = cxx_printable_name (current_function_decl, 2);
16106 init = cp_fname_init (name, &TREE_TYPE (decl));
16107 }
16108 else
16109 init = tsubst_init (init, decl, args, complain, in_decl);
16110
16111 if (VAR_P (decl))
16112 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16113 (pattern_decl));
16114 if (VAR_P (decl)
16115 && DECL_DECOMPOSITION_P (decl)
16116 && TREE_TYPE (pattern_decl) != error_mark_node)
16117 {
16118 unsigned int cnt;
16119 tree first;
16120 tree ndecl
16121 = tsubst_decomp_names (decl, pattern_decl, args,
16122 complain, in_decl, &first, &cnt);
16123 if (ndecl != error_mark_node)
16124 cp_maybe_mangle_decomp (ndecl, first, cnt);
16125 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16126 if (ndecl != error_mark_node)
16127 cp_finish_decomp (ndecl, first, cnt);
16128 }
16129 else
16130 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16131 }
16132 }
16133 }
16134
16135 break;
16136 }
16137
16138 case FOR_STMT:
16139 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16140 RECUR (FOR_INIT_STMT (t));
16141 finish_init_stmt (stmt);
16142 tmp = RECUR (FOR_COND (t));
16143 finish_for_cond (tmp, stmt, false, 0);
16144 tmp = RECUR (FOR_EXPR (t));
16145 finish_for_expr (tmp, stmt);
16146 {
16147 bool prev = note_iteration_stmt_body_start ();
16148 RECUR (FOR_BODY (t));
16149 note_iteration_stmt_body_end (prev);
16150 }
16151 finish_for_stmt (stmt);
16152 break;
16153
16154 case RANGE_FOR_STMT:
16155 {
16156 /* Construct another range_for, if this is not a final
16157 substitution (for inside inside a generic lambda of a
16158 template). Otherwise convert to a regular for. */
16159 tree decl, expr;
16160 stmt = (processing_template_decl
16161 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16162 : begin_for_stmt (NULL_TREE, NULL_TREE));
16163 decl = RANGE_FOR_DECL (t);
16164 decl = tsubst (decl, args, complain, in_decl);
16165 maybe_push_decl (decl);
16166 expr = RECUR (RANGE_FOR_EXPR (t));
16167
16168 tree decomp_first = NULL_TREE;
16169 unsigned decomp_cnt = 0;
16170 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16171 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16172 complain, in_decl,
16173 &decomp_first, &decomp_cnt);
16174
16175 if (processing_template_decl)
16176 {
16177 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16178 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16179 finish_range_for_decl (stmt, decl, expr);
16180 }
16181 else
16182 {
16183 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16184 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16185 stmt = cp_convert_range_for (stmt, decl, expr,
16186 decomp_first, decomp_cnt,
16187 RANGE_FOR_IVDEP (t), unroll);
16188 }
16189
16190 bool prev = note_iteration_stmt_body_start ();
16191 RECUR (RANGE_FOR_BODY (t));
16192 note_iteration_stmt_body_end (prev);
16193 finish_for_stmt (stmt);
16194 }
16195 break;
16196
16197 case WHILE_STMT:
16198 stmt = begin_while_stmt ();
16199 tmp = RECUR (WHILE_COND (t));
16200 finish_while_stmt_cond (tmp, stmt, false, 0);
16201 {
16202 bool prev = note_iteration_stmt_body_start ();
16203 RECUR (WHILE_BODY (t));
16204 note_iteration_stmt_body_end (prev);
16205 }
16206 finish_while_stmt (stmt);
16207 break;
16208
16209 case DO_STMT:
16210 stmt = begin_do_stmt ();
16211 {
16212 bool prev = note_iteration_stmt_body_start ();
16213 RECUR (DO_BODY (t));
16214 note_iteration_stmt_body_end (prev);
16215 }
16216 finish_do_body (stmt);
16217 tmp = RECUR (DO_COND (t));
16218 finish_do_stmt (tmp, stmt, false, 0);
16219 break;
16220
16221 case IF_STMT:
16222 stmt = begin_if_stmt ();
16223 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16224 tmp = RECUR (IF_COND (t));
16225 tmp = finish_if_stmt_cond (tmp, stmt);
16226 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16227 /* Don't instantiate the THEN_CLAUSE. */;
16228 else
16229 {
16230 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16231 if (inhibit)
16232 ++c_inhibit_evaluation_warnings;
16233 RECUR (THEN_CLAUSE (t));
16234 if (inhibit)
16235 --c_inhibit_evaluation_warnings;
16236 }
16237 finish_then_clause (stmt);
16238
16239 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16240 /* Don't instantiate the ELSE_CLAUSE. */;
16241 else if (ELSE_CLAUSE (t))
16242 {
16243 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16244 begin_else_clause (stmt);
16245 if (inhibit)
16246 ++c_inhibit_evaluation_warnings;
16247 RECUR (ELSE_CLAUSE (t));
16248 if (inhibit)
16249 --c_inhibit_evaluation_warnings;
16250 finish_else_clause (stmt);
16251 }
16252
16253 finish_if_stmt (stmt);
16254 break;
16255
16256 case BIND_EXPR:
16257 if (BIND_EXPR_BODY_BLOCK (t))
16258 stmt = begin_function_body ();
16259 else
16260 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16261 ? BCS_TRY_BLOCK : 0);
16262
16263 RECUR (BIND_EXPR_BODY (t));
16264
16265 if (BIND_EXPR_BODY_BLOCK (t))
16266 finish_function_body (stmt);
16267 else
16268 finish_compound_stmt (stmt);
16269 break;
16270
16271 case BREAK_STMT:
16272 finish_break_stmt ();
16273 break;
16274
16275 case CONTINUE_STMT:
16276 finish_continue_stmt ();
16277 break;
16278
16279 case SWITCH_STMT:
16280 stmt = begin_switch_stmt ();
16281 tmp = RECUR (SWITCH_STMT_COND (t));
16282 finish_switch_cond (tmp, stmt);
16283 RECUR (SWITCH_STMT_BODY (t));
16284 finish_switch_stmt (stmt);
16285 break;
16286
16287 case CASE_LABEL_EXPR:
16288 {
16289 tree low = RECUR (CASE_LOW (t));
16290 tree high = RECUR (CASE_HIGH (t));
16291 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16292 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16293 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16294 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16295 }
16296 break;
16297
16298 case LABEL_EXPR:
16299 {
16300 tree decl = LABEL_EXPR_LABEL (t);
16301 tree label;
16302
16303 label = finish_label_stmt (DECL_NAME (decl));
16304 if (TREE_CODE (label) == LABEL_DECL)
16305 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16306 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16307 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16308 }
16309 break;
16310
16311 case GOTO_EXPR:
16312 tmp = GOTO_DESTINATION (t);
16313 if (TREE_CODE (tmp) != LABEL_DECL)
16314 /* Computed goto's must be tsubst'd into. On the other hand,
16315 non-computed gotos must not be; the identifier in question
16316 will have no binding. */
16317 tmp = RECUR (tmp);
16318 else
16319 tmp = DECL_NAME (tmp);
16320 finish_goto_stmt (tmp);
16321 break;
16322
16323 case ASM_EXPR:
16324 {
16325 tree string = RECUR (ASM_STRING (t));
16326 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16327 complain, in_decl);
16328 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16329 complain, in_decl);
16330 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16331 complain, in_decl);
16332 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16333 complain, in_decl);
16334 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16335 clobbers, labels);
16336 tree asm_expr = tmp;
16337 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16338 asm_expr = TREE_OPERAND (asm_expr, 0);
16339 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16340 }
16341 break;
16342
16343 case TRY_BLOCK:
16344 if (CLEANUP_P (t))
16345 {
16346 stmt = begin_try_block ();
16347 RECUR (TRY_STMTS (t));
16348 finish_cleanup_try_block (stmt);
16349 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16350 }
16351 else
16352 {
16353 tree compound_stmt = NULL_TREE;
16354
16355 if (FN_TRY_BLOCK_P (t))
16356 stmt = begin_function_try_block (&compound_stmt);
16357 else
16358 stmt = begin_try_block ();
16359
16360 RECUR (TRY_STMTS (t));
16361
16362 if (FN_TRY_BLOCK_P (t))
16363 finish_function_try_block (stmt);
16364 else
16365 finish_try_block (stmt);
16366
16367 RECUR (TRY_HANDLERS (t));
16368 if (FN_TRY_BLOCK_P (t))
16369 finish_function_handler_sequence (stmt, compound_stmt);
16370 else
16371 finish_handler_sequence (stmt);
16372 }
16373 break;
16374
16375 case HANDLER:
16376 {
16377 tree decl = HANDLER_PARMS (t);
16378
16379 if (decl)
16380 {
16381 decl = tsubst (decl, args, complain, in_decl);
16382 /* Prevent instantiate_decl from trying to instantiate
16383 this variable. We've already done all that needs to be
16384 done. */
16385 if (decl != error_mark_node)
16386 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16387 }
16388 stmt = begin_handler ();
16389 finish_handler_parms (decl, stmt);
16390 RECUR (HANDLER_BODY (t));
16391 finish_handler (stmt);
16392 }
16393 break;
16394
16395 case TAG_DEFN:
16396 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16397 if (CLASS_TYPE_P (tmp))
16398 {
16399 /* Local classes are not independent templates; they are
16400 instantiated along with their containing function. And this
16401 way we don't have to deal with pushing out of one local class
16402 to instantiate a member of another local class. */
16403 /* Closures are handled by the LAMBDA_EXPR. */
16404 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16405 complete_type (tmp);
16406 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16407 if ((VAR_P (fld)
16408 || (TREE_CODE (fld) == FUNCTION_DECL
16409 && !DECL_ARTIFICIAL (fld)))
16410 && DECL_TEMPLATE_INSTANTIATION (fld))
16411 instantiate_decl (fld, /*defer_ok=*/false,
16412 /*expl_inst_class=*/false);
16413 }
16414 break;
16415
16416 case STATIC_ASSERT:
16417 {
16418 tree condition;
16419
16420 ++c_inhibit_evaluation_warnings;
16421 condition =
16422 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16423 args,
16424 complain, in_decl,
16425 /*integral_constant_expression_p=*/true);
16426 --c_inhibit_evaluation_warnings;
16427
16428 finish_static_assert (condition,
16429 STATIC_ASSERT_MESSAGE (t),
16430 STATIC_ASSERT_SOURCE_LOCATION (t),
16431 /*member_p=*/false);
16432 }
16433 break;
16434
16435 case OACC_KERNELS:
16436 case OACC_PARALLEL:
16437 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16438 in_decl);
16439 stmt = begin_omp_parallel ();
16440 RECUR (OMP_BODY (t));
16441 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16442 break;
16443
16444 case OMP_PARALLEL:
16445 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16446 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16447 complain, in_decl);
16448 if (OMP_PARALLEL_COMBINED (t))
16449 omp_parallel_combined_clauses = &tmp;
16450 stmt = begin_omp_parallel ();
16451 RECUR (OMP_PARALLEL_BODY (t));
16452 gcc_assert (omp_parallel_combined_clauses == NULL);
16453 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16454 = OMP_PARALLEL_COMBINED (t);
16455 pop_omp_privatization_clauses (r);
16456 break;
16457
16458 case OMP_TASK:
16459 r = push_omp_privatization_clauses (false);
16460 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16461 complain, in_decl);
16462 stmt = begin_omp_task ();
16463 RECUR (OMP_TASK_BODY (t));
16464 finish_omp_task (tmp, stmt);
16465 pop_omp_privatization_clauses (r);
16466 break;
16467
16468 case OMP_FOR:
16469 case OMP_SIMD:
16470 case OMP_DISTRIBUTE:
16471 case OMP_TASKLOOP:
16472 case OACC_LOOP:
16473 {
16474 tree clauses, body, pre_body;
16475 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16476 tree orig_declv = NULL_TREE;
16477 tree incrv = NULL_TREE;
16478 enum c_omp_region_type ort = C_ORT_OMP;
16479 int i;
16480
16481 if (TREE_CODE (t) == OACC_LOOP)
16482 ort = C_ORT_ACC;
16483
16484 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16485 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16486 in_decl);
16487 if (OMP_FOR_INIT (t) != NULL_TREE)
16488 {
16489 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16490 if (OMP_FOR_ORIG_DECLS (t))
16491 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16492 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16493 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16494 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16495 }
16496
16497 stmt = begin_omp_structured_block ();
16498
16499 pre_body = push_stmt_list ();
16500 RECUR (OMP_FOR_PRE_BODY (t));
16501 pre_body = pop_stmt_list (pre_body);
16502
16503 if (OMP_FOR_INIT (t) != NULL_TREE)
16504 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16505 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16506 incrv, &clauses, args, complain, in_decl,
16507 integral_constant_expression_p);
16508 omp_parallel_combined_clauses = NULL;
16509
16510 body = push_stmt_list ();
16511 RECUR (OMP_FOR_BODY (t));
16512 body = pop_stmt_list (body);
16513
16514 if (OMP_FOR_INIT (t) != NULL_TREE)
16515 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16516 orig_declv, initv, condv, incrv, body, pre_body,
16517 NULL, clauses);
16518 else
16519 {
16520 t = make_node (TREE_CODE (t));
16521 TREE_TYPE (t) = void_type_node;
16522 OMP_FOR_BODY (t) = body;
16523 OMP_FOR_PRE_BODY (t) = pre_body;
16524 OMP_FOR_CLAUSES (t) = clauses;
16525 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16526 add_stmt (t);
16527 }
16528
16529 add_stmt (finish_omp_structured_block (stmt));
16530 pop_omp_privatization_clauses (r);
16531 }
16532 break;
16533
16534 case OMP_SECTIONS:
16535 omp_parallel_combined_clauses = NULL;
16536 /* FALLTHRU */
16537 case OMP_SINGLE:
16538 case OMP_TEAMS:
16539 case OMP_CRITICAL:
16540 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16541 && OMP_TEAMS_COMBINED (t));
16542 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16543 in_decl);
16544 stmt = push_stmt_list ();
16545 RECUR (OMP_BODY (t));
16546 stmt = pop_stmt_list (stmt);
16547
16548 t = copy_node (t);
16549 OMP_BODY (t) = stmt;
16550 OMP_CLAUSES (t) = tmp;
16551 add_stmt (t);
16552 pop_omp_privatization_clauses (r);
16553 break;
16554
16555 case OACC_DATA:
16556 case OMP_TARGET_DATA:
16557 case OMP_TARGET:
16558 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16559 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16560 in_decl);
16561 keep_next_level (true);
16562 stmt = begin_omp_structured_block ();
16563
16564 RECUR (OMP_BODY (t));
16565 stmt = finish_omp_structured_block (stmt);
16566
16567 t = copy_node (t);
16568 OMP_BODY (t) = stmt;
16569 OMP_CLAUSES (t) = tmp;
16570 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16571 {
16572 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16573 if (teams)
16574 {
16575 /* For combined target teams, ensure the num_teams and
16576 thread_limit clause expressions are evaluated on the host,
16577 before entering the target construct. */
16578 tree c;
16579 for (c = OMP_TEAMS_CLAUSES (teams);
16580 c; c = OMP_CLAUSE_CHAIN (c))
16581 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16582 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16583 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16584 {
16585 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16586 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16587 if (expr == error_mark_node)
16588 continue;
16589 tmp = TARGET_EXPR_SLOT (expr);
16590 add_stmt (expr);
16591 OMP_CLAUSE_OPERAND (c, 0) = expr;
16592 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16593 OMP_CLAUSE_FIRSTPRIVATE);
16594 OMP_CLAUSE_DECL (tc) = tmp;
16595 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16596 OMP_TARGET_CLAUSES (t) = tc;
16597 }
16598 }
16599 }
16600 add_stmt (t);
16601 break;
16602
16603 case OACC_DECLARE:
16604 t = copy_node (t);
16605 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16606 complain, in_decl);
16607 OACC_DECLARE_CLAUSES (t) = tmp;
16608 add_stmt (t);
16609 break;
16610
16611 case OMP_TARGET_UPDATE:
16612 case OMP_TARGET_ENTER_DATA:
16613 case OMP_TARGET_EXIT_DATA:
16614 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16615 complain, in_decl);
16616 t = copy_node (t);
16617 OMP_STANDALONE_CLAUSES (t) = tmp;
16618 add_stmt (t);
16619 break;
16620
16621 case OACC_ENTER_DATA:
16622 case OACC_EXIT_DATA:
16623 case OACC_UPDATE:
16624 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16625 complain, in_decl);
16626 t = copy_node (t);
16627 OMP_STANDALONE_CLAUSES (t) = tmp;
16628 add_stmt (t);
16629 break;
16630
16631 case OMP_ORDERED:
16632 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16633 complain, in_decl);
16634 stmt = push_stmt_list ();
16635 RECUR (OMP_BODY (t));
16636 stmt = pop_stmt_list (stmt);
16637
16638 t = copy_node (t);
16639 OMP_BODY (t) = stmt;
16640 OMP_ORDERED_CLAUSES (t) = tmp;
16641 add_stmt (t);
16642 break;
16643
16644 case OMP_SECTION:
16645 case OMP_MASTER:
16646 case OMP_TASKGROUP:
16647 stmt = push_stmt_list ();
16648 RECUR (OMP_BODY (t));
16649 stmt = pop_stmt_list (stmt);
16650
16651 t = copy_node (t);
16652 OMP_BODY (t) = stmt;
16653 add_stmt (t);
16654 break;
16655
16656 case OMP_ATOMIC:
16657 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16658 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16659 {
16660 tree op1 = TREE_OPERAND (t, 1);
16661 tree rhs1 = NULL_TREE;
16662 tree lhs, rhs;
16663 if (TREE_CODE (op1) == COMPOUND_EXPR)
16664 {
16665 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16666 op1 = TREE_OPERAND (op1, 1);
16667 }
16668 lhs = RECUR (TREE_OPERAND (op1, 0));
16669 rhs = RECUR (TREE_OPERAND (op1, 1));
16670 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16671 NULL_TREE, NULL_TREE, rhs1,
16672 OMP_ATOMIC_SEQ_CST (t));
16673 }
16674 else
16675 {
16676 tree op1 = TREE_OPERAND (t, 1);
16677 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16678 tree rhs1 = NULL_TREE;
16679 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16680 enum tree_code opcode = NOP_EXPR;
16681 if (code == OMP_ATOMIC_READ)
16682 {
16683 v = RECUR (TREE_OPERAND (op1, 0));
16684 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16685 }
16686 else if (code == OMP_ATOMIC_CAPTURE_OLD
16687 || code == OMP_ATOMIC_CAPTURE_NEW)
16688 {
16689 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16690 v = RECUR (TREE_OPERAND (op1, 0));
16691 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16692 if (TREE_CODE (op11) == COMPOUND_EXPR)
16693 {
16694 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16695 op11 = TREE_OPERAND (op11, 1);
16696 }
16697 lhs = RECUR (TREE_OPERAND (op11, 0));
16698 rhs = RECUR (TREE_OPERAND (op11, 1));
16699 opcode = TREE_CODE (op11);
16700 if (opcode == MODIFY_EXPR)
16701 opcode = NOP_EXPR;
16702 }
16703 else
16704 {
16705 code = OMP_ATOMIC;
16706 lhs = RECUR (TREE_OPERAND (op1, 0));
16707 rhs = RECUR (TREE_OPERAND (op1, 1));
16708 }
16709 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16710 OMP_ATOMIC_SEQ_CST (t));
16711 }
16712 break;
16713
16714 case TRANSACTION_EXPR:
16715 {
16716 int flags = 0;
16717 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16718 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16719
16720 if (TRANSACTION_EXPR_IS_STMT (t))
16721 {
16722 tree body = TRANSACTION_EXPR_BODY (t);
16723 tree noex = NULL_TREE;
16724 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16725 {
16726 noex = MUST_NOT_THROW_COND (body);
16727 if (noex == NULL_TREE)
16728 noex = boolean_true_node;
16729 body = TREE_OPERAND (body, 0);
16730 }
16731 stmt = begin_transaction_stmt (input_location, NULL, flags);
16732 RECUR (body);
16733 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16734 }
16735 else
16736 {
16737 stmt = build_transaction_expr (EXPR_LOCATION (t),
16738 RECUR (TRANSACTION_EXPR_BODY (t)),
16739 flags, NULL_TREE);
16740 RETURN (stmt);
16741 }
16742 }
16743 break;
16744
16745 case MUST_NOT_THROW_EXPR:
16746 {
16747 tree op0 = RECUR (TREE_OPERAND (t, 0));
16748 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16749 RETURN (build_must_not_throw_expr (op0, cond));
16750 }
16751
16752 case EXPR_PACK_EXPANSION:
16753 error ("invalid use of pack expansion expression");
16754 RETURN (error_mark_node);
16755
16756 case NONTYPE_ARGUMENT_PACK:
16757 error ("use %<...%> to expand argument pack");
16758 RETURN (error_mark_node);
16759
16760 case COMPOUND_EXPR:
16761 tmp = RECUR (TREE_OPERAND (t, 0));
16762 if (tmp == NULL_TREE)
16763 /* If the first operand was a statement, we're done with it. */
16764 RETURN (RECUR (TREE_OPERAND (t, 1)));
16765 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16766 RECUR (TREE_OPERAND (t, 1)),
16767 complain));
16768
16769 case ANNOTATE_EXPR:
16770 tmp = RECUR (TREE_OPERAND (t, 0));
16771 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16772 TREE_TYPE (tmp), tmp,
16773 RECUR (TREE_OPERAND (t, 1)),
16774 RECUR (TREE_OPERAND (t, 2))));
16775
16776 default:
16777 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16778
16779 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16780 /*function_p=*/false,
16781 integral_constant_expression_p));
16782 }
16783
16784 RETURN (NULL_TREE);
16785 out:
16786 input_location = loc;
16787 return r;
16788 #undef RECUR
16789 #undef RETURN
16790 }
16791
16792 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16793 function. For description of the body see comment above
16794 cp_parser_omp_declare_reduction_exprs. */
16795
16796 static void
16797 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16798 {
16799 if (t == NULL_TREE || t == error_mark_node)
16800 return;
16801
16802 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16803
16804 tree_stmt_iterator tsi;
16805 int i;
16806 tree stmts[7];
16807 memset (stmts, 0, sizeof stmts);
16808 for (i = 0, tsi = tsi_start (t);
16809 i < 7 && !tsi_end_p (tsi);
16810 i++, tsi_next (&tsi))
16811 stmts[i] = tsi_stmt (tsi);
16812 gcc_assert (tsi_end_p (tsi));
16813
16814 if (i >= 3)
16815 {
16816 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16817 && TREE_CODE (stmts[1]) == DECL_EXPR);
16818 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16819 args, complain, in_decl);
16820 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16821 args, complain, in_decl);
16822 DECL_CONTEXT (omp_out) = current_function_decl;
16823 DECL_CONTEXT (omp_in) = current_function_decl;
16824 keep_next_level (true);
16825 tree block = begin_omp_structured_block ();
16826 tsubst_expr (stmts[2], args, complain, in_decl, false);
16827 block = finish_omp_structured_block (block);
16828 block = maybe_cleanup_point_expr_void (block);
16829 add_decl_expr (omp_out);
16830 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16831 TREE_NO_WARNING (omp_out) = 1;
16832 add_decl_expr (omp_in);
16833 finish_expr_stmt (block);
16834 }
16835 if (i >= 6)
16836 {
16837 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16838 && TREE_CODE (stmts[4]) == DECL_EXPR);
16839 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16840 args, complain, in_decl);
16841 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16842 args, complain, in_decl);
16843 DECL_CONTEXT (omp_priv) = current_function_decl;
16844 DECL_CONTEXT (omp_orig) = current_function_decl;
16845 keep_next_level (true);
16846 tree block = begin_omp_structured_block ();
16847 tsubst_expr (stmts[5], args, complain, in_decl, false);
16848 block = finish_omp_structured_block (block);
16849 block = maybe_cleanup_point_expr_void (block);
16850 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16851 add_decl_expr (omp_priv);
16852 add_decl_expr (omp_orig);
16853 finish_expr_stmt (block);
16854 if (i == 7)
16855 add_decl_expr (omp_orig);
16856 }
16857 }
16858
16859 /* T is a postfix-expression that is not being used in a function
16860 call. Return the substituted version of T. */
16861
16862 static tree
16863 tsubst_non_call_postfix_expression (tree t, tree args,
16864 tsubst_flags_t complain,
16865 tree in_decl)
16866 {
16867 if (TREE_CODE (t) == SCOPE_REF)
16868 t = tsubst_qualified_id (t, args, complain, in_decl,
16869 /*done=*/false, /*address_p=*/false);
16870 else
16871 t = tsubst_copy_and_build (t, args, complain, in_decl,
16872 /*function_p=*/false,
16873 /*integral_constant_expression_p=*/false);
16874
16875 return t;
16876 }
16877
16878 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16879 instantiation context. Instantiating a pack expansion containing a lambda
16880 might result in multiple lambdas all based on the same lambda in the
16881 template. */
16882
16883 tree
16884 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16885 {
16886 tree oldfn = lambda_function (t);
16887 in_decl = oldfn;
16888
16889 tree r = build_lambda_expr ();
16890
16891 LAMBDA_EXPR_LOCATION (r)
16892 = LAMBDA_EXPR_LOCATION (t);
16893 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16894 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16895 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16896
16897 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16898 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16899 else
16900 record_lambda_scope (r);
16901
16902 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16903 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16904
16905 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16906 cap = TREE_CHAIN (cap))
16907 {
16908 tree field = TREE_PURPOSE (cap);
16909 if (PACK_EXPANSION_P (field))
16910 field = PACK_EXPANSION_PATTERN (field);
16911 field = tsubst_decl (field, args, complain);
16912
16913 if (field == error_mark_node)
16914 return error_mark_node;
16915
16916 tree init = TREE_VALUE (cap);
16917 if (PACK_EXPANSION_P (init))
16918 init = tsubst_pack_expansion (init, args, complain, in_decl);
16919 else
16920 init = tsubst_copy_and_build (init, args, complain, in_decl,
16921 /*fn*/false, /*constexpr*/false);
16922
16923 if (TREE_CODE (field) == TREE_VEC)
16924 {
16925 int len = TREE_VEC_LENGTH (field);
16926 gcc_assert (TREE_CODE (init) == TREE_VEC
16927 && TREE_VEC_LENGTH (init) == len);
16928 for (int i = 0; i < len; ++i)
16929 LAMBDA_EXPR_CAPTURE_LIST (r)
16930 = tree_cons (TREE_VEC_ELT (field, i),
16931 TREE_VEC_ELT (init, i),
16932 LAMBDA_EXPR_CAPTURE_LIST (r));
16933 }
16934 else
16935 {
16936 LAMBDA_EXPR_CAPTURE_LIST (r)
16937 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16938
16939 if (id_equal (DECL_NAME (field), "__this"))
16940 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16941 }
16942 }
16943
16944 tree type = begin_lambda_type (r);
16945
16946 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16947 determine_visibility (TYPE_NAME (type));
16948
16949 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16950
16951 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16952 ? DECL_TI_TEMPLATE (oldfn)
16953 : NULL_TREE);
16954
16955 tree fntype = static_fn_type (oldfn);
16956 if (oldtmpl)
16957 ++processing_template_decl;
16958 fntype = tsubst (fntype, args, complain, in_decl);
16959 if (oldtmpl)
16960 --processing_template_decl;
16961
16962 if (fntype == error_mark_node)
16963 r = error_mark_node;
16964 else
16965 {
16966 /* Fix the type of 'this'. */
16967 fntype = build_memfn_type (fntype, type,
16968 type_memfn_quals (fntype),
16969 type_memfn_rqual (fntype));
16970 tree fn, tmpl;
16971 if (oldtmpl)
16972 {
16973 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16974 fn = DECL_TEMPLATE_RESULT (tmpl);
16975 finish_member_declaration (tmpl);
16976 }
16977 else
16978 {
16979 tmpl = NULL_TREE;
16980 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16981 finish_member_declaration (fn);
16982 }
16983
16984 /* Let finish_function set this. */
16985 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16986
16987 bool nested = cfun;
16988 if (nested)
16989 push_function_context ();
16990
16991 local_specialization_stack s (lss_copy);
16992
16993 tree body = start_lambda_function (fn, r);
16994
16995 register_parameter_specializations (oldfn, fn);
16996
16997 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16998 /*constexpr*/false);
16999
17000 finish_lambda_function (body);
17001
17002 if (nested)
17003 pop_function_context ();
17004
17005 /* The capture list was built up in reverse order; fix that now. */
17006 LAMBDA_EXPR_CAPTURE_LIST (r)
17007 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17008
17009 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17010
17011 maybe_add_lambda_conv_op (type);
17012 }
17013
17014 finish_struct (type, /*attr*/NULL_TREE);
17015
17016 insert_pending_capture_proxies ();
17017
17018 return r;
17019 }
17020
17021 /* Like tsubst but deals with expressions and performs semantic
17022 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17023
17024 tree
17025 tsubst_copy_and_build (tree t,
17026 tree args,
17027 tsubst_flags_t complain,
17028 tree in_decl,
17029 bool function_p,
17030 bool integral_constant_expression_p)
17031 {
17032 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17033 #define RECUR(NODE) \
17034 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17035 /*function_p=*/false, \
17036 integral_constant_expression_p)
17037
17038 tree retval, op1;
17039 location_t loc;
17040
17041 if (t == NULL_TREE || t == error_mark_node)
17042 return t;
17043
17044 loc = input_location;
17045 if (EXPR_HAS_LOCATION (t))
17046 input_location = EXPR_LOCATION (t);
17047
17048 /* N3276 decltype magic only applies to calls at the top level or on the
17049 right side of a comma. */
17050 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17051 complain &= ~tf_decltype;
17052
17053 switch (TREE_CODE (t))
17054 {
17055 case USING_DECL:
17056 t = DECL_NAME (t);
17057 /* Fall through. */
17058 case IDENTIFIER_NODE:
17059 {
17060 tree decl;
17061 cp_id_kind idk;
17062 bool non_integral_constant_expression_p;
17063 const char *error_msg;
17064
17065 if (IDENTIFIER_CONV_OP_P (t))
17066 {
17067 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17068 t = make_conv_op_name (new_type);
17069 }
17070
17071 /* Look up the name. */
17072 decl = lookup_name (t);
17073
17074 /* By convention, expressions use ERROR_MARK_NODE to indicate
17075 failure, not NULL_TREE. */
17076 if (decl == NULL_TREE)
17077 decl = error_mark_node;
17078
17079 decl = finish_id_expression (t, decl, NULL_TREE,
17080 &idk,
17081 integral_constant_expression_p,
17082 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17083 &non_integral_constant_expression_p,
17084 /*template_p=*/false,
17085 /*done=*/true,
17086 /*address_p=*/false,
17087 /*template_arg_p=*/false,
17088 &error_msg,
17089 input_location);
17090 if (error_msg)
17091 error (error_msg);
17092 if (!function_p && identifier_p (decl))
17093 {
17094 if (complain & tf_error)
17095 unqualified_name_lookup_error (decl);
17096 decl = error_mark_node;
17097 }
17098 RETURN (decl);
17099 }
17100
17101 case TEMPLATE_ID_EXPR:
17102 {
17103 tree object;
17104 tree templ = RECUR (TREE_OPERAND (t, 0));
17105 tree targs = TREE_OPERAND (t, 1);
17106
17107 if (targs)
17108 targs = tsubst_template_args (targs, args, complain, in_decl);
17109 if (targs == error_mark_node)
17110 return error_mark_node;
17111
17112 if (TREE_CODE (templ) == SCOPE_REF)
17113 {
17114 tree name = TREE_OPERAND (templ, 1);
17115 tree tid = lookup_template_function (name, targs);
17116 TREE_OPERAND (templ, 1) = tid;
17117 return templ;
17118 }
17119
17120 if (variable_template_p (templ))
17121 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17122
17123 if (TREE_CODE (templ) == COMPONENT_REF)
17124 {
17125 object = TREE_OPERAND (templ, 0);
17126 templ = TREE_OPERAND (templ, 1);
17127 }
17128 else
17129 object = NULL_TREE;
17130 templ = lookup_template_function (templ, targs);
17131
17132 if (object)
17133 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17134 object, templ, NULL_TREE));
17135 else
17136 RETURN (baselink_for_fns (templ));
17137 }
17138
17139 case INDIRECT_REF:
17140 {
17141 tree r = RECUR (TREE_OPERAND (t, 0));
17142
17143 if (REFERENCE_REF_P (t))
17144 {
17145 /* A type conversion to reference type will be enclosed in
17146 such an indirect ref, but the substitution of the cast
17147 will have also added such an indirect ref. */
17148 r = convert_from_reference (r);
17149 }
17150 else
17151 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17152 complain|decltype_flag);
17153
17154 if (TREE_CODE (r) == INDIRECT_REF)
17155 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17156
17157 RETURN (r);
17158 }
17159
17160 case NOP_EXPR:
17161 {
17162 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17163 tree op0 = RECUR (TREE_OPERAND (t, 0));
17164 RETURN (build_nop (type, op0));
17165 }
17166
17167 case IMPLICIT_CONV_EXPR:
17168 {
17169 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17170 tree expr = RECUR (TREE_OPERAND (t, 0));
17171 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17172 {
17173 retval = copy_node (t);
17174 TREE_TYPE (retval) = type;
17175 TREE_OPERAND (retval, 0) = expr;
17176 RETURN (retval);
17177 }
17178 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17179 /* We'll pass this to convert_nontype_argument again, we don't need
17180 to actually perform any conversion here. */
17181 RETURN (expr);
17182 int flags = LOOKUP_IMPLICIT;
17183 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17184 flags = LOOKUP_NORMAL;
17185 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17186 flags));
17187 }
17188
17189 case CONVERT_EXPR:
17190 {
17191 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17192 tree op0 = RECUR (TREE_OPERAND (t, 0));
17193 RETURN (build1 (CONVERT_EXPR, type, op0));
17194 }
17195
17196 case CAST_EXPR:
17197 case REINTERPRET_CAST_EXPR:
17198 case CONST_CAST_EXPR:
17199 case DYNAMIC_CAST_EXPR:
17200 case STATIC_CAST_EXPR:
17201 {
17202 tree type;
17203 tree op, r = NULL_TREE;
17204
17205 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17206 if (integral_constant_expression_p
17207 && !cast_valid_in_integral_constant_expression_p (type))
17208 {
17209 if (complain & tf_error)
17210 error ("a cast to a type other than an integral or "
17211 "enumeration type cannot appear in a constant-expression");
17212 RETURN (error_mark_node);
17213 }
17214
17215 op = RECUR (TREE_OPERAND (t, 0));
17216
17217 warning_sentinel s(warn_useless_cast);
17218 warning_sentinel s2(warn_ignored_qualifiers);
17219 switch (TREE_CODE (t))
17220 {
17221 case CAST_EXPR:
17222 r = build_functional_cast (type, op, complain);
17223 break;
17224 case REINTERPRET_CAST_EXPR:
17225 r = build_reinterpret_cast (type, op, complain);
17226 break;
17227 case CONST_CAST_EXPR:
17228 r = build_const_cast (type, op, complain);
17229 break;
17230 case DYNAMIC_CAST_EXPR:
17231 r = build_dynamic_cast (type, op, complain);
17232 break;
17233 case STATIC_CAST_EXPR:
17234 r = build_static_cast (type, op, complain);
17235 break;
17236 default:
17237 gcc_unreachable ();
17238 }
17239
17240 RETURN (r);
17241 }
17242
17243 case POSTDECREMENT_EXPR:
17244 case POSTINCREMENT_EXPR:
17245 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17246 args, complain, in_decl);
17247 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17248 complain|decltype_flag));
17249
17250 case PREDECREMENT_EXPR:
17251 case PREINCREMENT_EXPR:
17252 case NEGATE_EXPR:
17253 case BIT_NOT_EXPR:
17254 case ABS_EXPR:
17255 case TRUTH_NOT_EXPR:
17256 case UNARY_PLUS_EXPR: /* Unary + */
17257 case REALPART_EXPR:
17258 case IMAGPART_EXPR:
17259 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17260 RECUR (TREE_OPERAND (t, 0)),
17261 complain|decltype_flag));
17262
17263 case FIX_TRUNC_EXPR:
17264 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17265 false, complain));
17266
17267 case ADDR_EXPR:
17268 op1 = TREE_OPERAND (t, 0);
17269 if (TREE_CODE (op1) == LABEL_DECL)
17270 RETURN (finish_label_address_expr (DECL_NAME (op1),
17271 EXPR_LOCATION (op1)));
17272 if (TREE_CODE (op1) == SCOPE_REF)
17273 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17274 /*done=*/true, /*address_p=*/true);
17275 else
17276 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17277 in_decl);
17278 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17279 complain|decltype_flag));
17280
17281 case PLUS_EXPR:
17282 case MINUS_EXPR:
17283 case MULT_EXPR:
17284 case TRUNC_DIV_EXPR:
17285 case CEIL_DIV_EXPR:
17286 case FLOOR_DIV_EXPR:
17287 case ROUND_DIV_EXPR:
17288 case EXACT_DIV_EXPR:
17289 case BIT_AND_EXPR:
17290 case BIT_IOR_EXPR:
17291 case BIT_XOR_EXPR:
17292 case TRUNC_MOD_EXPR:
17293 case FLOOR_MOD_EXPR:
17294 case TRUTH_ANDIF_EXPR:
17295 case TRUTH_ORIF_EXPR:
17296 case TRUTH_AND_EXPR:
17297 case TRUTH_OR_EXPR:
17298 case RSHIFT_EXPR:
17299 case LSHIFT_EXPR:
17300 case RROTATE_EXPR:
17301 case LROTATE_EXPR:
17302 case EQ_EXPR:
17303 case NE_EXPR:
17304 case MAX_EXPR:
17305 case MIN_EXPR:
17306 case LE_EXPR:
17307 case GE_EXPR:
17308 case LT_EXPR:
17309 case GT_EXPR:
17310 case MEMBER_REF:
17311 case DOTSTAR_EXPR:
17312 {
17313 warning_sentinel s1(warn_type_limits);
17314 warning_sentinel s2(warn_div_by_zero);
17315 warning_sentinel s3(warn_logical_op);
17316 warning_sentinel s4(warn_tautological_compare);
17317 tree op0 = RECUR (TREE_OPERAND (t, 0));
17318 tree op1 = RECUR (TREE_OPERAND (t, 1));
17319 tree r = build_x_binary_op
17320 (input_location, TREE_CODE (t),
17321 op0,
17322 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17323 ? ERROR_MARK
17324 : TREE_CODE (TREE_OPERAND (t, 0))),
17325 op1,
17326 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17327 ? ERROR_MARK
17328 : TREE_CODE (TREE_OPERAND (t, 1))),
17329 /*overload=*/NULL,
17330 complain|decltype_flag);
17331 if (EXPR_P (r) && TREE_NO_WARNING (t))
17332 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17333
17334 RETURN (r);
17335 }
17336
17337 case POINTER_PLUS_EXPR:
17338 {
17339 tree op0 = RECUR (TREE_OPERAND (t, 0));
17340 tree op1 = RECUR (TREE_OPERAND (t, 1));
17341 return fold_build_pointer_plus (op0, op1);
17342 }
17343
17344 case SCOPE_REF:
17345 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17346 /*address_p=*/false));
17347 case ARRAY_REF:
17348 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17349 args, complain, in_decl);
17350 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17351 RECUR (TREE_OPERAND (t, 1)),
17352 complain|decltype_flag));
17353
17354 case SIZEOF_EXPR:
17355 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17356 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17357 RETURN (tsubst_copy (t, args, complain, in_decl));
17358 /* Fall through */
17359
17360 case ALIGNOF_EXPR:
17361 {
17362 tree r;
17363
17364 op1 = TREE_OPERAND (t, 0);
17365 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17366 op1 = TREE_TYPE (op1);
17367 if (!args)
17368 {
17369 /* When there are no ARGS, we are trying to evaluate a
17370 non-dependent expression from the parser. Trying to do
17371 the substitutions may not work. */
17372 if (!TYPE_P (op1))
17373 op1 = TREE_TYPE (op1);
17374 }
17375 else
17376 {
17377 ++cp_unevaluated_operand;
17378 ++c_inhibit_evaluation_warnings;
17379 if (TYPE_P (op1))
17380 op1 = tsubst (op1, args, complain, in_decl);
17381 else
17382 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17383 /*function_p=*/false,
17384 /*integral_constant_expression_p=*/
17385 false);
17386 --cp_unevaluated_operand;
17387 --c_inhibit_evaluation_warnings;
17388 }
17389 if (TYPE_P (op1))
17390 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17391 complain & tf_error);
17392 else
17393 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17394 complain & tf_error);
17395 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17396 {
17397 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17398 {
17399 if (!processing_template_decl && TYPE_P (op1))
17400 {
17401 r = build_min (SIZEOF_EXPR, size_type_node,
17402 build1 (NOP_EXPR, op1, error_mark_node));
17403 SIZEOF_EXPR_TYPE_P (r) = 1;
17404 }
17405 else
17406 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17407 TREE_SIDE_EFFECTS (r) = 0;
17408 TREE_READONLY (r) = 1;
17409 }
17410 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17411 }
17412 RETURN (r);
17413 }
17414
17415 case AT_ENCODE_EXPR:
17416 {
17417 op1 = TREE_OPERAND (t, 0);
17418 ++cp_unevaluated_operand;
17419 ++c_inhibit_evaluation_warnings;
17420 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17421 /*function_p=*/false,
17422 /*integral_constant_expression_p=*/false);
17423 --cp_unevaluated_operand;
17424 --c_inhibit_evaluation_warnings;
17425 RETURN (objc_build_encode_expr (op1));
17426 }
17427
17428 case NOEXCEPT_EXPR:
17429 op1 = TREE_OPERAND (t, 0);
17430 ++cp_unevaluated_operand;
17431 ++c_inhibit_evaluation_warnings;
17432 ++cp_noexcept_operand;
17433 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17434 /*function_p=*/false,
17435 /*integral_constant_expression_p=*/false);
17436 --cp_unevaluated_operand;
17437 --c_inhibit_evaluation_warnings;
17438 --cp_noexcept_operand;
17439 RETURN (finish_noexcept_expr (op1, complain));
17440
17441 case MODOP_EXPR:
17442 {
17443 warning_sentinel s(warn_div_by_zero);
17444 tree lhs = RECUR (TREE_OPERAND (t, 0));
17445 tree rhs = RECUR (TREE_OPERAND (t, 2));
17446 tree r = build_x_modify_expr
17447 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17448 complain|decltype_flag);
17449 /* TREE_NO_WARNING must be set if either the expression was
17450 parenthesized or it uses an operator such as >>= rather
17451 than plain assignment. In the former case, it was already
17452 set and must be copied. In the latter case,
17453 build_x_modify_expr sets it and it must not be reset
17454 here. */
17455 if (TREE_NO_WARNING (t))
17456 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17457
17458 RETURN (r);
17459 }
17460
17461 case ARROW_EXPR:
17462 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17463 args, complain, in_decl);
17464 /* Remember that there was a reference to this entity. */
17465 if (DECL_P (op1)
17466 && !mark_used (op1, complain) && !(complain & tf_error))
17467 RETURN (error_mark_node);
17468 RETURN (build_x_arrow (input_location, op1, complain));
17469
17470 case NEW_EXPR:
17471 {
17472 tree placement = RECUR (TREE_OPERAND (t, 0));
17473 tree init = RECUR (TREE_OPERAND (t, 3));
17474 vec<tree, va_gc> *placement_vec;
17475 vec<tree, va_gc> *init_vec;
17476 tree ret;
17477
17478 if (placement == NULL_TREE)
17479 placement_vec = NULL;
17480 else
17481 {
17482 placement_vec = make_tree_vector ();
17483 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17484 vec_safe_push (placement_vec, TREE_VALUE (placement));
17485 }
17486
17487 /* If there was an initializer in the original tree, but it
17488 instantiated to an empty list, then we should pass a
17489 non-NULL empty vector to tell build_new that it was an
17490 empty initializer() rather than no initializer. This can
17491 only happen when the initializer is a pack expansion whose
17492 parameter packs are of length zero. */
17493 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17494 init_vec = NULL;
17495 else
17496 {
17497 init_vec = make_tree_vector ();
17498 if (init == void_node)
17499 gcc_assert (init_vec != NULL);
17500 else
17501 {
17502 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17503 vec_safe_push (init_vec, TREE_VALUE (init));
17504 }
17505 }
17506
17507 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17508 tree op2 = RECUR (TREE_OPERAND (t, 2));
17509 ret = build_new (&placement_vec, op1, op2, &init_vec,
17510 NEW_EXPR_USE_GLOBAL (t),
17511 complain);
17512
17513 if (placement_vec != NULL)
17514 release_tree_vector (placement_vec);
17515 if (init_vec != NULL)
17516 release_tree_vector (init_vec);
17517
17518 RETURN (ret);
17519 }
17520
17521 case DELETE_EXPR:
17522 {
17523 tree op0 = RECUR (TREE_OPERAND (t, 0));
17524 tree op1 = RECUR (TREE_OPERAND (t, 1));
17525 RETURN (delete_sanity (op0, op1,
17526 DELETE_EXPR_USE_VEC (t),
17527 DELETE_EXPR_USE_GLOBAL (t),
17528 complain));
17529 }
17530
17531 case COMPOUND_EXPR:
17532 {
17533 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17534 complain & ~tf_decltype, in_decl,
17535 /*function_p=*/false,
17536 integral_constant_expression_p);
17537 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17538 op0,
17539 RECUR (TREE_OPERAND (t, 1)),
17540 complain|decltype_flag));
17541 }
17542
17543 case CALL_EXPR:
17544 {
17545 tree function;
17546 vec<tree, va_gc> *call_args;
17547 unsigned int nargs, i;
17548 bool qualified_p;
17549 bool koenig_p;
17550 tree ret;
17551
17552 function = CALL_EXPR_FN (t);
17553 /* Internal function with no arguments. */
17554 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17555 RETURN (t);
17556
17557 /* When we parsed the expression, we determined whether or
17558 not Koenig lookup should be performed. */
17559 koenig_p = KOENIG_LOOKUP_P (t);
17560 if (function == NULL_TREE)
17561 {
17562 koenig_p = false;
17563 qualified_p = false;
17564 }
17565 else if (TREE_CODE (function) == SCOPE_REF)
17566 {
17567 qualified_p = true;
17568 function = tsubst_qualified_id (function, args, complain, in_decl,
17569 /*done=*/false,
17570 /*address_p=*/false);
17571 }
17572 else if (koenig_p && identifier_p (function))
17573 {
17574 /* Do nothing; calling tsubst_copy_and_build on an identifier
17575 would incorrectly perform unqualified lookup again.
17576
17577 Note that we can also have an IDENTIFIER_NODE if the earlier
17578 unqualified lookup found a member function; in that case
17579 koenig_p will be false and we do want to do the lookup
17580 again to find the instantiated member function.
17581
17582 FIXME but doing that causes c++/15272, so we need to stop
17583 using IDENTIFIER_NODE in that situation. */
17584 qualified_p = false;
17585 }
17586 else
17587 {
17588 if (TREE_CODE (function) == COMPONENT_REF)
17589 {
17590 tree op = TREE_OPERAND (function, 1);
17591
17592 qualified_p = (TREE_CODE (op) == SCOPE_REF
17593 || (BASELINK_P (op)
17594 && BASELINK_QUALIFIED_P (op)));
17595 }
17596 else
17597 qualified_p = false;
17598
17599 if (TREE_CODE (function) == ADDR_EXPR
17600 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17601 /* Avoid error about taking the address of a constructor. */
17602 function = TREE_OPERAND (function, 0);
17603
17604 function = tsubst_copy_and_build (function, args, complain,
17605 in_decl,
17606 !qualified_p,
17607 integral_constant_expression_p);
17608
17609 if (BASELINK_P (function))
17610 qualified_p = true;
17611 }
17612
17613 nargs = call_expr_nargs (t);
17614 call_args = make_tree_vector ();
17615 for (i = 0; i < nargs; ++i)
17616 {
17617 tree arg = CALL_EXPR_ARG (t, i);
17618
17619 if (!PACK_EXPANSION_P (arg))
17620 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17621 else
17622 {
17623 /* Expand the pack expansion and push each entry onto
17624 CALL_ARGS. */
17625 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17626 if (TREE_CODE (arg) == TREE_VEC)
17627 {
17628 unsigned int len, j;
17629
17630 len = TREE_VEC_LENGTH (arg);
17631 for (j = 0; j < len; ++j)
17632 {
17633 tree value = TREE_VEC_ELT (arg, j);
17634 if (value != NULL_TREE)
17635 value = convert_from_reference (value);
17636 vec_safe_push (call_args, value);
17637 }
17638 }
17639 else
17640 {
17641 /* A partial substitution. Add one entry. */
17642 vec_safe_push (call_args, arg);
17643 }
17644 }
17645 }
17646
17647 /* We do not perform argument-dependent lookup if normal
17648 lookup finds a non-function, in accordance with the
17649 expected resolution of DR 218. */
17650 if (koenig_p
17651 && ((is_overloaded_fn (function)
17652 /* If lookup found a member function, the Koenig lookup is
17653 not appropriate, even if an unqualified-name was used
17654 to denote the function. */
17655 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17656 || identifier_p (function))
17657 /* Only do this when substitution turns a dependent call
17658 into a non-dependent call. */
17659 && type_dependent_expression_p_push (t)
17660 && !any_type_dependent_arguments_p (call_args))
17661 function = perform_koenig_lookup (function, call_args, tf_none);
17662
17663 if (function != NULL_TREE
17664 && identifier_p (function)
17665 && !any_type_dependent_arguments_p (call_args))
17666 {
17667 if (koenig_p && (complain & tf_warning_or_error))
17668 {
17669 /* For backwards compatibility and good diagnostics, try
17670 the unqualified lookup again if we aren't in SFINAE
17671 context. */
17672 tree unq = (tsubst_copy_and_build
17673 (function, args, complain, in_decl, true,
17674 integral_constant_expression_p));
17675 if (unq == error_mark_node)
17676 {
17677 release_tree_vector (call_args);
17678 RETURN (error_mark_node);
17679 }
17680
17681 if (unq != function)
17682 {
17683 /* In a lambda fn, we have to be careful to not
17684 introduce new this captures. Legacy code can't
17685 be using lambdas anyway, so it's ok to be
17686 stricter. */
17687 bool in_lambda = (current_class_type
17688 && LAMBDA_TYPE_P (current_class_type));
17689 char const *const msg
17690 = G_("%qD was not declared in this scope, "
17691 "and no declarations were found by "
17692 "argument-dependent lookup at the point "
17693 "of instantiation");
17694
17695 bool diag = true;
17696 if (in_lambda)
17697 error_at (EXPR_LOC_OR_LOC (t, input_location),
17698 msg, function);
17699 else
17700 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17701 msg, function);
17702 if (diag)
17703 {
17704 tree fn = unq;
17705
17706 if (INDIRECT_REF_P (fn))
17707 fn = TREE_OPERAND (fn, 0);
17708 if (is_overloaded_fn (fn))
17709 fn = get_first_fn (fn);
17710
17711 if (!DECL_P (fn))
17712 /* Can't say anything more. */;
17713 else if (DECL_CLASS_SCOPE_P (fn))
17714 {
17715 location_t loc = EXPR_LOC_OR_LOC (t,
17716 input_location);
17717 inform (loc,
17718 "declarations in dependent base %qT are "
17719 "not found by unqualified lookup",
17720 DECL_CLASS_CONTEXT (fn));
17721 if (current_class_ptr)
17722 inform (loc,
17723 "use %<this->%D%> instead", function);
17724 else
17725 inform (loc,
17726 "use %<%T::%D%> instead",
17727 current_class_name, function);
17728 }
17729 else
17730 inform (DECL_SOURCE_LOCATION (fn),
17731 "%qD declared here, later in the "
17732 "translation unit", fn);
17733 if (in_lambda)
17734 {
17735 release_tree_vector (call_args);
17736 RETURN (error_mark_node);
17737 }
17738 }
17739
17740 function = unq;
17741 }
17742 }
17743 if (identifier_p (function))
17744 {
17745 if (complain & tf_error)
17746 unqualified_name_lookup_error (function);
17747 release_tree_vector (call_args);
17748 RETURN (error_mark_node);
17749 }
17750 }
17751
17752 /* Remember that there was a reference to this entity. */
17753 if (function != NULL_TREE
17754 && DECL_P (function)
17755 && !mark_used (function, complain) && !(complain & tf_error))
17756 {
17757 release_tree_vector (call_args);
17758 RETURN (error_mark_node);
17759 }
17760
17761 /* Put back tf_decltype for the actual call. */
17762 complain |= decltype_flag;
17763
17764 if (function == NULL_TREE)
17765 switch (CALL_EXPR_IFN (t))
17766 {
17767 case IFN_LAUNDER:
17768 gcc_assert (nargs == 1);
17769 if (vec_safe_length (call_args) != 1)
17770 {
17771 error_at (EXPR_LOC_OR_LOC (t, input_location),
17772 "wrong number of arguments to "
17773 "%<__builtin_launder%>");
17774 ret = error_mark_node;
17775 }
17776 else
17777 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17778 input_location),
17779 (*call_args)[0], complain);
17780 break;
17781
17782 default:
17783 /* Unsupported internal function with arguments. */
17784 gcc_unreachable ();
17785 }
17786 else if (TREE_CODE (function) == OFFSET_REF)
17787 ret = build_offset_ref_call_from_tree (function, &call_args,
17788 complain);
17789 else if (TREE_CODE (function) == COMPONENT_REF)
17790 {
17791 tree instance = TREE_OPERAND (function, 0);
17792 tree fn = TREE_OPERAND (function, 1);
17793
17794 if (processing_template_decl
17795 && (type_dependent_expression_p (instance)
17796 || (!BASELINK_P (fn)
17797 && TREE_CODE (fn) != FIELD_DECL)
17798 || type_dependent_expression_p (fn)
17799 || any_type_dependent_arguments_p (call_args)))
17800 ret = build_min_nt_call_vec (function, call_args);
17801 else if (!BASELINK_P (fn))
17802 ret = finish_call_expr (function, &call_args,
17803 /*disallow_virtual=*/false,
17804 /*koenig_p=*/false,
17805 complain);
17806 else
17807 ret = (build_new_method_call
17808 (instance, fn,
17809 &call_args, NULL_TREE,
17810 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17811 /*fn_p=*/NULL,
17812 complain));
17813 }
17814 else
17815 ret = finish_call_expr (function, &call_args,
17816 /*disallow_virtual=*/qualified_p,
17817 koenig_p,
17818 complain);
17819
17820 release_tree_vector (call_args);
17821
17822 if (ret != error_mark_node)
17823 {
17824 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17825 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17826 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17827 bool thk = CALL_FROM_THUNK_P (t);
17828 if (op || ord || rev || thk)
17829 {
17830 function = extract_call_expr (ret);
17831 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17832 CALL_EXPR_ORDERED_ARGS (function) = ord;
17833 CALL_EXPR_REVERSE_ARGS (function) = rev;
17834 if (thk)
17835 {
17836 if (TREE_CODE (function) == CALL_EXPR)
17837 CALL_FROM_THUNK_P (function) = true;
17838 else
17839 AGGR_INIT_FROM_THUNK_P (function) = true;
17840 /* The thunk location is not interesting. */
17841 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17842 }
17843 }
17844 }
17845
17846 RETURN (ret);
17847 }
17848
17849 case COND_EXPR:
17850 {
17851 tree cond = RECUR (TREE_OPERAND (t, 0));
17852 tree folded_cond = fold_non_dependent_expr (cond);
17853 tree exp1, exp2;
17854
17855 if (TREE_CODE (folded_cond) == INTEGER_CST)
17856 {
17857 if (integer_zerop (folded_cond))
17858 {
17859 ++c_inhibit_evaluation_warnings;
17860 exp1 = RECUR (TREE_OPERAND (t, 1));
17861 --c_inhibit_evaluation_warnings;
17862 exp2 = RECUR (TREE_OPERAND (t, 2));
17863 }
17864 else
17865 {
17866 exp1 = RECUR (TREE_OPERAND (t, 1));
17867 ++c_inhibit_evaluation_warnings;
17868 exp2 = RECUR (TREE_OPERAND (t, 2));
17869 --c_inhibit_evaluation_warnings;
17870 }
17871 cond = folded_cond;
17872 }
17873 else
17874 {
17875 exp1 = RECUR (TREE_OPERAND (t, 1));
17876 exp2 = RECUR (TREE_OPERAND (t, 2));
17877 }
17878
17879 warning_sentinel s(warn_duplicated_branches);
17880 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17881 cond, exp1, exp2, complain));
17882 }
17883
17884 case PSEUDO_DTOR_EXPR:
17885 {
17886 tree op0 = RECUR (TREE_OPERAND (t, 0));
17887 tree op1 = RECUR (TREE_OPERAND (t, 1));
17888 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17889 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17890 input_location));
17891 }
17892
17893 case TREE_LIST:
17894 {
17895 tree purpose, value, chain;
17896
17897 if (t == void_list_node)
17898 RETURN (t);
17899
17900 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17901 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17902 {
17903 /* We have pack expansions, so expand those and
17904 create a new list out of it. */
17905 tree purposevec = NULL_TREE;
17906 tree valuevec = NULL_TREE;
17907 tree chain;
17908 int i, len = -1;
17909
17910 /* Expand the argument expressions. */
17911 if (TREE_PURPOSE (t))
17912 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17913 complain, in_decl);
17914 if (TREE_VALUE (t))
17915 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17916 complain, in_decl);
17917
17918 /* Build the rest of the list. */
17919 chain = TREE_CHAIN (t);
17920 if (chain && chain != void_type_node)
17921 chain = RECUR (chain);
17922
17923 /* Determine the number of arguments. */
17924 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17925 {
17926 len = TREE_VEC_LENGTH (purposevec);
17927 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17928 }
17929 else if (TREE_CODE (valuevec) == TREE_VEC)
17930 len = TREE_VEC_LENGTH (valuevec);
17931 else
17932 {
17933 /* Since we only performed a partial substitution into
17934 the argument pack, we only RETURN (a single list
17935 node. */
17936 if (purposevec == TREE_PURPOSE (t)
17937 && valuevec == TREE_VALUE (t)
17938 && chain == TREE_CHAIN (t))
17939 RETURN (t);
17940
17941 RETURN (tree_cons (purposevec, valuevec, chain));
17942 }
17943
17944 /* Convert the argument vectors into a TREE_LIST */
17945 i = len;
17946 while (i > 0)
17947 {
17948 /* Grab the Ith values. */
17949 i--;
17950 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17951 : NULL_TREE;
17952 value
17953 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17954 : NULL_TREE;
17955
17956 /* Build the list (backwards). */
17957 chain = tree_cons (purpose, value, chain);
17958 }
17959
17960 RETURN (chain);
17961 }
17962
17963 purpose = TREE_PURPOSE (t);
17964 if (purpose)
17965 purpose = RECUR (purpose);
17966 value = TREE_VALUE (t);
17967 if (value)
17968 value = RECUR (value);
17969 chain = TREE_CHAIN (t);
17970 if (chain && chain != void_type_node)
17971 chain = RECUR (chain);
17972 if (purpose == TREE_PURPOSE (t)
17973 && value == TREE_VALUE (t)
17974 && chain == TREE_CHAIN (t))
17975 RETURN (t);
17976 RETURN (tree_cons (purpose, value, chain));
17977 }
17978
17979 case COMPONENT_REF:
17980 {
17981 tree object;
17982 tree object_type;
17983 tree member;
17984 tree r;
17985
17986 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17987 args, complain, in_decl);
17988 /* Remember that there was a reference to this entity. */
17989 if (DECL_P (object)
17990 && !mark_used (object, complain) && !(complain & tf_error))
17991 RETURN (error_mark_node);
17992 object_type = TREE_TYPE (object);
17993
17994 member = TREE_OPERAND (t, 1);
17995 if (BASELINK_P (member))
17996 member = tsubst_baselink (member,
17997 non_reference (TREE_TYPE (object)),
17998 args, complain, in_decl);
17999 else
18000 member = tsubst_copy (member, args, complain, in_decl);
18001 if (member == error_mark_node)
18002 RETURN (error_mark_node);
18003
18004 if (TREE_CODE (member) == FIELD_DECL)
18005 {
18006 r = finish_non_static_data_member (member, object, NULL_TREE);
18007 if (TREE_CODE (r) == COMPONENT_REF)
18008 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18009 RETURN (r);
18010 }
18011 else if (type_dependent_expression_p (object))
18012 /* We can't do much here. */;
18013 else if (!CLASS_TYPE_P (object_type))
18014 {
18015 if (scalarish_type_p (object_type))
18016 {
18017 tree s = NULL_TREE;
18018 tree dtor = member;
18019
18020 if (TREE_CODE (dtor) == SCOPE_REF)
18021 {
18022 s = TREE_OPERAND (dtor, 0);
18023 dtor = TREE_OPERAND (dtor, 1);
18024 }
18025 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18026 {
18027 dtor = TREE_OPERAND (dtor, 0);
18028 if (TYPE_P (dtor))
18029 RETURN (finish_pseudo_destructor_expr
18030 (object, s, dtor, input_location));
18031 }
18032 }
18033 }
18034 else if (TREE_CODE (member) == SCOPE_REF
18035 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18036 {
18037 /* Lookup the template functions now that we know what the
18038 scope is. */
18039 tree scope = TREE_OPERAND (member, 0);
18040 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18041 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18042 member = lookup_qualified_name (scope, tmpl,
18043 /*is_type_p=*/false,
18044 /*complain=*/false);
18045 if (BASELINK_P (member))
18046 {
18047 BASELINK_FUNCTIONS (member)
18048 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18049 args);
18050 member = (adjust_result_of_qualified_name_lookup
18051 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18052 object_type));
18053 }
18054 else
18055 {
18056 qualified_name_lookup_error (scope, tmpl, member,
18057 input_location);
18058 RETURN (error_mark_node);
18059 }
18060 }
18061 else if (TREE_CODE (member) == SCOPE_REF
18062 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18063 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18064 {
18065 if (complain & tf_error)
18066 {
18067 if (TYPE_P (TREE_OPERAND (member, 0)))
18068 error ("%qT is not a class or namespace",
18069 TREE_OPERAND (member, 0));
18070 else
18071 error ("%qD is not a class or namespace",
18072 TREE_OPERAND (member, 0));
18073 }
18074 RETURN (error_mark_node);
18075 }
18076
18077 r = finish_class_member_access_expr (object, member,
18078 /*template_p=*/false,
18079 complain);
18080 if (TREE_CODE (r) == COMPONENT_REF)
18081 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18082 RETURN (r);
18083 }
18084
18085 case THROW_EXPR:
18086 RETURN (build_throw
18087 (RECUR (TREE_OPERAND (t, 0))));
18088
18089 case CONSTRUCTOR:
18090 {
18091 vec<constructor_elt, va_gc> *n;
18092 constructor_elt *ce;
18093 unsigned HOST_WIDE_INT idx;
18094 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18095 bool process_index_p;
18096 int newlen;
18097 bool need_copy_p = false;
18098 tree r;
18099
18100 if (type == error_mark_node)
18101 RETURN (error_mark_node);
18102
18103 /* digest_init will do the wrong thing if we let it. */
18104 if (type && TYPE_PTRMEMFUNC_P (type))
18105 RETURN (t);
18106
18107 /* We do not want to process the index of aggregate
18108 initializers as they are identifier nodes which will be
18109 looked up by digest_init. */
18110 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18111
18112 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18113 newlen = vec_safe_length (n);
18114 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18115 {
18116 if (ce->index && process_index_p
18117 /* An identifier index is looked up in the type
18118 being initialized, not the current scope. */
18119 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18120 ce->index = RECUR (ce->index);
18121
18122 if (PACK_EXPANSION_P (ce->value))
18123 {
18124 /* Substitute into the pack expansion. */
18125 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18126 in_decl);
18127
18128 if (ce->value == error_mark_node
18129 || PACK_EXPANSION_P (ce->value))
18130 ;
18131 else if (TREE_VEC_LENGTH (ce->value) == 1)
18132 /* Just move the argument into place. */
18133 ce->value = TREE_VEC_ELT (ce->value, 0);
18134 else
18135 {
18136 /* Update the length of the final CONSTRUCTOR
18137 arguments vector, and note that we will need to
18138 copy.*/
18139 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18140 need_copy_p = true;
18141 }
18142 }
18143 else
18144 ce->value = RECUR (ce->value);
18145 }
18146
18147 if (need_copy_p)
18148 {
18149 vec<constructor_elt, va_gc> *old_n = n;
18150
18151 vec_alloc (n, newlen);
18152 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18153 {
18154 if (TREE_CODE (ce->value) == TREE_VEC)
18155 {
18156 int i, len = TREE_VEC_LENGTH (ce->value);
18157 for (i = 0; i < len; ++i)
18158 CONSTRUCTOR_APPEND_ELT (n, 0,
18159 TREE_VEC_ELT (ce->value, i));
18160 }
18161 else
18162 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18163 }
18164 }
18165
18166 r = build_constructor (init_list_type_node, n);
18167 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18168
18169 if (TREE_HAS_CONSTRUCTOR (t))
18170 {
18171 fcl_t cl = fcl_functional;
18172 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18173 cl = fcl_c99;
18174 RETURN (finish_compound_literal (type, r, complain, cl));
18175 }
18176
18177 TREE_TYPE (r) = type;
18178 RETURN (r);
18179 }
18180
18181 case TYPEID_EXPR:
18182 {
18183 tree operand_0 = TREE_OPERAND (t, 0);
18184 if (TYPE_P (operand_0))
18185 {
18186 operand_0 = tsubst (operand_0, args, complain, in_decl);
18187 RETURN (get_typeid (operand_0, complain));
18188 }
18189 else
18190 {
18191 operand_0 = RECUR (operand_0);
18192 RETURN (build_typeid (operand_0, complain));
18193 }
18194 }
18195
18196 case VAR_DECL:
18197 if (!args)
18198 RETURN (t);
18199 else if (DECL_PACK_P (t))
18200 {
18201 /* We don't build decls for an instantiation of a
18202 variadic capture proxy, we instantiate the elements
18203 when needed. */
18204 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18205 return RECUR (DECL_VALUE_EXPR (t));
18206 }
18207 /* Fall through */
18208
18209 case PARM_DECL:
18210 {
18211 tree r = tsubst_copy (t, args, complain, in_decl);
18212 /* ??? We're doing a subset of finish_id_expression here. */
18213 if (VAR_P (r)
18214 && !processing_template_decl
18215 && !cp_unevaluated_operand
18216 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18217 && CP_DECL_THREAD_LOCAL_P (r))
18218 {
18219 if (tree wrap = get_tls_wrapper_fn (r))
18220 /* Replace an evaluated use of the thread_local variable with
18221 a call to its wrapper. */
18222 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18223 }
18224 else if (outer_automatic_var_p (r))
18225 r = process_outer_var_ref (r, complain);
18226
18227 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18228 /* If the original type was a reference, we'll be wrapped in
18229 the appropriate INDIRECT_REF. */
18230 r = convert_from_reference (r);
18231 RETURN (r);
18232 }
18233
18234 case VA_ARG_EXPR:
18235 {
18236 tree op0 = RECUR (TREE_OPERAND (t, 0));
18237 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18238 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18239 }
18240
18241 case OFFSETOF_EXPR:
18242 {
18243 tree object_ptr
18244 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18245 in_decl, /*function_p=*/false,
18246 /*integral_constant_expression_p=*/false);
18247 RETURN (finish_offsetof (object_ptr,
18248 RECUR (TREE_OPERAND (t, 0)),
18249 EXPR_LOCATION (t)));
18250 }
18251
18252 case ADDRESSOF_EXPR:
18253 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18254 RECUR (TREE_OPERAND (t, 0)), complain));
18255
18256 case TRAIT_EXPR:
18257 {
18258 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18259 complain, in_decl);
18260
18261 tree type2 = TRAIT_EXPR_TYPE2 (t);
18262 if (type2 && TREE_CODE (type2) == TREE_LIST)
18263 type2 = RECUR (type2);
18264 else if (type2)
18265 type2 = tsubst (type2, args, complain, in_decl);
18266
18267 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18268 }
18269
18270 case STMT_EXPR:
18271 {
18272 tree old_stmt_expr = cur_stmt_expr;
18273 tree stmt_expr = begin_stmt_expr ();
18274
18275 cur_stmt_expr = stmt_expr;
18276 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18277 integral_constant_expression_p);
18278 stmt_expr = finish_stmt_expr (stmt_expr, false);
18279 cur_stmt_expr = old_stmt_expr;
18280
18281 /* If the resulting list of expression statement is empty,
18282 fold it further into void_node. */
18283 if (empty_expr_stmt_p (stmt_expr))
18284 stmt_expr = void_node;
18285
18286 RETURN (stmt_expr);
18287 }
18288
18289 case LAMBDA_EXPR:
18290 {
18291 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18292
18293 RETURN (build_lambda_object (r));
18294 }
18295
18296 case TARGET_EXPR:
18297 /* We can get here for a constant initializer of non-dependent type.
18298 FIXME stop folding in cp_parser_initializer_clause. */
18299 {
18300 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18301 complain);
18302 RETURN (r);
18303 }
18304
18305 case TRANSACTION_EXPR:
18306 RETURN (tsubst_expr(t, args, complain, in_decl,
18307 integral_constant_expression_p));
18308
18309 case PAREN_EXPR:
18310 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18311
18312 case VEC_PERM_EXPR:
18313 {
18314 tree op0 = RECUR (TREE_OPERAND (t, 0));
18315 tree op1 = RECUR (TREE_OPERAND (t, 1));
18316 tree op2 = RECUR (TREE_OPERAND (t, 2));
18317 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18318 complain));
18319 }
18320
18321 case REQUIRES_EXPR:
18322 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18323
18324 case NON_LVALUE_EXPR:
18325 case VIEW_CONVERT_EXPR:
18326 /* We should only see these for location wrapper nodes, or within
18327 instantiate_non_dependent_expr (when args is NULL_TREE). */
18328 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18329 if (location_wrapper_p (t))
18330 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18331 EXPR_LOCATION (t)));
18332 /* fallthrough. */
18333
18334 default:
18335 /* Handle Objective-C++ constructs, if appropriate. */
18336 {
18337 tree subst
18338 = objcp_tsubst_copy_and_build (t, args, complain,
18339 in_decl, /*function_p=*/false);
18340 if (subst)
18341 RETURN (subst);
18342 }
18343 RETURN (tsubst_copy (t, args, complain, in_decl));
18344 }
18345
18346 #undef RECUR
18347 #undef RETURN
18348 out:
18349 input_location = loc;
18350 return retval;
18351 }
18352
18353 /* Verify that the instantiated ARGS are valid. For type arguments,
18354 make sure that the type's linkage is ok. For non-type arguments,
18355 make sure they are constants if they are integral or enumerations.
18356 Emit an error under control of COMPLAIN, and return TRUE on error. */
18357
18358 static bool
18359 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18360 {
18361 if (dependent_template_arg_p (t))
18362 return false;
18363 if (ARGUMENT_PACK_P (t))
18364 {
18365 tree vec = ARGUMENT_PACK_ARGS (t);
18366 int len = TREE_VEC_LENGTH (vec);
18367 bool result = false;
18368 int i;
18369
18370 for (i = 0; i < len; ++i)
18371 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18372 result = true;
18373 return result;
18374 }
18375 else if (TYPE_P (t))
18376 {
18377 /* [basic.link]: A name with no linkage (notably, the name
18378 of a class or enumeration declared in a local scope)
18379 shall not be used to declare an entity with linkage.
18380 This implies that names with no linkage cannot be used as
18381 template arguments
18382
18383 DR 757 relaxes this restriction for C++0x. */
18384 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18385 : no_linkage_check (t, /*relaxed_p=*/false));
18386
18387 if (nt)
18388 {
18389 /* DR 488 makes use of a type with no linkage cause
18390 type deduction to fail. */
18391 if (complain & tf_error)
18392 {
18393 if (TYPE_UNNAMED_P (nt))
18394 error ("%qT is/uses unnamed type", t);
18395 else
18396 error ("template argument for %qD uses local type %qT",
18397 tmpl, t);
18398 }
18399 return true;
18400 }
18401 /* In order to avoid all sorts of complications, we do not
18402 allow variably-modified types as template arguments. */
18403 else if (variably_modified_type_p (t, NULL_TREE))
18404 {
18405 if (complain & tf_error)
18406 error ("%qT is a variably modified type", t);
18407 return true;
18408 }
18409 }
18410 /* Class template and alias template arguments should be OK. */
18411 else if (DECL_TYPE_TEMPLATE_P (t))
18412 ;
18413 /* A non-type argument of integral or enumerated type must be a
18414 constant. */
18415 else if (TREE_TYPE (t)
18416 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18417 && !REFERENCE_REF_P (t)
18418 && !TREE_CONSTANT (t))
18419 {
18420 if (complain & tf_error)
18421 error ("integral expression %qE is not constant", t);
18422 return true;
18423 }
18424 return false;
18425 }
18426
18427 static bool
18428 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18429 {
18430 int ix, len = DECL_NTPARMS (tmpl);
18431 bool result = false;
18432
18433 for (ix = 0; ix != len; ix++)
18434 {
18435 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18436 result = true;
18437 }
18438 if (result && (complain & tf_error))
18439 error (" trying to instantiate %qD", tmpl);
18440 return result;
18441 }
18442
18443 /* We're out of SFINAE context now, so generate diagnostics for the access
18444 errors we saw earlier when instantiating D from TMPL and ARGS. */
18445
18446 static void
18447 recheck_decl_substitution (tree d, tree tmpl, tree args)
18448 {
18449 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18450 tree type = TREE_TYPE (pattern);
18451 location_t loc = input_location;
18452
18453 push_access_scope (d);
18454 push_deferring_access_checks (dk_no_deferred);
18455 input_location = DECL_SOURCE_LOCATION (pattern);
18456 tsubst (type, args, tf_warning_or_error, d);
18457 input_location = loc;
18458 pop_deferring_access_checks ();
18459 pop_access_scope (d);
18460 }
18461
18462 /* Instantiate the indicated variable, function, or alias template TMPL with
18463 the template arguments in TARG_PTR. */
18464
18465 static tree
18466 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18467 {
18468 tree targ_ptr = orig_args;
18469 tree fndecl;
18470 tree gen_tmpl;
18471 tree spec;
18472 bool access_ok = true;
18473
18474 if (tmpl == error_mark_node)
18475 return error_mark_node;
18476
18477 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18478
18479 /* If this function is a clone, handle it specially. */
18480 if (DECL_CLONED_FUNCTION_P (tmpl))
18481 {
18482 tree spec;
18483 tree clone;
18484
18485 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18486 DECL_CLONED_FUNCTION. */
18487 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18488 targ_ptr, complain);
18489 if (spec == error_mark_node)
18490 return error_mark_node;
18491
18492 /* Look for the clone. */
18493 FOR_EACH_CLONE (clone, spec)
18494 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18495 return clone;
18496 /* We should always have found the clone by now. */
18497 gcc_unreachable ();
18498 return NULL_TREE;
18499 }
18500
18501 if (targ_ptr == error_mark_node)
18502 return error_mark_node;
18503
18504 /* Check to see if we already have this specialization. */
18505 gen_tmpl = most_general_template (tmpl);
18506 if (TMPL_ARGS_DEPTH (targ_ptr)
18507 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18508 /* targ_ptr only has the innermost template args, so add the outer ones
18509 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18510 the case of a non-dependent call within a template definition). */
18511 targ_ptr = (add_outermost_template_args
18512 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18513 targ_ptr));
18514
18515 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18516 but it doesn't seem to be on the hot path. */
18517 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18518
18519 gcc_assert (tmpl == gen_tmpl
18520 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18521 == spec)
18522 || fndecl == NULL_TREE);
18523
18524 if (spec != NULL_TREE)
18525 {
18526 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18527 {
18528 if (complain & tf_error)
18529 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18530 return error_mark_node;
18531 }
18532 return spec;
18533 }
18534
18535 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18536 complain))
18537 return error_mark_node;
18538
18539 /* We are building a FUNCTION_DECL, during which the access of its
18540 parameters and return types have to be checked. However this
18541 FUNCTION_DECL which is the desired context for access checking
18542 is not built yet. We solve this chicken-and-egg problem by
18543 deferring all checks until we have the FUNCTION_DECL. */
18544 push_deferring_access_checks (dk_deferred);
18545
18546 /* Instantiation of the function happens in the context of the function
18547 template, not the context of the overload resolution we're doing. */
18548 push_to_top_level ();
18549 /* If there are dependent arguments, e.g. because we're doing partial
18550 ordering, make sure processing_template_decl stays set. */
18551 if (uses_template_parms (targ_ptr))
18552 ++processing_template_decl;
18553 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18554 {
18555 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18556 complain, gen_tmpl, true);
18557 push_nested_class (ctx);
18558 }
18559
18560 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18561
18562 fndecl = NULL_TREE;
18563 if (VAR_P (pattern))
18564 {
18565 /* We need to determine if we're using a partial or explicit
18566 specialization now, because the type of the variable could be
18567 different. */
18568 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18569 tree elt = most_specialized_partial_spec (tid, complain);
18570 if (elt == error_mark_node)
18571 pattern = error_mark_node;
18572 else if (elt)
18573 {
18574 tree partial_tmpl = TREE_VALUE (elt);
18575 tree partial_args = TREE_PURPOSE (elt);
18576 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18577 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18578 }
18579 }
18580
18581 /* Substitute template parameters to obtain the specialization. */
18582 if (fndecl == NULL_TREE)
18583 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18584 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18585 pop_nested_class ();
18586 pop_from_top_level ();
18587
18588 if (fndecl == error_mark_node)
18589 {
18590 pop_deferring_access_checks ();
18591 return error_mark_node;
18592 }
18593
18594 /* The DECL_TI_TEMPLATE should always be the immediate parent
18595 template, not the most general template. */
18596 DECL_TI_TEMPLATE (fndecl) = tmpl;
18597 DECL_TI_ARGS (fndecl) = targ_ptr;
18598
18599 /* Now we know the specialization, compute access previously
18600 deferred. Do no access control for inheriting constructors,
18601 as we already checked access for the inherited constructor. */
18602 if (!(flag_new_inheriting_ctors
18603 && DECL_INHERITED_CTOR (fndecl)))
18604 {
18605 push_access_scope (fndecl);
18606 if (!perform_deferred_access_checks (complain))
18607 access_ok = false;
18608 pop_access_scope (fndecl);
18609 }
18610 pop_deferring_access_checks ();
18611
18612 /* If we've just instantiated the main entry point for a function,
18613 instantiate all the alternate entry points as well. We do this
18614 by cloning the instantiation of the main entry point, not by
18615 instantiating the template clones. */
18616 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18617 clone_function_decl (fndecl, /*update_methods=*/false);
18618
18619 if (!access_ok)
18620 {
18621 if (!(complain & tf_error))
18622 {
18623 /* Remember to reinstantiate when we're out of SFINAE so the user
18624 can see the errors. */
18625 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18626 }
18627 return error_mark_node;
18628 }
18629 return fndecl;
18630 }
18631
18632 /* Wrapper for instantiate_template_1. */
18633
18634 tree
18635 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18636 {
18637 tree ret;
18638 timevar_push (TV_TEMPLATE_INST);
18639 ret = instantiate_template_1 (tmpl, orig_args, complain);
18640 timevar_pop (TV_TEMPLATE_INST);
18641 return ret;
18642 }
18643
18644 /* Instantiate the alias template TMPL with ARGS. Also push a template
18645 instantiation level, which instantiate_template doesn't do because
18646 functions and variables have sufficient context established by the
18647 callers. */
18648
18649 static tree
18650 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18651 {
18652 struct pending_template *old_last_pend = last_pending_template;
18653 struct tinst_level *old_error_tinst = last_error_tinst_level;
18654 if (tmpl == error_mark_node || args == error_mark_node)
18655 return error_mark_node;
18656 tree tinst = build_tree_list (tmpl, args);
18657 if (!push_tinst_level (tinst))
18658 {
18659 ggc_free (tinst);
18660 return error_mark_node;
18661 }
18662
18663 args =
18664 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18665 args, tmpl, complain,
18666 /*require_all_args=*/true,
18667 /*use_default_args=*/true);
18668
18669 tree r = instantiate_template (tmpl, args, complain);
18670 pop_tinst_level ();
18671 /* We can't free this if a pending_template entry or last_error_tinst_level
18672 is pointing at it. */
18673 if (last_pending_template == old_last_pend
18674 && last_error_tinst_level == old_error_tinst)
18675 ggc_free (tinst);
18676
18677 return r;
18678 }
18679
18680 /* PARM is a template parameter pack for FN. Returns true iff
18681 PARM is used in a deducible way in the argument list of FN. */
18682
18683 static bool
18684 pack_deducible_p (tree parm, tree fn)
18685 {
18686 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18687 for (; t; t = TREE_CHAIN (t))
18688 {
18689 tree type = TREE_VALUE (t);
18690 tree packs;
18691 if (!PACK_EXPANSION_P (type))
18692 continue;
18693 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18694 packs; packs = TREE_CHAIN (packs))
18695 if (template_args_equal (TREE_VALUE (packs), parm))
18696 {
18697 /* The template parameter pack is used in a function parameter
18698 pack. If this is the end of the parameter list, the
18699 template parameter pack is deducible. */
18700 if (TREE_CHAIN (t) == void_list_node)
18701 return true;
18702 else
18703 /* Otherwise, not. Well, it could be deduced from
18704 a non-pack parameter, but doing so would end up with
18705 a deduction mismatch, so don't bother. */
18706 return false;
18707 }
18708 }
18709 /* The template parameter pack isn't used in any function parameter
18710 packs, but it might be used deeper, e.g. tuple<Args...>. */
18711 return true;
18712 }
18713
18714 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18715 NARGS elements of the arguments that are being used when calling
18716 it. TARGS is a vector into which the deduced template arguments
18717 are placed.
18718
18719 Returns either a FUNCTION_DECL for the matching specialization of FN or
18720 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18721 true, diagnostics will be printed to explain why it failed.
18722
18723 If FN is a conversion operator, or we are trying to produce a specific
18724 specialization, RETURN_TYPE is the return type desired.
18725
18726 The EXPLICIT_TARGS are explicit template arguments provided via a
18727 template-id.
18728
18729 The parameter STRICT is one of:
18730
18731 DEDUCE_CALL:
18732 We are deducing arguments for a function call, as in
18733 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18734 deducing arguments for a call to the result of a conversion
18735 function template, as in [over.call.object].
18736
18737 DEDUCE_CONV:
18738 We are deducing arguments for a conversion function, as in
18739 [temp.deduct.conv].
18740
18741 DEDUCE_EXACT:
18742 We are deducing arguments when doing an explicit instantiation
18743 as in [temp.explicit], when determining an explicit specialization
18744 as in [temp.expl.spec], or when taking the address of a function
18745 template, as in [temp.deduct.funcaddr]. */
18746
18747 tree
18748 fn_type_unification (tree fn,
18749 tree explicit_targs,
18750 tree targs,
18751 const tree *args,
18752 unsigned int nargs,
18753 tree return_type,
18754 unification_kind_t strict,
18755 int flags,
18756 bool explain_p,
18757 bool decltype_p)
18758 {
18759 tree parms;
18760 tree fntype;
18761 tree decl = NULL_TREE;
18762 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18763 bool ok;
18764 static int deduction_depth;
18765 struct pending_template *old_last_pend = last_pending_template;
18766 struct tinst_level *old_error_tinst = last_error_tinst_level;
18767
18768 tree orig_fn = fn;
18769 if (flag_new_inheriting_ctors)
18770 fn = strip_inheriting_ctors (fn);
18771
18772 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18773 tree tinst;
18774 tree r = error_mark_node;
18775
18776 tree full_targs = targs;
18777 if (TMPL_ARGS_DEPTH (targs)
18778 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18779 full_targs = (add_outermost_template_args
18780 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18781 targs));
18782
18783 if (decltype_p)
18784 complain |= tf_decltype;
18785
18786 /* In C++0x, it's possible to have a function template whose type depends
18787 on itself recursively. This is most obvious with decltype, but can also
18788 occur with enumeration scope (c++/48969). So we need to catch infinite
18789 recursion and reject the substitution at deduction time; this function
18790 will return error_mark_node for any repeated substitution.
18791
18792 This also catches excessive recursion such as when f<N> depends on
18793 f<N-1> across all integers, and returns error_mark_node for all the
18794 substitutions back up to the initial one.
18795
18796 This is, of course, not reentrant. */
18797 if (excessive_deduction_depth)
18798 return error_mark_node;
18799 tinst = build_tree_list (fn, NULL_TREE);
18800 ++deduction_depth;
18801
18802 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18803
18804 fntype = TREE_TYPE (fn);
18805 if (explicit_targs)
18806 {
18807 /* [temp.deduct]
18808
18809 The specified template arguments must match the template
18810 parameters in kind (i.e., type, nontype, template), and there
18811 must not be more arguments than there are parameters;
18812 otherwise type deduction fails.
18813
18814 Nontype arguments must match the types of the corresponding
18815 nontype template parameters, or must be convertible to the
18816 types of the corresponding nontype parameters as specified in
18817 _temp.arg.nontype_, otherwise type deduction fails.
18818
18819 All references in the function type of the function template
18820 to the corresponding template parameters are replaced by the
18821 specified template argument values. If a substitution in a
18822 template parameter or in the function type of the function
18823 template results in an invalid type, type deduction fails. */
18824 int i, len = TREE_VEC_LENGTH (tparms);
18825 location_t loc = input_location;
18826 bool incomplete = false;
18827
18828 if (explicit_targs == error_mark_node)
18829 goto fail;
18830
18831 if (TMPL_ARGS_DEPTH (explicit_targs)
18832 < TMPL_ARGS_DEPTH (full_targs))
18833 explicit_targs = add_outermost_template_args (full_targs,
18834 explicit_targs);
18835
18836 /* Adjust any explicit template arguments before entering the
18837 substitution context. */
18838 explicit_targs
18839 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18840 complain,
18841 /*require_all_args=*/false,
18842 /*use_default_args=*/false));
18843 if (explicit_targs == error_mark_node)
18844 goto fail;
18845
18846 /* Substitute the explicit args into the function type. This is
18847 necessary so that, for instance, explicitly declared function
18848 arguments can match null pointed constants. If we were given
18849 an incomplete set of explicit args, we must not do semantic
18850 processing during substitution as we could create partial
18851 instantiations. */
18852 for (i = 0; i < len; i++)
18853 {
18854 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18855 bool parameter_pack = false;
18856 tree targ = TREE_VEC_ELT (explicit_targs, i);
18857
18858 /* Dig out the actual parm. */
18859 if (TREE_CODE (parm) == TYPE_DECL
18860 || TREE_CODE (parm) == TEMPLATE_DECL)
18861 {
18862 parm = TREE_TYPE (parm);
18863 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18864 }
18865 else if (TREE_CODE (parm) == PARM_DECL)
18866 {
18867 parm = DECL_INITIAL (parm);
18868 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18869 }
18870
18871 if (!parameter_pack && targ == NULL_TREE)
18872 /* No explicit argument for this template parameter. */
18873 incomplete = true;
18874
18875 if (parameter_pack && pack_deducible_p (parm, fn))
18876 {
18877 /* Mark the argument pack as "incomplete". We could
18878 still deduce more arguments during unification.
18879 We remove this mark in type_unification_real. */
18880 if (targ)
18881 {
18882 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18883 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18884 = ARGUMENT_PACK_ARGS (targ);
18885 }
18886
18887 /* We have some incomplete argument packs. */
18888 incomplete = true;
18889 }
18890 }
18891
18892 TREE_VALUE (tinst) = explicit_targs;
18893 if (!push_tinst_level (tinst))
18894 {
18895 excessive_deduction_depth = true;
18896 goto fail;
18897 }
18898 processing_template_decl += incomplete;
18899 input_location = DECL_SOURCE_LOCATION (fn);
18900 /* Ignore any access checks; we'll see them again in
18901 instantiate_template and they might have the wrong
18902 access path at this point. */
18903 push_deferring_access_checks (dk_deferred);
18904 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18905 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18906 pop_deferring_access_checks ();
18907 input_location = loc;
18908 processing_template_decl -= incomplete;
18909 pop_tinst_level ();
18910
18911 if (fntype == error_mark_node)
18912 goto fail;
18913
18914 /* Place the explicitly specified arguments in TARGS. */
18915 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18916 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18917 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18918 }
18919
18920 /* Never do unification on the 'this' parameter. */
18921 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18922
18923 if (return_type && strict == DEDUCE_CALL)
18924 {
18925 /* We're deducing for a call to the result of a template conversion
18926 function. The parms we really want are in return_type. */
18927 if (POINTER_TYPE_P (return_type))
18928 return_type = TREE_TYPE (return_type);
18929 parms = TYPE_ARG_TYPES (return_type);
18930 }
18931 else if (return_type)
18932 {
18933 tree *new_args;
18934
18935 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18936 new_args = XALLOCAVEC (tree, nargs + 1);
18937 new_args[0] = return_type;
18938 memcpy (new_args + 1, args, nargs * sizeof (tree));
18939 args = new_args;
18940 ++nargs;
18941 }
18942
18943 /* We allow incomplete unification without an error message here
18944 because the standard doesn't seem to explicitly prohibit it. Our
18945 callers must be ready to deal with unification failures in any
18946 event. */
18947
18948 TREE_VALUE (tinst) = targs;
18949 /* If we aren't explaining yet, push tinst context so we can see where
18950 any errors (e.g. from class instantiations triggered by instantiation
18951 of default template arguments) come from. If we are explaining, this
18952 context is redundant. */
18953 if (!explain_p && !push_tinst_level (tinst))
18954 {
18955 excessive_deduction_depth = true;
18956 goto fail;
18957 }
18958
18959 /* type_unification_real will pass back any access checks from default
18960 template argument substitution. */
18961 vec<deferred_access_check, va_gc> *checks;
18962 checks = NULL;
18963
18964 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18965 full_targs, parms, args, nargs, /*subr=*/0,
18966 strict, flags, &checks, explain_p);
18967 if (!explain_p)
18968 pop_tinst_level ();
18969 if (!ok)
18970 goto fail;
18971
18972 /* Now that we have bindings for all of the template arguments,
18973 ensure that the arguments deduced for the template template
18974 parameters have compatible template parameter lists. We cannot
18975 check this property before we have deduced all template
18976 arguments, because the template parameter types of a template
18977 template parameter might depend on prior template parameters
18978 deduced after the template template parameter. The following
18979 ill-formed example illustrates this issue:
18980
18981 template<typename T, template<T> class C> void f(C<5>, T);
18982
18983 template<int N> struct X {};
18984
18985 void g() {
18986 f(X<5>(), 5l); // error: template argument deduction fails
18987 }
18988
18989 The template parameter list of 'C' depends on the template type
18990 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18991 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18992 time that we deduce 'C'. */
18993 if (!template_template_parm_bindings_ok_p
18994 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18995 {
18996 unify_inconsistent_template_template_parameters (explain_p);
18997 goto fail;
18998 }
18999
19000 /* All is well so far. Now, check:
19001
19002 [temp.deduct]
19003
19004 When all template arguments have been deduced, all uses of
19005 template parameters in nondeduced contexts are replaced with
19006 the corresponding deduced argument values. If the
19007 substitution results in an invalid type, as described above,
19008 type deduction fails. */
19009 TREE_VALUE (tinst) = targs;
19010 if (!push_tinst_level (tinst))
19011 {
19012 excessive_deduction_depth = true;
19013 goto fail;
19014 }
19015
19016 /* Also collect access checks from the instantiation. */
19017 reopen_deferring_access_checks (checks);
19018
19019 decl = instantiate_template (fn, targs, complain);
19020
19021 checks = get_deferred_access_checks ();
19022 pop_deferring_access_checks ();
19023
19024 pop_tinst_level ();
19025
19026 if (decl == error_mark_node)
19027 goto fail;
19028
19029 /* Now perform any access checks encountered during substitution. */
19030 push_access_scope (decl);
19031 ok = perform_access_checks (checks, complain);
19032 pop_access_scope (decl);
19033 if (!ok)
19034 goto fail;
19035
19036 /* If we're looking for an exact match, check that what we got
19037 is indeed an exact match. It might not be if some template
19038 parameters are used in non-deduced contexts. But don't check
19039 for an exact match if we have dependent template arguments;
19040 in that case we're doing partial ordering, and we already know
19041 that we have two candidates that will provide the actual type. */
19042 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19043 {
19044 tree substed = TREE_TYPE (decl);
19045 unsigned int i;
19046
19047 tree sarg
19048 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19049 if (return_type)
19050 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19051 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19052 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19053 {
19054 unify_type_mismatch (explain_p, args[i],
19055 TREE_VALUE (sarg));
19056 goto fail;
19057 }
19058 }
19059
19060 /* After doing deduction with the inherited constructor, actually return an
19061 instantiation of the inheriting constructor. */
19062 if (orig_fn != fn)
19063 decl = instantiate_template (orig_fn, targs, complain);
19064
19065 r = decl;
19066
19067 fail:
19068 --deduction_depth;
19069 if (excessive_deduction_depth)
19070 {
19071 if (deduction_depth == 0)
19072 /* Reset once we're all the way out. */
19073 excessive_deduction_depth = false;
19074 }
19075
19076 /* We can't free this if a pending_template entry or last_error_tinst_level
19077 is pointing at it. */
19078 if (last_pending_template == old_last_pend
19079 && last_error_tinst_level == old_error_tinst)
19080 ggc_free (tinst);
19081
19082 return r;
19083 }
19084
19085 /* Adjust types before performing type deduction, as described in
19086 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19087 sections are symmetric. PARM is the type of a function parameter
19088 or the return type of the conversion function. ARG is the type of
19089 the argument passed to the call, or the type of the value
19090 initialized with the result of the conversion function.
19091 ARG_EXPR is the original argument expression, which may be null. */
19092
19093 static int
19094 maybe_adjust_types_for_deduction (unification_kind_t strict,
19095 tree* parm,
19096 tree* arg,
19097 tree arg_expr)
19098 {
19099 int result = 0;
19100
19101 switch (strict)
19102 {
19103 case DEDUCE_CALL:
19104 break;
19105
19106 case DEDUCE_CONV:
19107 /* Swap PARM and ARG throughout the remainder of this
19108 function; the handling is precisely symmetric since PARM
19109 will initialize ARG rather than vice versa. */
19110 std::swap (parm, arg);
19111 break;
19112
19113 case DEDUCE_EXACT:
19114 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19115 too, but here handle it by stripping the reference from PARM
19116 rather than by adding it to ARG. */
19117 if (TREE_CODE (*parm) == REFERENCE_TYPE
19118 && TYPE_REF_IS_RVALUE (*parm)
19119 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19120 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19121 && TREE_CODE (*arg) == REFERENCE_TYPE
19122 && !TYPE_REF_IS_RVALUE (*arg))
19123 *parm = TREE_TYPE (*parm);
19124 /* Nothing else to do in this case. */
19125 return 0;
19126
19127 default:
19128 gcc_unreachable ();
19129 }
19130
19131 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19132 {
19133 /* [temp.deduct.call]
19134
19135 If P is not a reference type:
19136
19137 --If A is an array type, the pointer type produced by the
19138 array-to-pointer standard conversion (_conv.array_) is
19139 used in place of A for type deduction; otherwise,
19140
19141 --If A is a function type, the pointer type produced by
19142 the function-to-pointer standard conversion
19143 (_conv.func_) is used in place of A for type deduction;
19144 otherwise,
19145
19146 --If A is a cv-qualified type, the top level
19147 cv-qualifiers of A's type are ignored for type
19148 deduction. */
19149 if (TREE_CODE (*arg) == ARRAY_TYPE)
19150 *arg = build_pointer_type (TREE_TYPE (*arg));
19151 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19152 *arg = build_pointer_type (*arg);
19153 else
19154 *arg = TYPE_MAIN_VARIANT (*arg);
19155 }
19156
19157 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19158 reference to a cv-unqualified template parameter that does not represent a
19159 template parameter of a class template (during class template argument
19160 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19161 an lvalue, the type "lvalue reference to A" is used in place of A for type
19162 deduction. */
19163 if (TREE_CODE (*parm) == REFERENCE_TYPE
19164 && TYPE_REF_IS_RVALUE (*parm)
19165 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19166 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19167 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19168 && (arg_expr ? lvalue_p (arg_expr)
19169 /* try_one_overload doesn't provide an arg_expr, but
19170 functions are always lvalues. */
19171 : TREE_CODE (*arg) == FUNCTION_TYPE))
19172 *arg = build_reference_type (*arg);
19173
19174 /* [temp.deduct.call]
19175
19176 If P is a cv-qualified type, the top level cv-qualifiers
19177 of P's type are ignored for type deduction. If P is a
19178 reference type, the type referred to by P is used for
19179 type deduction. */
19180 *parm = TYPE_MAIN_VARIANT (*parm);
19181 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19182 {
19183 *parm = TREE_TYPE (*parm);
19184 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19185 }
19186
19187 /* DR 322. For conversion deduction, remove a reference type on parm
19188 too (which has been swapped into ARG). */
19189 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19190 *arg = TREE_TYPE (*arg);
19191
19192 return result;
19193 }
19194
19195 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19196 template which does contain any deducible template parameters; check if
19197 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19198 unify_one_argument. */
19199
19200 static int
19201 check_non_deducible_conversion (tree parm, tree arg, int strict,
19202 int flags, bool explain_p)
19203 {
19204 tree type;
19205
19206 if (!TYPE_P (arg))
19207 type = TREE_TYPE (arg);
19208 else
19209 type = arg;
19210
19211 if (same_type_p (parm, type))
19212 return unify_success (explain_p);
19213
19214 if (strict == DEDUCE_CONV)
19215 {
19216 if (can_convert_arg (type, parm, NULL_TREE, flags,
19217 explain_p ? tf_warning_or_error : tf_none))
19218 return unify_success (explain_p);
19219 }
19220 else if (strict != DEDUCE_EXACT)
19221 {
19222 if (can_convert_arg (parm, type,
19223 TYPE_P (arg) ? NULL_TREE : arg,
19224 flags, explain_p ? tf_warning_or_error : tf_none))
19225 return unify_success (explain_p);
19226 }
19227
19228 if (strict == DEDUCE_EXACT)
19229 return unify_type_mismatch (explain_p, parm, arg);
19230 else
19231 return unify_arg_conversion (explain_p, parm, type, arg);
19232 }
19233
19234 static bool uses_deducible_template_parms (tree type);
19235
19236 /* Returns true iff the expression EXPR is one from which a template
19237 argument can be deduced. In other words, if it's an undecorated
19238 use of a template non-type parameter. */
19239
19240 static bool
19241 deducible_expression (tree expr)
19242 {
19243 /* Strip implicit conversions. */
19244 while (CONVERT_EXPR_P (expr))
19245 expr = TREE_OPERAND (expr, 0);
19246 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19247 }
19248
19249 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19250 deducible way; that is, if it has a max value of <PARM> - 1. */
19251
19252 static bool
19253 deducible_array_bound (tree domain)
19254 {
19255 if (domain == NULL_TREE)
19256 return false;
19257
19258 tree max = TYPE_MAX_VALUE (domain);
19259 if (TREE_CODE (max) != MINUS_EXPR)
19260 return false;
19261
19262 return deducible_expression (TREE_OPERAND (max, 0));
19263 }
19264
19265 /* Returns true iff the template arguments ARGS use a template parameter
19266 in a deducible way. */
19267
19268 static bool
19269 deducible_template_args (tree args)
19270 {
19271 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19272 {
19273 bool deducible;
19274 tree elt = TREE_VEC_ELT (args, i);
19275 if (ARGUMENT_PACK_P (elt))
19276 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19277 else
19278 {
19279 if (PACK_EXPANSION_P (elt))
19280 elt = PACK_EXPANSION_PATTERN (elt);
19281 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19282 deducible = true;
19283 else if (TYPE_P (elt))
19284 deducible = uses_deducible_template_parms (elt);
19285 else
19286 deducible = deducible_expression (elt);
19287 }
19288 if (deducible)
19289 return true;
19290 }
19291 return false;
19292 }
19293
19294 /* Returns true iff TYPE contains any deducible references to template
19295 parameters, as per 14.8.2.5. */
19296
19297 static bool
19298 uses_deducible_template_parms (tree type)
19299 {
19300 if (PACK_EXPANSION_P (type))
19301 type = PACK_EXPANSION_PATTERN (type);
19302
19303 /* T
19304 cv-list T
19305 TT<T>
19306 TT<i>
19307 TT<> */
19308 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19309 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19310 return true;
19311
19312 /* T*
19313 T&
19314 T&& */
19315 if (POINTER_TYPE_P (type))
19316 return uses_deducible_template_parms (TREE_TYPE (type));
19317
19318 /* T[integer-constant ]
19319 type [i] */
19320 if (TREE_CODE (type) == ARRAY_TYPE)
19321 return (uses_deducible_template_parms (TREE_TYPE (type))
19322 || deducible_array_bound (TYPE_DOMAIN (type)));
19323
19324 /* T type ::*
19325 type T::*
19326 T T::*
19327 T (type ::*)()
19328 type (T::*)()
19329 type (type ::*)(T)
19330 type (T::*)(T)
19331 T (type ::*)(T)
19332 T (T::*)()
19333 T (T::*)(T) */
19334 if (TYPE_PTRMEM_P (type))
19335 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19336 || (uses_deducible_template_parms
19337 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19338
19339 /* template-name <T> (where template-name refers to a class template)
19340 template-name <i> (where template-name refers to a class template) */
19341 if (CLASS_TYPE_P (type)
19342 && CLASSTYPE_TEMPLATE_INFO (type)
19343 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19344 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19345 (CLASSTYPE_TI_ARGS (type)));
19346
19347 /* type (T)
19348 T()
19349 T(T) */
19350 if (TREE_CODE (type) == FUNCTION_TYPE
19351 || TREE_CODE (type) == METHOD_TYPE)
19352 {
19353 if (uses_deducible_template_parms (TREE_TYPE (type)))
19354 return true;
19355 tree parm = TYPE_ARG_TYPES (type);
19356 if (TREE_CODE (type) == METHOD_TYPE)
19357 parm = TREE_CHAIN (parm);
19358 for (; parm; parm = TREE_CHAIN (parm))
19359 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19360 return true;
19361 }
19362
19363 return false;
19364 }
19365
19366 /* Subroutine of type_unification_real and unify_pack_expansion to
19367 handle unification of a single P/A pair. Parameters are as
19368 for those functions. */
19369
19370 static int
19371 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19372 int subr, unification_kind_t strict,
19373 bool explain_p)
19374 {
19375 tree arg_expr = NULL_TREE;
19376 int arg_strict;
19377
19378 if (arg == error_mark_node || parm == error_mark_node)
19379 return unify_invalid (explain_p);
19380 if (arg == unknown_type_node)
19381 /* We can't deduce anything from this, but we might get all the
19382 template args from other function args. */
19383 return unify_success (explain_p);
19384
19385 /* Implicit conversions (Clause 4) will be performed on a function
19386 argument to convert it to the type of the corresponding function
19387 parameter if the parameter type contains no template-parameters that
19388 participate in template argument deduction. */
19389 if (strict != DEDUCE_EXACT
19390 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19391 /* For function parameters with no deducible template parameters,
19392 just return. We'll check non-dependent conversions later. */
19393 return unify_success (explain_p);
19394
19395 switch (strict)
19396 {
19397 case DEDUCE_CALL:
19398 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19399 | UNIFY_ALLOW_MORE_CV_QUAL
19400 | UNIFY_ALLOW_DERIVED);
19401 break;
19402
19403 case DEDUCE_CONV:
19404 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19405 break;
19406
19407 case DEDUCE_EXACT:
19408 arg_strict = UNIFY_ALLOW_NONE;
19409 break;
19410
19411 default:
19412 gcc_unreachable ();
19413 }
19414
19415 /* We only do these transformations if this is the top-level
19416 parameter_type_list in a call or declaration matching; in other
19417 situations (nested function declarators, template argument lists) we
19418 won't be comparing a type to an expression, and we don't do any type
19419 adjustments. */
19420 if (!subr)
19421 {
19422 if (!TYPE_P (arg))
19423 {
19424 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19425 if (type_unknown_p (arg))
19426 {
19427 /* [temp.deduct.type] A template-argument can be
19428 deduced from a pointer to function or pointer
19429 to member function argument if the set of
19430 overloaded functions does not contain function
19431 templates and at most one of a set of
19432 overloaded functions provides a unique
19433 match. */
19434 resolve_overloaded_unification (tparms, targs, parm,
19435 arg, strict,
19436 arg_strict, explain_p);
19437 /* If a unique match was not found, this is a
19438 non-deduced context, so we still succeed. */
19439 return unify_success (explain_p);
19440 }
19441
19442 arg_expr = arg;
19443 arg = unlowered_expr_type (arg);
19444 if (arg == error_mark_node)
19445 return unify_invalid (explain_p);
19446 }
19447
19448 arg_strict |=
19449 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19450 }
19451 else
19452 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19453 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19454 return unify_template_argument_mismatch (explain_p, parm, arg);
19455
19456 /* For deduction from an init-list we need the actual list. */
19457 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19458 arg = arg_expr;
19459 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19460 }
19461
19462 /* for_each_template_parm callback that always returns 0. */
19463
19464 static int
19465 zero_r (tree, void *)
19466 {
19467 return 0;
19468 }
19469
19470 /* for_each_template_parm any_fn callback to handle deduction of a template
19471 type argument from the type of an array bound. */
19472
19473 static int
19474 array_deduction_r (tree t, void *data)
19475 {
19476 tree_pair_p d = (tree_pair_p)data;
19477 tree &tparms = d->purpose;
19478 tree &targs = d->value;
19479
19480 if (TREE_CODE (t) == ARRAY_TYPE)
19481 if (tree dom = TYPE_DOMAIN (t))
19482 if (tree max = TYPE_MAX_VALUE (dom))
19483 {
19484 if (TREE_CODE (max) == MINUS_EXPR)
19485 max = TREE_OPERAND (max, 0);
19486 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19487 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19488 UNIFY_ALLOW_NONE, /*explain*/false);
19489 }
19490
19491 /* Keep walking. */
19492 return 0;
19493 }
19494
19495 /* Try to deduce any not-yet-deduced template type arguments from the type of
19496 an array bound. This is handled separately from unify because 14.8.2.5 says
19497 "The type of a type parameter is only deduced from an array bound if it is
19498 not otherwise deduced." */
19499
19500 static void
19501 try_array_deduction (tree tparms, tree targs, tree parm)
19502 {
19503 tree_pair_s data = { tparms, targs };
19504 hash_set<tree> visited;
19505 for_each_template_parm (parm, zero_r, &data, &visited,
19506 /*nondeduced*/false, array_deduction_r);
19507 }
19508
19509 /* Most parms like fn_type_unification.
19510
19511 If SUBR is 1, we're being called recursively (to unify the
19512 arguments of a function or method parameter of a function
19513 template).
19514
19515 CHECKS is a pointer to a vector of access checks encountered while
19516 substituting default template arguments. */
19517
19518 static int
19519 type_unification_real (tree tparms,
19520 tree full_targs,
19521 tree xparms,
19522 const tree *xargs,
19523 unsigned int xnargs,
19524 int subr,
19525 unification_kind_t strict,
19526 int flags,
19527 vec<deferred_access_check, va_gc> **checks,
19528 bool explain_p)
19529 {
19530 tree parm, arg;
19531 int i;
19532 int ntparms = TREE_VEC_LENGTH (tparms);
19533 int saw_undeduced = 0;
19534 tree parms;
19535 const tree *args;
19536 unsigned int nargs;
19537 unsigned int ia;
19538
19539 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19540 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19541 gcc_assert (ntparms > 0);
19542
19543 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19544
19545 /* Reset the number of non-defaulted template arguments contained
19546 in TARGS. */
19547 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19548
19549 again:
19550 parms = xparms;
19551 args = xargs;
19552 nargs = xnargs;
19553
19554 ia = 0;
19555 while (parms && parms != void_list_node
19556 && ia < nargs)
19557 {
19558 parm = TREE_VALUE (parms);
19559
19560 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19561 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19562 /* For a function parameter pack that occurs at the end of the
19563 parameter-declaration-list, the type A of each remaining
19564 argument of the call is compared with the type P of the
19565 declarator-id of the function parameter pack. */
19566 break;
19567
19568 parms = TREE_CHAIN (parms);
19569
19570 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19571 /* For a function parameter pack that does not occur at the
19572 end of the parameter-declaration-list, the type of the
19573 parameter pack is a non-deduced context. */
19574 continue;
19575
19576 arg = args[ia];
19577 ++ia;
19578
19579 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19580 explain_p))
19581 return 1;
19582 }
19583
19584 if (parms
19585 && parms != void_list_node
19586 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19587 {
19588 /* Unify the remaining arguments with the pack expansion type. */
19589 tree argvec;
19590 tree parmvec = make_tree_vec (1);
19591
19592 /* Allocate a TREE_VEC and copy in all of the arguments */
19593 argvec = make_tree_vec (nargs - ia);
19594 for (i = 0; ia < nargs; ++ia, ++i)
19595 TREE_VEC_ELT (argvec, i) = args[ia];
19596
19597 /* Copy the parameter into parmvec. */
19598 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19599 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19600 /*subr=*/subr, explain_p))
19601 return 1;
19602
19603 /* Advance to the end of the list of parameters. */
19604 parms = TREE_CHAIN (parms);
19605 }
19606
19607 /* Fail if we've reached the end of the parm list, and more args
19608 are present, and the parm list isn't variadic. */
19609 if (ia < nargs && parms == void_list_node)
19610 return unify_too_many_arguments (explain_p, nargs, ia);
19611 /* Fail if parms are left and they don't have default values and
19612 they aren't all deduced as empty packs (c++/57397). This is
19613 consistent with sufficient_parms_p. */
19614 if (parms && parms != void_list_node
19615 && TREE_PURPOSE (parms) == NULL_TREE)
19616 {
19617 unsigned int count = nargs;
19618 tree p = parms;
19619 bool type_pack_p;
19620 do
19621 {
19622 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19623 if (!type_pack_p)
19624 count++;
19625 p = TREE_CHAIN (p);
19626 }
19627 while (p && p != void_list_node);
19628 if (count != nargs)
19629 return unify_too_few_arguments (explain_p, ia, count,
19630 type_pack_p);
19631 }
19632
19633 if (!subr)
19634 {
19635 tsubst_flags_t complain = (explain_p
19636 ? tf_warning_or_error
19637 : tf_none);
19638 bool tried_array_deduction = (cxx_dialect < cxx17);
19639
19640 for (i = 0; i < ntparms; i++)
19641 {
19642 tree targ = TREE_VEC_ELT (targs, i);
19643 tree tparm = TREE_VEC_ELT (tparms, i);
19644
19645 /* Clear the "incomplete" flags on all argument packs now so that
19646 substituting them into later default arguments works. */
19647 if (targ && ARGUMENT_PACK_P (targ))
19648 {
19649 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19650 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19651 }
19652
19653 if (targ || tparm == error_mark_node)
19654 continue;
19655 tparm = TREE_VALUE (tparm);
19656
19657 if (TREE_CODE (tparm) == TYPE_DECL
19658 && !tried_array_deduction)
19659 {
19660 try_array_deduction (tparms, targs, xparms);
19661 tried_array_deduction = true;
19662 if (TREE_VEC_ELT (targs, i))
19663 continue;
19664 }
19665
19666 /* If this is an undeduced nontype parameter that depends on
19667 a type parameter, try another pass; its type may have been
19668 deduced from a later argument than the one from which
19669 this parameter can be deduced. */
19670 if (TREE_CODE (tparm) == PARM_DECL
19671 && uses_template_parms (TREE_TYPE (tparm))
19672 && saw_undeduced < 2)
19673 {
19674 saw_undeduced = 1;
19675 continue;
19676 }
19677
19678 /* Core issue #226 (C++0x) [temp.deduct]:
19679
19680 If a template argument has not been deduced, its
19681 default template argument, if any, is used.
19682
19683 When we are in C++98 mode, TREE_PURPOSE will either
19684 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19685 to explicitly check cxx_dialect here. */
19686 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19687 /* OK, there is a default argument. Wait until after the
19688 conversion check to do substitution. */
19689 continue;
19690
19691 /* If the type parameter is a parameter pack, then it will
19692 be deduced to an empty parameter pack. */
19693 if (template_parameter_pack_p (tparm))
19694 {
19695 tree arg;
19696
19697 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19698 {
19699 arg = make_node (NONTYPE_ARGUMENT_PACK);
19700 TREE_CONSTANT (arg) = 1;
19701 }
19702 else
19703 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19704
19705 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19706
19707 TREE_VEC_ELT (targs, i) = arg;
19708 continue;
19709 }
19710
19711 return unify_parameter_deduction_failure (explain_p, tparm);
19712 }
19713
19714 /* DR 1391: All parameters have args, now check non-dependent parms for
19715 convertibility. */
19716 if (saw_undeduced < 2)
19717 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19718 parms && parms != void_list_node && ia < nargs; )
19719 {
19720 parm = TREE_VALUE (parms);
19721
19722 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19723 && (!TREE_CHAIN (parms)
19724 || TREE_CHAIN (parms) == void_list_node))
19725 /* For a function parameter pack that occurs at the end of the
19726 parameter-declaration-list, the type A of each remaining
19727 argument of the call is compared with the type P of the
19728 declarator-id of the function parameter pack. */
19729 break;
19730
19731 parms = TREE_CHAIN (parms);
19732
19733 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19734 /* For a function parameter pack that does not occur at the
19735 end of the parameter-declaration-list, the type of the
19736 parameter pack is a non-deduced context. */
19737 continue;
19738
19739 arg = args[ia];
19740 ++ia;
19741
19742 if (uses_template_parms (parm))
19743 continue;
19744 if (check_non_deducible_conversion (parm, arg, strict, flags,
19745 explain_p))
19746 return 1;
19747 }
19748
19749 /* Now substitute into the default template arguments. */
19750 for (i = 0; i < ntparms; i++)
19751 {
19752 tree targ = TREE_VEC_ELT (targs, i);
19753 tree tparm = TREE_VEC_ELT (tparms, i);
19754
19755 if (targ || tparm == error_mark_node)
19756 continue;
19757 tree parm = TREE_VALUE (tparm);
19758
19759 if (TREE_CODE (parm) == PARM_DECL
19760 && uses_template_parms (TREE_TYPE (parm))
19761 && saw_undeduced < 2)
19762 continue;
19763
19764 tree arg = TREE_PURPOSE (tparm);
19765 reopen_deferring_access_checks (*checks);
19766 location_t save_loc = input_location;
19767 if (DECL_P (parm))
19768 input_location = DECL_SOURCE_LOCATION (parm);
19769 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19770 if (!uses_template_parms (arg))
19771 arg = convert_template_argument (parm, arg, full_targs, complain,
19772 i, NULL_TREE);
19773 else if (saw_undeduced < 2)
19774 arg = NULL_TREE;
19775 else
19776 arg = error_mark_node;
19777 input_location = save_loc;
19778 *checks = get_deferred_access_checks ();
19779 pop_deferring_access_checks ();
19780 if (arg == error_mark_node)
19781 return 1;
19782 else if (arg)
19783 {
19784 TREE_VEC_ELT (targs, i) = arg;
19785 /* The position of the first default template argument,
19786 is also the number of non-defaulted arguments in TARGS.
19787 Record that. */
19788 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19789 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19790 }
19791 }
19792
19793 if (saw_undeduced++ == 1)
19794 goto again;
19795 }
19796
19797 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19798 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19799
19800 return unify_success (explain_p);
19801 }
19802
19803 /* Subroutine of type_unification_real. Args are like the variables
19804 at the call site. ARG is an overloaded function (or template-id);
19805 we try deducing template args from each of the overloads, and if
19806 only one succeeds, we go with that. Modifies TARGS and returns
19807 true on success. */
19808
19809 static bool
19810 resolve_overloaded_unification (tree tparms,
19811 tree targs,
19812 tree parm,
19813 tree arg,
19814 unification_kind_t strict,
19815 int sub_strict,
19816 bool explain_p)
19817 {
19818 tree tempargs = copy_node (targs);
19819 int good = 0;
19820 tree goodfn = NULL_TREE;
19821 bool addr_p;
19822
19823 if (TREE_CODE (arg) == ADDR_EXPR)
19824 {
19825 arg = TREE_OPERAND (arg, 0);
19826 addr_p = true;
19827 }
19828 else
19829 addr_p = false;
19830
19831 if (TREE_CODE (arg) == COMPONENT_REF)
19832 /* Handle `&x' where `x' is some static or non-static member
19833 function name. */
19834 arg = TREE_OPERAND (arg, 1);
19835
19836 if (TREE_CODE (arg) == OFFSET_REF)
19837 arg = TREE_OPERAND (arg, 1);
19838
19839 /* Strip baselink information. */
19840 if (BASELINK_P (arg))
19841 arg = BASELINK_FUNCTIONS (arg);
19842
19843 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19844 {
19845 /* If we got some explicit template args, we need to plug them into
19846 the affected templates before we try to unify, in case the
19847 explicit args will completely resolve the templates in question. */
19848
19849 int ok = 0;
19850 tree expl_subargs = TREE_OPERAND (arg, 1);
19851 arg = TREE_OPERAND (arg, 0);
19852
19853 for (lkp_iterator iter (arg); iter; ++iter)
19854 {
19855 tree fn = *iter;
19856 tree subargs, elem;
19857
19858 if (TREE_CODE (fn) != TEMPLATE_DECL)
19859 continue;
19860
19861 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19862 expl_subargs, NULL_TREE, tf_none,
19863 /*require_all_args=*/true,
19864 /*use_default_args=*/true);
19865 if (subargs != error_mark_node
19866 && !any_dependent_template_arguments_p (subargs))
19867 {
19868 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19869 if (try_one_overload (tparms, targs, tempargs, parm,
19870 elem, strict, sub_strict, addr_p, explain_p)
19871 && (!goodfn || !same_type_p (goodfn, elem)))
19872 {
19873 goodfn = elem;
19874 ++good;
19875 }
19876 }
19877 else if (subargs)
19878 ++ok;
19879 }
19880 /* If no templates (or more than one) are fully resolved by the
19881 explicit arguments, this template-id is a non-deduced context; it
19882 could still be OK if we deduce all template arguments for the
19883 enclosing call through other arguments. */
19884 if (good != 1)
19885 good = ok;
19886 }
19887 else if (TREE_CODE (arg) != OVERLOAD
19888 && TREE_CODE (arg) != FUNCTION_DECL)
19889 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19890 -- but the deduction does not succeed because the expression is
19891 not just the function on its own. */
19892 return false;
19893 else
19894 for (lkp_iterator iter (arg); iter; ++iter)
19895 {
19896 tree fn = *iter;
19897 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19898 strict, sub_strict, addr_p, explain_p)
19899 && (!goodfn || !decls_match (goodfn, fn)))
19900 {
19901 goodfn = fn;
19902 ++good;
19903 }
19904 }
19905
19906 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19907 to function or pointer to member function argument if the set of
19908 overloaded functions does not contain function templates and at most
19909 one of a set of overloaded functions provides a unique match.
19910
19911 So if we found multiple possibilities, we return success but don't
19912 deduce anything. */
19913
19914 if (good == 1)
19915 {
19916 int i = TREE_VEC_LENGTH (targs);
19917 for (; i--; )
19918 if (TREE_VEC_ELT (tempargs, i))
19919 {
19920 tree old = TREE_VEC_ELT (targs, i);
19921 tree new_ = TREE_VEC_ELT (tempargs, i);
19922 if (new_ && old && ARGUMENT_PACK_P (old)
19923 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19924 /* Don't forget explicit template arguments in a pack. */
19925 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19926 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19927 TREE_VEC_ELT (targs, i) = new_;
19928 }
19929 }
19930 if (good)
19931 return true;
19932
19933 return false;
19934 }
19935
19936 /* Core DR 115: In contexts where deduction is done and fails, or in
19937 contexts where deduction is not done, if a template argument list is
19938 specified and it, along with any default template arguments, identifies
19939 a single function template specialization, then the template-id is an
19940 lvalue for the function template specialization. */
19941
19942 tree
19943 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19944 {
19945 tree expr, offset, baselink;
19946 bool addr;
19947
19948 if (!type_unknown_p (orig_expr))
19949 return orig_expr;
19950
19951 expr = orig_expr;
19952 addr = false;
19953 offset = NULL_TREE;
19954 baselink = NULL_TREE;
19955
19956 if (TREE_CODE (expr) == ADDR_EXPR)
19957 {
19958 expr = TREE_OPERAND (expr, 0);
19959 addr = true;
19960 }
19961 if (TREE_CODE (expr) == OFFSET_REF)
19962 {
19963 offset = expr;
19964 expr = TREE_OPERAND (expr, 1);
19965 }
19966 if (BASELINK_P (expr))
19967 {
19968 baselink = expr;
19969 expr = BASELINK_FUNCTIONS (expr);
19970 }
19971
19972 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19973 {
19974 int good = 0;
19975 tree goodfn = NULL_TREE;
19976
19977 /* If we got some explicit template args, we need to plug them into
19978 the affected templates before we try to unify, in case the
19979 explicit args will completely resolve the templates in question. */
19980
19981 tree expl_subargs = TREE_OPERAND (expr, 1);
19982 tree arg = TREE_OPERAND (expr, 0);
19983 tree badfn = NULL_TREE;
19984 tree badargs = NULL_TREE;
19985
19986 for (lkp_iterator iter (arg); iter; ++iter)
19987 {
19988 tree fn = *iter;
19989 tree subargs, elem;
19990
19991 if (TREE_CODE (fn) != TEMPLATE_DECL)
19992 continue;
19993
19994 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19995 expl_subargs, NULL_TREE, tf_none,
19996 /*require_all_args=*/true,
19997 /*use_default_args=*/true);
19998 if (subargs != error_mark_node
19999 && !any_dependent_template_arguments_p (subargs))
20000 {
20001 elem = instantiate_template (fn, subargs, tf_none);
20002 if (elem == error_mark_node)
20003 {
20004 badfn = fn;
20005 badargs = subargs;
20006 }
20007 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20008 {
20009 goodfn = elem;
20010 ++good;
20011 }
20012 }
20013 }
20014 if (good == 1)
20015 {
20016 mark_used (goodfn);
20017 expr = goodfn;
20018 if (baselink)
20019 expr = build_baselink (BASELINK_BINFO (baselink),
20020 BASELINK_ACCESS_BINFO (baselink),
20021 expr, BASELINK_OPTYPE (baselink));
20022 if (offset)
20023 {
20024 tree base
20025 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20026 expr = build_offset_ref (base, expr, addr, complain);
20027 }
20028 if (addr)
20029 expr = cp_build_addr_expr (expr, complain);
20030 return expr;
20031 }
20032 else if (good == 0 && badargs && (complain & tf_error))
20033 /* There were no good options and at least one bad one, so let the
20034 user know what the problem is. */
20035 instantiate_template (badfn, badargs, complain);
20036 }
20037 return orig_expr;
20038 }
20039
20040 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20041 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20042 different overloads deduce different arguments for a given parm.
20043 ADDR_P is true if the expression for which deduction is being
20044 performed was of the form "& fn" rather than simply "fn".
20045
20046 Returns 1 on success. */
20047
20048 static int
20049 try_one_overload (tree tparms,
20050 tree orig_targs,
20051 tree targs,
20052 tree parm,
20053 tree arg,
20054 unification_kind_t strict,
20055 int sub_strict,
20056 bool addr_p,
20057 bool explain_p)
20058 {
20059 int nargs;
20060 tree tempargs;
20061 int i;
20062
20063 if (arg == error_mark_node)
20064 return 0;
20065
20066 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20067 to function or pointer to member function argument if the set of
20068 overloaded functions does not contain function templates and at most
20069 one of a set of overloaded functions provides a unique match.
20070
20071 So if this is a template, just return success. */
20072
20073 if (uses_template_parms (arg))
20074 return 1;
20075
20076 if (TREE_CODE (arg) == METHOD_TYPE)
20077 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20078 else if (addr_p)
20079 arg = build_pointer_type (arg);
20080
20081 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20082
20083 /* We don't copy orig_targs for this because if we have already deduced
20084 some template args from previous args, unify would complain when we
20085 try to deduce a template parameter for the same argument, even though
20086 there isn't really a conflict. */
20087 nargs = TREE_VEC_LENGTH (targs);
20088 tempargs = make_tree_vec (nargs);
20089
20090 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20091 return 0;
20092
20093 /* First make sure we didn't deduce anything that conflicts with
20094 explicitly specified args. */
20095 for (i = nargs; i--; )
20096 {
20097 tree elt = TREE_VEC_ELT (tempargs, i);
20098 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20099
20100 if (!elt)
20101 /*NOP*/;
20102 else if (uses_template_parms (elt))
20103 /* Since we're unifying against ourselves, we will fill in
20104 template args used in the function parm list with our own
20105 template parms. Discard them. */
20106 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20107 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20108 {
20109 /* Check that the argument at each index of the deduced argument pack
20110 is equivalent to the corresponding explicitly specified argument.
20111 We may have deduced more arguments than were explicitly specified,
20112 and that's OK. */
20113
20114 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20115 that's wrong if we deduce the same argument pack from multiple
20116 function arguments: it's only incomplete the first time. */
20117
20118 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20119 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20120
20121 if (TREE_VEC_LENGTH (deduced_pack)
20122 < TREE_VEC_LENGTH (explicit_pack))
20123 return 0;
20124
20125 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20126 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20127 TREE_VEC_ELT (deduced_pack, j)))
20128 return 0;
20129 }
20130 else if (oldelt && !template_args_equal (oldelt, elt))
20131 return 0;
20132 }
20133
20134 for (i = nargs; i--; )
20135 {
20136 tree elt = TREE_VEC_ELT (tempargs, i);
20137
20138 if (elt)
20139 TREE_VEC_ELT (targs, i) = elt;
20140 }
20141
20142 return 1;
20143 }
20144
20145 /* PARM is a template class (perhaps with unbound template
20146 parameters). ARG is a fully instantiated type. If ARG can be
20147 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20148 TARGS are as for unify. */
20149
20150 static tree
20151 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20152 bool explain_p)
20153 {
20154 tree copy_of_targs;
20155
20156 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20157 return NULL_TREE;
20158 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20159 /* Matches anything. */;
20160 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20161 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20162 return NULL_TREE;
20163
20164 /* We need to make a new template argument vector for the call to
20165 unify. If we used TARGS, we'd clutter it up with the result of
20166 the attempted unification, even if this class didn't work out.
20167 We also don't want to commit ourselves to all the unifications
20168 we've already done, since unification is supposed to be done on
20169 an argument-by-argument basis. In other words, consider the
20170 following pathological case:
20171
20172 template <int I, int J, int K>
20173 struct S {};
20174
20175 template <int I, int J>
20176 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20177
20178 template <int I, int J, int K>
20179 void f(S<I, J, K>, S<I, I, I>);
20180
20181 void g() {
20182 S<0, 0, 0> s0;
20183 S<0, 1, 2> s2;
20184
20185 f(s0, s2);
20186 }
20187
20188 Now, by the time we consider the unification involving `s2', we
20189 already know that we must have `f<0, 0, 0>'. But, even though
20190 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20191 because there are two ways to unify base classes of S<0, 1, 2>
20192 with S<I, I, I>. If we kept the already deduced knowledge, we
20193 would reject the possibility I=1. */
20194 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20195
20196 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20197 {
20198 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20199 return NULL_TREE;
20200 return arg;
20201 }
20202
20203 /* If unification failed, we're done. */
20204 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20205 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20206 return NULL_TREE;
20207
20208 return arg;
20209 }
20210
20211 /* Given a template type PARM and a class type ARG, find the unique
20212 base type in ARG that is an instance of PARM. We do not examine
20213 ARG itself; only its base-classes. If there is not exactly one
20214 appropriate base class, return NULL_TREE. PARM may be the type of
20215 a partial specialization, as well as a plain template type. Used
20216 by unify. */
20217
20218 static enum template_base_result
20219 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20220 bool explain_p, tree *result)
20221 {
20222 tree rval = NULL_TREE;
20223 tree binfo;
20224
20225 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20226
20227 binfo = TYPE_BINFO (complete_type (arg));
20228 if (!binfo)
20229 {
20230 /* The type could not be completed. */
20231 *result = NULL_TREE;
20232 return tbr_incomplete_type;
20233 }
20234
20235 /* Walk in inheritance graph order. The search order is not
20236 important, and this avoids multiple walks of virtual bases. */
20237 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20238 {
20239 tree r = try_class_unification (tparms, targs, parm,
20240 BINFO_TYPE (binfo), explain_p);
20241
20242 if (r)
20243 {
20244 /* If there is more than one satisfactory baseclass, then:
20245
20246 [temp.deduct.call]
20247
20248 If they yield more than one possible deduced A, the type
20249 deduction fails.
20250
20251 applies. */
20252 if (rval && !same_type_p (r, rval))
20253 {
20254 *result = NULL_TREE;
20255 return tbr_ambiguous_baseclass;
20256 }
20257
20258 rval = r;
20259 }
20260 }
20261
20262 *result = rval;
20263 return tbr_success;
20264 }
20265
20266 /* Returns the level of DECL, which declares a template parameter. */
20267
20268 static int
20269 template_decl_level (tree decl)
20270 {
20271 switch (TREE_CODE (decl))
20272 {
20273 case TYPE_DECL:
20274 case TEMPLATE_DECL:
20275 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20276
20277 case PARM_DECL:
20278 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20279
20280 default:
20281 gcc_unreachable ();
20282 }
20283 return 0;
20284 }
20285
20286 /* Decide whether ARG can be unified with PARM, considering only the
20287 cv-qualifiers of each type, given STRICT as documented for unify.
20288 Returns nonzero iff the unification is OK on that basis. */
20289
20290 static int
20291 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20292 {
20293 int arg_quals = cp_type_quals (arg);
20294 int parm_quals = cp_type_quals (parm);
20295
20296 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20297 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20298 {
20299 /* Although a CVR qualifier is ignored when being applied to a
20300 substituted template parameter ([8.3.2]/1 for example), that
20301 does not allow us to unify "const T" with "int&" because both
20302 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20303 It is ok when we're allowing additional CV qualifiers
20304 at the outer level [14.8.2.1]/3,1st bullet. */
20305 if ((TREE_CODE (arg) == REFERENCE_TYPE
20306 || TREE_CODE (arg) == FUNCTION_TYPE
20307 || TREE_CODE (arg) == METHOD_TYPE)
20308 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20309 return 0;
20310
20311 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20312 && (parm_quals & TYPE_QUAL_RESTRICT))
20313 return 0;
20314 }
20315
20316 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20317 && (arg_quals & parm_quals) != parm_quals)
20318 return 0;
20319
20320 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20321 && (parm_quals & arg_quals) != arg_quals)
20322 return 0;
20323
20324 return 1;
20325 }
20326
20327 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20328 void
20329 template_parm_level_and_index (tree parm, int* level, int* index)
20330 {
20331 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20332 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20333 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20334 {
20335 *index = TEMPLATE_TYPE_IDX (parm);
20336 *level = TEMPLATE_TYPE_LEVEL (parm);
20337 }
20338 else
20339 {
20340 *index = TEMPLATE_PARM_IDX (parm);
20341 *level = TEMPLATE_PARM_LEVEL (parm);
20342 }
20343 }
20344
20345 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20346 do { \
20347 if (unify (TP, TA, P, A, S, EP)) \
20348 return 1; \
20349 } while (0)
20350
20351 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20352 expansion at the end of PACKED_PARMS. Returns 0 if the type
20353 deduction succeeds, 1 otherwise. STRICT is the same as in
20354 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20355 function call argument list. We'll need to adjust the arguments to make them
20356 types. SUBR tells us if this is from a recursive call to
20357 type_unification_real, or for comparing two template argument
20358 lists. */
20359
20360 static int
20361 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20362 tree packed_args, unification_kind_t strict,
20363 bool subr, bool explain_p)
20364 {
20365 tree parm
20366 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20367 tree pattern = PACK_EXPANSION_PATTERN (parm);
20368 tree pack, packs = NULL_TREE;
20369 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20370
20371 /* Add in any args remembered from an earlier partial instantiation. */
20372 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20373 int levels = TMPL_ARGS_DEPTH (targs);
20374
20375 packed_args = expand_template_argument_pack (packed_args);
20376
20377 int len = TREE_VEC_LENGTH (packed_args);
20378
20379 /* Determine the parameter packs we will be deducing from the
20380 pattern, and record their current deductions. */
20381 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20382 pack; pack = TREE_CHAIN (pack))
20383 {
20384 tree parm_pack = TREE_VALUE (pack);
20385 int idx, level;
20386
20387 /* Determine the index and level of this parameter pack. */
20388 template_parm_level_and_index (parm_pack, &level, &idx);
20389 if (level < levels)
20390 continue;
20391
20392 /* Keep track of the parameter packs and their corresponding
20393 argument packs. */
20394 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20395 TREE_TYPE (packs) = make_tree_vec (len - start);
20396 }
20397
20398 /* Loop through all of the arguments that have not yet been
20399 unified and unify each with the pattern. */
20400 for (i = start; i < len; i++)
20401 {
20402 tree parm;
20403 bool any_explicit = false;
20404 tree arg = TREE_VEC_ELT (packed_args, i);
20405
20406 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20407 or the element of its argument pack at the current index if
20408 this argument was explicitly specified. */
20409 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20410 {
20411 int idx, level;
20412 tree arg, pargs;
20413 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20414
20415 arg = NULL_TREE;
20416 if (TREE_VALUE (pack)
20417 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20418 && (i - start < TREE_VEC_LENGTH (pargs)))
20419 {
20420 any_explicit = true;
20421 arg = TREE_VEC_ELT (pargs, i - start);
20422 }
20423 TMPL_ARG (targs, level, idx) = arg;
20424 }
20425
20426 /* If we had explicit template arguments, substitute them into the
20427 pattern before deduction. */
20428 if (any_explicit)
20429 {
20430 /* Some arguments might still be unspecified or dependent. */
20431 bool dependent;
20432 ++processing_template_decl;
20433 dependent = any_dependent_template_arguments_p (targs);
20434 if (!dependent)
20435 --processing_template_decl;
20436 parm = tsubst (pattern, targs,
20437 explain_p ? tf_warning_or_error : tf_none,
20438 NULL_TREE);
20439 if (dependent)
20440 --processing_template_decl;
20441 if (parm == error_mark_node)
20442 return 1;
20443 }
20444 else
20445 parm = pattern;
20446
20447 /* Unify the pattern with the current argument. */
20448 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20449 explain_p))
20450 return 1;
20451
20452 /* For each parameter pack, collect the deduced value. */
20453 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20454 {
20455 int idx, level;
20456 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20457
20458 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20459 TMPL_ARG (targs, level, idx);
20460 }
20461 }
20462
20463 /* Verify that the results of unification with the parameter packs
20464 produce results consistent with what we've seen before, and make
20465 the deduced argument packs available. */
20466 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20467 {
20468 tree old_pack = TREE_VALUE (pack);
20469 tree new_args = TREE_TYPE (pack);
20470 int i, len = TREE_VEC_LENGTH (new_args);
20471 int idx, level;
20472 bool nondeduced_p = false;
20473
20474 /* By default keep the original deduced argument pack.
20475 If necessary, more specific code is going to update the
20476 resulting deduced argument later down in this function. */
20477 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20478 TMPL_ARG (targs, level, idx) = old_pack;
20479
20480 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20481 actually deduce anything. */
20482 for (i = 0; i < len && !nondeduced_p; ++i)
20483 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20484 nondeduced_p = true;
20485 if (nondeduced_p)
20486 continue;
20487
20488 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20489 {
20490 /* If we had fewer function args than explicit template args,
20491 just use the explicits. */
20492 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20493 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20494 if (len < explicit_len)
20495 new_args = explicit_args;
20496 }
20497
20498 if (!old_pack)
20499 {
20500 tree result;
20501 /* Build the deduced *_ARGUMENT_PACK. */
20502 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20503 {
20504 result = make_node (NONTYPE_ARGUMENT_PACK);
20505 TREE_CONSTANT (result) = 1;
20506 }
20507 else
20508 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20509
20510 SET_ARGUMENT_PACK_ARGS (result, new_args);
20511
20512 /* Note the deduced argument packs for this parameter
20513 pack. */
20514 TMPL_ARG (targs, level, idx) = result;
20515 }
20516 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20517 && (ARGUMENT_PACK_ARGS (old_pack)
20518 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20519 {
20520 /* We only had the explicitly-provided arguments before, but
20521 now we have a complete set of arguments. */
20522 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20523
20524 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20525 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20526 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20527 }
20528 else
20529 {
20530 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20531 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20532
20533 if (!comp_template_args (old_args, new_args,
20534 &bad_old_arg, &bad_new_arg))
20535 /* Inconsistent unification of this parameter pack. */
20536 return unify_parameter_pack_inconsistent (explain_p,
20537 bad_old_arg,
20538 bad_new_arg);
20539 }
20540 }
20541
20542 return unify_success (explain_p);
20543 }
20544
20545 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20546 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20547 parameters and return value are as for unify. */
20548
20549 static int
20550 unify_array_domain (tree tparms, tree targs,
20551 tree parm_dom, tree arg_dom,
20552 bool explain_p)
20553 {
20554 tree parm_max;
20555 tree arg_max;
20556 bool parm_cst;
20557 bool arg_cst;
20558
20559 /* Our representation of array types uses "N - 1" as the
20560 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20561 not an integer constant. We cannot unify arbitrarily
20562 complex expressions, so we eliminate the MINUS_EXPRs
20563 here. */
20564 parm_max = TYPE_MAX_VALUE (parm_dom);
20565 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20566 if (!parm_cst)
20567 {
20568 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20569 parm_max = TREE_OPERAND (parm_max, 0);
20570 }
20571 arg_max = TYPE_MAX_VALUE (arg_dom);
20572 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20573 if (!arg_cst)
20574 {
20575 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20576 trying to unify the type of a variable with the type
20577 of a template parameter. For example:
20578
20579 template <unsigned int N>
20580 void f (char (&) [N]);
20581 int g();
20582 void h(int i) {
20583 char a[g(i)];
20584 f(a);
20585 }
20586
20587 Here, the type of the ARG will be "int [g(i)]", and
20588 may be a SAVE_EXPR, etc. */
20589 if (TREE_CODE (arg_max) != MINUS_EXPR)
20590 return unify_vla_arg (explain_p, arg_dom);
20591 arg_max = TREE_OPERAND (arg_max, 0);
20592 }
20593
20594 /* If only one of the bounds used a MINUS_EXPR, compensate
20595 by adding one to the other bound. */
20596 if (parm_cst && !arg_cst)
20597 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20598 integer_type_node,
20599 parm_max,
20600 integer_one_node);
20601 else if (arg_cst && !parm_cst)
20602 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20603 integer_type_node,
20604 arg_max,
20605 integer_one_node);
20606
20607 return unify (tparms, targs, parm_max, arg_max,
20608 UNIFY_ALLOW_INTEGER, explain_p);
20609 }
20610
20611 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20612
20613 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20614
20615 static pa_kind_t
20616 pa_kind (tree t)
20617 {
20618 if (PACK_EXPANSION_P (t))
20619 t = PACK_EXPANSION_PATTERN (t);
20620 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20621 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20622 || DECL_TYPE_TEMPLATE_P (t))
20623 return pa_tmpl;
20624 else if (TYPE_P (t))
20625 return pa_type;
20626 else
20627 return pa_expr;
20628 }
20629
20630 /* Deduce the value of template parameters. TPARMS is the (innermost)
20631 set of template parameters to a template. TARGS is the bindings
20632 for those template parameters, as determined thus far; TARGS may
20633 include template arguments for outer levels of template parameters
20634 as well. PARM is a parameter to a template function, or a
20635 subcomponent of that parameter; ARG is the corresponding argument.
20636 This function attempts to match PARM with ARG in a manner
20637 consistent with the existing assignments in TARGS. If more values
20638 are deduced, then TARGS is updated.
20639
20640 Returns 0 if the type deduction succeeds, 1 otherwise. The
20641 parameter STRICT is a bitwise or of the following flags:
20642
20643 UNIFY_ALLOW_NONE:
20644 Require an exact match between PARM and ARG.
20645 UNIFY_ALLOW_MORE_CV_QUAL:
20646 Allow the deduced ARG to be more cv-qualified (by qualification
20647 conversion) than ARG.
20648 UNIFY_ALLOW_LESS_CV_QUAL:
20649 Allow the deduced ARG to be less cv-qualified than ARG.
20650 UNIFY_ALLOW_DERIVED:
20651 Allow the deduced ARG to be a template base class of ARG,
20652 or a pointer to a template base class of the type pointed to by
20653 ARG.
20654 UNIFY_ALLOW_INTEGER:
20655 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20656 case for more information.
20657 UNIFY_ALLOW_OUTER_LEVEL:
20658 This is the outermost level of a deduction. Used to determine validity
20659 of qualification conversions. A valid qualification conversion must
20660 have const qualified pointers leading up to the inner type which
20661 requires additional CV quals, except at the outer level, where const
20662 is not required [conv.qual]. It would be normal to set this flag in
20663 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20664 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20665 This is the outermost level of a deduction, and PARM can be more CV
20666 qualified at this point.
20667 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20668 This is the outermost level of a deduction, and PARM can be less CV
20669 qualified at this point. */
20670
20671 static int
20672 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20673 bool explain_p)
20674 {
20675 int idx;
20676 tree targ;
20677 tree tparm;
20678 int strict_in = strict;
20679 tsubst_flags_t complain = (explain_p
20680 ? tf_warning_or_error
20681 : tf_none);
20682
20683 /* I don't think this will do the right thing with respect to types.
20684 But the only case I've seen it in so far has been array bounds, where
20685 signedness is the only information lost, and I think that will be
20686 okay. */
20687 while (CONVERT_EXPR_P (parm))
20688 parm = TREE_OPERAND (parm, 0);
20689
20690 if (arg == error_mark_node)
20691 return unify_invalid (explain_p);
20692 if (arg == unknown_type_node
20693 || arg == init_list_type_node)
20694 /* We can't deduce anything from this, but we might get all the
20695 template args from other function args. */
20696 return unify_success (explain_p);
20697
20698 if (parm == any_targ_node || arg == any_targ_node)
20699 return unify_success (explain_p);
20700
20701 /* If PARM uses template parameters, then we can't bail out here,
20702 even if ARG == PARM, since we won't record unifications for the
20703 template parameters. We might need them if we're trying to
20704 figure out which of two things is more specialized. */
20705 if (arg == parm && !uses_template_parms (parm))
20706 return unify_success (explain_p);
20707
20708 /* Handle init lists early, so the rest of the function can assume
20709 we're dealing with a type. */
20710 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20711 {
20712 tree elt, elttype;
20713 unsigned i;
20714 tree orig_parm = parm;
20715
20716 /* Replace T with std::initializer_list<T> for deduction. */
20717 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20718 && flag_deduce_init_list)
20719 parm = listify (parm);
20720
20721 if (!is_std_init_list (parm)
20722 && TREE_CODE (parm) != ARRAY_TYPE)
20723 /* We can only deduce from an initializer list argument if the
20724 parameter is std::initializer_list or an array; otherwise this
20725 is a non-deduced context. */
20726 return unify_success (explain_p);
20727
20728 if (TREE_CODE (parm) == ARRAY_TYPE)
20729 elttype = TREE_TYPE (parm);
20730 else
20731 {
20732 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20733 /* Deduction is defined in terms of a single type, so just punt
20734 on the (bizarre) std::initializer_list<T...>. */
20735 if (PACK_EXPANSION_P (elttype))
20736 return unify_success (explain_p);
20737 }
20738
20739 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20740 {
20741 int elt_strict = strict;
20742
20743 if (elt == error_mark_node)
20744 return unify_invalid (explain_p);
20745
20746 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20747 {
20748 tree type = TREE_TYPE (elt);
20749 if (type == error_mark_node)
20750 return unify_invalid (explain_p);
20751 /* It should only be possible to get here for a call. */
20752 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20753 elt_strict |= maybe_adjust_types_for_deduction
20754 (DEDUCE_CALL, &elttype, &type, elt);
20755 elt = type;
20756 }
20757
20758 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20759 explain_p);
20760 }
20761
20762 if (TREE_CODE (parm) == ARRAY_TYPE
20763 && deducible_array_bound (TYPE_DOMAIN (parm)))
20764 {
20765 /* Also deduce from the length of the initializer list. */
20766 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20767 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20768 if (idx == error_mark_node)
20769 return unify_invalid (explain_p);
20770 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20771 idx, explain_p);
20772 }
20773
20774 /* If the std::initializer_list<T> deduction worked, replace the
20775 deduced A with std::initializer_list<A>. */
20776 if (orig_parm != parm)
20777 {
20778 idx = TEMPLATE_TYPE_IDX (orig_parm);
20779 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20780 targ = listify (targ);
20781 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20782 }
20783 return unify_success (explain_p);
20784 }
20785
20786 /* If parm and arg aren't the same kind of thing (template, type, or
20787 expression), fail early. */
20788 if (pa_kind (parm) != pa_kind (arg))
20789 return unify_invalid (explain_p);
20790
20791 /* Immediately reject some pairs that won't unify because of
20792 cv-qualification mismatches. */
20793 if (TREE_CODE (arg) == TREE_CODE (parm)
20794 && TYPE_P (arg)
20795 /* It is the elements of the array which hold the cv quals of an array
20796 type, and the elements might be template type parms. We'll check
20797 when we recurse. */
20798 && TREE_CODE (arg) != ARRAY_TYPE
20799 /* We check the cv-qualifiers when unifying with template type
20800 parameters below. We want to allow ARG `const T' to unify with
20801 PARM `T' for example, when computing which of two templates
20802 is more specialized, for example. */
20803 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20804 && !check_cv_quals_for_unify (strict_in, arg, parm))
20805 return unify_cv_qual_mismatch (explain_p, parm, arg);
20806
20807 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20808 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20809 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20810 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20811 strict &= ~UNIFY_ALLOW_DERIVED;
20812 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20813 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20814
20815 switch (TREE_CODE (parm))
20816 {
20817 case TYPENAME_TYPE:
20818 case SCOPE_REF:
20819 case UNBOUND_CLASS_TEMPLATE:
20820 /* In a type which contains a nested-name-specifier, template
20821 argument values cannot be deduced for template parameters used
20822 within the nested-name-specifier. */
20823 return unify_success (explain_p);
20824
20825 case TEMPLATE_TYPE_PARM:
20826 case TEMPLATE_TEMPLATE_PARM:
20827 case BOUND_TEMPLATE_TEMPLATE_PARM:
20828 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20829 if (error_operand_p (tparm))
20830 return unify_invalid (explain_p);
20831
20832 if (TEMPLATE_TYPE_LEVEL (parm)
20833 != template_decl_level (tparm))
20834 /* The PARM is not one we're trying to unify. Just check
20835 to see if it matches ARG. */
20836 {
20837 if (TREE_CODE (arg) == TREE_CODE (parm)
20838 && (is_auto (parm) ? is_auto (arg)
20839 : same_type_p (parm, arg)))
20840 return unify_success (explain_p);
20841 else
20842 return unify_type_mismatch (explain_p, parm, arg);
20843 }
20844 idx = TEMPLATE_TYPE_IDX (parm);
20845 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20846 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20847 if (error_operand_p (tparm))
20848 return unify_invalid (explain_p);
20849
20850 /* Check for mixed types and values. */
20851 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20852 && TREE_CODE (tparm) != TYPE_DECL)
20853 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20854 && TREE_CODE (tparm) != TEMPLATE_DECL))
20855 gcc_unreachable ();
20856
20857 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20858 {
20859 if ((strict_in & UNIFY_ALLOW_DERIVED)
20860 && CLASS_TYPE_P (arg))
20861 {
20862 /* First try to match ARG directly. */
20863 tree t = try_class_unification (tparms, targs, parm, arg,
20864 explain_p);
20865 if (!t)
20866 {
20867 /* Otherwise, look for a suitable base of ARG, as below. */
20868 enum template_base_result r;
20869 r = get_template_base (tparms, targs, parm, arg,
20870 explain_p, &t);
20871 if (!t)
20872 return unify_no_common_base (explain_p, r, parm, arg);
20873 arg = t;
20874 }
20875 }
20876 /* ARG must be constructed from a template class or a template
20877 template parameter. */
20878 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20879 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20880 return unify_template_deduction_failure (explain_p, parm, arg);
20881
20882 /* Deduce arguments T, i from TT<T> or TT<i>. */
20883 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20884 return 1;
20885
20886 arg = TYPE_TI_TEMPLATE (arg);
20887
20888 /* Fall through to deduce template name. */
20889 }
20890
20891 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20892 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20893 {
20894 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20895
20896 /* Simple cases: Value already set, does match or doesn't. */
20897 if (targ != NULL_TREE && template_args_equal (targ, arg))
20898 return unify_success (explain_p);
20899 else if (targ)
20900 return unify_inconsistency (explain_p, parm, targ, arg);
20901 }
20902 else
20903 {
20904 /* If PARM is `const T' and ARG is only `int', we don't have
20905 a match unless we are allowing additional qualification.
20906 If ARG is `const int' and PARM is just `T' that's OK;
20907 that binds `const int' to `T'. */
20908 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20909 arg, parm))
20910 return unify_cv_qual_mismatch (explain_p, parm, arg);
20911
20912 /* Consider the case where ARG is `const volatile int' and
20913 PARM is `const T'. Then, T should be `volatile int'. */
20914 arg = cp_build_qualified_type_real
20915 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20916 if (arg == error_mark_node)
20917 return unify_invalid (explain_p);
20918
20919 /* Simple cases: Value already set, does match or doesn't. */
20920 if (targ != NULL_TREE && same_type_p (targ, arg))
20921 return unify_success (explain_p);
20922 else if (targ)
20923 return unify_inconsistency (explain_p, parm, targ, arg);
20924
20925 /* Make sure that ARG is not a variable-sized array. (Note
20926 that were talking about variable-sized arrays (like
20927 `int[n]'), rather than arrays of unknown size (like
20928 `int[]').) We'll get very confused by such a type since
20929 the bound of the array is not constant, and therefore
20930 not mangleable. Besides, such types are not allowed in
20931 ISO C++, so we can do as we please here. We do allow
20932 them for 'auto' deduction, since that isn't ABI-exposed. */
20933 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20934 return unify_vla_arg (explain_p, arg);
20935
20936 /* Strip typedefs as in convert_template_argument. */
20937 arg = canonicalize_type_argument (arg, tf_none);
20938 }
20939
20940 /* If ARG is a parameter pack or an expansion, we cannot unify
20941 against it unless PARM is also a parameter pack. */
20942 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20943 && !template_parameter_pack_p (parm))
20944 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20945
20946 /* If the argument deduction results is a METHOD_TYPE,
20947 then there is a problem.
20948 METHOD_TYPE doesn't map to any real C++ type the result of
20949 the deduction can not be of that type. */
20950 if (TREE_CODE (arg) == METHOD_TYPE)
20951 return unify_method_type_error (explain_p, arg);
20952
20953 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20954 return unify_success (explain_p);
20955
20956 case TEMPLATE_PARM_INDEX:
20957 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20958 if (error_operand_p (tparm))
20959 return unify_invalid (explain_p);
20960
20961 if (TEMPLATE_PARM_LEVEL (parm)
20962 != template_decl_level (tparm))
20963 {
20964 /* The PARM is not one we're trying to unify. Just check
20965 to see if it matches ARG. */
20966 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20967 && cp_tree_equal (parm, arg));
20968 if (result)
20969 unify_expression_unequal (explain_p, parm, arg);
20970 return result;
20971 }
20972
20973 idx = TEMPLATE_PARM_IDX (parm);
20974 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20975
20976 if (targ)
20977 {
20978 if ((strict & UNIFY_ALLOW_INTEGER)
20979 && TREE_TYPE (targ) && TREE_TYPE (arg)
20980 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
20981 /* We're deducing from an array bound, the type doesn't matter. */
20982 arg = fold_convert (TREE_TYPE (targ), arg);
20983 int x = !cp_tree_equal (targ, arg);
20984 if (x)
20985 unify_inconsistency (explain_p, parm, targ, arg);
20986 return x;
20987 }
20988
20989 /* [temp.deduct.type] If, in the declaration of a function template
20990 with a non-type template-parameter, the non-type
20991 template-parameter is used in an expression in the function
20992 parameter-list and, if the corresponding template-argument is
20993 deduced, the template-argument type shall match the type of the
20994 template-parameter exactly, except that a template-argument
20995 deduced from an array bound may be of any integral type.
20996 The non-type parameter might use already deduced type parameters. */
20997 ++processing_template_decl;
20998 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20999 --processing_template_decl;
21000 if (tree a = type_uses_auto (tparm))
21001 {
21002 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21003 if (tparm == error_mark_node)
21004 return 1;
21005 }
21006
21007 if (!TREE_TYPE (arg))
21008 /* Template-parameter dependent expression. Just accept it for now.
21009 It will later be processed in convert_template_argument. */
21010 ;
21011 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21012 non_reference (tparm)))
21013 /* OK */;
21014 else if ((strict & UNIFY_ALLOW_INTEGER)
21015 && CP_INTEGRAL_TYPE_P (tparm))
21016 /* Convert the ARG to the type of PARM; the deduced non-type
21017 template argument must exactly match the types of the
21018 corresponding parameter. */
21019 arg = fold (build_nop (tparm, arg));
21020 else if (uses_template_parms (tparm))
21021 {
21022 /* We haven't deduced the type of this parameter yet. */
21023 if (cxx_dialect >= cxx17
21024 /* We deduce from array bounds in try_array_deduction. */
21025 && !(strict & UNIFY_ALLOW_INTEGER))
21026 {
21027 /* Deduce it from the non-type argument. */
21028 tree atype = TREE_TYPE (arg);
21029 RECUR_AND_CHECK_FAILURE (tparms, targs,
21030 tparm, atype,
21031 UNIFY_ALLOW_NONE, explain_p);
21032 }
21033 else
21034 /* Try again later. */
21035 return unify_success (explain_p);
21036 }
21037 else
21038 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21039
21040 /* If ARG is a parameter pack or an expansion, we cannot unify
21041 against it unless PARM is also a parameter pack. */
21042 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21043 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21044 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21045
21046 {
21047 bool removed_attr = false;
21048 arg = strip_typedefs_expr (arg, &removed_attr);
21049 }
21050 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21051 return unify_success (explain_p);
21052
21053 case PTRMEM_CST:
21054 {
21055 /* A pointer-to-member constant can be unified only with
21056 another constant. */
21057 if (TREE_CODE (arg) != PTRMEM_CST)
21058 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21059
21060 /* Just unify the class member. It would be useless (and possibly
21061 wrong, depending on the strict flags) to unify also
21062 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21063 arg refer to the same variable, even if through different
21064 classes. For instance:
21065
21066 struct A { int x; };
21067 struct B : A { };
21068
21069 Unification of &A::x and &B::x must succeed. */
21070 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21071 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21072 }
21073
21074 case POINTER_TYPE:
21075 {
21076 if (!TYPE_PTR_P (arg))
21077 return unify_type_mismatch (explain_p, parm, arg);
21078
21079 /* [temp.deduct.call]
21080
21081 A can be another pointer or pointer to member type that can
21082 be converted to the deduced A via a qualification
21083 conversion (_conv.qual_).
21084
21085 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21086 This will allow for additional cv-qualification of the
21087 pointed-to types if appropriate. */
21088
21089 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21090 /* The derived-to-base conversion only persists through one
21091 level of pointers. */
21092 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21093
21094 return unify (tparms, targs, TREE_TYPE (parm),
21095 TREE_TYPE (arg), strict, explain_p);
21096 }
21097
21098 case REFERENCE_TYPE:
21099 if (TREE_CODE (arg) != REFERENCE_TYPE)
21100 return unify_type_mismatch (explain_p, parm, arg);
21101 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21102 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21103
21104 case ARRAY_TYPE:
21105 if (TREE_CODE (arg) != ARRAY_TYPE)
21106 return unify_type_mismatch (explain_p, parm, arg);
21107 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21108 != (TYPE_DOMAIN (arg) == NULL_TREE))
21109 return unify_type_mismatch (explain_p, parm, arg);
21110 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21111 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21112 if (TYPE_DOMAIN (parm) != NULL_TREE)
21113 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21114 TYPE_DOMAIN (arg), explain_p);
21115 return unify_success (explain_p);
21116
21117 case REAL_TYPE:
21118 case COMPLEX_TYPE:
21119 case VECTOR_TYPE:
21120 case INTEGER_TYPE:
21121 case BOOLEAN_TYPE:
21122 case ENUMERAL_TYPE:
21123 case VOID_TYPE:
21124 case NULLPTR_TYPE:
21125 if (TREE_CODE (arg) != TREE_CODE (parm))
21126 return unify_type_mismatch (explain_p, parm, arg);
21127
21128 /* We have already checked cv-qualification at the top of the
21129 function. */
21130 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21131 return unify_type_mismatch (explain_p, parm, arg);
21132
21133 /* As far as unification is concerned, this wins. Later checks
21134 will invalidate it if necessary. */
21135 return unify_success (explain_p);
21136
21137 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21138 /* Type INTEGER_CST can come from ordinary constant template args. */
21139 case INTEGER_CST:
21140 while (CONVERT_EXPR_P (arg))
21141 arg = TREE_OPERAND (arg, 0);
21142
21143 if (TREE_CODE (arg) != INTEGER_CST)
21144 return unify_template_argument_mismatch (explain_p, parm, arg);
21145 return (tree_int_cst_equal (parm, arg)
21146 ? unify_success (explain_p)
21147 : unify_template_argument_mismatch (explain_p, parm, arg));
21148
21149 case TREE_VEC:
21150 {
21151 int i, len, argslen;
21152 int parm_variadic_p = 0;
21153
21154 if (TREE_CODE (arg) != TREE_VEC)
21155 return unify_template_argument_mismatch (explain_p, parm, arg);
21156
21157 len = TREE_VEC_LENGTH (parm);
21158 argslen = TREE_VEC_LENGTH (arg);
21159
21160 /* Check for pack expansions in the parameters. */
21161 for (i = 0; i < len; ++i)
21162 {
21163 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21164 {
21165 if (i == len - 1)
21166 /* We can unify against something with a trailing
21167 parameter pack. */
21168 parm_variadic_p = 1;
21169 else
21170 /* [temp.deduct.type]/9: If the template argument list of
21171 P contains a pack expansion that is not the last
21172 template argument, the entire template argument list
21173 is a non-deduced context. */
21174 return unify_success (explain_p);
21175 }
21176 }
21177
21178 /* If we don't have enough arguments to satisfy the parameters
21179 (not counting the pack expression at the end), or we have
21180 too many arguments for a parameter list that doesn't end in
21181 a pack expression, we can't unify. */
21182 if (parm_variadic_p
21183 ? argslen < len - parm_variadic_p
21184 : argslen != len)
21185 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21186
21187 /* Unify all of the parameters that precede the (optional)
21188 pack expression. */
21189 for (i = 0; i < len - parm_variadic_p; ++i)
21190 {
21191 RECUR_AND_CHECK_FAILURE (tparms, targs,
21192 TREE_VEC_ELT (parm, i),
21193 TREE_VEC_ELT (arg, i),
21194 UNIFY_ALLOW_NONE, explain_p);
21195 }
21196 if (parm_variadic_p)
21197 return unify_pack_expansion (tparms, targs, parm, arg,
21198 DEDUCE_EXACT,
21199 /*subr=*/true, explain_p);
21200 return unify_success (explain_p);
21201 }
21202
21203 case RECORD_TYPE:
21204 case UNION_TYPE:
21205 if (TREE_CODE (arg) != TREE_CODE (parm))
21206 return unify_type_mismatch (explain_p, parm, arg);
21207
21208 if (TYPE_PTRMEMFUNC_P (parm))
21209 {
21210 if (!TYPE_PTRMEMFUNC_P (arg))
21211 return unify_type_mismatch (explain_p, parm, arg);
21212
21213 return unify (tparms, targs,
21214 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21215 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21216 strict, explain_p);
21217 }
21218 else if (TYPE_PTRMEMFUNC_P (arg))
21219 return unify_type_mismatch (explain_p, parm, arg);
21220
21221 if (CLASSTYPE_TEMPLATE_INFO (parm))
21222 {
21223 tree t = NULL_TREE;
21224
21225 if (strict_in & UNIFY_ALLOW_DERIVED)
21226 {
21227 /* First, we try to unify the PARM and ARG directly. */
21228 t = try_class_unification (tparms, targs,
21229 parm, arg, explain_p);
21230
21231 if (!t)
21232 {
21233 /* Fallback to the special case allowed in
21234 [temp.deduct.call]:
21235
21236 If P is a class, and P has the form
21237 template-id, then A can be a derived class of
21238 the deduced A. Likewise, if P is a pointer to
21239 a class of the form template-id, A can be a
21240 pointer to a derived class pointed to by the
21241 deduced A. */
21242 enum template_base_result r;
21243 r = get_template_base (tparms, targs, parm, arg,
21244 explain_p, &t);
21245
21246 if (!t)
21247 {
21248 /* Don't give the derived diagnostic if we're
21249 already dealing with the same template. */
21250 bool same_template
21251 = (CLASSTYPE_TEMPLATE_INFO (arg)
21252 && (CLASSTYPE_TI_TEMPLATE (parm)
21253 == CLASSTYPE_TI_TEMPLATE (arg)));
21254 return unify_no_common_base (explain_p && !same_template,
21255 r, parm, arg);
21256 }
21257 }
21258 }
21259 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21260 && (CLASSTYPE_TI_TEMPLATE (parm)
21261 == CLASSTYPE_TI_TEMPLATE (arg)))
21262 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21263 Then, we should unify `int' and `U'. */
21264 t = arg;
21265 else
21266 /* There's no chance of unification succeeding. */
21267 return unify_type_mismatch (explain_p, parm, arg);
21268
21269 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21270 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21271 }
21272 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21273 return unify_type_mismatch (explain_p, parm, arg);
21274 return unify_success (explain_p);
21275
21276 case METHOD_TYPE:
21277 case FUNCTION_TYPE:
21278 {
21279 unsigned int nargs;
21280 tree *args;
21281 tree a;
21282 unsigned int i;
21283
21284 if (TREE_CODE (arg) != TREE_CODE (parm))
21285 return unify_type_mismatch (explain_p, parm, arg);
21286
21287 /* CV qualifications for methods can never be deduced, they must
21288 match exactly. We need to check them explicitly here,
21289 because type_unification_real treats them as any other
21290 cv-qualified parameter. */
21291 if (TREE_CODE (parm) == METHOD_TYPE
21292 && (!check_cv_quals_for_unify
21293 (UNIFY_ALLOW_NONE,
21294 class_of_this_parm (arg),
21295 class_of_this_parm (parm))))
21296 return unify_cv_qual_mismatch (explain_p, parm, arg);
21297 if (TREE_CODE (arg) == FUNCTION_TYPE
21298 && type_memfn_quals (parm) != type_memfn_quals (arg))
21299 return unify_cv_qual_mismatch (explain_p, parm, arg);
21300 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21301 return unify_type_mismatch (explain_p, parm, arg);
21302
21303 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21304 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21305
21306 nargs = list_length (TYPE_ARG_TYPES (arg));
21307 args = XALLOCAVEC (tree, nargs);
21308 for (a = TYPE_ARG_TYPES (arg), i = 0;
21309 a != NULL_TREE && a != void_list_node;
21310 a = TREE_CHAIN (a), ++i)
21311 args[i] = TREE_VALUE (a);
21312 nargs = i;
21313
21314 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21315 args, nargs, 1, DEDUCE_EXACT,
21316 LOOKUP_NORMAL, NULL, explain_p))
21317 return 1;
21318
21319 if (flag_noexcept_type)
21320 {
21321 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21322 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21323 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21324 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21325 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21326 && uses_template_parms (TREE_PURPOSE (pspec)))
21327 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21328 TREE_PURPOSE (aspec),
21329 UNIFY_ALLOW_NONE, explain_p);
21330 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21331 return unify_type_mismatch (explain_p, parm, arg);
21332 }
21333
21334 return 0;
21335 }
21336
21337 case OFFSET_TYPE:
21338 /* Unify a pointer to member with a pointer to member function, which
21339 deduces the type of the member as a function type. */
21340 if (TYPE_PTRMEMFUNC_P (arg))
21341 {
21342 /* Check top-level cv qualifiers */
21343 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21344 return unify_cv_qual_mismatch (explain_p, parm, arg);
21345
21346 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21347 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21348 UNIFY_ALLOW_NONE, explain_p);
21349
21350 /* Determine the type of the function we are unifying against. */
21351 tree fntype = static_fn_type (arg);
21352
21353 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21354 }
21355
21356 if (TREE_CODE (arg) != OFFSET_TYPE)
21357 return unify_type_mismatch (explain_p, parm, arg);
21358 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21359 TYPE_OFFSET_BASETYPE (arg),
21360 UNIFY_ALLOW_NONE, explain_p);
21361 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21362 strict, explain_p);
21363
21364 case CONST_DECL:
21365 if (DECL_TEMPLATE_PARM_P (parm))
21366 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21367 if (arg != scalar_constant_value (parm))
21368 return unify_template_argument_mismatch (explain_p, parm, arg);
21369 return unify_success (explain_p);
21370
21371 case FIELD_DECL:
21372 case TEMPLATE_DECL:
21373 /* Matched cases are handled by the ARG == PARM test above. */
21374 return unify_template_argument_mismatch (explain_p, parm, arg);
21375
21376 case VAR_DECL:
21377 /* We might get a variable as a non-type template argument in parm if the
21378 corresponding parameter is type-dependent. Make any necessary
21379 adjustments based on whether arg is a reference. */
21380 if (CONSTANT_CLASS_P (arg))
21381 parm = fold_non_dependent_expr (parm);
21382 else if (REFERENCE_REF_P (arg))
21383 {
21384 tree sub = TREE_OPERAND (arg, 0);
21385 STRIP_NOPS (sub);
21386 if (TREE_CODE (sub) == ADDR_EXPR)
21387 arg = TREE_OPERAND (sub, 0);
21388 }
21389 /* Now use the normal expression code to check whether they match. */
21390 goto expr;
21391
21392 case TYPE_ARGUMENT_PACK:
21393 case NONTYPE_ARGUMENT_PACK:
21394 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21395 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21396
21397 case TYPEOF_TYPE:
21398 case DECLTYPE_TYPE:
21399 case UNDERLYING_TYPE:
21400 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21401 or UNDERLYING_TYPE nodes. */
21402 return unify_success (explain_p);
21403
21404 case ERROR_MARK:
21405 /* Unification fails if we hit an error node. */
21406 return unify_invalid (explain_p);
21407
21408 case INDIRECT_REF:
21409 if (REFERENCE_REF_P (parm))
21410 {
21411 bool pexp = PACK_EXPANSION_P (arg);
21412 if (pexp)
21413 arg = PACK_EXPANSION_PATTERN (arg);
21414 if (REFERENCE_REF_P (arg))
21415 arg = TREE_OPERAND (arg, 0);
21416 if (pexp)
21417 arg = make_pack_expansion (arg, complain);
21418 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21419 strict, explain_p);
21420 }
21421 /* FALLTHRU */
21422
21423 default:
21424 /* An unresolved overload is a nondeduced context. */
21425 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21426 return unify_success (explain_p);
21427 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21428 expr:
21429 /* We must be looking at an expression. This can happen with
21430 something like:
21431
21432 template <int I>
21433 void foo(S<I>, S<I + 2>);
21434
21435 This is a "nondeduced context":
21436
21437 [deduct.type]
21438
21439 The nondeduced contexts are:
21440
21441 --A type that is a template-id in which one or more of
21442 the template-arguments is an expression that references
21443 a template-parameter.
21444
21445 In these cases, we assume deduction succeeded, but don't
21446 actually infer any unifications. */
21447
21448 if (!uses_template_parms (parm)
21449 && !template_args_equal (parm, arg))
21450 return unify_expression_unequal (explain_p, parm, arg);
21451 else
21452 return unify_success (explain_p);
21453 }
21454 }
21455 #undef RECUR_AND_CHECK_FAILURE
21456 \f
21457 /* Note that DECL can be defined in this translation unit, if
21458 required. */
21459
21460 static void
21461 mark_definable (tree decl)
21462 {
21463 tree clone;
21464 DECL_NOT_REALLY_EXTERN (decl) = 1;
21465 FOR_EACH_CLONE (clone, decl)
21466 DECL_NOT_REALLY_EXTERN (clone) = 1;
21467 }
21468
21469 /* Called if RESULT is explicitly instantiated, or is a member of an
21470 explicitly instantiated class. */
21471
21472 void
21473 mark_decl_instantiated (tree result, int extern_p)
21474 {
21475 SET_DECL_EXPLICIT_INSTANTIATION (result);
21476
21477 /* If this entity has already been written out, it's too late to
21478 make any modifications. */
21479 if (TREE_ASM_WRITTEN (result))
21480 return;
21481
21482 /* For anonymous namespace we don't need to do anything. */
21483 if (decl_anon_ns_mem_p (result))
21484 {
21485 gcc_assert (!TREE_PUBLIC (result));
21486 return;
21487 }
21488
21489 if (TREE_CODE (result) != FUNCTION_DECL)
21490 /* The TREE_PUBLIC flag for function declarations will have been
21491 set correctly by tsubst. */
21492 TREE_PUBLIC (result) = 1;
21493
21494 /* This might have been set by an earlier implicit instantiation. */
21495 DECL_COMDAT (result) = 0;
21496
21497 if (extern_p)
21498 DECL_NOT_REALLY_EXTERN (result) = 0;
21499 else
21500 {
21501 mark_definable (result);
21502 mark_needed (result);
21503 /* Always make artificials weak. */
21504 if (DECL_ARTIFICIAL (result) && flag_weak)
21505 comdat_linkage (result);
21506 /* For WIN32 we also want to put explicit instantiations in
21507 linkonce sections. */
21508 else if (TREE_PUBLIC (result))
21509 maybe_make_one_only (result);
21510 }
21511
21512 /* If EXTERN_P, then this function will not be emitted -- unless
21513 followed by an explicit instantiation, at which point its linkage
21514 will be adjusted. If !EXTERN_P, then this function will be
21515 emitted here. In neither circumstance do we want
21516 import_export_decl to adjust the linkage. */
21517 DECL_INTERFACE_KNOWN (result) = 1;
21518 }
21519
21520 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21521 important template arguments. If any are missing, we check whether
21522 they're important by using error_mark_node for substituting into any
21523 args that were used for partial ordering (the ones between ARGS and END)
21524 and seeing if it bubbles up. */
21525
21526 static bool
21527 check_undeduced_parms (tree targs, tree args, tree end)
21528 {
21529 bool found = false;
21530 int i;
21531 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21532 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21533 {
21534 found = true;
21535 TREE_VEC_ELT (targs, i) = error_mark_node;
21536 }
21537 if (found)
21538 {
21539 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21540 if (substed == error_mark_node)
21541 return true;
21542 }
21543 return false;
21544 }
21545
21546 /* Given two function templates PAT1 and PAT2, return:
21547
21548 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21549 -1 if PAT2 is more specialized than PAT1.
21550 0 if neither is more specialized.
21551
21552 LEN indicates the number of parameters we should consider
21553 (defaulted parameters should not be considered).
21554
21555 The 1998 std underspecified function template partial ordering, and
21556 DR214 addresses the issue. We take pairs of arguments, one from
21557 each of the templates, and deduce them against each other. One of
21558 the templates will be more specialized if all the *other*
21559 template's arguments deduce against its arguments and at least one
21560 of its arguments *does* *not* deduce against the other template's
21561 corresponding argument. Deduction is done as for class templates.
21562 The arguments used in deduction have reference and top level cv
21563 qualifiers removed. Iff both arguments were originally reference
21564 types *and* deduction succeeds in both directions, an lvalue reference
21565 wins against an rvalue reference and otherwise the template
21566 with the more cv-qualified argument wins for that pairing (if
21567 neither is more cv-qualified, they both are equal). Unlike regular
21568 deduction, after all the arguments have been deduced in this way,
21569 we do *not* verify the deduced template argument values can be
21570 substituted into non-deduced contexts.
21571
21572 The logic can be a bit confusing here, because we look at deduce1 and
21573 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21574 can find template arguments for pat1 to make arg1 look like arg2, that
21575 means that arg2 is at least as specialized as arg1. */
21576
21577 int
21578 more_specialized_fn (tree pat1, tree pat2, int len)
21579 {
21580 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21581 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21582 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21583 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21584 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21585 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21586 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21587 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21588 tree origs1, origs2;
21589 bool lose1 = false;
21590 bool lose2 = false;
21591
21592 /* Remove the this parameter from non-static member functions. If
21593 one is a non-static member function and the other is not a static
21594 member function, remove the first parameter from that function
21595 also. This situation occurs for operator functions where we
21596 locate both a member function (with this pointer) and non-member
21597 operator (with explicit first operand). */
21598 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21599 {
21600 len--; /* LEN is the number of significant arguments for DECL1 */
21601 args1 = TREE_CHAIN (args1);
21602 if (!DECL_STATIC_FUNCTION_P (decl2))
21603 args2 = TREE_CHAIN (args2);
21604 }
21605 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21606 {
21607 args2 = TREE_CHAIN (args2);
21608 if (!DECL_STATIC_FUNCTION_P (decl1))
21609 {
21610 len--;
21611 args1 = TREE_CHAIN (args1);
21612 }
21613 }
21614
21615 /* If only one is a conversion operator, they are unordered. */
21616 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21617 return 0;
21618
21619 /* Consider the return type for a conversion function */
21620 if (DECL_CONV_FN_P (decl1))
21621 {
21622 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21623 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21624 len++;
21625 }
21626
21627 processing_template_decl++;
21628
21629 origs1 = args1;
21630 origs2 = args2;
21631
21632 while (len--
21633 /* Stop when an ellipsis is seen. */
21634 && args1 != NULL_TREE && args2 != NULL_TREE)
21635 {
21636 tree arg1 = TREE_VALUE (args1);
21637 tree arg2 = TREE_VALUE (args2);
21638 int deduce1, deduce2;
21639 int quals1 = -1;
21640 int quals2 = -1;
21641 int ref1 = 0;
21642 int ref2 = 0;
21643
21644 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21645 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21646 {
21647 /* When both arguments are pack expansions, we need only
21648 unify the patterns themselves. */
21649 arg1 = PACK_EXPANSION_PATTERN (arg1);
21650 arg2 = PACK_EXPANSION_PATTERN (arg2);
21651
21652 /* This is the last comparison we need to do. */
21653 len = 0;
21654 }
21655
21656 /* DR 1847: If a particular P contains no template-parameters that
21657 participate in template argument deduction, that P is not used to
21658 determine the ordering. */
21659 if (!uses_deducible_template_parms (arg1)
21660 && !uses_deducible_template_parms (arg2))
21661 goto next;
21662
21663 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21664 {
21665 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21666 arg1 = TREE_TYPE (arg1);
21667 quals1 = cp_type_quals (arg1);
21668 }
21669
21670 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21671 {
21672 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21673 arg2 = TREE_TYPE (arg2);
21674 quals2 = cp_type_quals (arg2);
21675 }
21676
21677 arg1 = TYPE_MAIN_VARIANT (arg1);
21678 arg2 = TYPE_MAIN_VARIANT (arg2);
21679
21680 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21681 {
21682 int i, len2 = remaining_arguments (args2);
21683 tree parmvec = make_tree_vec (1);
21684 tree argvec = make_tree_vec (len2);
21685 tree ta = args2;
21686
21687 /* Setup the parameter vector, which contains only ARG1. */
21688 TREE_VEC_ELT (parmvec, 0) = arg1;
21689
21690 /* Setup the argument vector, which contains the remaining
21691 arguments. */
21692 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21693 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21694
21695 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21696 argvec, DEDUCE_EXACT,
21697 /*subr=*/true, /*explain_p=*/false)
21698 == 0);
21699
21700 /* We cannot deduce in the other direction, because ARG1 is
21701 a pack expansion but ARG2 is not. */
21702 deduce2 = 0;
21703 }
21704 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21705 {
21706 int i, len1 = remaining_arguments (args1);
21707 tree parmvec = make_tree_vec (1);
21708 tree argvec = make_tree_vec (len1);
21709 tree ta = args1;
21710
21711 /* Setup the parameter vector, which contains only ARG1. */
21712 TREE_VEC_ELT (parmvec, 0) = arg2;
21713
21714 /* Setup the argument vector, which contains the remaining
21715 arguments. */
21716 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21717 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21718
21719 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21720 argvec, DEDUCE_EXACT,
21721 /*subr=*/true, /*explain_p=*/false)
21722 == 0);
21723
21724 /* We cannot deduce in the other direction, because ARG2 is
21725 a pack expansion but ARG1 is not.*/
21726 deduce1 = 0;
21727 }
21728
21729 else
21730 {
21731 /* The normal case, where neither argument is a pack
21732 expansion. */
21733 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21734 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21735 == 0);
21736 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21737 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21738 == 0);
21739 }
21740
21741 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21742 arg2, then arg2 is not as specialized as arg1. */
21743 if (!deduce1)
21744 lose2 = true;
21745 if (!deduce2)
21746 lose1 = true;
21747
21748 /* "If, for a given type, deduction succeeds in both directions
21749 (i.e., the types are identical after the transformations above)
21750 and both P and A were reference types (before being replaced with
21751 the type referred to above):
21752 - if the type from the argument template was an lvalue reference and
21753 the type from the parameter template was not, the argument type is
21754 considered to be more specialized than the other; otherwise,
21755 - if the type from the argument template is more cv-qualified
21756 than the type from the parameter template (as described above),
21757 the argument type is considered to be more specialized than the other;
21758 otherwise,
21759 - neither type is more specialized than the other." */
21760
21761 if (deduce1 && deduce2)
21762 {
21763 if (ref1 && ref2 && ref1 != ref2)
21764 {
21765 if (ref1 > ref2)
21766 lose1 = true;
21767 else
21768 lose2 = true;
21769 }
21770 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21771 {
21772 if ((quals1 & quals2) == quals2)
21773 lose2 = true;
21774 if ((quals1 & quals2) == quals1)
21775 lose1 = true;
21776 }
21777 }
21778
21779 if (lose1 && lose2)
21780 /* We've failed to deduce something in either direction.
21781 These must be unordered. */
21782 break;
21783
21784 next:
21785
21786 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21787 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21788 /* We have already processed all of the arguments in our
21789 handing of the pack expansion type. */
21790 len = 0;
21791
21792 args1 = TREE_CHAIN (args1);
21793 args2 = TREE_CHAIN (args2);
21794 }
21795
21796 /* "In most cases, all template parameters must have values in order for
21797 deduction to succeed, but for partial ordering purposes a template
21798 parameter may remain without a value provided it is not used in the
21799 types being used for partial ordering."
21800
21801 Thus, if we are missing any of the targs1 we need to substitute into
21802 origs1, then pat2 is not as specialized as pat1. This can happen when
21803 there is a nondeduced context. */
21804 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21805 lose2 = true;
21806 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21807 lose1 = true;
21808
21809 processing_template_decl--;
21810
21811 /* If both deductions succeed, the partial ordering selects the more
21812 constrained template. */
21813 if (!lose1 && !lose2)
21814 {
21815 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21816 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21817 lose1 = !subsumes_constraints (c1, c2);
21818 lose2 = !subsumes_constraints (c2, c1);
21819 }
21820
21821 /* All things being equal, if the next argument is a pack expansion
21822 for one function but not for the other, prefer the
21823 non-variadic function. FIXME this is bogus; see c++/41958. */
21824 if (lose1 == lose2
21825 && args1 && TREE_VALUE (args1)
21826 && args2 && TREE_VALUE (args2))
21827 {
21828 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21829 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21830 }
21831
21832 if (lose1 == lose2)
21833 return 0;
21834 else if (!lose1)
21835 return 1;
21836 else
21837 return -1;
21838 }
21839
21840 /* Determine which of two partial specializations of TMPL is more
21841 specialized.
21842
21843 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21844 to the first partial specialization. The TREE_PURPOSE is the
21845 innermost set of template parameters for the partial
21846 specialization. PAT2 is similar, but for the second template.
21847
21848 Return 1 if the first partial specialization is more specialized;
21849 -1 if the second is more specialized; 0 if neither is more
21850 specialized.
21851
21852 See [temp.class.order] for information about determining which of
21853 two templates is more specialized. */
21854
21855 static int
21856 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21857 {
21858 tree targs;
21859 int winner = 0;
21860 bool any_deductions = false;
21861
21862 tree tmpl1 = TREE_VALUE (pat1);
21863 tree tmpl2 = TREE_VALUE (pat2);
21864 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21865 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21866
21867 /* Just like what happens for functions, if we are ordering between
21868 different template specializations, we may encounter dependent
21869 types in the arguments, and we need our dependency check functions
21870 to behave correctly. */
21871 ++processing_template_decl;
21872 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21873 if (targs)
21874 {
21875 --winner;
21876 any_deductions = true;
21877 }
21878
21879 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21880 if (targs)
21881 {
21882 ++winner;
21883 any_deductions = true;
21884 }
21885 --processing_template_decl;
21886
21887 /* If both deductions succeed, the partial ordering selects the more
21888 constrained template. */
21889 if (!winner && any_deductions)
21890 return more_constrained (tmpl1, tmpl2);
21891
21892 /* In the case of a tie where at least one of the templates
21893 has a parameter pack at the end, the template with the most
21894 non-packed parameters wins. */
21895 if (winner == 0
21896 && any_deductions
21897 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21898 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21899 {
21900 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21901 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21902 int len1 = TREE_VEC_LENGTH (args1);
21903 int len2 = TREE_VEC_LENGTH (args2);
21904
21905 /* We don't count the pack expansion at the end. */
21906 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21907 --len1;
21908 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21909 --len2;
21910
21911 if (len1 > len2)
21912 return 1;
21913 else if (len1 < len2)
21914 return -1;
21915 }
21916
21917 return winner;
21918 }
21919
21920 /* Return the template arguments that will produce the function signature
21921 DECL from the function template FN, with the explicit template
21922 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21923 also match. Return NULL_TREE if no satisfactory arguments could be
21924 found. */
21925
21926 static tree
21927 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21928 {
21929 int ntparms = DECL_NTPARMS (fn);
21930 tree targs = make_tree_vec (ntparms);
21931 tree decl_type = TREE_TYPE (decl);
21932 tree decl_arg_types;
21933 tree *args;
21934 unsigned int nargs, ix;
21935 tree arg;
21936
21937 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21938
21939 /* Never do unification on the 'this' parameter. */
21940 decl_arg_types = skip_artificial_parms_for (decl,
21941 TYPE_ARG_TYPES (decl_type));
21942
21943 nargs = list_length (decl_arg_types);
21944 args = XALLOCAVEC (tree, nargs);
21945 for (arg = decl_arg_types, ix = 0;
21946 arg != NULL_TREE && arg != void_list_node;
21947 arg = TREE_CHAIN (arg), ++ix)
21948 args[ix] = TREE_VALUE (arg);
21949
21950 if (fn_type_unification (fn, explicit_args, targs,
21951 args, ix,
21952 (check_rettype || DECL_CONV_FN_P (fn)
21953 ? TREE_TYPE (decl_type) : NULL_TREE),
21954 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21955 /*decltype*/false)
21956 == error_mark_node)
21957 return NULL_TREE;
21958
21959 return targs;
21960 }
21961
21962 /* Return the innermost template arguments that, when applied to a partial
21963 specialization SPEC_TMPL of TMPL, yield the ARGS.
21964
21965 For example, suppose we have:
21966
21967 template <class T, class U> struct S {};
21968 template <class T> struct S<T*, int> {};
21969
21970 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21971 partial specialization and the ARGS will be {double*, int}. The resulting
21972 vector will be {double}, indicating that `T' is bound to `double'. */
21973
21974 static tree
21975 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21976 {
21977 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21978 tree spec_args
21979 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21980 int i, ntparms = TREE_VEC_LENGTH (tparms);
21981 tree deduced_args;
21982 tree innermost_deduced_args;
21983
21984 innermost_deduced_args = make_tree_vec (ntparms);
21985 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21986 {
21987 deduced_args = copy_node (args);
21988 SET_TMPL_ARGS_LEVEL (deduced_args,
21989 TMPL_ARGS_DEPTH (deduced_args),
21990 innermost_deduced_args);
21991 }
21992 else
21993 deduced_args = innermost_deduced_args;
21994
21995 bool tried_array_deduction = (cxx_dialect < cxx17);
21996 again:
21997 if (unify (tparms, deduced_args,
21998 INNERMOST_TEMPLATE_ARGS (spec_args),
21999 INNERMOST_TEMPLATE_ARGS (args),
22000 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22001 return NULL_TREE;
22002
22003 for (i = 0; i < ntparms; ++i)
22004 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22005 {
22006 if (!tried_array_deduction)
22007 {
22008 try_array_deduction (tparms, innermost_deduced_args,
22009 INNERMOST_TEMPLATE_ARGS (spec_args));
22010 tried_array_deduction = true;
22011 if (TREE_VEC_ELT (innermost_deduced_args, i))
22012 goto again;
22013 }
22014 return NULL_TREE;
22015 }
22016
22017 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22018 if (!push_tinst_level (tinst))
22019 {
22020 excessive_deduction_depth = true;
22021 return NULL_TREE;
22022 }
22023
22024 /* Verify that nondeduced template arguments agree with the type
22025 obtained from argument deduction.
22026
22027 For example:
22028
22029 struct A { typedef int X; };
22030 template <class T, class U> struct C {};
22031 template <class T> struct C<T, typename T::X> {};
22032
22033 Then with the instantiation `C<A, int>', we can deduce that
22034 `T' is `A' but unify () does not check whether `typename T::X'
22035 is `int'. */
22036 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22037
22038 if (spec_args != error_mark_node)
22039 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22040 INNERMOST_TEMPLATE_ARGS (spec_args),
22041 tmpl, tf_none, false, false);
22042
22043 pop_tinst_level ();
22044
22045 if (spec_args == error_mark_node
22046 /* We only need to check the innermost arguments; the other
22047 arguments will always agree. */
22048 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22049 INNERMOST_TEMPLATE_ARGS (args)))
22050 return NULL_TREE;
22051
22052 /* Now that we have bindings for all of the template arguments,
22053 ensure that the arguments deduced for the template template
22054 parameters have compatible template parameter lists. See the use
22055 of template_template_parm_bindings_ok_p in fn_type_unification
22056 for more information. */
22057 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22058 return NULL_TREE;
22059
22060 return deduced_args;
22061 }
22062
22063 // Compare two function templates T1 and T2 by deducing bindings
22064 // from one against the other. If both deductions succeed, compare
22065 // constraints to see which is more constrained.
22066 static int
22067 more_specialized_inst (tree t1, tree t2)
22068 {
22069 int fate = 0;
22070 int count = 0;
22071
22072 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22073 {
22074 --fate;
22075 ++count;
22076 }
22077
22078 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22079 {
22080 ++fate;
22081 ++count;
22082 }
22083
22084 // If both deductions succeed, then one may be more constrained.
22085 if (count == 2 && fate == 0)
22086 fate = more_constrained (t1, t2);
22087
22088 return fate;
22089 }
22090
22091 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22092 Return the TREE_LIST node with the most specialized template, if
22093 any. If there is no most specialized template, the error_mark_node
22094 is returned.
22095
22096 Note that this function does not look at, or modify, the
22097 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22098 returned is one of the elements of INSTANTIATIONS, callers may
22099 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22100 and retrieve it from the value returned. */
22101
22102 tree
22103 most_specialized_instantiation (tree templates)
22104 {
22105 tree fn, champ;
22106
22107 ++processing_template_decl;
22108
22109 champ = templates;
22110 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22111 {
22112 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22113 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22114 if (fate == -1)
22115 champ = fn;
22116 else if (!fate)
22117 {
22118 /* Equally specialized, move to next function. If there
22119 is no next function, nothing's most specialized. */
22120 fn = TREE_CHAIN (fn);
22121 champ = fn;
22122 if (!fn)
22123 break;
22124 }
22125 }
22126
22127 if (champ)
22128 /* Now verify that champ is better than everything earlier in the
22129 instantiation list. */
22130 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22131 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22132 {
22133 champ = NULL_TREE;
22134 break;
22135 }
22136 }
22137
22138 processing_template_decl--;
22139
22140 if (!champ)
22141 return error_mark_node;
22142
22143 return champ;
22144 }
22145
22146 /* If DECL is a specialization of some template, return the most
22147 general such template. Otherwise, returns NULL_TREE.
22148
22149 For example, given:
22150
22151 template <class T> struct S { template <class U> void f(U); };
22152
22153 if TMPL is `template <class U> void S<int>::f(U)' this will return
22154 the full template. This function will not trace past partial
22155 specializations, however. For example, given in addition:
22156
22157 template <class T> struct S<T*> { template <class U> void f(U); };
22158
22159 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22160 `template <class T> template <class U> S<T*>::f(U)'. */
22161
22162 tree
22163 most_general_template (tree decl)
22164 {
22165 if (TREE_CODE (decl) != TEMPLATE_DECL)
22166 {
22167 if (tree tinfo = get_template_info (decl))
22168 decl = TI_TEMPLATE (tinfo);
22169 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22170 template friend, or a FIELD_DECL for a capture pack. */
22171 if (TREE_CODE (decl) != TEMPLATE_DECL)
22172 return NULL_TREE;
22173 }
22174
22175 /* Look for more and more general templates. */
22176 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22177 {
22178 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22179 (See cp-tree.h for details.) */
22180 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22181 break;
22182
22183 if (CLASS_TYPE_P (TREE_TYPE (decl))
22184 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22185 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22186 break;
22187
22188 /* Stop if we run into an explicitly specialized class template. */
22189 if (!DECL_NAMESPACE_SCOPE_P (decl)
22190 && DECL_CONTEXT (decl)
22191 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22192 break;
22193
22194 decl = DECL_TI_TEMPLATE (decl);
22195 }
22196
22197 return decl;
22198 }
22199
22200 /* Return the most specialized of the template partial specializations
22201 which can produce TARGET, a specialization of some class or variable
22202 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22203 a TEMPLATE_DECL node corresponding to the partial specialization, while
22204 the TREE_PURPOSE is the set of template arguments that must be
22205 substituted into the template pattern in order to generate TARGET.
22206
22207 If the choice of partial specialization is ambiguous, a diagnostic
22208 is issued, and the error_mark_node is returned. If there are no
22209 partial specializations matching TARGET, then NULL_TREE is
22210 returned, indicating that the primary template should be used. */
22211
22212 static tree
22213 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22214 {
22215 tree list = NULL_TREE;
22216 tree t;
22217 tree champ;
22218 int fate;
22219 bool ambiguous_p;
22220 tree outer_args = NULL_TREE;
22221 tree tmpl, args;
22222
22223 if (TYPE_P (target))
22224 {
22225 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22226 tmpl = TI_TEMPLATE (tinfo);
22227 args = TI_ARGS (tinfo);
22228 }
22229 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22230 {
22231 tmpl = TREE_OPERAND (target, 0);
22232 args = TREE_OPERAND (target, 1);
22233 }
22234 else if (VAR_P (target))
22235 {
22236 tree tinfo = DECL_TEMPLATE_INFO (target);
22237 tmpl = TI_TEMPLATE (tinfo);
22238 args = TI_ARGS (tinfo);
22239 }
22240 else
22241 gcc_unreachable ();
22242
22243 tree main_tmpl = most_general_template (tmpl);
22244
22245 /* For determining which partial specialization to use, only the
22246 innermost args are interesting. */
22247 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22248 {
22249 outer_args = strip_innermost_template_args (args, 1);
22250 args = INNERMOST_TEMPLATE_ARGS (args);
22251 }
22252
22253 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22254 {
22255 tree spec_args;
22256 tree spec_tmpl = TREE_VALUE (t);
22257
22258 if (outer_args)
22259 {
22260 /* Substitute in the template args from the enclosing class. */
22261 ++processing_template_decl;
22262 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22263 --processing_template_decl;
22264 }
22265
22266 if (spec_tmpl == error_mark_node)
22267 return error_mark_node;
22268
22269 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22270 if (spec_args)
22271 {
22272 if (outer_args)
22273 spec_args = add_to_template_args (outer_args, spec_args);
22274
22275 /* Keep the candidate only if the constraints are satisfied,
22276 or if we're not compiling with concepts. */
22277 if (!flag_concepts
22278 || constraints_satisfied_p (spec_tmpl, spec_args))
22279 {
22280 list = tree_cons (spec_args, TREE_VALUE (t), list);
22281 TREE_TYPE (list) = TREE_TYPE (t);
22282 }
22283 }
22284 }
22285
22286 if (! list)
22287 return NULL_TREE;
22288
22289 ambiguous_p = false;
22290 t = list;
22291 champ = t;
22292 t = TREE_CHAIN (t);
22293 for (; t; t = TREE_CHAIN (t))
22294 {
22295 fate = more_specialized_partial_spec (tmpl, champ, t);
22296 if (fate == 1)
22297 ;
22298 else
22299 {
22300 if (fate == 0)
22301 {
22302 t = TREE_CHAIN (t);
22303 if (! t)
22304 {
22305 ambiguous_p = true;
22306 break;
22307 }
22308 }
22309 champ = t;
22310 }
22311 }
22312
22313 if (!ambiguous_p)
22314 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22315 {
22316 fate = more_specialized_partial_spec (tmpl, champ, t);
22317 if (fate != 1)
22318 {
22319 ambiguous_p = true;
22320 break;
22321 }
22322 }
22323
22324 if (ambiguous_p)
22325 {
22326 const char *str;
22327 char *spaces = NULL;
22328 if (!(complain & tf_error))
22329 return error_mark_node;
22330 if (TYPE_P (target))
22331 error ("ambiguous template instantiation for %q#T", target);
22332 else
22333 error ("ambiguous template instantiation for %q#D", target);
22334 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22335 for (t = list; t; t = TREE_CHAIN (t))
22336 {
22337 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22338 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22339 "%s %#qS", spaces ? spaces : str, subst);
22340 spaces = spaces ? spaces : get_spaces (str);
22341 }
22342 free (spaces);
22343 return error_mark_node;
22344 }
22345
22346 return champ;
22347 }
22348
22349 /* Explicitly instantiate DECL. */
22350
22351 void
22352 do_decl_instantiation (tree decl, tree storage)
22353 {
22354 tree result = NULL_TREE;
22355 int extern_p = 0;
22356
22357 if (!decl || decl == error_mark_node)
22358 /* An error occurred, for which grokdeclarator has already issued
22359 an appropriate message. */
22360 return;
22361 else if (! DECL_LANG_SPECIFIC (decl))
22362 {
22363 error ("explicit instantiation of non-template %q#D", decl);
22364 return;
22365 }
22366
22367 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22368 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22369
22370 if (VAR_P (decl) && !var_templ)
22371 {
22372 /* There is an asymmetry here in the way VAR_DECLs and
22373 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22374 the latter, the DECL we get back will be marked as a
22375 template instantiation, and the appropriate
22376 DECL_TEMPLATE_INFO will be set up. This does not happen for
22377 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22378 should handle VAR_DECLs as it currently handles
22379 FUNCTION_DECLs. */
22380 if (!DECL_CLASS_SCOPE_P (decl))
22381 {
22382 error ("%qD is not a static data member of a class template", decl);
22383 return;
22384 }
22385 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22386 if (!result || !VAR_P (result))
22387 {
22388 error ("no matching template for %qD found", decl);
22389 return;
22390 }
22391 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22392 {
22393 error ("type %qT for explicit instantiation %qD does not match "
22394 "declared type %qT", TREE_TYPE (result), decl,
22395 TREE_TYPE (decl));
22396 return;
22397 }
22398 }
22399 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22400 {
22401 error ("explicit instantiation of %q#D", decl);
22402 return;
22403 }
22404 else
22405 result = decl;
22406
22407 /* Check for various error cases. Note that if the explicit
22408 instantiation is valid the RESULT will currently be marked as an
22409 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22410 until we get here. */
22411
22412 if (DECL_TEMPLATE_SPECIALIZATION (result))
22413 {
22414 /* DR 259 [temp.spec].
22415
22416 Both an explicit instantiation and a declaration of an explicit
22417 specialization shall not appear in a program unless the explicit
22418 instantiation follows a declaration of the explicit specialization.
22419
22420 For a given set of template parameters, if an explicit
22421 instantiation of a template appears after a declaration of an
22422 explicit specialization for that template, the explicit
22423 instantiation has no effect. */
22424 return;
22425 }
22426 else if (DECL_EXPLICIT_INSTANTIATION (result))
22427 {
22428 /* [temp.spec]
22429
22430 No program shall explicitly instantiate any template more
22431 than once.
22432
22433 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22434 the first instantiation was `extern' and the second is not,
22435 and EXTERN_P for the opposite case. */
22436 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22437 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22438 /* If an "extern" explicit instantiation follows an ordinary
22439 explicit instantiation, the template is instantiated. */
22440 if (extern_p)
22441 return;
22442 }
22443 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22444 {
22445 error ("no matching template for %qD found", result);
22446 return;
22447 }
22448 else if (!DECL_TEMPLATE_INFO (result))
22449 {
22450 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22451 return;
22452 }
22453
22454 if (storage == NULL_TREE)
22455 ;
22456 else if (storage == ridpointers[(int) RID_EXTERN])
22457 {
22458 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22459 pedwarn (input_location, OPT_Wpedantic,
22460 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22461 "instantiations");
22462 extern_p = 1;
22463 }
22464 else
22465 error ("storage class %qD applied to template instantiation", storage);
22466
22467 check_explicit_instantiation_namespace (result);
22468 mark_decl_instantiated (result, extern_p);
22469 if (! extern_p)
22470 instantiate_decl (result, /*defer_ok=*/true,
22471 /*expl_inst_class_mem_p=*/false);
22472 }
22473
22474 static void
22475 mark_class_instantiated (tree t, int extern_p)
22476 {
22477 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22478 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22479 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22480 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22481 if (! extern_p)
22482 {
22483 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22484 rest_of_type_compilation (t, 1);
22485 }
22486 }
22487
22488 /* Called from do_type_instantiation through binding_table_foreach to
22489 do recursive instantiation for the type bound in ENTRY. */
22490 static void
22491 bt_instantiate_type_proc (binding_entry entry, void *data)
22492 {
22493 tree storage = *(tree *) data;
22494
22495 if (MAYBE_CLASS_TYPE_P (entry->type)
22496 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22497 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22498 }
22499
22500 /* Perform an explicit instantiation of template class T. STORAGE, if
22501 non-null, is the RID for extern, inline or static. COMPLAIN is
22502 nonzero if this is called from the parser, zero if called recursively,
22503 since the standard is unclear (as detailed below). */
22504
22505 void
22506 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22507 {
22508 int extern_p = 0;
22509 int nomem_p = 0;
22510 int static_p = 0;
22511 int previous_instantiation_extern_p = 0;
22512
22513 if (TREE_CODE (t) == TYPE_DECL)
22514 t = TREE_TYPE (t);
22515
22516 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22517 {
22518 tree tmpl =
22519 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22520 if (tmpl)
22521 error ("explicit instantiation of non-class template %qD", tmpl);
22522 else
22523 error ("explicit instantiation of non-template type %qT", t);
22524 return;
22525 }
22526
22527 complete_type (t);
22528
22529 if (!COMPLETE_TYPE_P (t))
22530 {
22531 if (complain & tf_error)
22532 error ("explicit instantiation of %q#T before definition of template",
22533 t);
22534 return;
22535 }
22536
22537 if (storage != NULL_TREE)
22538 {
22539 if (!in_system_header_at (input_location))
22540 {
22541 if (storage == ridpointers[(int) RID_EXTERN])
22542 {
22543 if (cxx_dialect == cxx98)
22544 pedwarn (input_location, OPT_Wpedantic,
22545 "ISO C++ 1998 forbids the use of %<extern%> on "
22546 "explicit instantiations");
22547 }
22548 else
22549 pedwarn (input_location, OPT_Wpedantic,
22550 "ISO C++ forbids the use of %qE"
22551 " on explicit instantiations", storage);
22552 }
22553
22554 if (storage == ridpointers[(int) RID_INLINE])
22555 nomem_p = 1;
22556 else if (storage == ridpointers[(int) RID_EXTERN])
22557 extern_p = 1;
22558 else if (storage == ridpointers[(int) RID_STATIC])
22559 static_p = 1;
22560 else
22561 {
22562 error ("storage class %qD applied to template instantiation",
22563 storage);
22564 extern_p = 0;
22565 }
22566 }
22567
22568 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22569 {
22570 /* DR 259 [temp.spec].
22571
22572 Both an explicit instantiation and a declaration of an explicit
22573 specialization shall not appear in a program unless the explicit
22574 instantiation follows a declaration of the explicit specialization.
22575
22576 For a given set of template parameters, if an explicit
22577 instantiation of a template appears after a declaration of an
22578 explicit specialization for that template, the explicit
22579 instantiation has no effect. */
22580 return;
22581 }
22582 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22583 {
22584 /* [temp.spec]
22585
22586 No program shall explicitly instantiate any template more
22587 than once.
22588
22589 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22590 instantiation was `extern'. If EXTERN_P then the second is.
22591 These cases are OK. */
22592 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22593
22594 if (!previous_instantiation_extern_p && !extern_p
22595 && (complain & tf_error))
22596 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22597
22598 /* If we've already instantiated the template, just return now. */
22599 if (!CLASSTYPE_INTERFACE_ONLY (t))
22600 return;
22601 }
22602
22603 check_explicit_instantiation_namespace (TYPE_NAME (t));
22604 mark_class_instantiated (t, extern_p);
22605
22606 if (nomem_p)
22607 return;
22608
22609 /* In contrast to implicit instantiation, where only the
22610 declarations, and not the definitions, of members are
22611 instantiated, we have here:
22612
22613 [temp.explicit]
22614
22615 The explicit instantiation of a class template specialization
22616 implies the instantiation of all of its members not
22617 previously explicitly specialized in the translation unit
22618 containing the explicit instantiation.
22619
22620 Of course, we can't instantiate member template classes, since we
22621 don't have any arguments for them. Note that the standard is
22622 unclear on whether the instantiation of the members are
22623 *explicit* instantiations or not. However, the most natural
22624 interpretation is that it should be an explicit
22625 instantiation. */
22626 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22627 if ((VAR_P (fld)
22628 || (TREE_CODE (fld) == FUNCTION_DECL
22629 && !static_p
22630 && user_provided_p (fld)))
22631 && DECL_TEMPLATE_INSTANTIATION (fld))
22632 {
22633 mark_decl_instantiated (fld, extern_p);
22634 if (! extern_p)
22635 instantiate_decl (fld, /*defer_ok=*/true,
22636 /*expl_inst_class_mem_p=*/true);
22637 }
22638
22639 if (CLASSTYPE_NESTED_UTDS (t))
22640 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22641 bt_instantiate_type_proc, &storage);
22642 }
22643
22644 /* Given a function DECL, which is a specialization of TMPL, modify
22645 DECL to be a re-instantiation of TMPL with the same template
22646 arguments. TMPL should be the template into which tsubst'ing
22647 should occur for DECL, not the most general template.
22648
22649 One reason for doing this is a scenario like this:
22650
22651 template <class T>
22652 void f(const T&, int i);
22653
22654 void g() { f(3, 7); }
22655
22656 template <class T>
22657 void f(const T& t, const int i) { }
22658
22659 Note that when the template is first instantiated, with
22660 instantiate_template, the resulting DECL will have no name for the
22661 first parameter, and the wrong type for the second. So, when we go
22662 to instantiate the DECL, we regenerate it. */
22663
22664 static void
22665 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22666 {
22667 /* The arguments used to instantiate DECL, from the most general
22668 template. */
22669 tree code_pattern;
22670
22671 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22672
22673 /* Make sure that we can see identifiers, and compute access
22674 correctly. */
22675 push_access_scope (decl);
22676
22677 if (TREE_CODE (decl) == FUNCTION_DECL)
22678 {
22679 tree decl_parm;
22680 tree pattern_parm;
22681 tree specs;
22682 int args_depth;
22683 int parms_depth;
22684
22685 args_depth = TMPL_ARGS_DEPTH (args);
22686 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22687 if (args_depth > parms_depth)
22688 args = get_innermost_template_args (args, parms_depth);
22689
22690 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22691 args, tf_error, NULL_TREE,
22692 /*defer_ok*/false);
22693 if (specs && specs != error_mark_node)
22694 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22695 specs);
22696
22697 /* Merge parameter declarations. */
22698 decl_parm = skip_artificial_parms_for (decl,
22699 DECL_ARGUMENTS (decl));
22700 pattern_parm
22701 = skip_artificial_parms_for (code_pattern,
22702 DECL_ARGUMENTS (code_pattern));
22703 while (decl_parm && !DECL_PACK_P (pattern_parm))
22704 {
22705 tree parm_type;
22706 tree attributes;
22707
22708 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22709 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22710 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22711 NULL_TREE);
22712 parm_type = type_decays_to (parm_type);
22713 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22714 TREE_TYPE (decl_parm) = parm_type;
22715 attributes = DECL_ATTRIBUTES (pattern_parm);
22716 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22717 {
22718 DECL_ATTRIBUTES (decl_parm) = attributes;
22719 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22720 }
22721 decl_parm = DECL_CHAIN (decl_parm);
22722 pattern_parm = DECL_CHAIN (pattern_parm);
22723 }
22724 /* Merge any parameters that match with the function parameter
22725 pack. */
22726 if (pattern_parm && DECL_PACK_P (pattern_parm))
22727 {
22728 int i, len;
22729 tree expanded_types;
22730 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22731 the parameters in this function parameter pack. */
22732 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22733 args, tf_error, NULL_TREE);
22734 len = TREE_VEC_LENGTH (expanded_types);
22735 for (i = 0; i < len; i++)
22736 {
22737 tree parm_type;
22738 tree attributes;
22739
22740 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22741 /* Rename the parameter to include the index. */
22742 DECL_NAME (decl_parm) =
22743 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22744 parm_type = TREE_VEC_ELT (expanded_types, i);
22745 parm_type = type_decays_to (parm_type);
22746 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22747 TREE_TYPE (decl_parm) = parm_type;
22748 attributes = DECL_ATTRIBUTES (pattern_parm);
22749 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22750 {
22751 DECL_ATTRIBUTES (decl_parm) = attributes;
22752 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22753 }
22754 decl_parm = DECL_CHAIN (decl_parm);
22755 }
22756 }
22757 /* Merge additional specifiers from the CODE_PATTERN. */
22758 if (DECL_DECLARED_INLINE_P (code_pattern)
22759 && !DECL_DECLARED_INLINE_P (decl))
22760 DECL_DECLARED_INLINE_P (decl) = 1;
22761 }
22762 else if (VAR_P (decl))
22763 {
22764 start_lambda_scope (decl);
22765 DECL_INITIAL (decl) =
22766 tsubst_expr (DECL_INITIAL (code_pattern), args,
22767 tf_error, DECL_TI_TEMPLATE (decl),
22768 /*integral_constant_expression_p=*/false);
22769 finish_lambda_scope ();
22770 if (VAR_HAD_UNKNOWN_BOUND (decl))
22771 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22772 tf_error, DECL_TI_TEMPLATE (decl));
22773 }
22774 else
22775 gcc_unreachable ();
22776
22777 pop_access_scope (decl);
22778 }
22779
22780 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22781 substituted to get DECL. */
22782
22783 tree
22784 template_for_substitution (tree decl)
22785 {
22786 tree tmpl = DECL_TI_TEMPLATE (decl);
22787
22788 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22789 for the instantiation. This is not always the most general
22790 template. Consider, for example:
22791
22792 template <class T>
22793 struct S { template <class U> void f();
22794 template <> void f<int>(); };
22795
22796 and an instantiation of S<double>::f<int>. We want TD to be the
22797 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22798 while (/* An instantiation cannot have a definition, so we need a
22799 more general template. */
22800 DECL_TEMPLATE_INSTANTIATION (tmpl)
22801 /* We must also deal with friend templates. Given:
22802
22803 template <class T> struct S {
22804 template <class U> friend void f() {};
22805 };
22806
22807 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22808 so far as the language is concerned, but that's still
22809 where we get the pattern for the instantiation from. On
22810 other hand, if the definition comes outside the class, say:
22811
22812 template <class T> struct S {
22813 template <class U> friend void f();
22814 };
22815 template <class U> friend void f() {}
22816
22817 we don't need to look any further. That's what the check for
22818 DECL_INITIAL is for. */
22819 || (TREE_CODE (decl) == FUNCTION_DECL
22820 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22821 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22822 {
22823 /* The present template, TD, should not be a definition. If it
22824 were a definition, we should be using it! Note that we
22825 cannot restructure the loop to just keep going until we find
22826 a template with a definition, since that might go too far if
22827 a specialization was declared, but not defined. */
22828
22829 /* Fetch the more general template. */
22830 tmpl = DECL_TI_TEMPLATE (tmpl);
22831 }
22832
22833 return tmpl;
22834 }
22835
22836 /* Returns true if we need to instantiate this template instance even if we
22837 know we aren't going to emit it. */
22838
22839 bool
22840 always_instantiate_p (tree decl)
22841 {
22842 /* We always instantiate inline functions so that we can inline them. An
22843 explicit instantiation declaration prohibits implicit instantiation of
22844 non-inline functions. With high levels of optimization, we would
22845 normally inline non-inline functions -- but we're not allowed to do
22846 that for "extern template" functions. Therefore, we check
22847 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22848 return ((TREE_CODE (decl) == FUNCTION_DECL
22849 && (DECL_DECLARED_INLINE_P (decl)
22850 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22851 /* And we need to instantiate static data members so that
22852 their initializers are available in integral constant
22853 expressions. */
22854 || (VAR_P (decl)
22855 && decl_maybe_constant_var_p (decl)));
22856 }
22857
22858 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22859 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22860 error, true otherwise. */
22861
22862 bool
22863 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22864 {
22865 tree fntype, spec, noex, clone;
22866
22867 /* Don't instantiate a noexcept-specification from template context. */
22868 if (processing_template_decl)
22869 return true;
22870
22871 if (DECL_CLONED_FUNCTION_P (fn))
22872 fn = DECL_CLONED_FUNCTION (fn);
22873 fntype = TREE_TYPE (fn);
22874 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22875
22876 if (!spec || !TREE_PURPOSE (spec))
22877 return true;
22878
22879 noex = TREE_PURPOSE (spec);
22880
22881 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22882 {
22883 static hash_set<tree>* fns = new hash_set<tree>;
22884 bool added = false;
22885 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22886 spec = get_defaulted_eh_spec (fn, complain);
22887 else if (!(added = !fns->add (fn)))
22888 {
22889 /* If hash_set::add returns true, the element was already there. */
22890 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22891 DECL_SOURCE_LOCATION (fn));
22892 error_at (loc,
22893 "exception specification of %qD depends on itself",
22894 fn);
22895 spec = noexcept_false_spec;
22896 }
22897 else if (push_tinst_level (fn))
22898 {
22899 push_access_scope (fn);
22900 push_deferring_access_checks (dk_no_deferred);
22901 input_location = DECL_SOURCE_LOCATION (fn);
22902 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22903 DEFERRED_NOEXCEPT_ARGS (noex),
22904 tf_warning_or_error, fn,
22905 /*function_p=*/false,
22906 /*integral_constant_expression_p=*/true);
22907 pop_deferring_access_checks ();
22908 pop_access_scope (fn);
22909 pop_tinst_level ();
22910 spec = build_noexcept_spec (noex, tf_warning_or_error);
22911 if (spec == error_mark_node)
22912 spec = noexcept_false_spec;
22913 }
22914 else
22915 spec = noexcept_false_spec;
22916
22917 if (added)
22918 fns->remove (fn);
22919
22920 if (spec == error_mark_node)
22921 return false;
22922
22923 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22924 }
22925
22926 FOR_EACH_CLONE (clone, fn)
22927 {
22928 if (TREE_TYPE (clone) == fntype)
22929 TREE_TYPE (clone) = TREE_TYPE (fn);
22930 else
22931 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22932 }
22933
22934 return true;
22935 }
22936
22937 /* We're starting to process the function INST, an instantiation of PATTERN;
22938 add their parameters to local_specializations. */
22939
22940 static void
22941 register_parameter_specializations (tree pattern, tree inst)
22942 {
22943 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22944 tree spec_parm = DECL_ARGUMENTS (inst);
22945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22946 {
22947 register_local_specialization (spec_parm, tmpl_parm);
22948 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22949 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22950 }
22951 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22952 {
22953 if (!DECL_PACK_P (tmpl_parm))
22954 {
22955 register_local_specialization (spec_parm, tmpl_parm);
22956 spec_parm = DECL_CHAIN (spec_parm);
22957 }
22958 else
22959 {
22960 /* Register the (value) argument pack as a specialization of
22961 TMPL_PARM, then move on. */
22962 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22963 register_local_specialization (argpack, tmpl_parm);
22964 }
22965 }
22966 gcc_assert (!spec_parm);
22967 }
22968
22969 /* Produce the definition of D, a _DECL generated from a template. If
22970 DEFER_OK is true, then we don't have to actually do the
22971 instantiation now; we just have to do it sometime. Normally it is
22972 an error if this is an explicit instantiation but D is undefined.
22973 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22974 instantiated class template. */
22975
22976 tree
22977 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22978 {
22979 tree tmpl = DECL_TI_TEMPLATE (d);
22980 tree gen_args;
22981 tree args;
22982 tree td;
22983 tree code_pattern;
22984 tree spec;
22985 tree gen_tmpl;
22986 bool pattern_defined;
22987 location_t saved_loc = input_location;
22988 int saved_unevaluated_operand = cp_unevaluated_operand;
22989 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22990 bool external_p;
22991 bool deleted_p;
22992
22993 /* This function should only be used to instantiate templates for
22994 functions and static member variables. */
22995 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22996
22997 /* A concept is never instantiated. */
22998 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22999
23000 /* Variables are never deferred; if instantiation is required, they
23001 are instantiated right away. That allows for better code in the
23002 case that an expression refers to the value of the variable --
23003 if the variable has a constant value the referring expression can
23004 take advantage of that fact. */
23005 if (VAR_P (d))
23006 defer_ok = false;
23007
23008 /* Don't instantiate cloned functions. Instead, instantiate the
23009 functions they cloned. */
23010 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23011 d = DECL_CLONED_FUNCTION (d);
23012
23013 if (DECL_TEMPLATE_INSTANTIATED (d)
23014 || (TREE_CODE (d) == FUNCTION_DECL
23015 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23016 || DECL_TEMPLATE_SPECIALIZATION (d))
23017 /* D has already been instantiated or explicitly specialized, so
23018 there's nothing for us to do here.
23019
23020 It might seem reasonable to check whether or not D is an explicit
23021 instantiation, and, if so, stop here. But when an explicit
23022 instantiation is deferred until the end of the compilation,
23023 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23024 the instantiation. */
23025 return d;
23026
23027 /* Check to see whether we know that this template will be
23028 instantiated in some other file, as with "extern template"
23029 extension. */
23030 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23031
23032 /* In general, we do not instantiate such templates. */
23033 if (external_p && !always_instantiate_p (d))
23034 return d;
23035
23036 gen_tmpl = most_general_template (tmpl);
23037 gen_args = DECL_TI_ARGS (d);
23038
23039 if (tmpl != gen_tmpl)
23040 /* We should already have the extra args. */
23041 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23042 == TMPL_ARGS_DEPTH (gen_args));
23043 /* And what's in the hash table should match D. */
23044 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23045 || spec == NULL_TREE);
23046
23047 /* This needs to happen before any tsubsting. */
23048 if (! push_tinst_level (d))
23049 return d;
23050
23051 timevar_push (TV_TEMPLATE_INST);
23052
23053 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23054 for the instantiation. */
23055 td = template_for_substitution (d);
23056 args = gen_args;
23057
23058 if (VAR_P (d))
23059 {
23060 /* Look up an explicit specialization, if any. */
23061 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23062 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23063 if (elt && elt != error_mark_node)
23064 {
23065 td = TREE_VALUE (elt);
23066 args = TREE_PURPOSE (elt);
23067 }
23068 }
23069
23070 code_pattern = DECL_TEMPLATE_RESULT (td);
23071
23072 /* We should never be trying to instantiate a member of a class
23073 template or partial specialization. */
23074 gcc_assert (d != code_pattern);
23075
23076 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23077 || DECL_TEMPLATE_SPECIALIZATION (td))
23078 /* In the case of a friend template whose definition is provided
23079 outside the class, we may have too many arguments. Drop the
23080 ones we don't need. The same is true for specializations. */
23081 args = get_innermost_template_args
23082 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23083
23084 if (TREE_CODE (d) == FUNCTION_DECL)
23085 {
23086 deleted_p = DECL_DELETED_FN (code_pattern);
23087 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23088 && DECL_INITIAL (code_pattern) != error_mark_node)
23089 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23090 || deleted_p);
23091 }
23092 else
23093 {
23094 deleted_p = false;
23095 if (DECL_CLASS_SCOPE_P (code_pattern))
23096 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23097 || DECL_INLINE_VAR_P (code_pattern));
23098 else
23099 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23100 }
23101
23102 /* We may be in the middle of deferred access check. Disable it now. */
23103 push_deferring_access_checks (dk_no_deferred);
23104
23105 /* Unless an explicit instantiation directive has already determined
23106 the linkage of D, remember that a definition is available for
23107 this entity. */
23108 if (pattern_defined
23109 && !DECL_INTERFACE_KNOWN (d)
23110 && !DECL_NOT_REALLY_EXTERN (d))
23111 mark_definable (d);
23112
23113 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23114 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23115 input_location = DECL_SOURCE_LOCATION (d);
23116
23117 /* If D is a member of an explicitly instantiated class template,
23118 and no definition is available, treat it like an implicit
23119 instantiation. */
23120 if (!pattern_defined && expl_inst_class_mem_p
23121 && DECL_EXPLICIT_INSTANTIATION (d))
23122 {
23123 /* Leave linkage flags alone on instantiations with anonymous
23124 visibility. */
23125 if (TREE_PUBLIC (d))
23126 {
23127 DECL_NOT_REALLY_EXTERN (d) = 0;
23128 DECL_INTERFACE_KNOWN (d) = 0;
23129 }
23130 SET_DECL_IMPLICIT_INSTANTIATION (d);
23131 }
23132
23133 /* Defer all other templates, unless we have been explicitly
23134 forbidden from doing so. */
23135 if (/* If there is no definition, we cannot instantiate the
23136 template. */
23137 ! pattern_defined
23138 /* If it's OK to postpone instantiation, do so. */
23139 || defer_ok
23140 /* If this is a static data member that will be defined
23141 elsewhere, we don't want to instantiate the entire data
23142 member, but we do want to instantiate the initializer so that
23143 we can substitute that elsewhere. */
23144 || (external_p && VAR_P (d))
23145 /* Handle here a deleted function too, avoid generating
23146 its body (c++/61080). */
23147 || deleted_p)
23148 {
23149 /* The definition of the static data member is now required so
23150 we must substitute the initializer. */
23151 if (VAR_P (d)
23152 && !DECL_INITIAL (d)
23153 && DECL_INITIAL (code_pattern))
23154 {
23155 tree ns;
23156 tree init;
23157 bool const_init = false;
23158 bool enter_context = DECL_CLASS_SCOPE_P (d);
23159
23160 ns = decl_namespace_context (d);
23161 push_nested_namespace (ns);
23162 if (enter_context)
23163 push_nested_class (DECL_CONTEXT (d));
23164 init = tsubst_expr (DECL_INITIAL (code_pattern),
23165 args,
23166 tf_warning_or_error, NULL_TREE,
23167 /*integral_constant_expression_p=*/false);
23168 /* If instantiating the initializer involved instantiating this
23169 again, don't call cp_finish_decl twice. */
23170 if (!DECL_INITIAL (d))
23171 {
23172 /* Make sure the initializer is still constant, in case of
23173 circular dependency (template/instantiate6.C). */
23174 const_init
23175 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23176 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23177 /*asmspec_tree=*/NULL_TREE,
23178 LOOKUP_ONLYCONVERTING);
23179 }
23180 if (enter_context)
23181 pop_nested_class ();
23182 pop_nested_namespace (ns);
23183 }
23184
23185 /* We restore the source position here because it's used by
23186 add_pending_template. */
23187 input_location = saved_loc;
23188
23189 if (at_eof && !pattern_defined
23190 && DECL_EXPLICIT_INSTANTIATION (d)
23191 && DECL_NOT_REALLY_EXTERN (d))
23192 /* [temp.explicit]
23193
23194 The definition of a non-exported function template, a
23195 non-exported member function template, or a non-exported
23196 member function or static data member of a class template
23197 shall be present in every translation unit in which it is
23198 explicitly instantiated. */
23199 permerror (input_location, "explicit instantiation of %qD "
23200 "but no definition available", d);
23201
23202 /* If we're in unevaluated context, we just wanted to get the
23203 constant value; this isn't an odr use, so don't queue
23204 a full instantiation. */
23205 if (cp_unevaluated_operand != 0)
23206 goto out;
23207 /* ??? Historically, we have instantiated inline functions, even
23208 when marked as "extern template". */
23209 if (!(external_p && VAR_P (d)))
23210 add_pending_template (d);
23211 goto out;
23212 }
23213 /* Tell the repository that D is available in this translation unit
23214 -- and see if it is supposed to be instantiated here. */
23215 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23216 {
23217 /* In a PCH file, despite the fact that the repository hasn't
23218 requested instantiation in the PCH it is still possible that
23219 an instantiation will be required in a file that includes the
23220 PCH. */
23221 if (pch_file)
23222 add_pending_template (d);
23223 /* Instantiate inline functions so that the inliner can do its
23224 job, even though we'll not be emitting a copy of this
23225 function. */
23226 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23227 goto out;
23228 }
23229
23230 bool push_to_top, nested;
23231 tree fn_context;
23232 fn_context = decl_function_context (d);
23233 nested = current_function_decl != NULL_TREE;
23234 push_to_top = !(nested && fn_context == current_function_decl);
23235
23236 vec<tree> omp_privatization_save;
23237 if (nested)
23238 save_omp_privatization_clauses (omp_privatization_save);
23239
23240 if (push_to_top)
23241 push_to_top_level ();
23242 else
23243 {
23244 push_function_context ();
23245 cp_unevaluated_operand = 0;
23246 c_inhibit_evaluation_warnings = 0;
23247 }
23248
23249 /* Mark D as instantiated so that recursive calls to
23250 instantiate_decl do not try to instantiate it again. */
23251 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23252
23253 /* Regenerate the declaration in case the template has been modified
23254 by a subsequent redeclaration. */
23255 regenerate_decl_from_template (d, td, args);
23256
23257 /* We already set the file and line above. Reset them now in case
23258 they changed as a result of calling regenerate_decl_from_template. */
23259 input_location = DECL_SOURCE_LOCATION (d);
23260
23261 if (VAR_P (d))
23262 {
23263 tree init;
23264 bool const_init = false;
23265
23266 /* Clear out DECL_RTL; whatever was there before may not be right
23267 since we've reset the type of the declaration. */
23268 SET_DECL_RTL (d, NULL);
23269 DECL_IN_AGGR_P (d) = 0;
23270
23271 /* The initializer is placed in DECL_INITIAL by
23272 regenerate_decl_from_template so we don't need to
23273 push/pop_access_scope again here. Pull it out so that
23274 cp_finish_decl can process it. */
23275 init = DECL_INITIAL (d);
23276 DECL_INITIAL (d) = NULL_TREE;
23277 DECL_INITIALIZED_P (d) = 0;
23278
23279 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23280 initializer. That function will defer actual emission until
23281 we have a chance to determine linkage. */
23282 DECL_EXTERNAL (d) = 0;
23283
23284 /* Enter the scope of D so that access-checking works correctly. */
23285 bool enter_context = DECL_CLASS_SCOPE_P (d);
23286 if (enter_context)
23287 push_nested_class (DECL_CONTEXT (d));
23288
23289 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23290 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23291
23292 if (enter_context)
23293 pop_nested_class ();
23294
23295 if (variable_template_p (gen_tmpl))
23296 note_variable_template_instantiation (d);
23297 }
23298 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23299 synthesize_method (d);
23300 else if (TREE_CODE (d) == FUNCTION_DECL)
23301 {
23302 /* Set up the list of local specializations. */
23303 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23304 tree block = NULL_TREE;
23305
23306 /* Set up context. */
23307 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23308 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23309 block = push_stmt_list ();
23310 else
23311 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23312
23313 /* Some typedefs referenced from within the template code need to be
23314 access checked at template instantiation time, i.e now. These
23315 types were added to the template at parsing time. Let's get those
23316 and perform the access checks then. */
23317 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23318 args);
23319
23320 /* Create substitution entries for the parameters. */
23321 register_parameter_specializations (code_pattern, d);
23322
23323 /* Substitute into the body of the function. */
23324 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23325 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23326 tf_warning_or_error, tmpl);
23327 else
23328 {
23329 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23330 tf_warning_or_error, tmpl,
23331 /*integral_constant_expression_p=*/false);
23332
23333 /* Set the current input_location to the end of the function
23334 so that finish_function knows where we are. */
23335 input_location
23336 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23337
23338 /* Remember if we saw an infinite loop in the template. */
23339 current_function_infinite_loop
23340 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23341 }
23342
23343 /* Finish the function. */
23344 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23345 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23346 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23347 else
23348 {
23349 d = finish_function (/*inline_p=*/false);
23350 expand_or_defer_fn (d);
23351 }
23352
23353 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23354 cp_check_omp_declare_reduction (d);
23355 }
23356
23357 /* We're not deferring instantiation any more. */
23358 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23359
23360 if (push_to_top)
23361 pop_from_top_level ();
23362 else
23363 pop_function_context ();
23364
23365 if (nested)
23366 restore_omp_privatization_clauses (omp_privatization_save);
23367
23368 out:
23369 pop_deferring_access_checks ();
23370 timevar_pop (TV_TEMPLATE_INST);
23371 pop_tinst_level ();
23372 input_location = saved_loc;
23373 cp_unevaluated_operand = saved_unevaluated_operand;
23374 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23375
23376 return d;
23377 }
23378
23379 /* Run through the list of templates that we wish we could
23380 instantiate, and instantiate any we can. RETRIES is the
23381 number of times we retry pending template instantiation. */
23382
23383 void
23384 instantiate_pending_templates (int retries)
23385 {
23386 int reconsider;
23387 location_t saved_loc = input_location;
23388
23389 /* Instantiating templates may trigger vtable generation. This in turn
23390 may require further template instantiations. We place a limit here
23391 to avoid infinite loop. */
23392 if (pending_templates && retries >= max_tinst_depth)
23393 {
23394 tree decl = pending_templates->tinst->decl;
23395
23396 fatal_error (input_location,
23397 "template instantiation depth exceeds maximum of %d"
23398 " instantiating %q+D, possibly from virtual table generation"
23399 " (use -ftemplate-depth= to increase the maximum)",
23400 max_tinst_depth, decl);
23401 if (TREE_CODE (decl) == FUNCTION_DECL)
23402 /* Pretend that we defined it. */
23403 DECL_INITIAL (decl) = error_mark_node;
23404 return;
23405 }
23406
23407 do
23408 {
23409 struct pending_template **t = &pending_templates;
23410 struct pending_template *last = NULL;
23411 reconsider = 0;
23412 while (*t)
23413 {
23414 tree instantiation = reopen_tinst_level ((*t)->tinst);
23415 bool complete = false;
23416
23417 if (TYPE_P (instantiation))
23418 {
23419 if (!COMPLETE_TYPE_P (instantiation))
23420 {
23421 instantiate_class_template (instantiation);
23422 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23423 for (tree fld = TYPE_FIELDS (instantiation);
23424 fld; fld = TREE_CHAIN (fld))
23425 if ((VAR_P (fld)
23426 || (TREE_CODE (fld) == FUNCTION_DECL
23427 && !DECL_ARTIFICIAL (fld)))
23428 && DECL_TEMPLATE_INSTANTIATION (fld))
23429 instantiate_decl (fld,
23430 /*defer_ok=*/false,
23431 /*expl_inst_class_mem_p=*/false);
23432
23433 if (COMPLETE_TYPE_P (instantiation))
23434 reconsider = 1;
23435 }
23436
23437 complete = COMPLETE_TYPE_P (instantiation);
23438 }
23439 else
23440 {
23441 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23442 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23443 {
23444 instantiation
23445 = instantiate_decl (instantiation,
23446 /*defer_ok=*/false,
23447 /*expl_inst_class_mem_p=*/false);
23448 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23449 reconsider = 1;
23450 }
23451
23452 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23453 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23454 }
23455
23456 if (complete)
23457 /* If INSTANTIATION has been instantiated, then we don't
23458 need to consider it again in the future. */
23459 *t = (*t)->next;
23460 else
23461 {
23462 last = *t;
23463 t = &(*t)->next;
23464 }
23465 tinst_depth = 0;
23466 current_tinst_level = NULL;
23467 }
23468 last_pending_template = last;
23469 }
23470 while (reconsider);
23471
23472 input_location = saved_loc;
23473 }
23474
23475 /* Substitute ARGVEC into T, which is a list of initializers for
23476 either base class or a non-static data member. The TREE_PURPOSEs
23477 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23478 instantiate_decl. */
23479
23480 static tree
23481 tsubst_initializer_list (tree t, tree argvec)
23482 {
23483 tree inits = NULL_TREE;
23484
23485 for (; t; t = TREE_CHAIN (t))
23486 {
23487 tree decl;
23488 tree init;
23489 tree expanded_bases = NULL_TREE;
23490 tree expanded_arguments = NULL_TREE;
23491 int i, len = 1;
23492
23493 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23494 {
23495 tree expr;
23496 tree arg;
23497
23498 /* Expand the base class expansion type into separate base
23499 classes. */
23500 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23501 tf_warning_or_error,
23502 NULL_TREE);
23503 if (expanded_bases == error_mark_node)
23504 continue;
23505
23506 /* We'll be building separate TREE_LISTs of arguments for
23507 each base. */
23508 len = TREE_VEC_LENGTH (expanded_bases);
23509 expanded_arguments = make_tree_vec (len);
23510 for (i = 0; i < len; i++)
23511 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23512
23513 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23514 expand each argument in the TREE_VALUE of t. */
23515 expr = make_node (EXPR_PACK_EXPANSION);
23516 PACK_EXPANSION_LOCAL_P (expr) = true;
23517 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23518 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23519
23520 if (TREE_VALUE (t) == void_type_node)
23521 /* VOID_TYPE_NODE is used to indicate
23522 value-initialization. */
23523 {
23524 for (i = 0; i < len; i++)
23525 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23526 }
23527 else
23528 {
23529 /* Substitute parameter packs into each argument in the
23530 TREE_LIST. */
23531 in_base_initializer = 1;
23532 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23533 {
23534 tree expanded_exprs;
23535
23536 /* Expand the argument. */
23537 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23538 expanded_exprs
23539 = tsubst_pack_expansion (expr, argvec,
23540 tf_warning_or_error,
23541 NULL_TREE);
23542 if (expanded_exprs == error_mark_node)
23543 continue;
23544
23545 /* Prepend each of the expanded expressions to the
23546 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23547 for (i = 0; i < len; i++)
23548 {
23549 TREE_VEC_ELT (expanded_arguments, i) =
23550 tree_cons (NULL_TREE,
23551 TREE_VEC_ELT (expanded_exprs, i),
23552 TREE_VEC_ELT (expanded_arguments, i));
23553 }
23554 }
23555 in_base_initializer = 0;
23556
23557 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23558 since we built them backwards. */
23559 for (i = 0; i < len; i++)
23560 {
23561 TREE_VEC_ELT (expanded_arguments, i) =
23562 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23563 }
23564 }
23565 }
23566
23567 for (i = 0; i < len; ++i)
23568 {
23569 if (expanded_bases)
23570 {
23571 decl = TREE_VEC_ELT (expanded_bases, i);
23572 decl = expand_member_init (decl);
23573 init = TREE_VEC_ELT (expanded_arguments, i);
23574 }
23575 else
23576 {
23577 tree tmp;
23578 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23579 tf_warning_or_error, NULL_TREE);
23580
23581 decl = expand_member_init (decl);
23582 if (decl && !DECL_P (decl))
23583 in_base_initializer = 1;
23584
23585 init = TREE_VALUE (t);
23586 tmp = init;
23587 if (init != void_type_node)
23588 init = tsubst_expr (init, argvec,
23589 tf_warning_or_error, NULL_TREE,
23590 /*integral_constant_expression_p=*/false);
23591 if (init == NULL_TREE && tmp != NULL_TREE)
23592 /* If we had an initializer but it instantiated to nothing,
23593 value-initialize the object. This will only occur when
23594 the initializer was a pack expansion where the parameter
23595 packs used in that expansion were of length zero. */
23596 init = void_type_node;
23597 in_base_initializer = 0;
23598 }
23599
23600 if (decl)
23601 {
23602 init = build_tree_list (decl, init);
23603 TREE_CHAIN (init) = inits;
23604 inits = init;
23605 }
23606 }
23607 }
23608 return inits;
23609 }
23610
23611 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23612
23613 static void
23614 set_current_access_from_decl (tree decl)
23615 {
23616 if (TREE_PRIVATE (decl))
23617 current_access_specifier = access_private_node;
23618 else if (TREE_PROTECTED (decl))
23619 current_access_specifier = access_protected_node;
23620 else
23621 current_access_specifier = access_public_node;
23622 }
23623
23624 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23625 is the instantiation (which should have been created with
23626 start_enum) and ARGS are the template arguments to use. */
23627
23628 static void
23629 tsubst_enum (tree tag, tree newtag, tree args)
23630 {
23631 tree e;
23632
23633 if (SCOPED_ENUM_P (newtag))
23634 begin_scope (sk_scoped_enum, newtag);
23635
23636 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23637 {
23638 tree value;
23639 tree decl;
23640
23641 decl = TREE_VALUE (e);
23642 /* Note that in a template enum, the TREE_VALUE is the
23643 CONST_DECL, not the corresponding INTEGER_CST. */
23644 value = tsubst_expr (DECL_INITIAL (decl),
23645 args, tf_warning_or_error, NULL_TREE,
23646 /*integral_constant_expression_p=*/true);
23647
23648 /* Give this enumeration constant the correct access. */
23649 set_current_access_from_decl (decl);
23650
23651 /* Actually build the enumerator itself. Here we're assuming that
23652 enumerators can't have dependent attributes. */
23653 build_enumerator (DECL_NAME (decl), value, newtag,
23654 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23655 }
23656
23657 if (SCOPED_ENUM_P (newtag))
23658 finish_scope ();
23659
23660 finish_enum_value_list (newtag);
23661 finish_enum (newtag);
23662
23663 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23664 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23665 }
23666
23667 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23668 its type -- but without substituting the innermost set of template
23669 arguments. So, innermost set of template parameters will appear in
23670 the type. */
23671
23672 tree
23673 get_mostly_instantiated_function_type (tree decl)
23674 {
23675 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23676 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23677 }
23678
23679 /* Return truthvalue if we're processing a template different from
23680 the last one involved in diagnostics. */
23681 bool
23682 problematic_instantiation_changed (void)
23683 {
23684 return current_tinst_level != last_error_tinst_level;
23685 }
23686
23687 /* Remember current template involved in diagnostics. */
23688 void
23689 record_last_problematic_instantiation (void)
23690 {
23691 last_error_tinst_level = current_tinst_level;
23692 }
23693
23694 struct tinst_level *
23695 current_instantiation (void)
23696 {
23697 return current_tinst_level;
23698 }
23699
23700 /* Return TRUE if current_function_decl is being instantiated, false
23701 otherwise. */
23702
23703 bool
23704 instantiating_current_function_p (void)
23705 {
23706 return (current_instantiation ()
23707 && current_instantiation ()->decl == current_function_decl);
23708 }
23709
23710 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23711 type. Return false for ok, true for disallowed. Issue error and
23712 inform messages under control of COMPLAIN. */
23713
23714 static bool
23715 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23716 {
23717 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23718 return false;
23719 else if (POINTER_TYPE_P (type))
23720 return false;
23721 else if (TYPE_PTRMEM_P (type))
23722 return false;
23723 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23724 return false;
23725 else if (TREE_CODE (type) == TYPENAME_TYPE)
23726 return false;
23727 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23728 return false;
23729 else if (TREE_CODE (type) == NULLPTR_TYPE)
23730 return false;
23731 /* A bound template template parm could later be instantiated to have a valid
23732 nontype parm type via an alias template. */
23733 else if (cxx_dialect >= cxx11
23734 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23735 return false;
23736
23737 if (complain & tf_error)
23738 {
23739 if (type == error_mark_node)
23740 inform (input_location, "invalid template non-type parameter");
23741 else
23742 error ("%q#T is not a valid type for a template non-type parameter",
23743 type);
23744 }
23745 return true;
23746 }
23747
23748 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23749 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23750
23751 static bool
23752 dependent_type_p_r (tree type)
23753 {
23754 tree scope;
23755
23756 /* [temp.dep.type]
23757
23758 A type is dependent if it is:
23759
23760 -- a template parameter. Template template parameters are types
23761 for us (since TYPE_P holds true for them) so we handle
23762 them here. */
23763 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23764 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23765 return true;
23766 /* -- a qualified-id with a nested-name-specifier which contains a
23767 class-name that names a dependent type or whose unqualified-id
23768 names a dependent type. */
23769 if (TREE_CODE (type) == TYPENAME_TYPE)
23770 return true;
23771
23772 /* An alias template specialization can be dependent even if the
23773 resulting type is not. */
23774 if (dependent_alias_template_spec_p (type))
23775 return true;
23776
23777 /* -- a cv-qualified type where the cv-unqualified type is
23778 dependent.
23779 No code is necessary for this bullet; the code below handles
23780 cv-qualified types, and we don't want to strip aliases with
23781 TYPE_MAIN_VARIANT because of DR 1558. */
23782 /* -- a compound type constructed from any dependent type. */
23783 if (TYPE_PTRMEM_P (type))
23784 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23785 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23786 (type)));
23787 else if (TYPE_PTR_P (type)
23788 || TREE_CODE (type) == REFERENCE_TYPE)
23789 return dependent_type_p (TREE_TYPE (type));
23790 else if (TREE_CODE (type) == FUNCTION_TYPE
23791 || TREE_CODE (type) == METHOD_TYPE)
23792 {
23793 tree arg_type;
23794
23795 if (dependent_type_p (TREE_TYPE (type)))
23796 return true;
23797 for (arg_type = TYPE_ARG_TYPES (type);
23798 arg_type;
23799 arg_type = TREE_CHAIN (arg_type))
23800 if (dependent_type_p (TREE_VALUE (arg_type)))
23801 return true;
23802 if (cxx_dialect >= cxx17)
23803 /* A value-dependent noexcept-specifier makes the type dependent. */
23804 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23805 if (tree noex = TREE_PURPOSE (spec))
23806 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23807 affect overload resolution and treating it as dependent breaks
23808 things. */
23809 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23810 && value_dependent_expression_p (noex))
23811 return true;
23812 return false;
23813 }
23814 /* -- an array type constructed from any dependent type or whose
23815 size is specified by a constant expression that is
23816 value-dependent.
23817
23818 We checked for type- and value-dependence of the bounds in
23819 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23820 if (TREE_CODE (type) == ARRAY_TYPE)
23821 {
23822 if (TYPE_DOMAIN (type)
23823 && dependent_type_p (TYPE_DOMAIN (type)))
23824 return true;
23825 return dependent_type_p (TREE_TYPE (type));
23826 }
23827
23828 /* -- a template-id in which either the template name is a template
23829 parameter ... */
23830 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23831 return true;
23832 /* ... or any of the template arguments is a dependent type or
23833 an expression that is type-dependent or value-dependent. */
23834 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23835 && (any_dependent_template_arguments_p
23836 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23837 return true;
23838
23839 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23840 dependent; if the argument of the `typeof' expression is not
23841 type-dependent, then it should already been have resolved. */
23842 if (TREE_CODE (type) == TYPEOF_TYPE
23843 || TREE_CODE (type) == DECLTYPE_TYPE
23844 || TREE_CODE (type) == UNDERLYING_TYPE)
23845 return true;
23846
23847 /* A template argument pack is dependent if any of its packed
23848 arguments are. */
23849 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23850 {
23851 tree args = ARGUMENT_PACK_ARGS (type);
23852 int i, len = TREE_VEC_LENGTH (args);
23853 for (i = 0; i < len; ++i)
23854 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23855 return true;
23856 }
23857
23858 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23859 be template parameters. */
23860 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23861 return true;
23862
23863 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23864 return true;
23865
23866 /* The standard does not specifically mention types that are local
23867 to template functions or local classes, but they should be
23868 considered dependent too. For example:
23869
23870 template <int I> void f() {
23871 enum E { a = I };
23872 S<sizeof (E)> s;
23873 }
23874
23875 The size of `E' cannot be known until the value of `I' has been
23876 determined. Therefore, `E' must be considered dependent. */
23877 scope = TYPE_CONTEXT (type);
23878 if (scope && TYPE_P (scope))
23879 return dependent_type_p (scope);
23880 /* Don't use type_dependent_expression_p here, as it can lead
23881 to infinite recursion trying to determine whether a lambda
23882 nested in a lambda is dependent (c++/47687). */
23883 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23884 && DECL_LANG_SPECIFIC (scope)
23885 && DECL_TEMPLATE_INFO (scope)
23886 && (any_dependent_template_arguments_p
23887 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23888 return true;
23889
23890 /* Other types are non-dependent. */
23891 return false;
23892 }
23893
23894 /* Returns TRUE if TYPE is dependent, in the sense of
23895 [temp.dep.type]. Note that a NULL type is considered dependent. */
23896
23897 bool
23898 dependent_type_p (tree type)
23899 {
23900 /* If there are no template parameters in scope, then there can't be
23901 any dependent types. */
23902 if (!processing_template_decl)
23903 {
23904 /* If we are not processing a template, then nobody should be
23905 providing us with a dependent type. */
23906 gcc_assert (type);
23907 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23908 return false;
23909 }
23910
23911 /* If the type is NULL, we have not computed a type for the entity
23912 in question; in that case, the type is dependent. */
23913 if (!type)
23914 return true;
23915
23916 /* Erroneous types can be considered non-dependent. */
23917 if (type == error_mark_node)
23918 return false;
23919
23920 /* Getting here with global_type_node means we improperly called this
23921 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23922 gcc_checking_assert (type != global_type_node);
23923
23924 /* If we have not already computed the appropriate value for TYPE,
23925 do so now. */
23926 if (!TYPE_DEPENDENT_P_VALID (type))
23927 {
23928 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23929 TYPE_DEPENDENT_P_VALID (type) = 1;
23930 }
23931
23932 return TYPE_DEPENDENT_P (type);
23933 }
23934
23935 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23936 lookup. In other words, a dependent type that is not the current
23937 instantiation. */
23938
23939 bool
23940 dependent_scope_p (tree scope)
23941 {
23942 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23943 && !currently_open_class (scope));
23944 }
23945
23946 /* T is a SCOPE_REF; return whether we need to consider it
23947 instantiation-dependent so that we can check access at instantiation
23948 time even though we know which member it resolves to. */
23949
23950 static bool
23951 instantiation_dependent_scope_ref_p (tree t)
23952 {
23953 if (DECL_P (TREE_OPERAND (t, 1))
23954 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23955 /* A dependent base could make a member inaccessible in the current
23956 class. */
23957 && !any_dependent_bases_p ()
23958 && accessible_in_template_p (TREE_OPERAND (t, 0),
23959 TREE_OPERAND (t, 1)))
23960 return false;
23961 else
23962 return true;
23963 }
23964
23965 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23966 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23967 expression. */
23968
23969 /* Note that this predicate is not appropriate for general expressions;
23970 only constant expressions (that satisfy potential_constant_expression)
23971 can be tested for value dependence. */
23972
23973 bool
23974 value_dependent_expression_p (tree expression)
23975 {
23976 if (!processing_template_decl || expression == NULL_TREE)
23977 return false;
23978
23979 /* A type-dependent expression is also value-dependent. */
23980 if (type_dependent_expression_p (expression))
23981 return true;
23982
23983 switch (TREE_CODE (expression))
23984 {
23985 case BASELINK:
23986 /* A dependent member function of the current instantiation. */
23987 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23988
23989 case FUNCTION_DECL:
23990 /* A dependent member function of the current instantiation. */
23991 if (DECL_CLASS_SCOPE_P (expression)
23992 && dependent_type_p (DECL_CONTEXT (expression)))
23993 return true;
23994 break;
23995
23996 case IDENTIFIER_NODE:
23997 /* A name that has not been looked up -- must be dependent. */
23998 return true;
23999
24000 case TEMPLATE_PARM_INDEX:
24001 /* A non-type template parm. */
24002 return true;
24003
24004 case CONST_DECL:
24005 /* A non-type template parm. */
24006 if (DECL_TEMPLATE_PARM_P (expression))
24007 return true;
24008 return value_dependent_expression_p (DECL_INITIAL (expression));
24009
24010 case VAR_DECL:
24011 /* A constant with literal type and is initialized
24012 with an expression that is value-dependent. */
24013 if (DECL_DEPENDENT_INIT_P (expression)
24014 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24015 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24016 return true;
24017 if (DECL_HAS_VALUE_EXPR_P (expression))
24018 {
24019 tree value_expr = DECL_VALUE_EXPR (expression);
24020 if (value_dependent_expression_p (value_expr))
24021 return true;
24022 }
24023 return false;
24024
24025 case DYNAMIC_CAST_EXPR:
24026 case STATIC_CAST_EXPR:
24027 case CONST_CAST_EXPR:
24028 case REINTERPRET_CAST_EXPR:
24029 case CAST_EXPR:
24030 case IMPLICIT_CONV_EXPR:
24031 /* These expressions are value-dependent if the type to which
24032 the cast occurs is dependent or the expression being casted
24033 is value-dependent. */
24034 {
24035 tree type = TREE_TYPE (expression);
24036
24037 if (dependent_type_p (type))
24038 return true;
24039
24040 /* A functional cast has a list of operands. */
24041 expression = TREE_OPERAND (expression, 0);
24042 if (!expression)
24043 {
24044 /* If there are no operands, it must be an expression such
24045 as "int()". This should not happen for aggregate types
24046 because it would form non-constant expressions. */
24047 gcc_assert (cxx_dialect >= cxx11
24048 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24049
24050 return false;
24051 }
24052
24053 if (TREE_CODE (expression) == TREE_LIST)
24054 return any_value_dependent_elements_p (expression);
24055
24056 return value_dependent_expression_p (expression);
24057 }
24058
24059 case SIZEOF_EXPR:
24060 if (SIZEOF_EXPR_TYPE_P (expression))
24061 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24062 /* FALLTHRU */
24063 case ALIGNOF_EXPR:
24064 case TYPEID_EXPR:
24065 /* A `sizeof' expression is value-dependent if the operand is
24066 type-dependent or is a pack expansion. */
24067 expression = TREE_OPERAND (expression, 0);
24068 if (PACK_EXPANSION_P (expression))
24069 return true;
24070 else if (TYPE_P (expression))
24071 return dependent_type_p (expression);
24072 return instantiation_dependent_uneval_expression_p (expression);
24073
24074 case AT_ENCODE_EXPR:
24075 /* An 'encode' expression is value-dependent if the operand is
24076 type-dependent. */
24077 expression = TREE_OPERAND (expression, 0);
24078 return dependent_type_p (expression);
24079
24080 case NOEXCEPT_EXPR:
24081 expression = TREE_OPERAND (expression, 0);
24082 return instantiation_dependent_uneval_expression_p (expression);
24083
24084 case SCOPE_REF:
24085 /* All instantiation-dependent expressions should also be considered
24086 value-dependent. */
24087 return instantiation_dependent_scope_ref_p (expression);
24088
24089 case COMPONENT_REF:
24090 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24091 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24092
24093 case NONTYPE_ARGUMENT_PACK:
24094 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24095 is value-dependent. */
24096 {
24097 tree values = ARGUMENT_PACK_ARGS (expression);
24098 int i, len = TREE_VEC_LENGTH (values);
24099
24100 for (i = 0; i < len; ++i)
24101 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24102 return true;
24103
24104 return false;
24105 }
24106
24107 case TRAIT_EXPR:
24108 {
24109 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24110
24111 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24112 return true;
24113
24114 if (!type2)
24115 return false;
24116
24117 if (TREE_CODE (type2) != TREE_LIST)
24118 return dependent_type_p (type2);
24119
24120 for (; type2; type2 = TREE_CHAIN (type2))
24121 if (dependent_type_p (TREE_VALUE (type2)))
24122 return true;
24123
24124 return false;
24125 }
24126
24127 case MODOP_EXPR:
24128 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24129 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24130
24131 case ARRAY_REF:
24132 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24133 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24134
24135 case ADDR_EXPR:
24136 {
24137 tree op = TREE_OPERAND (expression, 0);
24138 return (value_dependent_expression_p (op)
24139 || has_value_dependent_address (op));
24140 }
24141
24142 case REQUIRES_EXPR:
24143 /* Treat all requires-expressions as value-dependent so
24144 we don't try to fold them. */
24145 return true;
24146
24147 case TYPE_REQ:
24148 return dependent_type_p (TREE_OPERAND (expression, 0));
24149
24150 case CALL_EXPR:
24151 {
24152 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24153 return true;
24154 tree fn = get_callee_fndecl (expression);
24155 int i, nargs;
24156 nargs = call_expr_nargs (expression);
24157 for (i = 0; i < nargs; ++i)
24158 {
24159 tree op = CALL_EXPR_ARG (expression, i);
24160 /* In a call to a constexpr member function, look through the
24161 implicit ADDR_EXPR on the object argument so that it doesn't
24162 cause the call to be considered value-dependent. We also
24163 look through it in potential_constant_expression. */
24164 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24165 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24166 && TREE_CODE (op) == ADDR_EXPR)
24167 op = TREE_OPERAND (op, 0);
24168 if (value_dependent_expression_p (op))
24169 return true;
24170 }
24171 return false;
24172 }
24173
24174 case TEMPLATE_ID_EXPR:
24175 return variable_concept_p (TREE_OPERAND (expression, 0));
24176
24177 case CONSTRUCTOR:
24178 {
24179 unsigned ix;
24180 tree val;
24181 if (dependent_type_p (TREE_TYPE (expression)))
24182 return true;
24183 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24184 if (value_dependent_expression_p (val))
24185 return true;
24186 return false;
24187 }
24188
24189 case STMT_EXPR:
24190 /* Treat a GNU statement expression as dependent to avoid crashing
24191 under instantiate_non_dependent_expr; it can't be constant. */
24192 return true;
24193
24194 default:
24195 /* A constant expression is value-dependent if any subexpression is
24196 value-dependent. */
24197 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24198 {
24199 case tcc_reference:
24200 case tcc_unary:
24201 case tcc_comparison:
24202 case tcc_binary:
24203 case tcc_expression:
24204 case tcc_vl_exp:
24205 {
24206 int i, len = cp_tree_operand_length (expression);
24207
24208 for (i = 0; i < len; i++)
24209 {
24210 tree t = TREE_OPERAND (expression, i);
24211
24212 /* In some cases, some of the operands may be missing.
24213 (For example, in the case of PREDECREMENT_EXPR, the
24214 amount to increment by may be missing.) That doesn't
24215 make the expression dependent. */
24216 if (t && value_dependent_expression_p (t))
24217 return true;
24218 }
24219 }
24220 break;
24221 default:
24222 break;
24223 }
24224 break;
24225 }
24226
24227 /* The expression is not value-dependent. */
24228 return false;
24229 }
24230
24231 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24232 [temp.dep.expr]. Note that an expression with no type is
24233 considered dependent. Other parts of the compiler arrange for an
24234 expression with type-dependent subexpressions to have no type, so
24235 this function doesn't have to be fully recursive. */
24236
24237 bool
24238 type_dependent_expression_p (tree expression)
24239 {
24240 if (!processing_template_decl)
24241 return false;
24242
24243 if (expression == NULL_TREE || expression == error_mark_node)
24244 return false;
24245
24246 STRIP_ANY_LOCATION_WRAPPER (expression);
24247
24248 /* An unresolved name is always dependent. */
24249 if (identifier_p (expression)
24250 || TREE_CODE (expression) == USING_DECL
24251 || TREE_CODE (expression) == WILDCARD_DECL)
24252 return true;
24253
24254 /* A fold expression is type-dependent. */
24255 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24256 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24257 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24258 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24259 return true;
24260
24261 /* Some expression forms are never type-dependent. */
24262 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24263 || TREE_CODE (expression) == SIZEOF_EXPR
24264 || TREE_CODE (expression) == ALIGNOF_EXPR
24265 || TREE_CODE (expression) == AT_ENCODE_EXPR
24266 || TREE_CODE (expression) == NOEXCEPT_EXPR
24267 || TREE_CODE (expression) == TRAIT_EXPR
24268 || TREE_CODE (expression) == TYPEID_EXPR
24269 || TREE_CODE (expression) == DELETE_EXPR
24270 || TREE_CODE (expression) == VEC_DELETE_EXPR
24271 || TREE_CODE (expression) == THROW_EXPR
24272 || TREE_CODE (expression) == REQUIRES_EXPR)
24273 return false;
24274
24275 /* The types of these expressions depends only on the type to which
24276 the cast occurs. */
24277 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24278 || TREE_CODE (expression) == STATIC_CAST_EXPR
24279 || TREE_CODE (expression) == CONST_CAST_EXPR
24280 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24281 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24282 || TREE_CODE (expression) == CAST_EXPR)
24283 return dependent_type_p (TREE_TYPE (expression));
24284
24285 /* The types of these expressions depends only on the type created
24286 by the expression. */
24287 if (TREE_CODE (expression) == NEW_EXPR
24288 || TREE_CODE (expression) == VEC_NEW_EXPR)
24289 {
24290 /* For NEW_EXPR tree nodes created inside a template, either
24291 the object type itself or a TREE_LIST may appear as the
24292 operand 1. */
24293 tree type = TREE_OPERAND (expression, 1);
24294 if (TREE_CODE (type) == TREE_LIST)
24295 /* This is an array type. We need to check array dimensions
24296 as well. */
24297 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24298 || value_dependent_expression_p
24299 (TREE_OPERAND (TREE_VALUE (type), 1));
24300 else
24301 return dependent_type_p (type);
24302 }
24303
24304 if (TREE_CODE (expression) == SCOPE_REF)
24305 {
24306 tree scope = TREE_OPERAND (expression, 0);
24307 tree name = TREE_OPERAND (expression, 1);
24308
24309 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24310 contains an identifier associated by name lookup with one or more
24311 declarations declared with a dependent type, or...a
24312 nested-name-specifier or qualified-id that names a member of an
24313 unknown specialization. */
24314 return (type_dependent_expression_p (name)
24315 || dependent_scope_p (scope));
24316 }
24317
24318 if (TREE_CODE (expression) == TEMPLATE_DECL
24319 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24320 return uses_outer_template_parms (expression);
24321
24322 if (TREE_CODE (expression) == STMT_EXPR)
24323 expression = stmt_expr_value_expr (expression);
24324
24325 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24326 {
24327 tree elt;
24328 unsigned i;
24329
24330 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24331 {
24332 if (type_dependent_expression_p (elt))
24333 return true;
24334 }
24335 return false;
24336 }
24337
24338 /* A static data member of the current instantiation with incomplete
24339 array type is type-dependent, as the definition and specializations
24340 can have different bounds. */
24341 if (VAR_P (expression)
24342 && DECL_CLASS_SCOPE_P (expression)
24343 && dependent_type_p (DECL_CONTEXT (expression))
24344 && VAR_HAD_UNKNOWN_BOUND (expression))
24345 return true;
24346
24347 /* An array of unknown bound depending on a variadic parameter, eg:
24348
24349 template<typename... Args>
24350 void foo (Args... args)
24351 {
24352 int arr[] = { args... };
24353 }
24354
24355 template<int... vals>
24356 void bar ()
24357 {
24358 int arr[] = { vals... };
24359 }
24360
24361 If the array has no length and has an initializer, it must be that
24362 we couldn't determine its length in cp_complete_array_type because
24363 it is dependent. */
24364 if (VAR_P (expression)
24365 && TREE_TYPE (expression) != NULL_TREE
24366 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24367 && !TYPE_DOMAIN (TREE_TYPE (expression))
24368 && DECL_INITIAL (expression))
24369 return true;
24370
24371 /* A function or variable template-id is type-dependent if it has any
24372 dependent template arguments. */
24373 if (VAR_OR_FUNCTION_DECL_P (expression)
24374 && DECL_LANG_SPECIFIC (expression)
24375 && DECL_TEMPLATE_INFO (expression))
24376 {
24377 /* Consider the innermost template arguments, since those are the ones
24378 that come from the template-id; the template arguments for the
24379 enclosing class do not make it type-dependent unless they are used in
24380 the type of the decl. */
24381 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24382 && (any_dependent_template_arguments_p
24383 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24384 return true;
24385 }
24386
24387 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24388 type-dependent. Checking this is important for functions with auto return
24389 type, which looks like a dependent type. */
24390 if (TREE_CODE (expression) == FUNCTION_DECL
24391 && !(DECL_CLASS_SCOPE_P (expression)
24392 && dependent_type_p (DECL_CONTEXT (expression)))
24393 && !(DECL_FRIEND_P (expression)
24394 && (!DECL_FRIEND_CONTEXT (expression)
24395 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24396 && !DECL_LOCAL_FUNCTION_P (expression))
24397 {
24398 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24399 || undeduced_auto_decl (expression));
24400 return false;
24401 }
24402
24403 /* Always dependent, on the number of arguments if nothing else. */
24404 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24405 return true;
24406
24407 if (TREE_TYPE (expression) == unknown_type_node)
24408 {
24409 if (TREE_CODE (expression) == ADDR_EXPR)
24410 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24411 if (TREE_CODE (expression) == COMPONENT_REF
24412 || TREE_CODE (expression) == OFFSET_REF)
24413 {
24414 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24415 return true;
24416 expression = TREE_OPERAND (expression, 1);
24417 if (identifier_p (expression))
24418 return false;
24419 }
24420 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24421 if (TREE_CODE (expression) == SCOPE_REF)
24422 return false;
24423
24424 if (BASELINK_P (expression))
24425 {
24426 if (BASELINK_OPTYPE (expression)
24427 && dependent_type_p (BASELINK_OPTYPE (expression)))
24428 return true;
24429 expression = BASELINK_FUNCTIONS (expression);
24430 }
24431
24432 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24433 {
24434 if (any_dependent_template_arguments_p
24435 (TREE_OPERAND (expression, 1)))
24436 return true;
24437 expression = TREE_OPERAND (expression, 0);
24438 if (identifier_p (expression))
24439 return true;
24440 }
24441
24442 gcc_assert (TREE_CODE (expression) == OVERLOAD
24443 || TREE_CODE (expression) == FUNCTION_DECL);
24444
24445 for (lkp_iterator iter (expression); iter; ++iter)
24446 if (type_dependent_expression_p (*iter))
24447 return true;
24448
24449 return false;
24450 }
24451
24452 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24453
24454 /* Dependent type attributes might not have made it from the decl to
24455 the type yet. */
24456 if (DECL_P (expression)
24457 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24458 return true;
24459
24460 return (dependent_type_p (TREE_TYPE (expression)));
24461 }
24462
24463 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24464 type-dependent if the expression refers to a member of the current
24465 instantiation and the type of the referenced member is dependent, or the
24466 class member access expression refers to a member of an unknown
24467 specialization.
24468
24469 This function returns true if the OBJECT in such a class member access
24470 expression is of an unknown specialization. */
24471
24472 bool
24473 type_dependent_object_expression_p (tree object)
24474 {
24475 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24476 dependent. */
24477 if (TREE_CODE (object) == IDENTIFIER_NODE)
24478 return true;
24479 tree scope = TREE_TYPE (object);
24480 return (!scope || dependent_scope_p (scope));
24481 }
24482
24483 /* walk_tree callback function for instantiation_dependent_expression_p,
24484 below. Returns non-zero if a dependent subexpression is found. */
24485
24486 static tree
24487 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24488 void * /*data*/)
24489 {
24490 if (TYPE_P (*tp))
24491 {
24492 /* We don't have to worry about decltype currently because decltype
24493 of an instantiation-dependent expr is a dependent type. This
24494 might change depending on the resolution of DR 1172. */
24495 *walk_subtrees = false;
24496 return NULL_TREE;
24497 }
24498 enum tree_code code = TREE_CODE (*tp);
24499 switch (code)
24500 {
24501 /* Don't treat an argument list as dependent just because it has no
24502 TREE_TYPE. */
24503 case TREE_LIST:
24504 case TREE_VEC:
24505 return NULL_TREE;
24506
24507 case TEMPLATE_PARM_INDEX:
24508 return *tp;
24509
24510 /* Handle expressions with type operands. */
24511 case SIZEOF_EXPR:
24512 case ALIGNOF_EXPR:
24513 case TYPEID_EXPR:
24514 case AT_ENCODE_EXPR:
24515 {
24516 tree op = TREE_OPERAND (*tp, 0);
24517 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24518 op = TREE_TYPE (op);
24519 if (TYPE_P (op))
24520 {
24521 if (dependent_type_p (op))
24522 return *tp;
24523 else
24524 {
24525 *walk_subtrees = false;
24526 return NULL_TREE;
24527 }
24528 }
24529 break;
24530 }
24531
24532 case COMPONENT_REF:
24533 if (identifier_p (TREE_OPERAND (*tp, 1)))
24534 /* In a template, finish_class_member_access_expr creates a
24535 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24536 type-dependent, so that we can check access control at
24537 instantiation time (PR 42277). See also Core issue 1273. */
24538 return *tp;
24539 break;
24540
24541 case SCOPE_REF:
24542 if (instantiation_dependent_scope_ref_p (*tp))
24543 return *tp;
24544 else
24545 break;
24546
24547 /* Treat statement-expressions as dependent. */
24548 case BIND_EXPR:
24549 return *tp;
24550
24551 /* Treat requires-expressions as dependent. */
24552 case REQUIRES_EXPR:
24553 return *tp;
24554
24555 case CALL_EXPR:
24556 /* Treat calls to function concepts as dependent. */
24557 if (function_concept_check_p (*tp))
24558 return *tp;
24559 break;
24560
24561 case TEMPLATE_ID_EXPR:
24562 /* And variable concepts. */
24563 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24564 return *tp;
24565 break;
24566
24567 default:
24568 break;
24569 }
24570
24571 if (type_dependent_expression_p (*tp))
24572 return *tp;
24573 else
24574 return NULL_TREE;
24575 }
24576
24577 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24578 sense defined by the ABI:
24579
24580 "An expression is instantiation-dependent if it is type-dependent
24581 or value-dependent, or it has a subexpression that is type-dependent
24582 or value-dependent."
24583
24584 Except don't actually check value-dependence for unevaluated expressions,
24585 because in sizeof(i) we don't care about the value of i. Checking
24586 type-dependence will in turn check value-dependence of array bounds/template
24587 arguments as needed. */
24588
24589 bool
24590 instantiation_dependent_uneval_expression_p (tree expression)
24591 {
24592 tree result;
24593
24594 if (!processing_template_decl)
24595 return false;
24596
24597 if (expression == error_mark_node)
24598 return false;
24599
24600 result = cp_walk_tree_without_duplicates (&expression,
24601 instantiation_dependent_r, NULL);
24602 return result != NULL_TREE;
24603 }
24604
24605 /* As above, but also check value-dependence of the expression as a whole. */
24606
24607 bool
24608 instantiation_dependent_expression_p (tree expression)
24609 {
24610 return (instantiation_dependent_uneval_expression_p (expression)
24611 || value_dependent_expression_p (expression));
24612 }
24613
24614 /* Like type_dependent_expression_p, but it also works while not processing
24615 a template definition, i.e. during substitution or mangling. */
24616
24617 bool
24618 type_dependent_expression_p_push (tree expr)
24619 {
24620 bool b;
24621 ++processing_template_decl;
24622 b = type_dependent_expression_p (expr);
24623 --processing_template_decl;
24624 return b;
24625 }
24626
24627 /* Returns TRUE if ARGS contains a type-dependent expression. */
24628
24629 bool
24630 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24631 {
24632 unsigned int i;
24633 tree arg;
24634
24635 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24636 {
24637 if (type_dependent_expression_p (arg))
24638 return true;
24639 }
24640 return false;
24641 }
24642
24643 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24644 expressions) contains any type-dependent expressions. */
24645
24646 bool
24647 any_type_dependent_elements_p (const_tree list)
24648 {
24649 for (; list; list = TREE_CHAIN (list))
24650 if (type_dependent_expression_p (TREE_VALUE (list)))
24651 return true;
24652
24653 return false;
24654 }
24655
24656 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24657 expressions) contains any value-dependent expressions. */
24658
24659 bool
24660 any_value_dependent_elements_p (const_tree list)
24661 {
24662 for (; list; list = TREE_CHAIN (list))
24663 if (value_dependent_expression_p (TREE_VALUE (list)))
24664 return true;
24665
24666 return false;
24667 }
24668
24669 /* Returns TRUE if the ARG (a template argument) is dependent. */
24670
24671 bool
24672 dependent_template_arg_p (tree arg)
24673 {
24674 if (!processing_template_decl)
24675 return false;
24676
24677 /* Assume a template argument that was wrongly written by the user
24678 is dependent. This is consistent with what
24679 any_dependent_template_arguments_p [that calls this function]
24680 does. */
24681 if (!arg || arg == error_mark_node)
24682 return true;
24683
24684 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24685 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24686
24687 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24688 return true;
24689 if (TREE_CODE (arg) == TEMPLATE_DECL)
24690 {
24691 if (DECL_TEMPLATE_PARM_P (arg))
24692 return true;
24693 /* A member template of a dependent class is not necessarily
24694 type-dependent, but it is a dependent template argument because it
24695 will be a member of an unknown specialization to that template. */
24696 tree scope = CP_DECL_CONTEXT (arg);
24697 return TYPE_P (scope) && dependent_type_p (scope);
24698 }
24699 else if (ARGUMENT_PACK_P (arg))
24700 {
24701 tree args = ARGUMENT_PACK_ARGS (arg);
24702 int i, len = TREE_VEC_LENGTH (args);
24703 for (i = 0; i < len; ++i)
24704 {
24705 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24706 return true;
24707 }
24708
24709 return false;
24710 }
24711 else if (TYPE_P (arg))
24712 return dependent_type_p (arg);
24713 else
24714 return (type_dependent_expression_p (arg)
24715 || value_dependent_expression_p (arg));
24716 }
24717
24718 /* Returns true if ARGS (a collection of template arguments) contains
24719 any types that require structural equality testing. */
24720
24721 bool
24722 any_template_arguments_need_structural_equality_p (tree args)
24723 {
24724 int i;
24725 int j;
24726
24727 if (!args)
24728 return false;
24729 if (args == error_mark_node)
24730 return true;
24731
24732 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24733 {
24734 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24735 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24736 {
24737 tree arg = TREE_VEC_ELT (level, j);
24738 tree packed_args = NULL_TREE;
24739 int k, len = 1;
24740
24741 if (ARGUMENT_PACK_P (arg))
24742 {
24743 /* Look inside the argument pack. */
24744 packed_args = ARGUMENT_PACK_ARGS (arg);
24745 len = TREE_VEC_LENGTH (packed_args);
24746 }
24747
24748 for (k = 0; k < len; ++k)
24749 {
24750 if (packed_args)
24751 arg = TREE_VEC_ELT (packed_args, k);
24752
24753 if (error_operand_p (arg))
24754 return true;
24755 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24756 continue;
24757 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24758 return true;
24759 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24760 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24761 return true;
24762 }
24763 }
24764 }
24765
24766 return false;
24767 }
24768
24769 /* Returns true if ARGS (a collection of template arguments) contains
24770 any dependent arguments. */
24771
24772 bool
24773 any_dependent_template_arguments_p (const_tree args)
24774 {
24775 int i;
24776 int j;
24777
24778 if (!args)
24779 return false;
24780 if (args == error_mark_node)
24781 return true;
24782
24783 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24784 {
24785 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24786 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24787 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24788 return true;
24789 }
24790
24791 return false;
24792 }
24793
24794 /* Returns TRUE if the template TMPL is type-dependent. */
24795
24796 bool
24797 dependent_template_p (tree tmpl)
24798 {
24799 if (TREE_CODE (tmpl) == OVERLOAD)
24800 {
24801 for (lkp_iterator iter (tmpl); iter; ++iter)
24802 if (dependent_template_p (*iter))
24803 return true;
24804 return false;
24805 }
24806
24807 /* Template template parameters are dependent. */
24808 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24809 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24810 return true;
24811 /* So are names that have not been looked up. */
24812 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24813 return true;
24814 return false;
24815 }
24816
24817 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24818
24819 bool
24820 dependent_template_id_p (tree tmpl, tree args)
24821 {
24822 return (dependent_template_p (tmpl)
24823 || any_dependent_template_arguments_p (args));
24824 }
24825
24826 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24827 are dependent. */
24828
24829 bool
24830 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24831 {
24832 int i;
24833
24834 if (!processing_template_decl)
24835 return false;
24836
24837 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24838 {
24839 tree decl = TREE_VEC_ELT (declv, i);
24840 tree init = TREE_VEC_ELT (initv, i);
24841 tree cond = TREE_VEC_ELT (condv, i);
24842 tree incr = TREE_VEC_ELT (incrv, i);
24843
24844 if (type_dependent_expression_p (decl)
24845 || TREE_CODE (decl) == SCOPE_REF)
24846 return true;
24847
24848 if (init && type_dependent_expression_p (init))
24849 return true;
24850
24851 if (type_dependent_expression_p (cond))
24852 return true;
24853
24854 if (COMPARISON_CLASS_P (cond)
24855 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24856 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24857 return true;
24858
24859 if (TREE_CODE (incr) == MODOP_EXPR)
24860 {
24861 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24862 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24863 return true;
24864 }
24865 else if (type_dependent_expression_p (incr))
24866 return true;
24867 else if (TREE_CODE (incr) == MODIFY_EXPR)
24868 {
24869 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24870 return true;
24871 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24872 {
24873 tree t = TREE_OPERAND (incr, 1);
24874 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24875 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24876 return true;
24877 }
24878 }
24879 }
24880
24881 return false;
24882 }
24883
24884 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24885 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24886 no such TYPE can be found. Note that this function peers inside
24887 uninstantiated templates and therefore should be used only in
24888 extremely limited situations. ONLY_CURRENT_P restricts this
24889 peering to the currently open classes hierarchy (which is required
24890 when comparing types). */
24891
24892 tree
24893 resolve_typename_type (tree type, bool only_current_p)
24894 {
24895 tree scope;
24896 tree name;
24897 tree decl;
24898 int quals;
24899 tree pushed_scope;
24900 tree result;
24901
24902 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24903
24904 scope = TYPE_CONTEXT (type);
24905 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24906 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24907 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24908 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24909 identifier of the TYPENAME_TYPE anymore.
24910 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24911 TYPENAME_TYPE instead, we avoid messing up with a possible
24912 typedef variant case. */
24913 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24914
24915 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24916 it first before we can figure out what NAME refers to. */
24917 if (TREE_CODE (scope) == TYPENAME_TYPE)
24918 {
24919 if (TYPENAME_IS_RESOLVING_P (scope))
24920 /* Given a class template A with a dependent base with nested type C,
24921 typedef typename A::C::C C will land us here, as trying to resolve
24922 the initial A::C leads to the local C typedef, which leads back to
24923 A::C::C. So we break the recursion now. */
24924 return type;
24925 else
24926 scope = resolve_typename_type (scope, only_current_p);
24927 }
24928 /* If we don't know what SCOPE refers to, then we cannot resolve the
24929 TYPENAME_TYPE. */
24930 if (!CLASS_TYPE_P (scope))
24931 return type;
24932 /* If this is a typedef, we don't want to look inside (c++/11987). */
24933 if (typedef_variant_p (type))
24934 return type;
24935 /* If SCOPE isn't the template itself, it will not have a valid
24936 TYPE_FIELDS list. */
24937 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24938 /* scope is either the template itself or a compatible instantiation
24939 like X<T>, so look up the name in the original template. */
24940 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24941 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24942 gcc_checking_assert (uses_template_parms (scope));
24943 /* If scope has no fields, it can't be a current instantiation. Check this
24944 before currently_open_class to avoid infinite recursion (71515). */
24945 if (!TYPE_FIELDS (scope))
24946 return type;
24947 /* If the SCOPE is not the current instantiation, there's no reason
24948 to look inside it. */
24949 if (only_current_p && !currently_open_class (scope))
24950 return type;
24951 /* Enter the SCOPE so that name lookup will be resolved as if we
24952 were in the class definition. In particular, SCOPE will no
24953 longer be considered a dependent type. */
24954 pushed_scope = push_scope (scope);
24955 /* Look up the declaration. */
24956 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24957 tf_warning_or_error);
24958
24959 result = NULL_TREE;
24960
24961 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24962 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24963 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24964 if (!decl)
24965 /*nop*/;
24966 else if (identifier_p (fullname)
24967 && TREE_CODE (decl) == TYPE_DECL)
24968 {
24969 result = TREE_TYPE (decl);
24970 if (result == error_mark_node)
24971 result = NULL_TREE;
24972 }
24973 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24974 && DECL_CLASS_TEMPLATE_P (decl))
24975 {
24976 /* Obtain the template and the arguments. */
24977 tree tmpl = TREE_OPERAND (fullname, 0);
24978 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24979 {
24980 /* We get here with a plain identifier because a previous tentative
24981 parse of the nested-name-specifier as part of a ptr-operator saw
24982 ::template X<A>. The use of ::template is necessary in a
24983 ptr-operator, but wrong in a declarator-id.
24984
24985 [temp.names]: In a qualified-id of a declarator-id, the keyword
24986 template shall not appear at the top level. */
24987 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24988 "keyword %<template%> not allowed in declarator-id");
24989 tmpl = decl;
24990 }
24991 tree args = TREE_OPERAND (fullname, 1);
24992 /* Instantiate the template. */
24993 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24994 /*entering_scope=*/true,
24995 tf_error | tf_user);
24996 if (result == error_mark_node)
24997 result = NULL_TREE;
24998 }
24999
25000 /* Leave the SCOPE. */
25001 if (pushed_scope)
25002 pop_scope (pushed_scope);
25003
25004 /* If we failed to resolve it, return the original typename. */
25005 if (!result)
25006 return type;
25007
25008 /* If lookup found a typename type, resolve that too. */
25009 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25010 {
25011 /* Ill-formed programs can cause infinite recursion here, so we
25012 must catch that. */
25013 TYPENAME_IS_RESOLVING_P (result) = 1;
25014 result = resolve_typename_type (result, only_current_p);
25015 TYPENAME_IS_RESOLVING_P (result) = 0;
25016 }
25017
25018 /* Qualify the resulting type. */
25019 quals = cp_type_quals (type);
25020 if (quals)
25021 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25022
25023 return result;
25024 }
25025
25026 /* EXPR is an expression which is not type-dependent. Return a proxy
25027 for EXPR that can be used to compute the types of larger
25028 expressions containing EXPR. */
25029
25030 tree
25031 build_non_dependent_expr (tree expr)
25032 {
25033 tree orig_expr = expr;
25034 tree inner_expr;
25035
25036 /* When checking, try to get a constant value for all non-dependent
25037 expressions in order to expose bugs in *_dependent_expression_p
25038 and constexpr. This can affect code generation, see PR70704, so
25039 only do this for -fchecking=2. */
25040 if (flag_checking > 1
25041 && cxx_dialect >= cxx11
25042 /* Don't do this during nsdmi parsing as it can lead to
25043 unexpected recursive instantiations. */
25044 && !parsing_nsdmi ()
25045 /* Don't do this during concept expansion either and for
25046 the same reason. */
25047 && !expanding_concept ())
25048 fold_non_dependent_expr (expr);
25049
25050 STRIP_ANY_LOCATION_WRAPPER (expr);
25051
25052 /* Preserve OVERLOADs; the functions must be available to resolve
25053 types. */
25054 inner_expr = expr;
25055 if (TREE_CODE (inner_expr) == STMT_EXPR)
25056 inner_expr = stmt_expr_value_expr (inner_expr);
25057 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25058 inner_expr = TREE_OPERAND (inner_expr, 0);
25059 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25060 inner_expr = TREE_OPERAND (inner_expr, 1);
25061 if (is_overloaded_fn (inner_expr)
25062 || TREE_CODE (inner_expr) == OFFSET_REF)
25063 return orig_expr;
25064 /* There is no need to return a proxy for a variable. */
25065 if (VAR_P (expr))
25066 return orig_expr;
25067 /* Preserve string constants; conversions from string constants to
25068 "char *" are allowed, even though normally a "const char *"
25069 cannot be used to initialize a "char *". */
25070 if (TREE_CODE (expr) == STRING_CST)
25071 return orig_expr;
25072 /* Preserve void and arithmetic constants, as an optimization -- there is no
25073 reason to create a new node. */
25074 if (TREE_CODE (expr) == VOID_CST
25075 || TREE_CODE (expr) == INTEGER_CST
25076 || TREE_CODE (expr) == REAL_CST)
25077 return orig_expr;
25078 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25079 There is at least one place where we want to know that a
25080 particular expression is a throw-expression: when checking a ?:
25081 expression, there are special rules if the second or third
25082 argument is a throw-expression. */
25083 if (TREE_CODE (expr) == THROW_EXPR)
25084 return orig_expr;
25085
25086 /* Don't wrap an initializer list, we need to be able to look inside. */
25087 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25088 return orig_expr;
25089
25090 /* Don't wrap a dummy object, we need to be able to test for it. */
25091 if (is_dummy_object (expr))
25092 return orig_expr;
25093
25094 if (TREE_CODE (expr) == COND_EXPR)
25095 return build3 (COND_EXPR,
25096 TREE_TYPE (expr),
25097 TREE_OPERAND (expr, 0),
25098 (TREE_OPERAND (expr, 1)
25099 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25100 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25101 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25102 if (TREE_CODE (expr) == COMPOUND_EXPR
25103 && !COMPOUND_EXPR_OVERLOADED (expr))
25104 return build2 (COMPOUND_EXPR,
25105 TREE_TYPE (expr),
25106 TREE_OPERAND (expr, 0),
25107 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25108
25109 /* If the type is unknown, it can't really be non-dependent */
25110 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25111
25112 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25113 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25114 }
25115
25116 /* ARGS is a vector of expressions as arguments to a function call.
25117 Replace the arguments with equivalent non-dependent expressions.
25118 This modifies ARGS in place. */
25119
25120 void
25121 make_args_non_dependent (vec<tree, va_gc> *args)
25122 {
25123 unsigned int ix;
25124 tree arg;
25125
25126 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25127 {
25128 tree newarg = build_non_dependent_expr (arg);
25129 if (newarg != arg)
25130 (*args)[ix] = newarg;
25131 }
25132 }
25133
25134 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25135 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25136 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25137
25138 static tree
25139 make_auto_1 (tree name, bool set_canonical)
25140 {
25141 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25142 TYPE_NAME (au) = build_decl (input_location,
25143 TYPE_DECL, name, au);
25144 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25145 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25146 (0, processing_template_decl + 1, processing_template_decl + 1,
25147 TYPE_NAME (au), NULL_TREE);
25148 if (set_canonical)
25149 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25150 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25151 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25152
25153 return au;
25154 }
25155
25156 tree
25157 make_decltype_auto (void)
25158 {
25159 return make_auto_1 (decltype_auto_identifier, true);
25160 }
25161
25162 tree
25163 make_auto (void)
25164 {
25165 return make_auto_1 (auto_identifier, true);
25166 }
25167
25168 /* Return a C++17 deduction placeholder for class template TMPL. */
25169
25170 tree
25171 make_template_placeholder (tree tmpl)
25172 {
25173 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25174 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25175 return t;
25176 }
25177
25178 /* True iff T is a C++17 class template deduction placeholder. */
25179
25180 bool
25181 template_placeholder_p (tree t)
25182 {
25183 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25184 }
25185
25186 /* Make a "constrained auto" type-specifier. This is an
25187 auto type with constraints that must be associated after
25188 deduction. The constraint is formed from the given
25189 CONC and its optional sequence of arguments, which are
25190 non-null if written as partial-concept-id. */
25191
25192 tree
25193 make_constrained_auto (tree con, tree args)
25194 {
25195 tree type = make_auto_1 (auto_identifier, false);
25196
25197 /* Build the constraint. */
25198 tree tmpl = DECL_TI_TEMPLATE (con);
25199 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25200 expr = build_concept_check (expr, type, args);
25201
25202 tree constr = normalize_expression (expr);
25203 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25204
25205 /* Our canonical type depends on the constraint. */
25206 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25207
25208 /* Attach the constraint to the type declaration. */
25209 tree decl = TYPE_NAME (type);
25210 return decl;
25211 }
25212
25213 /* Given type ARG, return std::initializer_list<ARG>. */
25214
25215 static tree
25216 listify (tree arg)
25217 {
25218 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25219
25220 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25221 {
25222 gcc_rich_location richloc (input_location);
25223 maybe_add_include_fixit (&richloc, "<initializer_list>");
25224 error_at (&richloc,
25225 "deducing from brace-enclosed initializer list"
25226 " requires %<#include <initializer_list>%>");
25227
25228 return error_mark_node;
25229 }
25230 tree argvec = make_tree_vec (1);
25231 TREE_VEC_ELT (argvec, 0) = arg;
25232
25233 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25234 NULL_TREE, 0, tf_warning_or_error);
25235 }
25236
25237 /* Replace auto in TYPE with std::initializer_list<auto>. */
25238
25239 static tree
25240 listify_autos (tree type, tree auto_node)
25241 {
25242 tree init_auto = listify (auto_node);
25243 tree argvec = make_tree_vec (1);
25244 TREE_VEC_ELT (argvec, 0) = init_auto;
25245 if (processing_template_decl)
25246 argvec = add_to_template_args (current_template_args (), argvec);
25247 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25248 }
25249
25250 /* Hash traits for hashing possibly constrained 'auto'
25251 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25252
25253 struct auto_hash : default_hash_traits<tree>
25254 {
25255 static inline hashval_t hash (tree);
25256 static inline bool equal (tree, tree);
25257 };
25258
25259 /* Hash the 'auto' T. */
25260
25261 inline hashval_t
25262 auto_hash::hash (tree t)
25263 {
25264 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25265 /* Matching constrained-type-specifiers denote the same template
25266 parameter, so hash the constraint. */
25267 return hash_placeholder_constraint (c);
25268 else
25269 /* But unconstrained autos are all separate, so just hash the pointer. */
25270 return iterative_hash_object (t, 0);
25271 }
25272
25273 /* Compare two 'auto's. */
25274
25275 inline bool
25276 auto_hash::equal (tree t1, tree t2)
25277 {
25278 if (t1 == t2)
25279 return true;
25280
25281 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25282 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25283
25284 /* Two unconstrained autos are distinct. */
25285 if (!c1 || !c2)
25286 return false;
25287
25288 return equivalent_placeholder_constraints (c1, c2);
25289 }
25290
25291 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25292 constrained) auto, add it to the vector. */
25293
25294 static int
25295 extract_autos_r (tree t, void *data)
25296 {
25297 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25298 if (is_auto (t))
25299 {
25300 /* All the autos were built with index 0; fix that up now. */
25301 tree *p = hash.find_slot (t, INSERT);
25302 unsigned idx;
25303 if (*p)
25304 /* If this is a repeated constrained-type-specifier, use the index we
25305 chose before. */
25306 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25307 else
25308 {
25309 /* Otherwise this is new, so use the current count. */
25310 *p = t;
25311 idx = hash.elements () - 1;
25312 }
25313 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25314 }
25315
25316 /* Always keep walking. */
25317 return 0;
25318 }
25319
25320 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25321 says they can appear anywhere in the type. */
25322
25323 static tree
25324 extract_autos (tree type)
25325 {
25326 hash_set<tree> visited;
25327 hash_table<auto_hash> hash (2);
25328
25329 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25330
25331 tree tree_vec = make_tree_vec (hash.elements());
25332 for (hash_table<auto_hash>::iterator iter = hash.begin();
25333 iter != hash.end(); ++iter)
25334 {
25335 tree elt = *iter;
25336 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25337 TREE_VEC_ELT (tree_vec, i)
25338 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25339 }
25340
25341 return tree_vec;
25342 }
25343
25344 /* The stem for deduction guide names. */
25345 const char *const dguide_base = "__dguide_";
25346
25347 /* Return the name for a deduction guide for class template TMPL. */
25348
25349 tree
25350 dguide_name (tree tmpl)
25351 {
25352 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25353 tree tname = TYPE_IDENTIFIER (type);
25354 char *buf = (char *) alloca (1 + strlen (dguide_base)
25355 + IDENTIFIER_LENGTH (tname));
25356 memcpy (buf, dguide_base, strlen (dguide_base));
25357 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25358 IDENTIFIER_LENGTH (tname) + 1);
25359 tree dname = get_identifier (buf);
25360 TREE_TYPE (dname) = type;
25361 return dname;
25362 }
25363
25364 /* True if NAME is the name of a deduction guide. */
25365
25366 bool
25367 dguide_name_p (tree name)
25368 {
25369 return (TREE_TYPE (name)
25370 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25371 strlen (dguide_base)));
25372 }
25373
25374 /* True if FN is a deduction guide. */
25375
25376 bool
25377 deduction_guide_p (const_tree fn)
25378 {
25379 if (DECL_P (fn))
25380 if (tree name = DECL_NAME (fn))
25381 return dguide_name_p (name);
25382 return false;
25383 }
25384
25385 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25386
25387 bool
25388 copy_guide_p (const_tree fn)
25389 {
25390 gcc_assert (deduction_guide_p (fn));
25391 if (!DECL_ARTIFICIAL (fn))
25392 return false;
25393 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25394 return (TREE_CHAIN (parms) == void_list_node
25395 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25396 }
25397
25398 /* True if FN is a guide generated from a constructor template. */
25399
25400 bool
25401 template_guide_p (const_tree fn)
25402 {
25403 gcc_assert (deduction_guide_p (fn));
25404 if (!DECL_ARTIFICIAL (fn))
25405 return false;
25406 tree tmpl = DECL_TI_TEMPLATE (fn);
25407 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25408 return PRIMARY_TEMPLATE_P (org);
25409 return false;
25410 }
25411
25412 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25413 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25414 template parameter types. Note that the handling of template template
25415 parameters relies on current_template_parms being set appropriately for the
25416 new template. */
25417
25418 static tree
25419 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25420 tree tsubst_args, tsubst_flags_t complain)
25421 {
25422 tree oldidx = get_template_parm_index (olddecl);
25423
25424 tree newtype;
25425 if (TREE_CODE (olddecl) == TYPE_DECL
25426 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25427 {
25428 tree oldtype = TREE_TYPE (olddecl);
25429 newtype = cxx_make_type (TREE_CODE (oldtype));
25430 TYPE_MAIN_VARIANT (newtype) = newtype;
25431 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25432 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25433 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25434 }
25435 else
25436 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25437 complain, NULL_TREE);
25438
25439 tree newdecl
25440 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25441 DECL_NAME (olddecl), newtype);
25442 SET_DECL_TEMPLATE_PARM_P (newdecl);
25443
25444 tree newidx;
25445 if (TREE_CODE (olddecl) == TYPE_DECL
25446 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25447 {
25448 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25449 = build_template_parm_index (index, level, level,
25450 newdecl, newtype);
25451 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25452 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25453 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25454 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25455
25456 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25457 {
25458 DECL_TEMPLATE_RESULT (newdecl)
25459 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25460 DECL_NAME (olddecl), newtype);
25461 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25462 // First create a copy (ttargs) of tsubst_args with an
25463 // additional level for the template template parameter's own
25464 // template parameters (ttparms).
25465 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25466 (DECL_TEMPLATE_PARMS (olddecl)));
25467 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25468 tree ttargs = make_tree_vec (depth + 1);
25469 for (int i = 0; i < depth; ++i)
25470 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25471 TREE_VEC_ELT (ttargs, depth)
25472 = template_parms_level_to_args (ttparms);
25473 // Substitute ttargs into ttparms to fix references to
25474 // other template parameters.
25475 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25476 complain);
25477 // Now substitute again with args based on tparms, to reduce
25478 // the level of the ttparms.
25479 ttargs = current_template_args ();
25480 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25481 complain);
25482 // Finally, tack the adjusted parms onto tparms.
25483 ttparms = tree_cons (size_int (depth), ttparms,
25484 current_template_parms);
25485 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25486 }
25487 }
25488 else
25489 {
25490 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25491 tree newconst
25492 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25493 TREE_CODE (oldconst),
25494 DECL_NAME (oldconst), newtype);
25495 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25496 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25497 SET_DECL_TEMPLATE_PARM_P (newconst);
25498 newidx = build_template_parm_index (index, level, level,
25499 newconst, newtype);
25500 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25501 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25502 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25503 }
25504
25505 return newdecl;
25506 }
25507
25508 /* Returns a C++17 class deduction guide template based on the constructor
25509 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25510 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25511
25512 static tree
25513 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25514 {
25515 tree type, tparms, targs, fparms, fargs, ci;
25516 bool memtmpl = false;
25517 bool explicit_p;
25518 location_t loc;
25519 tree fn_tmpl = NULL_TREE;
25520
25521 if (TYPE_P (ctor))
25522 {
25523 type = ctor;
25524 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25525 if (copy_p)
25526 {
25527 type = TREE_TYPE (type);
25528 fparms = tree_cons (NULL_TREE, type, void_list_node);
25529 }
25530 else
25531 fparms = void_list_node;
25532
25533 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25534 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25535 targs = CLASSTYPE_TI_ARGS (type);
25536 ci = NULL_TREE;
25537 fargs = NULL_TREE;
25538 loc = DECL_SOURCE_LOCATION (ctmpl);
25539 explicit_p = false;
25540 }
25541 else
25542 {
25543 ++processing_template_decl;
25544
25545 fn_tmpl
25546 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25547 : DECL_TI_TEMPLATE (ctor));
25548 if (outer_args)
25549 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25550 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25551
25552 type = DECL_CONTEXT (ctor);
25553
25554 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25555 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25556 fully specialized args for the enclosing class. Strip those off, as
25557 the deduction guide won't have those template parameters. */
25558 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25559 TMPL_PARMS_DEPTH (tparms));
25560 /* Discard the 'this' parameter. */
25561 fparms = FUNCTION_ARG_CHAIN (ctor);
25562 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25563 ci = get_constraints (ctor);
25564 loc = DECL_SOURCE_LOCATION (ctor);
25565 explicit_p = DECL_NONCONVERTING_P (ctor);
25566
25567 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25568 {
25569 memtmpl = true;
25570
25571 /* For a member template constructor, we need to flatten the two
25572 template parameter lists into one, and then adjust the function
25573 signature accordingly. This gets...complicated. */
25574 tree save_parms = current_template_parms;
25575
25576 /* For a member template we should have two levels of parms/args, one
25577 for the class and one for the constructor. We stripped
25578 specialized args for further enclosing classes above. */
25579 const int depth = 2;
25580 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25581
25582 /* Template args for translating references to the two-level template
25583 parameters into references to the one-level template parameters we
25584 are creating. */
25585 tree tsubst_args = copy_node (targs);
25586 TMPL_ARGS_LEVEL (tsubst_args, depth)
25587 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25588
25589 /* Template parms for the constructor template. */
25590 tree ftparms = TREE_VALUE (tparms);
25591 unsigned flen = TREE_VEC_LENGTH (ftparms);
25592 /* Template parms for the class template. */
25593 tparms = TREE_CHAIN (tparms);
25594 tree ctparms = TREE_VALUE (tparms);
25595 unsigned clen = TREE_VEC_LENGTH (ctparms);
25596 /* Template parms for the deduction guide start as a copy of the
25597 template parms for the class. We set current_template_parms for
25598 lookup_template_class_1. */
25599 current_template_parms = tparms = copy_node (tparms);
25600 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25601 for (unsigned i = 0; i < clen; ++i)
25602 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25603
25604 /* Now we need to rewrite the constructor parms to append them to the
25605 class parms. */
25606 for (unsigned i = 0; i < flen; ++i)
25607 {
25608 unsigned index = i + clen;
25609 unsigned level = 1;
25610 tree oldelt = TREE_VEC_ELT (ftparms, i);
25611 tree olddecl = TREE_VALUE (oldelt);
25612 tree newdecl = rewrite_template_parm (olddecl, index, level,
25613 tsubst_args, complain);
25614 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25615 tsubst_args, complain, ctor);
25616 tree list = build_tree_list (newdef, newdecl);
25617 TEMPLATE_PARM_CONSTRAINTS (list)
25618 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25619 tsubst_args, complain, ctor);
25620 TREE_VEC_ELT (new_vec, index) = list;
25621 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25622 }
25623
25624 /* Now we have a final set of template parms to substitute into the
25625 function signature. */
25626 targs = template_parms_to_args (tparms);
25627 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25628 complain, ctor);
25629 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25630 if (ci)
25631 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25632
25633 current_template_parms = save_parms;
25634 }
25635 --processing_template_decl;
25636 }
25637
25638 if (!memtmpl)
25639 {
25640 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25641 tparms = copy_node (tparms);
25642 INNERMOST_TEMPLATE_PARMS (tparms)
25643 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25644 }
25645
25646 tree fntype = build_function_type (type, fparms);
25647 tree ded_fn = build_lang_decl_loc (loc,
25648 FUNCTION_DECL,
25649 dguide_name (type), fntype);
25650 DECL_ARGUMENTS (ded_fn) = fargs;
25651 DECL_ARTIFICIAL (ded_fn) = true;
25652 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25653 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25654 DECL_ARTIFICIAL (ded_tmpl) = true;
25655 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25656 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25657 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25658 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25659 if (DECL_P (ctor))
25660 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25661 if (ci)
25662 set_constraints (ded_tmpl, ci);
25663
25664 return ded_tmpl;
25665 }
25666
25667 /* Deduce template arguments for the class template placeholder PTYPE for
25668 template TMPL based on the initializer INIT, and return the resulting
25669 type. */
25670
25671 static tree
25672 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25673 tsubst_flags_t complain)
25674 {
25675 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25676 {
25677 /* We should have handled this in the caller. */
25678 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25679 return ptype;
25680 if (complain & tf_error)
25681 error ("non-class template %qT used without template arguments", tmpl);
25682 return error_mark_node;
25683 }
25684
25685 tree type = TREE_TYPE (tmpl);
25686
25687 bool try_list_ctor = false;
25688
25689 vec<tree,va_gc> *args;
25690 if (init == NULL_TREE
25691 || TREE_CODE (init) == TREE_LIST)
25692 args = make_tree_vector_from_list (init);
25693 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25694 {
25695 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25696 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25697 {
25698 /* As an exception, the first phase in 16.3.1.7 (considering the
25699 initializer list as a single argument) is omitted if the
25700 initializer list consists of a single expression of type cv U,
25701 where U is a specialization of C or a class derived from a
25702 specialization of C. */
25703 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25704 tree etype = TREE_TYPE (elt);
25705
25706 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25707 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25708 int err = unify (tparms, targs, type, etype,
25709 UNIFY_ALLOW_DERIVED, /*explain*/false);
25710 if (err == 0)
25711 try_list_ctor = false;
25712 ggc_free (targs);
25713 }
25714 if (try_list_ctor || is_std_init_list (type))
25715 args = make_tree_vector_single (init);
25716 else
25717 args = make_tree_vector_from_ctor (init);
25718 }
25719 else
25720 args = make_tree_vector_single (init);
25721
25722 tree dname = dguide_name (tmpl);
25723 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25724 /*type*/false, /*complain*/false,
25725 /*hidden*/false);
25726 bool elided = false;
25727 if (cands == error_mark_node)
25728 cands = NULL_TREE;
25729
25730 /* Prune explicit deduction guides in copy-initialization context. */
25731 if (flags & LOOKUP_ONLYCONVERTING)
25732 {
25733 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25734 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25735 elided = true;
25736
25737 if (elided)
25738 {
25739 /* Found a nonconverting guide, prune the candidates. */
25740 tree pruned = NULL_TREE;
25741 for (lkp_iterator iter (cands); iter; ++iter)
25742 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25743 pruned = lookup_add (*iter, pruned);
25744
25745 cands = pruned;
25746 }
25747 }
25748
25749 tree outer_args = NULL_TREE;
25750 if (DECL_CLASS_SCOPE_P (tmpl)
25751 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25752 {
25753 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25754 type = TREE_TYPE (most_general_template (tmpl));
25755 }
25756
25757 bool saw_ctor = false;
25758 // FIXME cache artificial deduction guides
25759 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25760 {
25761 tree guide = build_deduction_guide (*iter, outer_args, complain);
25762 if ((flags & LOOKUP_ONLYCONVERTING)
25763 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25764 elided = true;
25765 else
25766 cands = lookup_add (guide, cands);
25767
25768 saw_ctor = true;
25769 }
25770
25771 tree call = error_mark_node;
25772
25773 /* If this is list-initialization and the class has a list constructor, first
25774 try deducing from the list as a single argument, as [over.match.list]. */
25775 tree list_cands = NULL_TREE;
25776 if (try_list_ctor && cands)
25777 for (lkp_iterator iter (cands); iter; ++iter)
25778 {
25779 tree dg = *iter;
25780 if (is_list_ctor (dg))
25781 list_cands = lookup_add (dg, list_cands);
25782 }
25783 if (list_cands)
25784 {
25785 ++cp_unevaluated_operand;
25786 call = build_new_function_call (list_cands, &args, tf_decltype);
25787 --cp_unevaluated_operand;
25788
25789 if (call == error_mark_node)
25790 {
25791 /* That didn't work, now try treating the list as a sequence of
25792 arguments. */
25793 release_tree_vector (args);
25794 args = make_tree_vector_from_ctor (init);
25795 }
25796 }
25797
25798 /* Maybe generate an implicit deduction guide. */
25799 if (call == error_mark_node && args->length () < 2)
25800 {
25801 tree gtype = NULL_TREE;
25802
25803 if (args->length () == 1)
25804 /* Generate a copy guide. */
25805 gtype = build_reference_type (type);
25806 else if (!saw_ctor)
25807 /* Generate a default guide. */
25808 gtype = type;
25809
25810 if (gtype)
25811 {
25812 tree guide = build_deduction_guide (gtype, outer_args, complain);
25813 cands = lookup_add (guide, cands);
25814 }
25815 }
25816
25817 if (elided && !cands)
25818 {
25819 error ("cannot deduce template arguments for copy-initialization"
25820 " of %qT, as it has no non-explicit deduction guides or "
25821 "user-declared constructors", type);
25822 return error_mark_node;
25823 }
25824 else if (!cands && call == error_mark_node)
25825 {
25826 error ("cannot deduce template arguments of %qT, as it has no viable "
25827 "deduction guides", type);
25828 return error_mark_node;
25829 }
25830
25831 if (call == error_mark_node)
25832 {
25833 ++cp_unevaluated_operand;
25834 call = build_new_function_call (cands, &args, tf_decltype);
25835 --cp_unevaluated_operand;
25836 }
25837
25838 if (call == error_mark_node && (complain & tf_warning_or_error))
25839 {
25840 error ("class template argument deduction failed:");
25841
25842 ++cp_unevaluated_operand;
25843 call = build_new_function_call (cands, &args, complain | tf_decltype);
25844 --cp_unevaluated_operand;
25845
25846 if (elided)
25847 inform (input_location, "explicit deduction guides not considered "
25848 "for copy-initialization");
25849 }
25850
25851 release_tree_vector (args);
25852
25853 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25854 }
25855
25856 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25857 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25858
25859 tree
25860 do_auto_deduction (tree type, tree init, tree auto_node)
25861 {
25862 return do_auto_deduction (type, init, auto_node,
25863 tf_warning_or_error,
25864 adc_unspecified);
25865 }
25866
25867 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25868 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25869 The CONTEXT determines the context in which auto deduction is performed
25870 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25871 OUTER_TARGS are used during template argument deduction
25872 (context == adc_unify) to properly substitute the result, and is ignored
25873 in other contexts.
25874
25875 For partial-concept-ids, extra args may be appended to the list of deduced
25876 template arguments prior to determining constraint satisfaction. */
25877
25878 tree
25879 do_auto_deduction (tree type, tree init, tree auto_node,
25880 tsubst_flags_t complain, auto_deduction_context context,
25881 tree outer_targs, int flags)
25882 {
25883 tree targs;
25884
25885 if (init == error_mark_node)
25886 return error_mark_node;
25887
25888 if (init && type_dependent_expression_p (init)
25889 && context != adc_unify)
25890 /* Defining a subset of type-dependent expressions that we can deduce
25891 from ahead of time isn't worth the trouble. */
25892 return type;
25893
25894 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25895 /* C++17 class template argument deduction. */
25896 return do_class_deduction (type, tmpl, init, flags, complain);
25897
25898 if (TREE_TYPE (init) == NULL_TREE)
25899 /* Nothing we can do with this, even in deduction context. */
25900 return type;
25901
25902 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25903 with either a new invented type template parameter U or, if the
25904 initializer is a braced-init-list (8.5.4), with
25905 std::initializer_list<U>. */
25906 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25907 {
25908 if (!DIRECT_LIST_INIT_P (init))
25909 type = listify_autos (type, auto_node);
25910 else if (CONSTRUCTOR_NELTS (init) == 1)
25911 init = CONSTRUCTOR_ELT (init, 0)->value;
25912 else
25913 {
25914 if (complain & tf_warning_or_error)
25915 {
25916 if (permerror (input_location, "direct-list-initialization of "
25917 "%<auto%> requires exactly one element"))
25918 inform (input_location,
25919 "for deduction to %<std::initializer_list%>, use copy-"
25920 "list-initialization (i.e. add %<=%> before the %<{%>)");
25921 }
25922 type = listify_autos (type, auto_node);
25923 }
25924 }
25925
25926 if (type == error_mark_node)
25927 return error_mark_node;
25928
25929 init = resolve_nondeduced_context (init, complain);
25930
25931 if (context == adc_decomp_type
25932 && auto_node == type
25933 && init != error_mark_node
25934 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25935 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25936 and initializer has array type, deduce cv-qualified array type. */
25937 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25938 complain);
25939 else if (AUTO_IS_DECLTYPE (auto_node))
25940 {
25941 bool id = (DECL_P (init)
25942 || ((TREE_CODE (init) == COMPONENT_REF
25943 || TREE_CODE (init) == SCOPE_REF)
25944 && !REF_PARENTHESIZED_P (init)));
25945 targs = make_tree_vec (1);
25946 TREE_VEC_ELT (targs, 0)
25947 = finish_decltype_type (init, id, tf_warning_or_error);
25948 if (type != auto_node)
25949 {
25950 if (complain & tf_error)
25951 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25952 return error_mark_node;
25953 }
25954 }
25955 else
25956 {
25957 tree parms = build_tree_list (NULL_TREE, type);
25958 tree tparms;
25959
25960 if (flag_concepts)
25961 tparms = extract_autos (type);
25962 else
25963 {
25964 tparms = make_tree_vec (1);
25965 TREE_VEC_ELT (tparms, 0)
25966 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25967 }
25968
25969 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25970 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25971 DEDUCE_CALL, LOOKUP_NORMAL,
25972 NULL, /*explain_p=*/false);
25973 if (val > 0)
25974 {
25975 if (processing_template_decl)
25976 /* Try again at instantiation time. */
25977 return type;
25978 if (type && type != error_mark_node
25979 && (complain & tf_error))
25980 /* If type is error_mark_node a diagnostic must have been
25981 emitted by now. Also, having a mention to '<type error>'
25982 in the diagnostic is not really useful to the user. */
25983 {
25984 if (cfun && auto_node == current_function_auto_return_pattern
25985 && LAMBDA_FUNCTION_P (current_function_decl))
25986 error ("unable to deduce lambda return type from %qE", init);
25987 else
25988 error ("unable to deduce %qT from %qE", type, init);
25989 type_unification_real (tparms, targs, parms, &init, 1, 0,
25990 DEDUCE_CALL, LOOKUP_NORMAL,
25991 NULL, /*explain_p=*/true);
25992 }
25993 return error_mark_node;
25994 }
25995 }
25996
25997 /* Check any placeholder constraints against the deduced type. */
25998 if (flag_concepts && !processing_template_decl)
25999 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26000 {
26001 /* Use the deduced type to check the associated constraints. If we
26002 have a partial-concept-id, rebuild the argument list so that
26003 we check using the extra arguments. */
26004 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26005 tree cargs = CHECK_CONSTR_ARGS (constr);
26006 if (TREE_VEC_LENGTH (cargs) > 1)
26007 {
26008 cargs = copy_node (cargs);
26009 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26010 }
26011 else
26012 cargs = targs;
26013 if (!constraints_satisfied_p (constr, cargs))
26014 {
26015 if (complain & tf_warning_or_error)
26016 {
26017 switch (context)
26018 {
26019 case adc_unspecified:
26020 case adc_unify:
26021 error("placeholder constraints not satisfied");
26022 break;
26023 case adc_variable_type:
26024 case adc_decomp_type:
26025 error ("deduced initializer does not satisfy "
26026 "placeholder constraints");
26027 break;
26028 case adc_return_type:
26029 error ("deduced return type does not satisfy "
26030 "placeholder constraints");
26031 break;
26032 case adc_requirement:
26033 error ("deduced expression type does not satisfy "
26034 "placeholder constraints");
26035 break;
26036 }
26037 diagnose_constraints (input_location, constr, targs);
26038 }
26039 return error_mark_node;
26040 }
26041 }
26042
26043 if (processing_template_decl && context != adc_unify)
26044 outer_targs = current_template_args ();
26045 targs = add_to_template_args (outer_targs, targs);
26046 return tsubst (type, targs, complain, NULL_TREE);
26047 }
26048
26049 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26050 result. */
26051
26052 tree
26053 splice_late_return_type (tree type, tree late_return_type)
26054 {
26055 if (is_auto (type))
26056 {
26057 if (late_return_type)
26058 return late_return_type;
26059
26060 tree idx = get_template_parm_index (type);
26061 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26062 /* In an abbreviated function template we didn't know we were dealing
26063 with a function template when we saw the auto return type, so update
26064 it to have the correct level. */
26065 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26066 }
26067 return type;
26068 }
26069
26070 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26071 'decltype(auto)' or a deduced class template. */
26072
26073 bool
26074 is_auto (const_tree type)
26075 {
26076 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26077 && (TYPE_IDENTIFIER (type) == auto_identifier
26078 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26079 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26080 return true;
26081 else
26082 return false;
26083 }
26084
26085 /* for_each_template_parm callback for type_uses_auto. */
26086
26087 int
26088 is_auto_r (tree tp, void */*data*/)
26089 {
26090 return is_auto (tp);
26091 }
26092
26093 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26094 a use of `auto'. Returns NULL_TREE otherwise. */
26095
26096 tree
26097 type_uses_auto (tree type)
26098 {
26099 if (type == NULL_TREE)
26100 return NULL_TREE;
26101 else if (flag_concepts)
26102 {
26103 /* The Concepts TS allows multiple autos in one type-specifier; just
26104 return the first one we find, do_auto_deduction will collect all of
26105 them. */
26106 if (uses_template_parms (type))
26107 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26108 /*visited*/NULL, /*nondeduced*/true);
26109 else
26110 return NULL_TREE;
26111 }
26112 else
26113 return find_type_usage (type, is_auto);
26114 }
26115
26116 /* For a given template T, return the vector of typedefs referenced
26117 in T for which access check is needed at T instantiation time.
26118 T is either a FUNCTION_DECL or a RECORD_TYPE.
26119 Those typedefs were added to T by the function
26120 append_type_to_template_for_access_check. */
26121
26122 vec<qualified_typedef_usage_t, va_gc> *
26123 get_types_needing_access_check (tree t)
26124 {
26125 tree ti;
26126 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26127
26128 if (!t || t == error_mark_node)
26129 return NULL;
26130
26131 if (!(ti = get_template_info (t)))
26132 return NULL;
26133
26134 if (CLASS_TYPE_P (t)
26135 || TREE_CODE (t) == FUNCTION_DECL)
26136 {
26137 if (!TI_TEMPLATE (ti))
26138 return NULL;
26139
26140 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26141 }
26142
26143 return result;
26144 }
26145
26146 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26147 tied to T. That list of typedefs will be access checked at
26148 T instantiation time.
26149 T is either a FUNCTION_DECL or a RECORD_TYPE.
26150 TYPE_DECL is a TYPE_DECL node representing a typedef.
26151 SCOPE is the scope through which TYPE_DECL is accessed.
26152 LOCATION is the location of the usage point of TYPE_DECL.
26153
26154 This function is a subroutine of
26155 append_type_to_template_for_access_check. */
26156
26157 static void
26158 append_type_to_template_for_access_check_1 (tree t,
26159 tree type_decl,
26160 tree scope,
26161 location_t location)
26162 {
26163 qualified_typedef_usage_t typedef_usage;
26164 tree ti;
26165
26166 if (!t || t == error_mark_node)
26167 return;
26168
26169 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26170 || CLASS_TYPE_P (t))
26171 && type_decl
26172 && TREE_CODE (type_decl) == TYPE_DECL
26173 && scope);
26174
26175 if (!(ti = get_template_info (t)))
26176 return;
26177
26178 gcc_assert (TI_TEMPLATE (ti));
26179
26180 typedef_usage.typedef_decl = type_decl;
26181 typedef_usage.context = scope;
26182 typedef_usage.locus = location;
26183
26184 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26185 }
26186
26187 /* Append TYPE_DECL to the template TEMPL.
26188 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26189 At TEMPL instanciation time, TYPE_DECL will be checked to see
26190 if it can be accessed through SCOPE.
26191 LOCATION is the location of the usage point of TYPE_DECL.
26192
26193 e.g. consider the following code snippet:
26194
26195 class C
26196 {
26197 typedef int myint;
26198 };
26199
26200 template<class U> struct S
26201 {
26202 C::myint mi; // <-- usage point of the typedef C::myint
26203 };
26204
26205 S<char> s;
26206
26207 At S<char> instantiation time, we need to check the access of C::myint
26208 In other words, we need to check the access of the myint typedef through
26209 the C scope. For that purpose, this function will add the myint typedef
26210 and the scope C through which its being accessed to a list of typedefs
26211 tied to the template S. That list will be walked at template instantiation
26212 time and access check performed on each typedefs it contains.
26213 Note that this particular code snippet should yield an error because
26214 myint is private to C. */
26215
26216 void
26217 append_type_to_template_for_access_check (tree templ,
26218 tree type_decl,
26219 tree scope,
26220 location_t location)
26221 {
26222 qualified_typedef_usage_t *iter;
26223 unsigned i;
26224
26225 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26226
26227 /* Make sure we don't append the type to the template twice. */
26228 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26229 if (iter->typedef_decl == type_decl && scope == iter->context)
26230 return;
26231
26232 append_type_to_template_for_access_check_1 (templ, type_decl,
26233 scope, location);
26234 }
26235
26236 /* Convert the generic type parameters in PARM that match the types given in the
26237 range [START_IDX, END_IDX) from the current_template_parms into generic type
26238 packs. */
26239
26240 tree
26241 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26242 {
26243 tree current = current_template_parms;
26244 int depth = TMPL_PARMS_DEPTH (current);
26245 current = INNERMOST_TEMPLATE_PARMS (current);
26246 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26247
26248 for (int i = 0; i < start_idx; ++i)
26249 TREE_VEC_ELT (replacement, i)
26250 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26251
26252 for (int i = start_idx; i < end_idx; ++i)
26253 {
26254 /* Create a distinct parameter pack type from the current parm and add it
26255 to the replacement args to tsubst below into the generic function
26256 parameter. */
26257
26258 tree o = TREE_TYPE (TREE_VALUE
26259 (TREE_VEC_ELT (current, i)));
26260 tree t = copy_type (o);
26261 TEMPLATE_TYPE_PARM_INDEX (t)
26262 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26263 o, 0, 0, tf_none);
26264 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26265 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26266 TYPE_MAIN_VARIANT (t) = t;
26267 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26268 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26269 TREE_VEC_ELT (replacement, i) = t;
26270 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26271 }
26272
26273 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26274 TREE_VEC_ELT (replacement, i)
26275 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26276
26277 /* If there are more levels then build up the replacement with the outer
26278 template parms. */
26279 if (depth > 1)
26280 replacement = add_to_template_args (template_parms_to_args
26281 (TREE_CHAIN (current_template_parms)),
26282 replacement);
26283
26284 return tsubst (parm, replacement, tf_none, NULL_TREE);
26285 }
26286
26287 /* Entries in the decl_constraint hash table. */
26288 struct GTY((for_user)) constr_entry
26289 {
26290 tree decl;
26291 tree ci;
26292 };
26293
26294 /* Hashing function and equality for constraint entries. */
26295 struct constr_hasher : ggc_ptr_hash<constr_entry>
26296 {
26297 static hashval_t hash (constr_entry *e)
26298 {
26299 return (hashval_t)DECL_UID (e->decl);
26300 }
26301
26302 static bool equal (constr_entry *e1, constr_entry *e2)
26303 {
26304 return e1->decl == e2->decl;
26305 }
26306 };
26307
26308 /* A mapping from declarations to constraint information. Note that
26309 both templates and their underlying declarations are mapped to the
26310 same constraint information.
26311
26312 FIXME: This is defined in pt.c because garbage collection
26313 code is not being generated for constraint.cc. */
26314
26315 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26316
26317 /* Returns the template constraints of declaration T. If T is not
26318 constrained, return NULL_TREE. Note that T must be non-null. */
26319
26320 tree
26321 get_constraints (tree t)
26322 {
26323 if (!flag_concepts)
26324 return NULL_TREE;
26325
26326 gcc_assert (DECL_P (t));
26327 if (TREE_CODE (t) == TEMPLATE_DECL)
26328 t = DECL_TEMPLATE_RESULT (t);
26329 constr_entry elt = { t, NULL_TREE };
26330 constr_entry* found = decl_constraints->find (&elt);
26331 if (found)
26332 return found->ci;
26333 else
26334 return NULL_TREE;
26335 }
26336
26337 /* Associate the given constraint information CI with the declaration
26338 T. If T is a template, then the constraints are associated with
26339 its underlying declaration. Don't build associations if CI is
26340 NULL_TREE. */
26341
26342 void
26343 set_constraints (tree t, tree ci)
26344 {
26345 if (!ci)
26346 return;
26347 gcc_assert (t && flag_concepts);
26348 if (TREE_CODE (t) == TEMPLATE_DECL)
26349 t = DECL_TEMPLATE_RESULT (t);
26350 gcc_assert (!get_constraints (t));
26351 constr_entry elt = {t, ci};
26352 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26353 constr_entry* entry = ggc_alloc<constr_entry> ();
26354 *entry = elt;
26355 *slot = entry;
26356 }
26357
26358 /* Remove the associated constraints of the declaration T. */
26359
26360 void
26361 remove_constraints (tree t)
26362 {
26363 gcc_assert (DECL_P (t));
26364 if (TREE_CODE (t) == TEMPLATE_DECL)
26365 t = DECL_TEMPLATE_RESULT (t);
26366
26367 constr_entry elt = {t, NULL_TREE};
26368 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26369 if (slot)
26370 decl_constraints->clear_slot (slot);
26371 }
26372
26373 /* Memoized satisfaction results for declarations. This
26374 maps the pair (constraint_info, arguments) to the result computed
26375 by constraints_satisfied_p. */
26376
26377 struct GTY((for_user)) constraint_sat_entry
26378 {
26379 tree ci;
26380 tree args;
26381 tree result;
26382 };
26383
26384 /* Hashing function and equality for constraint entries. */
26385
26386 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26387 {
26388 static hashval_t hash (constraint_sat_entry *e)
26389 {
26390 hashval_t val = iterative_hash_object(e->ci, 0);
26391 return iterative_hash_template_arg (e->args, val);
26392 }
26393
26394 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26395 {
26396 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26397 }
26398 };
26399
26400 /* Memoized satisfaction results for concept checks. */
26401
26402 struct GTY((for_user)) concept_spec_entry
26403 {
26404 tree tmpl;
26405 tree args;
26406 tree result;
26407 };
26408
26409 /* Hashing function and equality for constraint entries. */
26410
26411 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26412 {
26413 static hashval_t hash (concept_spec_entry *e)
26414 {
26415 return hash_tmpl_and_args (e->tmpl, e->args);
26416 }
26417
26418 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26419 {
26420 ++comparing_specializations;
26421 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26422 --comparing_specializations;
26423 return eq;
26424 }
26425 };
26426
26427 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26428 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26429
26430 /* Search for a memoized satisfaction result. Returns one of the
26431 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26432
26433 tree
26434 lookup_constraint_satisfaction (tree ci, tree args)
26435 {
26436 constraint_sat_entry elt = { ci, args, NULL_TREE };
26437 constraint_sat_entry* found = constraint_memos->find (&elt);
26438 if (found)
26439 return found->result;
26440 else
26441 return NULL_TREE;
26442 }
26443
26444 /* Memoize the result of a satisfication test. Returns the saved result. */
26445
26446 tree
26447 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26448 {
26449 constraint_sat_entry elt = {ci, args, result};
26450 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26451 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26452 *entry = elt;
26453 *slot = entry;
26454 return result;
26455 }
26456
26457 /* Search for a memoized satisfaction result for a concept. */
26458
26459 tree
26460 lookup_concept_satisfaction (tree tmpl, tree args)
26461 {
26462 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26463 concept_spec_entry* found = concept_memos->find (&elt);
26464 if (found)
26465 return found->result;
26466 else
26467 return NULL_TREE;
26468 }
26469
26470 /* Memoize the result of a concept check. Returns the saved result. */
26471
26472 tree
26473 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26474 {
26475 concept_spec_entry elt = {tmpl, args, result};
26476 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26477 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26478 *entry = elt;
26479 *slot = entry;
26480 return result;
26481 }
26482
26483 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26484
26485 /* Returns a prior concept specialization. This returns the substituted
26486 and normalized constraints defined by the concept. */
26487
26488 tree
26489 get_concept_expansion (tree tmpl, tree args)
26490 {
26491 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26492 concept_spec_entry* found = concept_expansions->find (&elt);
26493 if (found)
26494 return found->result;
26495 else
26496 return NULL_TREE;
26497 }
26498
26499 /* Save a concept expansion for later. */
26500
26501 tree
26502 save_concept_expansion (tree tmpl, tree args, tree def)
26503 {
26504 concept_spec_entry elt = {tmpl, args, def};
26505 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26506 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26507 *entry = elt;
26508 *slot = entry;
26509 return def;
26510 }
26511
26512 static hashval_t
26513 hash_subsumption_args (tree t1, tree t2)
26514 {
26515 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26516 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26517 int val = 0;
26518 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26519 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26520 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26521 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26522 return val;
26523 }
26524
26525 /* Compare the constraints of two subsumption entries. The LEFT1 and
26526 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26527 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26528
26529 static bool
26530 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26531 {
26532 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26533 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26534 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26535 CHECK_CONSTR_ARGS (right1)))
26536 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26537 CHECK_CONSTR_ARGS (right2));
26538 return false;
26539 }
26540
26541 /* Key/value pair for learning and memoizing subsumption results. This
26542 associates a pair of check constraints (including arguments) with
26543 a boolean value indicating the result. */
26544
26545 struct GTY((for_user)) subsumption_entry
26546 {
26547 tree t1;
26548 tree t2;
26549 bool result;
26550 };
26551
26552 /* Hashing function and equality for constraint entries. */
26553
26554 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26555 {
26556 static hashval_t hash (subsumption_entry *e)
26557 {
26558 return hash_subsumption_args (e->t1, e->t2);
26559 }
26560
26561 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26562 {
26563 ++comparing_specializations;
26564 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26565 --comparing_specializations;
26566 return eq;
26567 }
26568 };
26569
26570 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26571
26572 /* Search for a previously cached subsumption result. */
26573
26574 bool*
26575 lookup_subsumption_result (tree t1, tree t2)
26576 {
26577 subsumption_entry elt = { t1, t2, false };
26578 subsumption_entry* found = subsumption_table->find (&elt);
26579 if (found)
26580 return &found->result;
26581 else
26582 return 0;
26583 }
26584
26585 /* Save a subsumption result. */
26586
26587 bool
26588 save_subsumption_result (tree t1, tree t2, bool result)
26589 {
26590 subsumption_entry elt = {t1, t2, result};
26591 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26592 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26593 *entry = elt;
26594 *slot = entry;
26595 return result;
26596 }
26597
26598 /* Set up the hash table for constraint association. */
26599
26600 void
26601 init_constraint_processing (void)
26602 {
26603 if (!flag_concepts)
26604 return;
26605
26606 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26607 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26608 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26609 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26610 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26611 }
26612
26613 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26614 0..N-1. */
26615
26616 void
26617 declare_integer_pack (void)
26618 {
26619 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26620 build_function_type_list (integer_type_node,
26621 integer_type_node,
26622 NULL_TREE),
26623 NULL_TREE, ECF_CONST);
26624 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26625 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26626 }
26627
26628 /* Set up the hash tables for template instantiations. */
26629
26630 void
26631 init_template_processing (void)
26632 {
26633 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26634 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26635
26636 if (cxx_dialect >= cxx11)
26637 declare_integer_pack ();
26638 }
26639
26640 /* Print stats about the template hash tables for -fstats. */
26641
26642 void
26643 print_template_statistics (void)
26644 {
26645 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26646 "%f collisions\n", (long) decl_specializations->size (),
26647 (long) decl_specializations->elements (),
26648 decl_specializations->collisions ());
26649 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26650 "%f collisions\n", (long) type_specializations->size (),
26651 (long) type_specializations->elements (),
26652 type_specializations->collisions ());
26653 }
26654
26655 #if CHECKING_P
26656
26657 namespace selftest {
26658
26659 /* Verify that build_non_dependent_expr () works, for various expressions,
26660 and that location wrappers don't affect the results. */
26661
26662 static void
26663 test_build_non_dependent_expr ()
26664 {
26665 location_t loc = BUILTINS_LOCATION;
26666
26667 /* Verify constants, without and with location wrappers. */
26668 tree int_cst = build_int_cst (integer_type_node, 42);
26669 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26670
26671 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26672 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26673 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26674
26675 tree string_lit = build_string (4, "foo");
26676 TREE_TYPE (string_lit) = char_array_type_node;
26677 string_lit = fix_string_type (string_lit);
26678 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26679
26680 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26681 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26682 ASSERT_EQ (wrapped_string_lit,
26683 build_non_dependent_expr (wrapped_string_lit));
26684 }
26685
26686 /* Verify that type_dependent_expression_p () works correctly, even
26687 in the presence of location wrapper nodes. */
26688
26689 static void
26690 test_type_dependent_expression_p ()
26691 {
26692 location_t loc = BUILTINS_LOCATION;
26693
26694 tree name = get_identifier ("foo");
26695
26696 /* If no templates are involved, nothing is type-dependent. */
26697 gcc_assert (!processing_template_decl);
26698 ASSERT_FALSE (type_dependent_expression_p (name));
26699
26700 ++processing_template_decl;
26701
26702 /* Within a template, an unresolved name is always type-dependent. */
26703 ASSERT_TRUE (type_dependent_expression_p (name));
26704
26705 /* Ensure it copes with NULL_TREE and errors. */
26706 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26707 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26708
26709 /* A USING_DECL in a template should be type-dependent, even if wrapped
26710 with a location wrapper (PR c++/83799). */
26711 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26712 TREE_TYPE (using_decl) = integer_type_node;
26713 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26714 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26715 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26716 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26717
26718 --processing_template_decl;
26719 }
26720
26721 /* Run all of the selftests within this file. */
26722
26723 void
26724 cp_pt_c_tests ()
26725 {
26726 test_build_non_dependent_expr ();
26727 test_type_dependent_expression_p ();
26728 }
26729
26730 } // namespace selftest
26731
26732 #endif /* #if CHECKING_P */
26733
26734 #include "gt-cp-pt.h"