Preserving locations for variable-uses and constants (PR c++/43486)
[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 tree decl, expr;
16157 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16158 decl = RANGE_FOR_DECL (t);
16159 decl = tsubst (decl, args, complain, in_decl);
16160 maybe_push_decl (decl);
16161 expr = RECUR (RANGE_FOR_EXPR (t));
16162 const unsigned short unroll
16163 = RANGE_FOR_UNROLL (t) ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0;
16164 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16165 {
16166 unsigned int cnt;
16167 tree first;
16168 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16169 complain, in_decl, &first, &cnt);
16170 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16171 RANGE_FOR_IVDEP (t), unroll);
16172 }
16173 else
16174 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16175 RANGE_FOR_IVDEP (t), unroll);
16176 bool prev = note_iteration_stmt_body_start ();
16177 RECUR (RANGE_FOR_BODY (t));
16178 note_iteration_stmt_body_end (prev);
16179 finish_for_stmt (stmt);
16180 }
16181 break;
16182
16183 case WHILE_STMT:
16184 stmt = begin_while_stmt ();
16185 tmp = RECUR (WHILE_COND (t));
16186 finish_while_stmt_cond (tmp, stmt, false, 0);
16187 {
16188 bool prev = note_iteration_stmt_body_start ();
16189 RECUR (WHILE_BODY (t));
16190 note_iteration_stmt_body_end (prev);
16191 }
16192 finish_while_stmt (stmt);
16193 break;
16194
16195 case DO_STMT:
16196 stmt = begin_do_stmt ();
16197 {
16198 bool prev = note_iteration_stmt_body_start ();
16199 RECUR (DO_BODY (t));
16200 note_iteration_stmt_body_end (prev);
16201 }
16202 finish_do_body (stmt);
16203 tmp = RECUR (DO_COND (t));
16204 finish_do_stmt (tmp, stmt, false, 0);
16205 break;
16206
16207 case IF_STMT:
16208 stmt = begin_if_stmt ();
16209 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16210 tmp = RECUR (IF_COND (t));
16211 tmp = finish_if_stmt_cond (tmp, stmt);
16212 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16213 /* Don't instantiate the THEN_CLAUSE. */;
16214 else
16215 {
16216 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16217 if (inhibit)
16218 ++c_inhibit_evaluation_warnings;
16219 RECUR (THEN_CLAUSE (t));
16220 if (inhibit)
16221 --c_inhibit_evaluation_warnings;
16222 }
16223 finish_then_clause (stmt);
16224
16225 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16226 /* Don't instantiate the ELSE_CLAUSE. */;
16227 else if (ELSE_CLAUSE (t))
16228 {
16229 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16230 begin_else_clause (stmt);
16231 if (inhibit)
16232 ++c_inhibit_evaluation_warnings;
16233 RECUR (ELSE_CLAUSE (t));
16234 if (inhibit)
16235 --c_inhibit_evaluation_warnings;
16236 finish_else_clause (stmt);
16237 }
16238
16239 finish_if_stmt (stmt);
16240 break;
16241
16242 case BIND_EXPR:
16243 if (BIND_EXPR_BODY_BLOCK (t))
16244 stmt = begin_function_body ();
16245 else
16246 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16247 ? BCS_TRY_BLOCK : 0);
16248
16249 RECUR (BIND_EXPR_BODY (t));
16250
16251 if (BIND_EXPR_BODY_BLOCK (t))
16252 finish_function_body (stmt);
16253 else
16254 finish_compound_stmt (stmt);
16255 break;
16256
16257 case BREAK_STMT:
16258 finish_break_stmt ();
16259 break;
16260
16261 case CONTINUE_STMT:
16262 finish_continue_stmt ();
16263 break;
16264
16265 case SWITCH_STMT:
16266 stmt = begin_switch_stmt ();
16267 tmp = RECUR (SWITCH_STMT_COND (t));
16268 finish_switch_cond (tmp, stmt);
16269 RECUR (SWITCH_STMT_BODY (t));
16270 finish_switch_stmt (stmt);
16271 break;
16272
16273 case CASE_LABEL_EXPR:
16274 {
16275 tree low = RECUR (CASE_LOW (t));
16276 tree high = RECUR (CASE_HIGH (t));
16277 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16278 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16279 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16280 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16281 }
16282 break;
16283
16284 case LABEL_EXPR:
16285 {
16286 tree decl = LABEL_EXPR_LABEL (t);
16287 tree label;
16288
16289 label = finish_label_stmt (DECL_NAME (decl));
16290 if (TREE_CODE (label) == LABEL_DECL)
16291 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16292 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16293 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16294 }
16295 break;
16296
16297 case GOTO_EXPR:
16298 tmp = GOTO_DESTINATION (t);
16299 if (TREE_CODE (tmp) != LABEL_DECL)
16300 /* Computed goto's must be tsubst'd into. On the other hand,
16301 non-computed gotos must not be; the identifier in question
16302 will have no binding. */
16303 tmp = RECUR (tmp);
16304 else
16305 tmp = DECL_NAME (tmp);
16306 finish_goto_stmt (tmp);
16307 break;
16308
16309 case ASM_EXPR:
16310 {
16311 tree string = RECUR (ASM_STRING (t));
16312 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16313 complain, in_decl);
16314 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16315 complain, in_decl);
16316 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16317 complain, in_decl);
16318 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16319 complain, in_decl);
16320 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16321 clobbers, labels);
16322 tree asm_expr = tmp;
16323 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16324 asm_expr = TREE_OPERAND (asm_expr, 0);
16325 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16326 }
16327 break;
16328
16329 case TRY_BLOCK:
16330 if (CLEANUP_P (t))
16331 {
16332 stmt = begin_try_block ();
16333 RECUR (TRY_STMTS (t));
16334 finish_cleanup_try_block (stmt);
16335 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16336 }
16337 else
16338 {
16339 tree compound_stmt = NULL_TREE;
16340
16341 if (FN_TRY_BLOCK_P (t))
16342 stmt = begin_function_try_block (&compound_stmt);
16343 else
16344 stmt = begin_try_block ();
16345
16346 RECUR (TRY_STMTS (t));
16347
16348 if (FN_TRY_BLOCK_P (t))
16349 finish_function_try_block (stmt);
16350 else
16351 finish_try_block (stmt);
16352
16353 RECUR (TRY_HANDLERS (t));
16354 if (FN_TRY_BLOCK_P (t))
16355 finish_function_handler_sequence (stmt, compound_stmt);
16356 else
16357 finish_handler_sequence (stmt);
16358 }
16359 break;
16360
16361 case HANDLER:
16362 {
16363 tree decl = HANDLER_PARMS (t);
16364
16365 if (decl)
16366 {
16367 decl = tsubst (decl, args, complain, in_decl);
16368 /* Prevent instantiate_decl from trying to instantiate
16369 this variable. We've already done all that needs to be
16370 done. */
16371 if (decl != error_mark_node)
16372 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16373 }
16374 stmt = begin_handler ();
16375 finish_handler_parms (decl, stmt);
16376 RECUR (HANDLER_BODY (t));
16377 finish_handler (stmt);
16378 }
16379 break;
16380
16381 case TAG_DEFN:
16382 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16383 if (CLASS_TYPE_P (tmp))
16384 {
16385 /* Local classes are not independent templates; they are
16386 instantiated along with their containing function. And this
16387 way we don't have to deal with pushing out of one local class
16388 to instantiate a member of another local class. */
16389 /* Closures are handled by the LAMBDA_EXPR. */
16390 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16391 complete_type (tmp);
16392 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16393 if ((VAR_P (fld)
16394 || (TREE_CODE (fld) == FUNCTION_DECL
16395 && !DECL_ARTIFICIAL (fld)))
16396 && DECL_TEMPLATE_INSTANTIATION (fld))
16397 instantiate_decl (fld, /*defer_ok=*/false,
16398 /*expl_inst_class=*/false);
16399 }
16400 break;
16401
16402 case STATIC_ASSERT:
16403 {
16404 tree condition;
16405
16406 ++c_inhibit_evaluation_warnings;
16407 condition =
16408 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16409 args,
16410 complain, in_decl,
16411 /*integral_constant_expression_p=*/true);
16412 --c_inhibit_evaluation_warnings;
16413
16414 finish_static_assert (condition,
16415 STATIC_ASSERT_MESSAGE (t),
16416 STATIC_ASSERT_SOURCE_LOCATION (t),
16417 /*member_p=*/false);
16418 }
16419 break;
16420
16421 case OACC_KERNELS:
16422 case OACC_PARALLEL:
16423 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16424 in_decl);
16425 stmt = begin_omp_parallel ();
16426 RECUR (OMP_BODY (t));
16427 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16428 break;
16429
16430 case OMP_PARALLEL:
16431 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16432 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16433 complain, in_decl);
16434 if (OMP_PARALLEL_COMBINED (t))
16435 omp_parallel_combined_clauses = &tmp;
16436 stmt = begin_omp_parallel ();
16437 RECUR (OMP_PARALLEL_BODY (t));
16438 gcc_assert (omp_parallel_combined_clauses == NULL);
16439 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16440 = OMP_PARALLEL_COMBINED (t);
16441 pop_omp_privatization_clauses (r);
16442 break;
16443
16444 case OMP_TASK:
16445 r = push_omp_privatization_clauses (false);
16446 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16447 complain, in_decl);
16448 stmt = begin_omp_task ();
16449 RECUR (OMP_TASK_BODY (t));
16450 finish_omp_task (tmp, stmt);
16451 pop_omp_privatization_clauses (r);
16452 break;
16453
16454 case OMP_FOR:
16455 case OMP_SIMD:
16456 case OMP_DISTRIBUTE:
16457 case OMP_TASKLOOP:
16458 case OACC_LOOP:
16459 {
16460 tree clauses, body, pre_body;
16461 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16462 tree orig_declv = NULL_TREE;
16463 tree incrv = NULL_TREE;
16464 enum c_omp_region_type ort = C_ORT_OMP;
16465 int i;
16466
16467 if (TREE_CODE (t) == OACC_LOOP)
16468 ort = C_ORT_ACC;
16469
16470 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16471 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16472 in_decl);
16473 if (OMP_FOR_INIT (t) != NULL_TREE)
16474 {
16475 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16476 if (OMP_FOR_ORIG_DECLS (t))
16477 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16478 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16479 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16480 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16481 }
16482
16483 stmt = begin_omp_structured_block ();
16484
16485 pre_body = push_stmt_list ();
16486 RECUR (OMP_FOR_PRE_BODY (t));
16487 pre_body = pop_stmt_list (pre_body);
16488
16489 if (OMP_FOR_INIT (t) != NULL_TREE)
16490 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16491 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16492 incrv, &clauses, args, complain, in_decl,
16493 integral_constant_expression_p);
16494 omp_parallel_combined_clauses = NULL;
16495
16496 body = push_stmt_list ();
16497 RECUR (OMP_FOR_BODY (t));
16498 body = pop_stmt_list (body);
16499
16500 if (OMP_FOR_INIT (t) != NULL_TREE)
16501 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16502 orig_declv, initv, condv, incrv, body, pre_body,
16503 NULL, clauses);
16504 else
16505 {
16506 t = make_node (TREE_CODE (t));
16507 TREE_TYPE (t) = void_type_node;
16508 OMP_FOR_BODY (t) = body;
16509 OMP_FOR_PRE_BODY (t) = pre_body;
16510 OMP_FOR_CLAUSES (t) = clauses;
16511 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16512 add_stmt (t);
16513 }
16514
16515 add_stmt (finish_omp_structured_block (stmt));
16516 pop_omp_privatization_clauses (r);
16517 }
16518 break;
16519
16520 case OMP_SECTIONS:
16521 omp_parallel_combined_clauses = NULL;
16522 /* FALLTHRU */
16523 case OMP_SINGLE:
16524 case OMP_TEAMS:
16525 case OMP_CRITICAL:
16526 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16527 && OMP_TEAMS_COMBINED (t));
16528 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16529 in_decl);
16530 stmt = push_stmt_list ();
16531 RECUR (OMP_BODY (t));
16532 stmt = pop_stmt_list (stmt);
16533
16534 t = copy_node (t);
16535 OMP_BODY (t) = stmt;
16536 OMP_CLAUSES (t) = tmp;
16537 add_stmt (t);
16538 pop_omp_privatization_clauses (r);
16539 break;
16540
16541 case OACC_DATA:
16542 case OMP_TARGET_DATA:
16543 case OMP_TARGET:
16544 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16545 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16546 in_decl);
16547 keep_next_level (true);
16548 stmt = begin_omp_structured_block ();
16549
16550 RECUR (OMP_BODY (t));
16551 stmt = finish_omp_structured_block (stmt);
16552
16553 t = copy_node (t);
16554 OMP_BODY (t) = stmt;
16555 OMP_CLAUSES (t) = tmp;
16556 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16557 {
16558 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16559 if (teams)
16560 {
16561 /* For combined target teams, ensure the num_teams and
16562 thread_limit clause expressions are evaluated on the host,
16563 before entering the target construct. */
16564 tree c;
16565 for (c = OMP_TEAMS_CLAUSES (teams);
16566 c; c = OMP_CLAUSE_CHAIN (c))
16567 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16568 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16569 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16570 {
16571 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16572 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16573 if (expr == error_mark_node)
16574 continue;
16575 tmp = TARGET_EXPR_SLOT (expr);
16576 add_stmt (expr);
16577 OMP_CLAUSE_OPERAND (c, 0) = expr;
16578 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16579 OMP_CLAUSE_FIRSTPRIVATE);
16580 OMP_CLAUSE_DECL (tc) = tmp;
16581 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16582 OMP_TARGET_CLAUSES (t) = tc;
16583 }
16584 }
16585 }
16586 add_stmt (t);
16587 break;
16588
16589 case OACC_DECLARE:
16590 t = copy_node (t);
16591 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16592 complain, in_decl);
16593 OACC_DECLARE_CLAUSES (t) = tmp;
16594 add_stmt (t);
16595 break;
16596
16597 case OMP_TARGET_UPDATE:
16598 case OMP_TARGET_ENTER_DATA:
16599 case OMP_TARGET_EXIT_DATA:
16600 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16601 complain, in_decl);
16602 t = copy_node (t);
16603 OMP_STANDALONE_CLAUSES (t) = tmp;
16604 add_stmt (t);
16605 break;
16606
16607 case OACC_ENTER_DATA:
16608 case OACC_EXIT_DATA:
16609 case OACC_UPDATE:
16610 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16611 complain, in_decl);
16612 t = copy_node (t);
16613 OMP_STANDALONE_CLAUSES (t) = tmp;
16614 add_stmt (t);
16615 break;
16616
16617 case OMP_ORDERED:
16618 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16619 complain, in_decl);
16620 stmt = push_stmt_list ();
16621 RECUR (OMP_BODY (t));
16622 stmt = pop_stmt_list (stmt);
16623
16624 t = copy_node (t);
16625 OMP_BODY (t) = stmt;
16626 OMP_ORDERED_CLAUSES (t) = tmp;
16627 add_stmt (t);
16628 break;
16629
16630 case OMP_SECTION:
16631 case OMP_MASTER:
16632 case OMP_TASKGROUP:
16633 stmt = push_stmt_list ();
16634 RECUR (OMP_BODY (t));
16635 stmt = pop_stmt_list (stmt);
16636
16637 t = copy_node (t);
16638 OMP_BODY (t) = stmt;
16639 add_stmt (t);
16640 break;
16641
16642 case OMP_ATOMIC:
16643 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16644 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16645 {
16646 tree op1 = TREE_OPERAND (t, 1);
16647 tree rhs1 = NULL_TREE;
16648 tree lhs, rhs;
16649 if (TREE_CODE (op1) == COMPOUND_EXPR)
16650 {
16651 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16652 op1 = TREE_OPERAND (op1, 1);
16653 }
16654 lhs = RECUR (TREE_OPERAND (op1, 0));
16655 rhs = RECUR (TREE_OPERAND (op1, 1));
16656 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16657 NULL_TREE, NULL_TREE, rhs1,
16658 OMP_ATOMIC_SEQ_CST (t));
16659 }
16660 else
16661 {
16662 tree op1 = TREE_OPERAND (t, 1);
16663 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16664 tree rhs1 = NULL_TREE;
16665 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16666 enum tree_code opcode = NOP_EXPR;
16667 if (code == OMP_ATOMIC_READ)
16668 {
16669 v = RECUR (TREE_OPERAND (op1, 0));
16670 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16671 }
16672 else if (code == OMP_ATOMIC_CAPTURE_OLD
16673 || code == OMP_ATOMIC_CAPTURE_NEW)
16674 {
16675 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16676 v = RECUR (TREE_OPERAND (op1, 0));
16677 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16678 if (TREE_CODE (op11) == COMPOUND_EXPR)
16679 {
16680 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16681 op11 = TREE_OPERAND (op11, 1);
16682 }
16683 lhs = RECUR (TREE_OPERAND (op11, 0));
16684 rhs = RECUR (TREE_OPERAND (op11, 1));
16685 opcode = TREE_CODE (op11);
16686 if (opcode == MODIFY_EXPR)
16687 opcode = NOP_EXPR;
16688 }
16689 else
16690 {
16691 code = OMP_ATOMIC;
16692 lhs = RECUR (TREE_OPERAND (op1, 0));
16693 rhs = RECUR (TREE_OPERAND (op1, 1));
16694 }
16695 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16696 OMP_ATOMIC_SEQ_CST (t));
16697 }
16698 break;
16699
16700 case TRANSACTION_EXPR:
16701 {
16702 int flags = 0;
16703 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16704 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16705
16706 if (TRANSACTION_EXPR_IS_STMT (t))
16707 {
16708 tree body = TRANSACTION_EXPR_BODY (t);
16709 tree noex = NULL_TREE;
16710 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16711 {
16712 noex = MUST_NOT_THROW_COND (body);
16713 if (noex == NULL_TREE)
16714 noex = boolean_true_node;
16715 body = TREE_OPERAND (body, 0);
16716 }
16717 stmt = begin_transaction_stmt (input_location, NULL, flags);
16718 RECUR (body);
16719 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16720 }
16721 else
16722 {
16723 stmt = build_transaction_expr (EXPR_LOCATION (t),
16724 RECUR (TRANSACTION_EXPR_BODY (t)),
16725 flags, NULL_TREE);
16726 RETURN (stmt);
16727 }
16728 }
16729 break;
16730
16731 case MUST_NOT_THROW_EXPR:
16732 {
16733 tree op0 = RECUR (TREE_OPERAND (t, 0));
16734 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16735 RETURN (build_must_not_throw_expr (op0, cond));
16736 }
16737
16738 case EXPR_PACK_EXPANSION:
16739 error ("invalid use of pack expansion expression");
16740 RETURN (error_mark_node);
16741
16742 case NONTYPE_ARGUMENT_PACK:
16743 error ("use %<...%> to expand argument pack");
16744 RETURN (error_mark_node);
16745
16746 case COMPOUND_EXPR:
16747 tmp = RECUR (TREE_OPERAND (t, 0));
16748 if (tmp == NULL_TREE)
16749 /* If the first operand was a statement, we're done with it. */
16750 RETURN (RECUR (TREE_OPERAND (t, 1)));
16751 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16752 RECUR (TREE_OPERAND (t, 1)),
16753 complain));
16754
16755 case ANNOTATE_EXPR:
16756 tmp = RECUR (TREE_OPERAND (t, 0));
16757 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16758 TREE_TYPE (tmp), tmp,
16759 RECUR (TREE_OPERAND (t, 1)),
16760 RECUR (TREE_OPERAND (t, 2))));
16761
16762 default:
16763 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16764
16765 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16766 /*function_p=*/false,
16767 integral_constant_expression_p));
16768 }
16769
16770 RETURN (NULL_TREE);
16771 out:
16772 input_location = loc;
16773 return r;
16774 #undef RECUR
16775 #undef RETURN
16776 }
16777
16778 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16779 function. For description of the body see comment above
16780 cp_parser_omp_declare_reduction_exprs. */
16781
16782 static void
16783 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16784 {
16785 if (t == NULL_TREE || t == error_mark_node)
16786 return;
16787
16788 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16789
16790 tree_stmt_iterator tsi;
16791 int i;
16792 tree stmts[7];
16793 memset (stmts, 0, sizeof stmts);
16794 for (i = 0, tsi = tsi_start (t);
16795 i < 7 && !tsi_end_p (tsi);
16796 i++, tsi_next (&tsi))
16797 stmts[i] = tsi_stmt (tsi);
16798 gcc_assert (tsi_end_p (tsi));
16799
16800 if (i >= 3)
16801 {
16802 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16803 && TREE_CODE (stmts[1]) == DECL_EXPR);
16804 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16805 args, complain, in_decl);
16806 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16807 args, complain, in_decl);
16808 DECL_CONTEXT (omp_out) = current_function_decl;
16809 DECL_CONTEXT (omp_in) = current_function_decl;
16810 keep_next_level (true);
16811 tree block = begin_omp_structured_block ();
16812 tsubst_expr (stmts[2], args, complain, in_decl, false);
16813 block = finish_omp_structured_block (block);
16814 block = maybe_cleanup_point_expr_void (block);
16815 add_decl_expr (omp_out);
16816 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16817 TREE_NO_WARNING (omp_out) = 1;
16818 add_decl_expr (omp_in);
16819 finish_expr_stmt (block);
16820 }
16821 if (i >= 6)
16822 {
16823 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16824 && TREE_CODE (stmts[4]) == DECL_EXPR);
16825 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16826 args, complain, in_decl);
16827 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16828 args, complain, in_decl);
16829 DECL_CONTEXT (omp_priv) = current_function_decl;
16830 DECL_CONTEXT (omp_orig) = current_function_decl;
16831 keep_next_level (true);
16832 tree block = begin_omp_structured_block ();
16833 tsubst_expr (stmts[5], args, complain, in_decl, false);
16834 block = finish_omp_structured_block (block);
16835 block = maybe_cleanup_point_expr_void (block);
16836 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16837 add_decl_expr (omp_priv);
16838 add_decl_expr (omp_orig);
16839 finish_expr_stmt (block);
16840 if (i == 7)
16841 add_decl_expr (omp_orig);
16842 }
16843 }
16844
16845 /* T is a postfix-expression that is not being used in a function
16846 call. Return the substituted version of T. */
16847
16848 static tree
16849 tsubst_non_call_postfix_expression (tree t, tree args,
16850 tsubst_flags_t complain,
16851 tree in_decl)
16852 {
16853 if (TREE_CODE (t) == SCOPE_REF)
16854 t = tsubst_qualified_id (t, args, complain, in_decl,
16855 /*done=*/false, /*address_p=*/false);
16856 else
16857 t = tsubst_copy_and_build (t, args, complain, in_decl,
16858 /*function_p=*/false,
16859 /*integral_constant_expression_p=*/false);
16860
16861 return t;
16862 }
16863
16864 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16865 instantiation context. Instantiating a pack expansion containing a lambda
16866 might result in multiple lambdas all based on the same lambda in the
16867 template. */
16868
16869 tree
16870 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16871 {
16872 tree oldfn = lambda_function (t);
16873 in_decl = oldfn;
16874
16875 tree r = build_lambda_expr ();
16876
16877 LAMBDA_EXPR_LOCATION (r)
16878 = LAMBDA_EXPR_LOCATION (t);
16879 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16880 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16881 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16882
16883 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16884 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16885 else
16886 record_lambda_scope (r);
16887
16888 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16889 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16890
16891 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16892 cap = TREE_CHAIN (cap))
16893 {
16894 tree field = TREE_PURPOSE (cap);
16895 if (PACK_EXPANSION_P (field))
16896 field = PACK_EXPANSION_PATTERN (field);
16897 field = tsubst_decl (field, args, complain);
16898
16899 if (field == error_mark_node)
16900 return error_mark_node;
16901
16902 tree init = TREE_VALUE (cap);
16903 if (PACK_EXPANSION_P (init))
16904 init = tsubst_pack_expansion (init, args, complain, in_decl);
16905 else
16906 init = tsubst_copy_and_build (init, args, complain, in_decl,
16907 /*fn*/false, /*constexpr*/false);
16908
16909 if (TREE_CODE (field) == TREE_VEC)
16910 {
16911 int len = TREE_VEC_LENGTH (field);
16912 gcc_assert (TREE_CODE (init) == TREE_VEC
16913 && TREE_VEC_LENGTH (init) == len);
16914 for (int i = 0; i < len; ++i)
16915 LAMBDA_EXPR_CAPTURE_LIST (r)
16916 = tree_cons (TREE_VEC_ELT (field, i),
16917 TREE_VEC_ELT (init, i),
16918 LAMBDA_EXPR_CAPTURE_LIST (r));
16919 }
16920 else
16921 {
16922 LAMBDA_EXPR_CAPTURE_LIST (r)
16923 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16924
16925 if (id_equal (DECL_NAME (field), "__this"))
16926 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16927 }
16928 }
16929
16930 tree type = begin_lambda_type (r);
16931
16932 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16933 determine_visibility (TYPE_NAME (type));
16934
16935 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16936
16937 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16938 ? DECL_TI_TEMPLATE (oldfn)
16939 : NULL_TREE);
16940
16941 tree fntype = static_fn_type (oldfn);
16942 if (oldtmpl)
16943 ++processing_template_decl;
16944 fntype = tsubst (fntype, args, complain, in_decl);
16945 if (oldtmpl)
16946 --processing_template_decl;
16947
16948 if (fntype == error_mark_node)
16949 r = error_mark_node;
16950 else
16951 {
16952 /* Fix the type of 'this'. */
16953 fntype = build_memfn_type (fntype, type,
16954 type_memfn_quals (fntype),
16955 type_memfn_rqual (fntype));
16956 tree fn, tmpl;
16957 if (oldtmpl)
16958 {
16959 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16960 fn = DECL_TEMPLATE_RESULT (tmpl);
16961 finish_member_declaration (tmpl);
16962 }
16963 else
16964 {
16965 tmpl = NULL_TREE;
16966 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16967 finish_member_declaration (fn);
16968 }
16969
16970 /* Let finish_function set this. */
16971 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16972
16973 bool nested = cfun;
16974 if (nested)
16975 push_function_context ();
16976
16977 local_specialization_stack s (lss_copy);
16978
16979 tree body = start_lambda_function (fn, r);
16980
16981 register_parameter_specializations (oldfn, fn);
16982
16983 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16984 /*constexpr*/false);
16985
16986 finish_lambda_function (body);
16987
16988 if (nested)
16989 pop_function_context ();
16990
16991 /* The capture list was built up in reverse order; fix that now. */
16992 LAMBDA_EXPR_CAPTURE_LIST (r)
16993 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16994
16995 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16996
16997 maybe_add_lambda_conv_op (type);
16998 }
16999
17000 finish_struct (type, /*attr*/NULL_TREE);
17001
17002 insert_pending_capture_proxies ();
17003
17004 return r;
17005 }
17006
17007 /* Like tsubst but deals with expressions and performs semantic
17008 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17009
17010 tree
17011 tsubst_copy_and_build (tree t,
17012 tree args,
17013 tsubst_flags_t complain,
17014 tree in_decl,
17015 bool function_p,
17016 bool integral_constant_expression_p)
17017 {
17018 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17019 #define RECUR(NODE) \
17020 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17021 /*function_p=*/false, \
17022 integral_constant_expression_p)
17023
17024 tree retval, op1;
17025 location_t loc;
17026
17027 if (t == NULL_TREE || t == error_mark_node)
17028 return t;
17029
17030 loc = input_location;
17031 if (EXPR_HAS_LOCATION (t))
17032 input_location = EXPR_LOCATION (t);
17033
17034 /* N3276 decltype magic only applies to calls at the top level or on the
17035 right side of a comma. */
17036 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17037 complain &= ~tf_decltype;
17038
17039 switch (TREE_CODE (t))
17040 {
17041 case USING_DECL:
17042 t = DECL_NAME (t);
17043 /* Fall through. */
17044 case IDENTIFIER_NODE:
17045 {
17046 tree decl;
17047 cp_id_kind idk;
17048 bool non_integral_constant_expression_p;
17049 const char *error_msg;
17050
17051 if (IDENTIFIER_CONV_OP_P (t))
17052 {
17053 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17054 t = make_conv_op_name (new_type);
17055 }
17056
17057 /* Look up the name. */
17058 decl = lookup_name (t);
17059
17060 /* By convention, expressions use ERROR_MARK_NODE to indicate
17061 failure, not NULL_TREE. */
17062 if (decl == NULL_TREE)
17063 decl = error_mark_node;
17064
17065 decl = finish_id_expression (t, decl, NULL_TREE,
17066 &idk,
17067 integral_constant_expression_p,
17068 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17069 &non_integral_constant_expression_p,
17070 /*template_p=*/false,
17071 /*done=*/true,
17072 /*address_p=*/false,
17073 /*template_arg_p=*/false,
17074 &error_msg,
17075 input_location);
17076 if (error_msg)
17077 error (error_msg);
17078 if (!function_p && identifier_p (decl))
17079 {
17080 if (complain & tf_error)
17081 unqualified_name_lookup_error (decl);
17082 decl = error_mark_node;
17083 }
17084 RETURN (decl);
17085 }
17086
17087 case TEMPLATE_ID_EXPR:
17088 {
17089 tree object;
17090 tree templ = RECUR (TREE_OPERAND (t, 0));
17091 tree targs = TREE_OPERAND (t, 1);
17092
17093 if (targs)
17094 targs = tsubst_template_args (targs, args, complain, in_decl);
17095 if (targs == error_mark_node)
17096 return error_mark_node;
17097
17098 if (TREE_CODE (templ) == SCOPE_REF)
17099 {
17100 tree name = TREE_OPERAND (templ, 1);
17101 tree tid = lookup_template_function (name, targs);
17102 TREE_OPERAND (templ, 1) = tid;
17103 return templ;
17104 }
17105
17106 if (variable_template_p (templ))
17107 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17108
17109 if (TREE_CODE (templ) == COMPONENT_REF)
17110 {
17111 object = TREE_OPERAND (templ, 0);
17112 templ = TREE_OPERAND (templ, 1);
17113 }
17114 else
17115 object = NULL_TREE;
17116 templ = lookup_template_function (templ, targs);
17117
17118 if (object)
17119 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17120 object, templ, NULL_TREE));
17121 else
17122 RETURN (baselink_for_fns (templ));
17123 }
17124
17125 case INDIRECT_REF:
17126 {
17127 tree r = RECUR (TREE_OPERAND (t, 0));
17128
17129 if (REFERENCE_REF_P (t))
17130 {
17131 /* A type conversion to reference type will be enclosed in
17132 such an indirect ref, but the substitution of the cast
17133 will have also added such an indirect ref. */
17134 r = convert_from_reference (r);
17135 }
17136 else
17137 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17138 complain|decltype_flag);
17139
17140 if (TREE_CODE (r) == INDIRECT_REF)
17141 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17142
17143 RETURN (r);
17144 }
17145
17146 case NOP_EXPR:
17147 {
17148 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17149 tree op0 = RECUR (TREE_OPERAND (t, 0));
17150 RETURN (build_nop (type, op0));
17151 }
17152
17153 case IMPLICIT_CONV_EXPR:
17154 {
17155 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17156 tree expr = RECUR (TREE_OPERAND (t, 0));
17157 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17158 {
17159 retval = copy_node (t);
17160 TREE_TYPE (retval) = type;
17161 TREE_OPERAND (retval, 0) = expr;
17162 RETURN (retval);
17163 }
17164 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17165 /* We'll pass this to convert_nontype_argument again, we don't need
17166 to actually perform any conversion here. */
17167 RETURN (expr);
17168 int flags = LOOKUP_IMPLICIT;
17169 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17170 flags = LOOKUP_NORMAL;
17171 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17172 flags));
17173 }
17174
17175 case CONVERT_EXPR:
17176 {
17177 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17178 tree op0 = RECUR (TREE_OPERAND (t, 0));
17179 RETURN (build1 (CONVERT_EXPR, type, op0));
17180 }
17181
17182 case CAST_EXPR:
17183 case REINTERPRET_CAST_EXPR:
17184 case CONST_CAST_EXPR:
17185 case DYNAMIC_CAST_EXPR:
17186 case STATIC_CAST_EXPR:
17187 {
17188 tree type;
17189 tree op, r = NULL_TREE;
17190
17191 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17192 if (integral_constant_expression_p
17193 && !cast_valid_in_integral_constant_expression_p (type))
17194 {
17195 if (complain & tf_error)
17196 error ("a cast to a type other than an integral or "
17197 "enumeration type cannot appear in a constant-expression");
17198 RETURN (error_mark_node);
17199 }
17200
17201 op = RECUR (TREE_OPERAND (t, 0));
17202
17203 warning_sentinel s(warn_useless_cast);
17204 warning_sentinel s2(warn_ignored_qualifiers);
17205 switch (TREE_CODE (t))
17206 {
17207 case CAST_EXPR:
17208 r = build_functional_cast (type, op, complain);
17209 break;
17210 case REINTERPRET_CAST_EXPR:
17211 r = build_reinterpret_cast (type, op, complain);
17212 break;
17213 case CONST_CAST_EXPR:
17214 r = build_const_cast (type, op, complain);
17215 break;
17216 case DYNAMIC_CAST_EXPR:
17217 r = build_dynamic_cast (type, op, complain);
17218 break;
17219 case STATIC_CAST_EXPR:
17220 r = build_static_cast (type, op, complain);
17221 break;
17222 default:
17223 gcc_unreachable ();
17224 }
17225
17226 RETURN (r);
17227 }
17228
17229 case POSTDECREMENT_EXPR:
17230 case POSTINCREMENT_EXPR:
17231 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17232 args, complain, in_decl);
17233 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17234 complain|decltype_flag));
17235
17236 case PREDECREMENT_EXPR:
17237 case PREINCREMENT_EXPR:
17238 case NEGATE_EXPR:
17239 case BIT_NOT_EXPR:
17240 case ABS_EXPR:
17241 case TRUTH_NOT_EXPR:
17242 case UNARY_PLUS_EXPR: /* Unary + */
17243 case REALPART_EXPR:
17244 case IMAGPART_EXPR:
17245 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17246 RECUR (TREE_OPERAND (t, 0)),
17247 complain|decltype_flag));
17248
17249 case FIX_TRUNC_EXPR:
17250 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17251 false, complain));
17252
17253 case ADDR_EXPR:
17254 op1 = TREE_OPERAND (t, 0);
17255 if (TREE_CODE (op1) == LABEL_DECL)
17256 RETURN (finish_label_address_expr (DECL_NAME (op1),
17257 EXPR_LOCATION (op1)));
17258 if (TREE_CODE (op1) == SCOPE_REF)
17259 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17260 /*done=*/true, /*address_p=*/true);
17261 else
17262 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17263 in_decl);
17264 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17265 complain|decltype_flag));
17266
17267 case PLUS_EXPR:
17268 case MINUS_EXPR:
17269 case MULT_EXPR:
17270 case TRUNC_DIV_EXPR:
17271 case CEIL_DIV_EXPR:
17272 case FLOOR_DIV_EXPR:
17273 case ROUND_DIV_EXPR:
17274 case EXACT_DIV_EXPR:
17275 case BIT_AND_EXPR:
17276 case BIT_IOR_EXPR:
17277 case BIT_XOR_EXPR:
17278 case TRUNC_MOD_EXPR:
17279 case FLOOR_MOD_EXPR:
17280 case TRUTH_ANDIF_EXPR:
17281 case TRUTH_ORIF_EXPR:
17282 case TRUTH_AND_EXPR:
17283 case TRUTH_OR_EXPR:
17284 case RSHIFT_EXPR:
17285 case LSHIFT_EXPR:
17286 case RROTATE_EXPR:
17287 case LROTATE_EXPR:
17288 case EQ_EXPR:
17289 case NE_EXPR:
17290 case MAX_EXPR:
17291 case MIN_EXPR:
17292 case LE_EXPR:
17293 case GE_EXPR:
17294 case LT_EXPR:
17295 case GT_EXPR:
17296 case MEMBER_REF:
17297 case DOTSTAR_EXPR:
17298 {
17299 warning_sentinel s1(warn_type_limits);
17300 warning_sentinel s2(warn_div_by_zero);
17301 warning_sentinel s3(warn_logical_op);
17302 warning_sentinel s4(warn_tautological_compare);
17303 tree op0 = RECUR (TREE_OPERAND (t, 0));
17304 tree op1 = RECUR (TREE_OPERAND (t, 1));
17305 tree r = build_x_binary_op
17306 (input_location, TREE_CODE (t),
17307 op0,
17308 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17309 ? ERROR_MARK
17310 : TREE_CODE (TREE_OPERAND (t, 0))),
17311 op1,
17312 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17313 ? ERROR_MARK
17314 : TREE_CODE (TREE_OPERAND (t, 1))),
17315 /*overload=*/NULL,
17316 complain|decltype_flag);
17317 if (EXPR_P (r) && TREE_NO_WARNING (t))
17318 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17319
17320 RETURN (r);
17321 }
17322
17323 case POINTER_PLUS_EXPR:
17324 {
17325 tree op0 = RECUR (TREE_OPERAND (t, 0));
17326 tree op1 = RECUR (TREE_OPERAND (t, 1));
17327 return fold_build_pointer_plus (op0, op1);
17328 }
17329
17330 case SCOPE_REF:
17331 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17332 /*address_p=*/false));
17333 case ARRAY_REF:
17334 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17335 args, complain, in_decl);
17336 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17337 RECUR (TREE_OPERAND (t, 1)),
17338 complain|decltype_flag));
17339
17340 case SIZEOF_EXPR:
17341 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17342 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17343 RETURN (tsubst_copy (t, args, complain, in_decl));
17344 /* Fall through */
17345
17346 case ALIGNOF_EXPR:
17347 {
17348 tree r;
17349
17350 op1 = TREE_OPERAND (t, 0);
17351 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17352 op1 = TREE_TYPE (op1);
17353 if (!args)
17354 {
17355 /* When there are no ARGS, we are trying to evaluate a
17356 non-dependent expression from the parser. Trying to do
17357 the substitutions may not work. */
17358 if (!TYPE_P (op1))
17359 op1 = TREE_TYPE (op1);
17360 }
17361 else
17362 {
17363 ++cp_unevaluated_operand;
17364 ++c_inhibit_evaluation_warnings;
17365 if (TYPE_P (op1))
17366 op1 = tsubst (op1, args, complain, in_decl);
17367 else
17368 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17369 /*function_p=*/false,
17370 /*integral_constant_expression_p=*/
17371 false);
17372 --cp_unevaluated_operand;
17373 --c_inhibit_evaluation_warnings;
17374 }
17375 if (TYPE_P (op1))
17376 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17377 complain & tf_error);
17378 else
17379 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17380 complain & tf_error);
17381 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17382 {
17383 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17384 {
17385 if (!processing_template_decl && TYPE_P (op1))
17386 {
17387 r = build_min (SIZEOF_EXPR, size_type_node,
17388 build1 (NOP_EXPR, op1, error_mark_node));
17389 SIZEOF_EXPR_TYPE_P (r) = 1;
17390 }
17391 else
17392 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17393 TREE_SIDE_EFFECTS (r) = 0;
17394 TREE_READONLY (r) = 1;
17395 }
17396 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17397 }
17398 RETURN (r);
17399 }
17400
17401 case AT_ENCODE_EXPR:
17402 {
17403 op1 = TREE_OPERAND (t, 0);
17404 ++cp_unevaluated_operand;
17405 ++c_inhibit_evaluation_warnings;
17406 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17407 /*function_p=*/false,
17408 /*integral_constant_expression_p=*/false);
17409 --cp_unevaluated_operand;
17410 --c_inhibit_evaluation_warnings;
17411 RETURN (objc_build_encode_expr (op1));
17412 }
17413
17414 case NOEXCEPT_EXPR:
17415 op1 = TREE_OPERAND (t, 0);
17416 ++cp_unevaluated_operand;
17417 ++c_inhibit_evaluation_warnings;
17418 ++cp_noexcept_operand;
17419 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17420 /*function_p=*/false,
17421 /*integral_constant_expression_p=*/false);
17422 --cp_unevaluated_operand;
17423 --c_inhibit_evaluation_warnings;
17424 --cp_noexcept_operand;
17425 RETURN (finish_noexcept_expr (op1, complain));
17426
17427 case MODOP_EXPR:
17428 {
17429 warning_sentinel s(warn_div_by_zero);
17430 tree lhs = RECUR (TREE_OPERAND (t, 0));
17431 tree rhs = RECUR (TREE_OPERAND (t, 2));
17432 tree r = build_x_modify_expr
17433 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17434 complain|decltype_flag);
17435 /* TREE_NO_WARNING must be set if either the expression was
17436 parenthesized or it uses an operator such as >>= rather
17437 than plain assignment. In the former case, it was already
17438 set and must be copied. In the latter case,
17439 build_x_modify_expr sets it and it must not be reset
17440 here. */
17441 if (TREE_NO_WARNING (t))
17442 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17443
17444 RETURN (r);
17445 }
17446
17447 case ARROW_EXPR:
17448 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17449 args, complain, in_decl);
17450 /* Remember that there was a reference to this entity. */
17451 if (DECL_P (op1)
17452 && !mark_used (op1, complain) && !(complain & tf_error))
17453 RETURN (error_mark_node);
17454 RETURN (build_x_arrow (input_location, op1, complain));
17455
17456 case NEW_EXPR:
17457 {
17458 tree placement = RECUR (TREE_OPERAND (t, 0));
17459 tree init = RECUR (TREE_OPERAND (t, 3));
17460 vec<tree, va_gc> *placement_vec;
17461 vec<tree, va_gc> *init_vec;
17462 tree ret;
17463
17464 if (placement == NULL_TREE)
17465 placement_vec = NULL;
17466 else
17467 {
17468 placement_vec = make_tree_vector ();
17469 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17470 vec_safe_push (placement_vec, TREE_VALUE (placement));
17471 }
17472
17473 /* If there was an initializer in the original tree, but it
17474 instantiated to an empty list, then we should pass a
17475 non-NULL empty vector to tell build_new that it was an
17476 empty initializer() rather than no initializer. This can
17477 only happen when the initializer is a pack expansion whose
17478 parameter packs are of length zero. */
17479 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17480 init_vec = NULL;
17481 else
17482 {
17483 init_vec = make_tree_vector ();
17484 if (init == void_node)
17485 gcc_assert (init_vec != NULL);
17486 else
17487 {
17488 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17489 vec_safe_push (init_vec, TREE_VALUE (init));
17490 }
17491 }
17492
17493 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17494 tree op2 = RECUR (TREE_OPERAND (t, 2));
17495 ret = build_new (&placement_vec, op1, op2, &init_vec,
17496 NEW_EXPR_USE_GLOBAL (t),
17497 complain);
17498
17499 if (placement_vec != NULL)
17500 release_tree_vector (placement_vec);
17501 if (init_vec != NULL)
17502 release_tree_vector (init_vec);
17503
17504 RETURN (ret);
17505 }
17506
17507 case DELETE_EXPR:
17508 {
17509 tree op0 = RECUR (TREE_OPERAND (t, 0));
17510 tree op1 = RECUR (TREE_OPERAND (t, 1));
17511 RETURN (delete_sanity (op0, op1,
17512 DELETE_EXPR_USE_VEC (t),
17513 DELETE_EXPR_USE_GLOBAL (t),
17514 complain));
17515 }
17516
17517 case COMPOUND_EXPR:
17518 {
17519 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17520 complain & ~tf_decltype, in_decl,
17521 /*function_p=*/false,
17522 integral_constant_expression_p);
17523 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17524 op0,
17525 RECUR (TREE_OPERAND (t, 1)),
17526 complain|decltype_flag));
17527 }
17528
17529 case CALL_EXPR:
17530 {
17531 tree function;
17532 vec<tree, va_gc> *call_args;
17533 unsigned int nargs, i;
17534 bool qualified_p;
17535 bool koenig_p;
17536 tree ret;
17537
17538 function = CALL_EXPR_FN (t);
17539 /* Internal function with no arguments. */
17540 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17541 RETURN (t);
17542
17543 /* When we parsed the expression, we determined whether or
17544 not Koenig lookup should be performed. */
17545 koenig_p = KOENIG_LOOKUP_P (t);
17546 if (function == NULL_TREE)
17547 {
17548 koenig_p = false;
17549 qualified_p = false;
17550 }
17551 else if (TREE_CODE (function) == SCOPE_REF)
17552 {
17553 qualified_p = true;
17554 function = tsubst_qualified_id (function, args, complain, in_decl,
17555 /*done=*/false,
17556 /*address_p=*/false);
17557 }
17558 else if (koenig_p && identifier_p (function))
17559 {
17560 /* Do nothing; calling tsubst_copy_and_build on an identifier
17561 would incorrectly perform unqualified lookup again.
17562
17563 Note that we can also have an IDENTIFIER_NODE if the earlier
17564 unqualified lookup found a member function; in that case
17565 koenig_p will be false and we do want to do the lookup
17566 again to find the instantiated member function.
17567
17568 FIXME but doing that causes c++/15272, so we need to stop
17569 using IDENTIFIER_NODE in that situation. */
17570 qualified_p = false;
17571 }
17572 else
17573 {
17574 if (TREE_CODE (function) == COMPONENT_REF)
17575 {
17576 tree op = TREE_OPERAND (function, 1);
17577
17578 qualified_p = (TREE_CODE (op) == SCOPE_REF
17579 || (BASELINK_P (op)
17580 && BASELINK_QUALIFIED_P (op)));
17581 }
17582 else
17583 qualified_p = false;
17584
17585 if (TREE_CODE (function) == ADDR_EXPR
17586 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17587 /* Avoid error about taking the address of a constructor. */
17588 function = TREE_OPERAND (function, 0);
17589
17590 function = tsubst_copy_and_build (function, args, complain,
17591 in_decl,
17592 !qualified_p,
17593 integral_constant_expression_p);
17594
17595 if (BASELINK_P (function))
17596 qualified_p = true;
17597 }
17598
17599 nargs = call_expr_nargs (t);
17600 call_args = make_tree_vector ();
17601 for (i = 0; i < nargs; ++i)
17602 {
17603 tree arg = CALL_EXPR_ARG (t, i);
17604
17605 if (!PACK_EXPANSION_P (arg))
17606 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17607 else
17608 {
17609 /* Expand the pack expansion and push each entry onto
17610 CALL_ARGS. */
17611 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17612 if (TREE_CODE (arg) == TREE_VEC)
17613 {
17614 unsigned int len, j;
17615
17616 len = TREE_VEC_LENGTH (arg);
17617 for (j = 0; j < len; ++j)
17618 {
17619 tree value = TREE_VEC_ELT (arg, j);
17620 if (value != NULL_TREE)
17621 value = convert_from_reference (value);
17622 vec_safe_push (call_args, value);
17623 }
17624 }
17625 else
17626 {
17627 /* A partial substitution. Add one entry. */
17628 vec_safe_push (call_args, arg);
17629 }
17630 }
17631 }
17632
17633 /* We do not perform argument-dependent lookup if normal
17634 lookup finds a non-function, in accordance with the
17635 expected resolution of DR 218. */
17636 if (koenig_p
17637 && ((is_overloaded_fn (function)
17638 /* If lookup found a member function, the Koenig lookup is
17639 not appropriate, even if an unqualified-name was used
17640 to denote the function. */
17641 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17642 || identifier_p (function))
17643 /* Only do this when substitution turns a dependent call
17644 into a non-dependent call. */
17645 && type_dependent_expression_p_push (t)
17646 && !any_type_dependent_arguments_p (call_args))
17647 function = perform_koenig_lookup (function, call_args, tf_none);
17648
17649 if (function != NULL_TREE
17650 && identifier_p (function)
17651 && !any_type_dependent_arguments_p (call_args))
17652 {
17653 if (koenig_p && (complain & tf_warning_or_error))
17654 {
17655 /* For backwards compatibility and good diagnostics, try
17656 the unqualified lookup again if we aren't in SFINAE
17657 context. */
17658 tree unq = (tsubst_copy_and_build
17659 (function, args, complain, in_decl, true,
17660 integral_constant_expression_p));
17661 if (unq == error_mark_node)
17662 {
17663 release_tree_vector (call_args);
17664 RETURN (error_mark_node);
17665 }
17666
17667 if (unq != function)
17668 {
17669 /* In a lambda fn, we have to be careful to not
17670 introduce new this captures. Legacy code can't
17671 be using lambdas anyway, so it's ok to be
17672 stricter. */
17673 bool in_lambda = (current_class_type
17674 && LAMBDA_TYPE_P (current_class_type));
17675 char const *const msg
17676 = G_("%qD was not declared in this scope, "
17677 "and no declarations were found by "
17678 "argument-dependent lookup at the point "
17679 "of instantiation");
17680
17681 bool diag = true;
17682 if (in_lambda)
17683 error_at (EXPR_LOC_OR_LOC (t, input_location),
17684 msg, function);
17685 else
17686 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17687 msg, function);
17688 if (diag)
17689 {
17690 tree fn = unq;
17691
17692 if (INDIRECT_REF_P (fn))
17693 fn = TREE_OPERAND (fn, 0);
17694 if (is_overloaded_fn (fn))
17695 fn = get_first_fn (fn);
17696
17697 if (!DECL_P (fn))
17698 /* Can't say anything more. */;
17699 else if (DECL_CLASS_SCOPE_P (fn))
17700 {
17701 location_t loc = EXPR_LOC_OR_LOC (t,
17702 input_location);
17703 inform (loc,
17704 "declarations in dependent base %qT are "
17705 "not found by unqualified lookup",
17706 DECL_CLASS_CONTEXT (fn));
17707 if (current_class_ptr)
17708 inform (loc,
17709 "use %<this->%D%> instead", function);
17710 else
17711 inform (loc,
17712 "use %<%T::%D%> instead",
17713 current_class_name, function);
17714 }
17715 else
17716 inform (DECL_SOURCE_LOCATION (fn),
17717 "%qD declared here, later in the "
17718 "translation unit", fn);
17719 if (in_lambda)
17720 {
17721 release_tree_vector (call_args);
17722 RETURN (error_mark_node);
17723 }
17724 }
17725
17726 function = unq;
17727 }
17728 }
17729 if (identifier_p (function))
17730 {
17731 if (complain & tf_error)
17732 unqualified_name_lookup_error (function);
17733 release_tree_vector (call_args);
17734 RETURN (error_mark_node);
17735 }
17736 }
17737
17738 /* Remember that there was a reference to this entity. */
17739 if (function != NULL_TREE
17740 && DECL_P (function)
17741 && !mark_used (function, complain) && !(complain & tf_error))
17742 {
17743 release_tree_vector (call_args);
17744 RETURN (error_mark_node);
17745 }
17746
17747 /* Put back tf_decltype for the actual call. */
17748 complain |= decltype_flag;
17749
17750 if (function == NULL_TREE)
17751 switch (CALL_EXPR_IFN (t))
17752 {
17753 case IFN_LAUNDER:
17754 gcc_assert (nargs == 1);
17755 if (vec_safe_length (call_args) != 1)
17756 {
17757 error_at (EXPR_LOC_OR_LOC (t, input_location),
17758 "wrong number of arguments to "
17759 "%<__builtin_launder%>");
17760 ret = error_mark_node;
17761 }
17762 else
17763 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17764 input_location),
17765 (*call_args)[0], complain);
17766 break;
17767
17768 default:
17769 /* Unsupported internal function with arguments. */
17770 gcc_unreachable ();
17771 }
17772 else if (TREE_CODE (function) == OFFSET_REF)
17773 ret = build_offset_ref_call_from_tree (function, &call_args,
17774 complain);
17775 else if (TREE_CODE (function) == COMPONENT_REF)
17776 {
17777 tree instance = TREE_OPERAND (function, 0);
17778 tree fn = TREE_OPERAND (function, 1);
17779
17780 if (processing_template_decl
17781 && (type_dependent_expression_p (instance)
17782 || (!BASELINK_P (fn)
17783 && TREE_CODE (fn) != FIELD_DECL)
17784 || type_dependent_expression_p (fn)
17785 || any_type_dependent_arguments_p (call_args)))
17786 ret = build_min_nt_call_vec (function, call_args);
17787 else if (!BASELINK_P (fn))
17788 ret = finish_call_expr (function, &call_args,
17789 /*disallow_virtual=*/false,
17790 /*koenig_p=*/false,
17791 complain);
17792 else
17793 ret = (build_new_method_call
17794 (instance, fn,
17795 &call_args, NULL_TREE,
17796 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17797 /*fn_p=*/NULL,
17798 complain));
17799 }
17800 else
17801 ret = finish_call_expr (function, &call_args,
17802 /*disallow_virtual=*/qualified_p,
17803 koenig_p,
17804 complain);
17805
17806 release_tree_vector (call_args);
17807
17808 if (ret != error_mark_node)
17809 {
17810 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17811 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17812 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17813 bool thk = CALL_FROM_THUNK_P (t);
17814 if (op || ord || rev || thk)
17815 {
17816 function = extract_call_expr (ret);
17817 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17818 CALL_EXPR_ORDERED_ARGS (function) = ord;
17819 CALL_EXPR_REVERSE_ARGS (function) = rev;
17820 if (thk)
17821 {
17822 CALL_FROM_THUNK_P (function) = true;
17823 /* The thunk location is not interesting. */
17824 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17825 }
17826 }
17827 }
17828
17829 RETURN (ret);
17830 }
17831
17832 case COND_EXPR:
17833 {
17834 tree cond = RECUR (TREE_OPERAND (t, 0));
17835 tree folded_cond = fold_non_dependent_expr (cond);
17836 tree exp1, exp2;
17837
17838 if (TREE_CODE (folded_cond) == INTEGER_CST)
17839 {
17840 if (integer_zerop (folded_cond))
17841 {
17842 ++c_inhibit_evaluation_warnings;
17843 exp1 = RECUR (TREE_OPERAND (t, 1));
17844 --c_inhibit_evaluation_warnings;
17845 exp2 = RECUR (TREE_OPERAND (t, 2));
17846 }
17847 else
17848 {
17849 exp1 = RECUR (TREE_OPERAND (t, 1));
17850 ++c_inhibit_evaluation_warnings;
17851 exp2 = RECUR (TREE_OPERAND (t, 2));
17852 --c_inhibit_evaluation_warnings;
17853 }
17854 cond = folded_cond;
17855 }
17856 else
17857 {
17858 exp1 = RECUR (TREE_OPERAND (t, 1));
17859 exp2 = RECUR (TREE_OPERAND (t, 2));
17860 }
17861
17862 warning_sentinel s(warn_duplicated_branches);
17863 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17864 cond, exp1, exp2, complain));
17865 }
17866
17867 case PSEUDO_DTOR_EXPR:
17868 {
17869 tree op0 = RECUR (TREE_OPERAND (t, 0));
17870 tree op1 = RECUR (TREE_OPERAND (t, 1));
17871 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17872 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17873 input_location));
17874 }
17875
17876 case TREE_LIST:
17877 {
17878 tree purpose, value, chain;
17879
17880 if (t == void_list_node)
17881 RETURN (t);
17882
17883 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17884 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17885 {
17886 /* We have pack expansions, so expand those and
17887 create a new list out of it. */
17888 tree purposevec = NULL_TREE;
17889 tree valuevec = NULL_TREE;
17890 tree chain;
17891 int i, len = -1;
17892
17893 /* Expand the argument expressions. */
17894 if (TREE_PURPOSE (t))
17895 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17896 complain, in_decl);
17897 if (TREE_VALUE (t))
17898 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17899 complain, in_decl);
17900
17901 /* Build the rest of the list. */
17902 chain = TREE_CHAIN (t);
17903 if (chain && chain != void_type_node)
17904 chain = RECUR (chain);
17905
17906 /* Determine the number of arguments. */
17907 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17908 {
17909 len = TREE_VEC_LENGTH (purposevec);
17910 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17911 }
17912 else if (TREE_CODE (valuevec) == TREE_VEC)
17913 len = TREE_VEC_LENGTH (valuevec);
17914 else
17915 {
17916 /* Since we only performed a partial substitution into
17917 the argument pack, we only RETURN (a single list
17918 node. */
17919 if (purposevec == TREE_PURPOSE (t)
17920 && valuevec == TREE_VALUE (t)
17921 && chain == TREE_CHAIN (t))
17922 RETURN (t);
17923
17924 RETURN (tree_cons (purposevec, valuevec, chain));
17925 }
17926
17927 /* Convert the argument vectors into a TREE_LIST */
17928 i = len;
17929 while (i > 0)
17930 {
17931 /* Grab the Ith values. */
17932 i--;
17933 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17934 : NULL_TREE;
17935 value
17936 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17937 : NULL_TREE;
17938
17939 /* Build the list (backwards). */
17940 chain = tree_cons (purpose, value, chain);
17941 }
17942
17943 RETURN (chain);
17944 }
17945
17946 purpose = TREE_PURPOSE (t);
17947 if (purpose)
17948 purpose = RECUR (purpose);
17949 value = TREE_VALUE (t);
17950 if (value)
17951 value = RECUR (value);
17952 chain = TREE_CHAIN (t);
17953 if (chain && chain != void_type_node)
17954 chain = RECUR (chain);
17955 if (purpose == TREE_PURPOSE (t)
17956 && value == TREE_VALUE (t)
17957 && chain == TREE_CHAIN (t))
17958 RETURN (t);
17959 RETURN (tree_cons (purpose, value, chain));
17960 }
17961
17962 case COMPONENT_REF:
17963 {
17964 tree object;
17965 tree object_type;
17966 tree member;
17967 tree r;
17968
17969 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17970 args, complain, in_decl);
17971 /* Remember that there was a reference to this entity. */
17972 if (DECL_P (object)
17973 && !mark_used (object, complain) && !(complain & tf_error))
17974 RETURN (error_mark_node);
17975 object_type = TREE_TYPE (object);
17976
17977 member = TREE_OPERAND (t, 1);
17978 if (BASELINK_P (member))
17979 member = tsubst_baselink (member,
17980 non_reference (TREE_TYPE (object)),
17981 args, complain, in_decl);
17982 else
17983 member = tsubst_copy (member, args, complain, in_decl);
17984 if (member == error_mark_node)
17985 RETURN (error_mark_node);
17986
17987 if (TREE_CODE (member) == FIELD_DECL)
17988 {
17989 r = finish_non_static_data_member (member, object, NULL_TREE);
17990 if (TREE_CODE (r) == COMPONENT_REF)
17991 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17992 RETURN (r);
17993 }
17994 else if (type_dependent_expression_p (object))
17995 /* We can't do much here. */;
17996 else if (!CLASS_TYPE_P (object_type))
17997 {
17998 if (scalarish_type_p (object_type))
17999 {
18000 tree s = NULL_TREE;
18001 tree dtor = member;
18002
18003 if (TREE_CODE (dtor) == SCOPE_REF)
18004 {
18005 s = TREE_OPERAND (dtor, 0);
18006 dtor = TREE_OPERAND (dtor, 1);
18007 }
18008 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18009 {
18010 dtor = TREE_OPERAND (dtor, 0);
18011 if (TYPE_P (dtor))
18012 RETURN (finish_pseudo_destructor_expr
18013 (object, s, dtor, input_location));
18014 }
18015 }
18016 }
18017 else if (TREE_CODE (member) == SCOPE_REF
18018 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18019 {
18020 /* Lookup the template functions now that we know what the
18021 scope is. */
18022 tree scope = TREE_OPERAND (member, 0);
18023 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18024 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18025 member = lookup_qualified_name (scope, tmpl,
18026 /*is_type_p=*/false,
18027 /*complain=*/false);
18028 if (BASELINK_P (member))
18029 {
18030 BASELINK_FUNCTIONS (member)
18031 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18032 args);
18033 member = (adjust_result_of_qualified_name_lookup
18034 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18035 object_type));
18036 }
18037 else
18038 {
18039 qualified_name_lookup_error (scope, tmpl, member,
18040 input_location);
18041 RETURN (error_mark_node);
18042 }
18043 }
18044 else if (TREE_CODE (member) == SCOPE_REF
18045 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18046 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18047 {
18048 if (complain & tf_error)
18049 {
18050 if (TYPE_P (TREE_OPERAND (member, 0)))
18051 error ("%qT is not a class or namespace",
18052 TREE_OPERAND (member, 0));
18053 else
18054 error ("%qD is not a class or namespace",
18055 TREE_OPERAND (member, 0));
18056 }
18057 RETURN (error_mark_node);
18058 }
18059
18060 r = finish_class_member_access_expr (object, member,
18061 /*template_p=*/false,
18062 complain);
18063 if (TREE_CODE (r) == COMPONENT_REF)
18064 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18065 RETURN (r);
18066 }
18067
18068 case THROW_EXPR:
18069 RETURN (build_throw
18070 (RECUR (TREE_OPERAND (t, 0))));
18071
18072 case CONSTRUCTOR:
18073 {
18074 vec<constructor_elt, va_gc> *n;
18075 constructor_elt *ce;
18076 unsigned HOST_WIDE_INT idx;
18077 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18078 bool process_index_p;
18079 int newlen;
18080 bool need_copy_p = false;
18081 tree r;
18082
18083 if (type == error_mark_node)
18084 RETURN (error_mark_node);
18085
18086 /* digest_init will do the wrong thing if we let it. */
18087 if (type && TYPE_PTRMEMFUNC_P (type))
18088 RETURN (t);
18089
18090 /* We do not want to process the index of aggregate
18091 initializers as they are identifier nodes which will be
18092 looked up by digest_init. */
18093 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18094
18095 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18096 newlen = vec_safe_length (n);
18097 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18098 {
18099 if (ce->index && process_index_p
18100 /* An identifier index is looked up in the type
18101 being initialized, not the current scope. */
18102 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18103 ce->index = RECUR (ce->index);
18104
18105 if (PACK_EXPANSION_P (ce->value))
18106 {
18107 /* Substitute into the pack expansion. */
18108 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18109 in_decl);
18110
18111 if (ce->value == error_mark_node
18112 || PACK_EXPANSION_P (ce->value))
18113 ;
18114 else if (TREE_VEC_LENGTH (ce->value) == 1)
18115 /* Just move the argument into place. */
18116 ce->value = TREE_VEC_ELT (ce->value, 0);
18117 else
18118 {
18119 /* Update the length of the final CONSTRUCTOR
18120 arguments vector, and note that we will need to
18121 copy.*/
18122 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18123 need_copy_p = true;
18124 }
18125 }
18126 else
18127 ce->value = RECUR (ce->value);
18128 }
18129
18130 if (need_copy_p)
18131 {
18132 vec<constructor_elt, va_gc> *old_n = n;
18133
18134 vec_alloc (n, newlen);
18135 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18136 {
18137 if (TREE_CODE (ce->value) == TREE_VEC)
18138 {
18139 int i, len = TREE_VEC_LENGTH (ce->value);
18140 for (i = 0; i < len; ++i)
18141 CONSTRUCTOR_APPEND_ELT (n, 0,
18142 TREE_VEC_ELT (ce->value, i));
18143 }
18144 else
18145 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18146 }
18147 }
18148
18149 r = build_constructor (init_list_type_node, n);
18150 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18151
18152 if (TREE_HAS_CONSTRUCTOR (t))
18153 {
18154 fcl_t cl = fcl_functional;
18155 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18156 cl = fcl_c99;
18157 RETURN (finish_compound_literal (type, r, complain, cl));
18158 }
18159
18160 TREE_TYPE (r) = type;
18161 RETURN (r);
18162 }
18163
18164 case TYPEID_EXPR:
18165 {
18166 tree operand_0 = TREE_OPERAND (t, 0);
18167 if (TYPE_P (operand_0))
18168 {
18169 operand_0 = tsubst (operand_0, args, complain, in_decl);
18170 RETURN (get_typeid (operand_0, complain));
18171 }
18172 else
18173 {
18174 operand_0 = RECUR (operand_0);
18175 RETURN (build_typeid (operand_0, complain));
18176 }
18177 }
18178
18179 case VAR_DECL:
18180 if (!args)
18181 RETURN (t);
18182 else if (DECL_PACK_P (t))
18183 {
18184 /* We don't build decls for an instantiation of a
18185 variadic capture proxy, we instantiate the elements
18186 when needed. */
18187 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18188 return RECUR (DECL_VALUE_EXPR (t));
18189 }
18190 /* Fall through */
18191
18192 case PARM_DECL:
18193 {
18194 tree r = tsubst_copy (t, args, complain, in_decl);
18195 /* ??? We're doing a subset of finish_id_expression here. */
18196 if (VAR_P (r)
18197 && !processing_template_decl
18198 && !cp_unevaluated_operand
18199 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18200 && CP_DECL_THREAD_LOCAL_P (r))
18201 {
18202 if (tree wrap = get_tls_wrapper_fn (r))
18203 /* Replace an evaluated use of the thread_local variable with
18204 a call to its wrapper. */
18205 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18206 }
18207 else if (outer_automatic_var_p (r))
18208 r = process_outer_var_ref (r, complain);
18209
18210 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18211 /* If the original type was a reference, we'll be wrapped in
18212 the appropriate INDIRECT_REF. */
18213 r = convert_from_reference (r);
18214 RETURN (r);
18215 }
18216
18217 case VA_ARG_EXPR:
18218 {
18219 tree op0 = RECUR (TREE_OPERAND (t, 0));
18220 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18221 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18222 }
18223
18224 case OFFSETOF_EXPR:
18225 {
18226 tree object_ptr
18227 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18228 in_decl, /*function_p=*/false,
18229 /*integral_constant_expression_p=*/false);
18230 RETURN (finish_offsetof (object_ptr,
18231 RECUR (TREE_OPERAND (t, 0)),
18232 EXPR_LOCATION (t)));
18233 }
18234
18235 case ADDRESSOF_EXPR:
18236 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18237 RECUR (TREE_OPERAND (t, 0)), complain));
18238
18239 case TRAIT_EXPR:
18240 {
18241 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18242 complain, in_decl);
18243
18244 tree type2 = TRAIT_EXPR_TYPE2 (t);
18245 if (type2 && TREE_CODE (type2) == TREE_LIST)
18246 type2 = RECUR (type2);
18247 else if (type2)
18248 type2 = tsubst (type2, args, complain, in_decl);
18249
18250 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18251 }
18252
18253 case STMT_EXPR:
18254 {
18255 tree old_stmt_expr = cur_stmt_expr;
18256 tree stmt_expr = begin_stmt_expr ();
18257
18258 cur_stmt_expr = stmt_expr;
18259 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18260 integral_constant_expression_p);
18261 stmt_expr = finish_stmt_expr (stmt_expr, false);
18262 cur_stmt_expr = old_stmt_expr;
18263
18264 /* If the resulting list of expression statement is empty,
18265 fold it further into void_node. */
18266 if (empty_expr_stmt_p (stmt_expr))
18267 stmt_expr = void_node;
18268
18269 RETURN (stmt_expr);
18270 }
18271
18272 case LAMBDA_EXPR:
18273 {
18274 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18275
18276 RETURN (build_lambda_object (r));
18277 }
18278
18279 case TARGET_EXPR:
18280 /* We can get here for a constant initializer of non-dependent type.
18281 FIXME stop folding in cp_parser_initializer_clause. */
18282 {
18283 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18284 complain);
18285 RETURN (r);
18286 }
18287
18288 case TRANSACTION_EXPR:
18289 RETURN (tsubst_expr(t, args, complain, in_decl,
18290 integral_constant_expression_p));
18291
18292 case PAREN_EXPR:
18293 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18294
18295 case VEC_PERM_EXPR:
18296 {
18297 tree op0 = RECUR (TREE_OPERAND (t, 0));
18298 tree op1 = RECUR (TREE_OPERAND (t, 1));
18299 tree op2 = RECUR (TREE_OPERAND (t, 2));
18300 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18301 complain));
18302 }
18303
18304 case REQUIRES_EXPR:
18305 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18306
18307 case NON_LVALUE_EXPR:
18308 case VIEW_CONVERT_EXPR:
18309 /* We should only see these for location wrapper nodes, or within
18310 instantiate_non_dependent_expr (when args is NULL_TREE). */
18311 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18312 if (location_wrapper_p (t))
18313 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18314 EXPR_LOCATION (t)));
18315 /* fallthrough. */
18316
18317 default:
18318 /* Handle Objective-C++ constructs, if appropriate. */
18319 {
18320 tree subst
18321 = objcp_tsubst_copy_and_build (t, args, complain,
18322 in_decl, /*function_p=*/false);
18323 if (subst)
18324 RETURN (subst);
18325 }
18326 RETURN (tsubst_copy (t, args, complain, in_decl));
18327 }
18328
18329 #undef RECUR
18330 #undef RETURN
18331 out:
18332 input_location = loc;
18333 return retval;
18334 }
18335
18336 /* Verify that the instantiated ARGS are valid. For type arguments,
18337 make sure that the type's linkage is ok. For non-type arguments,
18338 make sure they are constants if they are integral or enumerations.
18339 Emit an error under control of COMPLAIN, and return TRUE on error. */
18340
18341 static bool
18342 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18343 {
18344 if (dependent_template_arg_p (t))
18345 return false;
18346 if (ARGUMENT_PACK_P (t))
18347 {
18348 tree vec = ARGUMENT_PACK_ARGS (t);
18349 int len = TREE_VEC_LENGTH (vec);
18350 bool result = false;
18351 int i;
18352
18353 for (i = 0; i < len; ++i)
18354 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18355 result = true;
18356 return result;
18357 }
18358 else if (TYPE_P (t))
18359 {
18360 /* [basic.link]: A name with no linkage (notably, the name
18361 of a class or enumeration declared in a local scope)
18362 shall not be used to declare an entity with linkage.
18363 This implies that names with no linkage cannot be used as
18364 template arguments
18365
18366 DR 757 relaxes this restriction for C++0x. */
18367 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18368 : no_linkage_check (t, /*relaxed_p=*/false));
18369
18370 if (nt)
18371 {
18372 /* DR 488 makes use of a type with no linkage cause
18373 type deduction to fail. */
18374 if (complain & tf_error)
18375 {
18376 if (TYPE_UNNAMED_P (nt))
18377 error ("%qT is/uses unnamed type", t);
18378 else
18379 error ("template argument for %qD uses local type %qT",
18380 tmpl, t);
18381 }
18382 return true;
18383 }
18384 /* In order to avoid all sorts of complications, we do not
18385 allow variably-modified types as template arguments. */
18386 else if (variably_modified_type_p (t, NULL_TREE))
18387 {
18388 if (complain & tf_error)
18389 error ("%qT is a variably modified type", t);
18390 return true;
18391 }
18392 }
18393 /* Class template and alias template arguments should be OK. */
18394 else if (DECL_TYPE_TEMPLATE_P (t))
18395 ;
18396 /* A non-type argument of integral or enumerated type must be a
18397 constant. */
18398 else if (TREE_TYPE (t)
18399 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18400 && !REFERENCE_REF_P (t)
18401 && !TREE_CONSTANT (t))
18402 {
18403 if (complain & tf_error)
18404 error ("integral expression %qE is not constant", t);
18405 return true;
18406 }
18407 return false;
18408 }
18409
18410 static bool
18411 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18412 {
18413 int ix, len = DECL_NTPARMS (tmpl);
18414 bool result = false;
18415
18416 for (ix = 0; ix != len; ix++)
18417 {
18418 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18419 result = true;
18420 }
18421 if (result && (complain & tf_error))
18422 error (" trying to instantiate %qD", tmpl);
18423 return result;
18424 }
18425
18426 /* We're out of SFINAE context now, so generate diagnostics for the access
18427 errors we saw earlier when instantiating D from TMPL and ARGS. */
18428
18429 static void
18430 recheck_decl_substitution (tree d, tree tmpl, tree args)
18431 {
18432 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18433 tree type = TREE_TYPE (pattern);
18434 location_t loc = input_location;
18435
18436 push_access_scope (d);
18437 push_deferring_access_checks (dk_no_deferred);
18438 input_location = DECL_SOURCE_LOCATION (pattern);
18439 tsubst (type, args, tf_warning_or_error, d);
18440 input_location = loc;
18441 pop_deferring_access_checks ();
18442 pop_access_scope (d);
18443 }
18444
18445 /* Instantiate the indicated variable, function, or alias template TMPL with
18446 the template arguments in TARG_PTR. */
18447
18448 static tree
18449 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18450 {
18451 tree targ_ptr = orig_args;
18452 tree fndecl;
18453 tree gen_tmpl;
18454 tree spec;
18455 bool access_ok = true;
18456
18457 if (tmpl == error_mark_node)
18458 return error_mark_node;
18459
18460 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18461
18462 /* If this function is a clone, handle it specially. */
18463 if (DECL_CLONED_FUNCTION_P (tmpl))
18464 {
18465 tree spec;
18466 tree clone;
18467
18468 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18469 DECL_CLONED_FUNCTION. */
18470 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18471 targ_ptr, complain);
18472 if (spec == error_mark_node)
18473 return error_mark_node;
18474
18475 /* Look for the clone. */
18476 FOR_EACH_CLONE (clone, spec)
18477 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18478 return clone;
18479 /* We should always have found the clone by now. */
18480 gcc_unreachable ();
18481 return NULL_TREE;
18482 }
18483
18484 if (targ_ptr == error_mark_node)
18485 return error_mark_node;
18486
18487 /* Check to see if we already have this specialization. */
18488 gen_tmpl = most_general_template (tmpl);
18489 if (TMPL_ARGS_DEPTH (targ_ptr)
18490 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18491 /* targ_ptr only has the innermost template args, so add the outer ones
18492 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18493 the case of a non-dependent call within a template definition). */
18494 targ_ptr = (add_outermost_template_args
18495 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18496 targ_ptr));
18497
18498 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18499 but it doesn't seem to be on the hot path. */
18500 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18501
18502 gcc_assert (tmpl == gen_tmpl
18503 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18504 == spec)
18505 || fndecl == NULL_TREE);
18506
18507 if (spec != NULL_TREE)
18508 {
18509 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18510 {
18511 if (complain & tf_error)
18512 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18513 return error_mark_node;
18514 }
18515 return spec;
18516 }
18517
18518 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18519 complain))
18520 return error_mark_node;
18521
18522 /* We are building a FUNCTION_DECL, during which the access of its
18523 parameters and return types have to be checked. However this
18524 FUNCTION_DECL which is the desired context for access checking
18525 is not built yet. We solve this chicken-and-egg problem by
18526 deferring all checks until we have the FUNCTION_DECL. */
18527 push_deferring_access_checks (dk_deferred);
18528
18529 /* Instantiation of the function happens in the context of the function
18530 template, not the context of the overload resolution we're doing. */
18531 push_to_top_level ();
18532 /* If there are dependent arguments, e.g. because we're doing partial
18533 ordering, make sure processing_template_decl stays set. */
18534 if (uses_template_parms (targ_ptr))
18535 ++processing_template_decl;
18536 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18537 {
18538 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18539 complain, gen_tmpl, true);
18540 push_nested_class (ctx);
18541 }
18542
18543 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18544
18545 fndecl = NULL_TREE;
18546 if (VAR_P (pattern))
18547 {
18548 /* We need to determine if we're using a partial or explicit
18549 specialization now, because the type of the variable could be
18550 different. */
18551 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18552 tree elt = most_specialized_partial_spec (tid, complain);
18553 if (elt == error_mark_node)
18554 pattern = error_mark_node;
18555 else if (elt)
18556 {
18557 tree partial_tmpl = TREE_VALUE (elt);
18558 tree partial_args = TREE_PURPOSE (elt);
18559 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18560 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18561 }
18562 }
18563
18564 /* Substitute template parameters to obtain the specialization. */
18565 if (fndecl == NULL_TREE)
18566 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18567 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18568 pop_nested_class ();
18569 pop_from_top_level ();
18570
18571 if (fndecl == error_mark_node)
18572 {
18573 pop_deferring_access_checks ();
18574 return error_mark_node;
18575 }
18576
18577 /* The DECL_TI_TEMPLATE should always be the immediate parent
18578 template, not the most general template. */
18579 DECL_TI_TEMPLATE (fndecl) = tmpl;
18580 DECL_TI_ARGS (fndecl) = targ_ptr;
18581
18582 /* Now we know the specialization, compute access previously
18583 deferred. Do no access control for inheriting constructors,
18584 as we already checked access for the inherited constructor. */
18585 if (!(flag_new_inheriting_ctors
18586 && DECL_INHERITED_CTOR (fndecl)))
18587 {
18588 push_access_scope (fndecl);
18589 if (!perform_deferred_access_checks (complain))
18590 access_ok = false;
18591 pop_access_scope (fndecl);
18592 }
18593 pop_deferring_access_checks ();
18594
18595 /* If we've just instantiated the main entry point for a function,
18596 instantiate all the alternate entry points as well. We do this
18597 by cloning the instantiation of the main entry point, not by
18598 instantiating the template clones. */
18599 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18600 clone_function_decl (fndecl, /*update_methods=*/false);
18601
18602 if (!access_ok)
18603 {
18604 if (!(complain & tf_error))
18605 {
18606 /* Remember to reinstantiate when we're out of SFINAE so the user
18607 can see the errors. */
18608 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18609 }
18610 return error_mark_node;
18611 }
18612 return fndecl;
18613 }
18614
18615 /* Wrapper for instantiate_template_1. */
18616
18617 tree
18618 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18619 {
18620 tree ret;
18621 timevar_push (TV_TEMPLATE_INST);
18622 ret = instantiate_template_1 (tmpl, orig_args, complain);
18623 timevar_pop (TV_TEMPLATE_INST);
18624 return ret;
18625 }
18626
18627 /* Instantiate the alias template TMPL with ARGS. Also push a template
18628 instantiation level, which instantiate_template doesn't do because
18629 functions and variables have sufficient context established by the
18630 callers. */
18631
18632 static tree
18633 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18634 {
18635 struct pending_template *old_last_pend = last_pending_template;
18636 struct tinst_level *old_error_tinst = last_error_tinst_level;
18637 if (tmpl == error_mark_node || args == error_mark_node)
18638 return error_mark_node;
18639 tree tinst = build_tree_list (tmpl, args);
18640 if (!push_tinst_level (tinst))
18641 {
18642 ggc_free (tinst);
18643 return error_mark_node;
18644 }
18645
18646 args =
18647 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18648 args, tmpl, complain,
18649 /*require_all_args=*/true,
18650 /*use_default_args=*/true);
18651
18652 tree r = instantiate_template (tmpl, args, complain);
18653 pop_tinst_level ();
18654 /* We can't free this if a pending_template entry or last_error_tinst_level
18655 is pointing at it. */
18656 if (last_pending_template == old_last_pend
18657 && last_error_tinst_level == old_error_tinst)
18658 ggc_free (tinst);
18659
18660 return r;
18661 }
18662
18663 /* PARM is a template parameter pack for FN. Returns true iff
18664 PARM is used in a deducible way in the argument list of FN. */
18665
18666 static bool
18667 pack_deducible_p (tree parm, tree fn)
18668 {
18669 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18670 for (; t; t = TREE_CHAIN (t))
18671 {
18672 tree type = TREE_VALUE (t);
18673 tree packs;
18674 if (!PACK_EXPANSION_P (type))
18675 continue;
18676 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18677 packs; packs = TREE_CHAIN (packs))
18678 if (template_args_equal (TREE_VALUE (packs), parm))
18679 {
18680 /* The template parameter pack is used in a function parameter
18681 pack. If this is the end of the parameter list, the
18682 template parameter pack is deducible. */
18683 if (TREE_CHAIN (t) == void_list_node)
18684 return true;
18685 else
18686 /* Otherwise, not. Well, it could be deduced from
18687 a non-pack parameter, but doing so would end up with
18688 a deduction mismatch, so don't bother. */
18689 return false;
18690 }
18691 }
18692 /* The template parameter pack isn't used in any function parameter
18693 packs, but it might be used deeper, e.g. tuple<Args...>. */
18694 return true;
18695 }
18696
18697 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18698 NARGS elements of the arguments that are being used when calling
18699 it. TARGS is a vector into which the deduced template arguments
18700 are placed.
18701
18702 Returns either a FUNCTION_DECL for the matching specialization of FN or
18703 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18704 true, diagnostics will be printed to explain why it failed.
18705
18706 If FN is a conversion operator, or we are trying to produce a specific
18707 specialization, RETURN_TYPE is the return type desired.
18708
18709 The EXPLICIT_TARGS are explicit template arguments provided via a
18710 template-id.
18711
18712 The parameter STRICT is one of:
18713
18714 DEDUCE_CALL:
18715 We are deducing arguments for a function call, as in
18716 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18717 deducing arguments for a call to the result of a conversion
18718 function template, as in [over.call.object].
18719
18720 DEDUCE_CONV:
18721 We are deducing arguments for a conversion function, as in
18722 [temp.deduct.conv].
18723
18724 DEDUCE_EXACT:
18725 We are deducing arguments when doing an explicit instantiation
18726 as in [temp.explicit], when determining an explicit specialization
18727 as in [temp.expl.spec], or when taking the address of a function
18728 template, as in [temp.deduct.funcaddr]. */
18729
18730 tree
18731 fn_type_unification (tree fn,
18732 tree explicit_targs,
18733 tree targs,
18734 const tree *args,
18735 unsigned int nargs,
18736 tree return_type,
18737 unification_kind_t strict,
18738 int flags,
18739 bool explain_p,
18740 bool decltype_p)
18741 {
18742 tree parms;
18743 tree fntype;
18744 tree decl = NULL_TREE;
18745 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18746 bool ok;
18747 static int deduction_depth;
18748 struct pending_template *old_last_pend = last_pending_template;
18749 struct tinst_level *old_error_tinst = last_error_tinst_level;
18750
18751 tree orig_fn = fn;
18752 if (flag_new_inheriting_ctors)
18753 fn = strip_inheriting_ctors (fn);
18754
18755 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18756 tree tinst;
18757 tree r = error_mark_node;
18758
18759 tree full_targs = targs;
18760 if (TMPL_ARGS_DEPTH (targs)
18761 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18762 full_targs = (add_outermost_template_args
18763 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18764 targs));
18765
18766 if (decltype_p)
18767 complain |= tf_decltype;
18768
18769 /* In C++0x, it's possible to have a function template whose type depends
18770 on itself recursively. This is most obvious with decltype, but can also
18771 occur with enumeration scope (c++/48969). So we need to catch infinite
18772 recursion and reject the substitution at deduction time; this function
18773 will return error_mark_node for any repeated substitution.
18774
18775 This also catches excessive recursion such as when f<N> depends on
18776 f<N-1> across all integers, and returns error_mark_node for all the
18777 substitutions back up to the initial one.
18778
18779 This is, of course, not reentrant. */
18780 if (excessive_deduction_depth)
18781 return error_mark_node;
18782 tinst = build_tree_list (fn, NULL_TREE);
18783 ++deduction_depth;
18784
18785 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18786
18787 fntype = TREE_TYPE (fn);
18788 if (explicit_targs)
18789 {
18790 /* [temp.deduct]
18791
18792 The specified template arguments must match the template
18793 parameters in kind (i.e., type, nontype, template), and there
18794 must not be more arguments than there are parameters;
18795 otherwise type deduction fails.
18796
18797 Nontype arguments must match the types of the corresponding
18798 nontype template parameters, or must be convertible to the
18799 types of the corresponding nontype parameters as specified in
18800 _temp.arg.nontype_, otherwise type deduction fails.
18801
18802 All references in the function type of the function template
18803 to the corresponding template parameters are replaced by the
18804 specified template argument values. If a substitution in a
18805 template parameter or in the function type of the function
18806 template results in an invalid type, type deduction fails. */
18807 int i, len = TREE_VEC_LENGTH (tparms);
18808 location_t loc = input_location;
18809 bool incomplete = false;
18810
18811 if (explicit_targs == error_mark_node)
18812 goto fail;
18813
18814 if (TMPL_ARGS_DEPTH (explicit_targs)
18815 < TMPL_ARGS_DEPTH (full_targs))
18816 explicit_targs = add_outermost_template_args (full_targs,
18817 explicit_targs);
18818
18819 /* Adjust any explicit template arguments before entering the
18820 substitution context. */
18821 explicit_targs
18822 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18823 complain,
18824 /*require_all_args=*/false,
18825 /*use_default_args=*/false));
18826 if (explicit_targs == error_mark_node)
18827 goto fail;
18828
18829 /* Substitute the explicit args into the function type. This is
18830 necessary so that, for instance, explicitly declared function
18831 arguments can match null pointed constants. If we were given
18832 an incomplete set of explicit args, we must not do semantic
18833 processing during substitution as we could create partial
18834 instantiations. */
18835 for (i = 0; i < len; i++)
18836 {
18837 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18838 bool parameter_pack = false;
18839 tree targ = TREE_VEC_ELT (explicit_targs, i);
18840
18841 /* Dig out the actual parm. */
18842 if (TREE_CODE (parm) == TYPE_DECL
18843 || TREE_CODE (parm) == TEMPLATE_DECL)
18844 {
18845 parm = TREE_TYPE (parm);
18846 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18847 }
18848 else if (TREE_CODE (parm) == PARM_DECL)
18849 {
18850 parm = DECL_INITIAL (parm);
18851 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18852 }
18853
18854 if (!parameter_pack && targ == NULL_TREE)
18855 /* No explicit argument for this template parameter. */
18856 incomplete = true;
18857
18858 if (parameter_pack && pack_deducible_p (parm, fn))
18859 {
18860 /* Mark the argument pack as "incomplete". We could
18861 still deduce more arguments during unification.
18862 We remove this mark in type_unification_real. */
18863 if (targ)
18864 {
18865 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18866 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18867 = ARGUMENT_PACK_ARGS (targ);
18868 }
18869
18870 /* We have some incomplete argument packs. */
18871 incomplete = true;
18872 }
18873 }
18874
18875 TREE_VALUE (tinst) = explicit_targs;
18876 if (!push_tinst_level (tinst))
18877 {
18878 excessive_deduction_depth = true;
18879 goto fail;
18880 }
18881 processing_template_decl += incomplete;
18882 input_location = DECL_SOURCE_LOCATION (fn);
18883 /* Ignore any access checks; we'll see them again in
18884 instantiate_template and they might have the wrong
18885 access path at this point. */
18886 push_deferring_access_checks (dk_deferred);
18887 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18888 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18889 pop_deferring_access_checks ();
18890 input_location = loc;
18891 processing_template_decl -= incomplete;
18892 pop_tinst_level ();
18893
18894 if (fntype == error_mark_node)
18895 goto fail;
18896
18897 /* Place the explicitly specified arguments in TARGS. */
18898 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18899 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18900 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18901 }
18902
18903 /* Never do unification on the 'this' parameter. */
18904 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18905
18906 if (return_type && strict == DEDUCE_CALL)
18907 {
18908 /* We're deducing for a call to the result of a template conversion
18909 function. The parms we really want are in return_type. */
18910 if (POINTER_TYPE_P (return_type))
18911 return_type = TREE_TYPE (return_type);
18912 parms = TYPE_ARG_TYPES (return_type);
18913 }
18914 else if (return_type)
18915 {
18916 tree *new_args;
18917
18918 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18919 new_args = XALLOCAVEC (tree, nargs + 1);
18920 new_args[0] = return_type;
18921 memcpy (new_args + 1, args, nargs * sizeof (tree));
18922 args = new_args;
18923 ++nargs;
18924 }
18925
18926 /* We allow incomplete unification without an error message here
18927 because the standard doesn't seem to explicitly prohibit it. Our
18928 callers must be ready to deal with unification failures in any
18929 event. */
18930
18931 TREE_VALUE (tinst) = targs;
18932 /* If we aren't explaining yet, push tinst context so we can see where
18933 any errors (e.g. from class instantiations triggered by instantiation
18934 of default template arguments) come from. If we are explaining, this
18935 context is redundant. */
18936 if (!explain_p && !push_tinst_level (tinst))
18937 {
18938 excessive_deduction_depth = true;
18939 goto fail;
18940 }
18941
18942 /* type_unification_real will pass back any access checks from default
18943 template argument substitution. */
18944 vec<deferred_access_check, va_gc> *checks;
18945 checks = NULL;
18946
18947 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18948 full_targs, parms, args, nargs, /*subr=*/0,
18949 strict, flags, &checks, explain_p);
18950 if (!explain_p)
18951 pop_tinst_level ();
18952 if (!ok)
18953 goto fail;
18954
18955 /* Now that we have bindings for all of the template arguments,
18956 ensure that the arguments deduced for the template template
18957 parameters have compatible template parameter lists. We cannot
18958 check this property before we have deduced all template
18959 arguments, because the template parameter types of a template
18960 template parameter might depend on prior template parameters
18961 deduced after the template template parameter. The following
18962 ill-formed example illustrates this issue:
18963
18964 template<typename T, template<T> class C> void f(C<5>, T);
18965
18966 template<int N> struct X {};
18967
18968 void g() {
18969 f(X<5>(), 5l); // error: template argument deduction fails
18970 }
18971
18972 The template parameter list of 'C' depends on the template type
18973 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18974 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18975 time that we deduce 'C'. */
18976 if (!template_template_parm_bindings_ok_p
18977 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18978 {
18979 unify_inconsistent_template_template_parameters (explain_p);
18980 goto fail;
18981 }
18982
18983 /* All is well so far. Now, check:
18984
18985 [temp.deduct]
18986
18987 When all template arguments have been deduced, all uses of
18988 template parameters in nondeduced contexts are replaced with
18989 the corresponding deduced argument values. If the
18990 substitution results in an invalid type, as described above,
18991 type deduction fails. */
18992 TREE_VALUE (tinst) = targs;
18993 if (!push_tinst_level (tinst))
18994 {
18995 excessive_deduction_depth = true;
18996 goto fail;
18997 }
18998
18999 /* Also collect access checks from the instantiation. */
19000 reopen_deferring_access_checks (checks);
19001
19002 decl = instantiate_template (fn, targs, complain);
19003
19004 checks = get_deferred_access_checks ();
19005 pop_deferring_access_checks ();
19006
19007 pop_tinst_level ();
19008
19009 if (decl == error_mark_node)
19010 goto fail;
19011
19012 /* Now perform any access checks encountered during substitution. */
19013 push_access_scope (decl);
19014 ok = perform_access_checks (checks, complain);
19015 pop_access_scope (decl);
19016 if (!ok)
19017 goto fail;
19018
19019 /* If we're looking for an exact match, check that what we got
19020 is indeed an exact match. It might not be if some template
19021 parameters are used in non-deduced contexts. But don't check
19022 for an exact match if we have dependent template arguments;
19023 in that case we're doing partial ordering, and we already know
19024 that we have two candidates that will provide the actual type. */
19025 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19026 {
19027 tree substed = TREE_TYPE (decl);
19028 unsigned int i;
19029
19030 tree sarg
19031 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19032 if (return_type)
19033 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19034 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19035 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19036 {
19037 unify_type_mismatch (explain_p, args[i],
19038 TREE_VALUE (sarg));
19039 goto fail;
19040 }
19041 }
19042
19043 /* After doing deduction with the inherited constructor, actually return an
19044 instantiation of the inheriting constructor. */
19045 if (orig_fn != fn)
19046 decl = instantiate_template (orig_fn, targs, complain);
19047
19048 r = decl;
19049
19050 fail:
19051 --deduction_depth;
19052 if (excessive_deduction_depth)
19053 {
19054 if (deduction_depth == 0)
19055 /* Reset once we're all the way out. */
19056 excessive_deduction_depth = false;
19057 }
19058
19059 /* We can't free this if a pending_template entry or last_error_tinst_level
19060 is pointing at it. */
19061 if (last_pending_template == old_last_pend
19062 && last_error_tinst_level == old_error_tinst)
19063 ggc_free (tinst);
19064
19065 return r;
19066 }
19067
19068 /* Adjust types before performing type deduction, as described in
19069 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19070 sections are symmetric. PARM is the type of a function parameter
19071 or the return type of the conversion function. ARG is the type of
19072 the argument passed to the call, or the type of the value
19073 initialized with the result of the conversion function.
19074 ARG_EXPR is the original argument expression, which may be null. */
19075
19076 static int
19077 maybe_adjust_types_for_deduction (unification_kind_t strict,
19078 tree* parm,
19079 tree* arg,
19080 tree arg_expr)
19081 {
19082 int result = 0;
19083
19084 switch (strict)
19085 {
19086 case DEDUCE_CALL:
19087 break;
19088
19089 case DEDUCE_CONV:
19090 /* Swap PARM and ARG throughout the remainder of this
19091 function; the handling is precisely symmetric since PARM
19092 will initialize ARG rather than vice versa. */
19093 std::swap (parm, arg);
19094 break;
19095
19096 case DEDUCE_EXACT:
19097 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19098 too, but here handle it by stripping the reference from PARM
19099 rather than by adding it to ARG. */
19100 if (TREE_CODE (*parm) == REFERENCE_TYPE
19101 && TYPE_REF_IS_RVALUE (*parm)
19102 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19103 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19104 && TREE_CODE (*arg) == REFERENCE_TYPE
19105 && !TYPE_REF_IS_RVALUE (*arg))
19106 *parm = TREE_TYPE (*parm);
19107 /* Nothing else to do in this case. */
19108 return 0;
19109
19110 default:
19111 gcc_unreachable ();
19112 }
19113
19114 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19115 {
19116 /* [temp.deduct.call]
19117
19118 If P is not a reference type:
19119
19120 --If A is an array type, the pointer type produced by the
19121 array-to-pointer standard conversion (_conv.array_) is
19122 used in place of A for type deduction; otherwise,
19123
19124 --If A is a function type, the pointer type produced by
19125 the function-to-pointer standard conversion
19126 (_conv.func_) is used in place of A for type deduction;
19127 otherwise,
19128
19129 --If A is a cv-qualified type, the top level
19130 cv-qualifiers of A's type are ignored for type
19131 deduction. */
19132 if (TREE_CODE (*arg) == ARRAY_TYPE)
19133 *arg = build_pointer_type (TREE_TYPE (*arg));
19134 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19135 *arg = build_pointer_type (*arg);
19136 else
19137 *arg = TYPE_MAIN_VARIANT (*arg);
19138 }
19139
19140 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19141 reference to a cv-unqualified template parameter that does not represent a
19142 template parameter of a class template (during class template argument
19143 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19144 an lvalue, the type "lvalue reference to A" is used in place of A for type
19145 deduction. */
19146 if (TREE_CODE (*parm) == REFERENCE_TYPE
19147 && TYPE_REF_IS_RVALUE (*parm)
19148 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19149 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19150 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19151 && (arg_expr ? lvalue_p (arg_expr)
19152 /* try_one_overload doesn't provide an arg_expr, but
19153 functions are always lvalues. */
19154 : TREE_CODE (*arg) == FUNCTION_TYPE))
19155 *arg = build_reference_type (*arg);
19156
19157 /* [temp.deduct.call]
19158
19159 If P is a cv-qualified type, the top level cv-qualifiers
19160 of P's type are ignored for type deduction. If P is a
19161 reference type, the type referred to by P is used for
19162 type deduction. */
19163 *parm = TYPE_MAIN_VARIANT (*parm);
19164 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19165 {
19166 *parm = TREE_TYPE (*parm);
19167 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19168 }
19169
19170 /* DR 322. For conversion deduction, remove a reference type on parm
19171 too (which has been swapped into ARG). */
19172 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19173 *arg = TREE_TYPE (*arg);
19174
19175 return result;
19176 }
19177
19178 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19179 template which does contain any deducible template parameters; check if
19180 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19181 unify_one_argument. */
19182
19183 static int
19184 check_non_deducible_conversion (tree parm, tree arg, int strict,
19185 int flags, bool explain_p)
19186 {
19187 tree type;
19188
19189 if (!TYPE_P (arg))
19190 type = TREE_TYPE (arg);
19191 else
19192 type = arg;
19193
19194 if (same_type_p (parm, type))
19195 return unify_success (explain_p);
19196
19197 if (strict == DEDUCE_CONV)
19198 {
19199 if (can_convert_arg (type, parm, NULL_TREE, flags,
19200 explain_p ? tf_warning_or_error : tf_none))
19201 return unify_success (explain_p);
19202 }
19203 else if (strict != DEDUCE_EXACT)
19204 {
19205 if (can_convert_arg (parm, type,
19206 TYPE_P (arg) ? NULL_TREE : arg,
19207 flags, explain_p ? tf_warning_or_error : tf_none))
19208 return unify_success (explain_p);
19209 }
19210
19211 if (strict == DEDUCE_EXACT)
19212 return unify_type_mismatch (explain_p, parm, arg);
19213 else
19214 return unify_arg_conversion (explain_p, parm, type, arg);
19215 }
19216
19217 static bool uses_deducible_template_parms (tree type);
19218
19219 /* Returns true iff the expression EXPR is one from which a template
19220 argument can be deduced. In other words, if it's an undecorated
19221 use of a template non-type parameter. */
19222
19223 static bool
19224 deducible_expression (tree expr)
19225 {
19226 /* Strip implicit conversions. */
19227 while (CONVERT_EXPR_P (expr))
19228 expr = TREE_OPERAND (expr, 0);
19229 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19230 }
19231
19232 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19233 deducible way; that is, if it has a max value of <PARM> - 1. */
19234
19235 static bool
19236 deducible_array_bound (tree domain)
19237 {
19238 if (domain == NULL_TREE)
19239 return false;
19240
19241 tree max = TYPE_MAX_VALUE (domain);
19242 if (TREE_CODE (max) != MINUS_EXPR)
19243 return false;
19244
19245 return deducible_expression (TREE_OPERAND (max, 0));
19246 }
19247
19248 /* Returns true iff the template arguments ARGS use a template parameter
19249 in a deducible way. */
19250
19251 static bool
19252 deducible_template_args (tree args)
19253 {
19254 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19255 {
19256 bool deducible;
19257 tree elt = TREE_VEC_ELT (args, i);
19258 if (ARGUMENT_PACK_P (elt))
19259 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19260 else
19261 {
19262 if (PACK_EXPANSION_P (elt))
19263 elt = PACK_EXPANSION_PATTERN (elt);
19264 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19265 deducible = true;
19266 else if (TYPE_P (elt))
19267 deducible = uses_deducible_template_parms (elt);
19268 else
19269 deducible = deducible_expression (elt);
19270 }
19271 if (deducible)
19272 return true;
19273 }
19274 return false;
19275 }
19276
19277 /* Returns true iff TYPE contains any deducible references to template
19278 parameters, as per 14.8.2.5. */
19279
19280 static bool
19281 uses_deducible_template_parms (tree type)
19282 {
19283 if (PACK_EXPANSION_P (type))
19284 type = PACK_EXPANSION_PATTERN (type);
19285
19286 /* T
19287 cv-list T
19288 TT<T>
19289 TT<i>
19290 TT<> */
19291 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19292 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19293 return true;
19294
19295 /* T*
19296 T&
19297 T&& */
19298 if (POINTER_TYPE_P (type))
19299 return uses_deducible_template_parms (TREE_TYPE (type));
19300
19301 /* T[integer-constant ]
19302 type [i] */
19303 if (TREE_CODE (type) == ARRAY_TYPE)
19304 return (uses_deducible_template_parms (TREE_TYPE (type))
19305 || deducible_array_bound (TYPE_DOMAIN (type)));
19306
19307 /* T type ::*
19308 type T::*
19309 T T::*
19310 T (type ::*)()
19311 type (T::*)()
19312 type (type ::*)(T)
19313 type (T::*)(T)
19314 T (type ::*)(T)
19315 T (T::*)()
19316 T (T::*)(T) */
19317 if (TYPE_PTRMEM_P (type))
19318 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19319 || (uses_deducible_template_parms
19320 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19321
19322 /* template-name <T> (where template-name refers to a class template)
19323 template-name <i> (where template-name refers to a class template) */
19324 if (CLASS_TYPE_P (type)
19325 && CLASSTYPE_TEMPLATE_INFO (type)
19326 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19327 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19328 (CLASSTYPE_TI_ARGS (type)));
19329
19330 /* type (T)
19331 T()
19332 T(T) */
19333 if (TREE_CODE (type) == FUNCTION_TYPE
19334 || TREE_CODE (type) == METHOD_TYPE)
19335 {
19336 if (uses_deducible_template_parms (TREE_TYPE (type)))
19337 return true;
19338 tree parm = TYPE_ARG_TYPES (type);
19339 if (TREE_CODE (type) == METHOD_TYPE)
19340 parm = TREE_CHAIN (parm);
19341 for (; parm; parm = TREE_CHAIN (parm))
19342 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19343 return true;
19344 }
19345
19346 return false;
19347 }
19348
19349 /* Subroutine of type_unification_real and unify_pack_expansion to
19350 handle unification of a single P/A pair. Parameters are as
19351 for those functions. */
19352
19353 static int
19354 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19355 int subr, unification_kind_t strict,
19356 bool explain_p)
19357 {
19358 tree arg_expr = NULL_TREE;
19359 int arg_strict;
19360
19361 if (arg == error_mark_node || parm == error_mark_node)
19362 return unify_invalid (explain_p);
19363 if (arg == unknown_type_node)
19364 /* We can't deduce anything from this, but we might get all the
19365 template args from other function args. */
19366 return unify_success (explain_p);
19367
19368 /* Implicit conversions (Clause 4) will be performed on a function
19369 argument to convert it to the type of the corresponding function
19370 parameter if the parameter type contains no template-parameters that
19371 participate in template argument deduction. */
19372 if (strict != DEDUCE_EXACT
19373 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19374 /* For function parameters with no deducible template parameters,
19375 just return. We'll check non-dependent conversions later. */
19376 return unify_success (explain_p);
19377
19378 switch (strict)
19379 {
19380 case DEDUCE_CALL:
19381 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19382 | UNIFY_ALLOW_MORE_CV_QUAL
19383 | UNIFY_ALLOW_DERIVED);
19384 break;
19385
19386 case DEDUCE_CONV:
19387 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19388 break;
19389
19390 case DEDUCE_EXACT:
19391 arg_strict = UNIFY_ALLOW_NONE;
19392 break;
19393
19394 default:
19395 gcc_unreachable ();
19396 }
19397
19398 /* We only do these transformations if this is the top-level
19399 parameter_type_list in a call or declaration matching; in other
19400 situations (nested function declarators, template argument lists) we
19401 won't be comparing a type to an expression, and we don't do any type
19402 adjustments. */
19403 if (!subr)
19404 {
19405 if (!TYPE_P (arg))
19406 {
19407 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19408 if (type_unknown_p (arg))
19409 {
19410 /* [temp.deduct.type] A template-argument can be
19411 deduced from a pointer to function or pointer
19412 to member function argument if the set of
19413 overloaded functions does not contain function
19414 templates and at most one of a set of
19415 overloaded functions provides a unique
19416 match. */
19417 resolve_overloaded_unification (tparms, targs, parm,
19418 arg, strict,
19419 arg_strict, explain_p);
19420 /* If a unique match was not found, this is a
19421 non-deduced context, so we still succeed. */
19422 return unify_success (explain_p);
19423 }
19424
19425 arg_expr = arg;
19426 arg = unlowered_expr_type (arg);
19427 if (arg == error_mark_node)
19428 return unify_invalid (explain_p);
19429 }
19430
19431 arg_strict |=
19432 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19433 }
19434 else
19435 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19436 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19437 return unify_template_argument_mismatch (explain_p, parm, arg);
19438
19439 /* For deduction from an init-list we need the actual list. */
19440 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19441 arg = arg_expr;
19442 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19443 }
19444
19445 /* for_each_template_parm callback that always returns 0. */
19446
19447 static int
19448 zero_r (tree, void *)
19449 {
19450 return 0;
19451 }
19452
19453 /* for_each_template_parm any_fn callback to handle deduction of a template
19454 type argument from the type of an array bound. */
19455
19456 static int
19457 array_deduction_r (tree t, void *data)
19458 {
19459 tree_pair_p d = (tree_pair_p)data;
19460 tree &tparms = d->purpose;
19461 tree &targs = d->value;
19462
19463 if (TREE_CODE (t) == ARRAY_TYPE)
19464 if (tree dom = TYPE_DOMAIN (t))
19465 if (tree max = TYPE_MAX_VALUE (dom))
19466 {
19467 if (TREE_CODE (max) == MINUS_EXPR)
19468 max = TREE_OPERAND (max, 0);
19469 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19470 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19471 UNIFY_ALLOW_NONE, /*explain*/false);
19472 }
19473
19474 /* Keep walking. */
19475 return 0;
19476 }
19477
19478 /* Try to deduce any not-yet-deduced template type arguments from the type of
19479 an array bound. This is handled separately from unify because 14.8.2.5 says
19480 "The type of a type parameter is only deduced from an array bound if it is
19481 not otherwise deduced." */
19482
19483 static void
19484 try_array_deduction (tree tparms, tree targs, tree parm)
19485 {
19486 tree_pair_s data = { tparms, targs };
19487 hash_set<tree> visited;
19488 for_each_template_parm (parm, zero_r, &data, &visited,
19489 /*nondeduced*/false, array_deduction_r);
19490 }
19491
19492 /* Most parms like fn_type_unification.
19493
19494 If SUBR is 1, we're being called recursively (to unify the
19495 arguments of a function or method parameter of a function
19496 template).
19497
19498 CHECKS is a pointer to a vector of access checks encountered while
19499 substituting default template arguments. */
19500
19501 static int
19502 type_unification_real (tree tparms,
19503 tree full_targs,
19504 tree xparms,
19505 const tree *xargs,
19506 unsigned int xnargs,
19507 int subr,
19508 unification_kind_t strict,
19509 int flags,
19510 vec<deferred_access_check, va_gc> **checks,
19511 bool explain_p)
19512 {
19513 tree parm, arg;
19514 int i;
19515 int ntparms = TREE_VEC_LENGTH (tparms);
19516 int saw_undeduced = 0;
19517 tree parms;
19518 const tree *args;
19519 unsigned int nargs;
19520 unsigned int ia;
19521
19522 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19523 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19524 gcc_assert (ntparms > 0);
19525
19526 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19527
19528 /* Reset the number of non-defaulted template arguments contained
19529 in TARGS. */
19530 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19531
19532 again:
19533 parms = xparms;
19534 args = xargs;
19535 nargs = xnargs;
19536
19537 ia = 0;
19538 while (parms && parms != void_list_node
19539 && ia < nargs)
19540 {
19541 parm = TREE_VALUE (parms);
19542
19543 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19544 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19545 /* For a function parameter pack that occurs at the end of the
19546 parameter-declaration-list, the type A of each remaining
19547 argument of the call is compared with the type P of the
19548 declarator-id of the function parameter pack. */
19549 break;
19550
19551 parms = TREE_CHAIN (parms);
19552
19553 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19554 /* For a function parameter pack that does not occur at the
19555 end of the parameter-declaration-list, the type of the
19556 parameter pack is a non-deduced context. */
19557 continue;
19558
19559 arg = args[ia];
19560 ++ia;
19561
19562 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19563 explain_p))
19564 return 1;
19565 }
19566
19567 if (parms
19568 && parms != void_list_node
19569 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19570 {
19571 /* Unify the remaining arguments with the pack expansion type. */
19572 tree argvec;
19573 tree parmvec = make_tree_vec (1);
19574
19575 /* Allocate a TREE_VEC and copy in all of the arguments */
19576 argvec = make_tree_vec (nargs - ia);
19577 for (i = 0; ia < nargs; ++ia, ++i)
19578 TREE_VEC_ELT (argvec, i) = args[ia];
19579
19580 /* Copy the parameter into parmvec. */
19581 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19582 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19583 /*subr=*/subr, explain_p))
19584 return 1;
19585
19586 /* Advance to the end of the list of parameters. */
19587 parms = TREE_CHAIN (parms);
19588 }
19589
19590 /* Fail if we've reached the end of the parm list, and more args
19591 are present, and the parm list isn't variadic. */
19592 if (ia < nargs && parms == void_list_node)
19593 return unify_too_many_arguments (explain_p, nargs, ia);
19594 /* Fail if parms are left and they don't have default values and
19595 they aren't all deduced as empty packs (c++/57397). This is
19596 consistent with sufficient_parms_p. */
19597 if (parms && parms != void_list_node
19598 && TREE_PURPOSE (parms) == NULL_TREE)
19599 {
19600 unsigned int count = nargs;
19601 tree p = parms;
19602 bool type_pack_p;
19603 do
19604 {
19605 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19606 if (!type_pack_p)
19607 count++;
19608 p = TREE_CHAIN (p);
19609 }
19610 while (p && p != void_list_node);
19611 if (count != nargs)
19612 return unify_too_few_arguments (explain_p, ia, count,
19613 type_pack_p);
19614 }
19615
19616 if (!subr)
19617 {
19618 tsubst_flags_t complain = (explain_p
19619 ? tf_warning_or_error
19620 : tf_none);
19621 bool tried_array_deduction = (cxx_dialect < cxx17);
19622
19623 for (i = 0; i < ntparms; i++)
19624 {
19625 tree targ = TREE_VEC_ELT (targs, i);
19626 tree tparm = TREE_VEC_ELT (tparms, i);
19627
19628 /* Clear the "incomplete" flags on all argument packs now so that
19629 substituting them into later default arguments works. */
19630 if (targ && ARGUMENT_PACK_P (targ))
19631 {
19632 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19633 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19634 }
19635
19636 if (targ || tparm == error_mark_node)
19637 continue;
19638 tparm = TREE_VALUE (tparm);
19639
19640 if (TREE_CODE (tparm) == TYPE_DECL
19641 && !tried_array_deduction)
19642 {
19643 try_array_deduction (tparms, targs, xparms);
19644 tried_array_deduction = true;
19645 if (TREE_VEC_ELT (targs, i))
19646 continue;
19647 }
19648
19649 /* If this is an undeduced nontype parameter that depends on
19650 a type parameter, try another pass; its type may have been
19651 deduced from a later argument than the one from which
19652 this parameter can be deduced. */
19653 if (TREE_CODE (tparm) == PARM_DECL
19654 && uses_template_parms (TREE_TYPE (tparm))
19655 && saw_undeduced < 2)
19656 {
19657 saw_undeduced = 1;
19658 continue;
19659 }
19660
19661 /* Core issue #226 (C++0x) [temp.deduct]:
19662
19663 If a template argument has not been deduced, its
19664 default template argument, if any, is used.
19665
19666 When we are in C++98 mode, TREE_PURPOSE will either
19667 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19668 to explicitly check cxx_dialect here. */
19669 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19670 /* OK, there is a default argument. Wait until after the
19671 conversion check to do substitution. */
19672 continue;
19673
19674 /* If the type parameter is a parameter pack, then it will
19675 be deduced to an empty parameter pack. */
19676 if (template_parameter_pack_p (tparm))
19677 {
19678 tree arg;
19679
19680 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19681 {
19682 arg = make_node (NONTYPE_ARGUMENT_PACK);
19683 TREE_CONSTANT (arg) = 1;
19684 }
19685 else
19686 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19687
19688 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19689
19690 TREE_VEC_ELT (targs, i) = arg;
19691 continue;
19692 }
19693
19694 return unify_parameter_deduction_failure (explain_p, tparm);
19695 }
19696
19697 /* DR 1391: All parameters have args, now check non-dependent parms for
19698 convertibility. */
19699 if (saw_undeduced < 2)
19700 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19701 parms && parms != void_list_node && ia < nargs; )
19702 {
19703 parm = TREE_VALUE (parms);
19704
19705 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19706 && (!TREE_CHAIN (parms)
19707 || TREE_CHAIN (parms) == void_list_node))
19708 /* For a function parameter pack that occurs at the end of the
19709 parameter-declaration-list, the type A of each remaining
19710 argument of the call is compared with the type P of the
19711 declarator-id of the function parameter pack. */
19712 break;
19713
19714 parms = TREE_CHAIN (parms);
19715
19716 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19717 /* For a function parameter pack that does not occur at the
19718 end of the parameter-declaration-list, the type of the
19719 parameter pack is a non-deduced context. */
19720 continue;
19721
19722 arg = args[ia];
19723 ++ia;
19724
19725 if (uses_template_parms (parm))
19726 continue;
19727 if (check_non_deducible_conversion (parm, arg, strict, flags,
19728 explain_p))
19729 return 1;
19730 }
19731
19732 /* Now substitute into the default template arguments. */
19733 for (i = 0; i < ntparms; i++)
19734 {
19735 tree targ = TREE_VEC_ELT (targs, i);
19736 tree tparm = TREE_VEC_ELT (tparms, i);
19737
19738 if (targ || tparm == error_mark_node)
19739 continue;
19740 tree parm = TREE_VALUE (tparm);
19741
19742 if (TREE_CODE (parm) == PARM_DECL
19743 && uses_template_parms (TREE_TYPE (parm))
19744 && saw_undeduced < 2)
19745 continue;
19746
19747 tree arg = TREE_PURPOSE (tparm);
19748 reopen_deferring_access_checks (*checks);
19749 location_t save_loc = input_location;
19750 if (DECL_P (parm))
19751 input_location = DECL_SOURCE_LOCATION (parm);
19752 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19753 if (!uses_template_parms (arg))
19754 arg = convert_template_argument (parm, arg, full_targs, complain,
19755 i, NULL_TREE);
19756 else if (saw_undeduced < 2)
19757 arg = NULL_TREE;
19758 else
19759 arg = error_mark_node;
19760 input_location = save_loc;
19761 *checks = get_deferred_access_checks ();
19762 pop_deferring_access_checks ();
19763 if (arg == error_mark_node)
19764 return 1;
19765 else if (arg)
19766 {
19767 TREE_VEC_ELT (targs, i) = arg;
19768 /* The position of the first default template argument,
19769 is also the number of non-defaulted arguments in TARGS.
19770 Record that. */
19771 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19772 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19773 }
19774 }
19775
19776 if (saw_undeduced++ == 1)
19777 goto again;
19778 }
19779
19780 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19781 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19782
19783 return unify_success (explain_p);
19784 }
19785
19786 /* Subroutine of type_unification_real. Args are like the variables
19787 at the call site. ARG is an overloaded function (or template-id);
19788 we try deducing template args from each of the overloads, and if
19789 only one succeeds, we go with that. Modifies TARGS and returns
19790 true on success. */
19791
19792 static bool
19793 resolve_overloaded_unification (tree tparms,
19794 tree targs,
19795 tree parm,
19796 tree arg,
19797 unification_kind_t strict,
19798 int sub_strict,
19799 bool explain_p)
19800 {
19801 tree tempargs = copy_node (targs);
19802 int good = 0;
19803 tree goodfn = NULL_TREE;
19804 bool addr_p;
19805
19806 if (TREE_CODE (arg) == ADDR_EXPR)
19807 {
19808 arg = TREE_OPERAND (arg, 0);
19809 addr_p = true;
19810 }
19811 else
19812 addr_p = false;
19813
19814 if (TREE_CODE (arg) == COMPONENT_REF)
19815 /* Handle `&x' where `x' is some static or non-static member
19816 function name. */
19817 arg = TREE_OPERAND (arg, 1);
19818
19819 if (TREE_CODE (arg) == OFFSET_REF)
19820 arg = TREE_OPERAND (arg, 1);
19821
19822 /* Strip baselink information. */
19823 if (BASELINK_P (arg))
19824 arg = BASELINK_FUNCTIONS (arg);
19825
19826 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19827 {
19828 /* If we got some explicit template args, we need to plug them into
19829 the affected templates before we try to unify, in case the
19830 explicit args will completely resolve the templates in question. */
19831
19832 int ok = 0;
19833 tree expl_subargs = TREE_OPERAND (arg, 1);
19834 arg = TREE_OPERAND (arg, 0);
19835
19836 for (lkp_iterator iter (arg); iter; ++iter)
19837 {
19838 tree fn = *iter;
19839 tree subargs, elem;
19840
19841 if (TREE_CODE (fn) != TEMPLATE_DECL)
19842 continue;
19843
19844 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19845 expl_subargs, NULL_TREE, tf_none,
19846 /*require_all_args=*/true,
19847 /*use_default_args=*/true);
19848 if (subargs != error_mark_node
19849 && !any_dependent_template_arguments_p (subargs))
19850 {
19851 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19852 if (try_one_overload (tparms, targs, tempargs, parm,
19853 elem, strict, sub_strict, addr_p, explain_p)
19854 && (!goodfn || !same_type_p (goodfn, elem)))
19855 {
19856 goodfn = elem;
19857 ++good;
19858 }
19859 }
19860 else if (subargs)
19861 ++ok;
19862 }
19863 /* If no templates (or more than one) are fully resolved by the
19864 explicit arguments, this template-id is a non-deduced context; it
19865 could still be OK if we deduce all template arguments for the
19866 enclosing call through other arguments. */
19867 if (good != 1)
19868 good = ok;
19869 }
19870 else if (TREE_CODE (arg) != OVERLOAD
19871 && TREE_CODE (arg) != FUNCTION_DECL)
19872 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19873 -- but the deduction does not succeed because the expression is
19874 not just the function on its own. */
19875 return false;
19876 else
19877 for (lkp_iterator iter (arg); iter; ++iter)
19878 {
19879 tree fn = *iter;
19880 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19881 strict, sub_strict, addr_p, explain_p)
19882 && (!goodfn || !decls_match (goodfn, fn)))
19883 {
19884 goodfn = fn;
19885 ++good;
19886 }
19887 }
19888
19889 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19890 to function or pointer to member function argument if the set of
19891 overloaded functions does not contain function templates and at most
19892 one of a set of overloaded functions provides a unique match.
19893
19894 So if we found multiple possibilities, we return success but don't
19895 deduce anything. */
19896
19897 if (good == 1)
19898 {
19899 int i = TREE_VEC_LENGTH (targs);
19900 for (; i--; )
19901 if (TREE_VEC_ELT (tempargs, i))
19902 {
19903 tree old = TREE_VEC_ELT (targs, i);
19904 tree new_ = TREE_VEC_ELT (tempargs, i);
19905 if (new_ && old && ARGUMENT_PACK_P (old)
19906 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19907 /* Don't forget explicit template arguments in a pack. */
19908 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19909 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19910 TREE_VEC_ELT (targs, i) = new_;
19911 }
19912 }
19913 if (good)
19914 return true;
19915
19916 return false;
19917 }
19918
19919 /* Core DR 115: In contexts where deduction is done and fails, or in
19920 contexts where deduction is not done, if a template argument list is
19921 specified and it, along with any default template arguments, identifies
19922 a single function template specialization, then the template-id is an
19923 lvalue for the function template specialization. */
19924
19925 tree
19926 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19927 {
19928 tree expr, offset, baselink;
19929 bool addr;
19930
19931 if (!type_unknown_p (orig_expr))
19932 return orig_expr;
19933
19934 expr = orig_expr;
19935 addr = false;
19936 offset = NULL_TREE;
19937 baselink = NULL_TREE;
19938
19939 if (TREE_CODE (expr) == ADDR_EXPR)
19940 {
19941 expr = TREE_OPERAND (expr, 0);
19942 addr = true;
19943 }
19944 if (TREE_CODE (expr) == OFFSET_REF)
19945 {
19946 offset = expr;
19947 expr = TREE_OPERAND (expr, 1);
19948 }
19949 if (BASELINK_P (expr))
19950 {
19951 baselink = expr;
19952 expr = BASELINK_FUNCTIONS (expr);
19953 }
19954
19955 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19956 {
19957 int good = 0;
19958 tree goodfn = NULL_TREE;
19959
19960 /* If we got some explicit template args, we need to plug them into
19961 the affected templates before we try to unify, in case the
19962 explicit args will completely resolve the templates in question. */
19963
19964 tree expl_subargs = TREE_OPERAND (expr, 1);
19965 tree arg = TREE_OPERAND (expr, 0);
19966 tree badfn = NULL_TREE;
19967 tree badargs = NULL_TREE;
19968
19969 for (lkp_iterator iter (arg); iter; ++iter)
19970 {
19971 tree fn = *iter;
19972 tree subargs, elem;
19973
19974 if (TREE_CODE (fn) != TEMPLATE_DECL)
19975 continue;
19976
19977 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19978 expl_subargs, NULL_TREE, tf_none,
19979 /*require_all_args=*/true,
19980 /*use_default_args=*/true);
19981 if (subargs != error_mark_node
19982 && !any_dependent_template_arguments_p (subargs))
19983 {
19984 elem = instantiate_template (fn, subargs, tf_none);
19985 if (elem == error_mark_node)
19986 {
19987 badfn = fn;
19988 badargs = subargs;
19989 }
19990 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19991 {
19992 goodfn = elem;
19993 ++good;
19994 }
19995 }
19996 }
19997 if (good == 1)
19998 {
19999 mark_used (goodfn);
20000 expr = goodfn;
20001 if (baselink)
20002 expr = build_baselink (BASELINK_BINFO (baselink),
20003 BASELINK_ACCESS_BINFO (baselink),
20004 expr, BASELINK_OPTYPE (baselink));
20005 if (offset)
20006 {
20007 tree base
20008 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20009 expr = build_offset_ref (base, expr, addr, complain);
20010 }
20011 if (addr)
20012 expr = cp_build_addr_expr (expr, complain);
20013 return expr;
20014 }
20015 else if (good == 0 && badargs && (complain & tf_error))
20016 /* There were no good options and at least one bad one, so let the
20017 user know what the problem is. */
20018 instantiate_template (badfn, badargs, complain);
20019 }
20020 return orig_expr;
20021 }
20022
20023 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20024 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20025 different overloads deduce different arguments for a given parm.
20026 ADDR_P is true if the expression for which deduction is being
20027 performed was of the form "& fn" rather than simply "fn".
20028
20029 Returns 1 on success. */
20030
20031 static int
20032 try_one_overload (tree tparms,
20033 tree orig_targs,
20034 tree targs,
20035 tree parm,
20036 tree arg,
20037 unification_kind_t strict,
20038 int sub_strict,
20039 bool addr_p,
20040 bool explain_p)
20041 {
20042 int nargs;
20043 tree tempargs;
20044 int i;
20045
20046 if (arg == error_mark_node)
20047 return 0;
20048
20049 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20050 to function or pointer to member function argument if the set of
20051 overloaded functions does not contain function templates and at most
20052 one of a set of overloaded functions provides a unique match.
20053
20054 So if this is a template, just return success. */
20055
20056 if (uses_template_parms (arg))
20057 return 1;
20058
20059 if (TREE_CODE (arg) == METHOD_TYPE)
20060 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20061 else if (addr_p)
20062 arg = build_pointer_type (arg);
20063
20064 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20065
20066 /* We don't copy orig_targs for this because if we have already deduced
20067 some template args from previous args, unify would complain when we
20068 try to deduce a template parameter for the same argument, even though
20069 there isn't really a conflict. */
20070 nargs = TREE_VEC_LENGTH (targs);
20071 tempargs = make_tree_vec (nargs);
20072
20073 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20074 return 0;
20075
20076 /* First make sure we didn't deduce anything that conflicts with
20077 explicitly specified args. */
20078 for (i = nargs; i--; )
20079 {
20080 tree elt = TREE_VEC_ELT (tempargs, i);
20081 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20082
20083 if (!elt)
20084 /*NOP*/;
20085 else if (uses_template_parms (elt))
20086 /* Since we're unifying against ourselves, we will fill in
20087 template args used in the function parm list with our own
20088 template parms. Discard them. */
20089 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20090 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20091 {
20092 /* Check that the argument at each index of the deduced argument pack
20093 is equivalent to the corresponding explicitly specified argument.
20094 We may have deduced more arguments than were explicitly specified,
20095 and that's OK. */
20096
20097 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20098 that's wrong if we deduce the same argument pack from multiple
20099 function arguments: it's only incomplete the first time. */
20100
20101 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20102 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20103
20104 if (TREE_VEC_LENGTH (deduced_pack)
20105 < TREE_VEC_LENGTH (explicit_pack))
20106 return 0;
20107
20108 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20109 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20110 TREE_VEC_ELT (deduced_pack, j)))
20111 return 0;
20112 }
20113 else if (oldelt && !template_args_equal (oldelt, elt))
20114 return 0;
20115 }
20116
20117 for (i = nargs; i--; )
20118 {
20119 tree elt = TREE_VEC_ELT (tempargs, i);
20120
20121 if (elt)
20122 TREE_VEC_ELT (targs, i) = elt;
20123 }
20124
20125 return 1;
20126 }
20127
20128 /* PARM is a template class (perhaps with unbound template
20129 parameters). ARG is a fully instantiated type. If ARG can be
20130 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20131 TARGS are as for unify. */
20132
20133 static tree
20134 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20135 bool explain_p)
20136 {
20137 tree copy_of_targs;
20138
20139 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20140 return NULL_TREE;
20141 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20142 /* Matches anything. */;
20143 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20144 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20145 return NULL_TREE;
20146
20147 /* We need to make a new template argument vector for the call to
20148 unify. If we used TARGS, we'd clutter it up with the result of
20149 the attempted unification, even if this class didn't work out.
20150 We also don't want to commit ourselves to all the unifications
20151 we've already done, since unification is supposed to be done on
20152 an argument-by-argument basis. In other words, consider the
20153 following pathological case:
20154
20155 template <int I, int J, int K>
20156 struct S {};
20157
20158 template <int I, int J>
20159 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20160
20161 template <int I, int J, int K>
20162 void f(S<I, J, K>, S<I, I, I>);
20163
20164 void g() {
20165 S<0, 0, 0> s0;
20166 S<0, 1, 2> s2;
20167
20168 f(s0, s2);
20169 }
20170
20171 Now, by the time we consider the unification involving `s2', we
20172 already know that we must have `f<0, 0, 0>'. But, even though
20173 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20174 because there are two ways to unify base classes of S<0, 1, 2>
20175 with S<I, I, I>. If we kept the already deduced knowledge, we
20176 would reject the possibility I=1. */
20177 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20178
20179 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20180 {
20181 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20182 return NULL_TREE;
20183 return arg;
20184 }
20185
20186 /* If unification failed, we're done. */
20187 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20188 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20189 return NULL_TREE;
20190
20191 return arg;
20192 }
20193
20194 /* Given a template type PARM and a class type ARG, find the unique
20195 base type in ARG that is an instance of PARM. We do not examine
20196 ARG itself; only its base-classes. If there is not exactly one
20197 appropriate base class, return NULL_TREE. PARM may be the type of
20198 a partial specialization, as well as a plain template type. Used
20199 by unify. */
20200
20201 static enum template_base_result
20202 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20203 bool explain_p, tree *result)
20204 {
20205 tree rval = NULL_TREE;
20206 tree binfo;
20207
20208 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20209
20210 binfo = TYPE_BINFO (complete_type (arg));
20211 if (!binfo)
20212 {
20213 /* The type could not be completed. */
20214 *result = NULL_TREE;
20215 return tbr_incomplete_type;
20216 }
20217
20218 /* Walk in inheritance graph order. The search order is not
20219 important, and this avoids multiple walks of virtual bases. */
20220 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20221 {
20222 tree r = try_class_unification (tparms, targs, parm,
20223 BINFO_TYPE (binfo), explain_p);
20224
20225 if (r)
20226 {
20227 /* If there is more than one satisfactory baseclass, then:
20228
20229 [temp.deduct.call]
20230
20231 If they yield more than one possible deduced A, the type
20232 deduction fails.
20233
20234 applies. */
20235 if (rval && !same_type_p (r, rval))
20236 {
20237 *result = NULL_TREE;
20238 return tbr_ambiguous_baseclass;
20239 }
20240
20241 rval = r;
20242 }
20243 }
20244
20245 *result = rval;
20246 return tbr_success;
20247 }
20248
20249 /* Returns the level of DECL, which declares a template parameter. */
20250
20251 static int
20252 template_decl_level (tree decl)
20253 {
20254 switch (TREE_CODE (decl))
20255 {
20256 case TYPE_DECL:
20257 case TEMPLATE_DECL:
20258 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20259
20260 case PARM_DECL:
20261 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20262
20263 default:
20264 gcc_unreachable ();
20265 }
20266 return 0;
20267 }
20268
20269 /* Decide whether ARG can be unified with PARM, considering only the
20270 cv-qualifiers of each type, given STRICT as documented for unify.
20271 Returns nonzero iff the unification is OK on that basis. */
20272
20273 static int
20274 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20275 {
20276 int arg_quals = cp_type_quals (arg);
20277 int parm_quals = cp_type_quals (parm);
20278
20279 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20280 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20281 {
20282 /* Although a CVR qualifier is ignored when being applied to a
20283 substituted template parameter ([8.3.2]/1 for example), that
20284 does not allow us to unify "const T" with "int&" because both
20285 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20286 It is ok when we're allowing additional CV qualifiers
20287 at the outer level [14.8.2.1]/3,1st bullet. */
20288 if ((TREE_CODE (arg) == REFERENCE_TYPE
20289 || TREE_CODE (arg) == FUNCTION_TYPE
20290 || TREE_CODE (arg) == METHOD_TYPE)
20291 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20292 return 0;
20293
20294 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20295 && (parm_quals & TYPE_QUAL_RESTRICT))
20296 return 0;
20297 }
20298
20299 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20300 && (arg_quals & parm_quals) != parm_quals)
20301 return 0;
20302
20303 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20304 && (parm_quals & arg_quals) != arg_quals)
20305 return 0;
20306
20307 return 1;
20308 }
20309
20310 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20311 void
20312 template_parm_level_and_index (tree parm, int* level, int* index)
20313 {
20314 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20315 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20316 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20317 {
20318 *index = TEMPLATE_TYPE_IDX (parm);
20319 *level = TEMPLATE_TYPE_LEVEL (parm);
20320 }
20321 else
20322 {
20323 *index = TEMPLATE_PARM_IDX (parm);
20324 *level = TEMPLATE_PARM_LEVEL (parm);
20325 }
20326 }
20327
20328 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20329 do { \
20330 if (unify (TP, TA, P, A, S, EP)) \
20331 return 1; \
20332 } while (0)
20333
20334 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20335 expansion at the end of PACKED_PARMS. Returns 0 if the type
20336 deduction succeeds, 1 otherwise. STRICT is the same as in
20337 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20338 function call argument list. We'll need to adjust the arguments to make them
20339 types. SUBR tells us if this is from a recursive call to
20340 type_unification_real, or for comparing two template argument
20341 lists. */
20342
20343 static int
20344 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20345 tree packed_args, unification_kind_t strict,
20346 bool subr, bool explain_p)
20347 {
20348 tree parm
20349 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20350 tree pattern = PACK_EXPANSION_PATTERN (parm);
20351 tree pack, packs = NULL_TREE;
20352 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20353
20354 /* Add in any args remembered from an earlier partial instantiation. */
20355 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20356
20357 packed_args = expand_template_argument_pack (packed_args);
20358
20359 int len = TREE_VEC_LENGTH (packed_args);
20360
20361 /* Determine the parameter packs we will be deducing from the
20362 pattern, and record their current deductions. */
20363 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20364 pack; pack = TREE_CHAIN (pack))
20365 {
20366 tree parm_pack = TREE_VALUE (pack);
20367 int idx, level;
20368
20369 /* Determine the index and level of this parameter pack. */
20370 template_parm_level_and_index (parm_pack, &level, &idx);
20371
20372 /* Keep track of the parameter packs and their corresponding
20373 argument packs. */
20374 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20375 TREE_TYPE (packs) = make_tree_vec (len - start);
20376 }
20377
20378 /* Loop through all of the arguments that have not yet been
20379 unified and unify each with the pattern. */
20380 for (i = start; i < len; i++)
20381 {
20382 tree parm;
20383 bool any_explicit = false;
20384 tree arg = TREE_VEC_ELT (packed_args, i);
20385
20386 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20387 or the element of its argument pack at the current index if
20388 this argument was explicitly specified. */
20389 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20390 {
20391 int idx, level;
20392 tree arg, pargs;
20393 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20394
20395 arg = NULL_TREE;
20396 if (TREE_VALUE (pack)
20397 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20398 && (i - start < TREE_VEC_LENGTH (pargs)))
20399 {
20400 any_explicit = true;
20401 arg = TREE_VEC_ELT (pargs, i - start);
20402 }
20403 TMPL_ARG (targs, level, idx) = arg;
20404 }
20405
20406 /* If we had explicit template arguments, substitute them into the
20407 pattern before deduction. */
20408 if (any_explicit)
20409 {
20410 /* Some arguments might still be unspecified or dependent. */
20411 bool dependent;
20412 ++processing_template_decl;
20413 dependent = any_dependent_template_arguments_p (targs);
20414 if (!dependent)
20415 --processing_template_decl;
20416 parm = tsubst (pattern, targs,
20417 explain_p ? tf_warning_or_error : tf_none,
20418 NULL_TREE);
20419 if (dependent)
20420 --processing_template_decl;
20421 if (parm == error_mark_node)
20422 return 1;
20423 }
20424 else
20425 parm = pattern;
20426
20427 /* Unify the pattern with the current argument. */
20428 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20429 explain_p))
20430 return 1;
20431
20432 /* For each parameter pack, collect the deduced value. */
20433 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20434 {
20435 int idx, level;
20436 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20437
20438 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20439 TMPL_ARG (targs, level, idx);
20440 }
20441 }
20442
20443 /* Verify that the results of unification with the parameter packs
20444 produce results consistent with what we've seen before, and make
20445 the deduced argument packs available. */
20446 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20447 {
20448 tree old_pack = TREE_VALUE (pack);
20449 tree new_args = TREE_TYPE (pack);
20450 int i, len = TREE_VEC_LENGTH (new_args);
20451 int idx, level;
20452 bool nondeduced_p = false;
20453
20454 /* By default keep the original deduced argument pack.
20455 If necessary, more specific code is going to update the
20456 resulting deduced argument later down in this function. */
20457 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20458 TMPL_ARG (targs, level, idx) = old_pack;
20459
20460 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20461 actually deduce anything. */
20462 for (i = 0; i < len && !nondeduced_p; ++i)
20463 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20464 nondeduced_p = true;
20465 if (nondeduced_p)
20466 continue;
20467
20468 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20469 {
20470 /* If we had fewer function args than explicit template args,
20471 just use the explicits. */
20472 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20473 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20474 if (len < explicit_len)
20475 new_args = explicit_args;
20476 }
20477
20478 if (!old_pack)
20479 {
20480 tree result;
20481 /* Build the deduced *_ARGUMENT_PACK. */
20482 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20483 {
20484 result = make_node (NONTYPE_ARGUMENT_PACK);
20485 TREE_CONSTANT (result) = 1;
20486 }
20487 else
20488 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20489
20490 SET_ARGUMENT_PACK_ARGS (result, new_args);
20491
20492 /* Note the deduced argument packs for this parameter
20493 pack. */
20494 TMPL_ARG (targs, level, idx) = result;
20495 }
20496 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20497 && (ARGUMENT_PACK_ARGS (old_pack)
20498 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20499 {
20500 /* We only had the explicitly-provided arguments before, but
20501 now we have a complete set of arguments. */
20502 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20503
20504 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20505 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20506 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20507 }
20508 else
20509 {
20510 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20511 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20512
20513 if (!comp_template_args (old_args, new_args,
20514 &bad_old_arg, &bad_new_arg))
20515 /* Inconsistent unification of this parameter pack. */
20516 return unify_parameter_pack_inconsistent (explain_p,
20517 bad_old_arg,
20518 bad_new_arg);
20519 }
20520 }
20521
20522 return unify_success (explain_p);
20523 }
20524
20525 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20526 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20527 parameters and return value are as for unify. */
20528
20529 static int
20530 unify_array_domain (tree tparms, tree targs,
20531 tree parm_dom, tree arg_dom,
20532 bool explain_p)
20533 {
20534 tree parm_max;
20535 tree arg_max;
20536 bool parm_cst;
20537 bool arg_cst;
20538
20539 /* Our representation of array types uses "N - 1" as the
20540 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20541 not an integer constant. We cannot unify arbitrarily
20542 complex expressions, so we eliminate the MINUS_EXPRs
20543 here. */
20544 parm_max = TYPE_MAX_VALUE (parm_dom);
20545 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20546 if (!parm_cst)
20547 {
20548 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20549 parm_max = TREE_OPERAND (parm_max, 0);
20550 }
20551 arg_max = TYPE_MAX_VALUE (arg_dom);
20552 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20553 if (!arg_cst)
20554 {
20555 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20556 trying to unify the type of a variable with the type
20557 of a template parameter. For example:
20558
20559 template <unsigned int N>
20560 void f (char (&) [N]);
20561 int g();
20562 void h(int i) {
20563 char a[g(i)];
20564 f(a);
20565 }
20566
20567 Here, the type of the ARG will be "int [g(i)]", and
20568 may be a SAVE_EXPR, etc. */
20569 if (TREE_CODE (arg_max) != MINUS_EXPR)
20570 return unify_vla_arg (explain_p, arg_dom);
20571 arg_max = TREE_OPERAND (arg_max, 0);
20572 }
20573
20574 /* If only one of the bounds used a MINUS_EXPR, compensate
20575 by adding one to the other bound. */
20576 if (parm_cst && !arg_cst)
20577 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20578 integer_type_node,
20579 parm_max,
20580 integer_one_node);
20581 else if (arg_cst && !parm_cst)
20582 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20583 integer_type_node,
20584 arg_max,
20585 integer_one_node);
20586
20587 return unify (tparms, targs, parm_max, arg_max,
20588 UNIFY_ALLOW_INTEGER, explain_p);
20589 }
20590
20591 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20592
20593 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20594
20595 static pa_kind_t
20596 pa_kind (tree t)
20597 {
20598 if (PACK_EXPANSION_P (t))
20599 t = PACK_EXPANSION_PATTERN (t);
20600 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20601 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20602 || DECL_TYPE_TEMPLATE_P (t))
20603 return pa_tmpl;
20604 else if (TYPE_P (t))
20605 return pa_type;
20606 else
20607 return pa_expr;
20608 }
20609
20610 /* Deduce the value of template parameters. TPARMS is the (innermost)
20611 set of template parameters to a template. TARGS is the bindings
20612 for those template parameters, as determined thus far; TARGS may
20613 include template arguments for outer levels of template parameters
20614 as well. PARM is a parameter to a template function, or a
20615 subcomponent of that parameter; ARG is the corresponding argument.
20616 This function attempts to match PARM with ARG in a manner
20617 consistent with the existing assignments in TARGS. If more values
20618 are deduced, then TARGS is updated.
20619
20620 Returns 0 if the type deduction succeeds, 1 otherwise. The
20621 parameter STRICT is a bitwise or of the following flags:
20622
20623 UNIFY_ALLOW_NONE:
20624 Require an exact match between PARM and ARG.
20625 UNIFY_ALLOW_MORE_CV_QUAL:
20626 Allow the deduced ARG to be more cv-qualified (by qualification
20627 conversion) than ARG.
20628 UNIFY_ALLOW_LESS_CV_QUAL:
20629 Allow the deduced ARG to be less cv-qualified than ARG.
20630 UNIFY_ALLOW_DERIVED:
20631 Allow the deduced ARG to be a template base class of ARG,
20632 or a pointer to a template base class of the type pointed to by
20633 ARG.
20634 UNIFY_ALLOW_INTEGER:
20635 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20636 case for more information.
20637 UNIFY_ALLOW_OUTER_LEVEL:
20638 This is the outermost level of a deduction. Used to determine validity
20639 of qualification conversions. A valid qualification conversion must
20640 have const qualified pointers leading up to the inner type which
20641 requires additional CV quals, except at the outer level, where const
20642 is not required [conv.qual]. It would be normal to set this flag in
20643 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20644 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20645 This is the outermost level of a deduction, and PARM can be more CV
20646 qualified at this point.
20647 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20648 This is the outermost level of a deduction, and PARM can be less CV
20649 qualified at this point. */
20650
20651 static int
20652 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20653 bool explain_p)
20654 {
20655 int idx;
20656 tree targ;
20657 tree tparm;
20658 int strict_in = strict;
20659 tsubst_flags_t complain = (explain_p
20660 ? tf_warning_or_error
20661 : tf_none);
20662
20663 /* I don't think this will do the right thing with respect to types.
20664 But the only case I've seen it in so far has been array bounds, where
20665 signedness is the only information lost, and I think that will be
20666 okay. */
20667 while (CONVERT_EXPR_P (parm))
20668 parm = TREE_OPERAND (parm, 0);
20669
20670 if (arg == error_mark_node)
20671 return unify_invalid (explain_p);
20672 if (arg == unknown_type_node
20673 || arg == init_list_type_node)
20674 /* We can't deduce anything from this, but we might get all the
20675 template args from other function args. */
20676 return unify_success (explain_p);
20677
20678 if (parm == any_targ_node || arg == any_targ_node)
20679 return unify_success (explain_p);
20680
20681 /* If PARM uses template parameters, then we can't bail out here,
20682 even if ARG == PARM, since we won't record unifications for the
20683 template parameters. We might need them if we're trying to
20684 figure out which of two things is more specialized. */
20685 if (arg == parm && !uses_template_parms (parm))
20686 return unify_success (explain_p);
20687
20688 /* Handle init lists early, so the rest of the function can assume
20689 we're dealing with a type. */
20690 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20691 {
20692 tree elt, elttype;
20693 unsigned i;
20694 tree orig_parm = parm;
20695
20696 /* Replace T with std::initializer_list<T> for deduction. */
20697 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20698 && flag_deduce_init_list)
20699 parm = listify (parm);
20700
20701 if (!is_std_init_list (parm)
20702 && TREE_CODE (parm) != ARRAY_TYPE)
20703 /* We can only deduce from an initializer list argument if the
20704 parameter is std::initializer_list or an array; otherwise this
20705 is a non-deduced context. */
20706 return unify_success (explain_p);
20707
20708 if (TREE_CODE (parm) == ARRAY_TYPE)
20709 elttype = TREE_TYPE (parm);
20710 else
20711 {
20712 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20713 /* Deduction is defined in terms of a single type, so just punt
20714 on the (bizarre) std::initializer_list<T...>. */
20715 if (PACK_EXPANSION_P (elttype))
20716 return unify_success (explain_p);
20717 }
20718
20719 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20720 {
20721 int elt_strict = strict;
20722
20723 if (elt == error_mark_node)
20724 return unify_invalid (explain_p);
20725
20726 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20727 {
20728 tree type = TREE_TYPE (elt);
20729 if (type == error_mark_node)
20730 return unify_invalid (explain_p);
20731 /* It should only be possible to get here for a call. */
20732 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20733 elt_strict |= maybe_adjust_types_for_deduction
20734 (DEDUCE_CALL, &elttype, &type, elt);
20735 elt = type;
20736 }
20737
20738 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20739 explain_p);
20740 }
20741
20742 if (TREE_CODE (parm) == ARRAY_TYPE
20743 && deducible_array_bound (TYPE_DOMAIN (parm)))
20744 {
20745 /* Also deduce from the length of the initializer list. */
20746 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20747 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20748 if (idx == error_mark_node)
20749 return unify_invalid (explain_p);
20750 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20751 idx, explain_p);
20752 }
20753
20754 /* If the std::initializer_list<T> deduction worked, replace the
20755 deduced A with std::initializer_list<A>. */
20756 if (orig_parm != parm)
20757 {
20758 idx = TEMPLATE_TYPE_IDX (orig_parm);
20759 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20760 targ = listify (targ);
20761 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20762 }
20763 return unify_success (explain_p);
20764 }
20765
20766 /* If parm and arg aren't the same kind of thing (template, type, or
20767 expression), fail early. */
20768 if (pa_kind (parm) != pa_kind (arg))
20769 return unify_invalid (explain_p);
20770
20771 /* Immediately reject some pairs that won't unify because of
20772 cv-qualification mismatches. */
20773 if (TREE_CODE (arg) == TREE_CODE (parm)
20774 && TYPE_P (arg)
20775 /* It is the elements of the array which hold the cv quals of an array
20776 type, and the elements might be template type parms. We'll check
20777 when we recurse. */
20778 && TREE_CODE (arg) != ARRAY_TYPE
20779 /* We check the cv-qualifiers when unifying with template type
20780 parameters below. We want to allow ARG `const T' to unify with
20781 PARM `T' for example, when computing which of two templates
20782 is more specialized, for example. */
20783 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20784 && !check_cv_quals_for_unify (strict_in, arg, parm))
20785 return unify_cv_qual_mismatch (explain_p, parm, arg);
20786
20787 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20788 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20789 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20790 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20791 strict &= ~UNIFY_ALLOW_DERIVED;
20792 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20793 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20794
20795 switch (TREE_CODE (parm))
20796 {
20797 case TYPENAME_TYPE:
20798 case SCOPE_REF:
20799 case UNBOUND_CLASS_TEMPLATE:
20800 /* In a type which contains a nested-name-specifier, template
20801 argument values cannot be deduced for template parameters used
20802 within the nested-name-specifier. */
20803 return unify_success (explain_p);
20804
20805 case TEMPLATE_TYPE_PARM:
20806 case TEMPLATE_TEMPLATE_PARM:
20807 case BOUND_TEMPLATE_TEMPLATE_PARM:
20808 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20809 if (error_operand_p (tparm))
20810 return unify_invalid (explain_p);
20811
20812 if (TEMPLATE_TYPE_LEVEL (parm)
20813 != template_decl_level (tparm))
20814 /* The PARM is not one we're trying to unify. Just check
20815 to see if it matches ARG. */
20816 {
20817 if (TREE_CODE (arg) == TREE_CODE (parm)
20818 && (is_auto (parm) ? is_auto (arg)
20819 : same_type_p (parm, arg)))
20820 return unify_success (explain_p);
20821 else
20822 return unify_type_mismatch (explain_p, parm, arg);
20823 }
20824 idx = TEMPLATE_TYPE_IDX (parm);
20825 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20826 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20827 if (error_operand_p (tparm))
20828 return unify_invalid (explain_p);
20829
20830 /* Check for mixed types and values. */
20831 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20832 && TREE_CODE (tparm) != TYPE_DECL)
20833 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20834 && TREE_CODE (tparm) != TEMPLATE_DECL))
20835 gcc_unreachable ();
20836
20837 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20838 {
20839 if ((strict_in & UNIFY_ALLOW_DERIVED)
20840 && CLASS_TYPE_P (arg))
20841 {
20842 /* First try to match ARG directly. */
20843 tree t = try_class_unification (tparms, targs, parm, arg,
20844 explain_p);
20845 if (!t)
20846 {
20847 /* Otherwise, look for a suitable base of ARG, as below. */
20848 enum template_base_result r;
20849 r = get_template_base (tparms, targs, parm, arg,
20850 explain_p, &t);
20851 if (!t)
20852 return unify_no_common_base (explain_p, r, parm, arg);
20853 arg = t;
20854 }
20855 }
20856 /* ARG must be constructed from a template class or a template
20857 template parameter. */
20858 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20859 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20860 return unify_template_deduction_failure (explain_p, parm, arg);
20861
20862 /* Deduce arguments T, i from TT<T> or TT<i>. */
20863 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20864 return 1;
20865
20866 arg = TYPE_TI_TEMPLATE (arg);
20867
20868 /* Fall through to deduce template name. */
20869 }
20870
20871 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20872 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20873 {
20874 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20875
20876 /* Simple cases: Value already set, does match or doesn't. */
20877 if (targ != NULL_TREE && template_args_equal (targ, arg))
20878 return unify_success (explain_p);
20879 else if (targ)
20880 return unify_inconsistency (explain_p, parm, targ, arg);
20881 }
20882 else
20883 {
20884 /* If PARM is `const T' and ARG is only `int', we don't have
20885 a match unless we are allowing additional qualification.
20886 If ARG is `const int' and PARM is just `T' that's OK;
20887 that binds `const int' to `T'. */
20888 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20889 arg, parm))
20890 return unify_cv_qual_mismatch (explain_p, parm, arg);
20891
20892 /* Consider the case where ARG is `const volatile int' and
20893 PARM is `const T'. Then, T should be `volatile int'. */
20894 arg = cp_build_qualified_type_real
20895 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20896 if (arg == error_mark_node)
20897 return unify_invalid (explain_p);
20898
20899 /* Simple cases: Value already set, does match or doesn't. */
20900 if (targ != NULL_TREE && same_type_p (targ, arg))
20901 return unify_success (explain_p);
20902 else if (targ)
20903 return unify_inconsistency (explain_p, parm, targ, arg);
20904
20905 /* Make sure that ARG is not a variable-sized array. (Note
20906 that were talking about variable-sized arrays (like
20907 `int[n]'), rather than arrays of unknown size (like
20908 `int[]').) We'll get very confused by such a type since
20909 the bound of the array is not constant, and therefore
20910 not mangleable. Besides, such types are not allowed in
20911 ISO C++, so we can do as we please here. We do allow
20912 them for 'auto' deduction, since that isn't ABI-exposed. */
20913 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20914 return unify_vla_arg (explain_p, arg);
20915
20916 /* Strip typedefs as in convert_template_argument. */
20917 arg = canonicalize_type_argument (arg, tf_none);
20918 }
20919
20920 /* If ARG is a parameter pack or an expansion, we cannot unify
20921 against it unless PARM is also a parameter pack. */
20922 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20923 && !template_parameter_pack_p (parm))
20924 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20925
20926 /* If the argument deduction results is a METHOD_TYPE,
20927 then there is a problem.
20928 METHOD_TYPE doesn't map to any real C++ type the result of
20929 the deduction can not be of that type. */
20930 if (TREE_CODE (arg) == METHOD_TYPE)
20931 return unify_method_type_error (explain_p, arg);
20932
20933 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20934 return unify_success (explain_p);
20935
20936 case TEMPLATE_PARM_INDEX:
20937 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20938 if (error_operand_p (tparm))
20939 return unify_invalid (explain_p);
20940
20941 if (TEMPLATE_PARM_LEVEL (parm)
20942 != template_decl_level (tparm))
20943 {
20944 /* The PARM is not one we're trying to unify. Just check
20945 to see if it matches ARG. */
20946 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20947 && cp_tree_equal (parm, arg));
20948 if (result)
20949 unify_expression_unequal (explain_p, parm, arg);
20950 return result;
20951 }
20952
20953 idx = TEMPLATE_PARM_IDX (parm);
20954 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20955
20956 if (targ)
20957 {
20958 if ((strict & UNIFY_ALLOW_INTEGER)
20959 && TREE_TYPE (targ) && TREE_TYPE (arg)
20960 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
20961 /* We're deducing from an array bound, the type doesn't matter. */
20962 arg = fold_convert (TREE_TYPE (targ), arg);
20963 int x = !cp_tree_equal (targ, arg);
20964 if (x)
20965 unify_inconsistency (explain_p, parm, targ, arg);
20966 return x;
20967 }
20968
20969 /* [temp.deduct.type] If, in the declaration of a function template
20970 with a non-type template-parameter, the non-type
20971 template-parameter is used in an expression in the function
20972 parameter-list and, if the corresponding template-argument is
20973 deduced, the template-argument type shall match the type of the
20974 template-parameter exactly, except that a template-argument
20975 deduced from an array bound may be of any integral type.
20976 The non-type parameter might use already deduced type parameters. */
20977 ++processing_template_decl;
20978 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20979 --processing_template_decl;
20980 if (tree a = type_uses_auto (tparm))
20981 {
20982 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20983 if (tparm == error_mark_node)
20984 return 1;
20985 }
20986
20987 if (!TREE_TYPE (arg))
20988 /* Template-parameter dependent expression. Just accept it for now.
20989 It will later be processed in convert_template_argument. */
20990 ;
20991 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20992 non_reference (tparm)))
20993 /* OK */;
20994 else if ((strict & UNIFY_ALLOW_INTEGER)
20995 && CP_INTEGRAL_TYPE_P (tparm))
20996 /* Convert the ARG to the type of PARM; the deduced non-type
20997 template argument must exactly match the types of the
20998 corresponding parameter. */
20999 arg = fold (build_nop (tparm, arg));
21000 else if (uses_template_parms (tparm))
21001 {
21002 /* We haven't deduced the type of this parameter yet. */
21003 if (cxx_dialect >= cxx17
21004 /* We deduce from array bounds in try_array_deduction. */
21005 && !(strict & UNIFY_ALLOW_INTEGER))
21006 {
21007 /* Deduce it from the non-type argument. */
21008 tree atype = TREE_TYPE (arg);
21009 RECUR_AND_CHECK_FAILURE (tparms, targs,
21010 tparm, atype,
21011 UNIFY_ALLOW_NONE, explain_p);
21012 }
21013 else
21014 /* Try again later. */
21015 return unify_success (explain_p);
21016 }
21017 else
21018 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21019
21020 /* If ARG is a parameter pack or an expansion, we cannot unify
21021 against it unless PARM is also a parameter pack. */
21022 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21023 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21024 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21025
21026 {
21027 bool removed_attr = false;
21028 arg = strip_typedefs_expr (arg, &removed_attr);
21029 }
21030 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21031 return unify_success (explain_p);
21032
21033 case PTRMEM_CST:
21034 {
21035 /* A pointer-to-member constant can be unified only with
21036 another constant. */
21037 if (TREE_CODE (arg) != PTRMEM_CST)
21038 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21039
21040 /* Just unify the class member. It would be useless (and possibly
21041 wrong, depending on the strict flags) to unify also
21042 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21043 arg refer to the same variable, even if through different
21044 classes. For instance:
21045
21046 struct A { int x; };
21047 struct B : A { };
21048
21049 Unification of &A::x and &B::x must succeed. */
21050 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21051 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21052 }
21053
21054 case POINTER_TYPE:
21055 {
21056 if (!TYPE_PTR_P (arg))
21057 return unify_type_mismatch (explain_p, parm, arg);
21058
21059 /* [temp.deduct.call]
21060
21061 A can be another pointer or pointer to member type that can
21062 be converted to the deduced A via a qualification
21063 conversion (_conv.qual_).
21064
21065 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21066 This will allow for additional cv-qualification of the
21067 pointed-to types if appropriate. */
21068
21069 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21070 /* The derived-to-base conversion only persists through one
21071 level of pointers. */
21072 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21073
21074 return unify (tparms, targs, TREE_TYPE (parm),
21075 TREE_TYPE (arg), strict, explain_p);
21076 }
21077
21078 case REFERENCE_TYPE:
21079 if (TREE_CODE (arg) != REFERENCE_TYPE)
21080 return unify_type_mismatch (explain_p, parm, arg);
21081 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21082 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21083
21084 case ARRAY_TYPE:
21085 if (TREE_CODE (arg) != ARRAY_TYPE)
21086 return unify_type_mismatch (explain_p, parm, arg);
21087 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21088 != (TYPE_DOMAIN (arg) == NULL_TREE))
21089 return unify_type_mismatch (explain_p, parm, arg);
21090 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21091 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21092 if (TYPE_DOMAIN (parm) != NULL_TREE)
21093 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21094 TYPE_DOMAIN (arg), explain_p);
21095 return unify_success (explain_p);
21096
21097 case REAL_TYPE:
21098 case COMPLEX_TYPE:
21099 case VECTOR_TYPE:
21100 case INTEGER_TYPE:
21101 case BOOLEAN_TYPE:
21102 case ENUMERAL_TYPE:
21103 case VOID_TYPE:
21104 case NULLPTR_TYPE:
21105 if (TREE_CODE (arg) != TREE_CODE (parm))
21106 return unify_type_mismatch (explain_p, parm, arg);
21107
21108 /* We have already checked cv-qualification at the top of the
21109 function. */
21110 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21111 return unify_type_mismatch (explain_p, parm, arg);
21112
21113 /* As far as unification is concerned, this wins. Later checks
21114 will invalidate it if necessary. */
21115 return unify_success (explain_p);
21116
21117 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21118 /* Type INTEGER_CST can come from ordinary constant template args. */
21119 case INTEGER_CST:
21120 while (CONVERT_EXPR_P (arg))
21121 arg = TREE_OPERAND (arg, 0);
21122
21123 if (TREE_CODE (arg) != INTEGER_CST)
21124 return unify_template_argument_mismatch (explain_p, parm, arg);
21125 return (tree_int_cst_equal (parm, arg)
21126 ? unify_success (explain_p)
21127 : unify_template_argument_mismatch (explain_p, parm, arg));
21128
21129 case TREE_VEC:
21130 {
21131 int i, len, argslen;
21132 int parm_variadic_p = 0;
21133
21134 if (TREE_CODE (arg) != TREE_VEC)
21135 return unify_template_argument_mismatch (explain_p, parm, arg);
21136
21137 len = TREE_VEC_LENGTH (parm);
21138 argslen = TREE_VEC_LENGTH (arg);
21139
21140 /* Check for pack expansions in the parameters. */
21141 for (i = 0; i < len; ++i)
21142 {
21143 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21144 {
21145 if (i == len - 1)
21146 /* We can unify against something with a trailing
21147 parameter pack. */
21148 parm_variadic_p = 1;
21149 else
21150 /* [temp.deduct.type]/9: If the template argument list of
21151 P contains a pack expansion that is not the last
21152 template argument, the entire template argument list
21153 is a non-deduced context. */
21154 return unify_success (explain_p);
21155 }
21156 }
21157
21158 /* If we don't have enough arguments to satisfy the parameters
21159 (not counting the pack expression at the end), or we have
21160 too many arguments for a parameter list that doesn't end in
21161 a pack expression, we can't unify. */
21162 if (parm_variadic_p
21163 ? argslen < len - parm_variadic_p
21164 : argslen != len)
21165 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21166
21167 /* Unify all of the parameters that precede the (optional)
21168 pack expression. */
21169 for (i = 0; i < len - parm_variadic_p; ++i)
21170 {
21171 RECUR_AND_CHECK_FAILURE (tparms, targs,
21172 TREE_VEC_ELT (parm, i),
21173 TREE_VEC_ELT (arg, i),
21174 UNIFY_ALLOW_NONE, explain_p);
21175 }
21176 if (parm_variadic_p)
21177 return unify_pack_expansion (tparms, targs, parm, arg,
21178 DEDUCE_EXACT,
21179 /*subr=*/true, explain_p);
21180 return unify_success (explain_p);
21181 }
21182
21183 case RECORD_TYPE:
21184 case UNION_TYPE:
21185 if (TREE_CODE (arg) != TREE_CODE (parm))
21186 return unify_type_mismatch (explain_p, parm, arg);
21187
21188 if (TYPE_PTRMEMFUNC_P (parm))
21189 {
21190 if (!TYPE_PTRMEMFUNC_P (arg))
21191 return unify_type_mismatch (explain_p, parm, arg);
21192
21193 return unify (tparms, targs,
21194 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21195 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21196 strict, explain_p);
21197 }
21198 else if (TYPE_PTRMEMFUNC_P (arg))
21199 return unify_type_mismatch (explain_p, parm, arg);
21200
21201 if (CLASSTYPE_TEMPLATE_INFO (parm))
21202 {
21203 tree t = NULL_TREE;
21204
21205 if (strict_in & UNIFY_ALLOW_DERIVED)
21206 {
21207 /* First, we try to unify the PARM and ARG directly. */
21208 t = try_class_unification (tparms, targs,
21209 parm, arg, explain_p);
21210
21211 if (!t)
21212 {
21213 /* Fallback to the special case allowed in
21214 [temp.deduct.call]:
21215
21216 If P is a class, and P has the form
21217 template-id, then A can be a derived class of
21218 the deduced A. Likewise, if P is a pointer to
21219 a class of the form template-id, A can be a
21220 pointer to a derived class pointed to by the
21221 deduced A. */
21222 enum template_base_result r;
21223 r = get_template_base (tparms, targs, parm, arg,
21224 explain_p, &t);
21225
21226 if (!t)
21227 {
21228 /* Don't give the derived diagnostic if we're
21229 already dealing with the same template. */
21230 bool same_template
21231 = (CLASSTYPE_TEMPLATE_INFO (arg)
21232 && (CLASSTYPE_TI_TEMPLATE (parm)
21233 == CLASSTYPE_TI_TEMPLATE (arg)));
21234 return unify_no_common_base (explain_p && !same_template,
21235 r, parm, arg);
21236 }
21237 }
21238 }
21239 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21240 && (CLASSTYPE_TI_TEMPLATE (parm)
21241 == CLASSTYPE_TI_TEMPLATE (arg)))
21242 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21243 Then, we should unify `int' and `U'. */
21244 t = arg;
21245 else
21246 /* There's no chance of unification succeeding. */
21247 return unify_type_mismatch (explain_p, parm, arg);
21248
21249 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21250 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21251 }
21252 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21253 return unify_type_mismatch (explain_p, parm, arg);
21254 return unify_success (explain_p);
21255
21256 case METHOD_TYPE:
21257 case FUNCTION_TYPE:
21258 {
21259 unsigned int nargs;
21260 tree *args;
21261 tree a;
21262 unsigned int i;
21263
21264 if (TREE_CODE (arg) != TREE_CODE (parm))
21265 return unify_type_mismatch (explain_p, parm, arg);
21266
21267 /* CV qualifications for methods can never be deduced, they must
21268 match exactly. We need to check them explicitly here,
21269 because type_unification_real treats them as any other
21270 cv-qualified parameter. */
21271 if (TREE_CODE (parm) == METHOD_TYPE
21272 && (!check_cv_quals_for_unify
21273 (UNIFY_ALLOW_NONE,
21274 class_of_this_parm (arg),
21275 class_of_this_parm (parm))))
21276 return unify_cv_qual_mismatch (explain_p, parm, arg);
21277 if (TREE_CODE (arg) == FUNCTION_TYPE
21278 && type_memfn_quals (parm) != type_memfn_quals (arg))
21279 return unify_cv_qual_mismatch (explain_p, parm, arg);
21280 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21281 return unify_type_mismatch (explain_p, parm, arg);
21282
21283 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21284 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21285
21286 nargs = list_length (TYPE_ARG_TYPES (arg));
21287 args = XALLOCAVEC (tree, nargs);
21288 for (a = TYPE_ARG_TYPES (arg), i = 0;
21289 a != NULL_TREE && a != void_list_node;
21290 a = TREE_CHAIN (a), ++i)
21291 args[i] = TREE_VALUE (a);
21292 nargs = i;
21293
21294 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21295 args, nargs, 1, DEDUCE_EXACT,
21296 LOOKUP_NORMAL, NULL, explain_p))
21297 return 1;
21298
21299 if (flag_noexcept_type)
21300 {
21301 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21302 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21303 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21304 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21305 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21306 && uses_template_parms (TREE_PURPOSE (pspec)))
21307 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21308 TREE_PURPOSE (aspec),
21309 UNIFY_ALLOW_NONE, explain_p);
21310 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21311 return unify_type_mismatch (explain_p, parm, arg);
21312 }
21313
21314 return 0;
21315 }
21316
21317 case OFFSET_TYPE:
21318 /* Unify a pointer to member with a pointer to member function, which
21319 deduces the type of the member as a function type. */
21320 if (TYPE_PTRMEMFUNC_P (arg))
21321 {
21322 /* Check top-level cv qualifiers */
21323 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21324 return unify_cv_qual_mismatch (explain_p, parm, arg);
21325
21326 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21327 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21328 UNIFY_ALLOW_NONE, explain_p);
21329
21330 /* Determine the type of the function we are unifying against. */
21331 tree fntype = static_fn_type (arg);
21332
21333 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21334 }
21335
21336 if (TREE_CODE (arg) != OFFSET_TYPE)
21337 return unify_type_mismatch (explain_p, parm, arg);
21338 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21339 TYPE_OFFSET_BASETYPE (arg),
21340 UNIFY_ALLOW_NONE, explain_p);
21341 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21342 strict, explain_p);
21343
21344 case CONST_DECL:
21345 if (DECL_TEMPLATE_PARM_P (parm))
21346 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21347 if (arg != scalar_constant_value (parm))
21348 return unify_template_argument_mismatch (explain_p, parm, arg);
21349 return unify_success (explain_p);
21350
21351 case FIELD_DECL:
21352 case TEMPLATE_DECL:
21353 /* Matched cases are handled by the ARG == PARM test above. */
21354 return unify_template_argument_mismatch (explain_p, parm, arg);
21355
21356 case VAR_DECL:
21357 /* We might get a variable as a non-type template argument in parm if the
21358 corresponding parameter is type-dependent. Make any necessary
21359 adjustments based on whether arg is a reference. */
21360 if (CONSTANT_CLASS_P (arg))
21361 parm = fold_non_dependent_expr (parm);
21362 else if (REFERENCE_REF_P (arg))
21363 {
21364 tree sub = TREE_OPERAND (arg, 0);
21365 STRIP_NOPS (sub);
21366 if (TREE_CODE (sub) == ADDR_EXPR)
21367 arg = TREE_OPERAND (sub, 0);
21368 }
21369 /* Now use the normal expression code to check whether they match. */
21370 goto expr;
21371
21372 case TYPE_ARGUMENT_PACK:
21373 case NONTYPE_ARGUMENT_PACK:
21374 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21375 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21376
21377 case TYPEOF_TYPE:
21378 case DECLTYPE_TYPE:
21379 case UNDERLYING_TYPE:
21380 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21381 or UNDERLYING_TYPE nodes. */
21382 return unify_success (explain_p);
21383
21384 case ERROR_MARK:
21385 /* Unification fails if we hit an error node. */
21386 return unify_invalid (explain_p);
21387
21388 case INDIRECT_REF:
21389 if (REFERENCE_REF_P (parm))
21390 {
21391 bool pexp = PACK_EXPANSION_P (arg);
21392 if (pexp)
21393 arg = PACK_EXPANSION_PATTERN (arg);
21394 if (REFERENCE_REF_P (arg))
21395 arg = TREE_OPERAND (arg, 0);
21396 if (pexp)
21397 arg = make_pack_expansion (arg, complain);
21398 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21399 strict, explain_p);
21400 }
21401 /* FALLTHRU */
21402
21403 default:
21404 /* An unresolved overload is a nondeduced context. */
21405 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21406 return unify_success (explain_p);
21407 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21408 expr:
21409 /* We must be looking at an expression. This can happen with
21410 something like:
21411
21412 template <int I>
21413 void foo(S<I>, S<I + 2>);
21414
21415 This is a "nondeduced context":
21416
21417 [deduct.type]
21418
21419 The nondeduced contexts are:
21420
21421 --A type that is a template-id in which one or more of
21422 the template-arguments is an expression that references
21423 a template-parameter.
21424
21425 In these cases, we assume deduction succeeded, but don't
21426 actually infer any unifications. */
21427
21428 if (!uses_template_parms (parm)
21429 && !template_args_equal (parm, arg))
21430 return unify_expression_unequal (explain_p, parm, arg);
21431 else
21432 return unify_success (explain_p);
21433 }
21434 }
21435 #undef RECUR_AND_CHECK_FAILURE
21436 \f
21437 /* Note that DECL can be defined in this translation unit, if
21438 required. */
21439
21440 static void
21441 mark_definable (tree decl)
21442 {
21443 tree clone;
21444 DECL_NOT_REALLY_EXTERN (decl) = 1;
21445 FOR_EACH_CLONE (clone, decl)
21446 DECL_NOT_REALLY_EXTERN (clone) = 1;
21447 }
21448
21449 /* Called if RESULT is explicitly instantiated, or is a member of an
21450 explicitly instantiated class. */
21451
21452 void
21453 mark_decl_instantiated (tree result, int extern_p)
21454 {
21455 SET_DECL_EXPLICIT_INSTANTIATION (result);
21456
21457 /* If this entity has already been written out, it's too late to
21458 make any modifications. */
21459 if (TREE_ASM_WRITTEN (result))
21460 return;
21461
21462 /* For anonymous namespace we don't need to do anything. */
21463 if (decl_anon_ns_mem_p (result))
21464 {
21465 gcc_assert (!TREE_PUBLIC (result));
21466 return;
21467 }
21468
21469 if (TREE_CODE (result) != FUNCTION_DECL)
21470 /* The TREE_PUBLIC flag for function declarations will have been
21471 set correctly by tsubst. */
21472 TREE_PUBLIC (result) = 1;
21473
21474 /* This might have been set by an earlier implicit instantiation. */
21475 DECL_COMDAT (result) = 0;
21476
21477 if (extern_p)
21478 DECL_NOT_REALLY_EXTERN (result) = 0;
21479 else
21480 {
21481 mark_definable (result);
21482 mark_needed (result);
21483 /* Always make artificials weak. */
21484 if (DECL_ARTIFICIAL (result) && flag_weak)
21485 comdat_linkage (result);
21486 /* For WIN32 we also want to put explicit instantiations in
21487 linkonce sections. */
21488 else if (TREE_PUBLIC (result))
21489 maybe_make_one_only (result);
21490 }
21491
21492 /* If EXTERN_P, then this function will not be emitted -- unless
21493 followed by an explicit instantiation, at which point its linkage
21494 will be adjusted. If !EXTERN_P, then this function will be
21495 emitted here. In neither circumstance do we want
21496 import_export_decl to adjust the linkage. */
21497 DECL_INTERFACE_KNOWN (result) = 1;
21498 }
21499
21500 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21501 important template arguments. If any are missing, we check whether
21502 they're important by using error_mark_node for substituting into any
21503 args that were used for partial ordering (the ones between ARGS and END)
21504 and seeing if it bubbles up. */
21505
21506 static bool
21507 check_undeduced_parms (tree targs, tree args, tree end)
21508 {
21509 bool found = false;
21510 int i;
21511 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21512 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21513 {
21514 found = true;
21515 TREE_VEC_ELT (targs, i) = error_mark_node;
21516 }
21517 if (found)
21518 {
21519 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21520 if (substed == error_mark_node)
21521 return true;
21522 }
21523 return false;
21524 }
21525
21526 /* Given two function templates PAT1 and PAT2, return:
21527
21528 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21529 -1 if PAT2 is more specialized than PAT1.
21530 0 if neither is more specialized.
21531
21532 LEN indicates the number of parameters we should consider
21533 (defaulted parameters should not be considered).
21534
21535 The 1998 std underspecified function template partial ordering, and
21536 DR214 addresses the issue. We take pairs of arguments, one from
21537 each of the templates, and deduce them against each other. One of
21538 the templates will be more specialized if all the *other*
21539 template's arguments deduce against its arguments and at least one
21540 of its arguments *does* *not* deduce against the other template's
21541 corresponding argument. Deduction is done as for class templates.
21542 The arguments used in deduction have reference and top level cv
21543 qualifiers removed. Iff both arguments were originally reference
21544 types *and* deduction succeeds in both directions, an lvalue reference
21545 wins against an rvalue reference and otherwise the template
21546 with the more cv-qualified argument wins for that pairing (if
21547 neither is more cv-qualified, they both are equal). Unlike regular
21548 deduction, after all the arguments have been deduced in this way,
21549 we do *not* verify the deduced template argument values can be
21550 substituted into non-deduced contexts.
21551
21552 The logic can be a bit confusing here, because we look at deduce1 and
21553 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21554 can find template arguments for pat1 to make arg1 look like arg2, that
21555 means that arg2 is at least as specialized as arg1. */
21556
21557 int
21558 more_specialized_fn (tree pat1, tree pat2, int len)
21559 {
21560 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21561 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21562 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21563 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21564 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21565 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21566 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21567 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21568 tree origs1, origs2;
21569 bool lose1 = false;
21570 bool lose2 = false;
21571
21572 /* Remove the this parameter from non-static member functions. If
21573 one is a non-static member function and the other is not a static
21574 member function, remove the first parameter from that function
21575 also. This situation occurs for operator functions where we
21576 locate both a member function (with this pointer) and non-member
21577 operator (with explicit first operand). */
21578 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21579 {
21580 len--; /* LEN is the number of significant arguments for DECL1 */
21581 args1 = TREE_CHAIN (args1);
21582 if (!DECL_STATIC_FUNCTION_P (decl2))
21583 args2 = TREE_CHAIN (args2);
21584 }
21585 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21586 {
21587 args2 = TREE_CHAIN (args2);
21588 if (!DECL_STATIC_FUNCTION_P (decl1))
21589 {
21590 len--;
21591 args1 = TREE_CHAIN (args1);
21592 }
21593 }
21594
21595 /* If only one is a conversion operator, they are unordered. */
21596 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21597 return 0;
21598
21599 /* Consider the return type for a conversion function */
21600 if (DECL_CONV_FN_P (decl1))
21601 {
21602 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21603 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21604 len++;
21605 }
21606
21607 processing_template_decl++;
21608
21609 origs1 = args1;
21610 origs2 = args2;
21611
21612 while (len--
21613 /* Stop when an ellipsis is seen. */
21614 && args1 != NULL_TREE && args2 != NULL_TREE)
21615 {
21616 tree arg1 = TREE_VALUE (args1);
21617 tree arg2 = TREE_VALUE (args2);
21618 int deduce1, deduce2;
21619 int quals1 = -1;
21620 int quals2 = -1;
21621 int ref1 = 0;
21622 int ref2 = 0;
21623
21624 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21625 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21626 {
21627 /* When both arguments are pack expansions, we need only
21628 unify the patterns themselves. */
21629 arg1 = PACK_EXPANSION_PATTERN (arg1);
21630 arg2 = PACK_EXPANSION_PATTERN (arg2);
21631
21632 /* This is the last comparison we need to do. */
21633 len = 0;
21634 }
21635
21636 /* DR 1847: If a particular P contains no template-parameters that
21637 participate in template argument deduction, that P is not used to
21638 determine the ordering. */
21639 if (!uses_deducible_template_parms (arg1)
21640 && !uses_deducible_template_parms (arg2))
21641 goto next;
21642
21643 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21644 {
21645 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21646 arg1 = TREE_TYPE (arg1);
21647 quals1 = cp_type_quals (arg1);
21648 }
21649
21650 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21651 {
21652 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21653 arg2 = TREE_TYPE (arg2);
21654 quals2 = cp_type_quals (arg2);
21655 }
21656
21657 arg1 = TYPE_MAIN_VARIANT (arg1);
21658 arg2 = TYPE_MAIN_VARIANT (arg2);
21659
21660 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21661 {
21662 int i, len2 = remaining_arguments (args2);
21663 tree parmvec = make_tree_vec (1);
21664 tree argvec = make_tree_vec (len2);
21665 tree ta = args2;
21666
21667 /* Setup the parameter vector, which contains only ARG1. */
21668 TREE_VEC_ELT (parmvec, 0) = arg1;
21669
21670 /* Setup the argument vector, which contains the remaining
21671 arguments. */
21672 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21673 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21674
21675 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21676 argvec, DEDUCE_EXACT,
21677 /*subr=*/true, /*explain_p=*/false)
21678 == 0);
21679
21680 /* We cannot deduce in the other direction, because ARG1 is
21681 a pack expansion but ARG2 is not. */
21682 deduce2 = 0;
21683 }
21684 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21685 {
21686 int i, len1 = remaining_arguments (args1);
21687 tree parmvec = make_tree_vec (1);
21688 tree argvec = make_tree_vec (len1);
21689 tree ta = args1;
21690
21691 /* Setup the parameter vector, which contains only ARG1. */
21692 TREE_VEC_ELT (parmvec, 0) = arg2;
21693
21694 /* Setup the argument vector, which contains the remaining
21695 arguments. */
21696 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21697 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21698
21699 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21700 argvec, DEDUCE_EXACT,
21701 /*subr=*/true, /*explain_p=*/false)
21702 == 0);
21703
21704 /* We cannot deduce in the other direction, because ARG2 is
21705 a pack expansion but ARG1 is not.*/
21706 deduce1 = 0;
21707 }
21708
21709 else
21710 {
21711 /* The normal case, where neither argument is a pack
21712 expansion. */
21713 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21714 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21715 == 0);
21716 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21717 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21718 == 0);
21719 }
21720
21721 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21722 arg2, then arg2 is not as specialized as arg1. */
21723 if (!deduce1)
21724 lose2 = true;
21725 if (!deduce2)
21726 lose1 = true;
21727
21728 /* "If, for a given type, deduction succeeds in both directions
21729 (i.e., the types are identical after the transformations above)
21730 and both P and A were reference types (before being replaced with
21731 the type referred to above):
21732 - if the type from the argument template was an lvalue reference and
21733 the type from the parameter template was not, the argument type is
21734 considered to be more specialized than the other; otherwise,
21735 - if the type from the argument template is more cv-qualified
21736 than the type from the parameter template (as described above),
21737 the argument type is considered to be more specialized than the other;
21738 otherwise,
21739 - neither type is more specialized than the other." */
21740
21741 if (deduce1 && deduce2)
21742 {
21743 if (ref1 && ref2 && ref1 != ref2)
21744 {
21745 if (ref1 > ref2)
21746 lose1 = true;
21747 else
21748 lose2 = true;
21749 }
21750 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21751 {
21752 if ((quals1 & quals2) == quals2)
21753 lose2 = true;
21754 if ((quals1 & quals2) == quals1)
21755 lose1 = true;
21756 }
21757 }
21758
21759 if (lose1 && lose2)
21760 /* We've failed to deduce something in either direction.
21761 These must be unordered. */
21762 break;
21763
21764 next:
21765
21766 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21767 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21768 /* We have already processed all of the arguments in our
21769 handing of the pack expansion type. */
21770 len = 0;
21771
21772 args1 = TREE_CHAIN (args1);
21773 args2 = TREE_CHAIN (args2);
21774 }
21775
21776 /* "In most cases, all template parameters must have values in order for
21777 deduction to succeed, but for partial ordering purposes a template
21778 parameter may remain without a value provided it is not used in the
21779 types being used for partial ordering."
21780
21781 Thus, if we are missing any of the targs1 we need to substitute into
21782 origs1, then pat2 is not as specialized as pat1. This can happen when
21783 there is a nondeduced context. */
21784 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21785 lose2 = true;
21786 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21787 lose1 = true;
21788
21789 processing_template_decl--;
21790
21791 /* If both deductions succeed, the partial ordering selects the more
21792 constrained template. */
21793 if (!lose1 && !lose2)
21794 {
21795 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21796 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21797 lose1 = !subsumes_constraints (c1, c2);
21798 lose2 = !subsumes_constraints (c2, c1);
21799 }
21800
21801 /* All things being equal, if the next argument is a pack expansion
21802 for one function but not for the other, prefer the
21803 non-variadic function. FIXME this is bogus; see c++/41958. */
21804 if (lose1 == lose2
21805 && args1 && TREE_VALUE (args1)
21806 && args2 && TREE_VALUE (args2))
21807 {
21808 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21809 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21810 }
21811
21812 if (lose1 == lose2)
21813 return 0;
21814 else if (!lose1)
21815 return 1;
21816 else
21817 return -1;
21818 }
21819
21820 /* Determine which of two partial specializations of TMPL is more
21821 specialized.
21822
21823 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21824 to the first partial specialization. The TREE_PURPOSE is the
21825 innermost set of template parameters for the partial
21826 specialization. PAT2 is similar, but for the second template.
21827
21828 Return 1 if the first partial specialization is more specialized;
21829 -1 if the second is more specialized; 0 if neither is more
21830 specialized.
21831
21832 See [temp.class.order] for information about determining which of
21833 two templates is more specialized. */
21834
21835 static int
21836 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21837 {
21838 tree targs;
21839 int winner = 0;
21840 bool any_deductions = false;
21841
21842 tree tmpl1 = TREE_VALUE (pat1);
21843 tree tmpl2 = TREE_VALUE (pat2);
21844 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21845 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21846
21847 /* Just like what happens for functions, if we are ordering between
21848 different template specializations, we may encounter dependent
21849 types in the arguments, and we need our dependency check functions
21850 to behave correctly. */
21851 ++processing_template_decl;
21852 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21853 if (targs)
21854 {
21855 --winner;
21856 any_deductions = true;
21857 }
21858
21859 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21860 if (targs)
21861 {
21862 ++winner;
21863 any_deductions = true;
21864 }
21865 --processing_template_decl;
21866
21867 /* If both deductions succeed, the partial ordering selects the more
21868 constrained template. */
21869 if (!winner && any_deductions)
21870 return more_constrained (tmpl1, tmpl2);
21871
21872 /* In the case of a tie where at least one of the templates
21873 has a parameter pack at the end, the template with the most
21874 non-packed parameters wins. */
21875 if (winner == 0
21876 && any_deductions
21877 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21878 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21879 {
21880 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21881 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21882 int len1 = TREE_VEC_LENGTH (args1);
21883 int len2 = TREE_VEC_LENGTH (args2);
21884
21885 /* We don't count the pack expansion at the end. */
21886 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21887 --len1;
21888 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21889 --len2;
21890
21891 if (len1 > len2)
21892 return 1;
21893 else if (len1 < len2)
21894 return -1;
21895 }
21896
21897 return winner;
21898 }
21899
21900 /* Return the template arguments that will produce the function signature
21901 DECL from the function template FN, with the explicit template
21902 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21903 also match. Return NULL_TREE if no satisfactory arguments could be
21904 found. */
21905
21906 static tree
21907 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21908 {
21909 int ntparms = DECL_NTPARMS (fn);
21910 tree targs = make_tree_vec (ntparms);
21911 tree decl_type = TREE_TYPE (decl);
21912 tree decl_arg_types;
21913 tree *args;
21914 unsigned int nargs, ix;
21915 tree arg;
21916
21917 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21918
21919 /* Never do unification on the 'this' parameter. */
21920 decl_arg_types = skip_artificial_parms_for (decl,
21921 TYPE_ARG_TYPES (decl_type));
21922
21923 nargs = list_length (decl_arg_types);
21924 args = XALLOCAVEC (tree, nargs);
21925 for (arg = decl_arg_types, ix = 0;
21926 arg != NULL_TREE && arg != void_list_node;
21927 arg = TREE_CHAIN (arg), ++ix)
21928 args[ix] = TREE_VALUE (arg);
21929
21930 if (fn_type_unification (fn, explicit_args, targs,
21931 args, ix,
21932 (check_rettype || DECL_CONV_FN_P (fn)
21933 ? TREE_TYPE (decl_type) : NULL_TREE),
21934 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21935 /*decltype*/false)
21936 == error_mark_node)
21937 return NULL_TREE;
21938
21939 return targs;
21940 }
21941
21942 /* Return the innermost template arguments that, when applied to a partial
21943 specialization SPEC_TMPL of TMPL, yield the ARGS.
21944
21945 For example, suppose we have:
21946
21947 template <class T, class U> struct S {};
21948 template <class T> struct S<T*, int> {};
21949
21950 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21951 partial specialization and the ARGS will be {double*, int}. The resulting
21952 vector will be {double}, indicating that `T' is bound to `double'. */
21953
21954 static tree
21955 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21956 {
21957 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21958 tree spec_args
21959 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21960 int i, ntparms = TREE_VEC_LENGTH (tparms);
21961 tree deduced_args;
21962 tree innermost_deduced_args;
21963
21964 innermost_deduced_args = make_tree_vec (ntparms);
21965 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21966 {
21967 deduced_args = copy_node (args);
21968 SET_TMPL_ARGS_LEVEL (deduced_args,
21969 TMPL_ARGS_DEPTH (deduced_args),
21970 innermost_deduced_args);
21971 }
21972 else
21973 deduced_args = innermost_deduced_args;
21974
21975 bool tried_array_deduction = (cxx_dialect < cxx17);
21976 again:
21977 if (unify (tparms, deduced_args,
21978 INNERMOST_TEMPLATE_ARGS (spec_args),
21979 INNERMOST_TEMPLATE_ARGS (args),
21980 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21981 return NULL_TREE;
21982
21983 for (i = 0; i < ntparms; ++i)
21984 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21985 {
21986 if (!tried_array_deduction)
21987 {
21988 try_array_deduction (tparms, innermost_deduced_args,
21989 INNERMOST_TEMPLATE_ARGS (spec_args));
21990 tried_array_deduction = true;
21991 if (TREE_VEC_ELT (innermost_deduced_args, i))
21992 goto again;
21993 }
21994 return NULL_TREE;
21995 }
21996
21997 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21998 if (!push_tinst_level (tinst))
21999 {
22000 excessive_deduction_depth = true;
22001 return NULL_TREE;
22002 }
22003
22004 /* Verify that nondeduced template arguments agree with the type
22005 obtained from argument deduction.
22006
22007 For example:
22008
22009 struct A { typedef int X; };
22010 template <class T, class U> struct C {};
22011 template <class T> struct C<T, typename T::X> {};
22012
22013 Then with the instantiation `C<A, int>', we can deduce that
22014 `T' is `A' but unify () does not check whether `typename T::X'
22015 is `int'. */
22016 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22017
22018 if (spec_args != error_mark_node)
22019 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22020 INNERMOST_TEMPLATE_ARGS (spec_args),
22021 tmpl, tf_none, false, false);
22022
22023 pop_tinst_level ();
22024
22025 if (spec_args == error_mark_node
22026 /* We only need to check the innermost arguments; the other
22027 arguments will always agree. */
22028 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22029 INNERMOST_TEMPLATE_ARGS (args)))
22030 return NULL_TREE;
22031
22032 /* Now that we have bindings for all of the template arguments,
22033 ensure that the arguments deduced for the template template
22034 parameters have compatible template parameter lists. See the use
22035 of template_template_parm_bindings_ok_p in fn_type_unification
22036 for more information. */
22037 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22038 return NULL_TREE;
22039
22040 return deduced_args;
22041 }
22042
22043 // Compare two function templates T1 and T2 by deducing bindings
22044 // from one against the other. If both deductions succeed, compare
22045 // constraints to see which is more constrained.
22046 static int
22047 more_specialized_inst (tree t1, tree t2)
22048 {
22049 int fate = 0;
22050 int count = 0;
22051
22052 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22053 {
22054 --fate;
22055 ++count;
22056 }
22057
22058 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22059 {
22060 ++fate;
22061 ++count;
22062 }
22063
22064 // If both deductions succeed, then one may be more constrained.
22065 if (count == 2 && fate == 0)
22066 fate = more_constrained (t1, t2);
22067
22068 return fate;
22069 }
22070
22071 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22072 Return the TREE_LIST node with the most specialized template, if
22073 any. If there is no most specialized template, the error_mark_node
22074 is returned.
22075
22076 Note that this function does not look at, or modify, the
22077 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22078 returned is one of the elements of INSTANTIATIONS, callers may
22079 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22080 and retrieve it from the value returned. */
22081
22082 tree
22083 most_specialized_instantiation (tree templates)
22084 {
22085 tree fn, champ;
22086
22087 ++processing_template_decl;
22088
22089 champ = templates;
22090 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22091 {
22092 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22093 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22094 if (fate == -1)
22095 champ = fn;
22096 else if (!fate)
22097 {
22098 /* Equally specialized, move to next function. If there
22099 is no next function, nothing's most specialized. */
22100 fn = TREE_CHAIN (fn);
22101 champ = fn;
22102 if (!fn)
22103 break;
22104 }
22105 }
22106
22107 if (champ)
22108 /* Now verify that champ is better than everything earlier in the
22109 instantiation list. */
22110 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22111 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22112 {
22113 champ = NULL_TREE;
22114 break;
22115 }
22116 }
22117
22118 processing_template_decl--;
22119
22120 if (!champ)
22121 return error_mark_node;
22122
22123 return champ;
22124 }
22125
22126 /* If DECL is a specialization of some template, return the most
22127 general such template. Otherwise, returns NULL_TREE.
22128
22129 For example, given:
22130
22131 template <class T> struct S { template <class U> void f(U); };
22132
22133 if TMPL is `template <class U> void S<int>::f(U)' this will return
22134 the full template. This function will not trace past partial
22135 specializations, however. For example, given in addition:
22136
22137 template <class T> struct S<T*> { template <class U> void f(U); };
22138
22139 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22140 `template <class T> template <class U> S<T*>::f(U)'. */
22141
22142 tree
22143 most_general_template (tree decl)
22144 {
22145 if (TREE_CODE (decl) != TEMPLATE_DECL)
22146 {
22147 if (tree tinfo = get_template_info (decl))
22148 decl = TI_TEMPLATE (tinfo);
22149 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22150 template friend, or a FIELD_DECL for a capture pack. */
22151 if (TREE_CODE (decl) != TEMPLATE_DECL)
22152 return NULL_TREE;
22153 }
22154
22155 /* Look for more and more general templates. */
22156 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22157 {
22158 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22159 (See cp-tree.h for details.) */
22160 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22161 break;
22162
22163 if (CLASS_TYPE_P (TREE_TYPE (decl))
22164 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22165 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22166 break;
22167
22168 /* Stop if we run into an explicitly specialized class template. */
22169 if (!DECL_NAMESPACE_SCOPE_P (decl)
22170 && DECL_CONTEXT (decl)
22171 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22172 break;
22173
22174 decl = DECL_TI_TEMPLATE (decl);
22175 }
22176
22177 return decl;
22178 }
22179
22180 /* Return the most specialized of the template partial specializations
22181 which can produce TARGET, a specialization of some class or variable
22182 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22183 a TEMPLATE_DECL node corresponding to the partial specialization, while
22184 the TREE_PURPOSE is the set of template arguments that must be
22185 substituted into the template pattern in order to generate TARGET.
22186
22187 If the choice of partial specialization is ambiguous, a diagnostic
22188 is issued, and the error_mark_node is returned. If there are no
22189 partial specializations matching TARGET, then NULL_TREE is
22190 returned, indicating that the primary template should be used. */
22191
22192 static tree
22193 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22194 {
22195 tree list = NULL_TREE;
22196 tree t;
22197 tree champ;
22198 int fate;
22199 bool ambiguous_p;
22200 tree outer_args = NULL_TREE;
22201 tree tmpl, args;
22202
22203 if (TYPE_P (target))
22204 {
22205 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22206 tmpl = TI_TEMPLATE (tinfo);
22207 args = TI_ARGS (tinfo);
22208 }
22209 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22210 {
22211 tmpl = TREE_OPERAND (target, 0);
22212 args = TREE_OPERAND (target, 1);
22213 }
22214 else if (VAR_P (target))
22215 {
22216 tree tinfo = DECL_TEMPLATE_INFO (target);
22217 tmpl = TI_TEMPLATE (tinfo);
22218 args = TI_ARGS (tinfo);
22219 }
22220 else
22221 gcc_unreachable ();
22222
22223 tree main_tmpl = most_general_template (tmpl);
22224
22225 /* For determining which partial specialization to use, only the
22226 innermost args are interesting. */
22227 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22228 {
22229 outer_args = strip_innermost_template_args (args, 1);
22230 args = INNERMOST_TEMPLATE_ARGS (args);
22231 }
22232
22233 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22234 {
22235 tree spec_args;
22236 tree spec_tmpl = TREE_VALUE (t);
22237
22238 if (outer_args)
22239 {
22240 /* Substitute in the template args from the enclosing class. */
22241 ++processing_template_decl;
22242 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22243 --processing_template_decl;
22244 }
22245
22246 if (spec_tmpl == error_mark_node)
22247 return error_mark_node;
22248
22249 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22250 if (spec_args)
22251 {
22252 if (outer_args)
22253 spec_args = add_to_template_args (outer_args, spec_args);
22254
22255 /* Keep the candidate only if the constraints are satisfied,
22256 or if we're not compiling with concepts. */
22257 if (!flag_concepts
22258 || constraints_satisfied_p (spec_tmpl, spec_args))
22259 {
22260 list = tree_cons (spec_args, TREE_VALUE (t), list);
22261 TREE_TYPE (list) = TREE_TYPE (t);
22262 }
22263 }
22264 }
22265
22266 if (! list)
22267 return NULL_TREE;
22268
22269 ambiguous_p = false;
22270 t = list;
22271 champ = t;
22272 t = TREE_CHAIN (t);
22273 for (; t; t = TREE_CHAIN (t))
22274 {
22275 fate = more_specialized_partial_spec (tmpl, champ, t);
22276 if (fate == 1)
22277 ;
22278 else
22279 {
22280 if (fate == 0)
22281 {
22282 t = TREE_CHAIN (t);
22283 if (! t)
22284 {
22285 ambiguous_p = true;
22286 break;
22287 }
22288 }
22289 champ = t;
22290 }
22291 }
22292
22293 if (!ambiguous_p)
22294 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22295 {
22296 fate = more_specialized_partial_spec (tmpl, champ, t);
22297 if (fate != 1)
22298 {
22299 ambiguous_p = true;
22300 break;
22301 }
22302 }
22303
22304 if (ambiguous_p)
22305 {
22306 const char *str;
22307 char *spaces = NULL;
22308 if (!(complain & tf_error))
22309 return error_mark_node;
22310 if (TYPE_P (target))
22311 error ("ambiguous template instantiation for %q#T", target);
22312 else
22313 error ("ambiguous template instantiation for %q#D", target);
22314 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22315 for (t = list; t; t = TREE_CHAIN (t))
22316 {
22317 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22318 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22319 "%s %#qS", spaces ? spaces : str, subst);
22320 spaces = spaces ? spaces : get_spaces (str);
22321 }
22322 free (spaces);
22323 return error_mark_node;
22324 }
22325
22326 return champ;
22327 }
22328
22329 /* Explicitly instantiate DECL. */
22330
22331 void
22332 do_decl_instantiation (tree decl, tree storage)
22333 {
22334 tree result = NULL_TREE;
22335 int extern_p = 0;
22336
22337 if (!decl || decl == error_mark_node)
22338 /* An error occurred, for which grokdeclarator has already issued
22339 an appropriate message. */
22340 return;
22341 else if (! DECL_LANG_SPECIFIC (decl))
22342 {
22343 error ("explicit instantiation of non-template %q#D", decl);
22344 return;
22345 }
22346
22347 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22348 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22349
22350 if (VAR_P (decl) && !var_templ)
22351 {
22352 /* There is an asymmetry here in the way VAR_DECLs and
22353 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22354 the latter, the DECL we get back will be marked as a
22355 template instantiation, and the appropriate
22356 DECL_TEMPLATE_INFO will be set up. This does not happen for
22357 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22358 should handle VAR_DECLs as it currently handles
22359 FUNCTION_DECLs. */
22360 if (!DECL_CLASS_SCOPE_P (decl))
22361 {
22362 error ("%qD is not a static data member of a class template", decl);
22363 return;
22364 }
22365 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22366 if (!result || !VAR_P (result))
22367 {
22368 error ("no matching template for %qD found", decl);
22369 return;
22370 }
22371 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22372 {
22373 error ("type %qT for explicit instantiation %qD does not match "
22374 "declared type %qT", TREE_TYPE (result), decl,
22375 TREE_TYPE (decl));
22376 return;
22377 }
22378 }
22379 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22380 {
22381 error ("explicit instantiation of %q#D", decl);
22382 return;
22383 }
22384 else
22385 result = decl;
22386
22387 /* Check for various error cases. Note that if the explicit
22388 instantiation is valid the RESULT will currently be marked as an
22389 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22390 until we get here. */
22391
22392 if (DECL_TEMPLATE_SPECIALIZATION (result))
22393 {
22394 /* DR 259 [temp.spec].
22395
22396 Both an explicit instantiation and a declaration of an explicit
22397 specialization shall not appear in a program unless the explicit
22398 instantiation follows a declaration of the explicit specialization.
22399
22400 For a given set of template parameters, if an explicit
22401 instantiation of a template appears after a declaration of an
22402 explicit specialization for that template, the explicit
22403 instantiation has no effect. */
22404 return;
22405 }
22406 else if (DECL_EXPLICIT_INSTANTIATION (result))
22407 {
22408 /* [temp.spec]
22409
22410 No program shall explicitly instantiate any template more
22411 than once.
22412
22413 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22414 the first instantiation was `extern' and the second is not,
22415 and EXTERN_P for the opposite case. */
22416 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22417 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22418 /* If an "extern" explicit instantiation follows an ordinary
22419 explicit instantiation, the template is instantiated. */
22420 if (extern_p)
22421 return;
22422 }
22423 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22424 {
22425 error ("no matching template for %qD found", result);
22426 return;
22427 }
22428 else if (!DECL_TEMPLATE_INFO (result))
22429 {
22430 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22431 return;
22432 }
22433
22434 if (storage == NULL_TREE)
22435 ;
22436 else if (storage == ridpointers[(int) RID_EXTERN])
22437 {
22438 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22439 pedwarn (input_location, OPT_Wpedantic,
22440 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22441 "instantiations");
22442 extern_p = 1;
22443 }
22444 else
22445 error ("storage class %qD applied to template instantiation", storage);
22446
22447 check_explicit_instantiation_namespace (result);
22448 mark_decl_instantiated (result, extern_p);
22449 if (! extern_p)
22450 instantiate_decl (result, /*defer_ok=*/true,
22451 /*expl_inst_class_mem_p=*/false);
22452 }
22453
22454 static void
22455 mark_class_instantiated (tree t, int extern_p)
22456 {
22457 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22458 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22459 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22460 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22461 if (! extern_p)
22462 {
22463 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22464 rest_of_type_compilation (t, 1);
22465 }
22466 }
22467
22468 /* Called from do_type_instantiation through binding_table_foreach to
22469 do recursive instantiation for the type bound in ENTRY. */
22470 static void
22471 bt_instantiate_type_proc (binding_entry entry, void *data)
22472 {
22473 tree storage = *(tree *) data;
22474
22475 if (MAYBE_CLASS_TYPE_P (entry->type)
22476 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22477 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22478 }
22479
22480 /* Perform an explicit instantiation of template class T. STORAGE, if
22481 non-null, is the RID for extern, inline or static. COMPLAIN is
22482 nonzero if this is called from the parser, zero if called recursively,
22483 since the standard is unclear (as detailed below). */
22484
22485 void
22486 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22487 {
22488 int extern_p = 0;
22489 int nomem_p = 0;
22490 int static_p = 0;
22491 int previous_instantiation_extern_p = 0;
22492
22493 if (TREE_CODE (t) == TYPE_DECL)
22494 t = TREE_TYPE (t);
22495
22496 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22497 {
22498 tree tmpl =
22499 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22500 if (tmpl)
22501 error ("explicit instantiation of non-class template %qD", tmpl);
22502 else
22503 error ("explicit instantiation of non-template type %qT", t);
22504 return;
22505 }
22506
22507 complete_type (t);
22508
22509 if (!COMPLETE_TYPE_P (t))
22510 {
22511 if (complain & tf_error)
22512 error ("explicit instantiation of %q#T before definition of template",
22513 t);
22514 return;
22515 }
22516
22517 if (storage != NULL_TREE)
22518 {
22519 if (!in_system_header_at (input_location))
22520 {
22521 if (storage == ridpointers[(int) RID_EXTERN])
22522 {
22523 if (cxx_dialect == cxx98)
22524 pedwarn (input_location, OPT_Wpedantic,
22525 "ISO C++ 1998 forbids the use of %<extern%> on "
22526 "explicit instantiations");
22527 }
22528 else
22529 pedwarn (input_location, OPT_Wpedantic,
22530 "ISO C++ forbids the use of %qE"
22531 " on explicit instantiations", storage);
22532 }
22533
22534 if (storage == ridpointers[(int) RID_INLINE])
22535 nomem_p = 1;
22536 else if (storage == ridpointers[(int) RID_EXTERN])
22537 extern_p = 1;
22538 else if (storage == ridpointers[(int) RID_STATIC])
22539 static_p = 1;
22540 else
22541 {
22542 error ("storage class %qD applied to template instantiation",
22543 storage);
22544 extern_p = 0;
22545 }
22546 }
22547
22548 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22549 {
22550 /* DR 259 [temp.spec].
22551
22552 Both an explicit instantiation and a declaration of an explicit
22553 specialization shall not appear in a program unless the explicit
22554 instantiation follows a declaration of the explicit specialization.
22555
22556 For a given set of template parameters, if an explicit
22557 instantiation of a template appears after a declaration of an
22558 explicit specialization for that template, the explicit
22559 instantiation has no effect. */
22560 return;
22561 }
22562 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22563 {
22564 /* [temp.spec]
22565
22566 No program shall explicitly instantiate any template more
22567 than once.
22568
22569 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22570 instantiation was `extern'. If EXTERN_P then the second is.
22571 These cases are OK. */
22572 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22573
22574 if (!previous_instantiation_extern_p && !extern_p
22575 && (complain & tf_error))
22576 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22577
22578 /* If we've already instantiated the template, just return now. */
22579 if (!CLASSTYPE_INTERFACE_ONLY (t))
22580 return;
22581 }
22582
22583 check_explicit_instantiation_namespace (TYPE_NAME (t));
22584 mark_class_instantiated (t, extern_p);
22585
22586 if (nomem_p)
22587 return;
22588
22589 /* In contrast to implicit instantiation, where only the
22590 declarations, and not the definitions, of members are
22591 instantiated, we have here:
22592
22593 [temp.explicit]
22594
22595 The explicit instantiation of a class template specialization
22596 implies the instantiation of all of its members not
22597 previously explicitly specialized in the translation unit
22598 containing the explicit instantiation.
22599
22600 Of course, we can't instantiate member template classes, since we
22601 don't have any arguments for them. Note that the standard is
22602 unclear on whether the instantiation of the members are
22603 *explicit* instantiations or not. However, the most natural
22604 interpretation is that it should be an explicit
22605 instantiation. */
22606 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22607 if ((VAR_P (fld)
22608 || (TREE_CODE (fld) == FUNCTION_DECL
22609 && !static_p
22610 && user_provided_p (fld)))
22611 && DECL_TEMPLATE_INSTANTIATION (fld))
22612 {
22613 mark_decl_instantiated (fld, extern_p);
22614 if (! extern_p)
22615 instantiate_decl (fld, /*defer_ok=*/true,
22616 /*expl_inst_class_mem_p=*/true);
22617 }
22618
22619 if (CLASSTYPE_NESTED_UTDS (t))
22620 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22621 bt_instantiate_type_proc, &storage);
22622 }
22623
22624 /* Given a function DECL, which is a specialization of TMPL, modify
22625 DECL to be a re-instantiation of TMPL with the same template
22626 arguments. TMPL should be the template into which tsubst'ing
22627 should occur for DECL, not the most general template.
22628
22629 One reason for doing this is a scenario like this:
22630
22631 template <class T>
22632 void f(const T&, int i);
22633
22634 void g() { f(3, 7); }
22635
22636 template <class T>
22637 void f(const T& t, const int i) { }
22638
22639 Note that when the template is first instantiated, with
22640 instantiate_template, the resulting DECL will have no name for the
22641 first parameter, and the wrong type for the second. So, when we go
22642 to instantiate the DECL, we regenerate it. */
22643
22644 static void
22645 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22646 {
22647 /* The arguments used to instantiate DECL, from the most general
22648 template. */
22649 tree code_pattern;
22650
22651 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22652
22653 /* Make sure that we can see identifiers, and compute access
22654 correctly. */
22655 push_access_scope (decl);
22656
22657 if (TREE_CODE (decl) == FUNCTION_DECL)
22658 {
22659 tree decl_parm;
22660 tree pattern_parm;
22661 tree specs;
22662 int args_depth;
22663 int parms_depth;
22664
22665 args_depth = TMPL_ARGS_DEPTH (args);
22666 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22667 if (args_depth > parms_depth)
22668 args = get_innermost_template_args (args, parms_depth);
22669
22670 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22671 args, tf_error, NULL_TREE,
22672 /*defer_ok*/false);
22673 if (specs && specs != error_mark_node)
22674 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22675 specs);
22676
22677 /* Merge parameter declarations. */
22678 decl_parm = skip_artificial_parms_for (decl,
22679 DECL_ARGUMENTS (decl));
22680 pattern_parm
22681 = skip_artificial_parms_for (code_pattern,
22682 DECL_ARGUMENTS (code_pattern));
22683 while (decl_parm && !DECL_PACK_P (pattern_parm))
22684 {
22685 tree parm_type;
22686 tree attributes;
22687
22688 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22689 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22690 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22691 NULL_TREE);
22692 parm_type = type_decays_to (parm_type);
22693 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22694 TREE_TYPE (decl_parm) = parm_type;
22695 attributes = DECL_ATTRIBUTES (pattern_parm);
22696 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22697 {
22698 DECL_ATTRIBUTES (decl_parm) = attributes;
22699 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22700 }
22701 decl_parm = DECL_CHAIN (decl_parm);
22702 pattern_parm = DECL_CHAIN (pattern_parm);
22703 }
22704 /* Merge any parameters that match with the function parameter
22705 pack. */
22706 if (pattern_parm && DECL_PACK_P (pattern_parm))
22707 {
22708 int i, len;
22709 tree expanded_types;
22710 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22711 the parameters in this function parameter pack. */
22712 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22713 args, tf_error, NULL_TREE);
22714 len = TREE_VEC_LENGTH (expanded_types);
22715 for (i = 0; i < len; i++)
22716 {
22717 tree parm_type;
22718 tree attributes;
22719
22720 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22721 /* Rename the parameter to include the index. */
22722 DECL_NAME (decl_parm) =
22723 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22724 parm_type = TREE_VEC_ELT (expanded_types, i);
22725 parm_type = type_decays_to (parm_type);
22726 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22727 TREE_TYPE (decl_parm) = parm_type;
22728 attributes = DECL_ATTRIBUTES (pattern_parm);
22729 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22730 {
22731 DECL_ATTRIBUTES (decl_parm) = attributes;
22732 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22733 }
22734 decl_parm = DECL_CHAIN (decl_parm);
22735 }
22736 }
22737 /* Merge additional specifiers from the CODE_PATTERN. */
22738 if (DECL_DECLARED_INLINE_P (code_pattern)
22739 && !DECL_DECLARED_INLINE_P (decl))
22740 DECL_DECLARED_INLINE_P (decl) = 1;
22741 }
22742 else if (VAR_P (decl))
22743 {
22744 start_lambda_scope (decl);
22745 DECL_INITIAL (decl) =
22746 tsubst_expr (DECL_INITIAL (code_pattern), args,
22747 tf_error, DECL_TI_TEMPLATE (decl),
22748 /*integral_constant_expression_p=*/false);
22749 finish_lambda_scope ();
22750 if (VAR_HAD_UNKNOWN_BOUND (decl))
22751 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22752 tf_error, DECL_TI_TEMPLATE (decl));
22753 }
22754 else
22755 gcc_unreachable ();
22756
22757 pop_access_scope (decl);
22758 }
22759
22760 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22761 substituted to get DECL. */
22762
22763 tree
22764 template_for_substitution (tree decl)
22765 {
22766 tree tmpl = DECL_TI_TEMPLATE (decl);
22767
22768 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22769 for the instantiation. This is not always the most general
22770 template. Consider, for example:
22771
22772 template <class T>
22773 struct S { template <class U> void f();
22774 template <> void f<int>(); };
22775
22776 and an instantiation of S<double>::f<int>. We want TD to be the
22777 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22778 while (/* An instantiation cannot have a definition, so we need a
22779 more general template. */
22780 DECL_TEMPLATE_INSTANTIATION (tmpl)
22781 /* We must also deal with friend templates. Given:
22782
22783 template <class T> struct S {
22784 template <class U> friend void f() {};
22785 };
22786
22787 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22788 so far as the language is concerned, but that's still
22789 where we get the pattern for the instantiation from. On
22790 other hand, if the definition comes outside the class, say:
22791
22792 template <class T> struct S {
22793 template <class U> friend void f();
22794 };
22795 template <class U> friend void f() {}
22796
22797 we don't need to look any further. That's what the check for
22798 DECL_INITIAL is for. */
22799 || (TREE_CODE (decl) == FUNCTION_DECL
22800 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22801 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22802 {
22803 /* The present template, TD, should not be a definition. If it
22804 were a definition, we should be using it! Note that we
22805 cannot restructure the loop to just keep going until we find
22806 a template with a definition, since that might go too far if
22807 a specialization was declared, but not defined. */
22808
22809 /* Fetch the more general template. */
22810 tmpl = DECL_TI_TEMPLATE (tmpl);
22811 }
22812
22813 return tmpl;
22814 }
22815
22816 /* Returns true if we need to instantiate this template instance even if we
22817 know we aren't going to emit it. */
22818
22819 bool
22820 always_instantiate_p (tree decl)
22821 {
22822 /* We always instantiate inline functions so that we can inline them. An
22823 explicit instantiation declaration prohibits implicit instantiation of
22824 non-inline functions. With high levels of optimization, we would
22825 normally inline non-inline functions -- but we're not allowed to do
22826 that for "extern template" functions. Therefore, we check
22827 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22828 return ((TREE_CODE (decl) == FUNCTION_DECL
22829 && (DECL_DECLARED_INLINE_P (decl)
22830 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22831 /* And we need to instantiate static data members so that
22832 their initializers are available in integral constant
22833 expressions. */
22834 || (VAR_P (decl)
22835 && decl_maybe_constant_var_p (decl)));
22836 }
22837
22838 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22839 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22840 error, true otherwise. */
22841
22842 bool
22843 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22844 {
22845 tree fntype, spec, noex, clone;
22846
22847 /* Don't instantiate a noexcept-specification from template context. */
22848 if (processing_template_decl)
22849 return true;
22850
22851 if (DECL_CLONED_FUNCTION_P (fn))
22852 fn = DECL_CLONED_FUNCTION (fn);
22853 fntype = TREE_TYPE (fn);
22854 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22855
22856 if (!spec || !TREE_PURPOSE (spec))
22857 return true;
22858
22859 noex = TREE_PURPOSE (spec);
22860
22861 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22862 {
22863 static hash_set<tree>* fns = new hash_set<tree>;
22864 bool added = false;
22865 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22866 spec = get_defaulted_eh_spec (fn, complain);
22867 else if (!(added = !fns->add (fn)))
22868 {
22869 /* If hash_set::add returns true, the element was already there. */
22870 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22871 DECL_SOURCE_LOCATION (fn));
22872 error_at (loc,
22873 "exception specification of %qD depends on itself",
22874 fn);
22875 spec = noexcept_false_spec;
22876 }
22877 else if (push_tinst_level (fn))
22878 {
22879 push_access_scope (fn);
22880 push_deferring_access_checks (dk_no_deferred);
22881 input_location = DECL_SOURCE_LOCATION (fn);
22882 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22883 DEFERRED_NOEXCEPT_ARGS (noex),
22884 tf_warning_or_error, fn,
22885 /*function_p=*/false,
22886 /*integral_constant_expression_p=*/true);
22887 pop_deferring_access_checks ();
22888 pop_access_scope (fn);
22889 pop_tinst_level ();
22890 spec = build_noexcept_spec (noex, tf_warning_or_error);
22891 if (spec == error_mark_node)
22892 spec = noexcept_false_spec;
22893 }
22894 else
22895 spec = noexcept_false_spec;
22896
22897 if (added)
22898 fns->remove (fn);
22899
22900 if (spec == error_mark_node)
22901 return false;
22902
22903 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22904 }
22905
22906 FOR_EACH_CLONE (clone, fn)
22907 {
22908 if (TREE_TYPE (clone) == fntype)
22909 TREE_TYPE (clone) = TREE_TYPE (fn);
22910 else
22911 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22912 }
22913
22914 return true;
22915 }
22916
22917 /* We're starting to process the function INST, an instantiation of PATTERN;
22918 add their parameters to local_specializations. */
22919
22920 static void
22921 register_parameter_specializations (tree pattern, tree inst)
22922 {
22923 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22924 tree spec_parm = DECL_ARGUMENTS (inst);
22925 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22926 {
22927 register_local_specialization (spec_parm, tmpl_parm);
22928 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22929 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22930 }
22931 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22932 {
22933 if (!DECL_PACK_P (tmpl_parm))
22934 {
22935 register_local_specialization (spec_parm, tmpl_parm);
22936 spec_parm = DECL_CHAIN (spec_parm);
22937 }
22938 else
22939 {
22940 /* Register the (value) argument pack as a specialization of
22941 TMPL_PARM, then move on. */
22942 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22943 register_local_specialization (argpack, tmpl_parm);
22944 }
22945 }
22946 gcc_assert (!spec_parm);
22947 }
22948
22949 /* Produce the definition of D, a _DECL generated from a template. If
22950 DEFER_OK is true, then we don't have to actually do the
22951 instantiation now; we just have to do it sometime. Normally it is
22952 an error if this is an explicit instantiation but D is undefined.
22953 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22954 instantiated class template. */
22955
22956 tree
22957 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22958 {
22959 tree tmpl = DECL_TI_TEMPLATE (d);
22960 tree gen_args;
22961 tree args;
22962 tree td;
22963 tree code_pattern;
22964 tree spec;
22965 tree gen_tmpl;
22966 bool pattern_defined;
22967 location_t saved_loc = input_location;
22968 int saved_unevaluated_operand = cp_unevaluated_operand;
22969 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22970 bool external_p;
22971 bool deleted_p;
22972
22973 /* This function should only be used to instantiate templates for
22974 functions and static member variables. */
22975 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22976
22977 /* A concept is never instantiated. */
22978 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22979
22980 /* Variables are never deferred; if instantiation is required, they
22981 are instantiated right away. That allows for better code in the
22982 case that an expression refers to the value of the variable --
22983 if the variable has a constant value the referring expression can
22984 take advantage of that fact. */
22985 if (VAR_P (d))
22986 defer_ok = false;
22987
22988 /* Don't instantiate cloned functions. Instead, instantiate the
22989 functions they cloned. */
22990 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22991 d = DECL_CLONED_FUNCTION (d);
22992
22993 if (DECL_TEMPLATE_INSTANTIATED (d)
22994 || (TREE_CODE (d) == FUNCTION_DECL
22995 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22996 || DECL_TEMPLATE_SPECIALIZATION (d))
22997 /* D has already been instantiated or explicitly specialized, so
22998 there's nothing for us to do here.
22999
23000 It might seem reasonable to check whether or not D is an explicit
23001 instantiation, and, if so, stop here. But when an explicit
23002 instantiation is deferred until the end of the compilation,
23003 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23004 the instantiation. */
23005 return d;
23006
23007 /* Check to see whether we know that this template will be
23008 instantiated in some other file, as with "extern template"
23009 extension. */
23010 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23011
23012 /* In general, we do not instantiate such templates. */
23013 if (external_p && !always_instantiate_p (d))
23014 return d;
23015
23016 gen_tmpl = most_general_template (tmpl);
23017 gen_args = DECL_TI_ARGS (d);
23018
23019 if (tmpl != gen_tmpl)
23020 /* We should already have the extra args. */
23021 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23022 == TMPL_ARGS_DEPTH (gen_args));
23023 /* And what's in the hash table should match D. */
23024 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23025 || spec == NULL_TREE);
23026
23027 /* This needs to happen before any tsubsting. */
23028 if (! push_tinst_level (d))
23029 return d;
23030
23031 timevar_push (TV_TEMPLATE_INST);
23032
23033 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23034 for the instantiation. */
23035 td = template_for_substitution (d);
23036 args = gen_args;
23037
23038 if (VAR_P (d))
23039 {
23040 /* Look up an explicit specialization, if any. */
23041 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23042 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23043 if (elt && elt != error_mark_node)
23044 {
23045 td = TREE_VALUE (elt);
23046 args = TREE_PURPOSE (elt);
23047 }
23048 }
23049
23050 code_pattern = DECL_TEMPLATE_RESULT (td);
23051
23052 /* We should never be trying to instantiate a member of a class
23053 template or partial specialization. */
23054 gcc_assert (d != code_pattern);
23055
23056 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23057 || DECL_TEMPLATE_SPECIALIZATION (td))
23058 /* In the case of a friend template whose definition is provided
23059 outside the class, we may have too many arguments. Drop the
23060 ones we don't need. The same is true for specializations. */
23061 args = get_innermost_template_args
23062 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23063
23064 if (TREE_CODE (d) == FUNCTION_DECL)
23065 {
23066 deleted_p = DECL_DELETED_FN (code_pattern);
23067 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23068 && DECL_INITIAL (code_pattern) != error_mark_node)
23069 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23070 || deleted_p);
23071 }
23072 else
23073 {
23074 deleted_p = false;
23075 if (DECL_CLASS_SCOPE_P (code_pattern))
23076 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23077 || DECL_INLINE_VAR_P (code_pattern));
23078 else
23079 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23080 }
23081
23082 /* We may be in the middle of deferred access check. Disable it now. */
23083 push_deferring_access_checks (dk_no_deferred);
23084
23085 /* Unless an explicit instantiation directive has already determined
23086 the linkage of D, remember that a definition is available for
23087 this entity. */
23088 if (pattern_defined
23089 && !DECL_INTERFACE_KNOWN (d)
23090 && !DECL_NOT_REALLY_EXTERN (d))
23091 mark_definable (d);
23092
23093 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23094 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23095 input_location = DECL_SOURCE_LOCATION (d);
23096
23097 /* If D is a member of an explicitly instantiated class template,
23098 and no definition is available, treat it like an implicit
23099 instantiation. */
23100 if (!pattern_defined && expl_inst_class_mem_p
23101 && DECL_EXPLICIT_INSTANTIATION (d))
23102 {
23103 /* Leave linkage flags alone on instantiations with anonymous
23104 visibility. */
23105 if (TREE_PUBLIC (d))
23106 {
23107 DECL_NOT_REALLY_EXTERN (d) = 0;
23108 DECL_INTERFACE_KNOWN (d) = 0;
23109 }
23110 SET_DECL_IMPLICIT_INSTANTIATION (d);
23111 }
23112
23113 /* Defer all other templates, unless we have been explicitly
23114 forbidden from doing so. */
23115 if (/* If there is no definition, we cannot instantiate the
23116 template. */
23117 ! pattern_defined
23118 /* If it's OK to postpone instantiation, do so. */
23119 || defer_ok
23120 /* If this is a static data member that will be defined
23121 elsewhere, we don't want to instantiate the entire data
23122 member, but we do want to instantiate the initializer so that
23123 we can substitute that elsewhere. */
23124 || (external_p && VAR_P (d))
23125 /* Handle here a deleted function too, avoid generating
23126 its body (c++/61080). */
23127 || deleted_p)
23128 {
23129 /* The definition of the static data member is now required so
23130 we must substitute the initializer. */
23131 if (VAR_P (d)
23132 && !DECL_INITIAL (d)
23133 && DECL_INITIAL (code_pattern))
23134 {
23135 tree ns;
23136 tree init;
23137 bool const_init = false;
23138 bool enter_context = DECL_CLASS_SCOPE_P (d);
23139
23140 ns = decl_namespace_context (d);
23141 push_nested_namespace (ns);
23142 if (enter_context)
23143 push_nested_class (DECL_CONTEXT (d));
23144 init = tsubst_expr (DECL_INITIAL (code_pattern),
23145 args,
23146 tf_warning_or_error, NULL_TREE,
23147 /*integral_constant_expression_p=*/false);
23148 /* If instantiating the initializer involved instantiating this
23149 again, don't call cp_finish_decl twice. */
23150 if (!DECL_INITIAL (d))
23151 {
23152 /* Make sure the initializer is still constant, in case of
23153 circular dependency (template/instantiate6.C). */
23154 const_init
23155 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23156 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23157 /*asmspec_tree=*/NULL_TREE,
23158 LOOKUP_ONLYCONVERTING);
23159 }
23160 if (enter_context)
23161 pop_nested_class ();
23162 pop_nested_namespace (ns);
23163 }
23164
23165 /* We restore the source position here because it's used by
23166 add_pending_template. */
23167 input_location = saved_loc;
23168
23169 if (at_eof && !pattern_defined
23170 && DECL_EXPLICIT_INSTANTIATION (d)
23171 && DECL_NOT_REALLY_EXTERN (d))
23172 /* [temp.explicit]
23173
23174 The definition of a non-exported function template, a
23175 non-exported member function template, or a non-exported
23176 member function or static data member of a class template
23177 shall be present in every translation unit in which it is
23178 explicitly instantiated. */
23179 permerror (input_location, "explicit instantiation of %qD "
23180 "but no definition available", d);
23181
23182 /* If we're in unevaluated context, we just wanted to get the
23183 constant value; this isn't an odr use, so don't queue
23184 a full instantiation. */
23185 if (cp_unevaluated_operand != 0)
23186 goto out;
23187 /* ??? Historically, we have instantiated inline functions, even
23188 when marked as "extern template". */
23189 if (!(external_p && VAR_P (d)))
23190 add_pending_template (d);
23191 goto out;
23192 }
23193 /* Tell the repository that D is available in this translation unit
23194 -- and see if it is supposed to be instantiated here. */
23195 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23196 {
23197 /* In a PCH file, despite the fact that the repository hasn't
23198 requested instantiation in the PCH it is still possible that
23199 an instantiation will be required in a file that includes the
23200 PCH. */
23201 if (pch_file)
23202 add_pending_template (d);
23203 /* Instantiate inline functions so that the inliner can do its
23204 job, even though we'll not be emitting a copy of this
23205 function. */
23206 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23207 goto out;
23208 }
23209
23210 bool push_to_top, nested;
23211 tree fn_context;
23212 fn_context = decl_function_context (d);
23213 nested = current_function_decl != NULL_TREE;
23214 push_to_top = !(nested && fn_context == current_function_decl);
23215
23216 vec<tree> omp_privatization_save;
23217 if (nested)
23218 save_omp_privatization_clauses (omp_privatization_save);
23219
23220 if (push_to_top)
23221 push_to_top_level ();
23222 else
23223 {
23224 push_function_context ();
23225 cp_unevaluated_operand = 0;
23226 c_inhibit_evaluation_warnings = 0;
23227 }
23228
23229 /* Mark D as instantiated so that recursive calls to
23230 instantiate_decl do not try to instantiate it again. */
23231 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23232
23233 /* Regenerate the declaration in case the template has been modified
23234 by a subsequent redeclaration. */
23235 regenerate_decl_from_template (d, td, args);
23236
23237 /* We already set the file and line above. Reset them now in case
23238 they changed as a result of calling regenerate_decl_from_template. */
23239 input_location = DECL_SOURCE_LOCATION (d);
23240
23241 if (VAR_P (d))
23242 {
23243 tree init;
23244 bool const_init = false;
23245
23246 /* Clear out DECL_RTL; whatever was there before may not be right
23247 since we've reset the type of the declaration. */
23248 SET_DECL_RTL (d, NULL);
23249 DECL_IN_AGGR_P (d) = 0;
23250
23251 /* The initializer is placed in DECL_INITIAL by
23252 regenerate_decl_from_template so we don't need to
23253 push/pop_access_scope again here. Pull it out so that
23254 cp_finish_decl can process it. */
23255 init = DECL_INITIAL (d);
23256 DECL_INITIAL (d) = NULL_TREE;
23257 DECL_INITIALIZED_P (d) = 0;
23258
23259 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23260 initializer. That function will defer actual emission until
23261 we have a chance to determine linkage. */
23262 DECL_EXTERNAL (d) = 0;
23263
23264 /* Enter the scope of D so that access-checking works correctly. */
23265 bool enter_context = DECL_CLASS_SCOPE_P (d);
23266 if (enter_context)
23267 push_nested_class (DECL_CONTEXT (d));
23268
23269 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23270 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23271
23272 if (enter_context)
23273 pop_nested_class ();
23274
23275 if (variable_template_p (gen_tmpl))
23276 note_variable_template_instantiation (d);
23277 }
23278 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23279 synthesize_method (d);
23280 else if (TREE_CODE (d) == FUNCTION_DECL)
23281 {
23282 /* Set up the list of local specializations. */
23283 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23284 tree block = NULL_TREE;
23285
23286 /* Set up context. */
23287 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23288 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23289 block = push_stmt_list ();
23290 else
23291 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23292
23293 /* Some typedefs referenced from within the template code need to be
23294 access checked at template instantiation time, i.e now. These
23295 types were added to the template at parsing time. Let's get those
23296 and perform the access checks then. */
23297 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23298 args);
23299
23300 /* Create substitution entries for the parameters. */
23301 register_parameter_specializations (code_pattern, d);
23302
23303 /* Substitute into the body of the function. */
23304 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23305 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23306 tf_warning_or_error, tmpl);
23307 else
23308 {
23309 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23310 tf_warning_or_error, tmpl,
23311 /*integral_constant_expression_p=*/false);
23312
23313 /* Set the current input_location to the end of the function
23314 so that finish_function knows where we are. */
23315 input_location
23316 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23317
23318 /* Remember if we saw an infinite loop in the template. */
23319 current_function_infinite_loop
23320 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23321 }
23322
23323 /* Finish the function. */
23324 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23325 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23326 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23327 else
23328 {
23329 d = finish_function (/*inline_p=*/false);
23330 expand_or_defer_fn (d);
23331 }
23332
23333 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23334 cp_check_omp_declare_reduction (d);
23335 }
23336
23337 /* We're not deferring instantiation any more. */
23338 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23339
23340 if (push_to_top)
23341 pop_from_top_level ();
23342 else
23343 pop_function_context ();
23344
23345 if (nested)
23346 restore_omp_privatization_clauses (omp_privatization_save);
23347
23348 out:
23349 pop_deferring_access_checks ();
23350 timevar_pop (TV_TEMPLATE_INST);
23351 pop_tinst_level ();
23352 input_location = saved_loc;
23353 cp_unevaluated_operand = saved_unevaluated_operand;
23354 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23355
23356 return d;
23357 }
23358
23359 /* Run through the list of templates that we wish we could
23360 instantiate, and instantiate any we can. RETRIES is the
23361 number of times we retry pending template instantiation. */
23362
23363 void
23364 instantiate_pending_templates (int retries)
23365 {
23366 int reconsider;
23367 location_t saved_loc = input_location;
23368
23369 /* Instantiating templates may trigger vtable generation. This in turn
23370 may require further template instantiations. We place a limit here
23371 to avoid infinite loop. */
23372 if (pending_templates && retries >= max_tinst_depth)
23373 {
23374 tree decl = pending_templates->tinst->decl;
23375
23376 fatal_error (input_location,
23377 "template instantiation depth exceeds maximum of %d"
23378 " instantiating %q+D, possibly from virtual table generation"
23379 " (use -ftemplate-depth= to increase the maximum)",
23380 max_tinst_depth, decl);
23381 if (TREE_CODE (decl) == FUNCTION_DECL)
23382 /* Pretend that we defined it. */
23383 DECL_INITIAL (decl) = error_mark_node;
23384 return;
23385 }
23386
23387 do
23388 {
23389 struct pending_template **t = &pending_templates;
23390 struct pending_template *last = NULL;
23391 reconsider = 0;
23392 while (*t)
23393 {
23394 tree instantiation = reopen_tinst_level ((*t)->tinst);
23395 bool complete = false;
23396
23397 if (TYPE_P (instantiation))
23398 {
23399 if (!COMPLETE_TYPE_P (instantiation))
23400 {
23401 instantiate_class_template (instantiation);
23402 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23403 for (tree fld = TYPE_FIELDS (instantiation);
23404 fld; fld = TREE_CHAIN (fld))
23405 if ((VAR_P (fld)
23406 || (TREE_CODE (fld) == FUNCTION_DECL
23407 && !DECL_ARTIFICIAL (fld)))
23408 && DECL_TEMPLATE_INSTANTIATION (fld))
23409 instantiate_decl (fld,
23410 /*defer_ok=*/false,
23411 /*expl_inst_class_mem_p=*/false);
23412
23413 if (COMPLETE_TYPE_P (instantiation))
23414 reconsider = 1;
23415 }
23416
23417 complete = COMPLETE_TYPE_P (instantiation);
23418 }
23419 else
23420 {
23421 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23422 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23423 {
23424 instantiation
23425 = instantiate_decl (instantiation,
23426 /*defer_ok=*/false,
23427 /*expl_inst_class_mem_p=*/false);
23428 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23429 reconsider = 1;
23430 }
23431
23432 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23433 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23434 }
23435
23436 if (complete)
23437 /* If INSTANTIATION has been instantiated, then we don't
23438 need to consider it again in the future. */
23439 *t = (*t)->next;
23440 else
23441 {
23442 last = *t;
23443 t = &(*t)->next;
23444 }
23445 tinst_depth = 0;
23446 current_tinst_level = NULL;
23447 }
23448 last_pending_template = last;
23449 }
23450 while (reconsider);
23451
23452 input_location = saved_loc;
23453 }
23454
23455 /* Substitute ARGVEC into T, which is a list of initializers for
23456 either base class or a non-static data member. The TREE_PURPOSEs
23457 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23458 instantiate_decl. */
23459
23460 static tree
23461 tsubst_initializer_list (tree t, tree argvec)
23462 {
23463 tree inits = NULL_TREE;
23464
23465 for (; t; t = TREE_CHAIN (t))
23466 {
23467 tree decl;
23468 tree init;
23469 tree expanded_bases = NULL_TREE;
23470 tree expanded_arguments = NULL_TREE;
23471 int i, len = 1;
23472
23473 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23474 {
23475 tree expr;
23476 tree arg;
23477
23478 /* Expand the base class expansion type into separate base
23479 classes. */
23480 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23481 tf_warning_or_error,
23482 NULL_TREE);
23483 if (expanded_bases == error_mark_node)
23484 continue;
23485
23486 /* We'll be building separate TREE_LISTs of arguments for
23487 each base. */
23488 len = TREE_VEC_LENGTH (expanded_bases);
23489 expanded_arguments = make_tree_vec (len);
23490 for (i = 0; i < len; i++)
23491 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23492
23493 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23494 expand each argument in the TREE_VALUE of t. */
23495 expr = make_node (EXPR_PACK_EXPANSION);
23496 PACK_EXPANSION_LOCAL_P (expr) = true;
23497 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23498 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23499
23500 if (TREE_VALUE (t) == void_type_node)
23501 /* VOID_TYPE_NODE is used to indicate
23502 value-initialization. */
23503 {
23504 for (i = 0; i < len; i++)
23505 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23506 }
23507 else
23508 {
23509 /* Substitute parameter packs into each argument in the
23510 TREE_LIST. */
23511 in_base_initializer = 1;
23512 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23513 {
23514 tree expanded_exprs;
23515
23516 /* Expand the argument. */
23517 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23518 expanded_exprs
23519 = tsubst_pack_expansion (expr, argvec,
23520 tf_warning_or_error,
23521 NULL_TREE);
23522 if (expanded_exprs == error_mark_node)
23523 continue;
23524
23525 /* Prepend each of the expanded expressions to the
23526 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23527 for (i = 0; i < len; i++)
23528 {
23529 TREE_VEC_ELT (expanded_arguments, i) =
23530 tree_cons (NULL_TREE,
23531 TREE_VEC_ELT (expanded_exprs, i),
23532 TREE_VEC_ELT (expanded_arguments, i));
23533 }
23534 }
23535 in_base_initializer = 0;
23536
23537 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23538 since we built them backwards. */
23539 for (i = 0; i < len; i++)
23540 {
23541 TREE_VEC_ELT (expanded_arguments, i) =
23542 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23543 }
23544 }
23545 }
23546
23547 for (i = 0; i < len; ++i)
23548 {
23549 if (expanded_bases)
23550 {
23551 decl = TREE_VEC_ELT (expanded_bases, i);
23552 decl = expand_member_init (decl);
23553 init = TREE_VEC_ELT (expanded_arguments, i);
23554 }
23555 else
23556 {
23557 tree tmp;
23558 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23559 tf_warning_or_error, NULL_TREE);
23560
23561 decl = expand_member_init (decl);
23562 if (decl && !DECL_P (decl))
23563 in_base_initializer = 1;
23564
23565 init = TREE_VALUE (t);
23566 tmp = init;
23567 if (init != void_type_node)
23568 init = tsubst_expr (init, argvec,
23569 tf_warning_or_error, NULL_TREE,
23570 /*integral_constant_expression_p=*/false);
23571 if (init == NULL_TREE && tmp != NULL_TREE)
23572 /* If we had an initializer but it instantiated to nothing,
23573 value-initialize the object. This will only occur when
23574 the initializer was a pack expansion where the parameter
23575 packs used in that expansion were of length zero. */
23576 init = void_type_node;
23577 in_base_initializer = 0;
23578 }
23579
23580 if (decl)
23581 {
23582 init = build_tree_list (decl, init);
23583 TREE_CHAIN (init) = inits;
23584 inits = init;
23585 }
23586 }
23587 }
23588 return inits;
23589 }
23590
23591 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23592
23593 static void
23594 set_current_access_from_decl (tree decl)
23595 {
23596 if (TREE_PRIVATE (decl))
23597 current_access_specifier = access_private_node;
23598 else if (TREE_PROTECTED (decl))
23599 current_access_specifier = access_protected_node;
23600 else
23601 current_access_specifier = access_public_node;
23602 }
23603
23604 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23605 is the instantiation (which should have been created with
23606 start_enum) and ARGS are the template arguments to use. */
23607
23608 static void
23609 tsubst_enum (tree tag, tree newtag, tree args)
23610 {
23611 tree e;
23612
23613 if (SCOPED_ENUM_P (newtag))
23614 begin_scope (sk_scoped_enum, newtag);
23615
23616 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23617 {
23618 tree value;
23619 tree decl;
23620
23621 decl = TREE_VALUE (e);
23622 /* Note that in a template enum, the TREE_VALUE is the
23623 CONST_DECL, not the corresponding INTEGER_CST. */
23624 value = tsubst_expr (DECL_INITIAL (decl),
23625 args, tf_warning_or_error, NULL_TREE,
23626 /*integral_constant_expression_p=*/true);
23627
23628 /* Give this enumeration constant the correct access. */
23629 set_current_access_from_decl (decl);
23630
23631 /* Actually build the enumerator itself. Here we're assuming that
23632 enumerators can't have dependent attributes. */
23633 build_enumerator (DECL_NAME (decl), value, newtag,
23634 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23635 }
23636
23637 if (SCOPED_ENUM_P (newtag))
23638 finish_scope ();
23639
23640 finish_enum_value_list (newtag);
23641 finish_enum (newtag);
23642
23643 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23644 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23645 }
23646
23647 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23648 its type -- but without substituting the innermost set of template
23649 arguments. So, innermost set of template parameters will appear in
23650 the type. */
23651
23652 tree
23653 get_mostly_instantiated_function_type (tree decl)
23654 {
23655 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23656 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23657 }
23658
23659 /* Return truthvalue if we're processing a template different from
23660 the last one involved in diagnostics. */
23661 bool
23662 problematic_instantiation_changed (void)
23663 {
23664 return current_tinst_level != last_error_tinst_level;
23665 }
23666
23667 /* Remember current template involved in diagnostics. */
23668 void
23669 record_last_problematic_instantiation (void)
23670 {
23671 last_error_tinst_level = current_tinst_level;
23672 }
23673
23674 struct tinst_level *
23675 current_instantiation (void)
23676 {
23677 return current_tinst_level;
23678 }
23679
23680 /* Return TRUE if current_function_decl is being instantiated, false
23681 otherwise. */
23682
23683 bool
23684 instantiating_current_function_p (void)
23685 {
23686 return (current_instantiation ()
23687 && current_instantiation ()->decl == current_function_decl);
23688 }
23689
23690 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23691 type. Return false for ok, true for disallowed. Issue error and
23692 inform messages under control of COMPLAIN. */
23693
23694 static bool
23695 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23696 {
23697 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23698 return false;
23699 else if (POINTER_TYPE_P (type))
23700 return false;
23701 else if (TYPE_PTRMEM_P (type))
23702 return false;
23703 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23704 return false;
23705 else if (TREE_CODE (type) == TYPENAME_TYPE)
23706 return false;
23707 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23708 return false;
23709 else if (TREE_CODE (type) == NULLPTR_TYPE)
23710 return false;
23711 /* A bound template template parm could later be instantiated to have a valid
23712 nontype parm type via an alias template. */
23713 else if (cxx_dialect >= cxx11
23714 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23715 return false;
23716
23717 if (complain & tf_error)
23718 {
23719 if (type == error_mark_node)
23720 inform (input_location, "invalid template non-type parameter");
23721 else
23722 error ("%q#T is not a valid type for a template non-type parameter",
23723 type);
23724 }
23725 return true;
23726 }
23727
23728 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23729 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23730
23731 static bool
23732 dependent_type_p_r (tree type)
23733 {
23734 tree scope;
23735
23736 /* [temp.dep.type]
23737
23738 A type is dependent if it is:
23739
23740 -- a template parameter. Template template parameters are types
23741 for us (since TYPE_P holds true for them) so we handle
23742 them here. */
23743 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23744 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23745 return true;
23746 /* -- a qualified-id with a nested-name-specifier which contains a
23747 class-name that names a dependent type or whose unqualified-id
23748 names a dependent type. */
23749 if (TREE_CODE (type) == TYPENAME_TYPE)
23750 return true;
23751
23752 /* An alias template specialization can be dependent even if the
23753 resulting type is not. */
23754 if (dependent_alias_template_spec_p (type))
23755 return true;
23756
23757 /* -- a cv-qualified type where the cv-unqualified type is
23758 dependent.
23759 No code is necessary for this bullet; the code below handles
23760 cv-qualified types, and we don't want to strip aliases with
23761 TYPE_MAIN_VARIANT because of DR 1558. */
23762 /* -- a compound type constructed from any dependent type. */
23763 if (TYPE_PTRMEM_P (type))
23764 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23765 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23766 (type)));
23767 else if (TYPE_PTR_P (type)
23768 || TREE_CODE (type) == REFERENCE_TYPE)
23769 return dependent_type_p (TREE_TYPE (type));
23770 else if (TREE_CODE (type) == FUNCTION_TYPE
23771 || TREE_CODE (type) == METHOD_TYPE)
23772 {
23773 tree arg_type;
23774
23775 if (dependent_type_p (TREE_TYPE (type)))
23776 return true;
23777 for (arg_type = TYPE_ARG_TYPES (type);
23778 arg_type;
23779 arg_type = TREE_CHAIN (arg_type))
23780 if (dependent_type_p (TREE_VALUE (arg_type)))
23781 return true;
23782 if (cxx_dialect >= cxx17)
23783 /* A value-dependent noexcept-specifier makes the type dependent. */
23784 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23785 if (tree noex = TREE_PURPOSE (spec))
23786 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23787 affect overload resolution and treating it as dependent breaks
23788 things. */
23789 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23790 && value_dependent_expression_p (noex))
23791 return true;
23792 return false;
23793 }
23794 /* -- an array type constructed from any dependent type or whose
23795 size is specified by a constant expression that is
23796 value-dependent.
23797
23798 We checked for type- and value-dependence of the bounds in
23799 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23800 if (TREE_CODE (type) == ARRAY_TYPE)
23801 {
23802 if (TYPE_DOMAIN (type)
23803 && dependent_type_p (TYPE_DOMAIN (type)))
23804 return true;
23805 return dependent_type_p (TREE_TYPE (type));
23806 }
23807
23808 /* -- a template-id in which either the template name is a template
23809 parameter ... */
23810 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23811 return true;
23812 /* ... or any of the template arguments is a dependent type or
23813 an expression that is type-dependent or value-dependent. */
23814 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23815 && (any_dependent_template_arguments_p
23816 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23817 return true;
23818
23819 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23820 dependent; if the argument of the `typeof' expression is not
23821 type-dependent, then it should already been have resolved. */
23822 if (TREE_CODE (type) == TYPEOF_TYPE
23823 || TREE_CODE (type) == DECLTYPE_TYPE
23824 || TREE_CODE (type) == UNDERLYING_TYPE)
23825 return true;
23826
23827 /* A template argument pack is dependent if any of its packed
23828 arguments are. */
23829 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23830 {
23831 tree args = ARGUMENT_PACK_ARGS (type);
23832 int i, len = TREE_VEC_LENGTH (args);
23833 for (i = 0; i < len; ++i)
23834 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23835 return true;
23836 }
23837
23838 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23839 be template parameters. */
23840 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23841 return true;
23842
23843 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23844 return true;
23845
23846 /* The standard does not specifically mention types that are local
23847 to template functions or local classes, but they should be
23848 considered dependent too. For example:
23849
23850 template <int I> void f() {
23851 enum E { a = I };
23852 S<sizeof (E)> s;
23853 }
23854
23855 The size of `E' cannot be known until the value of `I' has been
23856 determined. Therefore, `E' must be considered dependent. */
23857 scope = TYPE_CONTEXT (type);
23858 if (scope && TYPE_P (scope))
23859 return dependent_type_p (scope);
23860 /* Don't use type_dependent_expression_p here, as it can lead
23861 to infinite recursion trying to determine whether a lambda
23862 nested in a lambda is dependent (c++/47687). */
23863 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23864 && DECL_LANG_SPECIFIC (scope)
23865 && DECL_TEMPLATE_INFO (scope)
23866 && (any_dependent_template_arguments_p
23867 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23868 return true;
23869
23870 /* Other types are non-dependent. */
23871 return false;
23872 }
23873
23874 /* Returns TRUE if TYPE is dependent, in the sense of
23875 [temp.dep.type]. Note that a NULL type is considered dependent. */
23876
23877 bool
23878 dependent_type_p (tree type)
23879 {
23880 /* If there are no template parameters in scope, then there can't be
23881 any dependent types. */
23882 if (!processing_template_decl)
23883 {
23884 /* If we are not processing a template, then nobody should be
23885 providing us with a dependent type. */
23886 gcc_assert (type);
23887 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23888 return false;
23889 }
23890
23891 /* If the type is NULL, we have not computed a type for the entity
23892 in question; in that case, the type is dependent. */
23893 if (!type)
23894 return true;
23895
23896 /* Erroneous types can be considered non-dependent. */
23897 if (type == error_mark_node)
23898 return false;
23899
23900 /* Getting here with global_type_node means we improperly called this
23901 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23902 gcc_checking_assert (type != global_type_node);
23903
23904 /* If we have not already computed the appropriate value for TYPE,
23905 do so now. */
23906 if (!TYPE_DEPENDENT_P_VALID (type))
23907 {
23908 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23909 TYPE_DEPENDENT_P_VALID (type) = 1;
23910 }
23911
23912 return TYPE_DEPENDENT_P (type);
23913 }
23914
23915 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23916 lookup. In other words, a dependent type that is not the current
23917 instantiation. */
23918
23919 bool
23920 dependent_scope_p (tree scope)
23921 {
23922 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23923 && !currently_open_class (scope));
23924 }
23925
23926 /* T is a SCOPE_REF; return whether we need to consider it
23927 instantiation-dependent so that we can check access at instantiation
23928 time even though we know which member it resolves to. */
23929
23930 static bool
23931 instantiation_dependent_scope_ref_p (tree t)
23932 {
23933 if (DECL_P (TREE_OPERAND (t, 1))
23934 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23935 && accessible_in_template_p (TREE_OPERAND (t, 0),
23936 TREE_OPERAND (t, 1)))
23937 return false;
23938 else
23939 return true;
23940 }
23941
23942 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23943 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23944 expression. */
23945
23946 /* Note that this predicate is not appropriate for general expressions;
23947 only constant expressions (that satisfy potential_constant_expression)
23948 can be tested for value dependence. */
23949
23950 bool
23951 value_dependent_expression_p (tree expression)
23952 {
23953 if (!processing_template_decl || expression == NULL_TREE)
23954 return false;
23955
23956 /* A type-dependent expression is also value-dependent. */
23957 if (type_dependent_expression_p (expression))
23958 return true;
23959
23960 switch (TREE_CODE (expression))
23961 {
23962 case BASELINK:
23963 /* A dependent member function of the current instantiation. */
23964 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23965
23966 case FUNCTION_DECL:
23967 /* A dependent member function of the current instantiation. */
23968 if (DECL_CLASS_SCOPE_P (expression)
23969 && dependent_type_p (DECL_CONTEXT (expression)))
23970 return true;
23971 break;
23972
23973 case IDENTIFIER_NODE:
23974 /* A name that has not been looked up -- must be dependent. */
23975 return true;
23976
23977 case TEMPLATE_PARM_INDEX:
23978 /* A non-type template parm. */
23979 return true;
23980
23981 case CONST_DECL:
23982 /* A non-type template parm. */
23983 if (DECL_TEMPLATE_PARM_P (expression))
23984 return true;
23985 return value_dependent_expression_p (DECL_INITIAL (expression));
23986
23987 case VAR_DECL:
23988 /* A constant with literal type and is initialized
23989 with an expression that is value-dependent. */
23990 if (DECL_DEPENDENT_INIT_P (expression)
23991 /* FIXME cp_finish_decl doesn't fold reference initializers. */
23992 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
23993 return true;
23994 if (DECL_HAS_VALUE_EXPR_P (expression))
23995 {
23996 tree value_expr = DECL_VALUE_EXPR (expression);
23997 if (value_dependent_expression_p (value_expr))
23998 return true;
23999 }
24000 return false;
24001
24002 case DYNAMIC_CAST_EXPR:
24003 case STATIC_CAST_EXPR:
24004 case CONST_CAST_EXPR:
24005 case REINTERPRET_CAST_EXPR:
24006 case CAST_EXPR:
24007 case IMPLICIT_CONV_EXPR:
24008 /* These expressions are value-dependent if the type to which
24009 the cast occurs is dependent or the expression being casted
24010 is value-dependent. */
24011 {
24012 tree type = TREE_TYPE (expression);
24013
24014 if (dependent_type_p (type))
24015 return true;
24016
24017 /* A functional cast has a list of operands. */
24018 expression = TREE_OPERAND (expression, 0);
24019 if (!expression)
24020 {
24021 /* If there are no operands, it must be an expression such
24022 as "int()". This should not happen for aggregate types
24023 because it would form non-constant expressions. */
24024 gcc_assert (cxx_dialect >= cxx11
24025 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24026
24027 return false;
24028 }
24029
24030 if (TREE_CODE (expression) == TREE_LIST)
24031 return any_value_dependent_elements_p (expression);
24032
24033 return value_dependent_expression_p (expression);
24034 }
24035
24036 case SIZEOF_EXPR:
24037 if (SIZEOF_EXPR_TYPE_P (expression))
24038 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24039 /* FALLTHRU */
24040 case ALIGNOF_EXPR:
24041 case TYPEID_EXPR:
24042 /* A `sizeof' expression is value-dependent if the operand is
24043 type-dependent or is a pack expansion. */
24044 expression = TREE_OPERAND (expression, 0);
24045 if (PACK_EXPANSION_P (expression))
24046 return true;
24047 else if (TYPE_P (expression))
24048 return dependent_type_p (expression);
24049 return instantiation_dependent_uneval_expression_p (expression);
24050
24051 case AT_ENCODE_EXPR:
24052 /* An 'encode' expression is value-dependent if the operand is
24053 type-dependent. */
24054 expression = TREE_OPERAND (expression, 0);
24055 return dependent_type_p (expression);
24056
24057 case NOEXCEPT_EXPR:
24058 expression = TREE_OPERAND (expression, 0);
24059 return instantiation_dependent_uneval_expression_p (expression);
24060
24061 case SCOPE_REF:
24062 /* All instantiation-dependent expressions should also be considered
24063 value-dependent. */
24064 return instantiation_dependent_scope_ref_p (expression);
24065
24066 case COMPONENT_REF:
24067 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24068 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24069
24070 case NONTYPE_ARGUMENT_PACK:
24071 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24072 is value-dependent. */
24073 {
24074 tree values = ARGUMENT_PACK_ARGS (expression);
24075 int i, len = TREE_VEC_LENGTH (values);
24076
24077 for (i = 0; i < len; ++i)
24078 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24079 return true;
24080
24081 return false;
24082 }
24083
24084 case TRAIT_EXPR:
24085 {
24086 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24087
24088 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24089 return true;
24090
24091 if (!type2)
24092 return false;
24093
24094 if (TREE_CODE (type2) != TREE_LIST)
24095 return dependent_type_p (type2);
24096
24097 for (; type2; type2 = TREE_CHAIN (type2))
24098 if (dependent_type_p (TREE_VALUE (type2)))
24099 return true;
24100
24101 return false;
24102 }
24103
24104 case MODOP_EXPR:
24105 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24106 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24107
24108 case ARRAY_REF:
24109 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24110 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24111
24112 case ADDR_EXPR:
24113 {
24114 tree op = TREE_OPERAND (expression, 0);
24115 return (value_dependent_expression_p (op)
24116 || has_value_dependent_address (op));
24117 }
24118
24119 case REQUIRES_EXPR:
24120 /* Treat all requires-expressions as value-dependent so
24121 we don't try to fold them. */
24122 return true;
24123
24124 case TYPE_REQ:
24125 return dependent_type_p (TREE_OPERAND (expression, 0));
24126
24127 case CALL_EXPR:
24128 {
24129 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24130 return true;
24131 tree fn = get_callee_fndecl (expression);
24132 int i, nargs;
24133 nargs = call_expr_nargs (expression);
24134 for (i = 0; i < nargs; ++i)
24135 {
24136 tree op = CALL_EXPR_ARG (expression, i);
24137 /* In a call to a constexpr member function, look through the
24138 implicit ADDR_EXPR on the object argument so that it doesn't
24139 cause the call to be considered value-dependent. We also
24140 look through it in potential_constant_expression. */
24141 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24142 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24143 && TREE_CODE (op) == ADDR_EXPR)
24144 op = TREE_OPERAND (op, 0);
24145 if (value_dependent_expression_p (op))
24146 return true;
24147 }
24148 return false;
24149 }
24150
24151 case TEMPLATE_ID_EXPR:
24152 return variable_concept_p (TREE_OPERAND (expression, 0));
24153
24154 case CONSTRUCTOR:
24155 {
24156 unsigned ix;
24157 tree val;
24158 if (dependent_type_p (TREE_TYPE (expression)))
24159 return true;
24160 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24161 if (value_dependent_expression_p (val))
24162 return true;
24163 return false;
24164 }
24165
24166 case STMT_EXPR:
24167 /* Treat a GNU statement expression as dependent to avoid crashing
24168 under instantiate_non_dependent_expr; it can't be constant. */
24169 return true;
24170
24171 default:
24172 /* A constant expression is value-dependent if any subexpression is
24173 value-dependent. */
24174 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24175 {
24176 case tcc_reference:
24177 case tcc_unary:
24178 case tcc_comparison:
24179 case tcc_binary:
24180 case tcc_expression:
24181 case tcc_vl_exp:
24182 {
24183 int i, len = cp_tree_operand_length (expression);
24184
24185 for (i = 0; i < len; i++)
24186 {
24187 tree t = TREE_OPERAND (expression, i);
24188
24189 /* In some cases, some of the operands may be missing.
24190 (For example, in the case of PREDECREMENT_EXPR, the
24191 amount to increment by may be missing.) That doesn't
24192 make the expression dependent. */
24193 if (t && value_dependent_expression_p (t))
24194 return true;
24195 }
24196 }
24197 break;
24198 default:
24199 break;
24200 }
24201 break;
24202 }
24203
24204 /* The expression is not value-dependent. */
24205 return false;
24206 }
24207
24208 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24209 [temp.dep.expr]. Note that an expression with no type is
24210 considered dependent. Other parts of the compiler arrange for an
24211 expression with type-dependent subexpressions to have no type, so
24212 this function doesn't have to be fully recursive. */
24213
24214 bool
24215 type_dependent_expression_p (tree expression)
24216 {
24217 if (!processing_template_decl)
24218 return false;
24219
24220 if (expression == NULL_TREE || expression == error_mark_node)
24221 return false;
24222
24223 /* An unresolved name is always dependent. */
24224 if (identifier_p (expression)
24225 || TREE_CODE (expression) == USING_DECL
24226 || TREE_CODE (expression) == WILDCARD_DECL)
24227 return true;
24228
24229 /* A fold expression is type-dependent. */
24230 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24231 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24232 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24233 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24234 return true;
24235
24236 /* Some expression forms are never type-dependent. */
24237 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24238 || TREE_CODE (expression) == SIZEOF_EXPR
24239 || TREE_CODE (expression) == ALIGNOF_EXPR
24240 || TREE_CODE (expression) == AT_ENCODE_EXPR
24241 || TREE_CODE (expression) == NOEXCEPT_EXPR
24242 || TREE_CODE (expression) == TRAIT_EXPR
24243 || TREE_CODE (expression) == TYPEID_EXPR
24244 || TREE_CODE (expression) == DELETE_EXPR
24245 || TREE_CODE (expression) == VEC_DELETE_EXPR
24246 || TREE_CODE (expression) == THROW_EXPR
24247 || TREE_CODE (expression) == REQUIRES_EXPR)
24248 return false;
24249
24250 /* The types of these expressions depends only on the type to which
24251 the cast occurs. */
24252 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24253 || TREE_CODE (expression) == STATIC_CAST_EXPR
24254 || TREE_CODE (expression) == CONST_CAST_EXPR
24255 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24256 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24257 || TREE_CODE (expression) == CAST_EXPR)
24258 return dependent_type_p (TREE_TYPE (expression));
24259
24260 /* The types of these expressions depends only on the type created
24261 by the expression. */
24262 if (TREE_CODE (expression) == NEW_EXPR
24263 || TREE_CODE (expression) == VEC_NEW_EXPR)
24264 {
24265 /* For NEW_EXPR tree nodes created inside a template, either
24266 the object type itself or a TREE_LIST may appear as the
24267 operand 1. */
24268 tree type = TREE_OPERAND (expression, 1);
24269 if (TREE_CODE (type) == TREE_LIST)
24270 /* This is an array type. We need to check array dimensions
24271 as well. */
24272 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24273 || value_dependent_expression_p
24274 (TREE_OPERAND (TREE_VALUE (type), 1));
24275 else
24276 return dependent_type_p (type);
24277 }
24278
24279 if (TREE_CODE (expression) == SCOPE_REF)
24280 {
24281 tree scope = TREE_OPERAND (expression, 0);
24282 tree name = TREE_OPERAND (expression, 1);
24283
24284 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24285 contains an identifier associated by name lookup with one or more
24286 declarations declared with a dependent type, or...a
24287 nested-name-specifier or qualified-id that names a member of an
24288 unknown specialization. */
24289 return (type_dependent_expression_p (name)
24290 || dependent_scope_p (scope));
24291 }
24292
24293 if (TREE_CODE (expression) == TEMPLATE_DECL
24294 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24295 return uses_outer_template_parms (expression);
24296
24297 if (TREE_CODE (expression) == STMT_EXPR)
24298 expression = stmt_expr_value_expr (expression);
24299
24300 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24301 {
24302 tree elt;
24303 unsigned i;
24304
24305 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24306 {
24307 if (type_dependent_expression_p (elt))
24308 return true;
24309 }
24310 return false;
24311 }
24312
24313 /* A static data member of the current instantiation with incomplete
24314 array type is type-dependent, as the definition and specializations
24315 can have different bounds. */
24316 if (VAR_P (expression)
24317 && DECL_CLASS_SCOPE_P (expression)
24318 && dependent_type_p (DECL_CONTEXT (expression))
24319 && VAR_HAD_UNKNOWN_BOUND (expression))
24320 return true;
24321
24322 /* An array of unknown bound depending on a variadic parameter, eg:
24323
24324 template<typename... Args>
24325 void foo (Args... args)
24326 {
24327 int arr[] = { args... };
24328 }
24329
24330 template<int... vals>
24331 void bar ()
24332 {
24333 int arr[] = { vals... };
24334 }
24335
24336 If the array has no length and has an initializer, it must be that
24337 we couldn't determine its length in cp_complete_array_type because
24338 it is dependent. */
24339 if (VAR_P (expression)
24340 && TREE_TYPE (expression) != NULL_TREE
24341 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24342 && !TYPE_DOMAIN (TREE_TYPE (expression))
24343 && DECL_INITIAL (expression))
24344 return true;
24345
24346 /* A function or variable template-id is type-dependent if it has any
24347 dependent template arguments. */
24348 if (VAR_OR_FUNCTION_DECL_P (expression)
24349 && DECL_LANG_SPECIFIC (expression)
24350 && DECL_TEMPLATE_INFO (expression))
24351 {
24352 /* Consider the innermost template arguments, since those are the ones
24353 that come from the template-id; the template arguments for the
24354 enclosing class do not make it type-dependent unless they are used in
24355 the type of the decl. */
24356 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24357 && (any_dependent_template_arguments_p
24358 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24359 return true;
24360 }
24361
24362 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24363 type-dependent. Checking this is important for functions with auto return
24364 type, which looks like a dependent type. */
24365 if (TREE_CODE (expression) == FUNCTION_DECL
24366 && !(DECL_CLASS_SCOPE_P (expression)
24367 && dependent_type_p (DECL_CONTEXT (expression)))
24368 && !(DECL_FRIEND_P (expression)
24369 && (!DECL_FRIEND_CONTEXT (expression)
24370 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24371 && !DECL_LOCAL_FUNCTION_P (expression))
24372 {
24373 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24374 || undeduced_auto_decl (expression));
24375 return false;
24376 }
24377
24378 /* Always dependent, on the number of arguments if nothing else. */
24379 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24380 return true;
24381
24382 if (TREE_TYPE (expression) == unknown_type_node)
24383 {
24384 if (TREE_CODE (expression) == ADDR_EXPR)
24385 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24386 if (TREE_CODE (expression) == COMPONENT_REF
24387 || TREE_CODE (expression) == OFFSET_REF)
24388 {
24389 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24390 return true;
24391 expression = TREE_OPERAND (expression, 1);
24392 if (identifier_p (expression))
24393 return false;
24394 }
24395 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24396 if (TREE_CODE (expression) == SCOPE_REF)
24397 return false;
24398
24399 if (BASELINK_P (expression))
24400 {
24401 if (BASELINK_OPTYPE (expression)
24402 && dependent_type_p (BASELINK_OPTYPE (expression)))
24403 return true;
24404 expression = BASELINK_FUNCTIONS (expression);
24405 }
24406
24407 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24408 {
24409 if (any_dependent_template_arguments_p
24410 (TREE_OPERAND (expression, 1)))
24411 return true;
24412 expression = TREE_OPERAND (expression, 0);
24413 if (identifier_p (expression))
24414 return true;
24415 }
24416
24417 gcc_assert (TREE_CODE (expression) == OVERLOAD
24418 || TREE_CODE (expression) == FUNCTION_DECL);
24419
24420 for (lkp_iterator iter (expression); iter; ++iter)
24421 if (type_dependent_expression_p (*iter))
24422 return true;
24423
24424 return false;
24425 }
24426
24427 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24428
24429 /* Dependent type attributes might not have made it from the decl to
24430 the type yet. */
24431 if (DECL_P (expression)
24432 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24433 return true;
24434
24435 return (dependent_type_p (TREE_TYPE (expression)));
24436 }
24437
24438 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24439 type-dependent if the expression refers to a member of the current
24440 instantiation and the type of the referenced member is dependent, or the
24441 class member access expression refers to a member of an unknown
24442 specialization.
24443
24444 This function returns true if the OBJECT in such a class member access
24445 expression is of an unknown specialization. */
24446
24447 bool
24448 type_dependent_object_expression_p (tree object)
24449 {
24450 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24451 dependent. */
24452 if (TREE_CODE (object) == IDENTIFIER_NODE)
24453 return true;
24454 tree scope = TREE_TYPE (object);
24455 return (!scope || dependent_scope_p (scope));
24456 }
24457
24458 /* walk_tree callback function for instantiation_dependent_expression_p,
24459 below. Returns non-zero if a dependent subexpression is found. */
24460
24461 static tree
24462 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24463 void * /*data*/)
24464 {
24465 if (TYPE_P (*tp))
24466 {
24467 /* We don't have to worry about decltype currently because decltype
24468 of an instantiation-dependent expr is a dependent type. This
24469 might change depending on the resolution of DR 1172. */
24470 *walk_subtrees = false;
24471 return NULL_TREE;
24472 }
24473 enum tree_code code = TREE_CODE (*tp);
24474 switch (code)
24475 {
24476 /* Don't treat an argument list as dependent just because it has no
24477 TREE_TYPE. */
24478 case TREE_LIST:
24479 case TREE_VEC:
24480 return NULL_TREE;
24481
24482 case TEMPLATE_PARM_INDEX:
24483 return *tp;
24484
24485 /* Handle expressions with type operands. */
24486 case SIZEOF_EXPR:
24487 case ALIGNOF_EXPR:
24488 case TYPEID_EXPR:
24489 case AT_ENCODE_EXPR:
24490 {
24491 tree op = TREE_OPERAND (*tp, 0);
24492 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24493 op = TREE_TYPE (op);
24494 if (TYPE_P (op))
24495 {
24496 if (dependent_type_p (op))
24497 return *tp;
24498 else
24499 {
24500 *walk_subtrees = false;
24501 return NULL_TREE;
24502 }
24503 }
24504 break;
24505 }
24506
24507 case COMPONENT_REF:
24508 if (identifier_p (TREE_OPERAND (*tp, 1)))
24509 /* In a template, finish_class_member_access_expr creates a
24510 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24511 type-dependent, so that we can check access control at
24512 instantiation time (PR 42277). See also Core issue 1273. */
24513 return *tp;
24514 break;
24515
24516 case SCOPE_REF:
24517 if (instantiation_dependent_scope_ref_p (*tp))
24518 return *tp;
24519 else
24520 break;
24521
24522 /* Treat statement-expressions as dependent. */
24523 case BIND_EXPR:
24524 return *tp;
24525
24526 /* Treat requires-expressions as dependent. */
24527 case REQUIRES_EXPR:
24528 return *tp;
24529
24530 case CALL_EXPR:
24531 /* Treat calls to function concepts as dependent. */
24532 if (function_concept_check_p (*tp))
24533 return *tp;
24534 break;
24535
24536 case TEMPLATE_ID_EXPR:
24537 /* And variable concepts. */
24538 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24539 return *tp;
24540 break;
24541
24542 default:
24543 break;
24544 }
24545
24546 if (type_dependent_expression_p (*tp))
24547 return *tp;
24548 else
24549 return NULL_TREE;
24550 }
24551
24552 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24553 sense defined by the ABI:
24554
24555 "An expression is instantiation-dependent if it is type-dependent
24556 or value-dependent, or it has a subexpression that is type-dependent
24557 or value-dependent."
24558
24559 Except don't actually check value-dependence for unevaluated expressions,
24560 because in sizeof(i) we don't care about the value of i. Checking
24561 type-dependence will in turn check value-dependence of array bounds/template
24562 arguments as needed. */
24563
24564 bool
24565 instantiation_dependent_uneval_expression_p (tree expression)
24566 {
24567 tree result;
24568
24569 if (!processing_template_decl)
24570 return false;
24571
24572 if (expression == error_mark_node)
24573 return false;
24574
24575 result = cp_walk_tree_without_duplicates (&expression,
24576 instantiation_dependent_r, NULL);
24577 return result != NULL_TREE;
24578 }
24579
24580 /* As above, but also check value-dependence of the expression as a whole. */
24581
24582 bool
24583 instantiation_dependent_expression_p (tree expression)
24584 {
24585 return (instantiation_dependent_uneval_expression_p (expression)
24586 || value_dependent_expression_p (expression));
24587 }
24588
24589 /* Like type_dependent_expression_p, but it also works while not processing
24590 a template definition, i.e. during substitution or mangling. */
24591
24592 bool
24593 type_dependent_expression_p_push (tree expr)
24594 {
24595 bool b;
24596 ++processing_template_decl;
24597 b = type_dependent_expression_p (expr);
24598 --processing_template_decl;
24599 return b;
24600 }
24601
24602 /* Returns TRUE if ARGS contains a type-dependent expression. */
24603
24604 bool
24605 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24606 {
24607 unsigned int i;
24608 tree arg;
24609
24610 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24611 {
24612 if (type_dependent_expression_p (arg))
24613 return true;
24614 }
24615 return false;
24616 }
24617
24618 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24619 expressions) contains any type-dependent expressions. */
24620
24621 bool
24622 any_type_dependent_elements_p (const_tree list)
24623 {
24624 for (; list; list = TREE_CHAIN (list))
24625 if (type_dependent_expression_p (TREE_VALUE (list)))
24626 return true;
24627
24628 return false;
24629 }
24630
24631 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24632 expressions) contains any value-dependent expressions. */
24633
24634 bool
24635 any_value_dependent_elements_p (const_tree list)
24636 {
24637 for (; list; list = TREE_CHAIN (list))
24638 if (value_dependent_expression_p (TREE_VALUE (list)))
24639 return true;
24640
24641 return false;
24642 }
24643
24644 /* Returns TRUE if the ARG (a template argument) is dependent. */
24645
24646 bool
24647 dependent_template_arg_p (tree arg)
24648 {
24649 if (!processing_template_decl)
24650 return false;
24651
24652 /* Assume a template argument that was wrongly written by the user
24653 is dependent. This is consistent with what
24654 any_dependent_template_arguments_p [that calls this function]
24655 does. */
24656 if (!arg || arg == error_mark_node)
24657 return true;
24658
24659 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24660 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24661
24662 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24663 return true;
24664 if (TREE_CODE (arg) == TEMPLATE_DECL)
24665 {
24666 if (DECL_TEMPLATE_PARM_P (arg))
24667 return true;
24668 /* A member template of a dependent class is not necessarily
24669 type-dependent, but it is a dependent template argument because it
24670 will be a member of an unknown specialization to that template. */
24671 tree scope = CP_DECL_CONTEXT (arg);
24672 return TYPE_P (scope) && dependent_type_p (scope);
24673 }
24674 else if (ARGUMENT_PACK_P (arg))
24675 {
24676 tree args = ARGUMENT_PACK_ARGS (arg);
24677 int i, len = TREE_VEC_LENGTH (args);
24678 for (i = 0; i < len; ++i)
24679 {
24680 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24681 return true;
24682 }
24683
24684 return false;
24685 }
24686 else if (TYPE_P (arg))
24687 return dependent_type_p (arg);
24688 else
24689 return (type_dependent_expression_p (arg)
24690 || value_dependent_expression_p (arg));
24691 }
24692
24693 /* Returns true if ARGS (a collection of template arguments) contains
24694 any types that require structural equality testing. */
24695
24696 bool
24697 any_template_arguments_need_structural_equality_p (tree args)
24698 {
24699 int i;
24700 int j;
24701
24702 if (!args)
24703 return false;
24704 if (args == error_mark_node)
24705 return true;
24706
24707 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24708 {
24709 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24710 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24711 {
24712 tree arg = TREE_VEC_ELT (level, j);
24713 tree packed_args = NULL_TREE;
24714 int k, len = 1;
24715
24716 if (ARGUMENT_PACK_P (arg))
24717 {
24718 /* Look inside the argument pack. */
24719 packed_args = ARGUMENT_PACK_ARGS (arg);
24720 len = TREE_VEC_LENGTH (packed_args);
24721 }
24722
24723 for (k = 0; k < len; ++k)
24724 {
24725 if (packed_args)
24726 arg = TREE_VEC_ELT (packed_args, k);
24727
24728 if (error_operand_p (arg))
24729 return true;
24730 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24731 continue;
24732 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24733 return true;
24734 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24735 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24736 return true;
24737 }
24738 }
24739 }
24740
24741 return false;
24742 }
24743
24744 /* Returns true if ARGS (a collection of template arguments) contains
24745 any dependent arguments. */
24746
24747 bool
24748 any_dependent_template_arguments_p (const_tree args)
24749 {
24750 int i;
24751 int j;
24752
24753 if (!args)
24754 return false;
24755 if (args == error_mark_node)
24756 return true;
24757
24758 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24759 {
24760 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24761 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24762 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24763 return true;
24764 }
24765
24766 return false;
24767 }
24768
24769 /* Returns TRUE if the template TMPL is type-dependent. */
24770
24771 bool
24772 dependent_template_p (tree tmpl)
24773 {
24774 if (TREE_CODE (tmpl) == OVERLOAD)
24775 {
24776 for (lkp_iterator iter (tmpl); iter; ++iter)
24777 if (dependent_template_p (*iter))
24778 return true;
24779 return false;
24780 }
24781
24782 /* Template template parameters are dependent. */
24783 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24784 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24785 return true;
24786 /* So are names that have not been looked up. */
24787 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24788 return true;
24789 return false;
24790 }
24791
24792 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24793
24794 bool
24795 dependent_template_id_p (tree tmpl, tree args)
24796 {
24797 return (dependent_template_p (tmpl)
24798 || any_dependent_template_arguments_p (args));
24799 }
24800
24801 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24802 are dependent. */
24803
24804 bool
24805 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24806 {
24807 int i;
24808
24809 if (!processing_template_decl)
24810 return false;
24811
24812 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24813 {
24814 tree decl = TREE_VEC_ELT (declv, i);
24815 tree init = TREE_VEC_ELT (initv, i);
24816 tree cond = TREE_VEC_ELT (condv, i);
24817 tree incr = TREE_VEC_ELT (incrv, i);
24818
24819 if (type_dependent_expression_p (decl)
24820 || TREE_CODE (decl) == SCOPE_REF)
24821 return true;
24822
24823 if (init && type_dependent_expression_p (init))
24824 return true;
24825
24826 if (type_dependent_expression_p (cond))
24827 return true;
24828
24829 if (COMPARISON_CLASS_P (cond)
24830 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24831 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24832 return true;
24833
24834 if (TREE_CODE (incr) == MODOP_EXPR)
24835 {
24836 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24837 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24838 return true;
24839 }
24840 else if (type_dependent_expression_p (incr))
24841 return true;
24842 else if (TREE_CODE (incr) == MODIFY_EXPR)
24843 {
24844 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24845 return true;
24846 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24847 {
24848 tree t = TREE_OPERAND (incr, 1);
24849 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24850 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24851 return true;
24852 }
24853 }
24854 }
24855
24856 return false;
24857 }
24858
24859 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24860 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24861 no such TYPE can be found. Note that this function peers inside
24862 uninstantiated templates and therefore should be used only in
24863 extremely limited situations. ONLY_CURRENT_P restricts this
24864 peering to the currently open classes hierarchy (which is required
24865 when comparing types). */
24866
24867 tree
24868 resolve_typename_type (tree type, bool only_current_p)
24869 {
24870 tree scope;
24871 tree name;
24872 tree decl;
24873 int quals;
24874 tree pushed_scope;
24875 tree result;
24876
24877 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24878
24879 scope = TYPE_CONTEXT (type);
24880 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24881 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24882 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24883 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24884 identifier of the TYPENAME_TYPE anymore.
24885 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24886 TYPENAME_TYPE instead, we avoid messing up with a possible
24887 typedef variant case. */
24888 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24889
24890 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24891 it first before we can figure out what NAME refers to. */
24892 if (TREE_CODE (scope) == TYPENAME_TYPE)
24893 {
24894 if (TYPENAME_IS_RESOLVING_P (scope))
24895 /* Given a class template A with a dependent base with nested type C,
24896 typedef typename A::C::C C will land us here, as trying to resolve
24897 the initial A::C leads to the local C typedef, which leads back to
24898 A::C::C. So we break the recursion now. */
24899 return type;
24900 else
24901 scope = resolve_typename_type (scope, only_current_p);
24902 }
24903 /* If we don't know what SCOPE refers to, then we cannot resolve the
24904 TYPENAME_TYPE. */
24905 if (!CLASS_TYPE_P (scope))
24906 return type;
24907 /* If this is a typedef, we don't want to look inside (c++/11987). */
24908 if (typedef_variant_p (type))
24909 return type;
24910 /* If SCOPE isn't the template itself, it will not have a valid
24911 TYPE_FIELDS list. */
24912 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24913 /* scope is either the template itself or a compatible instantiation
24914 like X<T>, so look up the name in the original template. */
24915 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24916 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24917 gcc_checking_assert (uses_template_parms (scope));
24918 /* If scope has no fields, it can't be a current instantiation. Check this
24919 before currently_open_class to avoid infinite recursion (71515). */
24920 if (!TYPE_FIELDS (scope))
24921 return type;
24922 /* If the SCOPE is not the current instantiation, there's no reason
24923 to look inside it. */
24924 if (only_current_p && !currently_open_class (scope))
24925 return type;
24926 /* Enter the SCOPE so that name lookup will be resolved as if we
24927 were in the class definition. In particular, SCOPE will no
24928 longer be considered a dependent type. */
24929 pushed_scope = push_scope (scope);
24930 /* Look up the declaration. */
24931 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24932 tf_warning_or_error);
24933
24934 result = NULL_TREE;
24935
24936 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24937 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24938 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24939 if (!decl)
24940 /*nop*/;
24941 else if (identifier_p (fullname)
24942 && TREE_CODE (decl) == TYPE_DECL)
24943 {
24944 result = TREE_TYPE (decl);
24945 if (result == error_mark_node)
24946 result = NULL_TREE;
24947 }
24948 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24949 && DECL_CLASS_TEMPLATE_P (decl))
24950 {
24951 /* Obtain the template and the arguments. */
24952 tree tmpl = TREE_OPERAND (fullname, 0);
24953 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24954 {
24955 /* We get here with a plain identifier because a previous tentative
24956 parse of the nested-name-specifier as part of a ptr-operator saw
24957 ::template X<A>. The use of ::template is necessary in a
24958 ptr-operator, but wrong in a declarator-id.
24959
24960 [temp.names]: In a qualified-id of a declarator-id, the keyword
24961 template shall not appear at the top level. */
24962 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24963 "keyword %<template%> not allowed in declarator-id");
24964 tmpl = decl;
24965 }
24966 tree args = TREE_OPERAND (fullname, 1);
24967 /* Instantiate the template. */
24968 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24969 /*entering_scope=*/true,
24970 tf_error | tf_user);
24971 if (result == error_mark_node)
24972 result = NULL_TREE;
24973 }
24974
24975 /* Leave the SCOPE. */
24976 if (pushed_scope)
24977 pop_scope (pushed_scope);
24978
24979 /* If we failed to resolve it, return the original typename. */
24980 if (!result)
24981 return type;
24982
24983 /* If lookup found a typename type, resolve that too. */
24984 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24985 {
24986 /* Ill-formed programs can cause infinite recursion here, so we
24987 must catch that. */
24988 TYPENAME_IS_RESOLVING_P (result) = 1;
24989 result = resolve_typename_type (result, only_current_p);
24990 TYPENAME_IS_RESOLVING_P (result) = 0;
24991 }
24992
24993 /* Qualify the resulting type. */
24994 quals = cp_type_quals (type);
24995 if (quals)
24996 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24997
24998 return result;
24999 }
25000
25001 /* EXPR is an expression which is not type-dependent. Return a proxy
25002 for EXPR that can be used to compute the types of larger
25003 expressions containing EXPR. */
25004
25005 tree
25006 build_non_dependent_expr (tree expr)
25007 {
25008 tree orig_expr = expr;
25009 tree inner_expr;
25010
25011 /* When checking, try to get a constant value for all non-dependent
25012 expressions in order to expose bugs in *_dependent_expression_p
25013 and constexpr. This can affect code generation, see PR70704, so
25014 only do this for -fchecking=2. */
25015 if (flag_checking > 1
25016 && cxx_dialect >= cxx11
25017 /* Don't do this during nsdmi parsing as it can lead to
25018 unexpected recursive instantiations. */
25019 && !parsing_nsdmi ()
25020 /* Don't do this during concept expansion either and for
25021 the same reason. */
25022 && !expanding_concept ())
25023 fold_non_dependent_expr (expr);
25024
25025 STRIP_ANY_LOCATION_WRAPPER (expr);
25026
25027 /* Preserve OVERLOADs; the functions must be available to resolve
25028 types. */
25029 inner_expr = expr;
25030 if (TREE_CODE (inner_expr) == STMT_EXPR)
25031 inner_expr = stmt_expr_value_expr (inner_expr);
25032 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25033 inner_expr = TREE_OPERAND (inner_expr, 0);
25034 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25035 inner_expr = TREE_OPERAND (inner_expr, 1);
25036 if (is_overloaded_fn (inner_expr)
25037 || TREE_CODE (inner_expr) == OFFSET_REF)
25038 return orig_expr;
25039 /* There is no need to return a proxy for a variable. */
25040 if (VAR_P (expr))
25041 return orig_expr;
25042 /* Preserve string constants; conversions from string constants to
25043 "char *" are allowed, even though normally a "const char *"
25044 cannot be used to initialize a "char *". */
25045 if (TREE_CODE (expr) == STRING_CST)
25046 return orig_expr;
25047 /* Preserve void and arithmetic constants, as an optimization -- there is no
25048 reason to create a new node. */
25049 if (TREE_CODE (expr) == VOID_CST
25050 || TREE_CODE (expr) == INTEGER_CST
25051 || TREE_CODE (expr) == REAL_CST)
25052 return orig_expr;
25053 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25054 There is at least one place where we want to know that a
25055 particular expression is a throw-expression: when checking a ?:
25056 expression, there are special rules if the second or third
25057 argument is a throw-expression. */
25058 if (TREE_CODE (expr) == THROW_EXPR)
25059 return orig_expr;
25060
25061 /* Don't wrap an initializer list, we need to be able to look inside. */
25062 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25063 return orig_expr;
25064
25065 /* Don't wrap a dummy object, we need to be able to test for it. */
25066 if (is_dummy_object (expr))
25067 return orig_expr;
25068
25069 if (TREE_CODE (expr) == COND_EXPR)
25070 return build3 (COND_EXPR,
25071 TREE_TYPE (expr),
25072 TREE_OPERAND (expr, 0),
25073 (TREE_OPERAND (expr, 1)
25074 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25075 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25076 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25077 if (TREE_CODE (expr) == COMPOUND_EXPR
25078 && !COMPOUND_EXPR_OVERLOADED (expr))
25079 return build2 (COMPOUND_EXPR,
25080 TREE_TYPE (expr),
25081 TREE_OPERAND (expr, 0),
25082 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25083
25084 /* If the type is unknown, it can't really be non-dependent */
25085 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25086
25087 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25088 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25089 }
25090
25091 /* ARGS is a vector of expressions as arguments to a function call.
25092 Replace the arguments with equivalent non-dependent expressions.
25093 This modifies ARGS in place. */
25094
25095 void
25096 make_args_non_dependent (vec<tree, va_gc> *args)
25097 {
25098 unsigned int ix;
25099 tree arg;
25100
25101 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25102 {
25103 tree newarg = build_non_dependent_expr (arg);
25104 if (newarg != arg)
25105 (*args)[ix] = newarg;
25106 }
25107 }
25108
25109 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25110 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25111 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25112
25113 static tree
25114 make_auto_1 (tree name, bool set_canonical)
25115 {
25116 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25117 TYPE_NAME (au) = build_decl (input_location,
25118 TYPE_DECL, name, au);
25119 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25120 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25121 (0, processing_template_decl + 1, processing_template_decl + 1,
25122 TYPE_NAME (au), NULL_TREE);
25123 if (set_canonical)
25124 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25125 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25126 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25127
25128 return au;
25129 }
25130
25131 tree
25132 make_decltype_auto (void)
25133 {
25134 return make_auto_1 (decltype_auto_identifier, true);
25135 }
25136
25137 tree
25138 make_auto (void)
25139 {
25140 return make_auto_1 (auto_identifier, true);
25141 }
25142
25143 /* Return a C++17 deduction placeholder for class template TMPL. */
25144
25145 tree
25146 make_template_placeholder (tree tmpl)
25147 {
25148 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25149 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25150 return t;
25151 }
25152
25153 /* True iff T is a C++17 class template deduction placeholder. */
25154
25155 bool
25156 template_placeholder_p (tree t)
25157 {
25158 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25159 }
25160
25161 /* Make a "constrained auto" type-specifier. This is an
25162 auto type with constraints that must be associated after
25163 deduction. The constraint is formed from the given
25164 CONC and its optional sequence of arguments, which are
25165 non-null if written as partial-concept-id. */
25166
25167 tree
25168 make_constrained_auto (tree con, tree args)
25169 {
25170 tree type = make_auto_1 (auto_identifier, false);
25171
25172 /* Build the constraint. */
25173 tree tmpl = DECL_TI_TEMPLATE (con);
25174 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25175 expr = build_concept_check (expr, type, args);
25176
25177 tree constr = normalize_expression (expr);
25178 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25179
25180 /* Our canonical type depends on the constraint. */
25181 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25182
25183 /* Attach the constraint to the type declaration. */
25184 tree decl = TYPE_NAME (type);
25185 return decl;
25186 }
25187
25188 /* Given type ARG, return std::initializer_list<ARG>. */
25189
25190 static tree
25191 listify (tree arg)
25192 {
25193 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25194
25195 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25196 {
25197 gcc_rich_location richloc (input_location);
25198 maybe_add_include_fixit (&richloc, "<initializer_list>");
25199 error_at (&richloc,
25200 "deducing from brace-enclosed initializer list"
25201 " requires %<#include <initializer_list>%>");
25202
25203 return error_mark_node;
25204 }
25205 tree argvec = make_tree_vec (1);
25206 TREE_VEC_ELT (argvec, 0) = arg;
25207
25208 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25209 NULL_TREE, 0, tf_warning_or_error);
25210 }
25211
25212 /* Replace auto in TYPE with std::initializer_list<auto>. */
25213
25214 static tree
25215 listify_autos (tree type, tree auto_node)
25216 {
25217 tree init_auto = listify (auto_node);
25218 tree argvec = make_tree_vec (1);
25219 TREE_VEC_ELT (argvec, 0) = init_auto;
25220 if (processing_template_decl)
25221 argvec = add_to_template_args (current_template_args (), argvec);
25222 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25223 }
25224
25225 /* Hash traits for hashing possibly constrained 'auto'
25226 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25227
25228 struct auto_hash : default_hash_traits<tree>
25229 {
25230 static inline hashval_t hash (tree);
25231 static inline bool equal (tree, tree);
25232 };
25233
25234 /* Hash the 'auto' T. */
25235
25236 inline hashval_t
25237 auto_hash::hash (tree t)
25238 {
25239 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25240 /* Matching constrained-type-specifiers denote the same template
25241 parameter, so hash the constraint. */
25242 return hash_placeholder_constraint (c);
25243 else
25244 /* But unconstrained autos are all separate, so just hash the pointer. */
25245 return iterative_hash_object (t, 0);
25246 }
25247
25248 /* Compare two 'auto's. */
25249
25250 inline bool
25251 auto_hash::equal (tree t1, tree t2)
25252 {
25253 if (t1 == t2)
25254 return true;
25255
25256 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25257 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25258
25259 /* Two unconstrained autos are distinct. */
25260 if (!c1 || !c2)
25261 return false;
25262
25263 return equivalent_placeholder_constraints (c1, c2);
25264 }
25265
25266 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25267 constrained) auto, add it to the vector. */
25268
25269 static int
25270 extract_autos_r (tree t, void *data)
25271 {
25272 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25273 if (is_auto (t))
25274 {
25275 /* All the autos were built with index 0; fix that up now. */
25276 tree *p = hash.find_slot (t, INSERT);
25277 unsigned idx;
25278 if (*p)
25279 /* If this is a repeated constrained-type-specifier, use the index we
25280 chose before. */
25281 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25282 else
25283 {
25284 /* Otherwise this is new, so use the current count. */
25285 *p = t;
25286 idx = hash.elements () - 1;
25287 }
25288 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25289 }
25290
25291 /* Always keep walking. */
25292 return 0;
25293 }
25294
25295 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25296 says they can appear anywhere in the type. */
25297
25298 static tree
25299 extract_autos (tree type)
25300 {
25301 hash_set<tree> visited;
25302 hash_table<auto_hash> hash (2);
25303
25304 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25305
25306 tree tree_vec = make_tree_vec (hash.elements());
25307 for (hash_table<auto_hash>::iterator iter = hash.begin();
25308 iter != hash.end(); ++iter)
25309 {
25310 tree elt = *iter;
25311 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25312 TREE_VEC_ELT (tree_vec, i)
25313 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25314 }
25315
25316 return tree_vec;
25317 }
25318
25319 /* The stem for deduction guide names. */
25320 const char *const dguide_base = "__dguide_";
25321
25322 /* Return the name for a deduction guide for class template TMPL. */
25323
25324 tree
25325 dguide_name (tree tmpl)
25326 {
25327 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25328 tree tname = TYPE_IDENTIFIER (type);
25329 char *buf = (char *) alloca (1 + strlen (dguide_base)
25330 + IDENTIFIER_LENGTH (tname));
25331 memcpy (buf, dguide_base, strlen (dguide_base));
25332 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25333 IDENTIFIER_LENGTH (tname) + 1);
25334 tree dname = get_identifier (buf);
25335 TREE_TYPE (dname) = type;
25336 return dname;
25337 }
25338
25339 /* True if NAME is the name of a deduction guide. */
25340
25341 bool
25342 dguide_name_p (tree name)
25343 {
25344 return (TREE_TYPE (name)
25345 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25346 strlen (dguide_base)));
25347 }
25348
25349 /* True if FN is a deduction guide. */
25350
25351 bool
25352 deduction_guide_p (const_tree fn)
25353 {
25354 if (DECL_P (fn))
25355 if (tree name = DECL_NAME (fn))
25356 return dguide_name_p (name);
25357 return false;
25358 }
25359
25360 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25361
25362 bool
25363 copy_guide_p (const_tree fn)
25364 {
25365 gcc_assert (deduction_guide_p (fn));
25366 if (!DECL_ARTIFICIAL (fn))
25367 return false;
25368 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25369 return (TREE_CHAIN (parms) == void_list_node
25370 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25371 }
25372
25373 /* True if FN is a guide generated from a constructor template. */
25374
25375 bool
25376 template_guide_p (const_tree fn)
25377 {
25378 gcc_assert (deduction_guide_p (fn));
25379 if (!DECL_ARTIFICIAL (fn))
25380 return false;
25381 tree tmpl = DECL_TI_TEMPLATE (fn);
25382 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25383 return PRIMARY_TEMPLATE_P (org);
25384 return false;
25385 }
25386
25387 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25388 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25389 template parameter types. Note that the handling of template template
25390 parameters relies on current_template_parms being set appropriately for the
25391 new template. */
25392
25393 static tree
25394 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25395 tree tsubst_args, tsubst_flags_t complain)
25396 {
25397 tree oldidx = get_template_parm_index (olddecl);
25398
25399 tree newtype;
25400 if (TREE_CODE (olddecl) == TYPE_DECL
25401 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25402 {
25403 tree oldtype = TREE_TYPE (olddecl);
25404 newtype = cxx_make_type (TREE_CODE (oldtype));
25405 TYPE_MAIN_VARIANT (newtype) = newtype;
25406 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25407 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25408 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25409 }
25410 else
25411 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25412 complain, NULL_TREE);
25413
25414 tree newdecl
25415 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25416 DECL_NAME (olddecl), newtype);
25417 SET_DECL_TEMPLATE_PARM_P (newdecl);
25418
25419 tree newidx;
25420 if (TREE_CODE (olddecl) == TYPE_DECL
25421 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25422 {
25423 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25424 = build_template_parm_index (index, level, level,
25425 newdecl, newtype);
25426 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25427 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25428 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25429 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25430
25431 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25432 {
25433 DECL_TEMPLATE_RESULT (newdecl)
25434 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25435 DECL_NAME (olddecl), newtype);
25436 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25437 // First create a copy (ttargs) of tsubst_args with an
25438 // additional level for the template template parameter's own
25439 // template parameters (ttparms).
25440 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25441 (DECL_TEMPLATE_PARMS (olddecl)));
25442 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25443 tree ttargs = make_tree_vec (depth + 1);
25444 for (int i = 0; i < depth; ++i)
25445 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25446 TREE_VEC_ELT (ttargs, depth)
25447 = template_parms_level_to_args (ttparms);
25448 // Substitute ttargs into ttparms to fix references to
25449 // other template parameters.
25450 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25451 complain);
25452 // Now substitute again with args based on tparms, to reduce
25453 // the level of the ttparms.
25454 ttargs = current_template_args ();
25455 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25456 complain);
25457 // Finally, tack the adjusted parms onto tparms.
25458 ttparms = tree_cons (size_int (depth), ttparms,
25459 current_template_parms);
25460 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25461 }
25462 }
25463 else
25464 {
25465 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25466 tree newconst
25467 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25468 TREE_CODE (oldconst),
25469 DECL_NAME (oldconst), newtype);
25470 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25471 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25472 SET_DECL_TEMPLATE_PARM_P (newconst);
25473 newidx = build_template_parm_index (index, level, level,
25474 newconst, newtype);
25475 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25476 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25477 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25478 }
25479
25480 return newdecl;
25481 }
25482
25483 /* Returns a C++17 class deduction guide template based on the constructor
25484 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25485 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25486
25487 static tree
25488 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25489 {
25490 tree type, tparms, targs, fparms, fargs, ci;
25491 bool memtmpl = false;
25492 bool explicit_p;
25493 location_t loc;
25494 tree fn_tmpl = NULL_TREE;
25495
25496 if (TYPE_P (ctor))
25497 {
25498 type = ctor;
25499 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25500 if (copy_p)
25501 {
25502 type = TREE_TYPE (type);
25503 fparms = tree_cons (NULL_TREE, type, void_list_node);
25504 }
25505 else
25506 fparms = void_list_node;
25507
25508 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25509 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25510 targs = CLASSTYPE_TI_ARGS (type);
25511 ci = NULL_TREE;
25512 fargs = NULL_TREE;
25513 loc = DECL_SOURCE_LOCATION (ctmpl);
25514 explicit_p = false;
25515 }
25516 else
25517 {
25518 ++processing_template_decl;
25519
25520 fn_tmpl
25521 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25522 : DECL_TI_TEMPLATE (ctor));
25523 if (outer_args)
25524 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25525 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25526
25527 type = DECL_CONTEXT (ctor);
25528
25529 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25530 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25531 fully specialized args for the enclosing class. Strip those off, as
25532 the deduction guide won't have those template parameters. */
25533 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25534 TMPL_PARMS_DEPTH (tparms));
25535 /* Discard the 'this' parameter. */
25536 fparms = FUNCTION_ARG_CHAIN (ctor);
25537 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25538 ci = get_constraints (ctor);
25539 loc = DECL_SOURCE_LOCATION (ctor);
25540 explicit_p = DECL_NONCONVERTING_P (ctor);
25541
25542 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25543 {
25544 memtmpl = true;
25545
25546 /* For a member template constructor, we need to flatten the two
25547 template parameter lists into one, and then adjust the function
25548 signature accordingly. This gets...complicated. */
25549 tree save_parms = current_template_parms;
25550
25551 /* For a member template we should have two levels of parms/args, one
25552 for the class and one for the constructor. We stripped
25553 specialized args for further enclosing classes above. */
25554 const int depth = 2;
25555 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25556
25557 /* Template args for translating references to the two-level template
25558 parameters into references to the one-level template parameters we
25559 are creating. */
25560 tree tsubst_args = copy_node (targs);
25561 TMPL_ARGS_LEVEL (tsubst_args, depth)
25562 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25563
25564 /* Template parms for the constructor template. */
25565 tree ftparms = TREE_VALUE (tparms);
25566 unsigned flen = TREE_VEC_LENGTH (ftparms);
25567 /* Template parms for the class template. */
25568 tparms = TREE_CHAIN (tparms);
25569 tree ctparms = TREE_VALUE (tparms);
25570 unsigned clen = TREE_VEC_LENGTH (ctparms);
25571 /* Template parms for the deduction guide start as a copy of the
25572 template parms for the class. We set current_template_parms for
25573 lookup_template_class_1. */
25574 current_template_parms = tparms = copy_node (tparms);
25575 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25576 for (unsigned i = 0; i < clen; ++i)
25577 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25578
25579 /* Now we need to rewrite the constructor parms to append them to the
25580 class parms. */
25581 for (unsigned i = 0; i < flen; ++i)
25582 {
25583 unsigned index = i + clen;
25584 unsigned level = 1;
25585 tree oldelt = TREE_VEC_ELT (ftparms, i);
25586 tree olddecl = TREE_VALUE (oldelt);
25587 tree newdecl = rewrite_template_parm (olddecl, index, level,
25588 tsubst_args, complain);
25589 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25590 tsubst_args, complain, ctor);
25591 tree list = build_tree_list (newdef, newdecl);
25592 TEMPLATE_PARM_CONSTRAINTS (list)
25593 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25594 tsubst_args, complain, ctor);
25595 TREE_VEC_ELT (new_vec, index) = list;
25596 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25597 }
25598
25599 /* Now we have a final set of template parms to substitute into the
25600 function signature. */
25601 targs = template_parms_to_args (tparms);
25602 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25603 complain, ctor);
25604 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25605 if (ci)
25606 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25607
25608 current_template_parms = save_parms;
25609 }
25610 --processing_template_decl;
25611 }
25612
25613 if (!memtmpl)
25614 {
25615 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25616 tparms = copy_node (tparms);
25617 INNERMOST_TEMPLATE_PARMS (tparms)
25618 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25619 }
25620
25621 tree fntype = build_function_type (type, fparms);
25622 tree ded_fn = build_lang_decl_loc (loc,
25623 FUNCTION_DECL,
25624 dguide_name (type), fntype);
25625 DECL_ARGUMENTS (ded_fn) = fargs;
25626 DECL_ARTIFICIAL (ded_fn) = true;
25627 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25628 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25629 DECL_ARTIFICIAL (ded_tmpl) = true;
25630 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25631 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25632 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25633 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25634 if (DECL_P (ctor))
25635 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25636 if (ci)
25637 set_constraints (ded_tmpl, ci);
25638
25639 return ded_tmpl;
25640 }
25641
25642 /* Deduce template arguments for the class template placeholder PTYPE for
25643 template TMPL based on the initializer INIT, and return the resulting
25644 type. */
25645
25646 static tree
25647 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25648 tsubst_flags_t complain)
25649 {
25650 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25651 {
25652 /* We should have handled this in the caller. */
25653 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25654 return ptype;
25655 if (complain & tf_error)
25656 error ("non-class template %qT used without template arguments", tmpl);
25657 return error_mark_node;
25658 }
25659
25660 tree type = TREE_TYPE (tmpl);
25661
25662 bool try_list_ctor = false;
25663
25664 vec<tree,va_gc> *args;
25665 if (init == NULL_TREE
25666 || TREE_CODE (init) == TREE_LIST)
25667 args = make_tree_vector_from_list (init);
25668 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25669 {
25670 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25671 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25672 {
25673 /* As an exception, the first phase in 16.3.1.7 (considering the
25674 initializer list as a single argument) is omitted if the
25675 initializer list consists of a single expression of type cv U,
25676 where U is a specialization of C or a class derived from a
25677 specialization of C. */
25678 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25679 tree etype = TREE_TYPE (elt);
25680
25681 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25682 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25683 int err = unify (tparms, targs, type, etype,
25684 UNIFY_ALLOW_DERIVED, /*explain*/false);
25685 if (err == 0)
25686 try_list_ctor = false;
25687 ggc_free (targs);
25688 }
25689 if (try_list_ctor || is_std_init_list (type))
25690 args = make_tree_vector_single (init);
25691 else
25692 args = make_tree_vector_from_ctor (init);
25693 }
25694 else
25695 args = make_tree_vector_single (init);
25696
25697 tree dname = dguide_name (tmpl);
25698 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25699 /*type*/false, /*complain*/false,
25700 /*hidden*/false);
25701 bool elided = false;
25702 if (cands == error_mark_node)
25703 cands = NULL_TREE;
25704
25705 /* Prune explicit deduction guides in copy-initialization context. */
25706 if (flags & LOOKUP_ONLYCONVERTING)
25707 {
25708 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25709 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25710 elided = true;
25711
25712 if (elided)
25713 {
25714 /* Found a nonconverting guide, prune the candidates. */
25715 tree pruned = NULL_TREE;
25716 for (lkp_iterator iter (cands); iter; ++iter)
25717 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25718 pruned = lookup_add (*iter, pruned);
25719
25720 cands = pruned;
25721 }
25722 }
25723
25724 tree outer_args = NULL_TREE;
25725 if (DECL_CLASS_SCOPE_P (tmpl)
25726 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25727 {
25728 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25729 type = TREE_TYPE (most_general_template (tmpl));
25730 }
25731
25732 bool saw_ctor = false;
25733 // FIXME cache artificial deduction guides
25734 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25735 {
25736 tree guide = build_deduction_guide (*iter, outer_args, complain);
25737 if ((flags & LOOKUP_ONLYCONVERTING)
25738 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25739 elided = true;
25740 else
25741 cands = lookup_add (guide, cands);
25742
25743 saw_ctor = true;
25744 }
25745
25746 tree call = error_mark_node;
25747
25748 /* If this is list-initialization and the class has a list constructor, first
25749 try deducing from the list as a single argument, as [over.match.list]. */
25750 tree list_cands = NULL_TREE;
25751 if (try_list_ctor && cands)
25752 for (lkp_iterator iter (cands); iter; ++iter)
25753 {
25754 tree dg = *iter;
25755 if (is_list_ctor (dg))
25756 list_cands = lookup_add (dg, list_cands);
25757 }
25758 if (list_cands)
25759 {
25760 ++cp_unevaluated_operand;
25761 call = build_new_function_call (list_cands, &args, tf_decltype);
25762 --cp_unevaluated_operand;
25763
25764 if (call == error_mark_node)
25765 {
25766 /* That didn't work, now try treating the list as a sequence of
25767 arguments. */
25768 release_tree_vector (args);
25769 args = make_tree_vector_from_ctor (init);
25770 }
25771 }
25772
25773 /* Maybe generate an implicit deduction guide. */
25774 if (call == error_mark_node && args->length () < 2)
25775 {
25776 tree gtype = NULL_TREE;
25777
25778 if (args->length () == 1)
25779 /* Generate a copy guide. */
25780 gtype = build_reference_type (type);
25781 else if (!saw_ctor)
25782 /* Generate a default guide. */
25783 gtype = type;
25784
25785 if (gtype)
25786 {
25787 tree guide = build_deduction_guide (gtype, outer_args, complain);
25788 cands = lookup_add (guide, cands);
25789 }
25790 }
25791
25792 if (elided && !cands)
25793 {
25794 error ("cannot deduce template arguments for copy-initialization"
25795 " of %qT, as it has no non-explicit deduction guides or "
25796 "user-declared constructors", type);
25797 return error_mark_node;
25798 }
25799 else if (!cands && call == error_mark_node)
25800 {
25801 error ("cannot deduce template arguments of %qT, as it has no viable "
25802 "deduction guides", type);
25803 return error_mark_node;
25804 }
25805
25806 if (call == error_mark_node)
25807 {
25808 ++cp_unevaluated_operand;
25809 call = build_new_function_call (cands, &args, tf_decltype);
25810 --cp_unevaluated_operand;
25811 }
25812
25813 if (call == error_mark_node && (complain & tf_warning_or_error))
25814 {
25815 error ("class template argument deduction failed:");
25816
25817 ++cp_unevaluated_operand;
25818 call = build_new_function_call (cands, &args, complain | tf_decltype);
25819 --cp_unevaluated_operand;
25820
25821 if (elided)
25822 inform (input_location, "explicit deduction guides not considered "
25823 "for copy-initialization");
25824 }
25825
25826 release_tree_vector (args);
25827
25828 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25829 }
25830
25831 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25832 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25833
25834 tree
25835 do_auto_deduction (tree type, tree init, tree auto_node)
25836 {
25837 return do_auto_deduction (type, init, auto_node,
25838 tf_warning_or_error,
25839 adc_unspecified);
25840 }
25841
25842 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25843 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25844 The CONTEXT determines the context in which auto deduction is performed
25845 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25846 OUTER_TARGS are used during template argument deduction
25847 (context == adc_unify) to properly substitute the result, and is ignored
25848 in other contexts.
25849
25850 For partial-concept-ids, extra args may be appended to the list of deduced
25851 template arguments prior to determining constraint satisfaction. */
25852
25853 tree
25854 do_auto_deduction (tree type, tree init, tree auto_node,
25855 tsubst_flags_t complain, auto_deduction_context context,
25856 tree outer_targs, int flags)
25857 {
25858 tree targs;
25859
25860 if (init == error_mark_node)
25861 return error_mark_node;
25862
25863 if (init && type_dependent_expression_p (init)
25864 && context != adc_unify)
25865 /* Defining a subset of type-dependent expressions that we can deduce
25866 from ahead of time isn't worth the trouble. */
25867 return type;
25868
25869 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25870 /* C++17 class template argument deduction. */
25871 return do_class_deduction (type, tmpl, init, flags, complain);
25872
25873 if (TREE_TYPE (init) == NULL_TREE)
25874 /* Nothing we can do with this, even in deduction context. */
25875 return type;
25876
25877 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25878 with either a new invented type template parameter U or, if the
25879 initializer is a braced-init-list (8.5.4), with
25880 std::initializer_list<U>. */
25881 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25882 {
25883 if (!DIRECT_LIST_INIT_P (init))
25884 type = listify_autos (type, auto_node);
25885 else if (CONSTRUCTOR_NELTS (init) == 1)
25886 init = CONSTRUCTOR_ELT (init, 0)->value;
25887 else
25888 {
25889 if (complain & tf_warning_or_error)
25890 {
25891 if (permerror (input_location, "direct-list-initialization of "
25892 "%<auto%> requires exactly one element"))
25893 inform (input_location,
25894 "for deduction to %<std::initializer_list%>, use copy-"
25895 "list-initialization (i.e. add %<=%> before the %<{%>)");
25896 }
25897 type = listify_autos (type, auto_node);
25898 }
25899 }
25900
25901 if (type == error_mark_node)
25902 return error_mark_node;
25903
25904 init = resolve_nondeduced_context (init, complain);
25905
25906 if (context == adc_decomp_type
25907 && auto_node == type
25908 && init != error_mark_node
25909 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25910 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25911 and initializer has array type, deduce cv-qualified array type. */
25912 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25913 complain);
25914 else if (AUTO_IS_DECLTYPE (auto_node))
25915 {
25916 bool id = (DECL_P (init)
25917 || ((TREE_CODE (init) == COMPONENT_REF
25918 || TREE_CODE (init) == SCOPE_REF)
25919 && !REF_PARENTHESIZED_P (init)));
25920 targs = make_tree_vec (1);
25921 TREE_VEC_ELT (targs, 0)
25922 = finish_decltype_type (init, id, tf_warning_or_error);
25923 if (type != auto_node)
25924 {
25925 if (complain & tf_error)
25926 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25927 return error_mark_node;
25928 }
25929 }
25930 else
25931 {
25932 tree parms = build_tree_list (NULL_TREE, type);
25933 tree tparms;
25934
25935 if (flag_concepts)
25936 tparms = extract_autos (type);
25937 else
25938 {
25939 tparms = make_tree_vec (1);
25940 TREE_VEC_ELT (tparms, 0)
25941 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25942 }
25943
25944 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25945 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25946 DEDUCE_CALL, LOOKUP_NORMAL,
25947 NULL, /*explain_p=*/false);
25948 if (val > 0)
25949 {
25950 if (processing_template_decl)
25951 /* Try again at instantiation time. */
25952 return type;
25953 if (type && type != error_mark_node
25954 && (complain & tf_error))
25955 /* If type is error_mark_node a diagnostic must have been
25956 emitted by now. Also, having a mention to '<type error>'
25957 in the diagnostic is not really useful to the user. */
25958 {
25959 if (cfun && auto_node == current_function_auto_return_pattern
25960 && LAMBDA_FUNCTION_P (current_function_decl))
25961 error ("unable to deduce lambda return type from %qE", init);
25962 else
25963 error ("unable to deduce %qT from %qE", type, init);
25964 type_unification_real (tparms, targs, parms, &init, 1, 0,
25965 DEDUCE_CALL, LOOKUP_NORMAL,
25966 NULL, /*explain_p=*/true);
25967 }
25968 return error_mark_node;
25969 }
25970 }
25971
25972 /* Check any placeholder constraints against the deduced type. */
25973 if (flag_concepts && !processing_template_decl)
25974 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25975 {
25976 /* Use the deduced type to check the associated constraints. If we
25977 have a partial-concept-id, rebuild the argument list so that
25978 we check using the extra arguments. */
25979 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25980 tree cargs = CHECK_CONSTR_ARGS (constr);
25981 if (TREE_VEC_LENGTH (cargs) > 1)
25982 {
25983 cargs = copy_node (cargs);
25984 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25985 }
25986 else
25987 cargs = targs;
25988 if (!constraints_satisfied_p (constr, cargs))
25989 {
25990 if (complain & tf_warning_or_error)
25991 {
25992 switch (context)
25993 {
25994 case adc_unspecified:
25995 case adc_unify:
25996 error("placeholder constraints not satisfied");
25997 break;
25998 case adc_variable_type:
25999 case adc_decomp_type:
26000 error ("deduced initializer does not satisfy "
26001 "placeholder constraints");
26002 break;
26003 case adc_return_type:
26004 error ("deduced return type does not satisfy "
26005 "placeholder constraints");
26006 break;
26007 case adc_requirement:
26008 error ("deduced expression type does not satisfy "
26009 "placeholder constraints");
26010 break;
26011 }
26012 diagnose_constraints (input_location, constr, targs);
26013 }
26014 return error_mark_node;
26015 }
26016 }
26017
26018 if (processing_template_decl && context != adc_unify)
26019 outer_targs = current_template_args ();
26020 targs = add_to_template_args (outer_targs, targs);
26021 return tsubst (type, targs, complain, NULL_TREE);
26022 }
26023
26024 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26025 result. */
26026
26027 tree
26028 splice_late_return_type (tree type, tree late_return_type)
26029 {
26030 if (is_auto (type))
26031 {
26032 if (late_return_type)
26033 return late_return_type;
26034
26035 tree idx = get_template_parm_index (type);
26036 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26037 /* In an abbreviated function template we didn't know we were dealing
26038 with a function template when we saw the auto return type, so update
26039 it to have the correct level. */
26040 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26041 }
26042 return type;
26043 }
26044
26045 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26046 'decltype(auto)' or a deduced class template. */
26047
26048 bool
26049 is_auto (const_tree type)
26050 {
26051 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26052 && (TYPE_IDENTIFIER (type) == auto_identifier
26053 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26054 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26055 return true;
26056 else
26057 return false;
26058 }
26059
26060 /* for_each_template_parm callback for type_uses_auto. */
26061
26062 int
26063 is_auto_r (tree tp, void */*data*/)
26064 {
26065 return is_auto (tp);
26066 }
26067
26068 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26069 a use of `auto'. Returns NULL_TREE otherwise. */
26070
26071 tree
26072 type_uses_auto (tree type)
26073 {
26074 if (type == NULL_TREE)
26075 return NULL_TREE;
26076 else if (flag_concepts)
26077 {
26078 /* The Concepts TS allows multiple autos in one type-specifier; just
26079 return the first one we find, do_auto_deduction will collect all of
26080 them. */
26081 if (uses_template_parms (type))
26082 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26083 /*visited*/NULL, /*nondeduced*/true);
26084 else
26085 return NULL_TREE;
26086 }
26087 else
26088 return find_type_usage (type, is_auto);
26089 }
26090
26091 /* For a given template T, return the vector of typedefs referenced
26092 in T for which access check is needed at T instantiation time.
26093 T is either a FUNCTION_DECL or a RECORD_TYPE.
26094 Those typedefs were added to T by the function
26095 append_type_to_template_for_access_check. */
26096
26097 vec<qualified_typedef_usage_t, va_gc> *
26098 get_types_needing_access_check (tree t)
26099 {
26100 tree ti;
26101 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26102
26103 if (!t || t == error_mark_node)
26104 return NULL;
26105
26106 if (!(ti = get_template_info (t)))
26107 return NULL;
26108
26109 if (CLASS_TYPE_P (t)
26110 || TREE_CODE (t) == FUNCTION_DECL)
26111 {
26112 if (!TI_TEMPLATE (ti))
26113 return NULL;
26114
26115 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26116 }
26117
26118 return result;
26119 }
26120
26121 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26122 tied to T. That list of typedefs will be access checked at
26123 T instantiation time.
26124 T is either a FUNCTION_DECL or a RECORD_TYPE.
26125 TYPE_DECL is a TYPE_DECL node representing a typedef.
26126 SCOPE is the scope through which TYPE_DECL is accessed.
26127 LOCATION is the location of the usage point of TYPE_DECL.
26128
26129 This function is a subroutine of
26130 append_type_to_template_for_access_check. */
26131
26132 static void
26133 append_type_to_template_for_access_check_1 (tree t,
26134 tree type_decl,
26135 tree scope,
26136 location_t location)
26137 {
26138 qualified_typedef_usage_t typedef_usage;
26139 tree ti;
26140
26141 if (!t || t == error_mark_node)
26142 return;
26143
26144 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26145 || CLASS_TYPE_P (t))
26146 && type_decl
26147 && TREE_CODE (type_decl) == TYPE_DECL
26148 && scope);
26149
26150 if (!(ti = get_template_info (t)))
26151 return;
26152
26153 gcc_assert (TI_TEMPLATE (ti));
26154
26155 typedef_usage.typedef_decl = type_decl;
26156 typedef_usage.context = scope;
26157 typedef_usage.locus = location;
26158
26159 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26160 }
26161
26162 /* Append TYPE_DECL to the template TEMPL.
26163 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26164 At TEMPL instanciation time, TYPE_DECL will be checked to see
26165 if it can be accessed through SCOPE.
26166 LOCATION is the location of the usage point of TYPE_DECL.
26167
26168 e.g. consider the following code snippet:
26169
26170 class C
26171 {
26172 typedef int myint;
26173 };
26174
26175 template<class U> struct S
26176 {
26177 C::myint mi; // <-- usage point of the typedef C::myint
26178 };
26179
26180 S<char> s;
26181
26182 At S<char> instantiation time, we need to check the access of C::myint
26183 In other words, we need to check the access of the myint typedef through
26184 the C scope. For that purpose, this function will add the myint typedef
26185 and the scope C through which its being accessed to a list of typedefs
26186 tied to the template S. That list will be walked at template instantiation
26187 time and access check performed on each typedefs it contains.
26188 Note that this particular code snippet should yield an error because
26189 myint is private to C. */
26190
26191 void
26192 append_type_to_template_for_access_check (tree templ,
26193 tree type_decl,
26194 tree scope,
26195 location_t location)
26196 {
26197 qualified_typedef_usage_t *iter;
26198 unsigned i;
26199
26200 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26201
26202 /* Make sure we don't append the type to the template twice. */
26203 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26204 if (iter->typedef_decl == type_decl && scope == iter->context)
26205 return;
26206
26207 append_type_to_template_for_access_check_1 (templ, type_decl,
26208 scope, location);
26209 }
26210
26211 /* Convert the generic type parameters in PARM that match the types given in the
26212 range [START_IDX, END_IDX) from the current_template_parms into generic type
26213 packs. */
26214
26215 tree
26216 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26217 {
26218 tree current = current_template_parms;
26219 int depth = TMPL_PARMS_DEPTH (current);
26220 current = INNERMOST_TEMPLATE_PARMS (current);
26221 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26222
26223 for (int i = 0; i < start_idx; ++i)
26224 TREE_VEC_ELT (replacement, i)
26225 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26226
26227 for (int i = start_idx; i < end_idx; ++i)
26228 {
26229 /* Create a distinct parameter pack type from the current parm and add it
26230 to the replacement args to tsubst below into the generic function
26231 parameter. */
26232
26233 tree o = TREE_TYPE (TREE_VALUE
26234 (TREE_VEC_ELT (current, i)));
26235 tree t = copy_type (o);
26236 TEMPLATE_TYPE_PARM_INDEX (t)
26237 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26238 o, 0, 0, tf_none);
26239 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26240 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26241 TYPE_MAIN_VARIANT (t) = t;
26242 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26243 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26244 TREE_VEC_ELT (replacement, i) = t;
26245 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26246 }
26247
26248 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26249 TREE_VEC_ELT (replacement, i)
26250 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26251
26252 /* If there are more levels then build up the replacement with the outer
26253 template parms. */
26254 if (depth > 1)
26255 replacement = add_to_template_args (template_parms_to_args
26256 (TREE_CHAIN (current_template_parms)),
26257 replacement);
26258
26259 return tsubst (parm, replacement, tf_none, NULL_TREE);
26260 }
26261
26262 /* Entries in the decl_constraint hash table. */
26263 struct GTY((for_user)) constr_entry
26264 {
26265 tree decl;
26266 tree ci;
26267 };
26268
26269 /* Hashing function and equality for constraint entries. */
26270 struct constr_hasher : ggc_ptr_hash<constr_entry>
26271 {
26272 static hashval_t hash (constr_entry *e)
26273 {
26274 return (hashval_t)DECL_UID (e->decl);
26275 }
26276
26277 static bool equal (constr_entry *e1, constr_entry *e2)
26278 {
26279 return e1->decl == e2->decl;
26280 }
26281 };
26282
26283 /* A mapping from declarations to constraint information. Note that
26284 both templates and their underlying declarations are mapped to the
26285 same constraint information.
26286
26287 FIXME: This is defined in pt.c because garbage collection
26288 code is not being generated for constraint.cc. */
26289
26290 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26291
26292 /* Returns the template constraints of declaration T. If T is not
26293 constrained, return NULL_TREE. Note that T must be non-null. */
26294
26295 tree
26296 get_constraints (tree t)
26297 {
26298 if (!flag_concepts)
26299 return NULL_TREE;
26300
26301 gcc_assert (DECL_P (t));
26302 if (TREE_CODE (t) == TEMPLATE_DECL)
26303 t = DECL_TEMPLATE_RESULT (t);
26304 constr_entry elt = { t, NULL_TREE };
26305 constr_entry* found = decl_constraints->find (&elt);
26306 if (found)
26307 return found->ci;
26308 else
26309 return NULL_TREE;
26310 }
26311
26312 /* Associate the given constraint information CI with the declaration
26313 T. If T is a template, then the constraints are associated with
26314 its underlying declaration. Don't build associations if CI is
26315 NULL_TREE. */
26316
26317 void
26318 set_constraints (tree t, tree ci)
26319 {
26320 if (!ci)
26321 return;
26322 gcc_assert (t && flag_concepts);
26323 if (TREE_CODE (t) == TEMPLATE_DECL)
26324 t = DECL_TEMPLATE_RESULT (t);
26325 gcc_assert (!get_constraints (t));
26326 constr_entry elt = {t, ci};
26327 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26328 constr_entry* entry = ggc_alloc<constr_entry> ();
26329 *entry = elt;
26330 *slot = entry;
26331 }
26332
26333 /* Remove the associated constraints of the declaration T. */
26334
26335 void
26336 remove_constraints (tree t)
26337 {
26338 gcc_assert (DECL_P (t));
26339 if (TREE_CODE (t) == TEMPLATE_DECL)
26340 t = DECL_TEMPLATE_RESULT (t);
26341
26342 constr_entry elt = {t, NULL_TREE};
26343 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26344 if (slot)
26345 decl_constraints->clear_slot (slot);
26346 }
26347
26348 /* Memoized satisfaction results for declarations. This
26349 maps the pair (constraint_info, arguments) to the result computed
26350 by constraints_satisfied_p. */
26351
26352 struct GTY((for_user)) constraint_sat_entry
26353 {
26354 tree ci;
26355 tree args;
26356 tree result;
26357 };
26358
26359 /* Hashing function and equality for constraint entries. */
26360
26361 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26362 {
26363 static hashval_t hash (constraint_sat_entry *e)
26364 {
26365 hashval_t val = iterative_hash_object(e->ci, 0);
26366 return iterative_hash_template_arg (e->args, val);
26367 }
26368
26369 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26370 {
26371 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26372 }
26373 };
26374
26375 /* Memoized satisfaction results for concept checks. */
26376
26377 struct GTY((for_user)) concept_spec_entry
26378 {
26379 tree tmpl;
26380 tree args;
26381 tree result;
26382 };
26383
26384 /* Hashing function and equality for constraint entries. */
26385
26386 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26387 {
26388 static hashval_t hash (concept_spec_entry *e)
26389 {
26390 return hash_tmpl_and_args (e->tmpl, e->args);
26391 }
26392
26393 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26394 {
26395 ++comparing_specializations;
26396 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26397 --comparing_specializations;
26398 return eq;
26399 }
26400 };
26401
26402 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26403 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26404
26405 /* Search for a memoized satisfaction result. Returns one of the
26406 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26407
26408 tree
26409 lookup_constraint_satisfaction (tree ci, tree args)
26410 {
26411 constraint_sat_entry elt = { ci, args, NULL_TREE };
26412 constraint_sat_entry* found = constraint_memos->find (&elt);
26413 if (found)
26414 return found->result;
26415 else
26416 return NULL_TREE;
26417 }
26418
26419 /* Memoize the result of a satisfication test. Returns the saved result. */
26420
26421 tree
26422 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26423 {
26424 constraint_sat_entry elt = {ci, args, result};
26425 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26426 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26427 *entry = elt;
26428 *slot = entry;
26429 return result;
26430 }
26431
26432 /* Search for a memoized satisfaction result for a concept. */
26433
26434 tree
26435 lookup_concept_satisfaction (tree tmpl, tree args)
26436 {
26437 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26438 concept_spec_entry* found = concept_memos->find (&elt);
26439 if (found)
26440 return found->result;
26441 else
26442 return NULL_TREE;
26443 }
26444
26445 /* Memoize the result of a concept check. Returns the saved result. */
26446
26447 tree
26448 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26449 {
26450 concept_spec_entry elt = {tmpl, args, result};
26451 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26452 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26453 *entry = elt;
26454 *slot = entry;
26455 return result;
26456 }
26457
26458 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26459
26460 /* Returns a prior concept specialization. This returns the substituted
26461 and normalized constraints defined by the concept. */
26462
26463 tree
26464 get_concept_expansion (tree tmpl, tree args)
26465 {
26466 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26467 concept_spec_entry* found = concept_expansions->find (&elt);
26468 if (found)
26469 return found->result;
26470 else
26471 return NULL_TREE;
26472 }
26473
26474 /* Save a concept expansion for later. */
26475
26476 tree
26477 save_concept_expansion (tree tmpl, tree args, tree def)
26478 {
26479 concept_spec_entry elt = {tmpl, args, def};
26480 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26481 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26482 *entry = elt;
26483 *slot = entry;
26484 return def;
26485 }
26486
26487 static hashval_t
26488 hash_subsumption_args (tree t1, tree t2)
26489 {
26490 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26491 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26492 int val = 0;
26493 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26494 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26495 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26496 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26497 return val;
26498 }
26499
26500 /* Compare the constraints of two subsumption entries. The LEFT1 and
26501 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26502 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26503
26504 static bool
26505 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26506 {
26507 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26508 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26509 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26510 CHECK_CONSTR_ARGS (right1)))
26511 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26512 CHECK_CONSTR_ARGS (right2));
26513 return false;
26514 }
26515
26516 /* Key/value pair for learning and memoizing subsumption results. This
26517 associates a pair of check constraints (including arguments) with
26518 a boolean value indicating the result. */
26519
26520 struct GTY((for_user)) subsumption_entry
26521 {
26522 tree t1;
26523 tree t2;
26524 bool result;
26525 };
26526
26527 /* Hashing function and equality for constraint entries. */
26528
26529 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26530 {
26531 static hashval_t hash (subsumption_entry *e)
26532 {
26533 return hash_subsumption_args (e->t1, e->t2);
26534 }
26535
26536 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26537 {
26538 ++comparing_specializations;
26539 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26540 --comparing_specializations;
26541 return eq;
26542 }
26543 };
26544
26545 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26546
26547 /* Search for a previously cached subsumption result. */
26548
26549 bool*
26550 lookup_subsumption_result (tree t1, tree t2)
26551 {
26552 subsumption_entry elt = { t1, t2, false };
26553 subsumption_entry* found = subsumption_table->find (&elt);
26554 if (found)
26555 return &found->result;
26556 else
26557 return 0;
26558 }
26559
26560 /* Save a subsumption result. */
26561
26562 bool
26563 save_subsumption_result (tree t1, tree t2, bool result)
26564 {
26565 subsumption_entry elt = {t1, t2, result};
26566 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26567 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26568 *entry = elt;
26569 *slot = entry;
26570 return result;
26571 }
26572
26573 /* Set up the hash table for constraint association. */
26574
26575 void
26576 init_constraint_processing (void)
26577 {
26578 if (!flag_concepts)
26579 return;
26580
26581 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26582 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26583 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26584 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26585 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26586 }
26587
26588 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26589 0..N-1. */
26590
26591 void
26592 declare_integer_pack (void)
26593 {
26594 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26595 build_function_type_list (integer_type_node,
26596 integer_type_node,
26597 NULL_TREE),
26598 NULL_TREE, ECF_CONST);
26599 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26600 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26601 }
26602
26603 /* Set up the hash tables for template instantiations. */
26604
26605 void
26606 init_template_processing (void)
26607 {
26608 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26609 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26610
26611 if (cxx_dialect >= cxx11)
26612 declare_integer_pack ();
26613 }
26614
26615 /* Print stats about the template hash tables for -fstats. */
26616
26617 void
26618 print_template_statistics (void)
26619 {
26620 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26621 "%f collisions\n", (long) decl_specializations->size (),
26622 (long) decl_specializations->elements (),
26623 decl_specializations->collisions ());
26624 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26625 "%f collisions\n", (long) type_specializations->size (),
26626 (long) type_specializations->elements (),
26627 type_specializations->collisions ());
26628 }
26629
26630 #if CHECKING_P
26631
26632 namespace selftest {
26633
26634 /* Verify that build_non_dependent_expr () works, for various expressions,
26635 and that location wrappers don't affect the results. */
26636
26637 static void
26638 test_build_non_dependent_expr ()
26639 {
26640 location_t loc = BUILTINS_LOCATION;
26641
26642 /* Verify constants, without and with location wrappers. */
26643 tree int_cst = build_int_cst (integer_type_node, 42);
26644 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26645
26646 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26647 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26648 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26649
26650 tree string_lit = build_string (4, "foo");
26651 TREE_TYPE (string_lit) = char_array_type_node;
26652 string_lit = fix_string_type (string_lit);
26653 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26654
26655 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26656 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26657 ASSERT_EQ (wrapped_string_lit,
26658 build_non_dependent_expr (wrapped_string_lit));
26659 }
26660
26661 /* Run all of the selftests within this file. */
26662
26663 void
26664 cp_pt_c_tests ()
26665 {
26666 test_build_non_dependent_expr ();
26667 }
26668
26669 } // namespace selftest
26670
26671 #endif /* #if CHECKING_P */
26672
26673 #include "gt-cp-pt.h"