cp-tree.h (do_auto_deduction (tree, tree, tree)): Remove.
[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 static tree enclosing_instantiation_of (tree tctx);
226
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
231
232 static void
233 push_access_scope (tree t)
234 {
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
237
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
244
245 if (TREE_CODE (t) == FUNCTION_DECL)
246 {
247 saved_access_scope = tree_cons
248 (NULL_TREE, current_function_decl, saved_access_scope);
249 current_function_decl = t;
250 }
251 }
252
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
255
256 static void
257 pop_access_scope (tree t)
258 {
259 if (TREE_CODE (t) == FUNCTION_DECL)
260 {
261 current_function_decl = TREE_VALUE (saved_access_scope);
262 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 }
264
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
269 }
270
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
275
276 tree
277 finish_member_template_decl (tree decl)
278 {
279 if (decl == error_mark_node)
280 return error_mark_node;
281
282 gcc_assert (DECL_P (decl));
283
284 if (TREE_CODE (decl) == TYPE_DECL)
285 {
286 tree type;
287
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
294 {
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
298 }
299 return NULL_TREE;
300 }
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("data member %qD cannot be a member template", decl);
303 else if (DECL_TEMPLATE_INFO (decl))
304 {
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
306 {
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
309 }
310 else
311 return decl;
312 }
313 else
314 error ("invalid member template declaration %qD", decl);
315
316 return error_mark_node;
317 }
318
319 /* Create a template info node. */
320
321 tree
322 build_template_info (tree template_decl, tree template_args)
323 {
324 tree result = make_node (TEMPLATE_INFO);
325 TI_TEMPLATE (result) = template_decl;
326 TI_ARGS (result) = template_args;
327 return result;
328 }
329
330 /* Return the template info node corresponding to T, whatever T is. */
331
332 tree
333 get_template_info (const_tree t)
334 {
335 tree tinfo = NULL_TREE;
336
337 if (!t || t == error_mark_node)
338 return NULL;
339
340 if (TREE_CODE (t) == NAMESPACE_DECL
341 || TREE_CODE (t) == PARM_DECL)
342 return NULL;
343
344 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
345 tinfo = DECL_TEMPLATE_INFO (t);
346
347 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
348 t = TREE_TYPE (t);
349
350 if (OVERLOAD_TYPE_P (t))
351 tinfo = TYPE_TEMPLATE_INFO (t);
352 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
353 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
354
355 return tinfo;
356 }
357
358 /* Returns the template nesting level of the indicated class TYPE.
359
360 For example, in:
361 template <class T>
362 struct A
363 {
364 template <class U>
365 struct B {};
366 };
367
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
371
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
374 always safe. */
375
376 int
377 template_class_depth (tree type)
378 {
379 int depth;
380
381 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
382 {
383 tree tinfo = get_template_info (type);
384
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
388
389 if (DECL_P (type))
390 type = CP_DECL_CONTEXT (type);
391 else if (LAMBDA_TYPE_P (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
395 }
396
397 return depth;
398 }
399
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
402
403 static bool
404 inline_needs_template_parms (tree decl, bool nsdmi)
405 {
406 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
407 return false;
408
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
410 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 }
412
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
416 innermost first. */
417
418 static void
419 push_inline_template_parms_recursive (tree parmlist, int levels)
420 {
421 tree parms = TREE_VALUE (parmlist);
422 int i;
423
424 if (levels > 1)
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
426
427 ++processing_template_decl;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl),
430 parms, current_template_parms);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
432
433 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
434 NULL);
435 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
436 {
437 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
438
439 if (error_operand_p (parm))
440 continue;
441
442 gcc_assert (DECL_P (parm));
443
444 switch (TREE_CODE (parm))
445 {
446 case TYPE_DECL:
447 case TEMPLATE_DECL:
448 pushdecl (parm);
449 break;
450
451 case PARM_DECL:
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
454 break;
455
456 default:
457 gcc_unreachable ();
458 }
459 }
460 }
461
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
465
466 void
467 maybe_begin_member_template_processing (tree decl)
468 {
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
472
473 if (nsdmi)
474 {
475 tree ctx = DECL_CONTEXT (decl);
476 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx)
479 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 }
481
482 if (inline_needs_template_parms (decl, nsdmi))
483 {
484 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
485 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
486
487 if (DECL_TEMPLATE_SPECIALIZATION (decl))
488 {
489 --levels;
490 parms = TREE_CHAIN (parms);
491 }
492
493 push_inline_template_parms_recursive (parms, levels);
494 }
495
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels.safe_push (levels);
499 }
500
501 /* Undo the effects of maybe_begin_member_template_processing. */
502
503 void
504 maybe_end_member_template_processing (void)
505 {
506 int i;
507 int last;
508
509 if (inline_parm_levels.length () == 0)
510 return;
511
512 last = inline_parm_levels.pop ();
513 for (i = 0; i < last; ++i)
514 {
515 --processing_template_decl;
516 current_template_parms = TREE_CHAIN (current_template_parms);
517 poplevel (0, 0, 0);
518 }
519 }
520
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
523
524 static tree
525 add_to_template_args (tree args, tree extra_args)
526 {
527 tree new_args;
528 int extra_depth;
529 int i;
530 int j;
531
532 if (args == NULL_TREE || extra_args == error_mark_node)
533 return extra_args;
534
535 extra_depth = TMPL_ARGS_DEPTH (extra_args);
536 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
537
538 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
539 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
540
541 for (j = 1; j <= extra_depth; ++j, ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
543
544 return new_args;
545 }
546
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
553
554 static tree
555 add_outermost_template_args (tree args, tree extra_args)
556 {
557 tree new_args;
558
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
562
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 them. */
565 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
566 return extra_args;
567
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
570
571 new_args = add_to_template_args (args, extra_args);
572
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
575
576 return new_args;
577 }
578
579 /* Return the N levels of innermost template arguments from the ARGS. */
580
581 tree
582 get_innermost_template_args (tree args, int n)
583 {
584 tree new_args;
585 int extra_levels;
586 int i;
587
588 gcc_assert (n >= 0);
589
590 /* If N is 1, just return the innermost set of template arguments. */
591 if (n == 1)
592 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
593
594 /* If we're not removing anything, just return the arguments we were
595 given. */
596 extra_levels = TMPL_ARGS_DEPTH (args) - n;
597 gcc_assert (extra_levels >= 0);
598 if (extra_levels == 0)
599 return args;
600
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args = make_tree_vec (n);
603 for (i = 1; i <= n; ++i)
604 SET_TMPL_ARGS_LEVEL (new_args, i,
605 TMPL_ARGS_LEVEL (args, i + extra_levels));
606
607 return new_args;
608 }
609
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
612
613 static tree
614 strip_innermost_template_args (tree args, int extra_levels)
615 {
616 tree new_args;
617 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
618 int i;
619
620 gcc_assert (n >= 0);
621
622 /* If N is 1, just return the outermost set of template arguments. */
623 if (n == 1)
624 return TMPL_ARGS_LEVEL (args, 1);
625
626 /* If we're not removing anything, just return the arguments we were
627 given. */
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
631
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i));
637
638 return new_args;
639 }
640
641 /* We've got a template header coming up; push to a new level for storing
642 the parms. */
643
644 void
645 begin_template_parm_list (void)
646 {
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
653 e.g.:
654
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
657 };
658
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms, NULL);
662 ++processing_template_decl;
663 ++processing_template_parmlist;
664 note_template_header (0);
665
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl),
669 make_tree_vec (0),
670 current_template_parms);
671 }
672
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
676
677 static bool
678 check_specialization_scope (void)
679 {
680 tree scope = current_scope ();
681
682 /* [temp.expl.spec]
683
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
690 is a member. */
691 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
692 {
693 error ("explicit specialization in non-namespace scope %qD", scope);
694 return false;
695 }
696
697 /* [temp.expl.spec]
698
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms)
706 {
707 error ("enclosing class templates are not explicitly specialized");
708 return false;
709 }
710
711 return true;
712 }
713
714 /* We've just seen template <>. */
715
716 bool
717 begin_specialization (void)
718 {
719 begin_scope (sk_template_spec, NULL);
720 note_template_header (1);
721 return check_specialization_scope ();
722 }
723
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
726
727 void
728 end_specialization (void)
729 {
730 finish_scope ();
731 reset_specialization ();
732 }
733
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
736
737 void
738 reset_specialization (void)
739 {
740 processing_specialization = 0;
741 template_header_count = 0;
742 }
743
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
746
747 static void
748 note_template_header (int specialization)
749 {
750 processing_specialization = specialization;
751 template_header_count++;
752 }
753
754 /* We're beginning an explicit instantiation. */
755
756 void
757 begin_explicit_instantiation (void)
758 {
759 gcc_assert (!processing_explicit_instantiation);
760 processing_explicit_instantiation = true;
761 }
762
763
764 void
765 end_explicit_instantiation (void)
766 {
767 gcc_assert (processing_explicit_instantiation);
768 processing_explicit_instantiation = false;
769 }
770
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
775
776 static bool
777 check_specialization_namespace (tree tmpl)
778 {
779 tree tpl_ns = decl_namespace_context (tmpl);
780
781 /* [tmpl.expl.spec]
782
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl)
789 && !at_namespace_scope_p ())
790 {
791 error ("specialization of %qD must appear at namespace scope", tmpl);
792 return false;
793 }
794
795 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
796 /* Same or enclosing namespace. */
797 return true;
798 else
799 {
800 permerror (input_location,
801 "specialization of %qD in different namespace", tmpl);
802 inform (DECL_SOURCE_LOCATION (tmpl),
803 " from definition of %q#D", tmpl);
804 return false;
805 }
806 }
807
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
810
811 static void
812 check_explicit_instantiation_namespace (tree spec)
813 {
814 tree ns;
815
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns = decl_namespace_context (spec);
819 if (!is_nested_namespace (current_namespace, ns))
820 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec, current_namespace, ns);
823 }
824
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
828 static tree
829 maybe_new_partial_specialization (tree type)
830 {
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
833 //
834 // template<typename T>
835 // struct S;
836 //
837 // template<typename T>
838 // struct S<T*>;
839 //
840 // Here, S<T*> is an implicit instantiation of S whose type
841 // is incomplete.
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
843 return type;
844
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
847 //
848 // template<typename T>
849 // requires Integral<T>
850 // struct S<T*>;
851 //
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
857 //
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
862 {
863 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
864 tree args = CLASSTYPE_TI_ARGS (type);
865
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms)
869 return NULL_TREE;
870
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
873 return NULL_TREE;
874
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr = current_template_constraints ();
878
879 if (type == TREE_TYPE (tmpl))
880 {
881 tree main_constr = get_constraints (tmpl);
882 if (equivalent_constraints (type_constr, main_constr))
883 return NULL_TREE;
884 }
885
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
889 while (specs)
890 {
891 tree spec_tmpl = TREE_VALUE (specs);
892 tree spec_args = TREE_PURPOSE (specs);
893 tree spec_constr = get_constraints (spec_tmpl);
894 if (comp_template_args (args, spec_args)
895 && equivalent_constraints (type_constr, spec_constr))
896 return NULL_TREE;
897 specs = TREE_CHAIN (specs);
898 }
899
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t = make_class_type (TREE_CODE (type));
903 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
904 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
905
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
910
911 // Build the corresponding type decl.
912 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
913 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
914 DECL_SOURCE_LOCATION (d) = input_location;
915
916 return t;
917 }
918
919 return NULL_TREE;
920 }
921
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
924
925 tree
926 maybe_process_partial_specialization (tree type)
927 {
928 tree context;
929
930 if (type == error_mark_node)
931 return error_mark_node;
932
933 /* A lambda that appears in specialization context is not itself a
934 specialization. */
935 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
936 return type;
937
938 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
939 {
940 error ("name of class shadows template template parameter %qD",
941 TYPE_NAME (type));
942 return error_mark_node;
943 }
944
945 context = TYPE_CONTEXT (type);
946
947 if (TYPE_ALIAS_P (type))
948 {
949 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
950
951 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo));
954 else
955 error ("explicit specialization of non-template %qT", type);
956 return error_mark_node;
957 }
958 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
959 {
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
962
963 template <> class C<int>;
964
965 or:
966
967 template <class T> class C<T*>;
968
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
970
971 if (tree t = maybe_new_partial_specialization (type))
972 {
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
974 && !at_namespace_scope_p ())
975 return error_mark_node;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
978 if (processing_template_decl)
979 {
980 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
981 if (decl == error_mark_node)
982 return error_mark_node;
983 return TREE_TYPE (decl);
984 }
985 }
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
987 error ("specialization of %qT after instantiation", type);
988 else if (errorcount && !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node;
995 }
996 else if (CLASS_TYPE_P (type)
997 && !CLASSTYPE_USE_TEMPLATE (type)
998 && CLASSTYPE_TEMPLATE_INFO (type)
999 && context && CLASS_TYPE_P (context)
1000 && CLASSTYPE_TEMPLATE_INFO (context))
1001 {
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1004
1005 template <> template <class U> class C<int>::D;
1006
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1009 earlier like:
1010
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1013
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1017
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1019 && !COMPLETE_TYPE_P (type))
1020 {
1021 tree t;
1022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1023
1024 if (current_namespace
1025 != decl_namespace_context (tmpl))
1026 {
1027 permerror (input_location,
1028 "specializing %q#T in different namespace", type);
1029 permerror (DECL_SOURCE_LOCATION (tmpl),
1030 " from definition of %q#D", tmpl);
1031 }
1032
1033 /* Check for invalid specialization after instantiation:
1034
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1037
1038 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1039 t; t = TREE_CHAIN (t))
1040 {
1041 tree inst = TREE_VALUE (t);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1044 {
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1049 spec_entry elt;
1050 spec_entry *entry;
1051
1052 elt.tmpl = most_general_template (tmpl);
1053 elt.args = CLASSTYPE_TI_ARGS (inst);
1054 elt.spec = inst;
1055
1056 type_specializations->remove_elt (&elt);
1057
1058 elt.tmpl = tmpl;
1059 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1060
1061 spec_entry **slot
1062 = type_specializations->find_slot (&elt, INSERT);
1063 entry = ggc_alloc<spec_entry> ();
1064 *entry = elt;
1065 *slot = entry;
1066 }
1067 else
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1071 type, inst);
1072 }
1073
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1076 class template. */
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 CLASSTYPE_TI_ARGS (type)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 }
1082 }
1083 else if (processing_specialization)
1084 {
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1088 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1089 "of %qD not allowed by ISO C++", type);
1090 else
1091 {
1092 error ("explicit specialization of non-template %qT", type);
1093 return error_mark_node;
1094 }
1095 }
1096
1097 return type;
1098 }
1099
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1103
1104 static inline bool
1105 optimize_specialization_lookup_p (tree tmpl)
1106 {
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1108 && DECL_CLASS_SCOPE_P (tmpl)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 parameter. */
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1116 specialization. */
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1119 && !DECL_CONV_FN_P (tmpl)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1122
1123 template <typename T>
1124 struct S { friend A::f(); };
1125
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 }
1132
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1135
1136 static void
1137 verify_unstripped_args (tree args)
1138 {
1139 ++processing_template_decl;
1140 if (!any_dependent_template_arguments_p (args))
1141 {
1142 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1144 {
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1153 else
1154 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1155 }
1156 }
1157 --processing_template_decl;
1158 }
1159
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1166
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1170
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1173
1174 static tree
1175 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1176 {
1177 if (tmpl == NULL_TREE)
1178 return NULL_TREE;
1179
1180 if (args == error_mark_node)
1181 return NULL_TREE;
1182
1183 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl) == FIELD_DECL);
1185
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args)
1189 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1191 : template_class_depth (DECL_CONTEXT (tmpl))));
1192
1193 if (flag_checking)
1194 verify_unstripped_args (args);
1195
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl))
1199 return NULL_TREE;
1200
1201 if (optimize_specialization_lookup_p (tmpl))
1202 {
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1205 arguments. */
1206 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1207 tree class_specialization
1208 = retrieve_specialization (class_template, args, 0);
1209 if (!class_specialization)
1210 return NULL_TREE;
1211
1212 /* Find the instance of TMPL. */
1213 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1214 for (ovl_iterator iter (fns); iter; ++iter)
1215 {
1216 tree fn = *iter;
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1222 }
1223 return NULL_TREE;
1224 }
1225 else
1226 {
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1230
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1234
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1239
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1245 }
1246
1247 return NULL_TREE;
1248 }
1249
1250 /* Like retrieve_specialization, but for local declarations. */
1251
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1254 {
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1257
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1260 }
1261
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263
1264 int
1265 is_specialization_of (tree decl, tree tmpl)
1266 {
1267 tree t;
1268
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1270 {
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1276 }
1277 else
1278 {
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1280
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1287 }
1288
1289 return 0;
1290 }
1291
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1294
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1297 {
1298 bool need_template = true;
1299 int template_depth;
1300
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1303
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1310 {
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1314 }
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1318
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1322
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1325
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1331
1332 For example, given a template friend declaration
1333
1334 template <class T> friend void A<T>::f();
1335
1336 the member function below is considered a friend
1337
1338 template <> struct A<int> {
1339 void f();
1340 };
1341
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1345
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1351 {
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1354
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1357
1358 and DECL's are
1359
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1362
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1371
1372 while (current_depth < template_depth)
1373 {
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1375 {
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1381 }
1382 context = TYPE_CONTEXT (context);
1383 }
1384
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1386 {
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1392
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1400 {
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1409
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1411 }
1412 else
1413 decl_type = TREE_TYPE (decl);
1414
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1419
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1423
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1432
1433 return compparms (decl_args_type, friend_args_type);
1434 }
1435 else
1436 {
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1440
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1446
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1450 {
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1457
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1465 }
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1469 }
1470 }
1471 return false;
1472 }
1473
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1478
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1481
1482 static tree
1483 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1485 {
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1489
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1493
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1502
1503 template <class T> void f(T t1, T t = T())
1504
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1508
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1514 {
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1518
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1521
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1528 }
1529
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1538 {
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1540 {
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1543 {
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1547 }
1548 else
1549 {
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1558
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1562
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1566
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1570
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1578
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1582 is.
1583
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1587 {
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1594 }
1595 check_specialization_namespace (tmpl);
1596
1597 return fn;
1598 }
1599 }
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1601 {
1602 tree dd = duplicate_decls (spec, fn, is_friend);
1603 if (dd == error_mark_node)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node;
1606
1607 if (dd == NULL_TREE && DECL_INITIAL (spec))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1610 definition. */
1611 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1612
1613 return fn;
1614 }
1615 }
1616 else if (fn)
1617 return duplicate_decls (spec, fn, is_friend);
1618
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1622 && !check_specialization_namespace (tmpl))
1623 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1624
1625 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1626 {
1627 spec_entry *entry = ggc_alloc<spec_entry> ();
1628 gcc_assert (tmpl && args && spec);
1629 *entry = elt;
1630 *slot = entry;
1631 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1632 && PRIMARY_TEMPLATE_P (tmpl)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1634 || variable_template_p (tmpl))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1638
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1643 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1644 }
1645
1646 return spec;
1647 }
1648
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1650
1651 int comparing_specializations;
1652
1653 bool
1654 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1655 {
1656 int equal;
1657
1658 ++comparing_specializations;
1659 equal = (e1->tmpl == e2->tmpl
1660 && comp_template_args (e1->args, e2->args));
1661 if (equal && flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1665 && uses_template_parms (e1->args))
1666 {
1667 /* Partial specializations of a variable template can be distinguished by
1668 constraints. */
1669 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1670 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1671 equal = equivalent_constraints (c1, c2);
1672 }
1673 --comparing_specializations;
1674
1675 return equal;
1676 }
1677
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1679
1680 static hashval_t
1681 hash_tmpl_and_args (tree tmpl, tree args)
1682 {
1683 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1684 return iterative_hash_template_arg (args, val);
1685 }
1686
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1688 ignoring SPEC. */
1689
1690 hashval_t
1691 spec_hasher::hash (spec_entry *e)
1692 {
1693 return hash_tmpl_and_args (e->tmpl, e->args);
1694 }
1695
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1698
1699 hashval_t
1700 iterative_hash_template_arg (tree arg, hashval_t val)
1701 {
1702 unsigned HOST_WIDE_INT i;
1703 enum tree_code code;
1704 char tclass;
1705
1706 if (arg == NULL_TREE)
1707 return iterative_hash_object (arg, val);
1708
1709 if (!TYPE_P (arg))
1710 STRIP_NOPS (arg);
1711
1712 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1713 gcc_unreachable ();
1714
1715 code = TREE_CODE (arg);
1716 tclass = TREE_CODE_CLASS (code);
1717
1718 val = iterative_hash_object (code, val);
1719
1720 switch (code)
1721 {
1722 case ERROR_MARK:
1723 return val;
1724
1725 case IDENTIFIER_NODE:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1727
1728 case TREE_VEC:
1729 {
1730 int i, len = TREE_VEC_LENGTH (arg);
1731 for (i = 0; i < len; ++i)
1732 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1733 return val;
1734 }
1735
1736 case TYPE_PACK_EXPANSION:
1737 case EXPR_PACK_EXPANSION:
1738 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1740
1741 case TYPE_ARGUMENT_PACK:
1742 case NONTYPE_ARGUMENT_PACK:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1744
1745 case TREE_LIST:
1746 for (; arg; arg = TREE_CHAIN (arg))
1747 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1748 return val;
1749
1750 case OVERLOAD:
1751 for (lkp_iterator iter (arg); iter; ++iter)
1752 val = iterative_hash_template_arg (*iter, val);
1753 return val;
1754
1755 case CONSTRUCTOR:
1756 {
1757 tree field, value;
1758 iterative_hash_template_arg (TREE_TYPE (arg), val);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1760 {
1761 val = iterative_hash_template_arg (field, val);
1762 val = iterative_hash_template_arg (value, val);
1763 }
1764 return val;
1765 }
1766
1767 case PARM_DECL:
1768 if (!DECL_ARTIFICIAL (arg))
1769 {
1770 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1771 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1772 }
1773 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1774
1775 case TARGET_EXPR:
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1777
1778 case PTRMEM_CST:
1779 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1781
1782 case TEMPLATE_PARM_INDEX:
1783 val = iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1785 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1787
1788 case TRAIT_EXPR:
1789 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1790 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1792
1793 case BASELINK:
1794 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1795 val);
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1797 val);
1798
1799 case MODOP_EXPR:
1800 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1801 code = TREE_CODE (TREE_OPERAND (arg, 1));
1802 val = iterative_hash_object (code, val);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1804
1805 case LAMBDA_EXPR:
1806 /* A lambda can't appear in a template arg, but don't crash on
1807 erroneous input. */
1808 gcc_assert (seen_error ());
1809 return val;
1810
1811 case CAST_EXPR:
1812 case IMPLICIT_CONV_EXPR:
1813 case STATIC_CAST_EXPR:
1814 case REINTERPRET_CAST_EXPR:
1815 case CONST_CAST_EXPR:
1816 case DYNAMIC_CAST_EXPR:
1817 case NEW_EXPR:
1818 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1819 /* Now hash operands as usual. */
1820 break;
1821
1822 default:
1823 break;
1824 }
1825
1826 switch (tclass)
1827 {
1828 case tcc_type:
1829 if (alias_template_specialization_p (arg))
1830 {
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1840 }
1841 if (TYPE_CANONICAL (arg))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1843 val);
1844 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1846 /* Otherwise just compare the types during lookup. */
1847 return val;
1848
1849 case tcc_declaration:
1850 case tcc_constant:
1851 return iterative_hash_expr (arg, val);
1852
1853 default:
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1855 {
1856 unsigned n = cp_tree_operand_length (arg);
1857 for (i = 0; i < n; ++i)
1858 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1859 return val;
1860 }
1861 }
1862 gcc_unreachable ();
1863 return 0;
1864 }
1865
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1869
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1871
1872 bool
1873 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1874 {
1875 spec_entry *entry;
1876 spec_entry elt;
1877
1878 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1879 elt.args = TI_ARGS (tinfo);
1880 elt.spec = NULL_TREE;
1881
1882 entry = decl_specializations->find (&elt);
1883 if (entry != NULL)
1884 {
1885 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1886 gcc_assert (new_spec != NULL_TREE);
1887 entry->spec = new_spec;
1888 return 1;
1889 }
1890
1891 return 0;
1892 }
1893
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1896
1897 void
1898 register_local_specialization (tree spec, tree tmpl)
1899 {
1900 gcc_assert (tmpl != spec);
1901 local_specializations->put (tmpl, spec);
1902 }
1903
1904 /* TYPE is a class type. Returns true if TYPE is an explicitly
1905 specialized class. */
1906
1907 bool
1908 explicit_class_specialization_p (tree type)
1909 {
1910 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1911 return false;
1912 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1913 }
1914
1915 /* Print the list of functions at FNS, going through all the overloads
1916 for each element of the list. Alternatively, FNS can not be a
1917 TREE_LIST, in which case it will be printed together with all the
1918 overloads.
1919
1920 MORE and *STR should respectively be FALSE and NULL when the function
1921 is called from the outside. They are used internally on recursive
1922 calls. print_candidates manages the two parameters and leaves NULL
1923 in *STR when it ends. */
1924
1925 static void
1926 print_candidates_1 (tree fns, char **str, bool more = false)
1927 {
1928 if (TREE_CODE (fns) == TREE_LIST)
1929 for (; fns; fns = TREE_CHAIN (fns))
1930 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1931 else
1932 for (lkp_iterator iter (fns); iter;)
1933 {
1934 tree cand = *iter;
1935 ++iter;
1936
1937 const char *pfx = *str;
1938 if (!pfx)
1939 {
1940 if (more || iter)
1941 pfx = _("candidates are:");
1942 else
1943 pfx = _("candidate is:");
1944 *str = get_spaces (pfx);
1945 }
1946 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1947 }
1948 }
1949
1950 /* Print the list of candidate FNS in an error message. FNS can also
1951 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1952
1953 void
1954 print_candidates (tree fns)
1955 {
1956 char *str = NULL;
1957 print_candidates_1 (fns, &str);
1958 free (str);
1959 }
1960
1961 /* Get a (possibly) constrained template declaration for the
1962 purpose of ordering candidates. */
1963 static tree
1964 get_template_for_ordering (tree list)
1965 {
1966 gcc_assert (TREE_CODE (list) == TREE_LIST);
1967 tree f = TREE_VALUE (list);
1968 if (tree ti = DECL_TEMPLATE_INFO (f))
1969 return TI_TEMPLATE (ti);
1970 return f;
1971 }
1972
1973 /* Among candidates having the same signature, return the
1974 most constrained or NULL_TREE if there is no best candidate.
1975 If the signatures of candidates vary (e.g., template
1976 specialization vs. member function), then there can be no
1977 most constrained.
1978
1979 Note that we don't compare constraints on the functions
1980 themselves, but rather those of their templates. */
1981 static tree
1982 most_constrained_function (tree candidates)
1983 {
1984 // Try to find the best candidate in a first pass.
1985 tree champ = candidates;
1986 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1987 {
1988 int winner = more_constrained (get_template_for_ordering (champ),
1989 get_template_for_ordering (c));
1990 if (winner == -1)
1991 champ = c; // The candidate is more constrained
1992 else if (winner == 0)
1993 return NULL_TREE; // Neither is more constrained
1994 }
1995
1996 // Verify that the champ is better than previous candidates.
1997 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1998 if (!more_constrained (get_template_for_ordering (champ),
1999 get_template_for_ordering (c)))
2000 return NULL_TREE;
2001 }
2002
2003 return champ;
2004 }
2005
2006
2007 /* Returns the template (one of the functions given by TEMPLATE_ID)
2008 which can be specialized to match the indicated DECL with the
2009 explicit template args given in TEMPLATE_ID. The DECL may be
2010 NULL_TREE if none is available. In that case, the functions in
2011 TEMPLATE_ID are non-members.
2012
2013 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2014 specialization of a member template.
2015
2016 The TEMPLATE_COUNT is the number of references to qualifying
2017 template classes that appeared in the name of the function. See
2018 check_explicit_specialization for a more accurate description.
2019
2020 TSK indicates what kind of template declaration (if any) is being
2021 declared. TSK_TEMPLATE indicates that the declaration given by
2022 DECL, though a FUNCTION_DECL, has template parameters, and is
2023 therefore a template function.
2024
2025 The template args (those explicitly specified and those deduced)
2026 are output in a newly created vector *TARGS_OUT.
2027
2028 If it is impossible to determine the result, an error message is
2029 issued. The error_mark_node is returned to indicate failure. */
2030
2031 static tree
2032 determine_specialization (tree template_id,
2033 tree decl,
2034 tree* targs_out,
2035 int need_member_template,
2036 int template_count,
2037 tmpl_spec_kind tsk)
2038 {
2039 tree fns;
2040 tree targs;
2041 tree explicit_targs;
2042 tree candidates = NULL_TREE;
2043
2044 /* A TREE_LIST of templates of which DECL may be a specialization.
2045 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2046 corresponding TREE_PURPOSE is the set of template arguments that,
2047 when used to instantiate the template, would produce a function
2048 with the signature of DECL. */
2049 tree templates = NULL_TREE;
2050 int header_count;
2051 cp_binding_level *b;
2052
2053 *targs_out = NULL_TREE;
2054
2055 if (template_id == error_mark_node || decl == error_mark_node)
2056 return error_mark_node;
2057
2058 /* We shouldn't be specializing a member template of an
2059 unspecialized class template; we already gave an error in
2060 check_specialization_scope, now avoid crashing. */
2061 if (template_count && DECL_CLASS_SCOPE_P (decl)
2062 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2063 {
2064 gcc_assert (errorcount);
2065 return error_mark_node;
2066 }
2067
2068 fns = TREE_OPERAND (template_id, 0);
2069 explicit_targs = TREE_OPERAND (template_id, 1);
2070
2071 if (fns == error_mark_node)
2072 return error_mark_node;
2073
2074 /* Check for baselinks. */
2075 if (BASELINK_P (fns))
2076 fns = BASELINK_FUNCTIONS (fns);
2077
2078 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2079 {
2080 error ("%qD is not a function template", fns);
2081 return error_mark_node;
2082 }
2083 else if (VAR_P (decl) && !variable_template_p (fns))
2084 {
2085 error ("%qD is not a variable template", fns);
2086 return error_mark_node;
2087 }
2088
2089 /* Count the number of template headers specified for this
2090 specialization. */
2091 header_count = 0;
2092 for (b = current_binding_level;
2093 b->kind == sk_template_parms;
2094 b = b->level_chain)
2095 ++header_count;
2096
2097 tree orig_fns = fns;
2098
2099 if (variable_template_p (fns))
2100 {
2101 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2102 targs = coerce_template_parms (parms, explicit_targs, fns,
2103 tf_warning_or_error,
2104 /*req_all*/true, /*use_defarg*/true);
2105 if (targs != error_mark_node)
2106 templates = tree_cons (targs, fns, templates);
2107 }
2108 else for (lkp_iterator iter (fns); iter; ++iter)
2109 {
2110 tree fn = *iter;
2111
2112 if (TREE_CODE (fn) == TEMPLATE_DECL)
2113 {
2114 tree decl_arg_types;
2115 tree fn_arg_types;
2116 tree insttype;
2117
2118 /* In case of explicit specialization, we need to check if
2119 the number of template headers appearing in the specialization
2120 is correct. This is usually done in check_explicit_specialization,
2121 but the check done there cannot be exhaustive when specializing
2122 member functions. Consider the following code:
2123
2124 template <> void A<int>::f(int);
2125 template <> template <> void A<int>::f(int);
2126
2127 Assuming that A<int> is not itself an explicit specialization
2128 already, the first line specializes "f" which is a non-template
2129 member function, whilst the second line specializes "f" which
2130 is a template member function. So both lines are syntactically
2131 correct, and check_explicit_specialization does not reject
2132 them.
2133
2134 Here, we can do better, as we are matching the specialization
2135 against the declarations. We count the number of template
2136 headers, and we check if they match TEMPLATE_COUNT + 1
2137 (TEMPLATE_COUNT is the number of qualifying template classes,
2138 plus there must be another header for the member template
2139 itself).
2140
2141 Notice that if header_count is zero, this is not a
2142 specialization but rather a template instantiation, so there
2143 is no check we can perform here. */
2144 if (header_count && header_count != template_count + 1)
2145 continue;
2146
2147 /* Check that the number of template arguments at the
2148 innermost level for DECL is the same as for FN. */
2149 if (current_binding_level->kind == sk_template_parms
2150 && !current_binding_level->explicit_spec_p
2151 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2152 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2153 (current_template_parms))))
2154 continue;
2155
2156 /* DECL might be a specialization of FN. */
2157 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2158 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2159
2160 /* For a non-static member function, we need to make sure
2161 that the const qualification is the same. Since
2162 get_bindings does not try to merge the "this" parameter,
2163 we must do the comparison explicitly. */
2164 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2165 && !same_type_p (TREE_VALUE (fn_arg_types),
2166 TREE_VALUE (decl_arg_types)))
2167 continue;
2168
2169 /* Skip the "this" parameter and, for constructors of
2170 classes with virtual bases, the VTT parameter. A
2171 full specialization of a constructor will have a VTT
2172 parameter, but a template never will. */
2173 decl_arg_types
2174 = skip_artificial_parms_for (decl, decl_arg_types);
2175 fn_arg_types
2176 = skip_artificial_parms_for (fn, fn_arg_types);
2177
2178 /* Function templates cannot be specializations; there are
2179 no partial specializations of functions. Therefore, if
2180 the type of DECL does not match FN, there is no
2181 match.
2182
2183 Note that it should never be the case that we have both
2184 candidates added here, and for regular member functions
2185 below. */
2186 if (tsk == tsk_template)
2187 {
2188 if (compparms (fn_arg_types, decl_arg_types))
2189 candidates = tree_cons (NULL_TREE, fn, candidates);
2190 continue;
2191 }
2192
2193 /* See whether this function might be a specialization of this
2194 template. Suppress access control because we might be trying
2195 to make this specialization a friend, and we have already done
2196 access control for the declaration of the specialization. */
2197 push_deferring_access_checks (dk_no_check);
2198 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2199 pop_deferring_access_checks ();
2200
2201 if (!targs)
2202 /* We cannot deduce template arguments that when used to
2203 specialize TMPL will produce DECL. */
2204 continue;
2205
2206 if (uses_template_parms (targs))
2207 /* We deduced something involving 'auto', which isn't a valid
2208 template argument. */
2209 continue;
2210
2211 /* Remove, from the set of candidates, all those functions
2212 whose constraints are not satisfied. */
2213 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2214 continue;
2215
2216 // Then, try to form the new function type.
2217 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2218 if (insttype == error_mark_node)
2219 continue;
2220 fn_arg_types
2221 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2222 if (!compparms (fn_arg_types, decl_arg_types))
2223 continue;
2224
2225 /* Save this template, and the arguments deduced. */
2226 templates = tree_cons (targs, fn, templates);
2227 }
2228 else if (need_member_template)
2229 /* FN is an ordinary member function, and we need a
2230 specialization of a member template. */
2231 ;
2232 else if (TREE_CODE (fn) != FUNCTION_DECL)
2233 /* We can get IDENTIFIER_NODEs here in certain erroneous
2234 cases. */
2235 ;
2236 else if (!DECL_FUNCTION_MEMBER_P (fn))
2237 /* This is just an ordinary non-member function. Nothing can
2238 be a specialization of that. */
2239 ;
2240 else if (DECL_ARTIFICIAL (fn))
2241 /* Cannot specialize functions that are created implicitly. */
2242 ;
2243 else
2244 {
2245 tree decl_arg_types;
2246
2247 /* This is an ordinary member function. However, since
2248 we're here, we can assume its enclosing class is a
2249 template class. For example,
2250
2251 template <typename T> struct S { void f(); };
2252 template <> void S<int>::f() {}
2253
2254 Here, S<int>::f is a non-template, but S<int> is a
2255 template class. If FN has the same type as DECL, we
2256 might be in business. */
2257
2258 if (!DECL_TEMPLATE_INFO (fn))
2259 /* Its enclosing class is an explicit specialization
2260 of a template class. This is not a candidate. */
2261 continue;
2262
2263 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2264 TREE_TYPE (TREE_TYPE (fn))))
2265 /* The return types differ. */
2266 continue;
2267
2268 /* Adjust the type of DECL in case FN is a static member. */
2269 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2270 if (DECL_STATIC_FUNCTION_P (fn)
2271 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2272 decl_arg_types = TREE_CHAIN (decl_arg_types);
2273
2274 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2275 decl_arg_types))
2276 continue;
2277
2278 // If the deduced arguments do not satisfy the constraints,
2279 // this is not a candidate.
2280 if (flag_concepts && !constraints_satisfied_p (fn))
2281 continue;
2282
2283 // Add the candidate.
2284 candidates = tree_cons (NULL_TREE, fn, candidates);
2285 }
2286 }
2287
2288 if (templates && TREE_CHAIN (templates))
2289 {
2290 /* We have:
2291
2292 [temp.expl.spec]
2293
2294 It is possible for a specialization with a given function
2295 signature to be instantiated from more than one function
2296 template. In such cases, explicit specification of the
2297 template arguments must be used to uniquely identify the
2298 function template specialization being specialized.
2299
2300 Note that here, there's no suggestion that we're supposed to
2301 determine which of the candidate templates is most
2302 specialized. However, we, also have:
2303
2304 [temp.func.order]
2305
2306 Partial ordering of overloaded function template
2307 declarations is used in the following contexts to select
2308 the function template to which a function template
2309 specialization refers:
2310
2311 -- when an explicit specialization refers to a function
2312 template.
2313
2314 So, we do use the partial ordering rules, at least for now.
2315 This extension can only serve to make invalid programs valid,
2316 so it's safe. And, there is strong anecdotal evidence that
2317 the committee intended the partial ordering rules to apply;
2318 the EDG front end has that behavior, and John Spicer claims
2319 that the committee simply forgot to delete the wording in
2320 [temp.expl.spec]. */
2321 tree tmpl = most_specialized_instantiation (templates);
2322 if (tmpl != error_mark_node)
2323 {
2324 templates = tmpl;
2325 TREE_CHAIN (templates) = NULL_TREE;
2326 }
2327 }
2328
2329 // Concepts allows multiple declarations of member functions
2330 // with the same signature. Like above, we need to rely on
2331 // on the partial ordering of those candidates to determine which
2332 // is the best.
2333 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2334 {
2335 if (tree cand = most_constrained_function (candidates))
2336 {
2337 candidates = cand;
2338 TREE_CHAIN (cand) = NULL_TREE;
2339 }
2340 }
2341
2342 if (templates == NULL_TREE && candidates == NULL_TREE)
2343 {
2344 error ("template-id %qD for %q+D does not match any template "
2345 "declaration", template_id, decl);
2346 if (header_count && header_count != template_count + 1)
2347 inform (input_location, "saw %d %<template<>%>, need %d for "
2348 "specializing a member function template",
2349 header_count, template_count + 1);
2350 else
2351 print_candidates (orig_fns);
2352 return error_mark_node;
2353 }
2354 else if ((templates && TREE_CHAIN (templates))
2355 || (candidates && TREE_CHAIN (candidates))
2356 || (templates && candidates))
2357 {
2358 error ("ambiguous template specialization %qD for %q+D",
2359 template_id, decl);
2360 candidates = chainon (candidates, templates);
2361 print_candidates (candidates);
2362 return error_mark_node;
2363 }
2364
2365 /* We have one, and exactly one, match. */
2366 if (candidates)
2367 {
2368 tree fn = TREE_VALUE (candidates);
2369 *targs_out = copy_node (DECL_TI_ARGS (fn));
2370
2371 // Propagate the candidate's constraints to the declaration.
2372 set_constraints (decl, get_constraints (fn));
2373
2374 /* DECL is a re-declaration or partial instantiation of a template
2375 function. */
2376 if (TREE_CODE (fn) == TEMPLATE_DECL)
2377 return fn;
2378 /* It was a specialization of an ordinary member function in a
2379 template class. */
2380 return DECL_TI_TEMPLATE (fn);
2381 }
2382
2383 /* It was a specialization of a template. */
2384 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2385 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2386 {
2387 *targs_out = copy_node (targs);
2388 SET_TMPL_ARGS_LEVEL (*targs_out,
2389 TMPL_ARGS_DEPTH (*targs_out),
2390 TREE_PURPOSE (templates));
2391 }
2392 else
2393 *targs_out = TREE_PURPOSE (templates);
2394 return TREE_VALUE (templates);
2395 }
2396
2397 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2398 but with the default argument values filled in from those in the
2399 TMPL_TYPES. */
2400
2401 static tree
2402 copy_default_args_to_explicit_spec_1 (tree spec_types,
2403 tree tmpl_types)
2404 {
2405 tree new_spec_types;
2406
2407 if (!spec_types)
2408 return NULL_TREE;
2409
2410 if (spec_types == void_list_node)
2411 return void_list_node;
2412
2413 /* Substitute into the rest of the list. */
2414 new_spec_types =
2415 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2416 TREE_CHAIN (tmpl_types));
2417
2418 /* Add the default argument for this parameter. */
2419 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2420 TREE_VALUE (spec_types),
2421 new_spec_types);
2422 }
2423
2424 /* DECL is an explicit specialization. Replicate default arguments
2425 from the template it specializes. (That way, code like:
2426
2427 template <class T> void f(T = 3);
2428 template <> void f(double);
2429 void g () { f (); }
2430
2431 works, as required.) An alternative approach would be to look up
2432 the correct default arguments at the call-site, but this approach
2433 is consistent with how implicit instantiations are handled. */
2434
2435 static void
2436 copy_default_args_to_explicit_spec (tree decl)
2437 {
2438 tree tmpl;
2439 tree spec_types;
2440 tree tmpl_types;
2441 tree new_spec_types;
2442 tree old_type;
2443 tree new_type;
2444 tree t;
2445 tree object_type = NULL_TREE;
2446 tree in_charge = NULL_TREE;
2447 tree vtt = NULL_TREE;
2448
2449 /* See if there's anything we need to do. */
2450 tmpl = DECL_TI_TEMPLATE (decl);
2451 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2452 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2453 if (TREE_PURPOSE (t))
2454 break;
2455 if (!t)
2456 return;
2457
2458 old_type = TREE_TYPE (decl);
2459 spec_types = TYPE_ARG_TYPES (old_type);
2460
2461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2462 {
2463 /* Remove the this pointer, but remember the object's type for
2464 CV quals. */
2465 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2466 spec_types = TREE_CHAIN (spec_types);
2467 tmpl_types = TREE_CHAIN (tmpl_types);
2468
2469 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2470 {
2471 /* DECL may contain more parameters than TMPL due to the extra
2472 in-charge parameter in constructors and destructors. */
2473 in_charge = spec_types;
2474 spec_types = TREE_CHAIN (spec_types);
2475 }
2476 if (DECL_HAS_VTT_PARM_P (decl))
2477 {
2478 vtt = spec_types;
2479 spec_types = TREE_CHAIN (spec_types);
2480 }
2481 }
2482
2483 /* Compute the merged default arguments. */
2484 new_spec_types =
2485 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2486
2487 /* Compute the new FUNCTION_TYPE. */
2488 if (object_type)
2489 {
2490 if (vtt)
2491 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2492 TREE_VALUE (vtt),
2493 new_spec_types);
2494
2495 if (in_charge)
2496 /* Put the in-charge parameter back. */
2497 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2498 TREE_VALUE (in_charge),
2499 new_spec_types);
2500
2501 new_type = build_method_type_directly (object_type,
2502 TREE_TYPE (old_type),
2503 new_spec_types);
2504 }
2505 else
2506 new_type = build_function_type (TREE_TYPE (old_type),
2507 new_spec_types);
2508 new_type = cp_build_type_attribute_variant (new_type,
2509 TYPE_ATTRIBUTES (old_type));
2510 new_type = build_exception_variant (new_type,
2511 TYPE_RAISES_EXCEPTIONS (old_type));
2512
2513 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2514 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2515
2516 TREE_TYPE (decl) = new_type;
2517 }
2518
2519 /* Return the number of template headers we expect to see for a definition
2520 or specialization of CTYPE or one of its non-template members. */
2521
2522 int
2523 num_template_headers_for_class (tree ctype)
2524 {
2525 int num_templates = 0;
2526
2527 while (ctype && CLASS_TYPE_P (ctype))
2528 {
2529 /* You're supposed to have one `template <...>' for every
2530 template class, but you don't need one for a full
2531 specialization. For example:
2532
2533 template <class T> struct S{};
2534 template <> struct S<int> { void f(); };
2535 void S<int>::f () {}
2536
2537 is correct; there shouldn't be a `template <>' for the
2538 definition of `S<int>::f'. */
2539 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2540 /* If CTYPE does not have template information of any
2541 kind, then it is not a template, nor is it nested
2542 within a template. */
2543 break;
2544 if (explicit_class_specialization_p (ctype))
2545 break;
2546 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2547 ++num_templates;
2548
2549 ctype = TYPE_CONTEXT (ctype);
2550 }
2551
2552 return num_templates;
2553 }
2554
2555 /* Do a simple sanity check on the template headers that precede the
2556 variable declaration DECL. */
2557
2558 void
2559 check_template_variable (tree decl)
2560 {
2561 tree ctx = CP_DECL_CONTEXT (decl);
2562 int wanted = num_template_headers_for_class (ctx);
2563 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2565 {
2566 if (cxx_dialect < cxx14)
2567 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2568 "variable templates only available with "
2569 "-std=c++14 or -std=gnu++14");
2570
2571 // Namespace-scope variable templates should have a template header.
2572 ++wanted;
2573 }
2574 if (template_header_count > wanted)
2575 {
2576 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2577 "too many template headers for %qD "
2578 "(should be %d)",
2579 decl, wanted);
2580 if (warned && CLASS_TYPE_P (ctx)
2581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2582 inform (DECL_SOURCE_LOCATION (decl),
2583 "members of an explicitly specialized class are defined "
2584 "without a template header");
2585 }
2586 }
2587
2588 /* An explicit specialization whose declarator-id or class-head-name is not
2589 qualified shall be declared in the nearest enclosing namespace of the
2590 template, or, if the namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set.
2592
2593 If the name declared in the explicit instantiation is an unqualified name,
2594 the explicit instantiation shall appear in the namespace where its template
2595 is declared or, if that namespace is inline (7.3.1), any namespace from its
2596 enclosing namespace set. */
2597
2598 void
2599 check_unqualified_spec_or_inst (tree t, location_t loc)
2600 {
2601 tree tmpl = most_general_template (t);
2602 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2603 && !is_nested_namespace (current_namespace,
2604 CP_DECL_CONTEXT (tmpl), true))
2605 {
2606 if (processing_specialization)
2607 permerror (loc, "explicit specialization of %qD outside its "
2608 "namespace must use a nested-name-specifier", tmpl);
2609 else if (processing_explicit_instantiation
2610 && cxx_dialect >= cxx11)
2611 /* This was allowed in C++98, so only pedwarn. */
2612 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2613 "outside its namespace must use a nested-name-"
2614 "specifier", tmpl);
2615 }
2616 }
2617
2618 /* Check to see if the function just declared, as indicated in
2619 DECLARATOR, and in DECL, is a specialization of a function
2620 template. We may also discover that the declaration is an explicit
2621 instantiation at this point.
2622
2623 Returns DECL, or an equivalent declaration that should be used
2624 instead if all goes well. Issues an error message if something is
2625 amiss. Returns error_mark_node if the error is not easily
2626 recoverable.
2627
2628 FLAGS is a bitmask consisting of the following flags:
2629
2630 2: The function has a definition.
2631 4: The function is a friend.
2632
2633 The TEMPLATE_COUNT is the number of references to qualifying
2634 template classes that appeared in the name of the function. For
2635 example, in
2636
2637 template <class T> struct S { void f(); };
2638 void S<int>::f();
2639
2640 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2641 classes are not counted in the TEMPLATE_COUNT, so that in
2642
2643 template <class T> struct S {};
2644 template <> struct S<int> { void f(); }
2645 template <> void S<int>::f();
2646
2647 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2648 invalid; there should be no template <>.)
2649
2650 If the function is a specialization, it is marked as such via
2651 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2652 is set up correctly, and it is added to the list of specializations
2653 for that template. */
2654
2655 tree
2656 check_explicit_specialization (tree declarator,
2657 tree decl,
2658 int template_count,
2659 int flags)
2660 {
2661 int have_def = flags & 2;
2662 int is_friend = flags & 4;
2663 bool is_concept = flags & 8;
2664 int specialization = 0;
2665 int explicit_instantiation = 0;
2666 int member_specialization = 0;
2667 tree ctype = DECL_CLASS_CONTEXT (decl);
2668 tree dname = DECL_NAME (decl);
2669 tmpl_spec_kind tsk;
2670
2671 if (is_friend)
2672 {
2673 if (!processing_specialization)
2674 tsk = tsk_none;
2675 else
2676 tsk = tsk_excessive_parms;
2677 }
2678 else
2679 tsk = current_tmpl_spec_kind (template_count);
2680
2681 switch (tsk)
2682 {
2683 case tsk_none:
2684 if (processing_specialization && !VAR_P (decl))
2685 {
2686 specialization = 1;
2687 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2688 }
2689 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2690 {
2691 if (is_friend)
2692 /* This could be something like:
2693
2694 template <class T> void f(T);
2695 class S { friend void f<>(int); } */
2696 specialization = 1;
2697 else
2698 {
2699 /* This case handles bogus declarations like template <>
2700 template <class T> void f<int>(); */
2701
2702 error ("template-id %qD in declaration of primary template",
2703 declarator);
2704 return decl;
2705 }
2706 }
2707 break;
2708
2709 case tsk_invalid_member_spec:
2710 /* The error has already been reported in
2711 check_specialization_scope. */
2712 return error_mark_node;
2713
2714 case tsk_invalid_expl_inst:
2715 error ("template parameter list used in explicit instantiation");
2716
2717 /* Fall through. */
2718
2719 case tsk_expl_inst:
2720 if (have_def)
2721 error ("definition provided for explicit instantiation");
2722
2723 explicit_instantiation = 1;
2724 break;
2725
2726 case tsk_excessive_parms:
2727 case tsk_insufficient_parms:
2728 if (tsk == tsk_excessive_parms)
2729 error ("too many template parameter lists in declaration of %qD",
2730 decl);
2731 else if (template_header_count)
2732 error("too few template parameter lists in declaration of %qD", decl);
2733 else
2734 error("explicit specialization of %qD must be introduced by "
2735 "%<template <>%>", decl);
2736
2737 /* Fall through. */
2738 case tsk_expl_spec:
2739 if (is_concept)
2740 error ("explicit specialization declared %<concept%>");
2741
2742 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2743 /* In cases like template<> constexpr bool v = true;
2744 We'll give an error in check_template_variable. */
2745 break;
2746
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2748 if (ctype)
2749 member_specialization = 1;
2750 else
2751 specialization = 1;
2752 break;
2753
2754 case tsk_template:
2755 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2756 {
2757 /* This case handles bogus declarations like template <>
2758 template <class T> void f<int>(); */
2759
2760 if (!uses_template_parms (declarator))
2761 error ("template-id %qD in declaration of primary template",
2762 declarator);
2763 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2764 {
2765 /* Partial specialization of variable template. */
2766 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2767 specialization = 1;
2768 goto ok;
2769 }
2770 else if (cxx_dialect < cxx14)
2771 error ("non-type partial specialization %qD "
2772 "is not allowed", declarator);
2773 else
2774 error ("non-class, non-variable partial specialization %qD "
2775 "is not allowed", declarator);
2776 return decl;
2777 ok:;
2778 }
2779
2780 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2781 /* This is a specialization of a member template, without
2782 specialization the containing class. Something like:
2783
2784 template <class T> struct S {
2785 template <class U> void f (U);
2786 };
2787 template <> template <class U> void S<int>::f(U) {}
2788
2789 That's a specialization -- but of the entire template. */
2790 specialization = 1;
2791 break;
2792
2793 default:
2794 gcc_unreachable ();
2795 }
2796
2797 if ((specialization || member_specialization)
2798 /* This doesn't apply to variable templates. */
2799 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2800 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2801 {
2802 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2803 for (; t; t = TREE_CHAIN (t))
2804 if (TREE_PURPOSE (t))
2805 {
2806 permerror (input_location,
2807 "default argument specified in explicit specialization");
2808 break;
2809 }
2810 }
2811
2812 if (specialization || member_specialization || explicit_instantiation)
2813 {
2814 tree tmpl = NULL_TREE;
2815 tree targs = NULL_TREE;
2816 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2817
2818 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2819 if (!was_template_id)
2820 {
2821 tree fns;
2822
2823 gcc_assert (identifier_p (declarator));
2824 if (ctype)
2825 fns = dname;
2826 else
2827 {
2828 /* If there is no class context, the explicit instantiation
2829 must be at namespace scope. */
2830 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2831
2832 /* Find the namespace binding, using the declaration
2833 context. */
2834 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2835 false, true);
2836 if (fns == error_mark_node)
2837 /* If lookup fails, look for a friend declaration so we can
2838 give a better diagnostic. */
2839 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2840 /*type*/false, /*complain*/true,
2841 /*hidden*/true);
2842
2843 if (fns == error_mark_node || !is_overloaded_fn (fns))
2844 {
2845 error ("%qD is not a template function", dname);
2846 fns = error_mark_node;
2847 }
2848 }
2849
2850 declarator = lookup_template_function (fns, NULL_TREE);
2851 }
2852
2853 if (declarator == error_mark_node)
2854 return error_mark_node;
2855
2856 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2857 {
2858 if (!explicit_instantiation)
2859 /* A specialization in class scope. This is invalid,
2860 but the error will already have been flagged by
2861 check_specialization_scope. */
2862 return error_mark_node;
2863 else
2864 {
2865 /* It's not valid to write an explicit instantiation in
2866 class scope, e.g.:
2867
2868 class C { template void f(); }
2869
2870 This case is caught by the parser. However, on
2871 something like:
2872
2873 template class C { void f(); };
2874
2875 (which is invalid) we can get here. The error will be
2876 issued later. */
2877 ;
2878 }
2879
2880 return decl;
2881 }
2882 else if (ctype != NULL_TREE
2883 && (identifier_p (TREE_OPERAND (declarator, 0))))
2884 {
2885 // We'll match variable templates in start_decl.
2886 if (VAR_P (decl))
2887 return decl;
2888
2889 /* Find the list of functions in ctype that have the same
2890 name as the declared function. */
2891 tree name = TREE_OPERAND (declarator, 0);
2892
2893 if (constructor_name_p (name, ctype))
2894 {
2895 if (DECL_CONSTRUCTOR_P (decl)
2896 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2897 : !CLASSTYPE_DESTRUCTOR (ctype))
2898 {
2899 /* From [temp.expl.spec]:
2900
2901 If such an explicit specialization for the member
2902 of a class template names an implicitly-declared
2903 special member function (clause _special_), the
2904 program is ill-formed.
2905
2906 Similar language is found in [temp.explicit]. */
2907 error ("specialization of implicitly-declared special member function");
2908 return error_mark_node;
2909 }
2910
2911 name = DECL_NAME (decl);
2912 }
2913
2914 /* For a type-conversion operator, We might be looking for
2915 `operator int' which will be a specialization of
2916 `operator T'. Grab all the conversion operators, and
2917 then select from them. */
2918 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2919 ? conv_op_identifier : name);
2920
2921 if (fns == NULL_TREE)
2922 {
2923 error ("no member function %qD declared in %qT", name, ctype);
2924 return error_mark_node;
2925 }
2926 else
2927 TREE_OPERAND (declarator, 0) = fns;
2928 }
2929
2930 /* Figure out what exactly is being specialized at this point.
2931 Note that for an explicit instantiation, even one for a
2932 member function, we cannot tell a priori whether the
2933 instantiation is for a member template, or just a member
2934 function of a template class. Even if a member template is
2935 being instantiated, the member template arguments may be
2936 elided if they can be deduced from the rest of the
2937 declaration. */
2938 tmpl = determine_specialization (declarator, decl,
2939 &targs,
2940 member_specialization,
2941 template_count,
2942 tsk);
2943
2944 if (!tmpl || tmpl == error_mark_node)
2945 /* We couldn't figure out what this declaration was
2946 specializing. */
2947 return error_mark_node;
2948 else
2949 {
2950 if (TREE_CODE (decl) == FUNCTION_DECL
2951 && DECL_HIDDEN_FRIEND_P (tmpl))
2952 {
2953 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2954 "friend declaration %qD is not visible to "
2955 "explicit specialization", tmpl))
2956 inform (DECL_SOURCE_LOCATION (tmpl),
2957 "friend declaration here");
2958 }
2959 else if (!ctype && !is_friend
2960 && CP_DECL_CONTEXT (decl) == current_namespace)
2961 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2962
2963 tree gen_tmpl = most_general_template (tmpl);
2964
2965 if (explicit_instantiation)
2966 {
2967 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2968 is done by do_decl_instantiation later. */
2969
2970 int arg_depth = TMPL_ARGS_DEPTH (targs);
2971 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2972
2973 if (arg_depth > parm_depth)
2974 {
2975 /* If TMPL is not the most general template (for
2976 example, if TMPL is a friend template that is
2977 injected into namespace scope), then there will
2978 be too many levels of TARGS. Remove some of them
2979 here. */
2980 int i;
2981 tree new_targs;
2982
2983 new_targs = make_tree_vec (parm_depth);
2984 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2985 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2986 = TREE_VEC_ELT (targs, i);
2987 targs = new_targs;
2988 }
2989
2990 return instantiate_template (tmpl, targs, tf_error);
2991 }
2992
2993 /* If we thought that the DECL was a member function, but it
2994 turns out to be specializing a static member function,
2995 make DECL a static member function as well. */
2996 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2997 && DECL_STATIC_FUNCTION_P (tmpl)
2998 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2999 revert_static_member_fn (decl);
3000
3001 /* If this is a specialization of a member template of a
3002 template class, we want to return the TEMPLATE_DECL, not
3003 the specialization of it. */
3004 if (tsk == tsk_template && !was_template_id)
3005 {
3006 tree result = DECL_TEMPLATE_RESULT (tmpl);
3007 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3008 DECL_INITIAL (result) = NULL_TREE;
3009 if (have_def)
3010 {
3011 tree parm;
3012 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3013 DECL_SOURCE_LOCATION (result)
3014 = DECL_SOURCE_LOCATION (decl);
3015 /* We want to use the argument list specified in the
3016 definition, not in the original declaration. */
3017 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3018 for (parm = DECL_ARGUMENTS (result); parm;
3019 parm = DECL_CHAIN (parm))
3020 DECL_CONTEXT (parm) = result;
3021 }
3022 return register_specialization (tmpl, gen_tmpl, targs,
3023 is_friend, 0);
3024 }
3025
3026 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3027 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3028
3029 if (was_template_id)
3030 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3031
3032 /* Inherit default function arguments from the template
3033 DECL is specializing. */
3034 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3035 copy_default_args_to_explicit_spec (decl);
3036
3037 /* This specialization has the same protection as the
3038 template it specializes. */
3039 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3040 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3041
3042 /* 7.1.1-1 [dcl.stc]
3043
3044 A storage-class-specifier shall not be specified in an
3045 explicit specialization...
3046
3047 The parser rejects these, so unless action is taken here,
3048 explicit function specializations will always appear with
3049 global linkage.
3050
3051 The action recommended by the C++ CWG in response to C++
3052 defect report 605 is to make the storage class and linkage
3053 of the explicit specialization match the templated function:
3054
3055 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3056 */
3057 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3058 {
3059 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3060 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3061
3062 /* A concept cannot be specialized. */
3063 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3064 {
3065 error ("explicit specialization of function concept %qD",
3066 gen_tmpl);
3067 return error_mark_node;
3068 }
3069
3070 /* This specialization has the same linkage and visibility as
3071 the function template it specializes. */
3072 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3073 if (! TREE_PUBLIC (decl))
3074 {
3075 DECL_INTERFACE_KNOWN (decl) = 1;
3076 DECL_NOT_REALLY_EXTERN (decl) = 1;
3077 }
3078 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3079 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3080 {
3081 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3082 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3083 }
3084 }
3085
3086 /* If DECL is a friend declaration, declared using an
3087 unqualified name, the namespace associated with DECL may
3088 have been set incorrectly. For example, in:
3089
3090 template <typename T> void f(T);
3091 namespace N {
3092 struct S { friend void f<int>(int); }
3093 }
3094
3095 we will have set the DECL_CONTEXT for the friend
3096 declaration to N, rather than to the global namespace. */
3097 if (DECL_NAMESPACE_SCOPE_P (decl))
3098 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3099
3100 if (is_friend && !have_def)
3101 /* This is not really a declaration of a specialization.
3102 It's just the name of an instantiation. But, it's not
3103 a request for an instantiation, either. */
3104 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3105 else if (TREE_CODE (decl) == FUNCTION_DECL)
3106 /* A specialization is not necessarily COMDAT. */
3107 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3108 && DECL_DECLARED_INLINE_P (decl));
3109 else if (VAR_P (decl))
3110 DECL_COMDAT (decl) = false;
3111
3112 /* If this is a full specialization, register it so that we can find
3113 it again. Partial specializations will be registered in
3114 process_partial_specialization. */
3115 if (!processing_template_decl)
3116 decl = register_specialization (decl, gen_tmpl, targs,
3117 is_friend, 0);
3118
3119 /* A 'structor should already have clones. */
3120 gcc_assert (decl == error_mark_node
3121 || variable_template_p (tmpl)
3122 || !(DECL_CONSTRUCTOR_P (decl)
3123 || DECL_DESTRUCTOR_P (decl))
3124 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3125 }
3126 }
3127
3128 return decl;
3129 }
3130
3131 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3132 parameters. These are represented in the same format used for
3133 DECL_TEMPLATE_PARMS. */
3134
3135 int
3136 comp_template_parms (const_tree parms1, const_tree parms2)
3137 {
3138 const_tree p1;
3139 const_tree p2;
3140
3141 if (parms1 == parms2)
3142 return 1;
3143
3144 for (p1 = parms1, p2 = parms2;
3145 p1 != NULL_TREE && p2 != NULL_TREE;
3146 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3147 {
3148 tree t1 = TREE_VALUE (p1);
3149 tree t2 = TREE_VALUE (p2);
3150 int i;
3151
3152 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3153 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3154
3155 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3156 return 0;
3157
3158 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3159 {
3160 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3161 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3162
3163 /* If either of the template parameters are invalid, assume
3164 they match for the sake of error recovery. */
3165 if (error_operand_p (parm1) || error_operand_p (parm2))
3166 return 1;
3167
3168 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3169 return 0;
3170
3171 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3172 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3173 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3174 continue;
3175 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3176 return 0;
3177 }
3178 }
3179
3180 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3181 /* One set of parameters has more parameters lists than the
3182 other. */
3183 return 0;
3184
3185 return 1;
3186 }
3187
3188 /* Determine whether PARM is a parameter pack. */
3189
3190 bool
3191 template_parameter_pack_p (const_tree parm)
3192 {
3193 /* Determine if we have a non-type template parameter pack. */
3194 if (TREE_CODE (parm) == PARM_DECL)
3195 return (DECL_TEMPLATE_PARM_P (parm)
3196 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3197 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3198 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3199
3200 /* If this is a list of template parameters, we could get a
3201 TYPE_DECL or a TEMPLATE_DECL. */
3202 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3203 parm = TREE_TYPE (parm);
3204
3205 /* Otherwise it must be a type template parameter. */
3206 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3207 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3208 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3209 }
3210
3211 /* Determine if T is a function parameter pack. */
3212
3213 bool
3214 function_parameter_pack_p (const_tree t)
3215 {
3216 if (t && TREE_CODE (t) == PARM_DECL)
3217 return DECL_PACK_P (t);
3218 return false;
3219 }
3220
3221 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3222 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3223
3224 tree
3225 get_function_template_decl (const_tree primary_func_tmpl_inst)
3226 {
3227 if (! primary_func_tmpl_inst
3228 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3229 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3230 return NULL;
3231
3232 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3233 }
3234
3235 /* Return true iff the function parameter PARAM_DECL was expanded
3236 from the function parameter pack PACK. */
3237
3238 bool
3239 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3240 {
3241 if (DECL_ARTIFICIAL (param_decl)
3242 || !function_parameter_pack_p (pack))
3243 return false;
3244
3245 /* The parameter pack and its pack arguments have the same
3246 DECL_PARM_INDEX. */
3247 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3248 }
3249
3250 /* Determine whether ARGS describes a variadic template args list,
3251 i.e., one that is terminated by a template argument pack. */
3252
3253 static bool
3254 template_args_variadic_p (tree args)
3255 {
3256 int nargs;
3257 tree last_parm;
3258
3259 if (args == NULL_TREE)
3260 return false;
3261
3262 args = INNERMOST_TEMPLATE_ARGS (args);
3263 nargs = TREE_VEC_LENGTH (args);
3264
3265 if (nargs == 0)
3266 return false;
3267
3268 last_parm = TREE_VEC_ELT (args, nargs - 1);
3269
3270 return ARGUMENT_PACK_P (last_parm);
3271 }
3272
3273 /* Generate a new name for the parameter pack name NAME (an
3274 IDENTIFIER_NODE) that incorporates its */
3275
3276 static tree
3277 make_ith_pack_parameter_name (tree name, int i)
3278 {
3279 /* Munge the name to include the parameter index. */
3280 #define NUMBUF_LEN 128
3281 char numbuf[NUMBUF_LEN];
3282 char* newname;
3283 int newname_len;
3284
3285 if (name == NULL_TREE)
3286 return name;
3287 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3288 newname_len = IDENTIFIER_LENGTH (name)
3289 + strlen (numbuf) + 2;
3290 newname = (char*)alloca (newname_len);
3291 snprintf (newname, newname_len,
3292 "%s#%i", IDENTIFIER_POINTER (name), i);
3293 return get_identifier (newname);
3294 }
3295
3296 /* Return true if T is a primary function, class or alias template
3297 specialization, not including the template pattern. */
3298
3299 bool
3300 primary_template_specialization_p (const_tree t)
3301 {
3302 if (!t)
3303 return false;
3304
3305 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3306 return (DECL_LANG_SPECIFIC (t)
3307 && DECL_USE_TEMPLATE (t)
3308 && DECL_TEMPLATE_INFO (t)
3309 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3310 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3311 return (CLASSTYPE_TEMPLATE_INFO (t)
3312 && CLASSTYPE_USE_TEMPLATE (t)
3313 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3314 else if (alias_template_specialization_p (t))
3315 return true;
3316 return false;
3317 }
3318
3319 /* Return true if PARM is a template template parameter. */
3320
3321 bool
3322 template_template_parameter_p (const_tree parm)
3323 {
3324 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3325 }
3326
3327 /* Return true iff PARM is a DECL representing a type template
3328 parameter. */
3329
3330 bool
3331 template_type_parameter_p (const_tree parm)
3332 {
3333 return (parm
3334 && (TREE_CODE (parm) == TYPE_DECL
3335 || TREE_CODE (parm) == TEMPLATE_DECL)
3336 && DECL_TEMPLATE_PARM_P (parm));
3337 }
3338
3339 /* Return the template parameters of T if T is a
3340 primary template instantiation, NULL otherwise. */
3341
3342 tree
3343 get_primary_template_innermost_parameters (const_tree t)
3344 {
3345 tree parms = NULL, template_info = NULL;
3346
3347 if ((template_info = get_template_info (t))
3348 && primary_template_specialization_p (t))
3349 parms = INNERMOST_TEMPLATE_PARMS
3350 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3351
3352 return parms;
3353 }
3354
3355 /* Return the template parameters of the LEVELth level from the full list
3356 of template parameters PARMS. */
3357
3358 tree
3359 get_template_parms_at_level (tree parms, int level)
3360 {
3361 tree p;
3362 if (!parms
3363 || TREE_CODE (parms) != TREE_LIST
3364 || level > TMPL_PARMS_DEPTH (parms))
3365 return NULL_TREE;
3366
3367 for (p = parms; p; p = TREE_CHAIN (p))
3368 if (TMPL_PARMS_DEPTH (p) == level)
3369 return p;
3370
3371 return NULL_TREE;
3372 }
3373
3374 /* Returns the template arguments of T if T is a template instantiation,
3375 NULL otherwise. */
3376
3377 tree
3378 get_template_innermost_arguments (const_tree t)
3379 {
3380 tree args = NULL, template_info = NULL;
3381
3382 if ((template_info = get_template_info (t))
3383 && TI_ARGS (template_info))
3384 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3385
3386 return args;
3387 }
3388
3389 /* Return the argument pack elements of T if T is a template argument pack,
3390 NULL otherwise. */
3391
3392 tree
3393 get_template_argument_pack_elems (const_tree t)
3394 {
3395 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3396 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3397 return NULL;
3398
3399 return ARGUMENT_PACK_ARGS (t);
3400 }
3401
3402 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3403 ARGUMENT_PACK_SELECT represents. */
3404
3405 static tree
3406 argument_pack_select_arg (tree t)
3407 {
3408 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3409 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3410
3411 /* If the selected argument is an expansion E, that most likely means we were
3412 called from gen_elem_of_pack_expansion_instantiation during the
3413 substituting of an argument pack (of which the Ith element is a pack
3414 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3415 In this case, the Ith element resulting from this substituting is going to
3416 be a pack expansion, which pattern is the pattern of E. Let's return the
3417 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3418 resulting pack expansion from it. */
3419 if (PACK_EXPANSION_P (arg))
3420 {
3421 /* Make sure we aren't throwing away arg info. */
3422 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3423 arg = PACK_EXPANSION_PATTERN (arg);
3424 }
3425
3426 return arg;
3427 }
3428
3429
3430 /* True iff FN is a function representing a built-in variadic parameter
3431 pack. */
3432
3433 bool
3434 builtin_pack_fn_p (tree fn)
3435 {
3436 if (!fn
3437 || TREE_CODE (fn) != FUNCTION_DECL
3438 || !DECL_IS_BUILTIN (fn))
3439 return false;
3440
3441 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3442 return true;
3443
3444 return false;
3445 }
3446
3447 /* True iff CALL is a call to a function representing a built-in variadic
3448 parameter pack. */
3449
3450 static bool
3451 builtin_pack_call_p (tree call)
3452 {
3453 if (TREE_CODE (call) != CALL_EXPR)
3454 return false;
3455 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3456 }
3457
3458 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3459
3460 static tree
3461 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3462 tree in_decl)
3463 {
3464 tree ohi = CALL_EXPR_ARG (call, 0);
3465 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3466 false/*fn*/, true/*int_cst*/);
3467
3468 if (value_dependent_expression_p (hi))
3469 {
3470 if (hi != ohi)
3471 {
3472 call = copy_node (call);
3473 CALL_EXPR_ARG (call, 0) = hi;
3474 }
3475 tree ex = make_pack_expansion (call, complain);
3476 tree vec = make_tree_vec (1);
3477 TREE_VEC_ELT (vec, 0) = ex;
3478 return vec;
3479 }
3480 else
3481 {
3482 hi = cxx_constant_value (hi);
3483 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3484
3485 /* Calculate the largest value of len that won't make the size of the vec
3486 overflow an int. The compiler will exceed resource limits long before
3487 this, but it seems a decent place to diagnose. */
3488 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3489
3490 if (len < 0 || len > max)
3491 {
3492 if ((complain & tf_error)
3493 && hi != error_mark_node)
3494 error ("argument to __integer_pack must be between 0 and %d", max);
3495 return error_mark_node;
3496 }
3497
3498 tree vec = make_tree_vec (len);
3499
3500 for (int i = 0; i < len; ++i)
3501 TREE_VEC_ELT (vec, i) = size_int (i);
3502
3503 return vec;
3504 }
3505 }
3506
3507 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3508 CALL. */
3509
3510 static tree
3511 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3512 tree in_decl)
3513 {
3514 if (!builtin_pack_call_p (call))
3515 return NULL_TREE;
3516
3517 tree fn = CALL_EXPR_FN (call);
3518
3519 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3520 return expand_integer_pack (call, args, complain, in_decl);
3521
3522 return NULL_TREE;
3523 }
3524
3525 /* Structure used to track the progress of find_parameter_packs_r. */
3526 struct find_parameter_pack_data
3527 {
3528 /* TREE_LIST that will contain all of the parameter packs found by
3529 the traversal. */
3530 tree* parameter_packs;
3531
3532 /* Set of AST nodes that have been visited by the traversal. */
3533 hash_set<tree> *visited;
3534
3535 /* True iff we're making a type pack expansion. */
3536 bool type_pack_expansion_p;
3537 };
3538
3539 /* Identifies all of the argument packs that occur in a template
3540 argument and appends them to the TREE_LIST inside DATA, which is a
3541 find_parameter_pack_data structure. This is a subroutine of
3542 make_pack_expansion and uses_parameter_packs. */
3543 static tree
3544 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3545 {
3546 tree t = *tp;
3547 struct find_parameter_pack_data* ppd =
3548 (struct find_parameter_pack_data*)data;
3549 bool parameter_pack_p = false;
3550
3551 /* Handle type aliases/typedefs. */
3552 if (TYPE_ALIAS_P (t))
3553 {
3554 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3555 cp_walk_tree (&TI_ARGS (tinfo),
3556 &find_parameter_packs_r,
3557 ppd, ppd->visited);
3558 *walk_subtrees = 0;
3559 return NULL_TREE;
3560 }
3561
3562 /* Identify whether this is a parameter pack or not. */
3563 switch (TREE_CODE (t))
3564 {
3565 case TEMPLATE_PARM_INDEX:
3566 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3567 parameter_pack_p = true;
3568 break;
3569
3570 case TEMPLATE_TYPE_PARM:
3571 t = TYPE_MAIN_VARIANT (t);
3572 /* FALLTHRU */
3573 case TEMPLATE_TEMPLATE_PARM:
3574 /* If the placeholder appears in the decl-specifier-seq of a function
3575 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3576 is a pack expansion, the invented template parameter is a template
3577 parameter pack. */
3578 if (ppd->type_pack_expansion_p && is_auto (t))
3579 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3580 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3581 parameter_pack_p = true;
3582 break;
3583
3584 case FIELD_DECL:
3585 case PARM_DECL:
3586 if (DECL_PACK_P (t))
3587 {
3588 /* We don't want to walk into the type of a PARM_DECL,
3589 because we don't want to see the type parameter pack. */
3590 *walk_subtrees = 0;
3591 parameter_pack_p = true;
3592 }
3593 break;
3594
3595 case VAR_DECL:
3596 if (DECL_PACK_P (t))
3597 {
3598 /* We don't want to walk into the type of a variadic capture proxy,
3599 because we don't want to see the type parameter pack. */
3600 *walk_subtrees = 0;
3601 parameter_pack_p = true;
3602 }
3603 else if (variable_template_specialization_p (t))
3604 {
3605 cp_walk_tree (&DECL_TI_ARGS (t),
3606 find_parameter_packs_r,
3607 ppd, ppd->visited);
3608 *walk_subtrees = 0;
3609 }
3610 break;
3611
3612 case CALL_EXPR:
3613 if (builtin_pack_call_p (t))
3614 parameter_pack_p = true;
3615 break;
3616
3617 case BASES:
3618 parameter_pack_p = true;
3619 break;
3620 default:
3621 /* Not a parameter pack. */
3622 break;
3623 }
3624
3625 if (parameter_pack_p)
3626 {
3627 /* Add this parameter pack to the list. */
3628 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3629 }
3630
3631 if (TYPE_P (t))
3632 cp_walk_tree (&TYPE_CONTEXT (t),
3633 &find_parameter_packs_r, ppd, ppd->visited);
3634
3635 /* This switch statement will return immediately if we don't find a
3636 parameter pack. */
3637 switch (TREE_CODE (t))
3638 {
3639 case TEMPLATE_PARM_INDEX:
3640 return NULL_TREE;
3641
3642 case BOUND_TEMPLATE_TEMPLATE_PARM:
3643 /* Check the template itself. */
3644 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3645 &find_parameter_packs_r, ppd, ppd->visited);
3646 /* Check the template arguments. */
3647 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3648 ppd->visited);
3649 *walk_subtrees = 0;
3650 return NULL_TREE;
3651
3652 case TEMPLATE_TYPE_PARM:
3653 case TEMPLATE_TEMPLATE_PARM:
3654 return NULL_TREE;
3655
3656 case PARM_DECL:
3657 return NULL_TREE;
3658
3659 case DECL_EXPR:
3660 /* Ignore the declaration of a capture proxy for a parameter pack. */
3661 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3662 *walk_subtrees = 0;
3663 return NULL_TREE;
3664
3665 case RECORD_TYPE:
3666 if (TYPE_PTRMEMFUNC_P (t))
3667 return NULL_TREE;
3668 /* Fall through. */
3669
3670 case UNION_TYPE:
3671 case ENUMERAL_TYPE:
3672 if (TYPE_TEMPLATE_INFO (t))
3673 cp_walk_tree (&TYPE_TI_ARGS (t),
3674 &find_parameter_packs_r, ppd, ppd->visited);
3675
3676 *walk_subtrees = 0;
3677 return NULL_TREE;
3678
3679 case TEMPLATE_DECL:
3680 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3681 return NULL_TREE;
3682 gcc_fallthrough();
3683
3684 case CONSTRUCTOR:
3685 cp_walk_tree (&TREE_TYPE (t),
3686 &find_parameter_packs_r, ppd, ppd->visited);
3687 return NULL_TREE;
3688
3689 case TYPENAME_TYPE:
3690 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3691 ppd, ppd->visited);
3692 *walk_subtrees = 0;
3693 return NULL_TREE;
3694
3695 case TYPE_PACK_EXPANSION:
3696 case EXPR_PACK_EXPANSION:
3697 *walk_subtrees = 0;
3698 return NULL_TREE;
3699
3700 case INTEGER_TYPE:
3701 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3702 ppd, ppd->visited);
3703 *walk_subtrees = 0;
3704 return NULL_TREE;
3705
3706 case IDENTIFIER_NODE:
3707 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3708 ppd->visited);
3709 *walk_subtrees = 0;
3710 return NULL_TREE;
3711
3712 case LAMBDA_EXPR:
3713 {
3714 /* Look at explicit captures. */
3715 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3716 cap; cap = TREE_CHAIN (cap))
3717 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3718 ppd->visited);
3719 /* Since we defer implicit capture, look in the body as well. */
3720 tree fn = lambda_function (t);
3721 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3722 ppd->visited);
3723 *walk_subtrees = 0;
3724 return NULL_TREE;
3725 }
3726
3727 case DECLTYPE_TYPE:
3728 {
3729 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3730 type_pack_expansion_p to false so that any placeholders
3731 within the expression don't get marked as parameter packs. */
3732 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3733 ppd->type_pack_expansion_p = false;
3734 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3735 ppd, ppd->visited);
3736 ppd->type_pack_expansion_p = type_pack_expansion_p;
3737 *walk_subtrees = 0;
3738 return NULL_TREE;
3739 }
3740
3741 default:
3742 return NULL_TREE;
3743 }
3744
3745 return NULL_TREE;
3746 }
3747
3748 /* Determines if the expression or type T uses any parameter packs. */
3749 bool
3750 uses_parameter_packs (tree t)
3751 {
3752 tree parameter_packs = NULL_TREE;
3753 struct find_parameter_pack_data ppd;
3754 ppd.parameter_packs = &parameter_packs;
3755 ppd.visited = new hash_set<tree>;
3756 ppd.type_pack_expansion_p = false;
3757 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3758 delete ppd.visited;
3759 return parameter_packs != NULL_TREE;
3760 }
3761
3762 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3763 representation a base-class initializer into a parameter pack
3764 expansion. If all goes well, the resulting node will be an
3765 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3766 respectively. */
3767 tree
3768 make_pack_expansion (tree arg, tsubst_flags_t complain)
3769 {
3770 tree result;
3771 tree parameter_packs = NULL_TREE;
3772 bool for_types = false;
3773 struct find_parameter_pack_data ppd;
3774
3775 if (!arg || arg == error_mark_node)
3776 return arg;
3777
3778 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3779 {
3780 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3781 class initializer. In this case, the TREE_PURPOSE will be a
3782 _TYPE node (representing the base class expansion we're
3783 initializing) and the TREE_VALUE will be a TREE_LIST
3784 containing the initialization arguments.
3785
3786 The resulting expansion looks somewhat different from most
3787 expansions. Rather than returning just one _EXPANSION, we
3788 return a TREE_LIST whose TREE_PURPOSE is a
3789 TYPE_PACK_EXPANSION containing the bases that will be
3790 initialized. The TREE_VALUE will be identical to the
3791 original TREE_VALUE, which is a list of arguments that will
3792 be passed to each base. We do not introduce any new pack
3793 expansion nodes into the TREE_VALUE (although it is possible
3794 that some already exist), because the TREE_PURPOSE and
3795 TREE_VALUE all need to be expanded together with the same
3796 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3797 resulting TREE_PURPOSE will mention the parameter packs in
3798 both the bases and the arguments to the bases. */
3799 tree purpose;
3800 tree value;
3801 tree parameter_packs = NULL_TREE;
3802
3803 /* Determine which parameter packs will be used by the base
3804 class expansion. */
3805 ppd.visited = new hash_set<tree>;
3806 ppd.parameter_packs = &parameter_packs;
3807 ppd.type_pack_expansion_p = true;
3808 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3809 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3810 &ppd, ppd.visited);
3811
3812 if (parameter_packs == NULL_TREE)
3813 {
3814 if (complain & tf_error)
3815 error ("base initializer expansion %qT contains no parameter packs",
3816 arg);
3817 delete ppd.visited;
3818 return error_mark_node;
3819 }
3820
3821 if (TREE_VALUE (arg) != void_type_node)
3822 {
3823 /* Collect the sets of parameter packs used in each of the
3824 initialization arguments. */
3825 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3826 {
3827 /* Determine which parameter packs will be expanded in this
3828 argument. */
3829 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3830 &ppd, ppd.visited);
3831 }
3832 }
3833
3834 delete ppd.visited;
3835
3836 /* Create the pack expansion type for the base type. */
3837 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3838 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3839 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3840 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3841
3842 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3843 they will rarely be compared to anything. */
3844 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3845
3846 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3847 }
3848
3849 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3850 for_types = true;
3851
3852 /* Build the PACK_EXPANSION_* node. */
3853 result = for_types
3854 ? cxx_make_type (TYPE_PACK_EXPANSION)
3855 : make_node (EXPR_PACK_EXPANSION);
3856 SET_PACK_EXPANSION_PATTERN (result, arg);
3857 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3858 {
3859 /* Propagate type and const-expression information. */
3860 TREE_TYPE (result) = TREE_TYPE (arg);
3861 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3862 /* Mark this read now, since the expansion might be length 0. */
3863 mark_exp_read (arg);
3864 }
3865 else
3866 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3867 they will rarely be compared to anything. */
3868 SET_TYPE_STRUCTURAL_EQUALITY (result);
3869
3870 /* Determine which parameter packs will be expanded. */
3871 ppd.parameter_packs = &parameter_packs;
3872 ppd.visited = new hash_set<tree>;
3873 ppd.type_pack_expansion_p = TYPE_P (arg);
3874 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3875 delete ppd.visited;
3876
3877 /* Make sure we found some parameter packs. */
3878 if (parameter_packs == NULL_TREE)
3879 {
3880 if (complain & tf_error)
3881 {
3882 if (TYPE_P (arg))
3883 error ("expansion pattern %qT contains no argument packs", arg);
3884 else
3885 error ("expansion pattern %qE contains no argument packs", arg);
3886 }
3887 return error_mark_node;
3888 }
3889 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3890
3891 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3892
3893 return result;
3894 }
3895
3896 /* Checks T for any "bare" parameter packs, which have not yet been
3897 expanded, and issues an error if any are found. This operation can
3898 only be done on full expressions or types (e.g., an expression
3899 statement, "if" condition, etc.), because we could have expressions like:
3900
3901 foo(f(g(h(args)))...)
3902
3903 where "args" is a parameter pack. check_for_bare_parameter_packs
3904 should not be called for the subexpressions args, h(args),
3905 g(h(args)), or f(g(h(args))), because we would produce erroneous
3906 error messages.
3907
3908 Returns TRUE and emits an error if there were bare parameter packs,
3909 returns FALSE otherwise. */
3910 bool
3911 check_for_bare_parameter_packs (tree t)
3912 {
3913 tree parameter_packs = NULL_TREE;
3914 struct find_parameter_pack_data ppd;
3915
3916 if (!processing_template_decl || !t || t == error_mark_node)
3917 return false;
3918
3919 /* A lambda might use a parameter pack from the containing context. */
3920 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
3921 return false;
3922
3923 if (TREE_CODE (t) == TYPE_DECL)
3924 t = TREE_TYPE (t);
3925
3926 ppd.parameter_packs = &parameter_packs;
3927 ppd.visited = new hash_set<tree>;
3928 ppd.type_pack_expansion_p = false;
3929 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3930 delete ppd.visited;
3931
3932 if (parameter_packs)
3933 {
3934 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3935 error_at (loc, "parameter packs not expanded with %<...%>:");
3936 while (parameter_packs)
3937 {
3938 tree pack = TREE_VALUE (parameter_packs);
3939 tree name = NULL_TREE;
3940
3941 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3942 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3943 name = TYPE_NAME (pack);
3944 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3945 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3946 else if (TREE_CODE (pack) == CALL_EXPR)
3947 name = DECL_NAME (CALL_EXPR_FN (pack));
3948 else
3949 name = DECL_NAME (pack);
3950
3951 if (name)
3952 inform (loc, " %qD", name);
3953 else
3954 inform (loc, " <anonymous>");
3955
3956 parameter_packs = TREE_CHAIN (parameter_packs);
3957 }
3958
3959 return true;
3960 }
3961
3962 return false;
3963 }
3964
3965 /* Expand any parameter packs that occur in the template arguments in
3966 ARGS. */
3967 tree
3968 expand_template_argument_pack (tree args)
3969 {
3970 if (args == error_mark_node)
3971 return error_mark_node;
3972
3973 tree result_args = NULL_TREE;
3974 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3975 int num_result_args = -1;
3976 int non_default_args_count = -1;
3977
3978 /* First, determine if we need to expand anything, and the number of
3979 slots we'll need. */
3980 for (in_arg = 0; in_arg < nargs; ++in_arg)
3981 {
3982 tree arg = TREE_VEC_ELT (args, in_arg);
3983 if (arg == NULL_TREE)
3984 return args;
3985 if (ARGUMENT_PACK_P (arg))
3986 {
3987 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3988 if (num_result_args < 0)
3989 num_result_args = in_arg + num_packed;
3990 else
3991 num_result_args += num_packed;
3992 }
3993 else
3994 {
3995 if (num_result_args >= 0)
3996 num_result_args++;
3997 }
3998 }
3999
4000 /* If no expansion is necessary, we're done. */
4001 if (num_result_args < 0)
4002 return args;
4003
4004 /* Expand arguments. */
4005 result_args = make_tree_vec (num_result_args);
4006 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4007 non_default_args_count =
4008 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4009 for (in_arg = 0; in_arg < nargs; ++in_arg)
4010 {
4011 tree arg = TREE_VEC_ELT (args, in_arg);
4012 if (ARGUMENT_PACK_P (arg))
4013 {
4014 tree packed = ARGUMENT_PACK_ARGS (arg);
4015 int i, num_packed = TREE_VEC_LENGTH (packed);
4016 for (i = 0; i < num_packed; ++i, ++out_arg)
4017 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4018 if (non_default_args_count > 0)
4019 non_default_args_count += num_packed - 1;
4020 }
4021 else
4022 {
4023 TREE_VEC_ELT (result_args, out_arg) = arg;
4024 ++out_arg;
4025 }
4026 }
4027 if (non_default_args_count >= 0)
4028 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4029 return result_args;
4030 }
4031
4032 /* Checks if DECL shadows a template parameter.
4033
4034 [temp.local]: A template-parameter shall not be redeclared within its
4035 scope (including nested scopes).
4036
4037 Emits an error and returns TRUE if the DECL shadows a parameter,
4038 returns FALSE otherwise. */
4039
4040 bool
4041 check_template_shadow (tree decl)
4042 {
4043 tree olddecl;
4044
4045 /* If we're not in a template, we can't possibly shadow a template
4046 parameter. */
4047 if (!current_template_parms)
4048 return true;
4049
4050 /* Figure out what we're shadowing. */
4051 decl = OVL_FIRST (decl);
4052 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4053
4054 /* If there's no previous binding for this name, we're not shadowing
4055 anything, let alone a template parameter. */
4056 if (!olddecl)
4057 return true;
4058
4059 /* If we're not shadowing a template parameter, we're done. Note
4060 that OLDDECL might be an OVERLOAD (or perhaps even an
4061 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4062 node. */
4063 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4064 return true;
4065
4066 /* We check for decl != olddecl to avoid bogus errors for using a
4067 name inside a class. We check TPFI to avoid duplicate errors for
4068 inline member templates. */
4069 if (decl == olddecl
4070 || (DECL_TEMPLATE_PARM_P (decl)
4071 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4072 return true;
4073
4074 /* Don't complain about the injected class name, as we've already
4075 complained about the class itself. */
4076 if (DECL_SELF_REFERENCE_P (decl))
4077 return false;
4078
4079 if (DECL_TEMPLATE_PARM_P (decl))
4080 error ("declaration of template parameter %q+D shadows "
4081 "template parameter", decl);
4082 else
4083 error ("declaration of %q+#D shadows template parameter", decl);
4084 inform (DECL_SOURCE_LOCATION (olddecl),
4085 "template parameter %qD declared here", olddecl);
4086 return false;
4087 }
4088
4089 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4090 ORIG_LEVEL, DECL, and TYPE. */
4091
4092 static tree
4093 build_template_parm_index (int index,
4094 int level,
4095 int orig_level,
4096 tree decl,
4097 tree type)
4098 {
4099 tree t = make_node (TEMPLATE_PARM_INDEX);
4100 TEMPLATE_PARM_IDX (t) = index;
4101 TEMPLATE_PARM_LEVEL (t) = level;
4102 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4103 TEMPLATE_PARM_DECL (t) = decl;
4104 TREE_TYPE (t) = type;
4105 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4106 TREE_READONLY (t) = TREE_READONLY (decl);
4107
4108 return t;
4109 }
4110
4111 /* Find the canonical type parameter for the given template type
4112 parameter. Returns the canonical type parameter, which may be TYPE
4113 if no such parameter existed. */
4114
4115 static tree
4116 canonical_type_parameter (tree type)
4117 {
4118 tree list;
4119 int idx = TEMPLATE_TYPE_IDX (type);
4120 if (!canonical_template_parms)
4121 vec_alloc (canonical_template_parms, idx + 1);
4122
4123 if (canonical_template_parms->length () <= (unsigned) idx)
4124 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4125
4126 list = (*canonical_template_parms)[idx];
4127 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4128 list = TREE_CHAIN (list);
4129
4130 if (list)
4131 return TREE_VALUE (list);
4132 else
4133 {
4134 (*canonical_template_parms)[idx]
4135 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4136 return type;
4137 }
4138 }
4139
4140 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4141 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4142 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4143 new one is created. */
4144
4145 static tree
4146 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4147 tsubst_flags_t complain)
4148 {
4149 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4150 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4151 != TEMPLATE_PARM_LEVEL (index) - levels)
4152 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4153 {
4154 tree orig_decl = TEMPLATE_PARM_DECL (index);
4155 tree decl, t;
4156
4157 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4158 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4159 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4160 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4161 DECL_ARTIFICIAL (decl) = 1;
4162 SET_DECL_TEMPLATE_PARM_P (decl);
4163
4164 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4165 TEMPLATE_PARM_LEVEL (index) - levels,
4166 TEMPLATE_PARM_ORIG_LEVEL (index),
4167 decl, type);
4168 TEMPLATE_PARM_DESCENDANTS (index) = t;
4169 TEMPLATE_PARM_PARAMETER_PACK (t)
4170 = TEMPLATE_PARM_PARAMETER_PACK (index);
4171
4172 /* Template template parameters need this. */
4173 if (TREE_CODE (decl) == TEMPLATE_DECL)
4174 {
4175 DECL_TEMPLATE_RESULT (decl)
4176 = build_decl (DECL_SOURCE_LOCATION (decl),
4177 TYPE_DECL, DECL_NAME (decl), type);
4178 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4179 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4180 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4181 }
4182 }
4183
4184 return TEMPLATE_PARM_DESCENDANTS (index);
4185 }
4186
4187 /* Process information from new template parameter PARM and append it
4188 to the LIST being built. This new parameter is a non-type
4189 parameter iff IS_NON_TYPE is true. This new parameter is a
4190 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4191 is in PARM_LOC. */
4192
4193 tree
4194 process_template_parm (tree list, location_t parm_loc, tree parm,
4195 bool is_non_type, bool is_parameter_pack)
4196 {
4197 tree decl = 0;
4198 int idx = 0;
4199
4200 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4201 tree defval = TREE_PURPOSE (parm);
4202 tree constr = TREE_TYPE (parm);
4203
4204 if (list)
4205 {
4206 tree p = tree_last (list);
4207
4208 if (p && TREE_VALUE (p) != error_mark_node)
4209 {
4210 p = TREE_VALUE (p);
4211 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4212 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4213 else
4214 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4215 }
4216
4217 ++idx;
4218 }
4219
4220 if (is_non_type)
4221 {
4222 parm = TREE_VALUE (parm);
4223
4224 SET_DECL_TEMPLATE_PARM_P (parm);
4225
4226 if (TREE_TYPE (parm) != error_mark_node)
4227 {
4228 /* [temp.param]
4229
4230 The top-level cv-qualifiers on the template-parameter are
4231 ignored when determining its type. */
4232 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4233 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4234 TREE_TYPE (parm) = error_mark_node;
4235 else if (uses_parameter_packs (TREE_TYPE (parm))
4236 && !is_parameter_pack
4237 /* If we're in a nested template parameter list, the template
4238 template parameter could be a parameter pack. */
4239 && processing_template_parmlist == 1)
4240 {
4241 /* This template parameter is not a parameter pack, but it
4242 should be. Complain about "bare" parameter packs. */
4243 check_for_bare_parameter_packs (TREE_TYPE (parm));
4244
4245 /* Recover by calling this a parameter pack. */
4246 is_parameter_pack = true;
4247 }
4248 }
4249
4250 /* A template parameter is not modifiable. */
4251 TREE_CONSTANT (parm) = 1;
4252 TREE_READONLY (parm) = 1;
4253 decl = build_decl (parm_loc,
4254 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4255 TREE_CONSTANT (decl) = 1;
4256 TREE_READONLY (decl) = 1;
4257 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4258 = build_template_parm_index (idx, processing_template_decl,
4259 processing_template_decl,
4260 decl, TREE_TYPE (parm));
4261
4262 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4263 = is_parameter_pack;
4264 }
4265 else
4266 {
4267 tree t;
4268 parm = TREE_VALUE (TREE_VALUE (parm));
4269
4270 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4271 {
4272 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4273 /* This is for distinguishing between real templates and template
4274 template parameters */
4275 TREE_TYPE (parm) = t;
4276 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4277 decl = parm;
4278 }
4279 else
4280 {
4281 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4282 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4283 decl = build_decl (parm_loc,
4284 TYPE_DECL, parm, t);
4285 }
4286
4287 TYPE_NAME (t) = decl;
4288 TYPE_STUB_DECL (t) = decl;
4289 parm = decl;
4290 TEMPLATE_TYPE_PARM_INDEX (t)
4291 = build_template_parm_index (idx, processing_template_decl,
4292 processing_template_decl,
4293 decl, TREE_TYPE (parm));
4294 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4295 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4296 }
4297 DECL_ARTIFICIAL (decl) = 1;
4298 SET_DECL_TEMPLATE_PARM_P (decl);
4299
4300 /* Build requirements for the type/template parameter.
4301 This must be done after SET_DECL_TEMPLATE_PARM_P or
4302 process_template_parm could fail. */
4303 tree reqs = finish_shorthand_constraint (parm, constr);
4304
4305 pushdecl (decl);
4306
4307 /* Build the parameter node linking the parameter declaration,
4308 its default argument (if any), and its constraints (if any). */
4309 parm = build_tree_list (defval, parm);
4310 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4311
4312 return chainon (list, parm);
4313 }
4314
4315 /* The end of a template parameter list has been reached. Process the
4316 tree list into a parameter vector, converting each parameter into a more
4317 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4318 as PARM_DECLs. */
4319
4320 tree
4321 end_template_parm_list (tree parms)
4322 {
4323 int nparms;
4324 tree parm, next;
4325 tree saved_parmlist = make_tree_vec (list_length (parms));
4326
4327 /* Pop the dummy parameter level and add the real one. */
4328 current_template_parms = TREE_CHAIN (current_template_parms);
4329
4330 current_template_parms
4331 = tree_cons (size_int (processing_template_decl),
4332 saved_parmlist, current_template_parms);
4333
4334 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4335 {
4336 next = TREE_CHAIN (parm);
4337 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4338 TREE_CHAIN (parm) = NULL_TREE;
4339 }
4340
4341 --processing_template_parmlist;
4342
4343 return saved_parmlist;
4344 }
4345
4346 // Explicitly indicate the end of the template parameter list. We assume
4347 // that the current template parameters have been constructed and/or
4348 // managed explicitly, as when creating new template template parameters
4349 // from a shorthand constraint.
4350 void
4351 end_template_parm_list ()
4352 {
4353 --processing_template_parmlist;
4354 }
4355
4356 /* end_template_decl is called after a template declaration is seen. */
4357
4358 void
4359 end_template_decl (void)
4360 {
4361 reset_specialization ();
4362
4363 if (! processing_template_decl)
4364 return;
4365
4366 /* This matches the pushlevel in begin_template_parm_list. */
4367 finish_scope ();
4368
4369 --processing_template_decl;
4370 current_template_parms = TREE_CHAIN (current_template_parms);
4371 }
4372
4373 /* Takes a TREE_LIST representing a template parameter and convert it
4374 into an argument suitable to be passed to the type substitution
4375 functions. Note that If the TREE_LIST contains an error_mark
4376 node, the returned argument is error_mark_node. */
4377
4378 tree
4379 template_parm_to_arg (tree t)
4380 {
4381
4382 if (t == NULL_TREE
4383 || TREE_CODE (t) != TREE_LIST)
4384 return t;
4385
4386 if (error_operand_p (TREE_VALUE (t)))
4387 return error_mark_node;
4388
4389 t = TREE_VALUE (t);
4390
4391 if (TREE_CODE (t) == TYPE_DECL
4392 || TREE_CODE (t) == TEMPLATE_DECL)
4393 {
4394 t = TREE_TYPE (t);
4395
4396 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4397 {
4398 /* Turn this argument into a TYPE_ARGUMENT_PACK
4399 with a single element, which expands T. */
4400 tree vec = make_tree_vec (1);
4401 if (CHECKING_P)
4402 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4403
4404 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4405
4406 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4407 SET_ARGUMENT_PACK_ARGS (t, vec);
4408 }
4409 }
4410 else
4411 {
4412 t = DECL_INITIAL (t);
4413
4414 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4415 {
4416 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4417 with a single element, which expands T. */
4418 tree vec = make_tree_vec (1);
4419 if (CHECKING_P)
4420 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4421
4422 t = convert_from_reference (t);
4423 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4424
4425 t = make_node (NONTYPE_ARGUMENT_PACK);
4426 SET_ARGUMENT_PACK_ARGS (t, vec);
4427 }
4428 else
4429 t = convert_from_reference (t);
4430 }
4431 return t;
4432 }
4433
4434 /* Given a single level of template parameters (a TREE_VEC), return it
4435 as a set of template arguments. */
4436
4437 static tree
4438 template_parms_level_to_args (tree parms)
4439 {
4440 tree a = copy_node (parms);
4441 TREE_TYPE (a) = NULL_TREE;
4442 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4443 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4444
4445 if (CHECKING_P)
4446 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4447
4448 return a;
4449 }
4450
4451 /* Given a set of template parameters, return them as a set of template
4452 arguments. The template parameters are represented as a TREE_VEC, in
4453 the form documented in cp-tree.h for template arguments. */
4454
4455 static tree
4456 template_parms_to_args (tree parms)
4457 {
4458 tree header;
4459 tree args = NULL_TREE;
4460 int length = TMPL_PARMS_DEPTH (parms);
4461 int l = length;
4462
4463 /* If there is only one level of template parameters, we do not
4464 create a TREE_VEC of TREE_VECs. Instead, we return a single
4465 TREE_VEC containing the arguments. */
4466 if (length > 1)
4467 args = make_tree_vec (length);
4468
4469 for (header = parms; header; header = TREE_CHAIN (header))
4470 {
4471 tree a = template_parms_level_to_args (TREE_VALUE (header));
4472
4473 if (length > 1)
4474 TREE_VEC_ELT (args, --l) = a;
4475 else
4476 args = a;
4477 }
4478
4479 return args;
4480 }
4481
4482 /* Within the declaration of a template, return the currently active
4483 template parameters as an argument TREE_VEC. */
4484
4485 static tree
4486 current_template_args (void)
4487 {
4488 return template_parms_to_args (current_template_parms);
4489 }
4490
4491 /* Update the declared TYPE by doing any lookups which were thought to be
4492 dependent, but are not now that we know the SCOPE of the declarator. */
4493
4494 tree
4495 maybe_update_decl_type (tree orig_type, tree scope)
4496 {
4497 tree type = orig_type;
4498
4499 if (type == NULL_TREE)
4500 return type;
4501
4502 if (TREE_CODE (orig_type) == TYPE_DECL)
4503 type = TREE_TYPE (type);
4504
4505 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4506 && dependent_type_p (type)
4507 /* Don't bother building up the args in this case. */
4508 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4509 {
4510 /* tsubst in the args corresponding to the template parameters,
4511 including auto if present. Most things will be unchanged, but
4512 make_typename_type and tsubst_qualified_id will resolve
4513 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4514 tree args = current_template_args ();
4515 tree auto_node = type_uses_auto (type);
4516 tree pushed;
4517 if (auto_node)
4518 {
4519 tree auto_vec = make_tree_vec (1);
4520 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4521 args = add_to_template_args (args, auto_vec);
4522 }
4523 pushed = push_scope (scope);
4524 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4525 if (pushed)
4526 pop_scope (scope);
4527 }
4528
4529 if (type == error_mark_node)
4530 return orig_type;
4531
4532 if (TREE_CODE (orig_type) == TYPE_DECL)
4533 {
4534 if (same_type_p (type, TREE_TYPE (orig_type)))
4535 type = orig_type;
4536 else
4537 type = TYPE_NAME (type);
4538 }
4539 return type;
4540 }
4541
4542 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4543 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4544 the new template is a member template. */
4545
4546 tree
4547 build_template_decl (tree decl, tree parms, bool member_template_p)
4548 {
4549 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4550 DECL_TEMPLATE_PARMS (tmpl) = parms;
4551 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4552 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4553 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4554
4555 return tmpl;
4556 }
4557
4558 struct template_parm_data
4559 {
4560 /* The level of the template parameters we are currently
4561 processing. */
4562 int level;
4563
4564 /* The index of the specialization argument we are currently
4565 processing. */
4566 int current_arg;
4567
4568 /* An array whose size is the number of template parameters. The
4569 elements are nonzero if the parameter has been used in any one
4570 of the arguments processed so far. */
4571 int* parms;
4572
4573 /* An array whose size is the number of template arguments. The
4574 elements are nonzero if the argument makes use of template
4575 parameters of this level. */
4576 int* arg_uses_template_parms;
4577 };
4578
4579 /* Subroutine of push_template_decl used to see if each template
4580 parameter in a partial specialization is used in the explicit
4581 argument list. If T is of the LEVEL given in DATA (which is
4582 treated as a template_parm_data*), then DATA->PARMS is marked
4583 appropriately. */
4584
4585 static int
4586 mark_template_parm (tree t, void* data)
4587 {
4588 int level;
4589 int idx;
4590 struct template_parm_data* tpd = (struct template_parm_data*) data;
4591
4592 template_parm_level_and_index (t, &level, &idx);
4593
4594 if (level == tpd->level)
4595 {
4596 tpd->parms[idx] = 1;
4597 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4598 }
4599
4600 /* In C++17 the type of a non-type argument is a deduced context. */
4601 if (cxx_dialect >= cxx17
4602 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4603 for_each_template_parm (TREE_TYPE (t),
4604 &mark_template_parm,
4605 data,
4606 NULL,
4607 /*include_nondeduced_p=*/false);
4608
4609 /* Return zero so that for_each_template_parm will continue the
4610 traversal of the tree; we want to mark *every* template parm. */
4611 return 0;
4612 }
4613
4614 /* Process the partial specialization DECL. */
4615
4616 static tree
4617 process_partial_specialization (tree decl)
4618 {
4619 tree type = TREE_TYPE (decl);
4620 tree tinfo = get_template_info (decl);
4621 tree maintmpl = TI_TEMPLATE (tinfo);
4622 tree specargs = TI_ARGS (tinfo);
4623 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4624 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4625 tree inner_parms;
4626 tree inst;
4627 int nargs = TREE_VEC_LENGTH (inner_args);
4628 int ntparms;
4629 int i;
4630 bool did_error_intro = false;
4631 struct template_parm_data tpd;
4632 struct template_parm_data tpd2;
4633
4634 gcc_assert (current_template_parms);
4635
4636 /* A concept cannot be specialized. */
4637 if (flag_concepts && variable_concept_p (maintmpl))
4638 {
4639 error ("specialization of variable concept %q#D", maintmpl);
4640 return error_mark_node;
4641 }
4642
4643 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4644 ntparms = TREE_VEC_LENGTH (inner_parms);
4645
4646 /* We check that each of the template parameters given in the
4647 partial specialization is used in the argument list to the
4648 specialization. For example:
4649
4650 template <class T> struct S;
4651 template <class T> struct S<T*>;
4652
4653 The second declaration is OK because `T*' uses the template
4654 parameter T, whereas
4655
4656 template <class T> struct S<int>;
4657
4658 is no good. Even trickier is:
4659
4660 template <class T>
4661 struct S1
4662 {
4663 template <class U>
4664 struct S2;
4665 template <class U>
4666 struct S2<T>;
4667 };
4668
4669 The S2<T> declaration is actually invalid; it is a
4670 full-specialization. Of course,
4671
4672 template <class U>
4673 struct S2<T (*)(U)>;
4674
4675 or some such would have been OK. */
4676 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4677 tpd.parms = XALLOCAVEC (int, ntparms);
4678 memset (tpd.parms, 0, sizeof (int) * ntparms);
4679
4680 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4681 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4682 for (i = 0; i < nargs; ++i)
4683 {
4684 tpd.current_arg = i;
4685 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4686 &mark_template_parm,
4687 &tpd,
4688 NULL,
4689 /*include_nondeduced_p=*/false);
4690 }
4691 for (i = 0; i < ntparms; ++i)
4692 if (tpd.parms[i] == 0)
4693 {
4694 /* One of the template parms was not used in a deduced context in the
4695 specialization. */
4696 if (!did_error_intro)
4697 {
4698 error ("template parameters not deducible in "
4699 "partial specialization:");
4700 did_error_intro = true;
4701 }
4702
4703 inform (input_location, " %qD",
4704 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4705 }
4706
4707 if (did_error_intro)
4708 return error_mark_node;
4709
4710 /* [temp.class.spec]
4711
4712 The argument list of the specialization shall not be identical to
4713 the implicit argument list of the primary template. */
4714 tree main_args
4715 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4716 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4717 && (!flag_concepts
4718 || !strictly_subsumes (current_template_constraints (),
4719 get_constraints (maintmpl))))
4720 {
4721 if (!flag_concepts)
4722 error ("partial specialization %q+D does not specialize "
4723 "any template arguments", decl);
4724 else
4725 error ("partial specialization %q+D does not specialize any "
4726 "template arguments and is not more constrained than", decl);
4727 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4728 }
4729
4730 /* A partial specialization that replaces multiple parameters of the
4731 primary template with a pack expansion is less specialized for those
4732 parameters. */
4733 if (nargs < DECL_NTPARMS (maintmpl))
4734 {
4735 error ("partial specialization is not more specialized than the "
4736 "primary template because it replaces multiple parameters "
4737 "with a pack expansion");
4738 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4739 /* Avoid crash in process_partial_specialization. */
4740 return decl;
4741 }
4742
4743 /* If we aren't in a dependent class, we can actually try deduction. */
4744 else if (tpd.level == 1
4745 /* FIXME we should be able to handle a partial specialization of a
4746 partial instantiation, but currently we can't (c++/41727). */
4747 && TMPL_ARGS_DEPTH (specargs) == 1
4748 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4749 {
4750 if (permerror (input_location, "partial specialization %qD is not "
4751 "more specialized than", decl))
4752 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4753 maintmpl);
4754 }
4755
4756 /* [temp.class.spec]
4757
4758 A partially specialized non-type argument expression shall not
4759 involve template parameters of the partial specialization except
4760 when the argument expression is a simple identifier.
4761
4762 The type of a template parameter corresponding to a specialized
4763 non-type argument shall not be dependent on a parameter of the
4764 specialization.
4765
4766 Also, we verify that pack expansions only occur at the
4767 end of the argument list. */
4768 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4769 tpd2.parms = 0;
4770 for (i = 0; i < nargs; ++i)
4771 {
4772 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4773 tree arg = TREE_VEC_ELT (inner_args, i);
4774 tree packed_args = NULL_TREE;
4775 int j, len = 1;
4776
4777 if (ARGUMENT_PACK_P (arg))
4778 {
4779 /* Extract the arguments from the argument pack. We'll be
4780 iterating over these in the following loop. */
4781 packed_args = ARGUMENT_PACK_ARGS (arg);
4782 len = TREE_VEC_LENGTH (packed_args);
4783 }
4784
4785 for (j = 0; j < len; j++)
4786 {
4787 if (packed_args)
4788 /* Get the Jth argument in the parameter pack. */
4789 arg = TREE_VEC_ELT (packed_args, j);
4790
4791 if (PACK_EXPANSION_P (arg))
4792 {
4793 /* Pack expansions must come at the end of the
4794 argument list. */
4795 if ((packed_args && j < len - 1)
4796 || (!packed_args && i < nargs - 1))
4797 {
4798 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4799 error ("parameter pack argument %qE must be at the "
4800 "end of the template argument list", arg);
4801 else
4802 error ("parameter pack argument %qT must be at the "
4803 "end of the template argument list", arg);
4804 }
4805 }
4806
4807 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4808 /* We only care about the pattern. */
4809 arg = PACK_EXPANSION_PATTERN (arg);
4810
4811 if (/* These first two lines are the `non-type' bit. */
4812 !TYPE_P (arg)
4813 && TREE_CODE (arg) != TEMPLATE_DECL
4814 /* This next two lines are the `argument expression is not just a
4815 simple identifier' condition and also the `specialized
4816 non-type argument' bit. */
4817 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4818 && !(REFERENCE_REF_P (arg)
4819 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4820 {
4821 if ((!packed_args && tpd.arg_uses_template_parms[i])
4822 || (packed_args && uses_template_parms (arg)))
4823 error ("template argument %qE involves template parameter(s)",
4824 arg);
4825 else
4826 {
4827 /* Look at the corresponding template parameter,
4828 marking which template parameters its type depends
4829 upon. */
4830 tree type = TREE_TYPE (parm);
4831
4832 if (!tpd2.parms)
4833 {
4834 /* We haven't yet initialized TPD2. Do so now. */
4835 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4836 /* The number of parameters here is the number in the
4837 main template, which, as checked in the assertion
4838 above, is NARGS. */
4839 tpd2.parms = XALLOCAVEC (int, nargs);
4840 tpd2.level =
4841 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4842 }
4843
4844 /* Mark the template parameters. But this time, we're
4845 looking for the template parameters of the main
4846 template, not in the specialization. */
4847 tpd2.current_arg = i;
4848 tpd2.arg_uses_template_parms[i] = 0;
4849 memset (tpd2.parms, 0, sizeof (int) * nargs);
4850 for_each_template_parm (type,
4851 &mark_template_parm,
4852 &tpd2,
4853 NULL,
4854 /*include_nondeduced_p=*/false);
4855
4856 if (tpd2.arg_uses_template_parms [i])
4857 {
4858 /* The type depended on some template parameters.
4859 If they are fully specialized in the
4860 specialization, that's OK. */
4861 int j;
4862 int count = 0;
4863 for (j = 0; j < nargs; ++j)
4864 if (tpd2.parms[j] != 0
4865 && tpd.arg_uses_template_parms [j])
4866 ++count;
4867 if (count != 0)
4868 error_n (input_location, count,
4869 "type %qT of template argument %qE depends "
4870 "on a template parameter",
4871 "type %qT of template argument %qE depends "
4872 "on template parameters",
4873 type,
4874 arg);
4875 }
4876 }
4877 }
4878 }
4879 }
4880
4881 /* We should only get here once. */
4882 if (TREE_CODE (decl) == TYPE_DECL)
4883 gcc_assert (!COMPLETE_TYPE_P (type));
4884
4885 // Build the template decl.
4886 tree tmpl = build_template_decl (decl, current_template_parms,
4887 DECL_MEMBER_TEMPLATE_P (maintmpl));
4888 TREE_TYPE (tmpl) = type;
4889 DECL_TEMPLATE_RESULT (tmpl) = decl;
4890 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4891 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4892 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4893
4894 /* Give template template parms a DECL_CONTEXT of the template
4895 for which they are a parameter. */
4896 for (i = 0; i < ntparms; ++i)
4897 {
4898 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4899 if (TREE_CODE (parm) == TEMPLATE_DECL)
4900 DECL_CONTEXT (parm) = tmpl;
4901 }
4902
4903 if (VAR_P (decl))
4904 /* We didn't register this in check_explicit_specialization so we could
4905 wait until the constraints were set. */
4906 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4907 else
4908 associate_classtype_constraints (type);
4909
4910 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4911 = tree_cons (specargs, tmpl,
4912 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4913 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4914
4915 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4916 inst = TREE_CHAIN (inst))
4917 {
4918 tree instance = TREE_VALUE (inst);
4919 if (TYPE_P (instance)
4920 ? (COMPLETE_TYPE_P (instance)
4921 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4922 : DECL_TEMPLATE_INSTANTIATION (instance))
4923 {
4924 tree spec = most_specialized_partial_spec (instance, tf_none);
4925 tree inst_decl = (DECL_P (instance)
4926 ? instance : TYPE_NAME (instance));
4927 if (!spec)
4928 /* OK */;
4929 else if (spec == error_mark_node)
4930 permerror (input_location,
4931 "declaration of %qD ambiguates earlier template "
4932 "instantiation for %qD", decl, inst_decl);
4933 else if (TREE_VALUE (spec) == tmpl)
4934 permerror (input_location,
4935 "partial specialization of %qD after instantiation "
4936 "of %qD", decl, inst_decl);
4937 }
4938 }
4939
4940 return decl;
4941 }
4942
4943 /* PARM is a template parameter of some form; return the corresponding
4944 TEMPLATE_PARM_INDEX. */
4945
4946 static tree
4947 get_template_parm_index (tree parm)
4948 {
4949 if (TREE_CODE (parm) == PARM_DECL
4950 || TREE_CODE (parm) == CONST_DECL)
4951 parm = DECL_INITIAL (parm);
4952 else if (TREE_CODE (parm) == TYPE_DECL
4953 || TREE_CODE (parm) == TEMPLATE_DECL)
4954 parm = TREE_TYPE (parm);
4955 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4956 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4957 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4958 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4959 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4960 return parm;
4961 }
4962
4963 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4964 parameter packs used by the template parameter PARM. */
4965
4966 static void
4967 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4968 {
4969 /* A type parm can't refer to another parm. */
4970 if (TREE_CODE (parm) == TYPE_DECL)
4971 return;
4972 else if (TREE_CODE (parm) == PARM_DECL)
4973 {
4974 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4975 ppd, ppd->visited);
4976 return;
4977 }
4978
4979 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4980
4981 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4982 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4983 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4984 }
4985
4986 /* PARM is a template parameter pack. Return any parameter packs used in
4987 its type or the type of any of its template parameters. If there are
4988 any such packs, it will be instantiated into a fixed template parameter
4989 list by partial instantiation rather than be fully deduced. */
4990
4991 tree
4992 fixed_parameter_pack_p (tree parm)
4993 {
4994 /* This can only be true in a member template. */
4995 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4996 return NULL_TREE;
4997 /* This can only be true for a parameter pack. */
4998 if (!template_parameter_pack_p (parm))
4999 return NULL_TREE;
5000 /* A type parm can't refer to another parm. */
5001 if (TREE_CODE (parm) == TYPE_DECL)
5002 return NULL_TREE;
5003
5004 tree parameter_packs = NULL_TREE;
5005 struct find_parameter_pack_data ppd;
5006 ppd.parameter_packs = &parameter_packs;
5007 ppd.visited = new hash_set<tree>;
5008 ppd.type_pack_expansion_p = false;
5009
5010 fixed_parameter_pack_p_1 (parm, &ppd);
5011
5012 delete ppd.visited;
5013 return parameter_packs;
5014 }
5015
5016 /* Check that a template declaration's use of default arguments and
5017 parameter packs is not invalid. Here, PARMS are the template
5018 parameters. IS_PRIMARY is true if DECL is the thing declared by
5019 a primary template. IS_PARTIAL is true if DECL is a partial
5020 specialization.
5021
5022 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5023 function template declaration or a friend class template
5024 declaration. In the function case, 1 indicates a declaration, 2
5025 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5026 emitted for extraneous default arguments.
5027
5028 Returns TRUE if there were no errors found, FALSE otherwise. */
5029
5030 bool
5031 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5032 bool is_partial, int is_friend_decl)
5033 {
5034 const char *msg;
5035 int last_level_to_check;
5036 tree parm_level;
5037 bool no_errors = true;
5038
5039 /* [temp.param]
5040
5041 A default template-argument shall not be specified in a
5042 function template declaration or a function template definition, nor
5043 in the template-parameter-list of the definition of a member of a
5044 class template. */
5045
5046 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5047 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5048 /* You can't have a function template declaration in a local
5049 scope, nor you can you define a member of a class template in a
5050 local scope. */
5051 return true;
5052
5053 if ((TREE_CODE (decl) == TYPE_DECL
5054 && TREE_TYPE (decl)
5055 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5056 || (TREE_CODE (decl) == FUNCTION_DECL
5057 && LAMBDA_FUNCTION_P (decl)))
5058 /* A lambda doesn't have an explicit declaration; don't complain
5059 about the parms of the enclosing class. */
5060 return true;
5061
5062 if (current_class_type
5063 && !TYPE_BEING_DEFINED (current_class_type)
5064 && DECL_LANG_SPECIFIC (decl)
5065 && DECL_DECLARES_FUNCTION_P (decl)
5066 /* If this is either a friend defined in the scope of the class
5067 or a member function. */
5068 && (DECL_FUNCTION_MEMBER_P (decl)
5069 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5070 : DECL_FRIEND_CONTEXT (decl)
5071 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5072 : false)
5073 /* And, if it was a member function, it really was defined in
5074 the scope of the class. */
5075 && (!DECL_FUNCTION_MEMBER_P (decl)
5076 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5077 /* We already checked these parameters when the template was
5078 declared, so there's no need to do it again now. This function
5079 was defined in class scope, but we're processing its body now
5080 that the class is complete. */
5081 return true;
5082
5083 /* Core issue 226 (C++0x only): the following only applies to class
5084 templates. */
5085 if (is_primary
5086 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5087 {
5088 /* [temp.param]
5089
5090 If a template-parameter has a default template-argument, all
5091 subsequent template-parameters shall have a default
5092 template-argument supplied. */
5093 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5094 {
5095 tree inner_parms = TREE_VALUE (parm_level);
5096 int ntparms = TREE_VEC_LENGTH (inner_parms);
5097 int seen_def_arg_p = 0;
5098 int i;
5099
5100 for (i = 0; i < ntparms; ++i)
5101 {
5102 tree parm = TREE_VEC_ELT (inner_parms, i);
5103
5104 if (parm == error_mark_node)
5105 continue;
5106
5107 if (TREE_PURPOSE (parm))
5108 seen_def_arg_p = 1;
5109 else if (seen_def_arg_p
5110 && !template_parameter_pack_p (TREE_VALUE (parm)))
5111 {
5112 error ("no default argument for %qD", TREE_VALUE (parm));
5113 /* For better subsequent error-recovery, we indicate that
5114 there should have been a default argument. */
5115 TREE_PURPOSE (parm) = error_mark_node;
5116 no_errors = false;
5117 }
5118 else if (!is_partial
5119 && !is_friend_decl
5120 /* Don't complain about an enclosing partial
5121 specialization. */
5122 && parm_level == parms
5123 && TREE_CODE (decl) == TYPE_DECL
5124 && i < ntparms - 1
5125 && template_parameter_pack_p (TREE_VALUE (parm))
5126 /* A fixed parameter pack will be partially
5127 instantiated into a fixed length list. */
5128 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5129 {
5130 /* A primary class template can only have one
5131 parameter pack, at the end of the template
5132 parameter list. */
5133
5134 error ("parameter pack %q+D must be at the end of the"
5135 " template parameter list", TREE_VALUE (parm));
5136
5137 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5138 = error_mark_node;
5139 no_errors = false;
5140 }
5141 }
5142 }
5143 }
5144
5145 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5146 || is_partial
5147 || !is_primary
5148 || is_friend_decl)
5149 /* For an ordinary class template, default template arguments are
5150 allowed at the innermost level, e.g.:
5151 template <class T = int>
5152 struct S {};
5153 but, in a partial specialization, they're not allowed even
5154 there, as we have in [temp.class.spec]:
5155
5156 The template parameter list of a specialization shall not
5157 contain default template argument values.
5158
5159 So, for a partial specialization, or for a function template
5160 (in C++98/C++03), we look at all of them. */
5161 ;
5162 else
5163 /* But, for a primary class template that is not a partial
5164 specialization we look at all template parameters except the
5165 innermost ones. */
5166 parms = TREE_CHAIN (parms);
5167
5168 /* Figure out what error message to issue. */
5169 if (is_friend_decl == 2)
5170 msg = G_("default template arguments may not be used in function template "
5171 "friend re-declaration");
5172 else if (is_friend_decl)
5173 msg = G_("default template arguments may not be used in template "
5174 "friend declarations");
5175 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5176 msg = G_("default template arguments may not be used in function templates "
5177 "without -std=c++11 or -std=gnu++11");
5178 else if (is_partial)
5179 msg = G_("default template arguments may not be used in "
5180 "partial specializations");
5181 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5182 msg = G_("default argument for template parameter for class enclosing %qD");
5183 else
5184 /* Per [temp.param]/9, "A default template-argument shall not be
5185 specified in the template-parameter-lists of the definition of
5186 a member of a class template that appears outside of the member's
5187 class.", thus if we aren't handling a member of a class template
5188 there is no need to examine the parameters. */
5189 return true;
5190
5191 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5192 /* If we're inside a class definition, there's no need to
5193 examine the parameters to the class itself. On the one
5194 hand, they will be checked when the class is defined, and,
5195 on the other, default arguments are valid in things like:
5196 template <class T = double>
5197 struct S { template <class U> void f(U); };
5198 Here the default argument for `S' has no bearing on the
5199 declaration of `f'. */
5200 last_level_to_check = template_class_depth (current_class_type) + 1;
5201 else
5202 /* Check everything. */
5203 last_level_to_check = 0;
5204
5205 for (parm_level = parms;
5206 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5207 parm_level = TREE_CHAIN (parm_level))
5208 {
5209 tree inner_parms = TREE_VALUE (parm_level);
5210 int i;
5211 int ntparms;
5212
5213 ntparms = TREE_VEC_LENGTH (inner_parms);
5214 for (i = 0; i < ntparms; ++i)
5215 {
5216 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5217 continue;
5218
5219 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5220 {
5221 if (msg)
5222 {
5223 no_errors = false;
5224 if (is_friend_decl == 2)
5225 return no_errors;
5226
5227 error (msg, decl);
5228 msg = 0;
5229 }
5230
5231 /* Clear out the default argument so that we are not
5232 confused later. */
5233 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5234 }
5235 }
5236
5237 /* At this point, if we're still interested in issuing messages,
5238 they must apply to classes surrounding the object declared. */
5239 if (msg)
5240 msg = G_("default argument for template parameter for class "
5241 "enclosing %qD");
5242 }
5243
5244 return no_errors;
5245 }
5246
5247 /* Worker for push_template_decl_real, called via
5248 for_each_template_parm. DATA is really an int, indicating the
5249 level of the parameters we are interested in. If T is a template
5250 parameter of that level, return nonzero. */
5251
5252 static int
5253 template_parm_this_level_p (tree t, void* data)
5254 {
5255 int this_level = *(int *)data;
5256 int level;
5257
5258 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5259 level = TEMPLATE_PARM_LEVEL (t);
5260 else
5261 level = TEMPLATE_TYPE_LEVEL (t);
5262 return level == this_level;
5263 }
5264
5265 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5266 DATA is really an int, indicating the innermost outer level of parameters.
5267 If T is a template parameter of that level or further out, return
5268 nonzero. */
5269
5270 static int
5271 template_parm_outer_level (tree t, void *data)
5272 {
5273 int this_level = *(int *)data;
5274 int level;
5275
5276 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5277 level = TEMPLATE_PARM_LEVEL (t);
5278 else
5279 level = TEMPLATE_TYPE_LEVEL (t);
5280 return level <= this_level;
5281 }
5282
5283 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5284 parameters given by current_template_args, or reuses a
5285 previously existing one, if appropriate. Returns the DECL, or an
5286 equivalent one, if it is replaced via a call to duplicate_decls.
5287
5288 If IS_FRIEND is true, DECL is a friend declaration. */
5289
5290 tree
5291 push_template_decl_real (tree decl, bool is_friend)
5292 {
5293 tree tmpl;
5294 tree args;
5295 tree info;
5296 tree ctx;
5297 bool is_primary;
5298 bool is_partial;
5299 int new_template_p = 0;
5300 /* True if the template is a member template, in the sense of
5301 [temp.mem]. */
5302 bool member_template_p = false;
5303
5304 if (decl == error_mark_node || !current_template_parms)
5305 return error_mark_node;
5306
5307 /* See if this is a partial specialization. */
5308 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5309 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5310 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5311 || (VAR_P (decl)
5312 && DECL_LANG_SPECIFIC (decl)
5313 && DECL_TEMPLATE_SPECIALIZATION (decl)
5314 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5315
5316 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5317 is_friend = true;
5318
5319 if (is_friend)
5320 /* For a friend, we want the context of the friend, not
5321 the type of which it is a friend. */
5322 ctx = CP_DECL_CONTEXT (decl);
5323 else if (CP_DECL_CONTEXT (decl)
5324 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5325 /* In the case of a virtual function, we want the class in which
5326 it is defined. */
5327 ctx = CP_DECL_CONTEXT (decl);
5328 else
5329 /* Otherwise, if we're currently defining some class, the DECL
5330 is assumed to be a member of the class. */
5331 ctx = current_scope ();
5332
5333 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5334 ctx = NULL_TREE;
5335
5336 if (!DECL_CONTEXT (decl))
5337 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5338
5339 /* See if this is a primary template. */
5340 if (is_friend && ctx
5341 && uses_template_parms_level (ctx, processing_template_decl))
5342 /* A friend template that specifies a class context, i.e.
5343 template <typename T> friend void A<T>::f();
5344 is not primary. */
5345 is_primary = false;
5346 else if (TREE_CODE (decl) == TYPE_DECL
5347 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5348 is_primary = false;
5349 else
5350 is_primary = template_parm_scope_p ();
5351
5352 if (is_primary)
5353 {
5354 warning (OPT_Wtemplates, "template %qD declared", decl);
5355
5356 if (DECL_CLASS_SCOPE_P (decl))
5357 member_template_p = true;
5358 if (TREE_CODE (decl) == TYPE_DECL
5359 && anon_aggrname_p (DECL_NAME (decl)))
5360 {
5361 error ("template class without a name");
5362 return error_mark_node;
5363 }
5364 else if (TREE_CODE (decl) == FUNCTION_DECL)
5365 {
5366 if (member_template_p)
5367 {
5368 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5369 error ("member template %qD may not have virt-specifiers", decl);
5370 }
5371 if (DECL_DESTRUCTOR_P (decl))
5372 {
5373 /* [temp.mem]
5374
5375 A destructor shall not be a member template. */
5376 error ("destructor %qD declared as member template", decl);
5377 return error_mark_node;
5378 }
5379 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5380 && (!prototype_p (TREE_TYPE (decl))
5381 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5382 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5383 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5384 == void_list_node)))
5385 {
5386 /* [basic.stc.dynamic.allocation]
5387
5388 An allocation function can be a function
5389 template. ... Template allocation functions shall
5390 have two or more parameters. */
5391 error ("invalid template declaration of %qD", decl);
5392 return error_mark_node;
5393 }
5394 }
5395 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5396 && CLASS_TYPE_P (TREE_TYPE (decl)))
5397 {
5398 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5399 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5400 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5401 {
5402 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5403 if (TREE_CODE (t) == TYPE_DECL)
5404 t = TREE_TYPE (t);
5405 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5406 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5407 }
5408 }
5409 else if (TREE_CODE (decl) == TYPE_DECL
5410 && TYPE_DECL_ALIAS_P (decl))
5411 /* alias-declaration */
5412 gcc_assert (!DECL_ARTIFICIAL (decl));
5413 else if (VAR_P (decl))
5414 /* C++14 variable template. */;
5415 else
5416 {
5417 error ("template declaration of %q#D", decl);
5418 return error_mark_node;
5419 }
5420 }
5421
5422 /* Check to see that the rules regarding the use of default
5423 arguments are not being violated. We check args for a friend
5424 functions when we know whether it's a definition, introducing
5425 declaration or re-declaration. */
5426 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5427 check_default_tmpl_args (decl, current_template_parms,
5428 is_primary, is_partial, is_friend);
5429
5430 /* Ensure that there are no parameter packs in the type of this
5431 declaration that have not been expanded. */
5432 if (TREE_CODE (decl) == FUNCTION_DECL)
5433 {
5434 /* Check each of the arguments individually to see if there are
5435 any bare parameter packs. */
5436 tree type = TREE_TYPE (decl);
5437 tree arg = DECL_ARGUMENTS (decl);
5438 tree argtype = TYPE_ARG_TYPES (type);
5439
5440 while (arg && argtype)
5441 {
5442 if (!DECL_PACK_P (arg)
5443 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5444 {
5445 /* This is a PARM_DECL that contains unexpanded parameter
5446 packs. We have already complained about this in the
5447 check_for_bare_parameter_packs call, so just replace
5448 these types with ERROR_MARK_NODE. */
5449 TREE_TYPE (arg) = error_mark_node;
5450 TREE_VALUE (argtype) = error_mark_node;
5451 }
5452
5453 arg = DECL_CHAIN (arg);
5454 argtype = TREE_CHAIN (argtype);
5455 }
5456
5457 /* Check for bare parameter packs in the return type and the
5458 exception specifiers. */
5459 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5460 /* Errors were already issued, set return type to int
5461 as the frontend doesn't expect error_mark_node as
5462 the return type. */
5463 TREE_TYPE (type) = integer_type_node;
5464 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5465 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5466 }
5467 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5468 && TYPE_DECL_ALIAS_P (decl))
5469 ? DECL_ORIGINAL_TYPE (decl)
5470 : TREE_TYPE (decl)))
5471 {
5472 TREE_TYPE (decl) = error_mark_node;
5473 return error_mark_node;
5474 }
5475
5476 if (is_partial)
5477 return process_partial_specialization (decl);
5478
5479 args = current_template_args ();
5480
5481 if (!ctx
5482 || TREE_CODE (ctx) == FUNCTION_DECL
5483 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5484 || (TREE_CODE (decl) == TYPE_DECL
5485 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5486 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5487 {
5488 if (DECL_LANG_SPECIFIC (decl)
5489 && DECL_TEMPLATE_INFO (decl)
5490 && DECL_TI_TEMPLATE (decl))
5491 tmpl = DECL_TI_TEMPLATE (decl);
5492 /* If DECL is a TYPE_DECL for a class-template, then there won't
5493 be DECL_LANG_SPECIFIC. The information equivalent to
5494 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5495 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5496 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5497 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5498 {
5499 /* Since a template declaration already existed for this
5500 class-type, we must be redeclaring it here. Make sure
5501 that the redeclaration is valid. */
5502 redeclare_class_template (TREE_TYPE (decl),
5503 current_template_parms,
5504 current_template_constraints ());
5505 /* We don't need to create a new TEMPLATE_DECL; just use the
5506 one we already had. */
5507 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5508 }
5509 else
5510 {
5511 tmpl = build_template_decl (decl, current_template_parms,
5512 member_template_p);
5513 new_template_p = 1;
5514
5515 if (DECL_LANG_SPECIFIC (decl)
5516 && DECL_TEMPLATE_SPECIALIZATION (decl))
5517 {
5518 /* A specialization of a member template of a template
5519 class. */
5520 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5521 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5522 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5523 }
5524 }
5525 }
5526 else
5527 {
5528 tree a, t, current, parms;
5529 int i;
5530 tree tinfo = get_template_info (decl);
5531
5532 if (!tinfo)
5533 {
5534 error ("template definition of non-template %q#D", decl);
5535 return error_mark_node;
5536 }
5537
5538 tmpl = TI_TEMPLATE (tinfo);
5539
5540 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5541 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5542 && DECL_TEMPLATE_SPECIALIZATION (decl)
5543 && DECL_MEMBER_TEMPLATE_P (tmpl))
5544 {
5545 tree new_tmpl;
5546
5547 /* The declaration is a specialization of a member
5548 template, declared outside the class. Therefore, the
5549 innermost template arguments will be NULL, so we
5550 replace them with the arguments determined by the
5551 earlier call to check_explicit_specialization. */
5552 args = DECL_TI_ARGS (decl);
5553
5554 new_tmpl
5555 = build_template_decl (decl, current_template_parms,
5556 member_template_p);
5557 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5558 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5559 DECL_TI_TEMPLATE (decl) = new_tmpl;
5560 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5561 DECL_TEMPLATE_INFO (new_tmpl)
5562 = build_template_info (tmpl, args);
5563
5564 register_specialization (new_tmpl,
5565 most_general_template (tmpl),
5566 args,
5567 is_friend, 0);
5568 return decl;
5569 }
5570
5571 /* Make sure the template headers we got make sense. */
5572
5573 parms = DECL_TEMPLATE_PARMS (tmpl);
5574 i = TMPL_PARMS_DEPTH (parms);
5575 if (TMPL_ARGS_DEPTH (args) != i)
5576 {
5577 error ("expected %d levels of template parms for %q#D, got %d",
5578 i, decl, TMPL_ARGS_DEPTH (args));
5579 DECL_INTERFACE_KNOWN (decl) = 1;
5580 return error_mark_node;
5581 }
5582 else
5583 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5584 {
5585 a = TMPL_ARGS_LEVEL (args, i);
5586 t = INNERMOST_TEMPLATE_PARMS (parms);
5587
5588 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5589 {
5590 if (current == decl)
5591 error ("got %d template parameters for %q#D",
5592 TREE_VEC_LENGTH (a), decl);
5593 else
5594 error ("got %d template parameters for %q#T",
5595 TREE_VEC_LENGTH (a), current);
5596 error (" but %d required", TREE_VEC_LENGTH (t));
5597 /* Avoid crash in import_export_decl. */
5598 DECL_INTERFACE_KNOWN (decl) = 1;
5599 return error_mark_node;
5600 }
5601
5602 if (current == decl)
5603 current = ctx;
5604 else if (current == NULL_TREE)
5605 /* Can happen in erroneous input. */
5606 break;
5607 else
5608 current = get_containing_scope (current);
5609 }
5610
5611 /* Check that the parms are used in the appropriate qualifying scopes
5612 in the declarator. */
5613 if (!comp_template_args
5614 (TI_ARGS (tinfo),
5615 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5616 {
5617 error ("template arguments to %qD do not match original "
5618 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5619 if (!uses_template_parms (TI_ARGS (tinfo)))
5620 inform (input_location, "use %<template<>%> for"
5621 " an explicit specialization");
5622 /* Avoid crash in import_export_decl. */
5623 DECL_INTERFACE_KNOWN (decl) = 1;
5624 return error_mark_node;
5625 }
5626 }
5627
5628 DECL_TEMPLATE_RESULT (tmpl) = decl;
5629 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5630
5631 /* Push template declarations for global functions and types. Note
5632 that we do not try to push a global template friend declared in a
5633 template class; such a thing may well depend on the template
5634 parameters of the class. */
5635 if (new_template_p && !ctx
5636 && !(is_friend && template_class_depth (current_class_type) > 0))
5637 {
5638 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5639 if (tmpl == error_mark_node)
5640 return error_mark_node;
5641
5642 /* Hide template friend classes that haven't been declared yet. */
5643 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5644 {
5645 DECL_ANTICIPATED (tmpl) = 1;
5646 DECL_FRIEND_P (tmpl) = 1;
5647 }
5648 }
5649
5650 if (is_primary)
5651 {
5652 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5653
5654 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5655
5656 /* Give template template parms a DECL_CONTEXT of the template
5657 for which they are a parameter. */
5658 parms = INNERMOST_TEMPLATE_PARMS (parms);
5659 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5660 {
5661 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5662 if (TREE_CODE (parm) == TEMPLATE_DECL)
5663 DECL_CONTEXT (parm) = tmpl;
5664 }
5665
5666 if (TREE_CODE (decl) == TYPE_DECL
5667 && TYPE_DECL_ALIAS_P (decl)
5668 && complex_alias_template_p (tmpl))
5669 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5670 }
5671
5672 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5673 back to its most general template. If TMPL is a specialization,
5674 ARGS may only have the innermost set of arguments. Add the missing
5675 argument levels if necessary. */
5676 if (DECL_TEMPLATE_INFO (tmpl))
5677 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5678
5679 info = build_template_info (tmpl, args);
5680
5681 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5682 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5683 else
5684 {
5685 if (is_primary)
5686 retrofit_lang_decl (decl);
5687 if (DECL_LANG_SPECIFIC (decl))
5688 DECL_TEMPLATE_INFO (decl) = info;
5689 }
5690
5691 if (flag_implicit_templates
5692 && !is_friend
5693 && TREE_PUBLIC (decl)
5694 && VAR_OR_FUNCTION_DECL_P (decl))
5695 /* Set DECL_COMDAT on template instantiations; if we force
5696 them to be emitted by explicit instantiation or -frepo,
5697 mark_needed will tell cgraph to do the right thing. */
5698 DECL_COMDAT (decl) = true;
5699
5700 return DECL_TEMPLATE_RESULT (tmpl);
5701 }
5702
5703 tree
5704 push_template_decl (tree decl)
5705 {
5706 return push_template_decl_real (decl, false);
5707 }
5708
5709 /* FN is an inheriting constructor that inherits from the constructor
5710 template INHERITED; turn FN into a constructor template with a matching
5711 template header. */
5712
5713 tree
5714 add_inherited_template_parms (tree fn, tree inherited)
5715 {
5716 tree inner_parms
5717 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5718 inner_parms = copy_node (inner_parms);
5719 tree parms
5720 = tree_cons (size_int (processing_template_decl + 1),
5721 inner_parms, current_template_parms);
5722 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5723 tree args = template_parms_to_args (parms);
5724 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5725 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5726 DECL_TEMPLATE_RESULT (tmpl) = fn;
5727 DECL_ARTIFICIAL (tmpl) = true;
5728 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5729 return tmpl;
5730 }
5731
5732 /* Called when a class template TYPE is redeclared with the indicated
5733 template PARMS, e.g.:
5734
5735 template <class T> struct S;
5736 template <class T> struct S {}; */
5737
5738 bool
5739 redeclare_class_template (tree type, tree parms, tree cons)
5740 {
5741 tree tmpl;
5742 tree tmpl_parms;
5743 int i;
5744
5745 if (!TYPE_TEMPLATE_INFO (type))
5746 {
5747 error ("%qT is not a template type", type);
5748 return false;
5749 }
5750
5751 tmpl = TYPE_TI_TEMPLATE (type);
5752 if (!PRIMARY_TEMPLATE_P (tmpl))
5753 /* The type is nested in some template class. Nothing to worry
5754 about here; there are no new template parameters for the nested
5755 type. */
5756 return true;
5757
5758 if (!parms)
5759 {
5760 error ("template specifiers not specified in declaration of %qD",
5761 tmpl);
5762 return false;
5763 }
5764
5765 parms = INNERMOST_TEMPLATE_PARMS (parms);
5766 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5767
5768 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5769 {
5770 error_n (input_location, TREE_VEC_LENGTH (parms),
5771 "redeclared with %d template parameter",
5772 "redeclared with %d template parameters",
5773 TREE_VEC_LENGTH (parms));
5774 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5775 "previous declaration %qD used %d template parameter",
5776 "previous declaration %qD used %d template parameters",
5777 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5778 return false;
5779 }
5780
5781 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5782 {
5783 tree tmpl_parm;
5784 tree parm;
5785 tree tmpl_default;
5786 tree parm_default;
5787
5788 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5789 || TREE_VEC_ELT (parms, i) == error_mark_node)
5790 continue;
5791
5792 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5793 if (error_operand_p (tmpl_parm))
5794 return false;
5795
5796 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5797 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5798 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5799
5800 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5801 TEMPLATE_DECL. */
5802 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5803 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5804 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5805 || (TREE_CODE (tmpl_parm) != PARM_DECL
5806 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5807 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5808 || (TREE_CODE (tmpl_parm) == PARM_DECL
5809 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5810 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5811 {
5812 error ("template parameter %q+#D", tmpl_parm);
5813 error ("redeclared here as %q#D", parm);
5814 return false;
5815 }
5816
5817 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5818 {
5819 /* We have in [temp.param]:
5820
5821 A template-parameter may not be given default arguments
5822 by two different declarations in the same scope. */
5823 error_at (input_location, "redefinition of default argument for %q#D", parm);
5824 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5825 "original definition appeared here");
5826 return false;
5827 }
5828
5829 if (parm_default != NULL_TREE)
5830 /* Update the previous template parameters (which are the ones
5831 that will really count) with the new default value. */
5832 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5833 else if (tmpl_default != NULL_TREE)
5834 /* Update the new parameters, too; they'll be used as the
5835 parameters for any members. */
5836 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5837
5838 /* Give each template template parm in this redeclaration a
5839 DECL_CONTEXT of the template for which they are a parameter. */
5840 if (TREE_CODE (parm) == TEMPLATE_DECL)
5841 {
5842 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5843 DECL_CONTEXT (parm) = tmpl;
5844 }
5845
5846 if (TREE_CODE (parm) == TYPE_DECL)
5847 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5848 }
5849
5850 // Cannot redeclare a class template with a different set of constraints.
5851 if (!equivalent_constraints (get_constraints (tmpl), cons))
5852 {
5853 error_at (input_location, "redeclaration %q#D with different "
5854 "constraints", tmpl);
5855 inform (DECL_SOURCE_LOCATION (tmpl),
5856 "original declaration appeared here");
5857 }
5858
5859 return true;
5860 }
5861
5862 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5863 to be used when the caller has already checked
5864 (processing_template_decl
5865 && !instantiation_dependent_expression_p (expr)
5866 && potential_constant_expression (expr))
5867 and cleared processing_template_decl. */
5868
5869 tree
5870 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5871 {
5872 return tsubst_copy_and_build (expr,
5873 /*args=*/NULL_TREE,
5874 complain,
5875 /*in_decl=*/NULL_TREE,
5876 /*function_p=*/false,
5877 /*integral_constant_expression_p=*/true);
5878 }
5879
5880 /* Simplify EXPR if it is a non-dependent expression. Returns the
5881 (possibly simplified) expression. */
5882
5883 tree
5884 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5885 {
5886 if (expr == NULL_TREE)
5887 return NULL_TREE;
5888
5889 /* If we're in a template, but EXPR isn't value dependent, simplify
5890 it. We're supposed to treat:
5891
5892 template <typename T> void f(T[1 + 1]);
5893 template <typename T> void f(T[2]);
5894
5895 as two declarations of the same function, for example. */
5896 if (processing_template_decl
5897 && is_nondependent_constant_expression (expr))
5898 {
5899 processing_template_decl_sentinel s;
5900 expr = instantiate_non_dependent_expr_internal (expr, complain);
5901 }
5902 return expr;
5903 }
5904
5905 tree
5906 instantiate_non_dependent_expr (tree expr)
5907 {
5908 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5909 }
5910
5911 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5912 an uninstantiated expression. */
5913
5914 tree
5915 instantiate_non_dependent_or_null (tree expr)
5916 {
5917 if (expr == NULL_TREE)
5918 return NULL_TREE;
5919 if (processing_template_decl)
5920 {
5921 if (!is_nondependent_constant_expression (expr))
5922 expr = NULL_TREE;
5923 else
5924 {
5925 processing_template_decl_sentinel s;
5926 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5927 }
5928 }
5929 return expr;
5930 }
5931
5932 /* True iff T is a specialization of a variable template. */
5933
5934 bool
5935 variable_template_specialization_p (tree t)
5936 {
5937 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5938 return false;
5939 tree tmpl = DECL_TI_TEMPLATE (t);
5940 return variable_template_p (tmpl);
5941 }
5942
5943 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5944 template declaration, or a TYPE_DECL for an alias declaration. */
5945
5946 bool
5947 alias_type_or_template_p (tree t)
5948 {
5949 if (t == NULL_TREE)
5950 return false;
5951 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5952 || (TYPE_P (t)
5953 && TYPE_NAME (t)
5954 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5955 || DECL_ALIAS_TEMPLATE_P (t));
5956 }
5957
5958 /* Return TRUE iff T is a specialization of an alias template. */
5959
5960 bool
5961 alias_template_specialization_p (const_tree t)
5962 {
5963 /* It's an alias template specialization if it's an alias and its
5964 TYPE_NAME is a specialization of a primary template. */
5965 if (TYPE_ALIAS_P (t))
5966 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5967 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5968
5969 return false;
5970 }
5971
5972 /* An alias template is complex from a SFINAE perspective if a template-id
5973 using that alias can be ill-formed when the expansion is not, as with
5974 the void_t template. We determine this by checking whether the
5975 expansion for the alias template uses all its template parameters. */
5976
5977 struct uses_all_template_parms_data
5978 {
5979 int level;
5980 bool *seen;
5981 };
5982
5983 static int
5984 uses_all_template_parms_r (tree t, void *data_)
5985 {
5986 struct uses_all_template_parms_data &data
5987 = *(struct uses_all_template_parms_data*)data_;
5988 tree idx = get_template_parm_index (t);
5989
5990 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5991 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5992 return 0;
5993 }
5994
5995 static bool
5996 complex_alias_template_p (const_tree tmpl)
5997 {
5998 struct uses_all_template_parms_data data;
5999 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6000 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6001 data.level = TMPL_PARMS_DEPTH (parms);
6002 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6003 data.seen = XALLOCAVEC (bool, len);
6004 for (int i = 0; i < len; ++i)
6005 data.seen[i] = false;
6006
6007 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6008 for (int i = 0; i < len; ++i)
6009 if (!data.seen[i])
6010 return true;
6011 return false;
6012 }
6013
6014 /* Return TRUE iff T is a specialization of a complex alias template with
6015 dependent template-arguments. */
6016
6017 bool
6018 dependent_alias_template_spec_p (const_tree t)
6019 {
6020 if (!alias_template_specialization_p (t))
6021 return false;
6022
6023 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6024 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6025 return false;
6026
6027 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6028 if (!any_dependent_template_arguments_p (args))
6029 return false;
6030
6031 return true;
6032 }
6033
6034 /* Return the number of innermost template parameters in TMPL. */
6035
6036 static int
6037 num_innermost_template_parms (tree tmpl)
6038 {
6039 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6040 return TREE_VEC_LENGTH (parms);
6041 }
6042
6043 /* Return either TMPL or another template that it is equivalent to under DR
6044 1286: An alias that just changes the name of a template is equivalent to
6045 the other template. */
6046
6047 static tree
6048 get_underlying_template (tree tmpl)
6049 {
6050 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6051 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6052 {
6053 /* Determine if the alias is equivalent to an underlying template. */
6054 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6055 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6056 if (!tinfo)
6057 break;
6058
6059 tree underlying = TI_TEMPLATE (tinfo);
6060 if (!PRIMARY_TEMPLATE_P (underlying)
6061 || (num_innermost_template_parms (tmpl)
6062 != num_innermost_template_parms (underlying)))
6063 break;
6064
6065 tree alias_args = INNERMOST_TEMPLATE_ARGS
6066 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6067 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6068 break;
6069
6070 /* Alias is equivalent. Strip it and repeat. */
6071 tmpl = underlying;
6072 }
6073
6074 return tmpl;
6075 }
6076
6077 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6078 must be a reference-to-function or a pointer-to-function type, as specified
6079 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6080 and check that the resulting function has external linkage. */
6081
6082 static tree
6083 convert_nontype_argument_function (tree type, tree expr,
6084 tsubst_flags_t complain)
6085 {
6086 tree fns = expr;
6087 tree fn, fn_no_ptr;
6088 linkage_kind linkage;
6089
6090 fn = instantiate_type (type, fns, tf_none);
6091 if (fn == error_mark_node)
6092 return error_mark_node;
6093
6094 if (value_dependent_expression_p (fn))
6095 goto accept;
6096
6097 fn_no_ptr = strip_fnptr_conv (fn);
6098 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6099 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6100 if (BASELINK_P (fn_no_ptr))
6101 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6102
6103 /* [temp.arg.nontype]/1
6104
6105 A template-argument for a non-type, non-template template-parameter
6106 shall be one of:
6107 [...]
6108 -- the address of an object or function with external [C++11: or
6109 internal] linkage. */
6110
6111 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6112 {
6113 if (complain & tf_error)
6114 {
6115 error ("%qE is not a valid template argument for type %qT",
6116 expr, type);
6117 if (TYPE_PTR_P (type))
6118 inform (input_location, "it must be the address of a function "
6119 "with external linkage");
6120 else
6121 inform (input_location, "it must be the name of a function with "
6122 "external linkage");
6123 }
6124 return NULL_TREE;
6125 }
6126
6127 linkage = decl_linkage (fn_no_ptr);
6128 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6129 {
6130 if (complain & tf_error)
6131 {
6132 if (cxx_dialect >= cxx11)
6133 error ("%qE is not a valid template argument for type %qT "
6134 "because %qD has no linkage",
6135 expr, type, fn_no_ptr);
6136 else
6137 error ("%qE is not a valid template argument for type %qT "
6138 "because %qD does not have external linkage",
6139 expr, type, fn_no_ptr);
6140 }
6141 return NULL_TREE;
6142 }
6143
6144 accept:
6145 if (TREE_CODE (type) == REFERENCE_TYPE)
6146 fn = build_address (fn);
6147 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6148 fn = build_nop (type, fn);
6149
6150 return fn;
6151 }
6152
6153 /* Subroutine of convert_nontype_argument.
6154 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6155 Emit an error otherwise. */
6156
6157 static bool
6158 check_valid_ptrmem_cst_expr (tree type, tree expr,
6159 tsubst_flags_t complain)
6160 {
6161 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6162 tree orig_expr = expr;
6163 STRIP_NOPS (expr);
6164 if (null_ptr_cst_p (expr))
6165 return true;
6166 if (TREE_CODE (expr) == PTRMEM_CST
6167 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6168 PTRMEM_CST_CLASS (expr)))
6169 return true;
6170 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6171 return true;
6172 if (processing_template_decl
6173 && TREE_CODE (expr) == ADDR_EXPR
6174 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6175 return true;
6176 if (complain & tf_error)
6177 {
6178 error_at (loc, "%qE is not a valid template argument for type %qT",
6179 orig_expr, type);
6180 if (TREE_CODE (expr) != PTRMEM_CST)
6181 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6182 else
6183 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6184 }
6185 return false;
6186 }
6187
6188 /* Returns TRUE iff the address of OP is value-dependent.
6189
6190 14.6.2.4 [temp.dep.temp]:
6191 A non-integral non-type template-argument is dependent if its type is
6192 dependent or it has either of the following forms
6193 qualified-id
6194 & qualified-id
6195 and contains a nested-name-specifier which specifies a class-name that
6196 names a dependent type.
6197
6198 We generalize this to just say that the address of a member of a
6199 dependent class is value-dependent; the above doesn't cover the
6200 address of a static data member named with an unqualified-id. */
6201
6202 static bool
6203 has_value_dependent_address (tree op)
6204 {
6205 /* We could use get_inner_reference here, but there's no need;
6206 this is only relevant for template non-type arguments, which
6207 can only be expressed as &id-expression. */
6208 if (DECL_P (op))
6209 {
6210 tree ctx = CP_DECL_CONTEXT (op);
6211 if (TYPE_P (ctx) && dependent_type_p (ctx))
6212 return true;
6213 }
6214
6215 return false;
6216 }
6217
6218 /* The next set of functions are used for providing helpful explanatory
6219 diagnostics for failed overload resolution. Their messages should be
6220 indented by two spaces for consistency with the messages in
6221 call.c */
6222
6223 static int
6224 unify_success (bool /*explain_p*/)
6225 {
6226 return 0;
6227 }
6228
6229 /* Other failure functions should call this one, to provide a single function
6230 for setting a breakpoint on. */
6231
6232 static int
6233 unify_invalid (bool /*explain_p*/)
6234 {
6235 return 1;
6236 }
6237
6238 static int
6239 unify_parameter_deduction_failure (bool explain_p, tree parm)
6240 {
6241 if (explain_p)
6242 inform (input_location,
6243 " couldn't deduce template parameter %qD", parm);
6244 return unify_invalid (explain_p);
6245 }
6246
6247 static int
6248 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6249 {
6250 if (explain_p)
6251 inform (input_location,
6252 " types %qT and %qT have incompatible cv-qualifiers",
6253 parm, arg);
6254 return unify_invalid (explain_p);
6255 }
6256
6257 static int
6258 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6259 {
6260 if (explain_p)
6261 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6262 return unify_invalid (explain_p);
6263 }
6264
6265 static int
6266 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6267 {
6268 if (explain_p)
6269 inform (input_location,
6270 " template parameter %qD is not a parameter pack, but "
6271 "argument %qD is",
6272 parm, arg);
6273 return unify_invalid (explain_p);
6274 }
6275
6276 static int
6277 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6278 {
6279 if (explain_p)
6280 inform (input_location,
6281 " template argument %qE does not match "
6282 "pointer-to-member constant %qE",
6283 arg, parm);
6284 return unify_invalid (explain_p);
6285 }
6286
6287 static int
6288 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6289 {
6290 if (explain_p)
6291 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6292 return unify_invalid (explain_p);
6293 }
6294
6295 static int
6296 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6297 {
6298 if (explain_p)
6299 inform (input_location,
6300 " inconsistent parameter pack deduction with %qT and %qT",
6301 old_arg, new_arg);
6302 return unify_invalid (explain_p);
6303 }
6304
6305 static int
6306 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6307 {
6308 if (explain_p)
6309 {
6310 if (TYPE_P (parm))
6311 inform (input_location,
6312 " deduced conflicting types for parameter %qT (%qT and %qT)",
6313 parm, first, second);
6314 else
6315 inform (input_location,
6316 " deduced conflicting values for non-type parameter "
6317 "%qE (%qE and %qE)", parm, first, second);
6318 }
6319 return unify_invalid (explain_p);
6320 }
6321
6322 static int
6323 unify_vla_arg (bool explain_p, tree arg)
6324 {
6325 if (explain_p)
6326 inform (input_location,
6327 " variable-sized array type %qT is not "
6328 "a valid template argument",
6329 arg);
6330 return unify_invalid (explain_p);
6331 }
6332
6333 static int
6334 unify_method_type_error (bool explain_p, tree arg)
6335 {
6336 if (explain_p)
6337 inform (input_location,
6338 " member function type %qT is not a valid template argument",
6339 arg);
6340 return unify_invalid (explain_p);
6341 }
6342
6343 static int
6344 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6345 {
6346 if (explain_p)
6347 {
6348 if (least_p)
6349 inform_n (input_location, wanted,
6350 " candidate expects at least %d argument, %d provided",
6351 " candidate expects at least %d arguments, %d provided",
6352 wanted, have);
6353 else
6354 inform_n (input_location, wanted,
6355 " candidate expects %d argument, %d provided",
6356 " candidate expects %d arguments, %d provided",
6357 wanted, have);
6358 }
6359 return unify_invalid (explain_p);
6360 }
6361
6362 static int
6363 unify_too_many_arguments (bool explain_p, int have, int wanted)
6364 {
6365 return unify_arity (explain_p, have, wanted);
6366 }
6367
6368 static int
6369 unify_too_few_arguments (bool explain_p, int have, int wanted,
6370 bool least_p = false)
6371 {
6372 return unify_arity (explain_p, have, wanted, least_p);
6373 }
6374
6375 static int
6376 unify_arg_conversion (bool explain_p, tree to_type,
6377 tree from_type, tree arg)
6378 {
6379 if (explain_p)
6380 inform (EXPR_LOC_OR_LOC (arg, input_location),
6381 " cannot convert %qE (type %qT) to type %qT",
6382 arg, from_type, to_type);
6383 return unify_invalid (explain_p);
6384 }
6385
6386 static int
6387 unify_no_common_base (bool explain_p, enum template_base_result r,
6388 tree parm, tree arg)
6389 {
6390 if (explain_p)
6391 switch (r)
6392 {
6393 case tbr_ambiguous_baseclass:
6394 inform (input_location, " %qT is an ambiguous base class of %qT",
6395 parm, arg);
6396 break;
6397 default:
6398 inform (input_location, " %qT is not derived from %qT", arg, parm);
6399 break;
6400 }
6401 return unify_invalid (explain_p);
6402 }
6403
6404 static int
6405 unify_inconsistent_template_template_parameters (bool explain_p)
6406 {
6407 if (explain_p)
6408 inform (input_location,
6409 " template parameters of a template template argument are "
6410 "inconsistent with other deduced template arguments");
6411 return unify_invalid (explain_p);
6412 }
6413
6414 static int
6415 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6416 {
6417 if (explain_p)
6418 inform (input_location,
6419 " can't deduce a template for %qT from non-template type %qT",
6420 parm, arg);
6421 return unify_invalid (explain_p);
6422 }
6423
6424 static int
6425 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6426 {
6427 if (explain_p)
6428 inform (input_location,
6429 " template argument %qE does not match %qE", arg, parm);
6430 return unify_invalid (explain_p);
6431 }
6432
6433 /* Attempt to convert the non-type template parameter EXPR to the
6434 indicated TYPE. If the conversion is successful, return the
6435 converted value. If the conversion is unsuccessful, return
6436 NULL_TREE if we issued an error message, or error_mark_node if we
6437 did not. We issue error messages for out-and-out bad template
6438 parameters, but not simply because the conversion failed, since we
6439 might be just trying to do argument deduction. Both TYPE and EXPR
6440 must be non-dependent.
6441
6442 The conversion follows the special rules described in
6443 [temp.arg.nontype], and it is much more strict than an implicit
6444 conversion.
6445
6446 This function is called twice for each template argument (see
6447 lookup_template_class for a more accurate description of this
6448 problem). This means that we need to handle expressions which
6449 are not valid in a C++ source, but can be created from the
6450 first call (for instance, casts to perform conversions). These
6451 hacks can go away after we fix the double coercion problem. */
6452
6453 static tree
6454 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6455 {
6456 tree expr_type;
6457 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6458 tree orig_expr = expr;
6459
6460 /* Detect immediately string literals as invalid non-type argument.
6461 This special-case is not needed for correctness (we would easily
6462 catch this later), but only to provide better diagnostic for this
6463 common user mistake. As suggested by DR 100, we do not mention
6464 linkage issues in the diagnostic as this is not the point. */
6465 /* FIXME we're making this OK. */
6466 if (TREE_CODE (expr) == STRING_CST)
6467 {
6468 if (complain & tf_error)
6469 error ("%qE is not a valid template argument for type %qT "
6470 "because string literals can never be used in this context",
6471 expr, type);
6472 return NULL_TREE;
6473 }
6474
6475 /* Add the ADDR_EXPR now for the benefit of
6476 value_dependent_expression_p. */
6477 if (TYPE_PTROBV_P (type)
6478 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6479 {
6480 expr = decay_conversion (expr, complain);
6481 if (expr == error_mark_node)
6482 return error_mark_node;
6483 }
6484
6485 /* If we are in a template, EXPR may be non-dependent, but still
6486 have a syntactic, rather than semantic, form. For example, EXPR
6487 might be a SCOPE_REF, rather than the VAR_DECL to which the
6488 SCOPE_REF refers. Preserving the qualifying scope is necessary
6489 so that access checking can be performed when the template is
6490 instantiated -- but here we need the resolved form so that we can
6491 convert the argument. */
6492 bool non_dep = false;
6493 if (TYPE_REF_OBJ_P (type)
6494 && has_value_dependent_address (expr))
6495 /* If we want the address and it's value-dependent, don't fold. */;
6496 else if (processing_template_decl
6497 && is_nondependent_constant_expression (expr))
6498 non_dep = true;
6499 if (error_operand_p (expr))
6500 return error_mark_node;
6501 expr_type = TREE_TYPE (expr);
6502
6503 /* If the argument is non-dependent, perform any conversions in
6504 non-dependent context as well. */
6505 processing_template_decl_sentinel s (non_dep);
6506 if (non_dep)
6507 expr = instantiate_non_dependent_expr_internal (expr, complain);
6508
6509 if (value_dependent_expression_p (expr))
6510 expr = canonicalize_expr_argument (expr, complain);
6511
6512 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6513 to a non-type argument of "nullptr". */
6514 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6515 expr = fold_simple (convert (type, expr));
6516
6517 /* In C++11, integral or enumeration non-type template arguments can be
6518 arbitrary constant expressions. Pointer and pointer to
6519 member arguments can be general constant expressions that evaluate
6520 to a null value, but otherwise still need to be of a specific form. */
6521 if (cxx_dialect >= cxx11)
6522 {
6523 if (TREE_CODE (expr) == PTRMEM_CST)
6524 /* A PTRMEM_CST is already constant, and a valid template
6525 argument for a parameter of pointer to member type, we just want
6526 to leave it in that form rather than lower it to a
6527 CONSTRUCTOR. */;
6528 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6529 || cxx_dialect >= cxx17)
6530 {
6531 /* C++17: A template-argument for a non-type template-parameter shall
6532 be a converted constant expression (8.20) of the type of the
6533 template-parameter. */
6534 expr = build_converted_constant_expr (type, expr, complain);
6535 if (expr == error_mark_node)
6536 return error_mark_node;
6537 expr = maybe_constant_value (expr);
6538 expr = convert_from_reference (expr);
6539 }
6540 else if (TYPE_PTR_OR_PTRMEM_P (type))
6541 {
6542 tree folded = maybe_constant_value (expr);
6543 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6544 : null_member_pointer_value_p (folded))
6545 expr = folded;
6546 }
6547 }
6548
6549 if (TREE_CODE (type) == REFERENCE_TYPE)
6550 expr = mark_lvalue_use (expr);
6551 else
6552 expr = mark_rvalue_use (expr);
6553
6554 /* HACK: Due to double coercion, we can get a
6555 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6556 which is the tree that we built on the first call (see
6557 below when coercing to reference to object or to reference to
6558 function). We just strip everything and get to the arg.
6559 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6560 for examples. */
6561 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6562 {
6563 tree probe_type, probe = expr;
6564 if (REFERENCE_REF_P (probe))
6565 probe = TREE_OPERAND (probe, 0);
6566 probe_type = TREE_TYPE (probe);
6567 if (TREE_CODE (probe) == NOP_EXPR)
6568 {
6569 /* ??? Maybe we could use convert_from_reference here, but we
6570 would need to relax its constraints because the NOP_EXPR
6571 could actually change the type to something more cv-qualified,
6572 and this is not folded by convert_from_reference. */
6573 tree addr = TREE_OPERAND (probe, 0);
6574 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6575 && TREE_CODE (addr) == ADDR_EXPR
6576 && TYPE_PTR_P (TREE_TYPE (addr))
6577 && (same_type_ignoring_top_level_qualifiers_p
6578 (TREE_TYPE (probe_type),
6579 TREE_TYPE (TREE_TYPE (addr)))))
6580 {
6581 expr = TREE_OPERAND (addr, 0);
6582 expr_type = TREE_TYPE (probe_type);
6583 }
6584 }
6585 }
6586
6587 /* [temp.arg.nontype]/5, bullet 1
6588
6589 For a non-type template-parameter of integral or enumeration type,
6590 integral promotions (_conv.prom_) and integral conversions
6591 (_conv.integral_) are applied. */
6592 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6593 {
6594 if (cxx_dialect < cxx11)
6595 {
6596 tree t = build_converted_constant_expr (type, expr, complain);
6597 t = maybe_constant_value (t);
6598 if (t != error_mark_node)
6599 expr = t;
6600 }
6601
6602 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6603 return error_mark_node;
6604
6605 /* Notice that there are constant expressions like '4 % 0' which
6606 do not fold into integer constants. */
6607 if (TREE_CODE (expr) != INTEGER_CST
6608 && !value_dependent_expression_p (expr))
6609 {
6610 if (complain & tf_error)
6611 {
6612 int errs = errorcount, warns = warningcount + werrorcount;
6613 if (!require_potential_constant_expression (expr))
6614 expr = error_mark_node;
6615 else
6616 expr = cxx_constant_value (expr);
6617 if (errorcount > errs || warningcount + werrorcount > warns)
6618 inform (loc, "in template argument for type %qT ", type);
6619 if (expr == error_mark_node)
6620 return NULL_TREE;
6621 /* else cxx_constant_value complained but gave us
6622 a real constant, so go ahead. */
6623 if (TREE_CODE (expr) != INTEGER_CST)
6624 {
6625 /* Some assemble time constant expressions like
6626 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6627 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6628 as we can emit them into .rodata initializers of
6629 variables, yet they can't fold into an INTEGER_CST at
6630 compile time. Refuse them here. */
6631 gcc_checking_assert (reduced_constant_expression_p (expr));
6632 error_at (loc, "template argument %qE for type %qT not "
6633 "a constant integer", expr, type);
6634 return NULL_TREE;
6635 }
6636 }
6637 else
6638 return NULL_TREE;
6639 }
6640
6641 /* Avoid typedef problems. */
6642 if (TREE_TYPE (expr) != type)
6643 expr = fold_convert (type, expr);
6644 }
6645 /* [temp.arg.nontype]/5, bullet 2
6646
6647 For a non-type template-parameter of type pointer to object,
6648 qualification conversions (_conv.qual_) and the array-to-pointer
6649 conversion (_conv.array_) are applied. */
6650 else if (TYPE_PTROBV_P (type))
6651 {
6652 tree decayed = expr;
6653
6654 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6655 decay_conversion or an explicit cast. If it's a problematic cast,
6656 we'll complain about it below. */
6657 if (TREE_CODE (expr) == NOP_EXPR)
6658 {
6659 tree probe = expr;
6660 STRIP_NOPS (probe);
6661 if (TREE_CODE (probe) == ADDR_EXPR
6662 && TYPE_PTR_P (TREE_TYPE (probe)))
6663 {
6664 expr = probe;
6665 expr_type = TREE_TYPE (expr);
6666 }
6667 }
6668
6669 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6670
6671 A template-argument for a non-type, non-template template-parameter
6672 shall be one of: [...]
6673
6674 -- the name of a non-type template-parameter;
6675 -- the address of an object or function with external linkage, [...]
6676 expressed as "& id-expression" where the & is optional if the name
6677 refers to a function or array, or if the corresponding
6678 template-parameter is a reference.
6679
6680 Here, we do not care about functions, as they are invalid anyway
6681 for a parameter of type pointer-to-object. */
6682
6683 if (value_dependent_expression_p (expr))
6684 /* Non-type template parameters are OK. */
6685 ;
6686 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6687 /* Null pointer values are OK in C++11. */;
6688 else if (TREE_CODE (expr) != ADDR_EXPR)
6689 {
6690 if (VAR_P (expr))
6691 {
6692 if (complain & tf_error)
6693 error ("%qD is not a valid template argument "
6694 "because %qD is a variable, not the address of "
6695 "a variable", orig_expr, expr);
6696 return NULL_TREE;
6697 }
6698 if (POINTER_TYPE_P (expr_type))
6699 {
6700 if (complain & tf_error)
6701 error ("%qE is not a valid template argument for %qT "
6702 "because it is not the address of a variable",
6703 orig_expr, type);
6704 return NULL_TREE;
6705 }
6706 /* Other values, like integer constants, might be valid
6707 non-type arguments of some other type. */
6708 return error_mark_node;
6709 }
6710 else
6711 {
6712 tree decl = TREE_OPERAND (expr, 0);
6713
6714 if (!VAR_P (decl))
6715 {
6716 if (complain & tf_error)
6717 error ("%qE is not a valid template argument of type %qT "
6718 "because %qE is not a variable", orig_expr, type, decl);
6719 return NULL_TREE;
6720 }
6721 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6722 {
6723 if (complain & tf_error)
6724 error ("%qE is not a valid template argument of type %qT "
6725 "because %qD does not have external linkage",
6726 orig_expr, type, decl);
6727 return NULL_TREE;
6728 }
6729 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6730 && decl_linkage (decl) == lk_none)
6731 {
6732 if (complain & tf_error)
6733 error ("%qE is not a valid template argument of type %qT "
6734 "because %qD has no linkage", orig_expr, type, decl);
6735 return NULL_TREE;
6736 }
6737 /* C++17: For a non-type template-parameter of reference or pointer
6738 type, the value of the constant expression shall not refer to (or
6739 for a pointer type, shall not be the address of):
6740 * a subobject (4.5),
6741 * a temporary object (15.2),
6742 * a string literal (5.13.5),
6743 * the result of a typeid expression (8.2.8), or
6744 * a predefined __func__ variable (11.4.1). */
6745 else if (DECL_ARTIFICIAL (decl))
6746 {
6747 if (complain & tf_error)
6748 error ("the address of %qD is not a valid template argument",
6749 decl);
6750 return NULL_TREE;
6751 }
6752 else if (!same_type_ignoring_top_level_qualifiers_p
6753 (strip_array_types (TREE_TYPE (type)),
6754 strip_array_types (TREE_TYPE (decl))))
6755 {
6756 if (complain & tf_error)
6757 error ("the address of the %qT subobject of %qD is not a "
6758 "valid template argument", TREE_TYPE (type), decl);
6759 return NULL_TREE;
6760 }
6761 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6762 {
6763 if (complain & tf_error)
6764 error ("the address of %qD is not a valid template argument "
6765 "because it does not have static storage duration",
6766 decl);
6767 return NULL_TREE;
6768 }
6769 }
6770
6771 expr = decayed;
6772
6773 expr = perform_qualification_conversions (type, expr);
6774 if (expr == error_mark_node)
6775 return error_mark_node;
6776 }
6777 /* [temp.arg.nontype]/5, bullet 3
6778
6779 For a non-type template-parameter of type reference to object, no
6780 conversions apply. The type referred to by the reference may be more
6781 cv-qualified than the (otherwise identical) type of the
6782 template-argument. The template-parameter is bound directly to the
6783 template-argument, which must be an lvalue. */
6784 else if (TYPE_REF_OBJ_P (type))
6785 {
6786 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6787 expr_type))
6788 return error_mark_node;
6789
6790 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6791 {
6792 if (complain & tf_error)
6793 error ("%qE is not a valid template argument for type %qT "
6794 "because of conflicts in cv-qualification", expr, type);
6795 return NULL_TREE;
6796 }
6797
6798 if (!lvalue_p (expr))
6799 {
6800 if (complain & tf_error)
6801 error ("%qE is not a valid template argument for type %qT "
6802 "because it is not an lvalue", expr, type);
6803 return NULL_TREE;
6804 }
6805
6806 /* [temp.arg.nontype]/1
6807
6808 A template-argument for a non-type, non-template template-parameter
6809 shall be one of: [...]
6810
6811 -- the address of an object or function with external linkage. */
6812 if (INDIRECT_REF_P (expr)
6813 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6814 {
6815 expr = TREE_OPERAND (expr, 0);
6816 if (DECL_P (expr))
6817 {
6818 if (complain & tf_error)
6819 error ("%q#D is not a valid template argument for type %qT "
6820 "because a reference variable does not have a constant "
6821 "address", expr, type);
6822 return NULL_TREE;
6823 }
6824 }
6825
6826 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6827 && value_dependent_expression_p (expr))
6828 /* OK, dependent reference. We don't want to ask whether a DECL is
6829 itself value-dependent, since what we want here is its address. */;
6830 else
6831 {
6832 if (!DECL_P (expr))
6833 {
6834 if (complain & tf_error)
6835 error ("%qE is not a valid template argument for type %qT "
6836 "because it is not an object with linkage",
6837 expr, type);
6838 return NULL_TREE;
6839 }
6840
6841 /* DR 1155 allows internal linkage in C++11 and up. */
6842 linkage_kind linkage = decl_linkage (expr);
6843 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6844 {
6845 if (complain & tf_error)
6846 error ("%qE is not a valid template argument for type %qT "
6847 "because object %qD does not have linkage",
6848 expr, type, expr);
6849 return NULL_TREE;
6850 }
6851
6852 expr = build_address (expr);
6853 }
6854
6855 if (!same_type_p (type, TREE_TYPE (expr)))
6856 expr = build_nop (type, expr);
6857 }
6858 /* [temp.arg.nontype]/5, bullet 4
6859
6860 For a non-type template-parameter of type pointer to function, only
6861 the function-to-pointer conversion (_conv.func_) is applied. If the
6862 template-argument represents a set of overloaded functions (or a
6863 pointer to such), the matching function is selected from the set
6864 (_over.over_). */
6865 else if (TYPE_PTRFN_P (type))
6866 {
6867 /* If the argument is a template-id, we might not have enough
6868 context information to decay the pointer. */
6869 if (!type_unknown_p (expr_type))
6870 {
6871 expr = decay_conversion (expr, complain);
6872 if (expr == error_mark_node)
6873 return error_mark_node;
6874 }
6875
6876 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6877 /* Null pointer values are OK in C++11. */
6878 return perform_qualification_conversions (type, expr);
6879
6880 expr = convert_nontype_argument_function (type, expr, complain);
6881 if (!expr || expr == error_mark_node)
6882 return expr;
6883 }
6884 /* [temp.arg.nontype]/5, bullet 5
6885
6886 For a non-type template-parameter of type reference to function, no
6887 conversions apply. If the template-argument represents a set of
6888 overloaded functions, the matching function is selected from the set
6889 (_over.over_). */
6890 else if (TYPE_REFFN_P (type))
6891 {
6892 if (TREE_CODE (expr) == ADDR_EXPR)
6893 {
6894 if (complain & tf_error)
6895 {
6896 error ("%qE is not a valid template argument for type %qT "
6897 "because it is a pointer", expr, type);
6898 inform (input_location, "try using %qE instead",
6899 TREE_OPERAND (expr, 0));
6900 }
6901 return NULL_TREE;
6902 }
6903
6904 expr = convert_nontype_argument_function (type, expr, complain);
6905 if (!expr || expr == error_mark_node)
6906 return expr;
6907 }
6908 /* [temp.arg.nontype]/5, bullet 6
6909
6910 For a non-type template-parameter of type pointer to member function,
6911 no conversions apply. If the template-argument represents a set of
6912 overloaded member functions, the matching member function is selected
6913 from the set (_over.over_). */
6914 else if (TYPE_PTRMEMFUNC_P (type))
6915 {
6916 expr = instantiate_type (type, expr, tf_none);
6917 if (expr == error_mark_node)
6918 return error_mark_node;
6919
6920 /* [temp.arg.nontype] bullet 1 says the pointer to member
6921 expression must be a pointer-to-member constant. */
6922 if (!value_dependent_expression_p (expr)
6923 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6924 return NULL_TREE;
6925
6926 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6927 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6928 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6929 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6930 }
6931 /* [temp.arg.nontype]/5, bullet 7
6932
6933 For a non-type template-parameter of type pointer to data member,
6934 qualification conversions (_conv.qual_) are applied. */
6935 else if (TYPE_PTRDATAMEM_P (type))
6936 {
6937 /* [temp.arg.nontype] bullet 1 says the pointer to member
6938 expression must be a pointer-to-member constant. */
6939 if (!value_dependent_expression_p (expr)
6940 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6941 return NULL_TREE;
6942
6943 expr = perform_qualification_conversions (type, expr);
6944 if (expr == error_mark_node)
6945 return expr;
6946 }
6947 else if (NULLPTR_TYPE_P (type))
6948 {
6949 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6950 {
6951 if (complain & tf_error)
6952 error ("%qE is not a valid template argument for type %qT "
6953 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6954 return NULL_TREE;
6955 }
6956 return expr;
6957 }
6958 /* A template non-type parameter must be one of the above. */
6959 else
6960 gcc_unreachable ();
6961
6962 /* Sanity check: did we actually convert the argument to the
6963 right type? */
6964 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6965 (type, TREE_TYPE (expr)));
6966 return convert_from_reference (expr);
6967 }
6968
6969 /* Subroutine of coerce_template_template_parms, which returns 1 if
6970 PARM_PARM and ARG_PARM match using the rule for the template
6971 parameters of template template parameters. Both PARM and ARG are
6972 template parameters; the rest of the arguments are the same as for
6973 coerce_template_template_parms.
6974 */
6975 static int
6976 coerce_template_template_parm (tree parm,
6977 tree arg,
6978 tsubst_flags_t complain,
6979 tree in_decl,
6980 tree outer_args)
6981 {
6982 if (arg == NULL_TREE || error_operand_p (arg)
6983 || parm == NULL_TREE || error_operand_p (parm))
6984 return 0;
6985
6986 if (TREE_CODE (arg) != TREE_CODE (parm))
6987 return 0;
6988
6989 switch (TREE_CODE (parm))
6990 {
6991 case TEMPLATE_DECL:
6992 /* We encounter instantiations of templates like
6993 template <template <template <class> class> class TT>
6994 class C; */
6995 {
6996 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6997 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6998
6999 if (!coerce_template_template_parms
7000 (parmparm, argparm, complain, in_decl, outer_args))
7001 return 0;
7002 }
7003 /* Fall through. */
7004
7005 case TYPE_DECL:
7006 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7007 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7008 /* Argument is a parameter pack but parameter is not. */
7009 return 0;
7010 break;
7011
7012 case PARM_DECL:
7013 /* The tsubst call is used to handle cases such as
7014
7015 template <int> class C {};
7016 template <class T, template <T> class TT> class D {};
7017 D<int, C> d;
7018
7019 i.e. the parameter list of TT depends on earlier parameters. */
7020 if (!uses_template_parms (TREE_TYPE (arg)))
7021 {
7022 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7023 if (!uses_template_parms (t)
7024 && !same_type_p (t, TREE_TYPE (arg)))
7025 return 0;
7026 }
7027
7028 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7029 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7030 /* Argument is a parameter pack but parameter is not. */
7031 return 0;
7032
7033 break;
7034
7035 default:
7036 gcc_unreachable ();
7037 }
7038
7039 return 1;
7040 }
7041
7042 /* Coerce template argument list ARGLIST for use with template
7043 template-parameter TEMPL. */
7044
7045 static tree
7046 coerce_template_args_for_ttp (tree templ, tree arglist,
7047 tsubst_flags_t complain)
7048 {
7049 /* Consider an example where a template template parameter declared as
7050
7051 template <class T, class U = std::allocator<T> > class TT
7052
7053 The template parameter level of T and U are one level larger than
7054 of TT. To proper process the default argument of U, say when an
7055 instantiation `TT<int>' is seen, we need to build the full
7056 arguments containing {int} as the innermost level. Outer levels,
7057 available when not appearing as default template argument, can be
7058 obtained from the arguments of the enclosing template.
7059
7060 Suppose that TT is later substituted with std::vector. The above
7061 instantiation is `TT<int, std::allocator<T> >' with TT at
7062 level 1, and T at level 2, while the template arguments at level 1
7063 becomes {std::vector} and the inner level 2 is {int}. */
7064
7065 tree outer = DECL_CONTEXT (templ);
7066 if (outer)
7067 {
7068 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7069 /* We want arguments for the partial specialization, not arguments for
7070 the primary template. */
7071 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7072 else
7073 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7074 }
7075 else if (current_template_parms)
7076 {
7077 /* This is an argument of the current template, so we haven't set
7078 DECL_CONTEXT yet. */
7079 tree relevant_template_parms;
7080
7081 /* Parameter levels that are greater than the level of the given
7082 template template parm are irrelevant. */
7083 relevant_template_parms = current_template_parms;
7084 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7085 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7086 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7087
7088 outer = template_parms_to_args (relevant_template_parms);
7089 }
7090
7091 if (outer)
7092 arglist = add_to_template_args (outer, arglist);
7093
7094 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7095 return coerce_template_parms (parmlist, arglist, templ,
7096 complain,
7097 /*require_all_args=*/true,
7098 /*use_default_args=*/true);
7099 }
7100
7101 /* A cache of template template parameters with match-all default
7102 arguments. */
7103 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7104 static void
7105 store_defaulted_ttp (tree v, tree t)
7106 {
7107 if (!defaulted_ttp_cache)
7108 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7109 defaulted_ttp_cache->put (v, t);
7110 }
7111 static tree
7112 lookup_defaulted_ttp (tree v)
7113 {
7114 if (defaulted_ttp_cache)
7115 if (tree *p = defaulted_ttp_cache->get (v))
7116 return *p;
7117 return NULL_TREE;
7118 }
7119
7120 /* T is a bound template template-parameter. Copy its arguments into default
7121 arguments of the template template-parameter's template parameters. */
7122
7123 static tree
7124 add_defaults_to_ttp (tree otmpl)
7125 {
7126 if (tree c = lookup_defaulted_ttp (otmpl))
7127 return c;
7128
7129 tree ntmpl = copy_node (otmpl);
7130
7131 tree ntype = copy_node (TREE_TYPE (otmpl));
7132 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7133 TYPE_MAIN_VARIANT (ntype) = ntype;
7134 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7135 TYPE_NAME (ntype) = ntmpl;
7136 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7137
7138 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7139 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7140 TEMPLATE_PARM_DECL (idx) = ntmpl;
7141 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7142
7143 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7144 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7145 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7146 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7147 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7148 {
7149 tree o = TREE_VEC_ELT (vec, i);
7150 if (!template_parameter_pack_p (TREE_VALUE (o)))
7151 {
7152 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7153 TREE_PURPOSE (n) = any_targ_node;
7154 }
7155 }
7156
7157 store_defaulted_ttp (otmpl, ntmpl);
7158 return ntmpl;
7159 }
7160
7161 /* ARG is a bound potential template template-argument, and PARGS is a list
7162 of arguments for the corresponding template template-parameter. Adjust
7163 PARGS as appropriate for application to ARG's template, and if ARG is a
7164 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7165 arguments to the template template parameter. */
7166
7167 static tree
7168 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7169 {
7170 ++processing_template_decl;
7171 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7172 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7173 {
7174 /* When comparing two template template-parameters in partial ordering,
7175 rewrite the one currently being used as an argument to have default
7176 arguments for all parameters. */
7177 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7178 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7179 if (pargs != error_mark_node)
7180 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7181 TYPE_TI_ARGS (arg));
7182 }
7183 else
7184 {
7185 tree aparms
7186 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7187 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7188 /*require_all*/true,
7189 /*use_default*/true);
7190 }
7191 --processing_template_decl;
7192 return pargs;
7193 }
7194
7195 /* Subroutine of unify for the case when PARM is a
7196 BOUND_TEMPLATE_TEMPLATE_PARM. */
7197
7198 static int
7199 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7200 bool explain_p)
7201 {
7202 tree parmvec = TYPE_TI_ARGS (parm);
7203 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7204
7205 /* The template template parm might be variadic and the argument
7206 not, so flatten both argument lists. */
7207 parmvec = expand_template_argument_pack (parmvec);
7208 argvec = expand_template_argument_pack (argvec);
7209
7210 if (flag_new_ttp)
7211 {
7212 /* In keeping with P0522R0, adjust P's template arguments
7213 to apply to A's template; then flatten it again. */
7214 tree nparmvec = parmvec;
7215 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7216 nparmvec = expand_template_argument_pack (nparmvec);
7217
7218 if (unify (tparms, targs, nparmvec, argvec,
7219 UNIFY_ALLOW_NONE, explain_p))
7220 return 1;
7221
7222 /* If the P0522 adjustment eliminated a pack expansion, deduce
7223 empty packs. */
7224 if (flag_new_ttp
7225 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7226 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7227 DEDUCE_EXACT, /*sub*/true, explain_p))
7228 return 1;
7229 }
7230 else
7231 {
7232 /* Deduce arguments T, i from TT<T> or TT<i>.
7233 We check each element of PARMVEC and ARGVEC individually
7234 rather than the whole TREE_VEC since they can have
7235 different number of elements, which is allowed under N2555. */
7236
7237 int len = TREE_VEC_LENGTH (parmvec);
7238
7239 /* Check if the parameters end in a pack, making them
7240 variadic. */
7241 int parm_variadic_p = 0;
7242 if (len > 0
7243 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7244 parm_variadic_p = 1;
7245
7246 for (int i = 0; i < len - parm_variadic_p; ++i)
7247 /* If the template argument list of P contains a pack
7248 expansion that is not the last template argument, the
7249 entire template argument list is a non-deduced
7250 context. */
7251 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7252 return unify_success (explain_p);
7253
7254 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7255 return unify_too_few_arguments (explain_p,
7256 TREE_VEC_LENGTH (argvec), len);
7257
7258 for (int i = 0; i < len - parm_variadic_p; ++i)
7259 if (unify (tparms, targs,
7260 TREE_VEC_ELT (parmvec, i),
7261 TREE_VEC_ELT (argvec, i),
7262 UNIFY_ALLOW_NONE, explain_p))
7263 return 1;
7264
7265 if (parm_variadic_p
7266 && unify_pack_expansion (tparms, targs,
7267 parmvec, argvec,
7268 DEDUCE_EXACT,
7269 /*subr=*/true, explain_p))
7270 return 1;
7271 }
7272
7273 return 0;
7274 }
7275
7276 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7277 template template parameters. Both PARM_PARMS and ARG_PARMS are
7278 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7279 or PARM_DECL.
7280
7281 Consider the example:
7282 template <class T> class A;
7283 template<template <class U> class TT> class B;
7284
7285 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7286 the parameters to A, and OUTER_ARGS contains A. */
7287
7288 static int
7289 coerce_template_template_parms (tree parm_parms,
7290 tree arg_parms,
7291 tsubst_flags_t complain,
7292 tree in_decl,
7293 tree outer_args)
7294 {
7295 int nparms, nargs, i;
7296 tree parm, arg;
7297 int variadic_p = 0;
7298
7299 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7300 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7301
7302 nparms = TREE_VEC_LENGTH (parm_parms);
7303 nargs = TREE_VEC_LENGTH (arg_parms);
7304
7305 if (flag_new_ttp)
7306 {
7307 /* P0522R0: A template template-parameter P is at least as specialized as
7308 a template template-argument A if, given the following rewrite to two
7309 function templates, the function template corresponding to P is at
7310 least as specialized as the function template corresponding to A
7311 according to the partial ordering rules for function templates
7312 ([temp.func.order]). Given an invented class template X with the
7313 template parameter list of A (including default arguments):
7314
7315 * Each of the two function templates has the same template parameters,
7316 respectively, as P or A.
7317
7318 * Each function template has a single function parameter whose type is
7319 a specialization of X with template arguments corresponding to the
7320 template parameters from the respective function template where, for
7321 each template parameter PP in the template parameter list of the
7322 function template, a corresponding template argument AA is formed. If
7323 PP declares a parameter pack, then AA is the pack expansion
7324 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7325
7326 If the rewrite produces an invalid type, then P is not at least as
7327 specialized as A. */
7328
7329 /* So coerce P's args to apply to A's parms, and then deduce between A's
7330 args and the converted args. If that succeeds, A is at least as
7331 specialized as P, so they match.*/
7332 tree pargs = template_parms_level_to_args (parm_parms);
7333 ++processing_template_decl;
7334 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7335 /*require_all*/true, /*use_default*/true);
7336 --processing_template_decl;
7337 if (pargs != error_mark_node)
7338 {
7339 tree targs = make_tree_vec (nargs);
7340 tree aargs = template_parms_level_to_args (arg_parms);
7341 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7342 /*explain*/false))
7343 return 1;
7344 }
7345 }
7346
7347 /* Determine whether we have a parameter pack at the end of the
7348 template template parameter's template parameter list. */
7349 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7350 {
7351 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7352
7353 if (error_operand_p (parm))
7354 return 0;
7355
7356 switch (TREE_CODE (parm))
7357 {
7358 case TEMPLATE_DECL:
7359 case TYPE_DECL:
7360 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7361 variadic_p = 1;
7362 break;
7363
7364 case PARM_DECL:
7365 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7366 variadic_p = 1;
7367 break;
7368
7369 default:
7370 gcc_unreachable ();
7371 }
7372 }
7373
7374 if (nargs != nparms
7375 && !(variadic_p && nargs >= nparms - 1))
7376 return 0;
7377
7378 /* Check all of the template parameters except the parameter pack at
7379 the end (if any). */
7380 for (i = 0; i < nparms - variadic_p; ++i)
7381 {
7382 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7383 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7384 continue;
7385
7386 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7387 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7388
7389 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7390 outer_args))
7391 return 0;
7392
7393 }
7394
7395 if (variadic_p)
7396 {
7397 /* Check each of the template parameters in the template
7398 argument against the template parameter pack at the end of
7399 the template template parameter. */
7400 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7401 return 0;
7402
7403 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7404
7405 for (; i < nargs; ++i)
7406 {
7407 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7408 continue;
7409
7410 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7411
7412 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7413 outer_args))
7414 return 0;
7415 }
7416 }
7417
7418 return 1;
7419 }
7420
7421 /* Verifies that the deduced template arguments (in TARGS) for the
7422 template template parameters (in TPARMS) represent valid bindings,
7423 by comparing the template parameter list of each template argument
7424 to the template parameter list of its corresponding template
7425 template parameter, in accordance with DR150. This
7426 routine can only be called after all template arguments have been
7427 deduced. It will return TRUE if all of the template template
7428 parameter bindings are okay, FALSE otherwise. */
7429 bool
7430 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7431 {
7432 int i, ntparms = TREE_VEC_LENGTH (tparms);
7433 bool ret = true;
7434
7435 /* We're dealing with template parms in this process. */
7436 ++processing_template_decl;
7437
7438 targs = INNERMOST_TEMPLATE_ARGS (targs);
7439
7440 for (i = 0; i < ntparms; ++i)
7441 {
7442 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7443 tree targ = TREE_VEC_ELT (targs, i);
7444
7445 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7446 {
7447 tree packed_args = NULL_TREE;
7448 int idx, len = 1;
7449
7450 if (ARGUMENT_PACK_P (targ))
7451 {
7452 /* Look inside the argument pack. */
7453 packed_args = ARGUMENT_PACK_ARGS (targ);
7454 len = TREE_VEC_LENGTH (packed_args);
7455 }
7456
7457 for (idx = 0; idx < len; ++idx)
7458 {
7459 tree targ_parms = NULL_TREE;
7460
7461 if (packed_args)
7462 /* Extract the next argument from the argument
7463 pack. */
7464 targ = TREE_VEC_ELT (packed_args, idx);
7465
7466 if (PACK_EXPANSION_P (targ))
7467 /* Look at the pattern of the pack expansion. */
7468 targ = PACK_EXPANSION_PATTERN (targ);
7469
7470 /* Extract the template parameters from the template
7471 argument. */
7472 if (TREE_CODE (targ) == TEMPLATE_DECL)
7473 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7474 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7475 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7476
7477 /* Verify that we can coerce the template template
7478 parameters from the template argument to the template
7479 parameter. This requires an exact match. */
7480 if (targ_parms
7481 && !coerce_template_template_parms
7482 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7483 targ_parms,
7484 tf_none,
7485 tparm,
7486 targs))
7487 {
7488 ret = false;
7489 goto out;
7490 }
7491 }
7492 }
7493 }
7494
7495 out:
7496
7497 --processing_template_decl;
7498 return ret;
7499 }
7500
7501 /* Since type attributes aren't mangled, we need to strip them from
7502 template type arguments. */
7503
7504 static tree
7505 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7506 {
7507 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7508 return arg;
7509 bool removed_attributes = false;
7510 tree canon = strip_typedefs (arg, &removed_attributes);
7511 if (removed_attributes
7512 && (complain & tf_warning))
7513 warning (OPT_Wignored_attributes,
7514 "ignoring attributes on template argument %qT", arg);
7515 return canon;
7516 }
7517
7518 /* And from inside dependent non-type arguments like sizeof(Type). */
7519
7520 static tree
7521 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7522 {
7523 if (!arg || arg == error_mark_node)
7524 return arg;
7525 bool removed_attributes = false;
7526 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7527 if (removed_attributes
7528 && (complain & tf_warning))
7529 warning (OPT_Wignored_attributes,
7530 "ignoring attributes in template argument %qE", arg);
7531 return canon;
7532 }
7533
7534 // A template declaration can be substituted for a constrained
7535 // template template parameter only when the argument is more
7536 // constrained than the parameter.
7537 static bool
7538 is_compatible_template_arg (tree parm, tree arg)
7539 {
7540 tree parm_cons = get_constraints (parm);
7541
7542 /* For now, allow constrained template template arguments
7543 and unconstrained template template parameters. */
7544 if (parm_cons == NULL_TREE)
7545 return true;
7546
7547 tree arg_cons = get_constraints (arg);
7548
7549 // If the template parameter is constrained, we need to rewrite its
7550 // constraints in terms of the ARG's template parameters. This ensures
7551 // that all of the template parameter types will have the same depth.
7552 //
7553 // Note that this is only valid when coerce_template_template_parm is
7554 // true for the innermost template parameters of PARM and ARG. In other
7555 // words, because coercion is successful, this conversion will be valid.
7556 if (parm_cons)
7557 {
7558 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7559 parm_cons = tsubst_constraint_info (parm_cons,
7560 INNERMOST_TEMPLATE_ARGS (args),
7561 tf_none, NULL_TREE);
7562 if (parm_cons == error_mark_node)
7563 return false;
7564 }
7565
7566 return subsumes (parm_cons, arg_cons);
7567 }
7568
7569 // Convert a placeholder argument into a binding to the original
7570 // parameter. The original parameter is saved as the TREE_TYPE of
7571 // ARG.
7572 static inline tree
7573 convert_wildcard_argument (tree parm, tree arg)
7574 {
7575 TREE_TYPE (arg) = parm;
7576 return arg;
7577 }
7578
7579 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7580 because one of them is dependent. But we need to represent the
7581 conversion for the benefit of cp_tree_equal. */
7582
7583 static tree
7584 maybe_convert_nontype_argument (tree type, tree arg)
7585 {
7586 /* Auto parms get no conversion. */
7587 if (type_uses_auto (type))
7588 return arg;
7589 /* We don't need or want to add this conversion now if we're going to use the
7590 argument for deduction. */
7591 if (value_dependent_expression_p (arg))
7592 return arg;
7593
7594 type = cv_unqualified (type);
7595 tree argtype = TREE_TYPE (arg);
7596 if (same_type_p (type, argtype))
7597 return arg;
7598
7599 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7600 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7601 return arg;
7602 }
7603
7604 /* Convert the indicated template ARG as necessary to match the
7605 indicated template PARM. Returns the converted ARG, or
7606 error_mark_node if the conversion was unsuccessful. Error and
7607 warning messages are issued under control of COMPLAIN. This
7608 conversion is for the Ith parameter in the parameter list. ARGS is
7609 the full set of template arguments deduced so far. */
7610
7611 static tree
7612 convert_template_argument (tree parm,
7613 tree arg,
7614 tree args,
7615 tsubst_flags_t complain,
7616 int i,
7617 tree in_decl)
7618 {
7619 tree orig_arg;
7620 tree val;
7621 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7622
7623 if (parm == error_mark_node)
7624 return error_mark_node;
7625
7626 /* Trivially convert placeholders. */
7627 if (TREE_CODE (arg) == WILDCARD_DECL)
7628 return convert_wildcard_argument (parm, arg);
7629
7630 if (arg == any_targ_node)
7631 return arg;
7632
7633 if (TREE_CODE (arg) == TREE_LIST
7634 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7635 {
7636 /* The template argument was the name of some
7637 member function. That's usually
7638 invalid, but static members are OK. In any
7639 case, grab the underlying fields/functions
7640 and issue an error later if required. */
7641 orig_arg = TREE_VALUE (arg);
7642 TREE_TYPE (arg) = unknown_type_node;
7643 }
7644
7645 orig_arg = arg;
7646
7647 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7648 requires_type = (TREE_CODE (parm) == TYPE_DECL
7649 || requires_tmpl_type);
7650
7651 /* When determining whether an argument pack expansion is a template,
7652 look at the pattern. */
7653 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7654 arg = PACK_EXPANSION_PATTERN (arg);
7655
7656 /* Deal with an injected-class-name used as a template template arg. */
7657 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7658 {
7659 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7660 if (TREE_CODE (t) == TEMPLATE_DECL)
7661 {
7662 if (cxx_dialect >= cxx11)
7663 /* OK under DR 1004. */;
7664 else if (complain & tf_warning_or_error)
7665 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7666 " used as template template argument", TYPE_NAME (arg));
7667 else if (flag_pedantic_errors)
7668 t = arg;
7669
7670 arg = t;
7671 }
7672 }
7673
7674 is_tmpl_type =
7675 ((TREE_CODE (arg) == TEMPLATE_DECL
7676 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7677 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7678 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7679 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7680
7681 if (is_tmpl_type
7682 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7683 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7684 arg = TYPE_STUB_DECL (arg);
7685
7686 is_type = TYPE_P (arg) || is_tmpl_type;
7687
7688 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7689 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7690 {
7691 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7692 {
7693 if (complain & tf_error)
7694 error ("invalid use of destructor %qE as a type", orig_arg);
7695 return error_mark_node;
7696 }
7697
7698 permerror (input_location,
7699 "to refer to a type member of a template parameter, "
7700 "use %<typename %E%>", orig_arg);
7701
7702 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7703 TREE_OPERAND (arg, 1),
7704 typename_type,
7705 complain);
7706 arg = orig_arg;
7707 is_type = 1;
7708 }
7709 if (is_type != requires_type)
7710 {
7711 if (in_decl)
7712 {
7713 if (complain & tf_error)
7714 {
7715 error ("type/value mismatch at argument %d in template "
7716 "parameter list for %qD",
7717 i + 1, in_decl);
7718 if (is_type)
7719 inform (input_location,
7720 " expected a constant of type %qT, got %qT",
7721 TREE_TYPE (parm),
7722 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7723 else if (requires_tmpl_type)
7724 inform (input_location,
7725 " expected a class template, got %qE", orig_arg);
7726 else
7727 inform (input_location,
7728 " expected a type, got %qE", orig_arg);
7729 }
7730 }
7731 return error_mark_node;
7732 }
7733 if (is_tmpl_type ^ requires_tmpl_type)
7734 {
7735 if (in_decl && (complain & tf_error))
7736 {
7737 error ("type/value mismatch at argument %d in template "
7738 "parameter list for %qD",
7739 i + 1, in_decl);
7740 if (is_tmpl_type)
7741 inform (input_location,
7742 " expected a type, got %qT", DECL_NAME (arg));
7743 else
7744 inform (input_location,
7745 " expected a class template, got %qT", orig_arg);
7746 }
7747 return error_mark_node;
7748 }
7749
7750 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7751 /* We already did the appropriate conversion when packing args. */
7752 val = orig_arg;
7753 else if (is_type)
7754 {
7755 if (requires_tmpl_type)
7756 {
7757 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7758 /* The number of argument required is not known yet.
7759 Just accept it for now. */
7760 val = orig_arg;
7761 else
7762 {
7763 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7764 tree argparm;
7765
7766 /* Strip alias templates that are equivalent to another
7767 template. */
7768 arg = get_underlying_template (arg);
7769 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7770
7771 if (coerce_template_template_parms (parmparm, argparm,
7772 complain, in_decl,
7773 args))
7774 {
7775 val = arg;
7776
7777 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7778 TEMPLATE_DECL. */
7779 if (val != error_mark_node)
7780 {
7781 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7782 val = TREE_TYPE (val);
7783 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7784 val = make_pack_expansion (val, complain);
7785 }
7786 }
7787 else
7788 {
7789 if (in_decl && (complain & tf_error))
7790 {
7791 error ("type/value mismatch at argument %d in "
7792 "template parameter list for %qD",
7793 i + 1, in_decl);
7794 inform (input_location,
7795 " expected a template of type %qD, got %qT",
7796 parm, orig_arg);
7797 }
7798
7799 val = error_mark_node;
7800 }
7801
7802 // Check that the constraints are compatible before allowing the
7803 // substitution.
7804 if (val != error_mark_node)
7805 if (!is_compatible_template_arg (parm, arg))
7806 {
7807 if (in_decl && (complain & tf_error))
7808 {
7809 error ("constraint mismatch at argument %d in "
7810 "template parameter list for %qD",
7811 i + 1, in_decl);
7812 inform (input_location, " expected %qD but got %qD",
7813 parm, arg);
7814 }
7815 val = error_mark_node;
7816 }
7817 }
7818 }
7819 else
7820 val = orig_arg;
7821 /* We only form one instance of each template specialization.
7822 Therefore, if we use a non-canonical variant (i.e., a
7823 typedef), any future messages referring to the type will use
7824 the typedef, which is confusing if those future uses do not
7825 themselves also use the typedef. */
7826 if (TYPE_P (val))
7827 val = canonicalize_type_argument (val, complain);
7828 }
7829 else
7830 {
7831 tree t = TREE_TYPE (parm);
7832
7833 if (tree a = type_uses_auto (t))
7834 {
7835 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7836 if (t == error_mark_node)
7837 return error_mark_node;
7838 }
7839 else
7840 t = tsubst (t, args, complain, in_decl);
7841
7842 if (invalid_nontype_parm_type_p (t, complain))
7843 return error_mark_node;
7844
7845 if (!type_dependent_expression_p (orig_arg)
7846 && !uses_template_parms (t))
7847 /* We used to call digest_init here. However, digest_init
7848 will report errors, which we don't want when complain
7849 is zero. More importantly, digest_init will try too
7850 hard to convert things: for example, `0' should not be
7851 converted to pointer type at this point according to
7852 the standard. Accepting this is not merely an
7853 extension, since deciding whether or not these
7854 conversions can occur is part of determining which
7855 function template to call, or whether a given explicit
7856 argument specification is valid. */
7857 val = convert_nontype_argument (t, orig_arg, complain);
7858 else
7859 {
7860 val = canonicalize_expr_argument (orig_arg, complain);
7861 val = maybe_convert_nontype_argument (t, val);
7862 }
7863
7864
7865 if (val == NULL_TREE)
7866 val = error_mark_node;
7867 else if (val == error_mark_node && (complain & tf_error))
7868 error ("could not convert template argument %qE from %qT to %qT",
7869 orig_arg, TREE_TYPE (orig_arg), t);
7870
7871 if (INDIRECT_REF_P (val))
7872 {
7873 /* Reject template arguments that are references to built-in
7874 functions with no library fallbacks. */
7875 const_tree inner = TREE_OPERAND (val, 0);
7876 const_tree innertype = TREE_TYPE (inner);
7877 if (innertype
7878 && TREE_CODE (innertype) == REFERENCE_TYPE
7879 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7880 && TREE_OPERAND_LENGTH (inner) > 0
7881 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7882 return error_mark_node;
7883 }
7884
7885 if (TREE_CODE (val) == SCOPE_REF)
7886 {
7887 /* Strip typedefs from the SCOPE_REF. */
7888 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7889 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7890 complain);
7891 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7892 QUALIFIED_NAME_IS_TEMPLATE (val));
7893 }
7894 }
7895
7896 return val;
7897 }
7898
7899 /* Coerces the remaining template arguments in INNER_ARGS (from
7900 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7901 Returns the coerced argument pack. PARM_IDX is the position of this
7902 parameter in the template parameter list. ARGS is the original
7903 template argument list. */
7904 static tree
7905 coerce_template_parameter_pack (tree parms,
7906 int parm_idx,
7907 tree args,
7908 tree inner_args,
7909 int arg_idx,
7910 tree new_args,
7911 int* lost,
7912 tree in_decl,
7913 tsubst_flags_t complain)
7914 {
7915 tree parm = TREE_VEC_ELT (parms, parm_idx);
7916 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7917 tree packed_args;
7918 tree argument_pack;
7919 tree packed_parms = NULL_TREE;
7920
7921 if (arg_idx > nargs)
7922 arg_idx = nargs;
7923
7924 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7925 {
7926 /* When the template parameter is a non-type template parameter pack
7927 or template template parameter pack whose type or template
7928 parameters use parameter packs, we know exactly how many arguments
7929 we are looking for. Build a vector of the instantiated decls for
7930 these template parameters in PACKED_PARMS. */
7931 /* We can't use make_pack_expansion here because it would interpret a
7932 _DECL as a use rather than a declaration. */
7933 tree decl = TREE_VALUE (parm);
7934 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7935 SET_PACK_EXPANSION_PATTERN (exp, decl);
7936 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7937 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7938
7939 TREE_VEC_LENGTH (args)--;
7940 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7941 TREE_VEC_LENGTH (args)++;
7942
7943 if (packed_parms == error_mark_node)
7944 return error_mark_node;
7945
7946 /* If we're doing a partial instantiation of a member template,
7947 verify that all of the types used for the non-type
7948 template parameter pack are, in fact, valid for non-type
7949 template parameters. */
7950 if (arg_idx < nargs
7951 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7952 {
7953 int j, len = TREE_VEC_LENGTH (packed_parms);
7954 for (j = 0; j < len; ++j)
7955 {
7956 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7957 if (invalid_nontype_parm_type_p (t, complain))
7958 return error_mark_node;
7959 }
7960 /* We don't know how many args we have yet, just
7961 use the unconverted ones for now. */
7962 return NULL_TREE;
7963 }
7964
7965 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7966 }
7967 /* Check if we have a placeholder pack, which indicates we're
7968 in the context of a introduction list. In that case we want
7969 to match this pack to the single placeholder. */
7970 else if (arg_idx < nargs
7971 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7972 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7973 {
7974 nargs = arg_idx + 1;
7975 packed_args = make_tree_vec (1);
7976 }
7977 else
7978 packed_args = make_tree_vec (nargs - arg_idx);
7979
7980 /* Convert the remaining arguments, which will be a part of the
7981 parameter pack "parm". */
7982 int first_pack_arg = arg_idx;
7983 for (; arg_idx < nargs; ++arg_idx)
7984 {
7985 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7986 tree actual_parm = TREE_VALUE (parm);
7987 int pack_idx = arg_idx - first_pack_arg;
7988
7989 if (packed_parms)
7990 {
7991 /* Once we've packed as many args as we have types, stop. */
7992 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7993 break;
7994 else if (PACK_EXPANSION_P (arg))
7995 /* We don't know how many args we have yet, just
7996 use the unconverted ones for now. */
7997 return NULL_TREE;
7998 else
7999 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8000 }
8001
8002 if (arg == error_mark_node)
8003 {
8004 if (complain & tf_error)
8005 error ("template argument %d is invalid", arg_idx + 1);
8006 }
8007 else
8008 arg = convert_template_argument (actual_parm,
8009 arg, new_args, complain, parm_idx,
8010 in_decl);
8011 if (arg == error_mark_node)
8012 (*lost)++;
8013 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8014 }
8015
8016 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8017 && TREE_VEC_LENGTH (packed_args) > 0)
8018 {
8019 if (complain & tf_error)
8020 error ("wrong number of template arguments (%d, should be %d)",
8021 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8022 return error_mark_node;
8023 }
8024
8025 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8026 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8027 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8028 else
8029 {
8030 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8031 TREE_CONSTANT (argument_pack) = 1;
8032 }
8033
8034 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8035 if (CHECKING_P)
8036 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8037 TREE_VEC_LENGTH (packed_args));
8038 return argument_pack;
8039 }
8040
8041 /* Returns the number of pack expansions in the template argument vector
8042 ARGS. */
8043
8044 static int
8045 pack_expansion_args_count (tree args)
8046 {
8047 int i;
8048 int count = 0;
8049 if (args)
8050 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8051 {
8052 tree elt = TREE_VEC_ELT (args, i);
8053 if (elt && PACK_EXPANSION_P (elt))
8054 ++count;
8055 }
8056 return count;
8057 }
8058
8059 /* Convert all template arguments to their appropriate types, and
8060 return a vector containing the innermost resulting template
8061 arguments. If any error occurs, return error_mark_node. Error and
8062 warning messages are issued under control of COMPLAIN.
8063
8064 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8065 for arguments not specified in ARGS. Otherwise, if
8066 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8067 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8068 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8069 ARGS. */
8070
8071 static tree
8072 coerce_template_parms (tree parms,
8073 tree args,
8074 tree in_decl,
8075 tsubst_flags_t complain,
8076 bool require_all_args,
8077 bool use_default_args)
8078 {
8079 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8080 tree orig_inner_args;
8081 tree inner_args;
8082 tree new_args;
8083 tree new_inner_args;
8084 int saved_unevaluated_operand;
8085 int saved_inhibit_evaluation_warnings;
8086
8087 /* When used as a boolean value, indicates whether this is a
8088 variadic template parameter list. Since it's an int, we can also
8089 subtract it from nparms to get the number of non-variadic
8090 parameters. */
8091 int variadic_p = 0;
8092 int variadic_args_p = 0;
8093 int post_variadic_parms = 0;
8094
8095 /* Likewise for parameters with default arguments. */
8096 int default_p = 0;
8097
8098 if (args == error_mark_node)
8099 return error_mark_node;
8100
8101 nparms = TREE_VEC_LENGTH (parms);
8102
8103 /* Determine if there are any parameter packs or default arguments. */
8104 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8105 {
8106 tree parm = TREE_VEC_ELT (parms, parm_idx);
8107 if (variadic_p)
8108 ++post_variadic_parms;
8109 if (template_parameter_pack_p (TREE_VALUE (parm)))
8110 ++variadic_p;
8111 if (TREE_PURPOSE (parm))
8112 ++default_p;
8113 }
8114
8115 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8116 /* If there are no parameters that follow a parameter pack, we need to
8117 expand any argument packs so that we can deduce a parameter pack from
8118 some non-packed args followed by an argument pack, as in variadic85.C.
8119 If there are such parameters, we need to leave argument packs intact
8120 so the arguments are assigned properly. This can happen when dealing
8121 with a nested class inside a partial specialization of a class
8122 template, as in variadic92.C, or when deducing a template parameter pack
8123 from a sub-declarator, as in variadic114.C. */
8124 if (!post_variadic_parms)
8125 inner_args = expand_template_argument_pack (inner_args);
8126
8127 /* Count any pack expansion args. */
8128 variadic_args_p = pack_expansion_args_count (inner_args);
8129
8130 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8131 if ((nargs - variadic_args_p > nparms && !variadic_p)
8132 || (nargs < nparms - variadic_p
8133 && require_all_args
8134 && !variadic_args_p
8135 && (!use_default_args
8136 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8137 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8138 {
8139 if (complain & tf_error)
8140 {
8141 if (variadic_p || default_p)
8142 {
8143 nparms -= variadic_p + default_p;
8144 error ("wrong number of template arguments "
8145 "(%d, should be at least %d)", nargs, nparms);
8146 }
8147 else
8148 error ("wrong number of template arguments "
8149 "(%d, should be %d)", nargs, nparms);
8150
8151 if (in_decl)
8152 inform (DECL_SOURCE_LOCATION (in_decl),
8153 "provided for %qD", in_decl);
8154 }
8155
8156 return error_mark_node;
8157 }
8158 /* We can't pass a pack expansion to a non-pack parameter of an alias
8159 template (DR 1430). */
8160 else if (in_decl
8161 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8162 || concept_template_p (in_decl))
8163 && variadic_args_p
8164 && nargs - variadic_args_p < nparms - variadic_p)
8165 {
8166 if (complain & tf_error)
8167 {
8168 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8169 {
8170 tree arg = TREE_VEC_ELT (inner_args, i);
8171 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8172
8173 if (PACK_EXPANSION_P (arg)
8174 && !template_parameter_pack_p (parm))
8175 {
8176 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8177 error_at (location_of (arg),
8178 "pack expansion argument for non-pack parameter "
8179 "%qD of alias template %qD", parm, in_decl);
8180 else
8181 error_at (location_of (arg),
8182 "pack expansion argument for non-pack parameter "
8183 "%qD of concept %qD", parm, in_decl);
8184 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8185 goto found;
8186 }
8187 }
8188 gcc_unreachable ();
8189 found:;
8190 }
8191 return error_mark_node;
8192 }
8193
8194 /* We need to evaluate the template arguments, even though this
8195 template-id may be nested within a "sizeof". */
8196 saved_unevaluated_operand = cp_unevaluated_operand;
8197 cp_unevaluated_operand = 0;
8198 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8199 c_inhibit_evaluation_warnings = 0;
8200 new_inner_args = make_tree_vec (nparms);
8201 new_args = add_outermost_template_args (args, new_inner_args);
8202 int pack_adjust = 0;
8203 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8204 {
8205 tree arg;
8206 tree parm;
8207
8208 /* Get the Ith template parameter. */
8209 parm = TREE_VEC_ELT (parms, parm_idx);
8210
8211 if (parm == error_mark_node)
8212 {
8213 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8214 continue;
8215 }
8216
8217 /* Calculate the next argument. */
8218 if (arg_idx < nargs)
8219 arg = TREE_VEC_ELT (inner_args, arg_idx);
8220 else
8221 arg = NULL_TREE;
8222
8223 if (template_parameter_pack_p (TREE_VALUE (parm))
8224 && !(arg && ARGUMENT_PACK_P (arg)))
8225 {
8226 /* Some arguments will be placed in the
8227 template parameter pack PARM. */
8228 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8229 inner_args, arg_idx,
8230 new_args, &lost,
8231 in_decl, complain);
8232
8233 if (arg == NULL_TREE)
8234 {
8235 /* We don't know how many args we have yet, just use the
8236 unconverted (and still packed) ones for now. */
8237 new_inner_args = orig_inner_args;
8238 arg_idx = nargs;
8239 break;
8240 }
8241
8242 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8243
8244 /* Store this argument. */
8245 if (arg == error_mark_node)
8246 {
8247 lost++;
8248 /* We are done with all of the arguments. */
8249 arg_idx = nargs;
8250 }
8251 else
8252 {
8253 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8254 arg_idx += pack_adjust;
8255 }
8256
8257 continue;
8258 }
8259 else if (arg)
8260 {
8261 if (PACK_EXPANSION_P (arg))
8262 {
8263 /* "If every valid specialization of a variadic template
8264 requires an empty template parameter pack, the template is
8265 ill-formed, no diagnostic required." So check that the
8266 pattern works with this parameter. */
8267 tree pattern = PACK_EXPANSION_PATTERN (arg);
8268 tree conv = convert_template_argument (TREE_VALUE (parm),
8269 pattern, new_args,
8270 complain, parm_idx,
8271 in_decl);
8272 if (conv == error_mark_node)
8273 {
8274 if (complain & tf_error)
8275 inform (input_location, "so any instantiation with a "
8276 "non-empty parameter pack would be ill-formed");
8277 ++lost;
8278 }
8279 else if (TYPE_P (conv) && !TYPE_P (pattern))
8280 /* Recover from missing typename. */
8281 TREE_VEC_ELT (inner_args, arg_idx)
8282 = make_pack_expansion (conv, complain);
8283
8284 /* We don't know how many args we have yet, just
8285 use the unconverted ones for now. */
8286 new_inner_args = inner_args;
8287 arg_idx = nargs;
8288 break;
8289 }
8290 }
8291 else if (require_all_args)
8292 {
8293 /* There must be a default arg in this case. */
8294 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8295 complain, in_decl);
8296 /* The position of the first default template argument,
8297 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8298 Record that. */
8299 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8300 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8301 arg_idx - pack_adjust);
8302 }
8303 else
8304 break;
8305
8306 if (arg == error_mark_node)
8307 {
8308 if (complain & tf_error)
8309 error ("template argument %d is invalid", arg_idx + 1);
8310 }
8311 else if (!arg)
8312 /* This only occurs if there was an error in the template
8313 parameter list itself (which we would already have
8314 reported) that we are trying to recover from, e.g., a class
8315 template with a parameter list such as
8316 template<typename..., typename>. */
8317 ++lost;
8318 else
8319 arg = convert_template_argument (TREE_VALUE (parm),
8320 arg, new_args, complain,
8321 parm_idx, in_decl);
8322
8323 if (arg == error_mark_node)
8324 lost++;
8325 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8326 }
8327 cp_unevaluated_operand = saved_unevaluated_operand;
8328 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8329
8330 if (variadic_p && arg_idx < nargs)
8331 {
8332 if (complain & tf_error)
8333 {
8334 error ("wrong number of template arguments "
8335 "(%d, should be %d)", nargs, arg_idx);
8336 if (in_decl)
8337 error ("provided for %q+D", in_decl);
8338 }
8339 return error_mark_node;
8340 }
8341
8342 if (lost)
8343 return error_mark_node;
8344
8345 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8346 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8347 TREE_VEC_LENGTH (new_inner_args));
8348
8349 return new_inner_args;
8350 }
8351
8352 /* Convert all template arguments to their appropriate types, and
8353 return a vector containing the innermost resulting template
8354 arguments. If any error occurs, return error_mark_node. Error and
8355 warning messages are not issued.
8356
8357 Note that no function argument deduction is performed, and default
8358 arguments are used to fill in unspecified arguments. */
8359 tree
8360 coerce_template_parms (tree parms, tree args, tree in_decl)
8361 {
8362 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8363 }
8364
8365 /* Convert all template arguments to their appropriate type, and
8366 instantiate default arguments as needed. This returns a vector
8367 containing the innermost resulting template arguments, or
8368 error_mark_node if unsuccessful. */
8369 tree
8370 coerce_template_parms (tree parms, tree args, tree in_decl,
8371 tsubst_flags_t complain)
8372 {
8373 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8374 }
8375
8376 /* Like coerce_template_parms. If PARMS represents all template
8377 parameters levels, this function returns a vector of vectors
8378 representing all the resulting argument levels. Note that in this
8379 case, only the innermost arguments are coerced because the
8380 outermost ones are supposed to have been coerced already.
8381
8382 Otherwise, if PARMS represents only (the innermost) vector of
8383 parameters, this function returns a vector containing just the
8384 innermost resulting arguments. */
8385
8386 static tree
8387 coerce_innermost_template_parms (tree parms,
8388 tree args,
8389 tree in_decl,
8390 tsubst_flags_t complain,
8391 bool require_all_args,
8392 bool use_default_args)
8393 {
8394 int parms_depth = TMPL_PARMS_DEPTH (parms);
8395 int args_depth = TMPL_ARGS_DEPTH (args);
8396 tree coerced_args;
8397
8398 if (parms_depth > 1)
8399 {
8400 coerced_args = make_tree_vec (parms_depth);
8401 tree level;
8402 int cur_depth;
8403
8404 for (level = parms, cur_depth = parms_depth;
8405 parms_depth > 0 && level != NULL_TREE;
8406 level = TREE_CHAIN (level), --cur_depth)
8407 {
8408 tree l;
8409 if (cur_depth == args_depth)
8410 l = coerce_template_parms (TREE_VALUE (level),
8411 args, in_decl, complain,
8412 require_all_args,
8413 use_default_args);
8414 else
8415 l = TMPL_ARGS_LEVEL (args, cur_depth);
8416
8417 if (l == error_mark_node)
8418 return error_mark_node;
8419
8420 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8421 }
8422 }
8423 else
8424 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8425 args, in_decl, complain,
8426 require_all_args,
8427 use_default_args);
8428 return coerced_args;
8429 }
8430
8431 /* Returns 1 if template args OT and NT are equivalent. */
8432
8433 int
8434 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8435 {
8436 if (nt == ot)
8437 return 1;
8438 if (nt == NULL_TREE || ot == NULL_TREE)
8439 return false;
8440 if (nt == any_targ_node || ot == any_targ_node)
8441 return true;
8442
8443 if (TREE_CODE (nt) == TREE_VEC)
8444 /* For member templates */
8445 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8446 else if (PACK_EXPANSION_P (ot))
8447 return (PACK_EXPANSION_P (nt)
8448 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8449 PACK_EXPANSION_PATTERN (nt))
8450 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8451 PACK_EXPANSION_EXTRA_ARGS (nt)));
8452 else if (ARGUMENT_PACK_P (ot))
8453 {
8454 int i, len;
8455 tree opack, npack;
8456
8457 if (!ARGUMENT_PACK_P (nt))
8458 return 0;
8459
8460 opack = ARGUMENT_PACK_ARGS (ot);
8461 npack = ARGUMENT_PACK_ARGS (nt);
8462 len = TREE_VEC_LENGTH (opack);
8463 if (TREE_VEC_LENGTH (npack) != len)
8464 return 0;
8465 for (i = 0; i < len; ++i)
8466 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8467 TREE_VEC_ELT (npack, i)))
8468 return 0;
8469 return 1;
8470 }
8471 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8472 gcc_unreachable ();
8473 else if (TYPE_P (nt))
8474 {
8475 if (!TYPE_P (ot))
8476 return false;
8477 /* Don't treat an alias template specialization with dependent
8478 arguments as equivalent to its underlying type when used as a
8479 template argument; we need them to be distinct so that we
8480 substitute into the specialization arguments at instantiation
8481 time. And aliases can't be equivalent without being ==, so
8482 we don't need to look any deeper.
8483
8484 During partial ordering, however, we need to treat them normally so
8485 that we can order uses of the same alias with different
8486 cv-qualification (79960). */
8487 if (!partial_order
8488 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8489 return false;
8490 else
8491 return same_type_p (ot, nt);
8492 }
8493 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8494 return 0;
8495 else
8496 {
8497 /* Try to treat a template non-type argument that has been converted
8498 to the parameter type as equivalent to one that hasn't yet. */
8499 for (enum tree_code code1 = TREE_CODE (ot);
8500 CONVERT_EXPR_CODE_P (code1)
8501 || code1 == NON_LVALUE_EXPR;
8502 code1 = TREE_CODE (ot))
8503 ot = TREE_OPERAND (ot, 0);
8504 for (enum tree_code code2 = TREE_CODE (nt);
8505 CONVERT_EXPR_CODE_P (code2)
8506 || code2 == NON_LVALUE_EXPR;
8507 code2 = TREE_CODE (nt))
8508 nt = TREE_OPERAND (nt, 0);
8509
8510 return cp_tree_equal (ot, nt);
8511 }
8512 }
8513
8514 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8515 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8516 NEWARG_PTR with the offending arguments if they are non-NULL. */
8517
8518 int
8519 comp_template_args (tree oldargs, tree newargs,
8520 tree *oldarg_ptr, tree *newarg_ptr,
8521 bool partial_order)
8522 {
8523 int i;
8524
8525 if (oldargs == newargs)
8526 return 1;
8527
8528 if (!oldargs || !newargs)
8529 return 0;
8530
8531 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8532 return 0;
8533
8534 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8535 {
8536 tree nt = TREE_VEC_ELT (newargs, i);
8537 tree ot = TREE_VEC_ELT (oldargs, i);
8538
8539 if (! template_args_equal (ot, nt, partial_order))
8540 {
8541 if (oldarg_ptr != NULL)
8542 *oldarg_ptr = ot;
8543 if (newarg_ptr != NULL)
8544 *newarg_ptr = nt;
8545 return 0;
8546 }
8547 }
8548 return 1;
8549 }
8550
8551 inline bool
8552 comp_template_args_porder (tree oargs, tree nargs)
8553 {
8554 return comp_template_args (oargs, nargs, NULL, NULL, true);
8555 }
8556
8557 static void
8558 add_pending_template (tree d)
8559 {
8560 tree ti = (TYPE_P (d)
8561 ? CLASSTYPE_TEMPLATE_INFO (d)
8562 : DECL_TEMPLATE_INFO (d));
8563 struct pending_template *pt;
8564 int level;
8565
8566 if (TI_PENDING_TEMPLATE_FLAG (ti))
8567 return;
8568
8569 /* We are called both from instantiate_decl, where we've already had a
8570 tinst_level pushed, and instantiate_template, where we haven't.
8571 Compensate. */
8572 level = !current_tinst_level || current_tinst_level->decl != d;
8573
8574 if (level)
8575 push_tinst_level (d);
8576
8577 pt = ggc_alloc<pending_template> ();
8578 pt->next = NULL;
8579 pt->tinst = current_tinst_level;
8580 if (last_pending_template)
8581 last_pending_template->next = pt;
8582 else
8583 pending_templates = pt;
8584
8585 last_pending_template = pt;
8586
8587 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8588
8589 if (level)
8590 pop_tinst_level ();
8591 }
8592
8593
8594 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8595 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8596 documentation for TEMPLATE_ID_EXPR. */
8597
8598 tree
8599 lookup_template_function (tree fns, tree arglist)
8600 {
8601 tree type;
8602
8603 if (fns == error_mark_node || arglist == error_mark_node)
8604 return error_mark_node;
8605
8606 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8607
8608 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8609 {
8610 error ("%q#D is not a function template", fns);
8611 return error_mark_node;
8612 }
8613
8614 if (BASELINK_P (fns))
8615 {
8616 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8617 unknown_type_node,
8618 BASELINK_FUNCTIONS (fns),
8619 arglist);
8620 return fns;
8621 }
8622
8623 type = TREE_TYPE (fns);
8624 if (TREE_CODE (fns) == OVERLOAD || !type)
8625 type = unknown_type_node;
8626
8627 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8628 }
8629
8630 /* Within the scope of a template class S<T>, the name S gets bound
8631 (in build_self_reference) to a TYPE_DECL for the class, not a
8632 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8633 or one of its enclosing classes, and that type is a template,
8634 return the associated TEMPLATE_DECL. Otherwise, the original
8635 DECL is returned.
8636
8637 Also handle the case when DECL is a TREE_LIST of ambiguous
8638 injected-class-names from different bases. */
8639
8640 tree
8641 maybe_get_template_decl_from_type_decl (tree decl)
8642 {
8643 if (decl == NULL_TREE)
8644 return decl;
8645
8646 /* DR 176: A lookup that finds an injected-class-name (10.2
8647 [class.member.lookup]) can result in an ambiguity in certain cases
8648 (for example, if it is found in more than one base class). If all of
8649 the injected-class-names that are found refer to specializations of
8650 the same class template, and if the name is followed by a
8651 template-argument-list, the reference refers to the class template
8652 itself and not a specialization thereof, and is not ambiguous. */
8653 if (TREE_CODE (decl) == TREE_LIST)
8654 {
8655 tree t, tmpl = NULL_TREE;
8656 for (t = decl; t; t = TREE_CHAIN (t))
8657 {
8658 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8659 if (!tmpl)
8660 tmpl = elt;
8661 else if (tmpl != elt)
8662 break;
8663 }
8664 if (tmpl && t == NULL_TREE)
8665 return tmpl;
8666 else
8667 return decl;
8668 }
8669
8670 return (decl != NULL_TREE
8671 && DECL_SELF_REFERENCE_P (decl)
8672 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8673 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8674 }
8675
8676 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8677 parameters, find the desired type.
8678
8679 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8680
8681 IN_DECL, if non-NULL, is the template declaration we are trying to
8682 instantiate.
8683
8684 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8685 the class we are looking up.
8686
8687 Issue error and warning messages under control of COMPLAIN.
8688
8689 If the template class is really a local class in a template
8690 function, then the FUNCTION_CONTEXT is the function in which it is
8691 being instantiated.
8692
8693 ??? Note that this function is currently called *twice* for each
8694 template-id: the first time from the parser, while creating the
8695 incomplete type (finish_template_type), and the second type during the
8696 real instantiation (instantiate_template_class). This is surely something
8697 that we want to avoid. It also causes some problems with argument
8698 coercion (see convert_nontype_argument for more information on this). */
8699
8700 static tree
8701 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8702 int entering_scope, tsubst_flags_t complain)
8703 {
8704 tree templ = NULL_TREE, parmlist;
8705 tree t;
8706 spec_entry **slot;
8707 spec_entry *entry;
8708 spec_entry elt;
8709 hashval_t hash;
8710
8711 if (identifier_p (d1))
8712 {
8713 tree value = innermost_non_namespace_value (d1);
8714 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8715 templ = value;
8716 else
8717 {
8718 if (context)
8719 push_decl_namespace (context);
8720 templ = lookup_name (d1);
8721 templ = maybe_get_template_decl_from_type_decl (templ);
8722 if (context)
8723 pop_decl_namespace ();
8724 }
8725 if (templ)
8726 context = DECL_CONTEXT (templ);
8727 }
8728 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8729 {
8730 tree type = TREE_TYPE (d1);
8731
8732 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8733 an implicit typename for the second A. Deal with it. */
8734 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8735 type = TREE_TYPE (type);
8736
8737 if (CLASSTYPE_TEMPLATE_INFO (type))
8738 {
8739 templ = CLASSTYPE_TI_TEMPLATE (type);
8740 d1 = DECL_NAME (templ);
8741 }
8742 }
8743 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8744 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8745 {
8746 templ = TYPE_TI_TEMPLATE (d1);
8747 d1 = DECL_NAME (templ);
8748 }
8749 else if (DECL_TYPE_TEMPLATE_P (d1))
8750 {
8751 templ = d1;
8752 d1 = DECL_NAME (templ);
8753 context = DECL_CONTEXT (templ);
8754 }
8755 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8756 {
8757 templ = d1;
8758 d1 = DECL_NAME (templ);
8759 }
8760
8761 /* Issue an error message if we didn't find a template. */
8762 if (! templ)
8763 {
8764 if (complain & tf_error)
8765 error ("%qT is not a template", d1);
8766 return error_mark_node;
8767 }
8768
8769 if (TREE_CODE (templ) != TEMPLATE_DECL
8770 /* Make sure it's a user visible template, if it was named by
8771 the user. */
8772 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8773 && !PRIMARY_TEMPLATE_P (templ)))
8774 {
8775 if (complain & tf_error)
8776 {
8777 error ("non-template type %qT used as a template", d1);
8778 if (in_decl)
8779 error ("for template declaration %q+D", in_decl);
8780 }
8781 return error_mark_node;
8782 }
8783
8784 complain &= ~tf_user;
8785
8786 /* An alias that just changes the name of a template is equivalent to the
8787 other template, so if any of the arguments are pack expansions, strip
8788 the alias to avoid problems with a pack expansion passed to a non-pack
8789 alias template parameter (DR 1430). */
8790 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8791 templ = get_underlying_template (templ);
8792
8793 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8794 {
8795 tree parm;
8796 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8797 if (arglist2 == error_mark_node
8798 || (!uses_template_parms (arglist2)
8799 && check_instantiated_args (templ, arglist2, complain)))
8800 return error_mark_node;
8801
8802 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8803 return parm;
8804 }
8805 else
8806 {
8807 tree template_type = TREE_TYPE (templ);
8808 tree gen_tmpl;
8809 tree type_decl;
8810 tree found = NULL_TREE;
8811 int arg_depth;
8812 int parm_depth;
8813 int is_dependent_type;
8814 int use_partial_inst_tmpl = false;
8815
8816 if (template_type == error_mark_node)
8817 /* An error occurred while building the template TEMPL, and a
8818 diagnostic has most certainly been emitted for that
8819 already. Let's propagate that error. */
8820 return error_mark_node;
8821
8822 gen_tmpl = most_general_template (templ);
8823 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8824 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8825 arg_depth = TMPL_ARGS_DEPTH (arglist);
8826
8827 if (arg_depth == 1 && parm_depth > 1)
8828 {
8829 /* We've been given an incomplete set of template arguments.
8830 For example, given:
8831
8832 template <class T> struct S1 {
8833 template <class U> struct S2 {};
8834 template <class U> struct S2<U*> {};
8835 };
8836
8837 we will be called with an ARGLIST of `U*', but the
8838 TEMPLATE will be `template <class T> template
8839 <class U> struct S1<T>::S2'. We must fill in the missing
8840 arguments. */
8841 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8842 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8843 arg_depth = TMPL_ARGS_DEPTH (arglist);
8844 }
8845
8846 /* Now we should have enough arguments. */
8847 gcc_assert (parm_depth == arg_depth);
8848
8849 /* From here on, we're only interested in the most general
8850 template. */
8851
8852 /* Calculate the BOUND_ARGS. These will be the args that are
8853 actually tsubst'd into the definition to create the
8854 instantiation. */
8855 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8856 complain,
8857 /*require_all_args=*/true,
8858 /*use_default_args=*/true);
8859
8860 if (arglist == error_mark_node)
8861 /* We were unable to bind the arguments. */
8862 return error_mark_node;
8863
8864 /* In the scope of a template class, explicit references to the
8865 template class refer to the type of the template, not any
8866 instantiation of it. For example, in:
8867
8868 template <class T> class C { void f(C<T>); }
8869
8870 the `C<T>' is just the same as `C'. Outside of the
8871 class, however, such a reference is an instantiation. */
8872 if (entering_scope
8873 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8874 || currently_open_class (template_type))
8875 {
8876 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8877
8878 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8879 return template_type;
8880 }
8881
8882 /* If we already have this specialization, return it. */
8883 elt.tmpl = gen_tmpl;
8884 elt.args = arglist;
8885 elt.spec = NULL_TREE;
8886 hash = spec_hasher::hash (&elt);
8887 entry = type_specializations->find_with_hash (&elt, hash);
8888
8889 if (entry)
8890 return entry->spec;
8891
8892 /* If the the template's constraints are not satisfied,
8893 then we cannot form a valid type.
8894
8895 Note that the check is deferred until after the hash
8896 lookup. This prevents redundant checks on previously
8897 instantiated specializations. */
8898 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8899 {
8900 if (complain & tf_error)
8901 {
8902 error ("template constraint failure");
8903 diagnose_constraints (input_location, gen_tmpl, arglist);
8904 }
8905 return error_mark_node;
8906 }
8907
8908 is_dependent_type = uses_template_parms (arglist);
8909
8910 /* If the deduced arguments are invalid, then the binding
8911 failed. */
8912 if (!is_dependent_type
8913 && check_instantiated_args (gen_tmpl,
8914 INNERMOST_TEMPLATE_ARGS (arglist),
8915 complain))
8916 return error_mark_node;
8917
8918 if (!is_dependent_type
8919 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8920 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8921 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8922 {
8923 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8924 DECL_NAME (gen_tmpl),
8925 /*tag_scope=*/ts_global);
8926 return found;
8927 }
8928
8929 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8930 complain, in_decl);
8931 if (context == error_mark_node)
8932 return error_mark_node;
8933
8934 if (!context)
8935 context = global_namespace;
8936
8937 /* Create the type. */
8938 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8939 {
8940 /* The user referred to a specialization of an alias
8941 template represented by GEN_TMPL.
8942
8943 [temp.alias]/2 says:
8944
8945 When a template-id refers to the specialization of an
8946 alias template, it is equivalent to the associated
8947 type obtained by substitution of its
8948 template-arguments for the template-parameters in the
8949 type-id of the alias template. */
8950
8951 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8952 /* Note that the call above (by indirectly calling
8953 register_specialization in tsubst_decl) registers the
8954 TYPE_DECL representing the specialization of the alias
8955 template. So next time someone substitutes ARGLIST for
8956 the template parms into the alias template (GEN_TMPL),
8957 she'll get that TYPE_DECL back. */
8958
8959 if (t == error_mark_node)
8960 return t;
8961 }
8962 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8963 {
8964 if (!is_dependent_type)
8965 {
8966 set_current_access_from_decl (TYPE_NAME (template_type));
8967 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8968 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8969 arglist, complain, in_decl),
8970 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8971 arglist, complain, in_decl),
8972 SCOPED_ENUM_P (template_type), NULL);
8973
8974 if (t == error_mark_node)
8975 return t;
8976 }
8977 else
8978 {
8979 /* We don't want to call start_enum for this type, since
8980 the values for the enumeration constants may involve
8981 template parameters. And, no one should be interested
8982 in the enumeration constants for such a type. */
8983 t = cxx_make_type (ENUMERAL_TYPE);
8984 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8985 }
8986 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8987 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8988 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8989 }
8990 else if (CLASS_TYPE_P (template_type))
8991 {
8992 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8993 instantiated here. */
8994 gcc_assert (!LAMBDA_TYPE_P (template_type));
8995
8996 t = make_class_type (TREE_CODE (template_type));
8997 CLASSTYPE_DECLARED_CLASS (t)
8998 = CLASSTYPE_DECLARED_CLASS (template_type);
8999 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9000
9001 /* A local class. Make sure the decl gets registered properly. */
9002 if (context == current_function_decl)
9003 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9004
9005 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9006 /* This instantiation is another name for the primary
9007 template type. Set the TYPE_CANONICAL field
9008 appropriately. */
9009 TYPE_CANONICAL (t) = template_type;
9010 else if (any_template_arguments_need_structural_equality_p (arglist))
9011 /* Some of the template arguments require structural
9012 equality testing, so this template class requires
9013 structural equality testing. */
9014 SET_TYPE_STRUCTURAL_EQUALITY (t);
9015 }
9016 else
9017 gcc_unreachable ();
9018
9019 /* If we called start_enum or pushtag above, this information
9020 will already be set up. */
9021 if (!TYPE_NAME (t))
9022 {
9023 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9024
9025 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9026 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9027 DECL_SOURCE_LOCATION (type_decl)
9028 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9029 }
9030 else
9031 type_decl = TYPE_NAME (t);
9032
9033 if (CLASS_TYPE_P (template_type))
9034 {
9035 TREE_PRIVATE (type_decl)
9036 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9037 TREE_PROTECTED (type_decl)
9038 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9039 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9040 {
9041 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9042 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9043 }
9044 }
9045
9046 if (OVERLOAD_TYPE_P (t)
9047 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9048 {
9049 static const char *tags[] = {"abi_tag", "may_alias"};
9050
9051 for (unsigned ix = 0; ix != 2; ix++)
9052 {
9053 tree attributes
9054 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9055
9056 if (attributes)
9057 TYPE_ATTRIBUTES (t)
9058 = tree_cons (TREE_PURPOSE (attributes),
9059 TREE_VALUE (attributes),
9060 TYPE_ATTRIBUTES (t));
9061 }
9062 }
9063
9064 /* Let's consider the explicit specialization of a member
9065 of a class template specialization that is implicitly instantiated,
9066 e.g.:
9067 template<class T>
9068 struct S
9069 {
9070 template<class U> struct M {}; //#0
9071 };
9072
9073 template<>
9074 template<>
9075 struct S<int>::M<char> //#1
9076 {
9077 int i;
9078 };
9079 [temp.expl.spec]/4 says this is valid.
9080
9081 In this case, when we write:
9082 S<int>::M<char> m;
9083
9084 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9085 the one of #0.
9086
9087 When we encounter #1, we want to store the partial instantiation
9088 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9089
9090 For all cases other than this "explicit specialization of member of a
9091 class template", we just want to store the most general template into
9092 the CLASSTYPE_TI_TEMPLATE of M.
9093
9094 This case of "explicit specialization of member of a class template"
9095 only happens when:
9096 1/ the enclosing class is an instantiation of, and therefore not
9097 the same as, the context of the most general template, and
9098 2/ we aren't looking at the partial instantiation itself, i.e.
9099 the innermost arguments are not the same as the innermost parms of
9100 the most general template.
9101
9102 So it's only when 1/ and 2/ happens that we want to use the partial
9103 instantiation of the member template in lieu of its most general
9104 template. */
9105
9106 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9107 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9108 /* the enclosing class must be an instantiation... */
9109 && CLASS_TYPE_P (context)
9110 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9111 {
9112 TREE_VEC_LENGTH (arglist)--;
9113 ++processing_template_decl;
9114 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9115 tree partial_inst_args =
9116 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9117 arglist, complain, NULL_TREE);
9118 --processing_template_decl;
9119 TREE_VEC_LENGTH (arglist)++;
9120 if (partial_inst_args == error_mark_node)
9121 return error_mark_node;
9122 use_partial_inst_tmpl =
9123 /*...and we must not be looking at the partial instantiation
9124 itself. */
9125 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9126 partial_inst_args);
9127 }
9128
9129 if (!use_partial_inst_tmpl)
9130 /* This case is easy; there are no member templates involved. */
9131 found = gen_tmpl;
9132 else
9133 {
9134 /* This is a full instantiation of a member template. Find
9135 the partial instantiation of which this is an instance. */
9136
9137 /* Temporarily reduce by one the number of levels in the ARGLIST
9138 so as to avoid comparing the last set of arguments. */
9139 TREE_VEC_LENGTH (arglist)--;
9140 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9141 TREE_VEC_LENGTH (arglist)++;
9142 /* FOUND is either a proper class type, or an alias
9143 template specialization. In the later case, it's a
9144 TYPE_DECL, resulting from the substituting of arguments
9145 for parameters in the TYPE_DECL of the alias template
9146 done earlier. So be careful while getting the template
9147 of FOUND. */
9148 found = (TREE_CODE (found) == TEMPLATE_DECL
9149 ? found
9150 : (TREE_CODE (found) == TYPE_DECL
9151 ? DECL_TI_TEMPLATE (found)
9152 : CLASSTYPE_TI_TEMPLATE (found)));
9153 }
9154
9155 // Build template info for the new specialization.
9156 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9157
9158 elt.spec = t;
9159 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9160 entry = ggc_alloc<spec_entry> ();
9161 *entry = elt;
9162 *slot = entry;
9163
9164 /* Note this use of the partial instantiation so we can check it
9165 later in maybe_process_partial_specialization. */
9166 DECL_TEMPLATE_INSTANTIATIONS (found)
9167 = tree_cons (arglist, t,
9168 DECL_TEMPLATE_INSTANTIATIONS (found));
9169
9170 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9171 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9172 /* Now that the type has been registered on the instantiations
9173 list, we set up the enumerators. Because the enumeration
9174 constants may involve the enumeration type itself, we make
9175 sure to register the type first, and then create the
9176 constants. That way, doing tsubst_expr for the enumeration
9177 constants won't result in recursive calls here; we'll find
9178 the instantiation and exit above. */
9179 tsubst_enum (template_type, t, arglist);
9180
9181 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9182 /* If the type makes use of template parameters, the
9183 code that generates debugging information will crash. */
9184 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9185
9186 /* Possibly limit visibility based on template args. */
9187 TREE_PUBLIC (type_decl) = 1;
9188 determine_visibility (type_decl);
9189
9190 inherit_targ_abi_tags (t);
9191
9192 return t;
9193 }
9194 }
9195
9196 /* Wrapper for lookup_template_class_1. */
9197
9198 tree
9199 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9200 int entering_scope, tsubst_flags_t complain)
9201 {
9202 tree ret;
9203 timevar_push (TV_TEMPLATE_INST);
9204 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9205 entering_scope, complain);
9206 timevar_pop (TV_TEMPLATE_INST);
9207 return ret;
9208 }
9209
9210 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9211
9212 tree
9213 lookup_template_variable (tree templ, tree arglist)
9214 {
9215 /* The type of the expression is NULL_TREE since the template-id could refer
9216 to an explicit or partial specialization. */
9217 tree type = NULL_TREE;
9218 if (flag_concepts && variable_concept_p (templ))
9219 /* Except that concepts are always bool. */
9220 type = boolean_type_node;
9221 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9222 }
9223
9224 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9225
9226 tree
9227 finish_template_variable (tree var, tsubst_flags_t complain)
9228 {
9229 tree templ = TREE_OPERAND (var, 0);
9230 tree arglist = TREE_OPERAND (var, 1);
9231
9232 /* We never want to return a VAR_DECL for a variable concept, since they
9233 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9234 bool concept_p = flag_concepts && variable_concept_p (templ);
9235 if (concept_p && processing_template_decl)
9236 return var;
9237
9238 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9239 arglist = add_outermost_template_args (tmpl_args, arglist);
9240
9241 templ = most_general_template (templ);
9242 tree parms = DECL_TEMPLATE_PARMS (templ);
9243 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9244 /*req_all*/true,
9245 /*use_default*/true);
9246
9247 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9248 {
9249 if (complain & tf_error)
9250 {
9251 error ("use of invalid variable template %qE", var);
9252 diagnose_constraints (location_of (var), templ, arglist);
9253 }
9254 return error_mark_node;
9255 }
9256
9257 /* If a template-id refers to a specialization of a variable
9258 concept, then the expression is true if and only if the
9259 concept's constraints are satisfied by the given template
9260 arguments.
9261
9262 NOTE: This is an extension of Concepts Lite TS that
9263 allows constraints to be used in expressions. */
9264 if (concept_p)
9265 {
9266 tree decl = DECL_TEMPLATE_RESULT (templ);
9267 return evaluate_variable_concept (decl, arglist);
9268 }
9269
9270 return instantiate_template (templ, arglist, complain);
9271 }
9272
9273 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9274 TARGS template args, and instantiate it if it's not dependent. */
9275
9276 tree
9277 lookup_and_finish_template_variable (tree templ, tree targs,
9278 tsubst_flags_t complain)
9279 {
9280 templ = lookup_template_variable (templ, targs);
9281 if (!any_dependent_template_arguments_p (targs))
9282 {
9283 templ = finish_template_variable (templ, complain);
9284 mark_used (templ);
9285 }
9286
9287 return convert_from_reference (templ);
9288 }
9289
9290 \f
9291 struct pair_fn_data
9292 {
9293 tree_fn_t fn;
9294 tree_fn_t any_fn;
9295 void *data;
9296 /* True when we should also visit template parameters that occur in
9297 non-deduced contexts. */
9298 bool include_nondeduced_p;
9299 hash_set<tree> *visited;
9300 };
9301
9302 /* Called from for_each_template_parm via walk_tree. */
9303
9304 static tree
9305 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9306 {
9307 tree t = *tp;
9308 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9309 tree_fn_t fn = pfd->fn;
9310 void *data = pfd->data;
9311 tree result = NULL_TREE;
9312
9313 #define WALK_SUBTREE(NODE) \
9314 do \
9315 { \
9316 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9317 pfd->include_nondeduced_p, \
9318 pfd->any_fn); \
9319 if (result) goto out; \
9320 } \
9321 while (0)
9322
9323 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9324 return t;
9325
9326 if (TYPE_P (t)
9327 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9328 WALK_SUBTREE (TYPE_CONTEXT (t));
9329
9330 switch (TREE_CODE (t))
9331 {
9332 case RECORD_TYPE:
9333 if (TYPE_PTRMEMFUNC_P (t))
9334 break;
9335 /* Fall through. */
9336
9337 case UNION_TYPE:
9338 case ENUMERAL_TYPE:
9339 if (!TYPE_TEMPLATE_INFO (t))
9340 *walk_subtrees = 0;
9341 else
9342 WALK_SUBTREE (TYPE_TI_ARGS (t));
9343 break;
9344
9345 case INTEGER_TYPE:
9346 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9347 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9348 break;
9349
9350 case METHOD_TYPE:
9351 /* Since we're not going to walk subtrees, we have to do this
9352 explicitly here. */
9353 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9354 /* Fall through. */
9355
9356 case FUNCTION_TYPE:
9357 /* Check the return type. */
9358 WALK_SUBTREE (TREE_TYPE (t));
9359
9360 /* Check the parameter types. Since default arguments are not
9361 instantiated until they are needed, the TYPE_ARG_TYPES may
9362 contain expressions that involve template parameters. But,
9363 no-one should be looking at them yet. And, once they're
9364 instantiated, they don't contain template parameters, so
9365 there's no point in looking at them then, either. */
9366 {
9367 tree parm;
9368
9369 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9370 WALK_SUBTREE (TREE_VALUE (parm));
9371
9372 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9373 want walk_tree walking into them itself. */
9374 *walk_subtrees = 0;
9375 }
9376
9377 if (flag_noexcept_type)
9378 {
9379 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9380 if (spec)
9381 WALK_SUBTREE (TREE_PURPOSE (spec));
9382 }
9383 break;
9384
9385 case TYPEOF_TYPE:
9386 case UNDERLYING_TYPE:
9387 if (pfd->include_nondeduced_p
9388 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9389 pfd->visited,
9390 pfd->include_nondeduced_p,
9391 pfd->any_fn))
9392 return error_mark_node;
9393 break;
9394
9395 case FUNCTION_DECL:
9396 case VAR_DECL:
9397 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9398 WALK_SUBTREE (DECL_TI_ARGS (t));
9399 /* Fall through. */
9400
9401 case PARM_DECL:
9402 case CONST_DECL:
9403 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9404 WALK_SUBTREE (DECL_INITIAL (t));
9405 if (DECL_CONTEXT (t)
9406 && pfd->include_nondeduced_p)
9407 WALK_SUBTREE (DECL_CONTEXT (t));
9408 break;
9409
9410 case BOUND_TEMPLATE_TEMPLATE_PARM:
9411 /* Record template parameters such as `T' inside `TT<T>'. */
9412 WALK_SUBTREE (TYPE_TI_ARGS (t));
9413 /* Fall through. */
9414
9415 case TEMPLATE_TEMPLATE_PARM:
9416 case TEMPLATE_TYPE_PARM:
9417 case TEMPLATE_PARM_INDEX:
9418 if (fn && (*fn)(t, data))
9419 return t;
9420 else if (!fn)
9421 return t;
9422 break;
9423
9424 case TEMPLATE_DECL:
9425 /* A template template parameter is encountered. */
9426 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9427 WALK_SUBTREE (TREE_TYPE (t));
9428
9429 /* Already substituted template template parameter */
9430 *walk_subtrees = 0;
9431 break;
9432
9433 case TYPENAME_TYPE:
9434 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9435 partial instantiation. */
9436 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9437 break;
9438
9439 case CONSTRUCTOR:
9440 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9441 && pfd->include_nondeduced_p)
9442 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9443 break;
9444
9445 case INDIRECT_REF:
9446 case COMPONENT_REF:
9447 /* If there's no type, then this thing must be some expression
9448 involving template parameters. */
9449 if (!fn && !TREE_TYPE (t))
9450 return error_mark_node;
9451 break;
9452
9453 case MODOP_EXPR:
9454 case CAST_EXPR:
9455 case IMPLICIT_CONV_EXPR:
9456 case REINTERPRET_CAST_EXPR:
9457 case CONST_CAST_EXPR:
9458 case STATIC_CAST_EXPR:
9459 case DYNAMIC_CAST_EXPR:
9460 case ARROW_EXPR:
9461 case DOTSTAR_EXPR:
9462 case TYPEID_EXPR:
9463 case PSEUDO_DTOR_EXPR:
9464 if (!fn)
9465 return error_mark_node;
9466 break;
9467
9468 default:
9469 break;
9470 }
9471
9472 #undef WALK_SUBTREE
9473
9474 /* We didn't find any template parameters we liked. */
9475 out:
9476 return result;
9477 }
9478
9479 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9480 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9481 call FN with the parameter and the DATA.
9482 If FN returns nonzero, the iteration is terminated, and
9483 for_each_template_parm returns 1. Otherwise, the iteration
9484 continues. If FN never returns a nonzero value, the value
9485 returned by for_each_template_parm is 0. If FN is NULL, it is
9486 considered to be the function which always returns 1.
9487
9488 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9489 parameters that occur in non-deduced contexts. When false, only
9490 visits those template parameters that can be deduced. */
9491
9492 static tree
9493 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9494 hash_set<tree> *visited,
9495 bool include_nondeduced_p,
9496 tree_fn_t any_fn)
9497 {
9498 struct pair_fn_data pfd;
9499 tree result;
9500
9501 /* Set up. */
9502 pfd.fn = fn;
9503 pfd.any_fn = any_fn;
9504 pfd.data = data;
9505 pfd.include_nondeduced_p = include_nondeduced_p;
9506
9507 /* Walk the tree. (Conceptually, we would like to walk without
9508 duplicates, but for_each_template_parm_r recursively calls
9509 for_each_template_parm, so we would need to reorganize a fair
9510 bit to use walk_tree_without_duplicates, so we keep our own
9511 visited list.) */
9512 if (visited)
9513 pfd.visited = visited;
9514 else
9515 pfd.visited = new hash_set<tree>;
9516 result = cp_walk_tree (&t,
9517 for_each_template_parm_r,
9518 &pfd,
9519 pfd.visited);
9520
9521 /* Clean up. */
9522 if (!visited)
9523 {
9524 delete pfd.visited;
9525 pfd.visited = 0;
9526 }
9527
9528 return result;
9529 }
9530
9531 /* Returns true if T depends on any template parameter. */
9532
9533 int
9534 uses_template_parms (tree t)
9535 {
9536 if (t == NULL_TREE)
9537 return false;
9538
9539 bool dependent_p;
9540 int saved_processing_template_decl;
9541
9542 saved_processing_template_decl = processing_template_decl;
9543 if (!saved_processing_template_decl)
9544 processing_template_decl = 1;
9545 if (TYPE_P (t))
9546 dependent_p = dependent_type_p (t);
9547 else if (TREE_CODE (t) == TREE_VEC)
9548 dependent_p = any_dependent_template_arguments_p (t);
9549 else if (TREE_CODE (t) == TREE_LIST)
9550 dependent_p = (uses_template_parms (TREE_VALUE (t))
9551 || uses_template_parms (TREE_CHAIN (t)));
9552 else if (TREE_CODE (t) == TYPE_DECL)
9553 dependent_p = dependent_type_p (TREE_TYPE (t));
9554 else if (DECL_P (t)
9555 || EXPR_P (t)
9556 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9557 || TREE_CODE (t) == OVERLOAD
9558 || BASELINK_P (t)
9559 || identifier_p (t)
9560 || TREE_CODE (t) == TRAIT_EXPR
9561 || TREE_CODE (t) == CONSTRUCTOR
9562 || CONSTANT_CLASS_P (t))
9563 dependent_p = (type_dependent_expression_p (t)
9564 || value_dependent_expression_p (t));
9565 else
9566 {
9567 gcc_assert (t == error_mark_node);
9568 dependent_p = false;
9569 }
9570
9571 processing_template_decl = saved_processing_template_decl;
9572
9573 return dependent_p;
9574 }
9575
9576 /* Returns true iff current_function_decl is an incompletely instantiated
9577 template. Useful instead of processing_template_decl because the latter
9578 is set to 0 during instantiate_non_dependent_expr. */
9579
9580 bool
9581 in_template_function (void)
9582 {
9583 tree fn = current_function_decl;
9584 bool ret;
9585 ++processing_template_decl;
9586 ret = (fn && DECL_LANG_SPECIFIC (fn)
9587 && DECL_TEMPLATE_INFO (fn)
9588 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9589 --processing_template_decl;
9590 return ret;
9591 }
9592
9593 /* Returns true if T depends on any template parameter with level LEVEL. */
9594
9595 bool
9596 uses_template_parms_level (tree t, int level)
9597 {
9598 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9599 /*include_nondeduced_p=*/true);
9600 }
9601
9602 /* Returns true if the signature of DECL depends on any template parameter from
9603 its enclosing class. */
9604
9605 bool
9606 uses_outer_template_parms (tree decl)
9607 {
9608 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9609 if (depth == 0)
9610 return false;
9611 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9612 &depth, NULL, /*include_nondeduced_p=*/true))
9613 return true;
9614 if (PRIMARY_TEMPLATE_P (decl)
9615 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9616 (DECL_TEMPLATE_PARMS (decl)),
9617 template_parm_outer_level,
9618 &depth, NULL, /*include_nondeduced_p=*/true))
9619 return true;
9620 tree ci = get_constraints (decl);
9621 if (ci)
9622 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9623 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9624 &depth, NULL, /*nondeduced*/true))
9625 return true;
9626 return false;
9627 }
9628
9629 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9630 ill-formed translation unit, i.e. a variable or function that isn't
9631 usable in a constant expression. */
9632
9633 static inline bool
9634 neglectable_inst_p (tree d)
9635 {
9636 return (DECL_P (d)
9637 && !undeduced_auto_decl (d)
9638 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9639 : decl_maybe_constant_var_p (d)));
9640 }
9641
9642 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9643 neglectable and instantiated from within an erroneous instantiation. */
9644
9645 static bool
9646 limit_bad_template_recursion (tree decl)
9647 {
9648 struct tinst_level *lev = current_tinst_level;
9649 int errs = errorcount + sorrycount;
9650 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9651 return false;
9652
9653 for (; lev; lev = lev->next)
9654 if (neglectable_inst_p (lev->decl))
9655 break;
9656
9657 return (lev && errs > lev->errors);
9658 }
9659
9660 static int tinst_depth;
9661 extern int max_tinst_depth;
9662 int depth_reached;
9663
9664 static GTY(()) struct tinst_level *last_error_tinst_level;
9665
9666 /* We're starting to instantiate D; record the template instantiation context
9667 for diagnostics and to restore it later. */
9668
9669 bool
9670 push_tinst_level (tree d)
9671 {
9672 return push_tinst_level_loc (d, input_location);
9673 }
9674
9675 /* We're starting to instantiate D; record the template instantiation context
9676 at LOC for diagnostics and to restore it later. */
9677
9678 bool
9679 push_tinst_level_loc (tree d, location_t loc)
9680 {
9681 struct tinst_level *new_level;
9682
9683 if (tinst_depth >= max_tinst_depth)
9684 {
9685 /* Tell error.c not to try to instantiate any templates. */
9686 at_eof = 2;
9687 fatal_error (input_location,
9688 "template instantiation depth exceeds maximum of %d"
9689 " (use -ftemplate-depth= to increase the maximum)",
9690 max_tinst_depth);
9691 return false;
9692 }
9693
9694 /* If the current instantiation caused problems, don't let it instantiate
9695 anything else. Do allow deduction substitution and decls usable in
9696 constant expressions. */
9697 if (limit_bad_template_recursion (d))
9698 return false;
9699
9700 /* When not -quiet, dump template instantiations other than functions, since
9701 announce_function will take care of those. */
9702 if (!quiet_flag
9703 && TREE_CODE (d) != TREE_LIST
9704 && TREE_CODE (d) != FUNCTION_DECL)
9705 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9706
9707 new_level = ggc_alloc<tinst_level> ();
9708 new_level->decl = d;
9709 new_level->locus = loc;
9710 new_level->errors = errorcount+sorrycount;
9711 new_level->in_system_header_p = in_system_header_at (input_location);
9712 new_level->next = current_tinst_level;
9713 current_tinst_level = new_level;
9714
9715 ++tinst_depth;
9716 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9717 depth_reached = tinst_depth;
9718
9719 return true;
9720 }
9721
9722 /* We're done instantiating this template; return to the instantiation
9723 context. */
9724
9725 void
9726 pop_tinst_level (void)
9727 {
9728 /* Restore the filename and line number stashed away when we started
9729 this instantiation. */
9730 input_location = current_tinst_level->locus;
9731 current_tinst_level = current_tinst_level->next;
9732 --tinst_depth;
9733 }
9734
9735 /* We're instantiating a deferred template; restore the template
9736 instantiation context in which the instantiation was requested, which
9737 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9738
9739 static tree
9740 reopen_tinst_level (struct tinst_level *level)
9741 {
9742 struct tinst_level *t;
9743
9744 tinst_depth = 0;
9745 for (t = level; t; t = t->next)
9746 ++tinst_depth;
9747
9748 current_tinst_level = level;
9749 pop_tinst_level ();
9750 if (current_tinst_level)
9751 current_tinst_level->errors = errorcount+sorrycount;
9752 return level->decl;
9753 }
9754
9755 /* Returns the TINST_LEVEL which gives the original instantiation
9756 context. */
9757
9758 struct tinst_level *
9759 outermost_tinst_level (void)
9760 {
9761 struct tinst_level *level = current_tinst_level;
9762 if (level)
9763 while (level->next)
9764 level = level->next;
9765 return level;
9766 }
9767
9768 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9769 vector of template arguments, as for tsubst.
9770
9771 Returns an appropriate tsubst'd friend declaration. */
9772
9773 static tree
9774 tsubst_friend_function (tree decl, tree args)
9775 {
9776 tree new_friend;
9777
9778 if (TREE_CODE (decl) == FUNCTION_DECL
9779 && DECL_TEMPLATE_INSTANTIATION (decl)
9780 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9781 /* This was a friend declared with an explicit template
9782 argument list, e.g.:
9783
9784 friend void f<>(T);
9785
9786 to indicate that f was a template instantiation, not a new
9787 function declaration. Now, we have to figure out what
9788 instantiation of what template. */
9789 {
9790 tree template_id, arglist, fns;
9791 tree new_args;
9792 tree tmpl;
9793 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9794
9795 /* Friend functions are looked up in the containing namespace scope.
9796 We must enter that scope, to avoid finding member functions of the
9797 current class with same name. */
9798 push_nested_namespace (ns);
9799 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9800 tf_warning_or_error, NULL_TREE,
9801 /*integral_constant_expression_p=*/false);
9802 pop_nested_namespace (ns);
9803 arglist = tsubst (DECL_TI_ARGS (decl), args,
9804 tf_warning_or_error, NULL_TREE);
9805 template_id = lookup_template_function (fns, arglist);
9806
9807 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9808 tmpl = determine_specialization (template_id, new_friend,
9809 &new_args,
9810 /*need_member_template=*/0,
9811 TREE_VEC_LENGTH (args),
9812 tsk_none);
9813 return instantiate_template (tmpl, new_args, tf_error);
9814 }
9815
9816 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9817
9818 /* The NEW_FRIEND will look like an instantiation, to the
9819 compiler, but is not an instantiation from the point of view of
9820 the language. For example, we might have had:
9821
9822 template <class T> struct S {
9823 template <class U> friend void f(T, U);
9824 };
9825
9826 Then, in S<int>, template <class U> void f(int, U) is not an
9827 instantiation of anything. */
9828 if (new_friend == error_mark_node)
9829 return error_mark_node;
9830
9831 DECL_USE_TEMPLATE (new_friend) = 0;
9832 if (TREE_CODE (decl) == TEMPLATE_DECL)
9833 {
9834 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9835 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9836 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9837 }
9838
9839 /* The mangled name for the NEW_FRIEND is incorrect. The function
9840 is not a template instantiation and should not be mangled like
9841 one. Therefore, we forget the mangling here; we'll recompute it
9842 later if we need it. */
9843 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9844 {
9845 SET_DECL_RTL (new_friend, NULL);
9846 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9847 }
9848
9849 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9850 {
9851 tree old_decl;
9852 tree new_friend_template_info;
9853 tree new_friend_result_template_info;
9854 tree ns;
9855 int new_friend_is_defn;
9856
9857 /* We must save some information from NEW_FRIEND before calling
9858 duplicate decls since that function will free NEW_FRIEND if
9859 possible. */
9860 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9861 new_friend_is_defn =
9862 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9863 (template_for_substitution (new_friend)))
9864 != NULL_TREE);
9865 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9866 {
9867 /* This declaration is a `primary' template. */
9868 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9869
9870 new_friend_result_template_info
9871 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9872 }
9873 else
9874 new_friend_result_template_info = NULL_TREE;
9875
9876 /* Inside pushdecl_namespace_level, we will push into the
9877 current namespace. However, the friend function should go
9878 into the namespace of the template. */
9879 ns = decl_namespace_context (new_friend);
9880 push_nested_namespace (ns);
9881 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9882 pop_nested_namespace (ns);
9883
9884 if (old_decl == error_mark_node)
9885 return error_mark_node;
9886
9887 if (old_decl != new_friend)
9888 {
9889 /* This new friend declaration matched an existing
9890 declaration. For example, given:
9891
9892 template <class T> void f(T);
9893 template <class U> class C {
9894 template <class T> friend void f(T) {}
9895 };
9896
9897 the friend declaration actually provides the definition
9898 of `f', once C has been instantiated for some type. So,
9899 old_decl will be the out-of-class template declaration,
9900 while new_friend is the in-class definition.
9901
9902 But, if `f' was called before this point, the
9903 instantiation of `f' will have DECL_TI_ARGS corresponding
9904 to `T' but not to `U', references to which might appear
9905 in the definition of `f'. Previously, the most general
9906 template for an instantiation of `f' was the out-of-class
9907 version; now it is the in-class version. Therefore, we
9908 run through all specialization of `f', adding to their
9909 DECL_TI_ARGS appropriately. In particular, they need a
9910 new set of outer arguments, corresponding to the
9911 arguments for this class instantiation.
9912
9913 The same situation can arise with something like this:
9914
9915 friend void f(int);
9916 template <class T> class C {
9917 friend void f(T) {}
9918 };
9919
9920 when `C<int>' is instantiated. Now, `f(int)' is defined
9921 in the class. */
9922
9923 if (!new_friend_is_defn)
9924 /* On the other hand, if the in-class declaration does
9925 *not* provide a definition, then we don't want to alter
9926 existing definitions. We can just leave everything
9927 alone. */
9928 ;
9929 else
9930 {
9931 tree new_template = TI_TEMPLATE (new_friend_template_info);
9932 tree new_args = TI_ARGS (new_friend_template_info);
9933
9934 /* Overwrite whatever template info was there before, if
9935 any, with the new template information pertaining to
9936 the declaration. */
9937 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9938
9939 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9940 {
9941 /* We should have called reregister_specialization in
9942 duplicate_decls. */
9943 gcc_assert (retrieve_specialization (new_template,
9944 new_args, 0)
9945 == old_decl);
9946
9947 /* Instantiate it if the global has already been used. */
9948 if (DECL_ODR_USED (old_decl))
9949 instantiate_decl (old_decl, /*defer_ok=*/true,
9950 /*expl_inst_class_mem_p=*/false);
9951 }
9952 else
9953 {
9954 tree t;
9955
9956 /* Indicate that the old function template is a partial
9957 instantiation. */
9958 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9959 = new_friend_result_template_info;
9960
9961 gcc_assert (new_template
9962 == most_general_template (new_template));
9963 gcc_assert (new_template != old_decl);
9964
9965 /* Reassign any specializations already in the hash table
9966 to the new more general template, and add the
9967 additional template args. */
9968 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9969 t != NULL_TREE;
9970 t = TREE_CHAIN (t))
9971 {
9972 tree spec = TREE_VALUE (t);
9973 spec_entry elt;
9974
9975 elt.tmpl = old_decl;
9976 elt.args = DECL_TI_ARGS (spec);
9977 elt.spec = NULL_TREE;
9978
9979 decl_specializations->remove_elt (&elt);
9980
9981 DECL_TI_ARGS (spec)
9982 = add_outermost_template_args (new_args,
9983 DECL_TI_ARGS (spec));
9984
9985 register_specialization
9986 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9987
9988 }
9989 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9990 }
9991 }
9992
9993 /* The information from NEW_FRIEND has been merged into OLD_DECL
9994 by duplicate_decls. */
9995 new_friend = old_decl;
9996 }
9997 }
9998 else
9999 {
10000 tree context = DECL_CONTEXT (new_friend);
10001 bool dependent_p;
10002
10003 /* In the code
10004 template <class T> class C {
10005 template <class U> friend void C1<U>::f (); // case 1
10006 friend void C2<T>::f (); // case 2
10007 };
10008 we only need to make sure CONTEXT is a complete type for
10009 case 2. To distinguish between the two cases, we note that
10010 CONTEXT of case 1 remains dependent type after tsubst while
10011 this isn't true for case 2. */
10012 ++processing_template_decl;
10013 dependent_p = dependent_type_p (context);
10014 --processing_template_decl;
10015
10016 if (!dependent_p
10017 && !complete_type_or_else (context, NULL_TREE))
10018 return error_mark_node;
10019
10020 if (COMPLETE_TYPE_P (context))
10021 {
10022 tree fn = new_friend;
10023 /* do_friend adds the TEMPLATE_DECL for any member friend
10024 template even if it isn't a member template, i.e.
10025 template <class T> friend A<T>::f();
10026 Look through it in that case. */
10027 if (TREE_CODE (fn) == TEMPLATE_DECL
10028 && !PRIMARY_TEMPLATE_P (fn))
10029 fn = DECL_TEMPLATE_RESULT (fn);
10030 /* Check to see that the declaration is really present, and,
10031 possibly obtain an improved declaration. */
10032 fn = check_classfn (context, fn, NULL_TREE);
10033
10034 if (fn)
10035 new_friend = fn;
10036 }
10037 }
10038
10039 return new_friend;
10040 }
10041
10042 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10043 template arguments, as for tsubst.
10044
10045 Returns an appropriate tsubst'd friend type or error_mark_node on
10046 failure. */
10047
10048 static tree
10049 tsubst_friend_class (tree friend_tmpl, tree args)
10050 {
10051 tree tmpl;
10052
10053 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10054 {
10055 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10056 return TREE_TYPE (tmpl);
10057 }
10058
10059 tree context = CP_DECL_CONTEXT (friend_tmpl);
10060 if (TREE_CODE (context) == NAMESPACE_DECL)
10061 push_nested_namespace (context);
10062 else
10063 push_nested_class (context);
10064
10065 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10066 /*non_class=*/false, /*block_p=*/false,
10067 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10068
10069 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10070 {
10071 /* The friend template has already been declared. Just
10072 check to see that the declarations match, and install any new
10073 default parameters. We must tsubst the default parameters,
10074 of course. We only need the innermost template parameters
10075 because that is all that redeclare_class_template will look
10076 at. */
10077 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10078 > TMPL_ARGS_DEPTH (args))
10079 {
10080 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10081 args, tf_warning_or_error);
10082 location_t saved_input_location = input_location;
10083 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10084 tree cons = get_constraints (tmpl);
10085 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10086 input_location = saved_input_location;
10087 }
10088 }
10089 else
10090 {
10091 /* The friend template has not already been declared. In this
10092 case, the instantiation of the template class will cause the
10093 injection of this template into the namespace scope. */
10094 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10095
10096 if (tmpl != error_mark_node)
10097 {
10098 /* The new TMPL is not an instantiation of anything, so we
10099 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10100 for the new type because that is supposed to be the
10101 corresponding template decl, i.e., TMPL. */
10102 DECL_USE_TEMPLATE (tmpl) = 0;
10103 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10104 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10105 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10106 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10107
10108 /* It is hidden. */
10109 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10110 DECL_ANTICIPATED (tmpl)
10111 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10112
10113 /* Inject this template into the enclosing namspace scope. */
10114 tmpl = pushdecl_namespace_level (tmpl, true);
10115 }
10116 }
10117
10118 if (TREE_CODE (context) == NAMESPACE_DECL)
10119 pop_nested_namespace (context);
10120 else
10121 pop_nested_class ();
10122
10123 return TREE_TYPE (tmpl);
10124 }
10125
10126 /* Returns zero if TYPE cannot be completed later due to circularity.
10127 Otherwise returns one. */
10128
10129 static int
10130 can_complete_type_without_circularity (tree type)
10131 {
10132 if (type == NULL_TREE || type == error_mark_node)
10133 return 0;
10134 else if (COMPLETE_TYPE_P (type))
10135 return 1;
10136 else if (TREE_CODE (type) == ARRAY_TYPE)
10137 return can_complete_type_without_circularity (TREE_TYPE (type));
10138 else if (CLASS_TYPE_P (type)
10139 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10140 return 0;
10141 else
10142 return 1;
10143 }
10144
10145 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10146 tsubst_flags_t, tree);
10147
10148 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10149 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10150
10151 static tree
10152 tsubst_attribute (tree t, tree *decl_p, tree args,
10153 tsubst_flags_t complain, tree in_decl)
10154 {
10155 gcc_assert (ATTR_IS_DEPENDENT (t));
10156
10157 tree val = TREE_VALUE (t);
10158 if (val == NULL_TREE)
10159 /* Nothing to do. */;
10160 else if ((flag_openmp || flag_openmp_simd)
10161 && is_attribute_p ("omp declare simd",
10162 get_attribute_name (t)))
10163 {
10164 tree clauses = TREE_VALUE (val);
10165 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10166 complain, in_decl);
10167 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10168 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10169 tree parms = DECL_ARGUMENTS (*decl_p);
10170 clauses
10171 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10172 if (clauses)
10173 val = build_tree_list (NULL_TREE, clauses);
10174 else
10175 val = NULL_TREE;
10176 }
10177 /* If the first attribute argument is an identifier, don't
10178 pass it through tsubst. Attributes like mode, format,
10179 cleanup and several target specific attributes expect it
10180 unmodified. */
10181 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10182 {
10183 tree chain
10184 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10185 /*integral_constant_expression_p=*/false);
10186 if (chain != TREE_CHAIN (val))
10187 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10188 }
10189 else if (PACK_EXPANSION_P (val))
10190 {
10191 /* An attribute pack expansion. */
10192 tree purp = TREE_PURPOSE (t);
10193 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10194 if (pack == error_mark_node)
10195 return error_mark_node;
10196 int len = TREE_VEC_LENGTH (pack);
10197 tree list = NULL_TREE;
10198 tree *q = &list;
10199 for (int i = 0; i < len; ++i)
10200 {
10201 tree elt = TREE_VEC_ELT (pack, i);
10202 *q = build_tree_list (purp, elt);
10203 q = &TREE_CHAIN (*q);
10204 }
10205 return list;
10206 }
10207 else
10208 val = tsubst_expr (val, args, complain, in_decl,
10209 /*integral_constant_expression_p=*/false);
10210
10211 if (val != TREE_VALUE (t))
10212 return build_tree_list (TREE_PURPOSE (t), val);
10213 return t;
10214 }
10215
10216 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10217 unchanged or a new TREE_LIST chain. */
10218
10219 static tree
10220 tsubst_attributes (tree attributes, tree args,
10221 tsubst_flags_t complain, tree in_decl)
10222 {
10223 tree last_dep = NULL_TREE;
10224
10225 for (tree t = attributes; t; t = TREE_CHAIN (t))
10226 if (ATTR_IS_DEPENDENT (t))
10227 {
10228 last_dep = t;
10229 attributes = copy_list (attributes);
10230 break;
10231 }
10232
10233 if (last_dep)
10234 for (tree *p = &attributes; *p; )
10235 {
10236 tree t = *p;
10237 if (ATTR_IS_DEPENDENT (t))
10238 {
10239 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10240 if (subst != t)
10241 {
10242 *p = subst;
10243 do
10244 p = &TREE_CHAIN (*p);
10245 while (*p);
10246 *p = TREE_CHAIN (t);
10247 continue;
10248 }
10249 }
10250 p = &TREE_CHAIN (*p);
10251 }
10252
10253 return attributes;
10254 }
10255
10256 /* Apply any attributes which had to be deferred until instantiation
10257 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10258 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10259
10260 static void
10261 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10262 tree args, tsubst_flags_t complain, tree in_decl)
10263 {
10264 tree last_dep = NULL_TREE;
10265 tree t;
10266 tree *p;
10267
10268 if (attributes == NULL_TREE)
10269 return;
10270
10271 if (DECL_P (*decl_p))
10272 {
10273 if (TREE_TYPE (*decl_p) == error_mark_node)
10274 return;
10275 p = &DECL_ATTRIBUTES (*decl_p);
10276 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10277 to our attributes parameter. */
10278 gcc_assert (*p == attributes);
10279 }
10280 else
10281 {
10282 p = &TYPE_ATTRIBUTES (*decl_p);
10283 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10284 lookup_template_class_1, and should be preserved. */
10285 gcc_assert (*p != attributes);
10286 while (*p)
10287 p = &TREE_CHAIN (*p);
10288 }
10289
10290 for (t = attributes; t; t = TREE_CHAIN (t))
10291 if (ATTR_IS_DEPENDENT (t))
10292 {
10293 last_dep = t;
10294 attributes = copy_list (attributes);
10295 break;
10296 }
10297
10298 *p = attributes;
10299 if (last_dep)
10300 {
10301 tree late_attrs = NULL_TREE;
10302 tree *q = &late_attrs;
10303
10304 for (; *p; )
10305 {
10306 t = *p;
10307 if (ATTR_IS_DEPENDENT (t))
10308 {
10309 *p = TREE_CHAIN (t);
10310 TREE_CHAIN (t) = NULL_TREE;
10311 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10312 do
10313 q = &TREE_CHAIN (*q);
10314 while (*q);
10315 }
10316 else
10317 p = &TREE_CHAIN (t);
10318 }
10319
10320 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10321 }
10322 }
10323
10324 /* Perform (or defer) access check for typedefs that were referenced
10325 from within the template TMPL code.
10326 This is a subroutine of instantiate_decl and instantiate_class_template.
10327 TMPL is the template to consider and TARGS is the list of arguments of
10328 that template. */
10329
10330 static void
10331 perform_typedefs_access_check (tree tmpl, tree targs)
10332 {
10333 location_t saved_location;
10334 unsigned i;
10335 qualified_typedef_usage_t *iter;
10336
10337 if (!tmpl
10338 || (!CLASS_TYPE_P (tmpl)
10339 && TREE_CODE (tmpl) != FUNCTION_DECL))
10340 return;
10341
10342 saved_location = input_location;
10343 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10344 {
10345 tree type_decl = iter->typedef_decl;
10346 tree type_scope = iter->context;
10347
10348 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10349 continue;
10350
10351 if (uses_template_parms (type_decl))
10352 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10353 if (uses_template_parms (type_scope))
10354 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10355
10356 /* Make access check error messages point to the location
10357 of the use of the typedef. */
10358 input_location = iter->locus;
10359 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10360 type_decl, type_decl,
10361 tf_warning_or_error);
10362 }
10363 input_location = saved_location;
10364 }
10365
10366 static tree
10367 instantiate_class_template_1 (tree type)
10368 {
10369 tree templ, args, pattern, t, member;
10370 tree typedecl;
10371 tree pbinfo;
10372 tree base_list;
10373 unsigned int saved_maximum_field_alignment;
10374 tree fn_context;
10375
10376 if (type == error_mark_node)
10377 return error_mark_node;
10378
10379 if (COMPLETE_OR_OPEN_TYPE_P (type)
10380 || uses_template_parms (type))
10381 return type;
10382
10383 /* Figure out which template is being instantiated. */
10384 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10385 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10386
10387 /* Mark the type as in the process of being defined. */
10388 TYPE_BEING_DEFINED (type) = 1;
10389
10390 /* Determine what specialization of the original template to
10391 instantiate. */
10392 t = most_specialized_partial_spec (type, tf_warning_or_error);
10393 if (t == error_mark_node)
10394 return error_mark_node;
10395 else if (t)
10396 {
10397 /* This TYPE is actually an instantiation of a partial
10398 specialization. We replace the innermost set of ARGS with
10399 the arguments appropriate for substitution. For example,
10400 given:
10401
10402 template <class T> struct S {};
10403 template <class T> struct S<T*> {};
10404
10405 and supposing that we are instantiating S<int*>, ARGS will
10406 presently be {int*} -- but we need {int}. */
10407 pattern = TREE_TYPE (t);
10408 args = TREE_PURPOSE (t);
10409 }
10410 else
10411 {
10412 pattern = TREE_TYPE (templ);
10413 args = CLASSTYPE_TI_ARGS (type);
10414 }
10415
10416 /* If the template we're instantiating is incomplete, then clearly
10417 there's nothing we can do. */
10418 if (!COMPLETE_TYPE_P (pattern))
10419 {
10420 /* We can try again later. */
10421 TYPE_BEING_DEFINED (type) = 0;
10422 return type;
10423 }
10424
10425 /* If we've recursively instantiated too many templates, stop. */
10426 if (! push_tinst_level (type))
10427 return type;
10428
10429 /* We may be in the middle of deferred access check. Disable
10430 it now. */
10431 push_deferring_access_checks (dk_no_deferred);
10432
10433 int saved_unevaluated_operand = cp_unevaluated_operand;
10434 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10435
10436 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10437 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10438 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10439 fn_context = error_mark_node;
10440 if (!fn_context)
10441 push_to_top_level ();
10442 else
10443 {
10444 cp_unevaluated_operand = 0;
10445 c_inhibit_evaluation_warnings = 0;
10446 }
10447 /* Use #pragma pack from the template context. */
10448 saved_maximum_field_alignment = maximum_field_alignment;
10449 maximum_field_alignment = TYPE_PRECISION (pattern);
10450
10451 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10452
10453 /* Set the input location to the most specialized template definition.
10454 This is needed if tsubsting causes an error. */
10455 typedecl = TYPE_MAIN_DECL (pattern);
10456 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10457 DECL_SOURCE_LOCATION (typedecl);
10458
10459 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10460 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10461 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10462 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10463 if (ANON_AGGR_TYPE_P (pattern))
10464 SET_ANON_AGGR_TYPE_P (type);
10465 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10466 {
10467 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10468 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10469 /* Adjust visibility for template arguments. */
10470 determine_visibility (TYPE_MAIN_DECL (type));
10471 }
10472 if (CLASS_TYPE_P (type))
10473 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10474
10475 pbinfo = TYPE_BINFO (pattern);
10476
10477 /* We should never instantiate a nested class before its enclosing
10478 class; we need to look up the nested class by name before we can
10479 instantiate it, and that lookup should instantiate the enclosing
10480 class. */
10481 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10482 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10483
10484 base_list = NULL_TREE;
10485 if (BINFO_N_BASE_BINFOS (pbinfo))
10486 {
10487 tree pbase_binfo;
10488 tree pushed_scope;
10489 int i;
10490
10491 /* We must enter the scope containing the type, as that is where
10492 the accessibility of types named in dependent bases are
10493 looked up from. */
10494 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10495
10496 /* Substitute into each of the bases to determine the actual
10497 basetypes. */
10498 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10499 {
10500 tree base;
10501 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10502 tree expanded_bases = NULL_TREE;
10503 int idx, len = 1;
10504
10505 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10506 {
10507 expanded_bases =
10508 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10509 args, tf_error, NULL_TREE);
10510 if (expanded_bases == error_mark_node)
10511 continue;
10512
10513 len = TREE_VEC_LENGTH (expanded_bases);
10514 }
10515
10516 for (idx = 0; idx < len; idx++)
10517 {
10518 if (expanded_bases)
10519 /* Extract the already-expanded base class. */
10520 base = TREE_VEC_ELT (expanded_bases, idx);
10521 else
10522 /* Substitute to figure out the base class. */
10523 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10524 NULL_TREE);
10525
10526 if (base == error_mark_node)
10527 continue;
10528
10529 base_list = tree_cons (access, base, base_list);
10530 if (BINFO_VIRTUAL_P (pbase_binfo))
10531 TREE_TYPE (base_list) = integer_type_node;
10532 }
10533 }
10534
10535 /* The list is now in reverse order; correct that. */
10536 base_list = nreverse (base_list);
10537
10538 if (pushed_scope)
10539 pop_scope (pushed_scope);
10540 }
10541 /* Now call xref_basetypes to set up all the base-class
10542 information. */
10543 xref_basetypes (type, base_list);
10544
10545 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10546 (int) ATTR_FLAG_TYPE_IN_PLACE,
10547 args, tf_error, NULL_TREE);
10548 fixup_attribute_variants (type);
10549
10550 /* Now that our base classes are set up, enter the scope of the
10551 class, so that name lookups into base classes, etc. will work
10552 correctly. This is precisely analogous to what we do in
10553 begin_class_definition when defining an ordinary non-template
10554 class, except we also need to push the enclosing classes. */
10555 push_nested_class (type);
10556
10557 /* Now members are processed in the order of declaration. */
10558 for (member = CLASSTYPE_DECL_LIST (pattern);
10559 member; member = TREE_CHAIN (member))
10560 {
10561 tree t = TREE_VALUE (member);
10562
10563 if (TREE_PURPOSE (member))
10564 {
10565 if (TYPE_P (t))
10566 {
10567 if (LAMBDA_TYPE_P (t))
10568 /* A closure type for a lambda in an NSDMI or default argument.
10569 Ignore it; it will be regenerated when needed. */
10570 continue;
10571
10572 /* Build new CLASSTYPE_NESTED_UTDS. */
10573
10574 tree newtag;
10575 bool class_template_p;
10576
10577 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10578 && TYPE_LANG_SPECIFIC (t)
10579 && CLASSTYPE_IS_TEMPLATE (t));
10580 /* If the member is a class template, then -- even after
10581 substitution -- there may be dependent types in the
10582 template argument list for the class. We increment
10583 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10584 that function will assume that no types are dependent
10585 when outside of a template. */
10586 if (class_template_p)
10587 ++processing_template_decl;
10588 newtag = tsubst (t, args, tf_error, NULL_TREE);
10589 if (class_template_p)
10590 --processing_template_decl;
10591 if (newtag == error_mark_node)
10592 continue;
10593
10594 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10595 {
10596 tree name = TYPE_IDENTIFIER (t);
10597
10598 if (class_template_p)
10599 /* Unfortunately, lookup_template_class sets
10600 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10601 instantiation (i.e., for the type of a member
10602 template class nested within a template class.)
10603 This behavior is required for
10604 maybe_process_partial_specialization to work
10605 correctly, but is not accurate in this case;
10606 the TAG is not an instantiation of anything.
10607 (The corresponding TEMPLATE_DECL is an
10608 instantiation, but the TYPE is not.) */
10609 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10610
10611 /* Now, we call pushtag to put this NEWTAG into the scope of
10612 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10613 pushtag calling push_template_decl. We don't have to do
10614 this for enums because it will already have been done in
10615 tsubst_enum. */
10616 if (name)
10617 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10618 pushtag (name, newtag, /*tag_scope=*/ts_current);
10619 }
10620 }
10621 else if (DECL_DECLARES_FUNCTION_P (t))
10622 {
10623 tree r;
10624
10625 if (TREE_CODE (t) == TEMPLATE_DECL)
10626 ++processing_template_decl;
10627 r = tsubst (t, args, tf_error, NULL_TREE);
10628 if (TREE_CODE (t) == TEMPLATE_DECL)
10629 --processing_template_decl;
10630 set_current_access_from_decl (r);
10631 finish_member_declaration (r);
10632 /* Instantiate members marked with attribute used. */
10633 if (r != error_mark_node && DECL_PRESERVE_P (r))
10634 mark_used (r);
10635 if (TREE_CODE (r) == FUNCTION_DECL
10636 && DECL_OMP_DECLARE_REDUCTION_P (r))
10637 cp_check_omp_declare_reduction (r);
10638 }
10639 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10640 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10641 /* A closure type for a lambda in an NSDMI or default argument.
10642 Ignore it; it will be regenerated when needed. */;
10643 else
10644 {
10645 /* Build new TYPE_FIELDS. */
10646 if (TREE_CODE (t) == STATIC_ASSERT)
10647 {
10648 tree condition;
10649
10650 ++c_inhibit_evaluation_warnings;
10651 condition =
10652 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10653 tf_warning_or_error, NULL_TREE,
10654 /*integral_constant_expression_p=*/true);
10655 --c_inhibit_evaluation_warnings;
10656
10657 finish_static_assert (condition,
10658 STATIC_ASSERT_MESSAGE (t),
10659 STATIC_ASSERT_SOURCE_LOCATION (t),
10660 /*member_p=*/true);
10661 }
10662 else if (TREE_CODE (t) != CONST_DECL)
10663 {
10664 tree r;
10665 tree vec = NULL_TREE;
10666 int len = 1;
10667
10668 /* The file and line for this declaration, to
10669 assist in error message reporting. Since we
10670 called push_tinst_level above, we don't need to
10671 restore these. */
10672 input_location = DECL_SOURCE_LOCATION (t);
10673
10674 if (TREE_CODE (t) == TEMPLATE_DECL)
10675 ++processing_template_decl;
10676 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10677 if (TREE_CODE (t) == TEMPLATE_DECL)
10678 --processing_template_decl;
10679
10680 if (TREE_CODE (r) == TREE_VEC)
10681 {
10682 /* A capture pack became multiple fields. */
10683 vec = r;
10684 len = TREE_VEC_LENGTH (vec);
10685 }
10686
10687 for (int i = 0; i < len; ++i)
10688 {
10689 if (vec)
10690 r = TREE_VEC_ELT (vec, i);
10691 if (VAR_P (r))
10692 {
10693 /* In [temp.inst]:
10694
10695 [t]he initialization (and any associated
10696 side-effects) of a static data member does
10697 not occur unless the static data member is
10698 itself used in a way that requires the
10699 definition of the static data member to
10700 exist.
10701
10702 Therefore, we do not substitute into the
10703 initialized for the static data member here. */
10704 finish_static_data_member_decl
10705 (r,
10706 /*init=*/NULL_TREE,
10707 /*init_const_expr_p=*/false,
10708 /*asmspec_tree=*/NULL_TREE,
10709 /*flags=*/0);
10710 /* Instantiate members marked with attribute used. */
10711 if (r != error_mark_node && DECL_PRESERVE_P (r))
10712 mark_used (r);
10713 }
10714 else if (TREE_CODE (r) == FIELD_DECL)
10715 {
10716 /* Determine whether R has a valid type and can be
10717 completed later. If R is invalid, then its type
10718 is replaced by error_mark_node. */
10719 tree rtype = TREE_TYPE (r);
10720 if (can_complete_type_without_circularity (rtype))
10721 complete_type (rtype);
10722
10723 if (!complete_or_array_type_p (rtype))
10724 {
10725 /* If R's type couldn't be completed and
10726 it isn't a flexible array member (whose
10727 type is incomplete by definition) give
10728 an error. */
10729 cxx_incomplete_type_error (r, rtype);
10730 TREE_TYPE (r) = error_mark_node;
10731 }
10732 }
10733
10734 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10735 such a thing will already have been added to the field
10736 list by tsubst_enum in finish_member_declaration in the
10737 CLASSTYPE_NESTED_UTDS case above. */
10738 if (!(TREE_CODE (r) == TYPE_DECL
10739 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10740 && DECL_ARTIFICIAL (r)))
10741 {
10742 set_current_access_from_decl (r);
10743 finish_member_declaration (r);
10744 }
10745 }
10746 }
10747 }
10748 }
10749 else
10750 {
10751 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10752 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10753 {
10754 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10755
10756 tree friend_type = t;
10757 bool adjust_processing_template_decl = false;
10758
10759 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10760 {
10761 /* template <class T> friend class C; */
10762 friend_type = tsubst_friend_class (friend_type, args);
10763 adjust_processing_template_decl = true;
10764 }
10765 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10766 {
10767 /* template <class T> friend class C::D; */
10768 friend_type = tsubst (friend_type, args,
10769 tf_warning_or_error, NULL_TREE);
10770 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10771 friend_type = TREE_TYPE (friend_type);
10772 adjust_processing_template_decl = true;
10773 }
10774 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10775 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10776 {
10777 /* This could be either
10778
10779 friend class T::C;
10780
10781 when dependent_type_p is false or
10782
10783 template <class U> friend class T::C;
10784
10785 otherwise. */
10786 /* Bump processing_template_decl in case this is something like
10787 template <class T> friend struct A<T>::B. */
10788 ++processing_template_decl;
10789 friend_type = tsubst (friend_type, args,
10790 tf_warning_or_error, NULL_TREE);
10791 if (dependent_type_p (friend_type))
10792 adjust_processing_template_decl = true;
10793 --processing_template_decl;
10794 }
10795 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10796 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10797 && TYPE_HIDDEN_P (friend_type))
10798 {
10799 /* friend class C;
10800
10801 where C hasn't been declared yet. Let's lookup name
10802 from namespace scope directly, bypassing any name that
10803 come from dependent base class. */
10804 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10805
10806 /* The call to xref_tag_from_type does injection for friend
10807 classes. */
10808 push_nested_namespace (ns);
10809 friend_type =
10810 xref_tag_from_type (friend_type, NULL_TREE,
10811 /*tag_scope=*/ts_current);
10812 pop_nested_namespace (ns);
10813 }
10814 else if (uses_template_parms (friend_type))
10815 /* friend class C<T>; */
10816 friend_type = tsubst (friend_type, args,
10817 tf_warning_or_error, NULL_TREE);
10818 /* Otherwise it's
10819
10820 friend class C;
10821
10822 where C is already declared or
10823
10824 friend class C<int>;
10825
10826 We don't have to do anything in these cases. */
10827
10828 if (adjust_processing_template_decl)
10829 /* Trick make_friend_class into realizing that the friend
10830 we're adding is a template, not an ordinary class. It's
10831 important that we use make_friend_class since it will
10832 perform some error-checking and output cross-reference
10833 information. */
10834 ++processing_template_decl;
10835
10836 if (friend_type != error_mark_node)
10837 make_friend_class (type, friend_type, /*complain=*/false);
10838
10839 if (adjust_processing_template_decl)
10840 --processing_template_decl;
10841 }
10842 else
10843 {
10844 /* Build new DECL_FRIENDLIST. */
10845 tree r;
10846
10847 /* The file and line for this declaration, to
10848 assist in error message reporting. Since we
10849 called push_tinst_level above, we don't need to
10850 restore these. */
10851 input_location = DECL_SOURCE_LOCATION (t);
10852
10853 if (TREE_CODE (t) == TEMPLATE_DECL)
10854 {
10855 ++processing_template_decl;
10856 push_deferring_access_checks (dk_no_check);
10857 }
10858
10859 r = tsubst_friend_function (t, args);
10860 add_friend (type, r, /*complain=*/false);
10861 if (TREE_CODE (t) == TEMPLATE_DECL)
10862 {
10863 pop_deferring_access_checks ();
10864 --processing_template_decl;
10865 }
10866 }
10867 }
10868 }
10869
10870 if (fn_context)
10871 {
10872 /* Restore these before substituting into the lambda capture
10873 initializers. */
10874 cp_unevaluated_operand = saved_unevaluated_operand;
10875 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10876 }
10877
10878 /* Set the file and line number information to whatever is given for
10879 the class itself. This puts error messages involving generated
10880 implicit functions at a predictable point, and the same point
10881 that would be used for non-template classes. */
10882 input_location = DECL_SOURCE_LOCATION (typedecl);
10883
10884 unreverse_member_declarations (type);
10885 finish_struct_1 (type);
10886 TYPE_BEING_DEFINED (type) = 0;
10887
10888 /* We don't instantiate default arguments for member functions. 14.7.1:
10889
10890 The implicit instantiation of a class template specialization causes
10891 the implicit instantiation of the declarations, but not of the
10892 definitions or default arguments, of the class member functions,
10893 member classes, static data members and member templates.... */
10894
10895 /* Some typedefs referenced from within the template code need to be access
10896 checked at template instantiation time, i.e now. These types were
10897 added to the template at parsing time. Let's get those and perform
10898 the access checks then. */
10899 perform_typedefs_access_check (pattern, args);
10900 perform_deferred_access_checks (tf_warning_or_error);
10901 pop_nested_class ();
10902 maximum_field_alignment = saved_maximum_field_alignment;
10903 if (!fn_context)
10904 pop_from_top_level ();
10905 pop_deferring_access_checks ();
10906 pop_tinst_level ();
10907
10908 /* The vtable for a template class can be emitted in any translation
10909 unit in which the class is instantiated. When there is no key
10910 method, however, finish_struct_1 will already have added TYPE to
10911 the keyed_classes. */
10912 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10913 vec_safe_push (keyed_classes, type);
10914
10915 return type;
10916 }
10917
10918 /* Wrapper for instantiate_class_template_1. */
10919
10920 tree
10921 instantiate_class_template (tree type)
10922 {
10923 tree ret;
10924 timevar_push (TV_TEMPLATE_INST);
10925 ret = instantiate_class_template_1 (type);
10926 timevar_pop (TV_TEMPLATE_INST);
10927 return ret;
10928 }
10929
10930 static tree
10931 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10932 {
10933 tree r;
10934
10935 if (!t)
10936 r = t;
10937 else if (TYPE_P (t))
10938 r = tsubst (t, args, complain, in_decl);
10939 else
10940 {
10941 if (!(complain & tf_warning))
10942 ++c_inhibit_evaluation_warnings;
10943 r = tsubst_expr (t, args, complain, in_decl,
10944 /*integral_constant_expression_p=*/true);
10945 if (!(complain & tf_warning))
10946 --c_inhibit_evaluation_warnings;
10947 }
10948 return r;
10949 }
10950
10951 /* Given a function parameter pack TMPL_PARM and some function parameters
10952 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10953 and set *SPEC_P to point at the next point in the list. */
10954
10955 tree
10956 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10957 {
10958 /* Collect all of the extra "packed" parameters into an
10959 argument pack. */
10960 tree parmvec;
10961 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10962 tree spec_parm = *spec_p;
10963 int i, len;
10964
10965 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10966 if (tmpl_parm
10967 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10968 break;
10969
10970 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10971 parmvec = make_tree_vec (len);
10972 spec_parm = *spec_p;
10973 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10974 {
10975 tree elt = spec_parm;
10976 if (DECL_PACK_P (elt))
10977 elt = make_pack_expansion (elt);
10978 TREE_VEC_ELT (parmvec, i) = elt;
10979 }
10980
10981 /* Build the argument packs. */
10982 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10983 *spec_p = spec_parm;
10984
10985 return argpack;
10986 }
10987
10988 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10989 NONTYPE_ARGUMENT_PACK. */
10990
10991 static tree
10992 make_fnparm_pack (tree spec_parm)
10993 {
10994 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10995 }
10996
10997 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10998 pack expansion with no extra args, 2 if it has extra args, or 0
10999 if it is not a pack expansion. */
11000
11001 static int
11002 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11003 {
11004 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11005 if (i >= TREE_VEC_LENGTH (vec))
11006 return 0;
11007 tree elt = TREE_VEC_ELT (vec, i);
11008 if (DECL_P (elt))
11009 /* A decl pack is itself an expansion. */
11010 elt = TREE_TYPE (elt);
11011 if (!PACK_EXPANSION_P (elt))
11012 return 0;
11013 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11014 return 2;
11015 return 1;
11016 }
11017
11018
11019 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11020
11021 static tree
11022 make_argument_pack_select (tree arg_pack, unsigned index)
11023 {
11024 tree aps = make_node (ARGUMENT_PACK_SELECT);
11025
11026 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11027 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11028
11029 return aps;
11030 }
11031
11032 /* This is a subroutine of tsubst_pack_expansion.
11033
11034 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11035 mechanism to store the (non complete list of) arguments of the
11036 substitution and return a non substituted pack expansion, in order
11037 to wait for when we have enough arguments to really perform the
11038 substitution. */
11039
11040 static bool
11041 use_pack_expansion_extra_args_p (tree parm_packs,
11042 int arg_pack_len,
11043 bool has_empty_arg)
11044 {
11045 /* If one pack has an expansion and another pack has a normal
11046 argument or if one pack has an empty argument and an another
11047 one hasn't then tsubst_pack_expansion cannot perform the
11048 substitution and need to fall back on the
11049 PACK_EXPANSION_EXTRA mechanism. */
11050 if (parm_packs == NULL_TREE)
11051 return false;
11052 else if (has_empty_arg)
11053 return true;
11054
11055 bool has_expansion_arg = false;
11056 for (int i = 0 ; i < arg_pack_len; ++i)
11057 {
11058 bool has_non_expansion_arg = false;
11059 for (tree parm_pack = parm_packs;
11060 parm_pack;
11061 parm_pack = TREE_CHAIN (parm_pack))
11062 {
11063 tree arg = TREE_VALUE (parm_pack);
11064
11065 int exp = argument_pack_element_is_expansion_p (arg, i);
11066 if (exp == 2)
11067 /* We can't substitute a pack expansion with extra args into
11068 our pattern. */
11069 return true;
11070 else if (exp)
11071 has_expansion_arg = true;
11072 else
11073 has_non_expansion_arg = true;
11074 }
11075
11076 if (has_expansion_arg && has_non_expansion_arg)
11077 return true;
11078 }
11079 return false;
11080 }
11081
11082 /* [temp.variadic]/6 says that:
11083
11084 The instantiation of a pack expansion [...]
11085 produces a list E1,E2, ..., En, where N is the number of elements
11086 in the pack expansion parameters.
11087
11088 This subroutine of tsubst_pack_expansion produces one of these Ei.
11089
11090 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11091 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11092 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11093 INDEX is the index 'i' of the element Ei to produce. ARGS,
11094 COMPLAIN, and IN_DECL are the same parameters as for the
11095 tsubst_pack_expansion function.
11096
11097 The function returns the resulting Ei upon successful completion,
11098 or error_mark_node.
11099
11100 Note that this function possibly modifies the ARGS parameter, so
11101 it's the responsibility of the caller to restore it. */
11102
11103 static tree
11104 gen_elem_of_pack_expansion_instantiation (tree pattern,
11105 tree parm_packs,
11106 unsigned index,
11107 tree args /* This parm gets
11108 modified. */,
11109 tsubst_flags_t complain,
11110 tree in_decl)
11111 {
11112 tree t;
11113 bool ith_elem_is_expansion = false;
11114
11115 /* For each parameter pack, change the substitution of the parameter
11116 pack to the ith argument in its argument pack, then expand the
11117 pattern. */
11118 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11119 {
11120 tree parm = TREE_PURPOSE (pack);
11121 tree arg_pack = TREE_VALUE (pack);
11122 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11123
11124 ith_elem_is_expansion |=
11125 argument_pack_element_is_expansion_p (arg_pack, index);
11126
11127 /* Select the Ith argument from the pack. */
11128 if (TREE_CODE (parm) == PARM_DECL
11129 || VAR_P (parm)
11130 || TREE_CODE (parm) == FIELD_DECL)
11131 {
11132 if (index == 0)
11133 {
11134 aps = make_argument_pack_select (arg_pack, index);
11135 if (!mark_used (parm, complain) && !(complain & tf_error))
11136 return error_mark_node;
11137 register_local_specialization (aps, parm);
11138 }
11139 else
11140 aps = retrieve_local_specialization (parm);
11141 }
11142 else
11143 {
11144 int idx, level;
11145 template_parm_level_and_index (parm, &level, &idx);
11146
11147 if (index == 0)
11148 {
11149 aps = make_argument_pack_select (arg_pack, index);
11150 /* Update the corresponding argument. */
11151 TMPL_ARG (args, level, idx) = aps;
11152 }
11153 else
11154 /* Re-use the ARGUMENT_PACK_SELECT. */
11155 aps = TMPL_ARG (args, level, idx);
11156 }
11157 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11158 }
11159
11160 /* Substitute into the PATTERN with the (possibly altered)
11161 arguments. */
11162 if (pattern == in_decl)
11163 /* Expanding a fixed parameter pack from
11164 coerce_template_parameter_pack. */
11165 t = tsubst_decl (pattern, args, complain);
11166 else if (pattern == error_mark_node)
11167 t = error_mark_node;
11168 else if (constraint_p (pattern))
11169 {
11170 if (processing_template_decl)
11171 t = tsubst_constraint (pattern, args, complain, in_decl);
11172 else
11173 t = (constraints_satisfied_p (pattern, args)
11174 ? boolean_true_node : boolean_false_node);
11175 }
11176 else if (!TYPE_P (pattern))
11177 t = tsubst_expr (pattern, args, complain, in_decl,
11178 /*integral_constant_expression_p=*/false);
11179 else
11180 t = tsubst (pattern, args, complain, in_decl);
11181
11182 /* If the Ith argument pack element is a pack expansion, then
11183 the Ith element resulting from the substituting is going to
11184 be a pack expansion as well. */
11185 if (ith_elem_is_expansion)
11186 t = make_pack_expansion (t, complain);
11187
11188 return t;
11189 }
11190
11191 /* When the unexpanded parameter pack in a fold expression expands to an empty
11192 sequence, the value of the expression is as follows; the program is
11193 ill-formed if the operator is not listed in this table.
11194
11195 && true
11196 || false
11197 , void() */
11198
11199 tree
11200 expand_empty_fold (tree t, tsubst_flags_t complain)
11201 {
11202 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11203 if (!FOLD_EXPR_MODIFY_P (t))
11204 switch (code)
11205 {
11206 case TRUTH_ANDIF_EXPR:
11207 return boolean_true_node;
11208 case TRUTH_ORIF_EXPR:
11209 return boolean_false_node;
11210 case COMPOUND_EXPR:
11211 return void_node;
11212 default:
11213 break;
11214 }
11215
11216 if (complain & tf_error)
11217 error_at (location_of (t),
11218 "fold of empty expansion over %O", code);
11219 return error_mark_node;
11220 }
11221
11222 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11223 form an expression that combines the two terms using the
11224 operator of T. */
11225
11226 static tree
11227 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11228 {
11229 tree op = FOLD_EXPR_OP (t);
11230 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11231
11232 // Handle compound assignment operators.
11233 if (FOLD_EXPR_MODIFY_P (t))
11234 return build_x_modify_expr (input_location, left, code, right, complain);
11235
11236 switch (code)
11237 {
11238 case COMPOUND_EXPR:
11239 return build_x_compound_expr (input_location, left, right, complain);
11240 case DOTSTAR_EXPR:
11241 return build_m_component_ref (left, right, complain);
11242 default:
11243 return build_x_binary_op (input_location, code,
11244 left, TREE_CODE (left),
11245 right, TREE_CODE (right),
11246 /*overload=*/NULL,
11247 complain);
11248 }
11249 }
11250
11251 /* Substitute ARGS into the pack of a fold expression T. */
11252
11253 static inline tree
11254 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11255 {
11256 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11257 }
11258
11259 /* Substitute ARGS into the pack of a fold expression T. */
11260
11261 static inline tree
11262 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11263 {
11264 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11265 }
11266
11267 /* Expand a PACK of arguments into a grouped as left fold.
11268 Given a pack containing elements A0, A1, ..., An and an
11269 operator @, this builds the expression:
11270
11271 ((A0 @ A1) @ A2) ... @ An
11272
11273 Note that PACK must not be empty.
11274
11275 The operator is defined by the original fold expression T. */
11276
11277 static tree
11278 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11279 {
11280 tree left = TREE_VEC_ELT (pack, 0);
11281 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11282 {
11283 tree right = TREE_VEC_ELT (pack, i);
11284 left = fold_expression (t, left, right, complain);
11285 }
11286 return left;
11287 }
11288
11289 /* Substitute into a unary left fold expression. */
11290
11291 static tree
11292 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11293 tree in_decl)
11294 {
11295 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11296 if (pack == error_mark_node)
11297 return error_mark_node;
11298 if (PACK_EXPANSION_P (pack))
11299 {
11300 tree r = copy_node (t);
11301 FOLD_EXPR_PACK (r) = pack;
11302 return r;
11303 }
11304 if (TREE_VEC_LENGTH (pack) == 0)
11305 return expand_empty_fold (t, complain);
11306 else
11307 return expand_left_fold (t, pack, complain);
11308 }
11309
11310 /* Substitute into a binary left fold expression.
11311
11312 Do ths by building a single (non-empty) vector of argumnts and
11313 building the expression from those elements. */
11314
11315 static tree
11316 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11317 tree in_decl)
11318 {
11319 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11320 if (pack == error_mark_node)
11321 return error_mark_node;
11322 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11323 if (init == error_mark_node)
11324 return error_mark_node;
11325
11326 if (PACK_EXPANSION_P (pack))
11327 {
11328 tree r = copy_node (t);
11329 FOLD_EXPR_PACK (r) = pack;
11330 FOLD_EXPR_INIT (r) = init;
11331 return r;
11332 }
11333
11334 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11335 TREE_VEC_ELT (vec, 0) = init;
11336 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11337 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11338
11339 return expand_left_fold (t, vec, complain);
11340 }
11341
11342 /* Expand a PACK of arguments into a grouped as right fold.
11343 Given a pack containing elementns A0, A1, ..., and an
11344 operator @, this builds the expression:
11345
11346 A0@ ... (An-2 @ (An-1 @ An))
11347
11348 Note that PACK must not be empty.
11349
11350 The operator is defined by the original fold expression T. */
11351
11352 tree
11353 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11354 {
11355 // Build the expression.
11356 int n = TREE_VEC_LENGTH (pack);
11357 tree right = TREE_VEC_ELT (pack, n - 1);
11358 for (--n; n != 0; --n)
11359 {
11360 tree left = TREE_VEC_ELT (pack, n - 1);
11361 right = fold_expression (t, left, right, complain);
11362 }
11363 return right;
11364 }
11365
11366 /* Substitute into a unary right fold expression. */
11367
11368 static tree
11369 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11370 tree in_decl)
11371 {
11372 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11373 if (pack == error_mark_node)
11374 return error_mark_node;
11375 if (PACK_EXPANSION_P (pack))
11376 {
11377 tree r = copy_node (t);
11378 FOLD_EXPR_PACK (r) = pack;
11379 return r;
11380 }
11381 if (TREE_VEC_LENGTH (pack) == 0)
11382 return expand_empty_fold (t, complain);
11383 else
11384 return expand_right_fold (t, pack, complain);
11385 }
11386
11387 /* Substitute into a binary right fold expression.
11388
11389 Do ths by building a single (non-empty) vector of arguments and
11390 building the expression from those elements. */
11391
11392 static tree
11393 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11394 tree in_decl)
11395 {
11396 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11397 if (pack == error_mark_node)
11398 return error_mark_node;
11399 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11400 if (init == error_mark_node)
11401 return error_mark_node;
11402
11403 if (PACK_EXPANSION_P (pack))
11404 {
11405 tree r = copy_node (t);
11406 FOLD_EXPR_PACK (r) = pack;
11407 FOLD_EXPR_INIT (r) = init;
11408 return r;
11409 }
11410
11411 int n = TREE_VEC_LENGTH (pack);
11412 tree vec = make_tree_vec (n + 1);
11413 for (int i = 0; i < n; ++i)
11414 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11415 TREE_VEC_ELT (vec, n) = init;
11416
11417 return expand_right_fold (t, vec, complain);
11418 }
11419
11420 /* Walk through the pattern of a pack expansion, adding everything in
11421 local_specializations to a list. */
11422
11423 struct el_data
11424 {
11425 tree extra;
11426 tsubst_flags_t complain;
11427 };
11428 static tree
11429 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11430 {
11431 el_data &data = *reinterpret_cast<el_data*>(data_);
11432 tree *extra = &data.extra;
11433 tsubst_flags_t complain = data.complain;
11434 if (tree spec = retrieve_local_specialization (*tp))
11435 {
11436 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11437 {
11438 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11439 tree args = ARGUMENT_PACK_ARGS (spec);
11440 if (TREE_VEC_LENGTH (args) == 1)
11441 {
11442 tree elt = TREE_VEC_ELT (args, 0);
11443 if (PACK_EXPANSION_P (elt))
11444 elt = PACK_EXPANSION_PATTERN (elt);
11445 if (DECL_PACK_P (elt))
11446 spec = elt;
11447 }
11448 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11449 {
11450 /* Handle lambda capture here, since we aren't doing any
11451 substitution now, and so tsubst_copy won't call
11452 process_outer_var_ref. */
11453 tree args = ARGUMENT_PACK_ARGS (spec);
11454 int len = TREE_VEC_LENGTH (args);
11455 for (int i = 0; i < len; ++i)
11456 {
11457 tree arg = TREE_VEC_ELT (args, i);
11458 tree carg = arg;
11459 if (outer_automatic_var_p (arg))
11460 carg = process_outer_var_ref (arg, complain);
11461 if (carg != arg)
11462 {
11463 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11464 proxies. */
11465 if (i == 0)
11466 {
11467 spec = copy_node (spec);
11468 args = copy_node (args);
11469 SET_ARGUMENT_PACK_ARGS (spec, args);
11470 register_local_specialization (spec, *tp);
11471 }
11472 TREE_VEC_ELT (args, i) = carg;
11473 }
11474 }
11475 }
11476 }
11477 if (outer_automatic_var_p (spec))
11478 spec = process_outer_var_ref (spec, complain);
11479 *extra = tree_cons (*tp, spec, *extra);
11480 }
11481 return NULL_TREE;
11482 }
11483 static tree
11484 extract_local_specs (tree pattern, tsubst_flags_t complain)
11485 {
11486 el_data data = { NULL_TREE, complain };
11487 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11488 return data.extra;
11489 }
11490
11491 /* Substitute ARGS into T, which is an pack expansion
11492 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11493 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11494 (if only a partial substitution could be performed) or
11495 ERROR_MARK_NODE if there was an error. */
11496 tree
11497 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11498 tree in_decl)
11499 {
11500 tree pattern;
11501 tree pack, packs = NULL_TREE;
11502 bool unsubstituted_packs = false;
11503 bool unsubstituted_fn_pack = false;
11504 int i, len = -1;
11505 tree result;
11506 hash_map<tree, tree> *saved_local_specializations = NULL;
11507 bool need_local_specializations = false;
11508 int levels;
11509
11510 gcc_assert (PACK_EXPANSION_P (t));
11511 pattern = PACK_EXPANSION_PATTERN (t);
11512
11513 /* Add in any args remembered from an earlier partial instantiation. */
11514 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11515 if (extra && TREE_CODE (extra) == TREE_LIST)
11516 {
11517 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11518 {
11519 /* The partial instantiation involved local declarations collected in
11520 extract_local_specs; map from the general template to our local
11521 context. */
11522 tree gen = TREE_PURPOSE (elt);
11523 tree inst = TREE_VALUE (elt);
11524 if (DECL_PACK_P (inst))
11525 inst = retrieve_local_specialization (inst);
11526 /* else inst is already a full instantiation of the pack. */
11527 register_local_specialization (inst, gen);
11528 }
11529 gcc_assert (!TREE_PURPOSE (extra));
11530 extra = TREE_VALUE (extra);
11531 }
11532 args = add_to_template_args (extra, args);
11533
11534 levels = TMPL_ARGS_DEPTH (args);
11535
11536 /* Determine the argument packs that will instantiate the parameter
11537 packs used in the expansion expression. While we're at it,
11538 compute the number of arguments to be expanded and make sure it
11539 is consistent. */
11540 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11541 pack = TREE_CHAIN (pack))
11542 {
11543 tree parm_pack = TREE_VALUE (pack);
11544 tree arg_pack = NULL_TREE;
11545 tree orig_arg = NULL_TREE;
11546 int level = 0;
11547
11548 if (TREE_CODE (parm_pack) == BASES)
11549 {
11550 gcc_assert (parm_pack == pattern);
11551 if (BASES_DIRECT (parm_pack))
11552 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11553 args, complain, in_decl, false));
11554 else
11555 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11556 args, complain, in_decl, false));
11557 }
11558 else if (builtin_pack_call_p (parm_pack))
11559 {
11560 /* ??? Support use in other patterns. */
11561 gcc_assert (parm_pack == pattern);
11562 return expand_builtin_pack_call (parm_pack, args,
11563 complain, in_decl);
11564 }
11565 else if (TREE_CODE (parm_pack) == PARM_DECL)
11566 {
11567 /* We know we have correct local_specializations if this
11568 expansion is at function scope, or if we're dealing with a
11569 local parameter in a requires expression; for the latter,
11570 tsubst_requires_expr set it up appropriately. */
11571 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11572 arg_pack = retrieve_local_specialization (parm_pack);
11573 else
11574 /* We can't rely on local_specializations for a parameter
11575 name used later in a function declaration (such as in a
11576 late-specified return type). Even if it exists, it might
11577 have the wrong value for a recursive call. */
11578 need_local_specializations = true;
11579
11580 if (!arg_pack)
11581 {
11582 /* This parameter pack was used in an unevaluated context. Just
11583 make a dummy decl, since it's only used for its type. */
11584 arg_pack = tsubst_decl (parm_pack, args, complain);
11585 if (arg_pack && DECL_PACK_P (arg_pack))
11586 /* Partial instantiation of the parm_pack, we can't build
11587 up an argument pack yet. */
11588 arg_pack = NULL_TREE;
11589 else
11590 arg_pack = make_fnparm_pack (arg_pack);
11591 }
11592 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11593 /* This argument pack isn't fully instantiated yet. We set this
11594 flag rather than clear arg_pack because we do want to do the
11595 optimization below, and we don't want to substitute directly
11596 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11597 where it isn't expected). */
11598 unsubstituted_fn_pack = true;
11599 }
11600 else if (is_normal_capture_proxy (parm_pack))
11601 {
11602 arg_pack = retrieve_local_specialization (parm_pack);
11603 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11604 unsubstituted_fn_pack = true;
11605 }
11606 else
11607 {
11608 int idx;
11609 template_parm_level_and_index (parm_pack, &level, &idx);
11610
11611 if (level <= levels)
11612 arg_pack = TMPL_ARG (args, level, idx);
11613 }
11614
11615 orig_arg = arg_pack;
11616 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11617 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11618
11619 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11620 /* This can only happen if we forget to expand an argument
11621 pack somewhere else. Just return an error, silently. */
11622 {
11623 result = make_tree_vec (1);
11624 TREE_VEC_ELT (result, 0) = error_mark_node;
11625 return result;
11626 }
11627
11628 if (arg_pack)
11629 {
11630 int my_len =
11631 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11632
11633 /* Don't bother trying to do a partial substitution with
11634 incomplete packs; we'll try again after deduction. */
11635 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11636 return t;
11637
11638 if (len < 0)
11639 len = my_len;
11640 else if (len != my_len
11641 && !unsubstituted_fn_pack)
11642 {
11643 if (!(complain & tf_error))
11644 /* Fail quietly. */;
11645 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11646 error ("mismatched argument pack lengths while expanding %qT",
11647 pattern);
11648 else
11649 error ("mismatched argument pack lengths while expanding %qE",
11650 pattern);
11651 return error_mark_node;
11652 }
11653
11654 /* Keep track of the parameter packs and their corresponding
11655 argument packs. */
11656 packs = tree_cons (parm_pack, arg_pack, packs);
11657 TREE_TYPE (packs) = orig_arg;
11658 }
11659 else
11660 {
11661 /* We can't substitute for this parameter pack. We use a flag as
11662 well as the missing_level counter because function parameter
11663 packs don't have a level. */
11664 gcc_assert (processing_template_decl);
11665 unsubstituted_packs = true;
11666 }
11667 }
11668
11669 /* If the expansion is just T..., return the matching argument pack, unless
11670 we need to call convert_from_reference on all the elements. This is an
11671 important optimization; see c++/68422. */
11672 if (!unsubstituted_packs
11673 && TREE_PURPOSE (packs) == pattern)
11674 {
11675 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11676
11677 /* If the argument pack is a single pack expansion, pull it out. */
11678 if (TREE_VEC_LENGTH (args) == 1
11679 && pack_expansion_args_count (args))
11680 return TREE_VEC_ELT (args, 0);
11681
11682 /* Types need no adjustment, nor does sizeof..., and if we still have
11683 some pack expansion args we won't do anything yet. */
11684 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11685 || PACK_EXPANSION_SIZEOF_P (t)
11686 || pack_expansion_args_count (args))
11687 return args;
11688 /* Also optimize expression pack expansions if we can tell that the
11689 elements won't have reference type. */
11690 tree type = TREE_TYPE (pattern);
11691 if (type && TREE_CODE (type) != REFERENCE_TYPE
11692 && !PACK_EXPANSION_P (type)
11693 && !WILDCARD_TYPE_P (type))
11694 return args;
11695 /* Otherwise use the normal path so we get convert_from_reference. */
11696 }
11697
11698 /* We cannot expand this expansion expression, because we don't have
11699 all of the argument packs we need. */
11700 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11701 {
11702 /* We got some full packs, but we can't substitute them in until we
11703 have values for all the packs. So remember these until then. */
11704
11705 t = make_pack_expansion (pattern, complain);
11706 tree extra = args;
11707 if (local_specializations)
11708 if (tree locals = extract_local_specs (pattern, complain))
11709 extra = tree_cons (NULL_TREE, extra, locals);
11710 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11711 return t;
11712 }
11713 else if (unsubstituted_packs)
11714 {
11715 /* There were no real arguments, we're just replacing a parameter
11716 pack with another version of itself. Substitute into the
11717 pattern and return a PACK_EXPANSION_*. The caller will need to
11718 deal with that. */
11719 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11720 t = tsubst_expr (pattern, args, complain, in_decl,
11721 /*integral_constant_expression_p=*/false);
11722 else
11723 t = tsubst (pattern, args, complain, in_decl);
11724 t = make_pack_expansion (t, complain);
11725 return t;
11726 }
11727
11728 gcc_assert (len >= 0);
11729
11730 if (need_local_specializations)
11731 {
11732 /* We're in a late-specified return type, so create our own local
11733 specializations map; the current map is either NULL or (in the
11734 case of recursive unification) might have bindings that we don't
11735 want to use or alter. */
11736 saved_local_specializations = local_specializations;
11737 local_specializations = new hash_map<tree, tree>;
11738 }
11739
11740 /* For each argument in each argument pack, substitute into the
11741 pattern. */
11742 result = make_tree_vec (len);
11743 tree elem_args = copy_template_args (args);
11744 for (i = 0; i < len; ++i)
11745 {
11746 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11747 i,
11748 elem_args, complain,
11749 in_decl);
11750 TREE_VEC_ELT (result, i) = t;
11751 if (t == error_mark_node)
11752 {
11753 result = error_mark_node;
11754 break;
11755 }
11756 }
11757
11758 /* Update ARGS to restore the substitution from parameter packs to
11759 their argument packs. */
11760 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11761 {
11762 tree parm = TREE_PURPOSE (pack);
11763
11764 if (TREE_CODE (parm) == PARM_DECL
11765 || VAR_P (parm)
11766 || TREE_CODE (parm) == FIELD_DECL)
11767 register_local_specialization (TREE_TYPE (pack), parm);
11768 else
11769 {
11770 int idx, level;
11771
11772 if (TREE_VALUE (pack) == NULL_TREE)
11773 continue;
11774
11775 template_parm_level_and_index (parm, &level, &idx);
11776
11777 /* Update the corresponding argument. */
11778 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11779 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11780 TREE_TYPE (pack);
11781 else
11782 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11783 }
11784 }
11785
11786 if (need_local_specializations)
11787 {
11788 delete local_specializations;
11789 local_specializations = saved_local_specializations;
11790 }
11791
11792 /* If the dependent pack arguments were such that we end up with only a
11793 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11794 if (len == 1 && TREE_CODE (result) == TREE_VEC
11795 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11796 return TREE_VEC_ELT (result, 0);
11797
11798 return result;
11799 }
11800
11801 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11802 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11803 parameter packs; all parms generated from a function parameter pack will
11804 have the same DECL_PARM_INDEX. */
11805
11806 tree
11807 get_pattern_parm (tree parm, tree tmpl)
11808 {
11809 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11810 tree patparm;
11811
11812 if (DECL_ARTIFICIAL (parm))
11813 {
11814 for (patparm = DECL_ARGUMENTS (pattern);
11815 patparm; patparm = DECL_CHAIN (patparm))
11816 if (DECL_ARTIFICIAL (patparm)
11817 && DECL_NAME (parm) == DECL_NAME (patparm))
11818 break;
11819 }
11820 else
11821 {
11822 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11823 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11824 gcc_assert (DECL_PARM_INDEX (patparm)
11825 == DECL_PARM_INDEX (parm));
11826 }
11827
11828 return patparm;
11829 }
11830
11831 /* Make an argument pack out of the TREE_VEC VEC. */
11832
11833 static tree
11834 make_argument_pack (tree vec)
11835 {
11836 tree pack;
11837 tree elt = TREE_VEC_ELT (vec, 0);
11838 if (TYPE_P (elt))
11839 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11840 else
11841 {
11842 pack = make_node (NONTYPE_ARGUMENT_PACK);
11843 TREE_CONSTANT (pack) = 1;
11844 }
11845 SET_ARGUMENT_PACK_ARGS (pack, vec);
11846 return pack;
11847 }
11848
11849 /* Return an exact copy of template args T that can be modified
11850 independently. */
11851
11852 static tree
11853 copy_template_args (tree t)
11854 {
11855 if (t == error_mark_node)
11856 return t;
11857
11858 int len = TREE_VEC_LENGTH (t);
11859 tree new_vec = make_tree_vec (len);
11860
11861 for (int i = 0; i < len; ++i)
11862 {
11863 tree elt = TREE_VEC_ELT (t, i);
11864 if (elt && TREE_CODE (elt) == TREE_VEC)
11865 elt = copy_template_args (elt);
11866 TREE_VEC_ELT (new_vec, i) = elt;
11867 }
11868
11869 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11870 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11871
11872 return new_vec;
11873 }
11874
11875 /* Substitute ARGS into the vector or list of template arguments T. */
11876
11877 static tree
11878 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11879 {
11880 tree orig_t = t;
11881 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11882 tree *elts;
11883
11884 if (t == error_mark_node)
11885 return error_mark_node;
11886
11887 len = TREE_VEC_LENGTH (t);
11888 elts = XALLOCAVEC (tree, len);
11889
11890 for (i = 0; i < len; i++)
11891 {
11892 tree orig_arg = TREE_VEC_ELT (t, i);
11893 tree new_arg;
11894
11895 if (TREE_CODE (orig_arg) == TREE_VEC)
11896 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11897 else if (PACK_EXPANSION_P (orig_arg))
11898 {
11899 /* Substitute into an expansion expression. */
11900 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11901
11902 if (TREE_CODE (new_arg) == TREE_VEC)
11903 /* Add to the expanded length adjustment the number of
11904 expanded arguments. We subtract one from this
11905 measurement, because the argument pack expression
11906 itself is already counted as 1 in
11907 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11908 the argument pack is empty. */
11909 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11910 }
11911 else if (ARGUMENT_PACK_P (orig_arg))
11912 {
11913 /* Substitute into each of the arguments. */
11914 new_arg = TYPE_P (orig_arg)
11915 ? cxx_make_type (TREE_CODE (orig_arg))
11916 : make_node (TREE_CODE (orig_arg));
11917
11918 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11919 args, complain, in_decl);
11920 if (pack_args == error_mark_node)
11921 new_arg = error_mark_node;
11922 else
11923 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11924
11925 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11926 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11927 }
11928 else
11929 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11930
11931 if (new_arg == error_mark_node)
11932 return error_mark_node;
11933
11934 elts[i] = new_arg;
11935 if (new_arg != orig_arg)
11936 need_new = 1;
11937 }
11938
11939 if (!need_new)
11940 return t;
11941
11942 /* Make space for the expanded arguments coming from template
11943 argument packs. */
11944 t = make_tree_vec (len + expanded_len_adjust);
11945 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11946 arguments for a member template.
11947 In that case each TREE_VEC in ORIG_T represents a level of template
11948 arguments, and ORIG_T won't carry any non defaulted argument count.
11949 It will rather be the nested TREE_VECs that will carry one.
11950 In other words, ORIG_T carries a non defaulted argument count only
11951 if it doesn't contain any nested TREE_VEC. */
11952 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11953 {
11954 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11955 count += expanded_len_adjust;
11956 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11957 }
11958 for (i = 0, out = 0; i < len; i++)
11959 {
11960 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11961 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11962 && TREE_CODE (elts[i]) == TREE_VEC)
11963 {
11964 int idx;
11965
11966 /* Now expand the template argument pack "in place". */
11967 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11968 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11969 }
11970 else
11971 {
11972 TREE_VEC_ELT (t, out) = elts[i];
11973 out++;
11974 }
11975 }
11976
11977 return t;
11978 }
11979
11980 /* Substitute ARGS into one level PARMS of template parameters. */
11981
11982 static tree
11983 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11984 {
11985 if (parms == error_mark_node)
11986 return error_mark_node;
11987
11988 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11989
11990 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11991 {
11992 tree tuple = TREE_VEC_ELT (parms, i);
11993
11994 if (tuple == error_mark_node)
11995 continue;
11996
11997 TREE_VEC_ELT (new_vec, i) =
11998 tsubst_template_parm (tuple, args, complain);
11999 }
12000
12001 return new_vec;
12002 }
12003
12004 /* Return the result of substituting ARGS into the template parameters
12005 given by PARMS. If there are m levels of ARGS and m + n levels of
12006 PARMS, then the result will contain n levels of PARMS. For
12007 example, if PARMS is `template <class T> template <class U>
12008 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12009 result will be `template <int*, double, class V>'. */
12010
12011 static tree
12012 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12013 {
12014 tree r = NULL_TREE;
12015 tree* new_parms;
12016
12017 /* When substituting into a template, we must set
12018 PROCESSING_TEMPLATE_DECL as the template parameters may be
12019 dependent if they are based on one-another, and the dependency
12020 predicates are short-circuit outside of templates. */
12021 ++processing_template_decl;
12022
12023 for (new_parms = &r;
12024 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12025 new_parms = &(TREE_CHAIN (*new_parms)),
12026 parms = TREE_CHAIN (parms))
12027 {
12028 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12029 args, complain);
12030 *new_parms =
12031 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12032 - TMPL_ARGS_DEPTH (args)),
12033 new_vec, NULL_TREE);
12034 }
12035
12036 --processing_template_decl;
12037
12038 return r;
12039 }
12040
12041 /* Return the result of substituting ARGS into one template parameter
12042 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12043 parameter and which TREE_PURPOSE is the default argument of the
12044 template parameter. */
12045
12046 static tree
12047 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12048 {
12049 tree default_value, parm_decl;
12050
12051 if (args == NULL_TREE
12052 || t == NULL_TREE
12053 || t == error_mark_node)
12054 return t;
12055
12056 gcc_assert (TREE_CODE (t) == TREE_LIST);
12057
12058 default_value = TREE_PURPOSE (t);
12059 parm_decl = TREE_VALUE (t);
12060
12061 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12062 if (TREE_CODE (parm_decl) == PARM_DECL
12063 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12064 parm_decl = error_mark_node;
12065 default_value = tsubst_template_arg (default_value, args,
12066 complain, NULL_TREE);
12067
12068 return build_tree_list (default_value, parm_decl);
12069 }
12070
12071 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12072 type T. If T is not an aggregate or enumeration type, it is
12073 handled as if by tsubst. IN_DECL is as for tsubst. If
12074 ENTERING_SCOPE is nonzero, T is the context for a template which
12075 we are presently tsubst'ing. Return the substituted value. */
12076
12077 static tree
12078 tsubst_aggr_type (tree t,
12079 tree args,
12080 tsubst_flags_t complain,
12081 tree in_decl,
12082 int entering_scope)
12083 {
12084 if (t == NULL_TREE)
12085 return NULL_TREE;
12086
12087 switch (TREE_CODE (t))
12088 {
12089 case RECORD_TYPE:
12090 if (TYPE_PTRMEMFUNC_P (t))
12091 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12092
12093 /* Fall through. */
12094 case ENUMERAL_TYPE:
12095 case UNION_TYPE:
12096 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12097 {
12098 tree argvec;
12099 tree context;
12100 tree r;
12101 int saved_unevaluated_operand;
12102 int saved_inhibit_evaluation_warnings;
12103
12104 /* In "sizeof(X<I>)" we need to evaluate "I". */
12105 saved_unevaluated_operand = cp_unevaluated_operand;
12106 cp_unevaluated_operand = 0;
12107 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12108 c_inhibit_evaluation_warnings = 0;
12109
12110 /* First, determine the context for the type we are looking
12111 up. */
12112 context = TYPE_CONTEXT (t);
12113 if (context && TYPE_P (context))
12114 {
12115 context = tsubst_aggr_type (context, args, complain,
12116 in_decl, /*entering_scope=*/1);
12117 /* If context is a nested class inside a class template,
12118 it may still need to be instantiated (c++/33959). */
12119 context = complete_type (context);
12120 }
12121
12122 /* Then, figure out what arguments are appropriate for the
12123 type we are trying to find. For example, given:
12124
12125 template <class T> struct S;
12126 template <class T, class U> void f(T, U) { S<U> su; }
12127
12128 and supposing that we are instantiating f<int, double>,
12129 then our ARGS will be {int, double}, but, when looking up
12130 S we only want {double}. */
12131 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12132 complain, in_decl);
12133 if (argvec == error_mark_node)
12134 r = error_mark_node;
12135 else
12136 {
12137 r = lookup_template_class (t, argvec, in_decl, context,
12138 entering_scope, complain);
12139 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12140 }
12141
12142 cp_unevaluated_operand = saved_unevaluated_operand;
12143 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12144
12145 return r;
12146 }
12147 else
12148 /* This is not a template type, so there's nothing to do. */
12149 return t;
12150
12151 default:
12152 return tsubst (t, args, complain, in_decl);
12153 }
12154 }
12155
12156 static GTY((cache)) tree_cache_map *defarg_inst;
12157
12158 /* Substitute into the default argument ARG (a default argument for
12159 FN), which has the indicated TYPE. */
12160
12161 tree
12162 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12163 tsubst_flags_t complain)
12164 {
12165 tree saved_class_ptr = NULL_TREE;
12166 tree saved_class_ref = NULL_TREE;
12167 int errs = errorcount + sorrycount;
12168
12169 /* This can happen in invalid code. */
12170 if (TREE_CODE (arg) == DEFAULT_ARG)
12171 return arg;
12172
12173 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12174 parm = chain_index (parmnum, parm);
12175 tree parmtype = TREE_TYPE (parm);
12176 if (DECL_BY_REFERENCE (parm))
12177 parmtype = TREE_TYPE (parmtype);
12178 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12179
12180 tree *slot;
12181 if (defarg_inst && (slot = defarg_inst->get (parm)))
12182 return *slot;
12183
12184 /* This default argument came from a template. Instantiate the
12185 default argument here, not in tsubst. In the case of
12186 something like:
12187
12188 template <class T>
12189 struct S {
12190 static T t();
12191 void f(T = t());
12192 };
12193
12194 we must be careful to do name lookup in the scope of S<T>,
12195 rather than in the current class. */
12196 push_access_scope (fn);
12197 /* The "this" pointer is not valid in a default argument. */
12198 if (cfun)
12199 {
12200 saved_class_ptr = current_class_ptr;
12201 cp_function_chain->x_current_class_ptr = NULL_TREE;
12202 saved_class_ref = current_class_ref;
12203 cp_function_chain->x_current_class_ref = NULL_TREE;
12204 }
12205
12206 start_lambda_scope (parm);
12207
12208 push_deferring_access_checks(dk_no_deferred);
12209 /* The default argument expression may cause implicitly defined
12210 member functions to be synthesized, which will result in garbage
12211 collection. We must treat this situation as if we were within
12212 the body of function so as to avoid collecting live data on the
12213 stack. */
12214 ++function_depth;
12215 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12216 complain, NULL_TREE,
12217 /*integral_constant_expression_p=*/false);
12218 --function_depth;
12219 pop_deferring_access_checks();
12220
12221 finish_lambda_scope ();
12222
12223 /* Restore the "this" pointer. */
12224 if (cfun)
12225 {
12226 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12227 cp_function_chain->x_current_class_ref = saved_class_ref;
12228 }
12229
12230 if (errorcount+sorrycount > errs
12231 && (complain & tf_warning_or_error))
12232 inform (input_location,
12233 " when instantiating default argument for call to %qD", fn);
12234
12235 /* Make sure the default argument is reasonable. */
12236 arg = check_default_argument (type, arg, complain);
12237
12238 pop_access_scope (fn);
12239
12240 if (arg != error_mark_node && !cp_unevaluated_operand)
12241 {
12242 if (!defarg_inst)
12243 defarg_inst = tree_cache_map::create_ggc (37);
12244 defarg_inst->put (parm, arg);
12245 }
12246
12247 return arg;
12248 }
12249
12250 /* Substitute into all the default arguments for FN. */
12251
12252 static void
12253 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12254 {
12255 tree arg;
12256 tree tmpl_args;
12257
12258 tmpl_args = DECL_TI_ARGS (fn);
12259
12260 /* If this function is not yet instantiated, we certainly don't need
12261 its default arguments. */
12262 if (uses_template_parms (tmpl_args))
12263 return;
12264 /* Don't do this again for clones. */
12265 if (DECL_CLONED_FUNCTION_P (fn))
12266 return;
12267
12268 int i = 0;
12269 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12270 arg;
12271 arg = TREE_CHAIN (arg), ++i)
12272 if (TREE_PURPOSE (arg))
12273 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12274 TREE_VALUE (arg),
12275 TREE_PURPOSE (arg),
12276 complain);
12277 }
12278
12279 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12280
12281 static tree
12282 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12283 tree lambda_fntype)
12284 {
12285 tree gen_tmpl, argvec;
12286 hashval_t hash = 0;
12287 tree in_decl = t;
12288
12289 /* Nobody should be tsubst'ing into non-template functions. */
12290 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12291
12292 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12293 {
12294 /* If T is not dependent, just return it. */
12295 if (!uses_template_parms (DECL_TI_ARGS (t)))
12296 return t;
12297
12298 /* Calculate the most general template of which R is a
12299 specialization. */
12300 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12301
12302 /* We're substituting a lambda function under tsubst_lambda_expr but not
12303 directly from it; find the matching function we're already inside.
12304 But don't do this if T is a generic lambda with a single level of
12305 template parms, as in that case we're doing a normal instantiation. */
12306 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12307 && (!generic_lambda_fn_p (t)
12308 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12309 return enclosing_instantiation_of (t);
12310
12311 /* Calculate the complete set of arguments used to
12312 specialize R. */
12313 argvec = tsubst_template_args (DECL_TI_ARGS
12314 (DECL_TEMPLATE_RESULT
12315 (DECL_TI_TEMPLATE (t))),
12316 args, complain, in_decl);
12317 if (argvec == error_mark_node)
12318 return error_mark_node;
12319
12320 /* Check to see if we already have this specialization. */
12321 if (!lambda_fntype)
12322 {
12323 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12324 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12325 return spec;
12326 }
12327
12328 /* We can see more levels of arguments than parameters if
12329 there was a specialization of a member template, like
12330 this:
12331
12332 template <class T> struct S { template <class U> void f(); }
12333 template <> template <class U> void S<int>::f(U);
12334
12335 Here, we'll be substituting into the specialization,
12336 because that's where we can find the code we actually
12337 want to generate, but we'll have enough arguments for
12338 the most general template.
12339
12340 We also deal with the peculiar case:
12341
12342 template <class T> struct S {
12343 template <class U> friend void f();
12344 };
12345 template <class U> void f() {}
12346 template S<int>;
12347 template void f<double>();
12348
12349 Here, the ARGS for the instantiation of will be {int,
12350 double}. But, we only need as many ARGS as there are
12351 levels of template parameters in CODE_PATTERN. We are
12352 careful not to get fooled into reducing the ARGS in
12353 situations like:
12354
12355 template <class T> struct S { template <class U> void f(U); }
12356 template <class T> template <> void S<T>::f(int) {}
12357
12358 which we can spot because the pattern will be a
12359 specialization in this case. */
12360 int args_depth = TMPL_ARGS_DEPTH (args);
12361 int parms_depth =
12362 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12363
12364 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12365 args = get_innermost_template_args (args, parms_depth);
12366 }
12367 else
12368 {
12369 /* This special case arises when we have something like this:
12370
12371 template <class T> struct S {
12372 friend void f<int>(int, double);
12373 };
12374
12375 Here, the DECL_TI_TEMPLATE for the friend declaration
12376 will be an IDENTIFIER_NODE. We are being called from
12377 tsubst_friend_function, and we want only to create a
12378 new decl (R) with appropriate types so that we can call
12379 determine_specialization. */
12380 gen_tmpl = NULL_TREE;
12381 argvec = NULL_TREE;
12382 }
12383
12384 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12385 : NULL_TREE);
12386 tree ctx = closure ? closure : DECL_CONTEXT (t);
12387 bool member = ctx && TYPE_P (ctx);
12388
12389 if (member && !closure)
12390 ctx = tsubst_aggr_type (ctx, args,
12391 complain, t, /*entering_scope=*/1);
12392
12393 tree type = (lambda_fntype ? lambda_fntype
12394 : tsubst (TREE_TYPE (t), args,
12395 complain | tf_fndecl_type, in_decl));
12396 if (type == error_mark_node)
12397 return error_mark_node;
12398
12399 /* If we hit excessive deduction depth, the type is bogus even if
12400 it isn't error_mark_node, so don't build a decl. */
12401 if (excessive_deduction_depth)
12402 return error_mark_node;
12403
12404 /* We do NOT check for matching decls pushed separately at this
12405 point, as they may not represent instantiations of this
12406 template, and in any case are considered separate under the
12407 discrete model. */
12408 tree r = copy_decl (t);
12409 DECL_USE_TEMPLATE (r) = 0;
12410 TREE_TYPE (r) = type;
12411 /* Clear out the mangled name and RTL for the instantiation. */
12412 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12413 SET_DECL_RTL (r, NULL);
12414 /* Leave DECL_INITIAL set on deleted instantiations. */
12415 if (!DECL_DELETED_FN (r))
12416 DECL_INITIAL (r) = NULL_TREE;
12417 DECL_CONTEXT (r) = ctx;
12418
12419 /* OpenMP UDRs have the only argument a reference to the declared
12420 type. We want to diagnose if the declared type is a reference,
12421 which is invalid, but as references to references are usually
12422 quietly merged, diagnose it here. */
12423 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12424 {
12425 tree argtype
12426 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12427 argtype = tsubst (argtype, args, complain, in_decl);
12428 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12429 error_at (DECL_SOURCE_LOCATION (t),
12430 "reference type %qT in "
12431 "%<#pragma omp declare reduction%>", argtype);
12432 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12433 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12434 argtype);
12435 }
12436
12437 if (member && DECL_CONV_FN_P (r))
12438 /* Type-conversion operator. Reconstruct the name, in
12439 case it's the name of one of the template's parameters. */
12440 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12441
12442 tree parms = DECL_ARGUMENTS (t);
12443 if (closure)
12444 parms = DECL_CHAIN (parms);
12445 parms = tsubst (parms, args, complain, t);
12446 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12447 DECL_CONTEXT (parm) = r;
12448 if (closure)
12449 {
12450 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12451 DECL_CHAIN (tparm) = parms;
12452 parms = tparm;
12453 }
12454 DECL_ARGUMENTS (r) = parms;
12455 DECL_RESULT (r) = NULL_TREE;
12456
12457 TREE_STATIC (r) = 0;
12458 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12459 DECL_EXTERNAL (r) = 1;
12460 /* If this is an instantiation of a function with internal
12461 linkage, we already know what object file linkage will be
12462 assigned to the instantiation. */
12463 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12464 DECL_DEFER_OUTPUT (r) = 0;
12465 DECL_CHAIN (r) = NULL_TREE;
12466 DECL_PENDING_INLINE_INFO (r) = 0;
12467 DECL_PENDING_INLINE_P (r) = 0;
12468 DECL_SAVED_TREE (r) = NULL_TREE;
12469 DECL_STRUCT_FUNCTION (r) = NULL;
12470 TREE_USED (r) = 0;
12471 /* We'll re-clone as appropriate in instantiate_template. */
12472 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12473
12474 /* If we aren't complaining now, return on error before we register
12475 the specialization so that we'll complain eventually. */
12476 if ((complain & tf_error) == 0
12477 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12478 && !grok_op_properties (r, /*complain=*/false))
12479 return error_mark_node;
12480
12481 /* When instantiating a constrained member, substitute
12482 into the constraints to create a new constraint. */
12483 if (tree ci = get_constraints (t))
12484 if (member)
12485 {
12486 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12487 set_constraints (r, ci);
12488 }
12489
12490 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12491 this in the special friend case mentioned above where
12492 GEN_TMPL is NULL. */
12493 if (gen_tmpl && !closure)
12494 {
12495 DECL_TEMPLATE_INFO (r)
12496 = build_template_info (gen_tmpl, argvec);
12497 SET_DECL_IMPLICIT_INSTANTIATION (r);
12498
12499 tree new_r
12500 = register_specialization (r, gen_tmpl, argvec, false, hash);
12501 if (new_r != r)
12502 /* We instantiated this while substituting into
12503 the type earlier (template/friend54.C). */
12504 return new_r;
12505
12506 /* We're not supposed to instantiate default arguments
12507 until they are called, for a template. But, for a
12508 declaration like:
12509
12510 template <class T> void f ()
12511 { extern void g(int i = T()); }
12512
12513 we should do the substitution when the template is
12514 instantiated. We handle the member function case in
12515 instantiate_class_template since the default arguments
12516 might refer to other members of the class. */
12517 if (!member
12518 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12519 && !uses_template_parms (argvec))
12520 tsubst_default_arguments (r, complain);
12521 }
12522 else
12523 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12524
12525 /* Copy the list of befriending classes. */
12526 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12527 *friends;
12528 friends = &TREE_CHAIN (*friends))
12529 {
12530 *friends = copy_node (*friends);
12531 TREE_VALUE (*friends)
12532 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12533 }
12534
12535 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12536 {
12537 maybe_retrofit_in_chrg (r);
12538 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12539 return error_mark_node;
12540 /* If this is an instantiation of a member template, clone it.
12541 If it isn't, that'll be handled by
12542 clone_constructors_and_destructors. */
12543 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12544 clone_function_decl (r, /*update_methods=*/false);
12545 }
12546 else if ((complain & tf_error) != 0
12547 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12548 && !grok_op_properties (r, /*complain=*/true))
12549 return error_mark_node;
12550
12551 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12552 SET_DECL_FRIEND_CONTEXT (r,
12553 tsubst (DECL_FRIEND_CONTEXT (t),
12554 args, complain, in_decl));
12555
12556 /* Possibly limit visibility based on template args. */
12557 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12558 if (DECL_VISIBILITY_SPECIFIED (t))
12559 {
12560 DECL_VISIBILITY_SPECIFIED (r) = 0;
12561 DECL_ATTRIBUTES (r)
12562 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12563 }
12564 determine_visibility (r);
12565 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12566 && !processing_template_decl)
12567 defaulted_late_check (r);
12568
12569 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12570 args, complain, in_decl);
12571 return r;
12572 }
12573
12574 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12575
12576 static tree
12577 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12578 tree lambda_fntype)
12579 {
12580 /* We can get here when processing a member function template,
12581 member class template, or template template parameter. */
12582 tree decl = DECL_TEMPLATE_RESULT (t);
12583 tree in_decl = t;
12584 tree spec;
12585 tree tmpl_args;
12586 tree full_args;
12587 tree r;
12588 hashval_t hash = 0;
12589
12590 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12591 {
12592 /* Template template parameter is treated here. */
12593 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12594 if (new_type == error_mark_node)
12595 r = error_mark_node;
12596 /* If we get a real template back, return it. This can happen in
12597 the context of most_specialized_partial_spec. */
12598 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12599 r = new_type;
12600 else
12601 /* The new TEMPLATE_DECL was built in
12602 reduce_template_parm_level. */
12603 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12604 return r;
12605 }
12606
12607 if (!lambda_fntype)
12608 {
12609 /* We might already have an instance of this template.
12610 The ARGS are for the surrounding class type, so the
12611 full args contain the tsubst'd args for the context,
12612 plus the innermost args from the template decl. */
12613 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12614 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12615 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12616 /* Because this is a template, the arguments will still be
12617 dependent, even after substitution. If
12618 PROCESSING_TEMPLATE_DECL is not set, the dependency
12619 predicates will short-circuit. */
12620 ++processing_template_decl;
12621 full_args = tsubst_template_args (tmpl_args, args,
12622 complain, in_decl);
12623 --processing_template_decl;
12624 if (full_args == error_mark_node)
12625 return error_mark_node;
12626
12627 /* If this is a default template template argument,
12628 tsubst might not have changed anything. */
12629 if (full_args == tmpl_args)
12630 return t;
12631
12632 hash = hash_tmpl_and_args (t, full_args);
12633 spec = retrieve_specialization (t, full_args, hash);
12634 if (spec != NULL_TREE)
12635 return spec;
12636 }
12637
12638 /* Make a new template decl. It will be similar to the
12639 original, but will record the current template arguments.
12640 We also create a new function declaration, which is just
12641 like the old one, but points to this new template, rather
12642 than the old one. */
12643 r = copy_decl (t);
12644 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12645 DECL_CHAIN (r) = NULL_TREE;
12646
12647 // Build new template info linking to the original template decl.
12648 if (!lambda_fntype)
12649 {
12650 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12651 SET_DECL_IMPLICIT_INSTANTIATION (r);
12652 }
12653 else
12654 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12655
12656 /* The template parameters for this new template are all the
12657 template parameters for the old template, except the
12658 outermost level of parameters. */
12659 DECL_TEMPLATE_PARMS (r)
12660 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12661 complain);
12662
12663 if (TREE_CODE (decl) == TYPE_DECL
12664 && !TYPE_DECL_ALIAS_P (decl))
12665 {
12666 tree new_type;
12667 ++processing_template_decl;
12668 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12669 --processing_template_decl;
12670 if (new_type == error_mark_node)
12671 return error_mark_node;
12672
12673 TREE_TYPE (r) = new_type;
12674 /* For a partial specialization, we need to keep pointing to
12675 the primary template. */
12676 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12677 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12678 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12679 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12680 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12681 }
12682 else
12683 {
12684 tree new_decl;
12685 ++processing_template_decl;
12686 if (TREE_CODE (decl) == FUNCTION_DECL)
12687 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12688 else
12689 new_decl = tsubst (decl, args, complain, in_decl);
12690 --processing_template_decl;
12691 if (new_decl == error_mark_node)
12692 return error_mark_node;
12693
12694 DECL_TEMPLATE_RESULT (r) = new_decl;
12695 TREE_TYPE (r) = TREE_TYPE (new_decl);
12696 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12697 if (lambda_fntype)
12698 {
12699 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12700 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12701 }
12702 else
12703 {
12704 DECL_TI_TEMPLATE (new_decl) = r;
12705 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12706 }
12707 }
12708
12709 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12710 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12711
12712 if (PRIMARY_TEMPLATE_P (t))
12713 DECL_PRIMARY_TEMPLATE (r) = r;
12714
12715 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12716 && !lambda_fntype)
12717 /* Record this non-type partial instantiation. */
12718 register_specialization (r, t,
12719 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12720 false, hash);
12721
12722 return r;
12723 }
12724
12725 /* True if FN is the op() for a lambda in an uninstantiated template. */
12726
12727 bool
12728 lambda_fn_in_template_p (tree fn)
12729 {
12730 if (!fn || !LAMBDA_FUNCTION_P (fn))
12731 return false;
12732 tree closure = DECL_CONTEXT (fn);
12733 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12734 }
12735
12736 /* We're instantiating a variable from template function TCTX. Return the
12737 corresponding current enclosing scope. This gets complicated because lambda
12738 functions in templates are regenerated rather than instantiated, but generic
12739 lambda functions are subsequently instantiated. */
12740
12741 static tree
12742 enclosing_instantiation_of (tree otctx)
12743 {
12744 tree tctx = otctx;
12745 tree fn = current_function_decl;
12746 int lambda_count = 0;
12747
12748 for (; tctx && lambda_fn_in_template_p (tctx);
12749 tctx = decl_function_context (tctx))
12750 ++lambda_count;
12751 for (; fn; fn = decl_function_context (fn))
12752 {
12753 tree ofn = fn;
12754 int flambda_count = 0;
12755 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12756 fn = decl_function_context (fn))
12757 ++flambda_count;
12758 if (DECL_TEMPLATE_INFO (fn)
12759 ? most_general_template (fn) != most_general_template (tctx)
12760 : fn != tctx)
12761 continue;
12762 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12763 || DECL_CONV_FN_P (ofn));
12764 return ofn;
12765 }
12766 gcc_unreachable ();
12767 }
12768
12769 /* Substitute the ARGS into the T, which is a _DECL. Return the
12770 result of the substitution. Issue error and warning messages under
12771 control of COMPLAIN. */
12772
12773 static tree
12774 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12775 {
12776 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12777 location_t saved_loc;
12778 tree r = NULL_TREE;
12779 tree in_decl = t;
12780 hashval_t hash = 0;
12781
12782 /* Set the filename and linenumber to improve error-reporting. */
12783 saved_loc = input_location;
12784 input_location = DECL_SOURCE_LOCATION (t);
12785
12786 switch (TREE_CODE (t))
12787 {
12788 case TEMPLATE_DECL:
12789 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12790 break;
12791
12792 case FUNCTION_DECL:
12793 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12794 break;
12795
12796 case PARM_DECL:
12797 {
12798 tree type = NULL_TREE;
12799 int i, len = 1;
12800 tree expanded_types = NULL_TREE;
12801 tree prev_r = NULL_TREE;
12802 tree first_r = NULL_TREE;
12803
12804 if (DECL_PACK_P (t))
12805 {
12806 /* If there is a local specialization that isn't a
12807 parameter pack, it means that we're doing a "simple"
12808 substitution from inside tsubst_pack_expansion. Just
12809 return the local specialization (which will be a single
12810 parm). */
12811 tree spec = retrieve_local_specialization (t);
12812 if (spec
12813 && TREE_CODE (spec) == PARM_DECL
12814 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12815 RETURN (spec);
12816
12817 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12818 the parameters in this function parameter pack. */
12819 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12820 complain, in_decl);
12821 if (TREE_CODE (expanded_types) == TREE_VEC)
12822 {
12823 len = TREE_VEC_LENGTH (expanded_types);
12824
12825 /* Zero-length parameter packs are boring. Just substitute
12826 into the chain. */
12827 if (len == 0)
12828 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12829 TREE_CHAIN (t)));
12830 }
12831 else
12832 {
12833 /* All we did was update the type. Make a note of that. */
12834 type = expanded_types;
12835 expanded_types = NULL_TREE;
12836 }
12837 }
12838
12839 /* Loop through all of the parameters we'll build. When T is
12840 a function parameter pack, LEN is the number of expanded
12841 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12842 r = NULL_TREE;
12843 for (i = 0; i < len; ++i)
12844 {
12845 prev_r = r;
12846 r = copy_node (t);
12847 if (DECL_TEMPLATE_PARM_P (t))
12848 SET_DECL_TEMPLATE_PARM_P (r);
12849
12850 if (expanded_types)
12851 /* We're on the Ith parameter of the function parameter
12852 pack. */
12853 {
12854 /* Get the Ith type. */
12855 type = TREE_VEC_ELT (expanded_types, i);
12856
12857 /* Rename the parameter to include the index. */
12858 DECL_NAME (r)
12859 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12860 }
12861 else if (!type)
12862 /* We're dealing with a normal parameter. */
12863 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12864
12865 type = type_decays_to (type);
12866 TREE_TYPE (r) = type;
12867 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12868
12869 if (DECL_INITIAL (r))
12870 {
12871 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12872 DECL_INITIAL (r) = TREE_TYPE (r);
12873 else
12874 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12875 complain, in_decl);
12876 }
12877
12878 DECL_CONTEXT (r) = NULL_TREE;
12879
12880 if (!DECL_TEMPLATE_PARM_P (r))
12881 DECL_ARG_TYPE (r) = type_passed_as (type);
12882
12883 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12884 args, complain, in_decl);
12885
12886 /* Keep track of the first new parameter we
12887 generate. That's what will be returned to the
12888 caller. */
12889 if (!first_r)
12890 first_r = r;
12891
12892 /* Build a proper chain of parameters when substituting
12893 into a function parameter pack. */
12894 if (prev_r)
12895 DECL_CHAIN (prev_r) = r;
12896 }
12897
12898 /* If cp_unevaluated_operand is set, we're just looking for a
12899 single dummy parameter, so don't keep going. */
12900 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12901 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12902 complain, DECL_CHAIN (t));
12903
12904 /* FIRST_R contains the start of the chain we've built. */
12905 r = first_r;
12906 }
12907 break;
12908
12909 case FIELD_DECL:
12910 {
12911 tree type = NULL_TREE;
12912 tree vec = NULL_TREE;
12913 tree expanded_types = NULL_TREE;
12914 int len = 1;
12915
12916 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12917 {
12918 /* This field is a lambda capture pack. Return a TREE_VEC of
12919 the expanded fields to instantiate_class_template_1. */
12920 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12921 complain, in_decl);
12922 if (TREE_CODE (expanded_types) == TREE_VEC)
12923 {
12924 len = TREE_VEC_LENGTH (expanded_types);
12925 vec = make_tree_vec (len);
12926 }
12927 else
12928 {
12929 /* All we did was update the type. Make a note of that. */
12930 type = expanded_types;
12931 expanded_types = NULL_TREE;
12932 }
12933 }
12934
12935 for (int i = 0; i < len; ++i)
12936 {
12937 r = copy_decl (t);
12938 if (expanded_types)
12939 {
12940 type = TREE_VEC_ELT (expanded_types, i);
12941 DECL_NAME (r)
12942 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12943 }
12944 else if (!type)
12945 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12946
12947 if (type == error_mark_node)
12948 RETURN (error_mark_node);
12949 TREE_TYPE (r) = type;
12950 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12951
12952 if (DECL_C_BIT_FIELD (r))
12953 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12954 number of bits. */
12955 DECL_BIT_FIELD_REPRESENTATIVE (r)
12956 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12957 complain, in_decl,
12958 /*integral_constant_expression_p=*/true);
12959 if (DECL_INITIAL (t))
12960 {
12961 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12962 NSDMI in perform_member_init. Still set DECL_INITIAL
12963 so that we know there is one. */
12964 DECL_INITIAL (r) = void_node;
12965 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12966 retrofit_lang_decl (r);
12967 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12968 }
12969 /* We don't have to set DECL_CONTEXT here; it is set by
12970 finish_member_declaration. */
12971 DECL_CHAIN (r) = NULL_TREE;
12972
12973 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12974 args, complain, in_decl);
12975
12976 if (vec)
12977 TREE_VEC_ELT (vec, i) = r;
12978 }
12979
12980 if (vec)
12981 r = vec;
12982 }
12983 break;
12984
12985 case USING_DECL:
12986 /* We reach here only for member using decls. We also need to check
12987 uses_template_parms because DECL_DEPENDENT_P is not set for a
12988 using-declaration that designates a member of the current
12989 instantiation (c++/53549). */
12990 if (DECL_DEPENDENT_P (t)
12991 || uses_template_parms (USING_DECL_SCOPE (t)))
12992 {
12993 tree scope = USING_DECL_SCOPE (t);
12994 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12995 if (PACK_EXPANSION_P (scope))
12996 {
12997 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12998 int len = TREE_VEC_LENGTH (vec);
12999 r = make_tree_vec (len);
13000 for (int i = 0; i < len; ++i)
13001 {
13002 tree escope = TREE_VEC_ELT (vec, i);
13003 tree elt = do_class_using_decl (escope, name);
13004 if (!elt)
13005 {
13006 r = error_mark_node;
13007 break;
13008 }
13009 else
13010 {
13011 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13012 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13013 }
13014 TREE_VEC_ELT (r, i) = elt;
13015 }
13016 }
13017 else
13018 {
13019 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13020 complain, in_decl);
13021 r = do_class_using_decl (inst_scope, name);
13022 if (!r)
13023 r = error_mark_node;
13024 else
13025 {
13026 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13027 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13028 }
13029 }
13030 }
13031 else
13032 {
13033 r = copy_node (t);
13034 DECL_CHAIN (r) = NULL_TREE;
13035 }
13036 break;
13037
13038 case TYPE_DECL:
13039 case VAR_DECL:
13040 {
13041 tree argvec = NULL_TREE;
13042 tree gen_tmpl = NULL_TREE;
13043 tree spec;
13044 tree tmpl = NULL_TREE;
13045 tree ctx;
13046 tree type = NULL_TREE;
13047 bool local_p;
13048
13049 if (TREE_TYPE (t) == error_mark_node)
13050 RETURN (error_mark_node);
13051
13052 if (TREE_CODE (t) == TYPE_DECL
13053 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13054 {
13055 /* If this is the canonical decl, we don't have to
13056 mess with instantiations, and often we can't (for
13057 typename, template type parms and such). Note that
13058 TYPE_NAME is not correct for the above test if
13059 we've copied the type for a typedef. */
13060 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13061 if (type == error_mark_node)
13062 RETURN (error_mark_node);
13063 r = TYPE_NAME (type);
13064 break;
13065 }
13066
13067 /* Check to see if we already have the specialization we
13068 need. */
13069 spec = NULL_TREE;
13070 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13071 {
13072 /* T is a static data member or namespace-scope entity.
13073 We have to substitute into namespace-scope variables
13074 (not just variable templates) because of cases like:
13075
13076 template <class T> void f() { extern T t; }
13077
13078 where the entity referenced is not known until
13079 instantiation time. */
13080 local_p = false;
13081 ctx = DECL_CONTEXT (t);
13082 if (DECL_CLASS_SCOPE_P (t))
13083 {
13084 ctx = tsubst_aggr_type (ctx, args,
13085 complain,
13086 in_decl, /*entering_scope=*/1);
13087 /* If CTX is unchanged, then T is in fact the
13088 specialization we want. That situation occurs when
13089 referencing a static data member within in its own
13090 class. We can use pointer equality, rather than
13091 same_type_p, because DECL_CONTEXT is always
13092 canonical... */
13093 if (ctx == DECL_CONTEXT (t)
13094 /* ... unless T is a member template; in which
13095 case our caller can be willing to create a
13096 specialization of that template represented
13097 by T. */
13098 && !(DECL_TI_TEMPLATE (t)
13099 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13100 spec = t;
13101 }
13102
13103 if (!spec)
13104 {
13105 tmpl = DECL_TI_TEMPLATE (t);
13106 gen_tmpl = most_general_template (tmpl);
13107 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13108 if (argvec != error_mark_node)
13109 argvec = (coerce_innermost_template_parms
13110 (DECL_TEMPLATE_PARMS (gen_tmpl),
13111 argvec, t, complain,
13112 /*all*/true, /*defarg*/true));
13113 if (argvec == error_mark_node)
13114 RETURN (error_mark_node);
13115 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13116 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13117 }
13118 }
13119 else
13120 {
13121 /* A local variable. */
13122 local_p = true;
13123 /* Subsequent calls to pushdecl will fill this in. */
13124 ctx = NULL_TREE;
13125 /* Unless this is a reference to a static variable from an
13126 enclosing function, in which case we need to fill it in now. */
13127 if (TREE_STATIC (t))
13128 {
13129 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13130 if (fn != current_function_decl)
13131 ctx = fn;
13132 }
13133 spec = retrieve_local_specialization (t);
13134 }
13135 /* If we already have the specialization we need, there is
13136 nothing more to do. */
13137 if (spec)
13138 {
13139 r = spec;
13140 break;
13141 }
13142
13143 /* Create a new node for the specialization we need. */
13144 r = copy_decl (t);
13145 if (type == NULL_TREE)
13146 {
13147 if (is_typedef_decl (t))
13148 type = DECL_ORIGINAL_TYPE (t);
13149 else
13150 type = TREE_TYPE (t);
13151 if (VAR_P (t)
13152 && VAR_HAD_UNKNOWN_BOUND (t)
13153 && type != error_mark_node)
13154 type = strip_array_domain (type);
13155 tree sub_args = args;
13156 if (tree auto_node = type_uses_auto (type))
13157 {
13158 /* Mask off any template args past the variable's context so we
13159 don't replace the auto with an unrelated argument. */
13160 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13161 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13162 if (extra > 0)
13163 /* This should never happen with the new lambda instantiation
13164 model, but keep the handling just in case. */
13165 gcc_assert (!CHECKING_P),
13166 sub_args = strip_innermost_template_args (args, extra);
13167 }
13168 type = tsubst (type, sub_args, complain, in_decl);
13169 }
13170 if (VAR_P (r))
13171 {
13172 /* Even if the original location is out of scope, the
13173 newly substituted one is not. */
13174 DECL_DEAD_FOR_LOCAL (r) = 0;
13175 DECL_INITIALIZED_P (r) = 0;
13176 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13177 if (type == error_mark_node)
13178 RETURN (error_mark_node);
13179 if (TREE_CODE (type) == FUNCTION_TYPE)
13180 {
13181 /* It may seem that this case cannot occur, since:
13182
13183 typedef void f();
13184 void g() { f x; }
13185
13186 declares a function, not a variable. However:
13187
13188 typedef void f();
13189 template <typename T> void g() { T t; }
13190 template void g<f>();
13191
13192 is an attempt to declare a variable with function
13193 type. */
13194 error ("variable %qD has function type",
13195 /* R is not yet sufficiently initialized, so we
13196 just use its name. */
13197 DECL_NAME (r));
13198 RETURN (error_mark_node);
13199 }
13200 type = complete_type (type);
13201 /* Wait until cp_finish_decl to set this again, to handle
13202 circular dependency (template/instantiate6.C). */
13203 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13204 type = check_var_type (DECL_NAME (r), type);
13205
13206 if (DECL_HAS_VALUE_EXPR_P (t))
13207 {
13208 tree ve = DECL_VALUE_EXPR (t);
13209 ve = tsubst_expr (ve, args, complain, in_decl,
13210 /*constant_expression_p=*/false);
13211 if (REFERENCE_REF_P (ve))
13212 {
13213 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13214 ve = TREE_OPERAND (ve, 0);
13215 }
13216 SET_DECL_VALUE_EXPR (r, ve);
13217 }
13218 if (CP_DECL_THREAD_LOCAL_P (r)
13219 && !processing_template_decl)
13220 set_decl_tls_model (r, decl_default_tls_model (r));
13221 }
13222 else if (DECL_SELF_REFERENCE_P (t))
13223 SET_DECL_SELF_REFERENCE_P (r);
13224 TREE_TYPE (r) = type;
13225 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13226 DECL_CONTEXT (r) = ctx;
13227 /* Clear out the mangled name and RTL for the instantiation. */
13228 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13229 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13230 SET_DECL_RTL (r, NULL);
13231 /* The initializer must not be expanded until it is required;
13232 see [temp.inst]. */
13233 DECL_INITIAL (r) = NULL_TREE;
13234 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13235 if (VAR_P (r))
13236 {
13237 if (DECL_LANG_SPECIFIC (r))
13238 SET_DECL_DEPENDENT_INIT_P (r, false);
13239
13240 SET_DECL_MODE (r, VOIDmode);
13241
13242 /* Possibly limit visibility based on template args. */
13243 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13244 if (DECL_VISIBILITY_SPECIFIED (t))
13245 {
13246 DECL_VISIBILITY_SPECIFIED (r) = 0;
13247 DECL_ATTRIBUTES (r)
13248 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13249 }
13250 determine_visibility (r);
13251 }
13252
13253 if (!local_p)
13254 {
13255 /* A static data member declaration is always marked
13256 external when it is declared in-class, even if an
13257 initializer is present. We mimic the non-template
13258 processing here. */
13259 DECL_EXTERNAL (r) = 1;
13260 if (DECL_NAMESPACE_SCOPE_P (t))
13261 DECL_NOT_REALLY_EXTERN (r) = 1;
13262
13263 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13264 SET_DECL_IMPLICIT_INSTANTIATION (r);
13265 register_specialization (r, gen_tmpl, argvec, false, hash);
13266 }
13267 else
13268 {
13269 if (DECL_LANG_SPECIFIC (r))
13270 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13271 if (!cp_unevaluated_operand)
13272 register_local_specialization (r, t);
13273 }
13274
13275 DECL_CHAIN (r) = NULL_TREE;
13276
13277 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13278 /*flags=*/0,
13279 args, complain, in_decl);
13280
13281 /* Preserve a typedef that names a type. */
13282 if (is_typedef_decl (r) && type != error_mark_node)
13283 {
13284 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13285 set_underlying_type (r);
13286 if (TYPE_DECL_ALIAS_P (r))
13287 /* An alias template specialization can be dependent
13288 even if its underlying type is not. */
13289 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13290 }
13291
13292 layout_decl (r, 0);
13293 }
13294 break;
13295
13296 default:
13297 gcc_unreachable ();
13298 }
13299 #undef RETURN
13300
13301 out:
13302 /* Restore the file and line information. */
13303 input_location = saved_loc;
13304
13305 return r;
13306 }
13307
13308 /* Substitute into the ARG_TYPES of a function type.
13309 If END is a TREE_CHAIN, leave it and any following types
13310 un-substituted. */
13311
13312 static tree
13313 tsubst_arg_types (tree arg_types,
13314 tree args,
13315 tree end,
13316 tsubst_flags_t complain,
13317 tree in_decl)
13318 {
13319 tree remaining_arg_types;
13320 tree type = NULL_TREE;
13321 int i = 1;
13322 tree expanded_args = NULL_TREE;
13323 tree default_arg;
13324
13325 if (!arg_types || arg_types == void_list_node || arg_types == end)
13326 return arg_types;
13327
13328 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13329 args, end, complain, in_decl);
13330 if (remaining_arg_types == error_mark_node)
13331 return error_mark_node;
13332
13333 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13334 {
13335 /* For a pack expansion, perform substitution on the
13336 entire expression. Later on, we'll handle the arguments
13337 one-by-one. */
13338 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13339 args, complain, in_decl);
13340
13341 if (TREE_CODE (expanded_args) == TREE_VEC)
13342 /* So that we'll spin through the parameters, one by one. */
13343 i = TREE_VEC_LENGTH (expanded_args);
13344 else
13345 {
13346 /* We only partially substituted into the parameter
13347 pack. Our type is TYPE_PACK_EXPANSION. */
13348 type = expanded_args;
13349 expanded_args = NULL_TREE;
13350 }
13351 }
13352
13353 while (i > 0) {
13354 --i;
13355
13356 if (expanded_args)
13357 type = TREE_VEC_ELT (expanded_args, i);
13358 else if (!type)
13359 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13360
13361 if (type == error_mark_node)
13362 return error_mark_node;
13363 if (VOID_TYPE_P (type))
13364 {
13365 if (complain & tf_error)
13366 {
13367 error ("invalid parameter type %qT", type);
13368 if (in_decl)
13369 error ("in declaration %q+D", in_decl);
13370 }
13371 return error_mark_node;
13372 }
13373 /* DR 657. */
13374 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13375 return error_mark_node;
13376
13377 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13378 top-level qualifiers as required. */
13379 type = cv_unqualified (type_decays_to (type));
13380
13381 /* We do not substitute into default arguments here. The standard
13382 mandates that they be instantiated only when needed, which is
13383 done in build_over_call. */
13384 default_arg = TREE_PURPOSE (arg_types);
13385
13386 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13387 since the new op() won't have any associated template arguments for us
13388 to refer to later. */
13389 if (lambda_fn_in_template_p (in_decl))
13390 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13391 false/*fn*/, false/*constexpr*/);
13392
13393 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13394 {
13395 /* We've instantiated a template before its default arguments
13396 have been parsed. This can happen for a nested template
13397 class, and is not an error unless we require the default
13398 argument in a call of this function. */
13399 remaining_arg_types =
13400 tree_cons (default_arg, type, remaining_arg_types);
13401 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13402 }
13403 else
13404 remaining_arg_types =
13405 hash_tree_cons (default_arg, type, remaining_arg_types);
13406 }
13407
13408 return remaining_arg_types;
13409 }
13410
13411 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13412 *not* handle the exception-specification for FNTYPE, because the
13413 initial substitution of explicitly provided template parameters
13414 during argument deduction forbids substitution into the
13415 exception-specification:
13416
13417 [temp.deduct]
13418
13419 All references in the function type of the function template to the
13420 corresponding template parameters are replaced by the specified tem-
13421 plate argument values. If a substitution in a template parameter or
13422 in the function type of the function template results in an invalid
13423 type, type deduction fails. [Note: The equivalent substitution in
13424 exception specifications is done only when the function is instanti-
13425 ated, at which point a program is ill-formed if the substitution
13426 results in an invalid type.] */
13427
13428 static tree
13429 tsubst_function_type (tree t,
13430 tree args,
13431 tsubst_flags_t complain,
13432 tree in_decl)
13433 {
13434 tree return_type;
13435 tree arg_types = NULL_TREE;
13436 tree fntype;
13437
13438 /* The TYPE_CONTEXT is not used for function/method types. */
13439 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13440
13441 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13442 failure. */
13443 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13444
13445 if (late_return_type_p)
13446 {
13447 /* Substitute the argument types. */
13448 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13449 complain, in_decl);
13450 if (arg_types == error_mark_node)
13451 return error_mark_node;
13452
13453 tree save_ccp = current_class_ptr;
13454 tree save_ccr = current_class_ref;
13455 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13456 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13457 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13458 if (do_inject)
13459 {
13460 /* DR 1207: 'this' is in scope in the trailing return type. */
13461 inject_this_parameter (this_type, cp_type_quals (this_type));
13462 }
13463
13464 /* Substitute the return type. */
13465 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13466
13467 if (do_inject)
13468 {
13469 current_class_ptr = save_ccp;
13470 current_class_ref = save_ccr;
13471 }
13472 }
13473 else
13474 /* Substitute the return type. */
13475 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13476
13477 if (return_type == error_mark_node)
13478 return error_mark_node;
13479 /* DR 486 clarifies that creation of a function type with an
13480 invalid return type is a deduction failure. */
13481 if (TREE_CODE (return_type) == ARRAY_TYPE
13482 || TREE_CODE (return_type) == FUNCTION_TYPE)
13483 {
13484 if (complain & tf_error)
13485 {
13486 if (TREE_CODE (return_type) == ARRAY_TYPE)
13487 error ("function returning an array");
13488 else
13489 error ("function returning a function");
13490 }
13491 return error_mark_node;
13492 }
13493 /* And DR 657. */
13494 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13495 return error_mark_node;
13496
13497 if (!late_return_type_p)
13498 {
13499 /* Substitute the argument types. */
13500 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13501 complain, in_decl);
13502 if (arg_types == error_mark_node)
13503 return error_mark_node;
13504 }
13505
13506 /* Construct a new type node and return it. */
13507 if (TREE_CODE (t) == FUNCTION_TYPE)
13508 {
13509 fntype = build_function_type (return_type, arg_types);
13510 fntype = apply_memfn_quals (fntype,
13511 type_memfn_quals (t),
13512 type_memfn_rqual (t));
13513 }
13514 else
13515 {
13516 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13517 /* Don't pick up extra function qualifiers from the basetype. */
13518 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13519 if (! MAYBE_CLASS_TYPE_P (r))
13520 {
13521 /* [temp.deduct]
13522
13523 Type deduction may fail for any of the following
13524 reasons:
13525
13526 -- Attempting to create "pointer to member of T" when T
13527 is not a class type. */
13528 if (complain & tf_error)
13529 error ("creating pointer to member function of non-class type %qT",
13530 r);
13531 return error_mark_node;
13532 }
13533
13534 fntype = build_method_type_directly (r, return_type,
13535 TREE_CHAIN (arg_types));
13536 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13537 }
13538 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13539
13540 if (late_return_type_p)
13541 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13542
13543 return fntype;
13544 }
13545
13546 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13547 ARGS into that specification, and return the substituted
13548 specification. If there is no specification, return NULL_TREE. */
13549
13550 static tree
13551 tsubst_exception_specification (tree fntype,
13552 tree args,
13553 tsubst_flags_t complain,
13554 tree in_decl,
13555 bool defer_ok)
13556 {
13557 tree specs;
13558 tree new_specs;
13559
13560 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13561 new_specs = NULL_TREE;
13562 if (specs && TREE_PURPOSE (specs))
13563 {
13564 /* A noexcept-specifier. */
13565 tree expr = TREE_PURPOSE (specs);
13566 if (TREE_CODE (expr) == INTEGER_CST)
13567 new_specs = expr;
13568 else if (defer_ok)
13569 {
13570 /* Defer instantiation of noexcept-specifiers to avoid
13571 excessive instantiations (c++/49107). */
13572 new_specs = make_node (DEFERRED_NOEXCEPT);
13573 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13574 {
13575 /* We already partially instantiated this member template,
13576 so combine the new args with the old. */
13577 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13578 = DEFERRED_NOEXCEPT_PATTERN (expr);
13579 DEFERRED_NOEXCEPT_ARGS (new_specs)
13580 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13581 }
13582 else
13583 {
13584 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13585 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13586 }
13587 }
13588 else
13589 new_specs = tsubst_copy_and_build
13590 (expr, args, complain, in_decl, /*function_p=*/false,
13591 /*integral_constant_expression_p=*/true);
13592 new_specs = build_noexcept_spec (new_specs, complain);
13593 }
13594 else if (specs)
13595 {
13596 if (! TREE_VALUE (specs))
13597 new_specs = specs;
13598 else
13599 while (specs)
13600 {
13601 tree spec;
13602 int i, len = 1;
13603 tree expanded_specs = NULL_TREE;
13604
13605 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13606 {
13607 /* Expand the pack expansion type. */
13608 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13609 args, complain,
13610 in_decl);
13611
13612 if (expanded_specs == error_mark_node)
13613 return error_mark_node;
13614 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13615 len = TREE_VEC_LENGTH (expanded_specs);
13616 else
13617 {
13618 /* We're substituting into a member template, so
13619 we got a TYPE_PACK_EXPANSION back. Add that
13620 expansion and move on. */
13621 gcc_assert (TREE_CODE (expanded_specs)
13622 == TYPE_PACK_EXPANSION);
13623 new_specs = add_exception_specifier (new_specs,
13624 expanded_specs,
13625 complain);
13626 specs = TREE_CHAIN (specs);
13627 continue;
13628 }
13629 }
13630
13631 for (i = 0; i < len; ++i)
13632 {
13633 if (expanded_specs)
13634 spec = TREE_VEC_ELT (expanded_specs, i);
13635 else
13636 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13637 if (spec == error_mark_node)
13638 return spec;
13639 new_specs = add_exception_specifier (new_specs, spec,
13640 complain);
13641 }
13642
13643 specs = TREE_CHAIN (specs);
13644 }
13645 }
13646 return new_specs;
13647 }
13648
13649 /* Take the tree structure T and replace template parameters used
13650 therein with the argument vector ARGS. IN_DECL is an associated
13651 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13652 Issue error and warning messages under control of COMPLAIN. Note
13653 that we must be relatively non-tolerant of extensions here, in
13654 order to preserve conformance; if we allow substitutions that
13655 should not be allowed, we may allow argument deductions that should
13656 not succeed, and therefore report ambiguous overload situations
13657 where there are none. In theory, we could allow the substitution,
13658 but indicate that it should have failed, and allow our caller to
13659 make sure that the right thing happens, but we don't try to do this
13660 yet.
13661
13662 This function is used for dealing with types, decls and the like;
13663 for expressions, use tsubst_expr or tsubst_copy. */
13664
13665 tree
13666 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13667 {
13668 enum tree_code code;
13669 tree type, r = NULL_TREE;
13670
13671 if (t == NULL_TREE || t == error_mark_node
13672 || t == integer_type_node
13673 || t == void_type_node
13674 || t == char_type_node
13675 || t == unknown_type_node
13676 || TREE_CODE (t) == NAMESPACE_DECL
13677 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13678 return t;
13679
13680 if (DECL_P (t))
13681 return tsubst_decl (t, args, complain);
13682
13683 if (args == NULL_TREE)
13684 return t;
13685
13686 code = TREE_CODE (t);
13687
13688 if (code == IDENTIFIER_NODE)
13689 type = IDENTIFIER_TYPE_VALUE (t);
13690 else
13691 type = TREE_TYPE (t);
13692
13693 gcc_assert (type != unknown_type_node);
13694
13695 /* Reuse typedefs. We need to do this to handle dependent attributes,
13696 such as attribute aligned. */
13697 if (TYPE_P (t)
13698 && typedef_variant_p (t))
13699 {
13700 tree decl = TYPE_NAME (t);
13701
13702 if (alias_template_specialization_p (t))
13703 {
13704 /* DECL represents an alias template and we want to
13705 instantiate it. */
13706 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13707 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13708 r = instantiate_alias_template (tmpl, gen_args, complain);
13709 }
13710 else if (DECL_CLASS_SCOPE_P (decl)
13711 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13712 && uses_template_parms (DECL_CONTEXT (decl)))
13713 {
13714 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13715 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13716 r = retrieve_specialization (tmpl, gen_args, 0);
13717 }
13718 else if (DECL_FUNCTION_SCOPE_P (decl)
13719 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13720 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13721 r = retrieve_local_specialization (decl);
13722 else
13723 /* The typedef is from a non-template context. */
13724 return t;
13725
13726 if (r)
13727 {
13728 r = TREE_TYPE (r);
13729 r = cp_build_qualified_type_real
13730 (r, cp_type_quals (t) | cp_type_quals (r),
13731 complain | tf_ignore_bad_quals);
13732 return r;
13733 }
13734 else
13735 {
13736 /* We don't have an instantiation yet, so drop the typedef. */
13737 int quals = cp_type_quals (t);
13738 t = DECL_ORIGINAL_TYPE (decl);
13739 t = cp_build_qualified_type_real (t, quals,
13740 complain | tf_ignore_bad_quals);
13741 }
13742 }
13743
13744 bool fndecl_type = (complain & tf_fndecl_type);
13745 complain &= ~tf_fndecl_type;
13746
13747 if (type
13748 && code != TYPENAME_TYPE
13749 && code != TEMPLATE_TYPE_PARM
13750 && code != TEMPLATE_PARM_INDEX
13751 && code != IDENTIFIER_NODE
13752 && code != FUNCTION_TYPE
13753 && code != METHOD_TYPE)
13754 type = tsubst (type, args, complain, in_decl);
13755 if (type == error_mark_node)
13756 return error_mark_node;
13757
13758 switch (code)
13759 {
13760 case RECORD_TYPE:
13761 case UNION_TYPE:
13762 case ENUMERAL_TYPE:
13763 return tsubst_aggr_type (t, args, complain, in_decl,
13764 /*entering_scope=*/0);
13765
13766 case ERROR_MARK:
13767 case IDENTIFIER_NODE:
13768 case VOID_TYPE:
13769 case REAL_TYPE:
13770 case COMPLEX_TYPE:
13771 case VECTOR_TYPE:
13772 case BOOLEAN_TYPE:
13773 case NULLPTR_TYPE:
13774 case LANG_TYPE:
13775 return t;
13776
13777 case INTEGER_TYPE:
13778 if (t == integer_type_node)
13779 return t;
13780
13781 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13782 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13783 return t;
13784
13785 {
13786 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13787
13788 max = tsubst_expr (omax, args, complain, in_decl,
13789 /*integral_constant_expression_p=*/false);
13790
13791 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13792 needed. */
13793 if (TREE_CODE (max) == NOP_EXPR
13794 && TREE_SIDE_EFFECTS (omax)
13795 && !TREE_TYPE (max))
13796 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13797
13798 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13799 with TREE_SIDE_EFFECTS that indicates this is not an integral
13800 constant expression. */
13801 if (processing_template_decl
13802 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13803 {
13804 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13805 TREE_SIDE_EFFECTS (max) = 1;
13806 }
13807
13808 return compute_array_index_type (NULL_TREE, max, complain);
13809 }
13810
13811 case TEMPLATE_TYPE_PARM:
13812 case TEMPLATE_TEMPLATE_PARM:
13813 case BOUND_TEMPLATE_TEMPLATE_PARM:
13814 case TEMPLATE_PARM_INDEX:
13815 {
13816 int idx;
13817 int level;
13818 int levels;
13819 tree arg = NULL_TREE;
13820
13821 /* Early in template argument deduction substitution, we don't
13822 want to reduce the level of 'auto', or it will be confused
13823 with a normal template parm in subsequent deduction. */
13824 if (is_auto (t) && (complain & tf_partial))
13825 return t;
13826
13827 r = NULL_TREE;
13828
13829 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13830 template_parm_level_and_index (t, &level, &idx);
13831
13832 levels = TMPL_ARGS_DEPTH (args);
13833 if (level <= levels
13834 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13835 {
13836 arg = TMPL_ARG (args, level, idx);
13837
13838 /* See through ARGUMENT_PACK_SELECT arguments. */
13839 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13840 arg = argument_pack_select_arg (arg);
13841 }
13842
13843 if (arg == error_mark_node)
13844 return error_mark_node;
13845 else if (arg != NULL_TREE)
13846 {
13847 if (ARGUMENT_PACK_P (arg))
13848 /* If ARG is an argument pack, we don't actually want to
13849 perform a substitution here, because substitutions
13850 for argument packs are only done
13851 element-by-element. We can get to this point when
13852 substituting the type of a non-type template
13853 parameter pack, when that type actually contains
13854 template parameter packs from an outer template, e.g.,
13855
13856 template<typename... Types> struct A {
13857 template<Types... Values> struct B { };
13858 }; */
13859 return t;
13860
13861 if (code == TEMPLATE_TYPE_PARM)
13862 {
13863 int quals;
13864 gcc_assert (TYPE_P (arg));
13865
13866 quals = cp_type_quals (arg) | cp_type_quals (t);
13867
13868 return cp_build_qualified_type_real
13869 (arg, quals, complain | tf_ignore_bad_quals);
13870 }
13871 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13872 {
13873 /* We are processing a type constructed from a
13874 template template parameter. */
13875 tree argvec = tsubst (TYPE_TI_ARGS (t),
13876 args, complain, in_decl);
13877 if (argvec == error_mark_node)
13878 return error_mark_node;
13879
13880 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13881 || TREE_CODE (arg) == TEMPLATE_DECL
13882 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13883
13884 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13885 /* Consider this code:
13886
13887 template <template <class> class Template>
13888 struct Internal {
13889 template <class Arg> using Bind = Template<Arg>;
13890 };
13891
13892 template <template <class> class Template, class Arg>
13893 using Instantiate = Template<Arg>; //#0
13894
13895 template <template <class> class Template,
13896 class Argument>
13897 using Bind =
13898 Instantiate<Internal<Template>::template Bind,
13899 Argument>; //#1
13900
13901 When #1 is parsed, the
13902 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13903 parameter `Template' in #0 matches the
13904 UNBOUND_CLASS_TEMPLATE representing the argument
13905 `Internal<Template>::template Bind'; We then want
13906 to assemble the type `Bind<Argument>' that can't
13907 be fully created right now, because
13908 `Internal<Template>' not being complete, the Bind
13909 template cannot be looked up in that context. So
13910 we need to "store" `Bind<Argument>' for later
13911 when the context of Bind becomes complete. Let's
13912 store that in a TYPENAME_TYPE. */
13913 return make_typename_type (TYPE_CONTEXT (arg),
13914 build_nt (TEMPLATE_ID_EXPR,
13915 TYPE_IDENTIFIER (arg),
13916 argvec),
13917 typename_type,
13918 complain);
13919
13920 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13921 are resolving nested-types in the signature of a
13922 member function templates. Otherwise ARG is a
13923 TEMPLATE_DECL and is the real template to be
13924 instantiated. */
13925 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13926 arg = TYPE_NAME (arg);
13927
13928 r = lookup_template_class (arg,
13929 argvec, in_decl,
13930 DECL_CONTEXT (arg),
13931 /*entering_scope=*/0,
13932 complain);
13933 return cp_build_qualified_type_real
13934 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13935 }
13936 else if (code == TEMPLATE_TEMPLATE_PARM)
13937 return arg;
13938 else
13939 /* TEMPLATE_PARM_INDEX. */
13940 return convert_from_reference (unshare_expr (arg));
13941 }
13942
13943 if (level == 1)
13944 /* This can happen during the attempted tsubst'ing in
13945 unify. This means that we don't yet have any information
13946 about the template parameter in question. */
13947 return t;
13948
13949 /* If we get here, we must have been looking at a parm for a
13950 more deeply nested template. Make a new version of this
13951 template parameter, but with a lower level. */
13952 switch (code)
13953 {
13954 case TEMPLATE_TYPE_PARM:
13955 case TEMPLATE_TEMPLATE_PARM:
13956 case BOUND_TEMPLATE_TEMPLATE_PARM:
13957 if (cp_type_quals (t))
13958 {
13959 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13960 r = cp_build_qualified_type_real
13961 (r, cp_type_quals (t),
13962 complain | (code == TEMPLATE_TYPE_PARM
13963 ? tf_ignore_bad_quals : 0));
13964 }
13965 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13966 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13967 && (r = (TEMPLATE_PARM_DESCENDANTS
13968 (TEMPLATE_TYPE_PARM_INDEX (t))))
13969 && (r = TREE_TYPE (r))
13970 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13971 /* Break infinite recursion when substituting the constraints
13972 of a constrained placeholder. */;
13973 else
13974 {
13975 r = copy_type (t);
13976 TEMPLATE_TYPE_PARM_INDEX (r)
13977 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13978 r, levels, args, complain);
13979 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13980 TYPE_MAIN_VARIANT (r) = r;
13981 TYPE_POINTER_TO (r) = NULL_TREE;
13982 TYPE_REFERENCE_TO (r) = NULL_TREE;
13983
13984 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13985 {
13986 /* Propagate constraints on placeholders. */
13987 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13988 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13989 = tsubst_constraint (constr, args, complain, in_decl);
13990 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13991 {
13992 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13993 pl = tsubst (pl, args, complain, in_decl);
13994 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13995 }
13996 }
13997
13998 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13999 /* We have reduced the level of the template
14000 template parameter, but not the levels of its
14001 template parameters, so canonical_type_parameter
14002 will not be able to find the canonical template
14003 template parameter for this level. Thus, we
14004 require structural equality checking to compare
14005 TEMPLATE_TEMPLATE_PARMs. */
14006 SET_TYPE_STRUCTURAL_EQUALITY (r);
14007 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14008 SET_TYPE_STRUCTURAL_EQUALITY (r);
14009 else
14010 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14011
14012 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14013 {
14014 tree tinfo = TYPE_TEMPLATE_INFO (t);
14015 /* We might need to substitute into the types of non-type
14016 template parameters. */
14017 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14018 complain, in_decl);
14019 if (tmpl == error_mark_node)
14020 return error_mark_node;
14021 tree argvec = tsubst (TI_ARGS (tinfo), args,
14022 complain, in_decl);
14023 if (argvec == error_mark_node)
14024 return error_mark_node;
14025
14026 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14027 = build_template_info (tmpl, argvec);
14028 }
14029 }
14030 break;
14031
14032 case TEMPLATE_PARM_INDEX:
14033 /* OK, now substitute the type of the non-type parameter. We
14034 couldn't do it earlier because it might be an auto parameter,
14035 and we wouldn't need to if we had an argument. */
14036 type = tsubst (type, args, complain, in_decl);
14037 if (type == error_mark_node)
14038 return error_mark_node;
14039 r = reduce_template_parm_level (t, type, levels, args, complain);
14040 break;
14041
14042 default:
14043 gcc_unreachable ();
14044 }
14045
14046 return r;
14047 }
14048
14049 case TREE_LIST:
14050 {
14051 tree purpose, value, chain;
14052
14053 if (t == void_list_node)
14054 return t;
14055
14056 purpose = TREE_PURPOSE (t);
14057 if (purpose)
14058 {
14059 purpose = tsubst (purpose, args, complain, in_decl);
14060 if (purpose == error_mark_node)
14061 return error_mark_node;
14062 }
14063 value = TREE_VALUE (t);
14064 if (value)
14065 {
14066 value = tsubst (value, args, complain, in_decl);
14067 if (value == error_mark_node)
14068 return error_mark_node;
14069 }
14070 chain = TREE_CHAIN (t);
14071 if (chain && chain != void_type_node)
14072 {
14073 chain = tsubst (chain, args, complain, in_decl);
14074 if (chain == error_mark_node)
14075 return error_mark_node;
14076 }
14077 if (purpose == TREE_PURPOSE (t)
14078 && value == TREE_VALUE (t)
14079 && chain == TREE_CHAIN (t))
14080 return t;
14081 return hash_tree_cons (purpose, value, chain);
14082 }
14083
14084 case TREE_BINFO:
14085 /* We should never be tsubsting a binfo. */
14086 gcc_unreachable ();
14087
14088 case TREE_VEC:
14089 /* A vector of template arguments. */
14090 gcc_assert (!type);
14091 return tsubst_template_args (t, args, complain, in_decl);
14092
14093 case POINTER_TYPE:
14094 case REFERENCE_TYPE:
14095 {
14096 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14097 return t;
14098
14099 /* [temp.deduct]
14100
14101 Type deduction may fail for any of the following
14102 reasons:
14103
14104 -- Attempting to create a pointer to reference type.
14105 -- Attempting to create a reference to a reference type or
14106 a reference to void.
14107
14108 Core issue 106 says that creating a reference to a reference
14109 during instantiation is no longer a cause for failure. We
14110 only enforce this check in strict C++98 mode. */
14111 if ((TREE_CODE (type) == REFERENCE_TYPE
14112 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14113 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14114 {
14115 static location_t last_loc;
14116
14117 /* We keep track of the last time we issued this error
14118 message to avoid spewing a ton of messages during a
14119 single bad template instantiation. */
14120 if (complain & tf_error
14121 && last_loc != input_location)
14122 {
14123 if (VOID_TYPE_P (type))
14124 error ("forming reference to void");
14125 else if (code == POINTER_TYPE)
14126 error ("forming pointer to reference type %qT", type);
14127 else
14128 error ("forming reference to reference type %qT", type);
14129 last_loc = input_location;
14130 }
14131
14132 return error_mark_node;
14133 }
14134 else if (TREE_CODE (type) == FUNCTION_TYPE
14135 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14136 || type_memfn_rqual (type) != REF_QUAL_NONE))
14137 {
14138 if (complain & tf_error)
14139 {
14140 if (code == POINTER_TYPE)
14141 error ("forming pointer to qualified function type %qT",
14142 type);
14143 else
14144 error ("forming reference to qualified function type %qT",
14145 type);
14146 }
14147 return error_mark_node;
14148 }
14149 else if (code == POINTER_TYPE)
14150 {
14151 r = build_pointer_type (type);
14152 if (TREE_CODE (type) == METHOD_TYPE)
14153 r = build_ptrmemfunc_type (r);
14154 }
14155 else if (TREE_CODE (type) == REFERENCE_TYPE)
14156 /* In C++0x, during template argument substitution, when there is an
14157 attempt to create a reference to a reference type, reference
14158 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14159
14160 "If a template-argument for a template-parameter T names a type
14161 that is a reference to a type A, an attempt to create the type
14162 'lvalue reference to cv T' creates the type 'lvalue reference to
14163 A,' while an attempt to create the type type rvalue reference to
14164 cv T' creates the type T"
14165 */
14166 r = cp_build_reference_type
14167 (TREE_TYPE (type),
14168 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14169 else
14170 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14171 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14172
14173 if (r != error_mark_node)
14174 /* Will this ever be needed for TYPE_..._TO values? */
14175 layout_type (r);
14176
14177 return r;
14178 }
14179 case OFFSET_TYPE:
14180 {
14181 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14182 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14183 {
14184 /* [temp.deduct]
14185
14186 Type deduction may fail for any of the following
14187 reasons:
14188
14189 -- Attempting to create "pointer to member of T" when T
14190 is not a class type. */
14191 if (complain & tf_error)
14192 error ("creating pointer to member of non-class type %qT", r);
14193 return error_mark_node;
14194 }
14195 if (TREE_CODE (type) == REFERENCE_TYPE)
14196 {
14197 if (complain & tf_error)
14198 error ("creating pointer to member reference type %qT", type);
14199 return error_mark_node;
14200 }
14201 if (VOID_TYPE_P (type))
14202 {
14203 if (complain & tf_error)
14204 error ("creating pointer to member of type void");
14205 return error_mark_node;
14206 }
14207 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14208 if (TREE_CODE (type) == FUNCTION_TYPE)
14209 {
14210 /* The type of the implicit object parameter gets its
14211 cv-qualifiers from the FUNCTION_TYPE. */
14212 tree memptr;
14213 tree method_type
14214 = build_memfn_type (type, r, type_memfn_quals (type),
14215 type_memfn_rqual (type));
14216 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14217 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14218 complain);
14219 }
14220 else
14221 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14222 cp_type_quals (t),
14223 complain);
14224 }
14225 case FUNCTION_TYPE:
14226 case METHOD_TYPE:
14227 {
14228 tree fntype;
14229 tree specs;
14230 fntype = tsubst_function_type (t, args, complain, in_decl);
14231 if (fntype == error_mark_node)
14232 return error_mark_node;
14233
14234 /* Substitute the exception specification. */
14235 specs = tsubst_exception_specification (t, args, complain, in_decl,
14236 /*defer_ok*/fndecl_type);
14237 if (specs == error_mark_node)
14238 return error_mark_node;
14239 if (specs)
14240 fntype = build_exception_variant (fntype, specs);
14241 return fntype;
14242 }
14243 case ARRAY_TYPE:
14244 {
14245 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14246 if (domain == error_mark_node)
14247 return error_mark_node;
14248
14249 /* As an optimization, we avoid regenerating the array type if
14250 it will obviously be the same as T. */
14251 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14252 return t;
14253
14254 /* These checks should match the ones in create_array_type_for_decl.
14255
14256 [temp.deduct]
14257
14258 The deduction may fail for any of the following reasons:
14259
14260 -- Attempting to create an array with an element type that
14261 is void, a function type, or a reference type, or [DR337]
14262 an abstract class type. */
14263 if (VOID_TYPE_P (type)
14264 || TREE_CODE (type) == FUNCTION_TYPE
14265 || (TREE_CODE (type) == ARRAY_TYPE
14266 && TYPE_DOMAIN (type) == NULL_TREE)
14267 || TREE_CODE (type) == REFERENCE_TYPE)
14268 {
14269 if (complain & tf_error)
14270 error ("creating array of %qT", type);
14271 return error_mark_node;
14272 }
14273
14274 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14275 return error_mark_node;
14276
14277 r = build_cplus_array_type (type, domain);
14278
14279 if (TYPE_USER_ALIGN (t))
14280 {
14281 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14282 TYPE_USER_ALIGN (r) = 1;
14283 }
14284
14285 return r;
14286 }
14287
14288 case TYPENAME_TYPE:
14289 {
14290 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14291 in_decl, /*entering_scope=*/1);
14292 if (ctx == error_mark_node)
14293 return error_mark_node;
14294
14295 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14296 complain, in_decl);
14297 if (f == error_mark_node)
14298 return error_mark_node;
14299
14300 if (!MAYBE_CLASS_TYPE_P (ctx))
14301 {
14302 if (complain & tf_error)
14303 error ("%qT is not a class, struct, or union type", ctx);
14304 return error_mark_node;
14305 }
14306 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14307 {
14308 /* Normally, make_typename_type does not require that the CTX
14309 have complete type in order to allow things like:
14310
14311 template <class T> struct S { typename S<T>::X Y; };
14312
14313 But, such constructs have already been resolved by this
14314 point, so here CTX really should have complete type, unless
14315 it's a partial instantiation. */
14316 ctx = complete_type (ctx);
14317 if (!COMPLETE_TYPE_P (ctx))
14318 {
14319 if (complain & tf_error)
14320 cxx_incomplete_type_error (NULL_TREE, ctx);
14321 return error_mark_node;
14322 }
14323 }
14324
14325 f = make_typename_type (ctx, f, typename_type,
14326 complain | tf_keep_type_decl);
14327 if (f == error_mark_node)
14328 return f;
14329 if (TREE_CODE (f) == TYPE_DECL)
14330 {
14331 complain |= tf_ignore_bad_quals;
14332 f = TREE_TYPE (f);
14333 }
14334
14335 if (TREE_CODE (f) != TYPENAME_TYPE)
14336 {
14337 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14338 {
14339 if (complain & tf_error)
14340 error ("%qT resolves to %qT, which is not an enumeration type",
14341 t, f);
14342 else
14343 return error_mark_node;
14344 }
14345 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14346 {
14347 if (complain & tf_error)
14348 error ("%qT resolves to %qT, which is is not a class type",
14349 t, f);
14350 else
14351 return error_mark_node;
14352 }
14353 }
14354
14355 return cp_build_qualified_type_real
14356 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14357 }
14358
14359 case UNBOUND_CLASS_TEMPLATE:
14360 {
14361 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14362 in_decl, /*entering_scope=*/1);
14363 tree name = TYPE_IDENTIFIER (t);
14364 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14365
14366 if (ctx == error_mark_node || name == error_mark_node)
14367 return error_mark_node;
14368
14369 if (parm_list)
14370 parm_list = tsubst_template_parms (parm_list, args, complain);
14371 return make_unbound_class_template (ctx, name, parm_list, complain);
14372 }
14373
14374 case TYPEOF_TYPE:
14375 {
14376 tree type;
14377
14378 ++cp_unevaluated_operand;
14379 ++c_inhibit_evaluation_warnings;
14380
14381 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14382 complain, in_decl,
14383 /*integral_constant_expression_p=*/false);
14384
14385 --cp_unevaluated_operand;
14386 --c_inhibit_evaluation_warnings;
14387
14388 type = finish_typeof (type);
14389 return cp_build_qualified_type_real (type,
14390 cp_type_quals (t)
14391 | cp_type_quals (type),
14392 complain);
14393 }
14394
14395 case DECLTYPE_TYPE:
14396 {
14397 tree type;
14398
14399 ++cp_unevaluated_operand;
14400 ++c_inhibit_evaluation_warnings;
14401
14402 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14403 complain|tf_decltype, in_decl,
14404 /*function_p*/false,
14405 /*integral_constant_expression*/false);
14406
14407 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14408 {
14409 if (type == NULL_TREE)
14410 {
14411 if (complain & tf_error)
14412 error ("empty initializer in lambda init-capture");
14413 type = error_mark_node;
14414 }
14415 else if (TREE_CODE (type) == TREE_LIST)
14416 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14417 }
14418
14419 --cp_unevaluated_operand;
14420 --c_inhibit_evaluation_warnings;
14421
14422 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14423 type = lambda_capture_field_type (type,
14424 DECLTYPE_FOR_INIT_CAPTURE (t),
14425 DECLTYPE_FOR_REF_CAPTURE (t));
14426 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14427 type = lambda_proxy_type (type);
14428 else
14429 {
14430 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14431 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14432 && EXPR_P (type))
14433 /* In a template ~id could be either a complement expression
14434 or an unqualified-id naming a destructor; if instantiating
14435 it produces an expression, it's not an id-expression or
14436 member access. */
14437 id = false;
14438 type = finish_decltype_type (type, id, complain);
14439 }
14440 return cp_build_qualified_type_real (type,
14441 cp_type_quals (t)
14442 | cp_type_quals (type),
14443 complain | tf_ignore_bad_quals);
14444 }
14445
14446 case UNDERLYING_TYPE:
14447 {
14448 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14449 complain, in_decl);
14450 return finish_underlying_type (type);
14451 }
14452
14453 case TYPE_ARGUMENT_PACK:
14454 case NONTYPE_ARGUMENT_PACK:
14455 {
14456 tree r;
14457
14458 if (code == NONTYPE_ARGUMENT_PACK)
14459 r = make_node (code);
14460 else
14461 r = cxx_make_type (code);
14462
14463 tree pack_args = ARGUMENT_PACK_ARGS (t);
14464 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14465 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14466
14467 return r;
14468 }
14469
14470 case VOID_CST:
14471 case INTEGER_CST:
14472 case REAL_CST:
14473 case STRING_CST:
14474 case PLUS_EXPR:
14475 case MINUS_EXPR:
14476 case NEGATE_EXPR:
14477 case NOP_EXPR:
14478 case INDIRECT_REF:
14479 case ADDR_EXPR:
14480 case CALL_EXPR:
14481 case ARRAY_REF:
14482 case SCOPE_REF:
14483 /* We should use one of the expression tsubsts for these codes. */
14484 gcc_unreachable ();
14485
14486 default:
14487 sorry ("use of %qs in template", get_tree_code_name (code));
14488 return error_mark_node;
14489 }
14490 }
14491
14492 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14493 expression on the left-hand side of the "." or "->" operator. We
14494 only do the lookup if we had a dependent BASELINK. Otherwise we
14495 adjust it onto the instantiated heirarchy. */
14496
14497 static tree
14498 tsubst_baselink (tree baselink, tree object_type,
14499 tree args, tsubst_flags_t complain, tree in_decl)
14500 {
14501 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14502 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14503 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14504
14505 tree optype = BASELINK_OPTYPE (baselink);
14506 optype = tsubst (optype, args, complain, in_decl);
14507
14508 tree template_args = NULL_TREE;
14509 bool template_id_p = false;
14510 tree fns = BASELINK_FUNCTIONS (baselink);
14511 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14512 {
14513 template_id_p = true;
14514 template_args = TREE_OPERAND (fns, 1);
14515 fns = TREE_OPERAND (fns, 0);
14516 if (template_args)
14517 template_args = tsubst_template_args (template_args, args,
14518 complain, in_decl);
14519 }
14520
14521 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14522 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14523 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14524
14525 if (dependent_p)
14526 {
14527 tree name = OVL_NAME (fns);
14528 if (IDENTIFIER_CONV_OP_P (name))
14529 name = make_conv_op_name (optype);
14530
14531 if (name == complete_dtor_identifier)
14532 /* Treat as-if non-dependent below. */
14533 dependent_p = false;
14534
14535 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14536 if (!baselink)
14537 {
14538 if ((complain & tf_error)
14539 && constructor_name_p (name, qualifying_scope))
14540 error ("cannot call constructor %<%T::%D%> directly",
14541 qualifying_scope, name);
14542 return error_mark_node;
14543 }
14544
14545 if (BASELINK_P (baselink))
14546 fns = BASELINK_FUNCTIONS (baselink);
14547 }
14548 else
14549 /* We're going to overwrite pieces below, make a duplicate. */
14550 baselink = copy_node (baselink);
14551
14552 /* If lookup found a single function, mark it as used at this point.
14553 (If lookup found multiple functions the one selected later by
14554 overload resolution will be marked as used at that point.) */
14555 if (!template_id_p && !really_overloaded_fn (fns)
14556 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14557 return error_mark_node;
14558
14559 if (BASELINK_P (baselink))
14560 {
14561 /* Add back the template arguments, if present. */
14562 if (template_id_p)
14563 BASELINK_FUNCTIONS (baselink)
14564 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14565
14566 /* Update the conversion operator type. */
14567 BASELINK_OPTYPE (baselink) = optype;
14568 }
14569
14570 if (!object_type)
14571 object_type = current_class_type;
14572
14573 if (qualified_p || !dependent_p)
14574 {
14575 baselink = adjust_result_of_qualified_name_lookup (baselink,
14576 qualifying_scope,
14577 object_type);
14578 if (!qualified_p)
14579 /* We need to call adjust_result_of_qualified_name_lookup in case the
14580 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14581 so that we still get virtual function binding. */
14582 BASELINK_QUALIFIED_P (baselink) = false;
14583 }
14584
14585 return baselink;
14586 }
14587
14588 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14589 true if the qualified-id will be a postfix-expression in-and-of
14590 itself; false if more of the postfix-expression follows the
14591 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14592 of "&". */
14593
14594 static tree
14595 tsubst_qualified_id (tree qualified_id, tree args,
14596 tsubst_flags_t complain, tree in_decl,
14597 bool done, bool address_p)
14598 {
14599 tree expr;
14600 tree scope;
14601 tree name;
14602 bool is_template;
14603 tree template_args;
14604 location_t loc = UNKNOWN_LOCATION;
14605
14606 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14607
14608 /* Figure out what name to look up. */
14609 name = TREE_OPERAND (qualified_id, 1);
14610 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14611 {
14612 is_template = true;
14613 loc = EXPR_LOCATION (name);
14614 template_args = TREE_OPERAND (name, 1);
14615 if (template_args)
14616 template_args = tsubst_template_args (template_args, args,
14617 complain, in_decl);
14618 if (template_args == error_mark_node)
14619 return error_mark_node;
14620 name = TREE_OPERAND (name, 0);
14621 }
14622 else
14623 {
14624 is_template = false;
14625 template_args = NULL_TREE;
14626 }
14627
14628 /* Substitute into the qualifying scope. When there are no ARGS, we
14629 are just trying to simplify a non-dependent expression. In that
14630 case the qualifying scope may be dependent, and, in any case,
14631 substituting will not help. */
14632 scope = TREE_OPERAND (qualified_id, 0);
14633 if (args)
14634 {
14635 scope = tsubst (scope, args, complain, in_decl);
14636 expr = tsubst_copy (name, args, complain, in_decl);
14637 }
14638 else
14639 expr = name;
14640
14641 if (dependent_scope_p (scope))
14642 {
14643 if (is_template)
14644 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14645 tree r = build_qualified_name (NULL_TREE, scope, expr,
14646 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14647 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14648 return r;
14649 }
14650
14651 if (!BASELINK_P (name) && !DECL_P (expr))
14652 {
14653 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14654 {
14655 /* A BIT_NOT_EXPR is used to represent a destructor. */
14656 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14657 {
14658 error ("qualifying type %qT does not match destructor name ~%qT",
14659 scope, TREE_OPERAND (expr, 0));
14660 expr = error_mark_node;
14661 }
14662 else
14663 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14664 /*is_type_p=*/0, false);
14665 }
14666 else
14667 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14668 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14669 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14670 {
14671 if (complain & tf_error)
14672 {
14673 error ("dependent-name %qE is parsed as a non-type, but "
14674 "instantiation yields a type", qualified_id);
14675 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14676 }
14677 return error_mark_node;
14678 }
14679 }
14680
14681 if (DECL_P (expr))
14682 {
14683 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14684 scope);
14685 /* Remember that there was a reference to this entity. */
14686 if (!mark_used (expr, complain) && !(complain & tf_error))
14687 return error_mark_node;
14688 }
14689
14690 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14691 {
14692 if (complain & tf_error)
14693 qualified_name_lookup_error (scope,
14694 TREE_OPERAND (qualified_id, 1),
14695 expr, input_location);
14696 return error_mark_node;
14697 }
14698
14699 if (is_template)
14700 {
14701 if (variable_template_p (expr))
14702 expr = lookup_and_finish_template_variable (expr, template_args,
14703 complain);
14704 else
14705 expr = lookup_template_function (expr, template_args);
14706 }
14707
14708 if (expr == error_mark_node && complain & tf_error)
14709 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14710 expr, input_location);
14711 else if (TYPE_P (scope))
14712 {
14713 expr = (adjust_result_of_qualified_name_lookup
14714 (expr, scope, current_nonlambda_class_type ()));
14715 expr = (finish_qualified_id_expr
14716 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14717 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14718 /*template_arg_p=*/false, complain));
14719 }
14720
14721 /* Expressions do not generally have reference type. */
14722 if (TREE_CODE (expr) != SCOPE_REF
14723 /* However, if we're about to form a pointer-to-member, we just
14724 want the referenced member referenced. */
14725 && TREE_CODE (expr) != OFFSET_REF)
14726 expr = convert_from_reference (expr);
14727
14728 if (REF_PARENTHESIZED_P (qualified_id))
14729 expr = force_paren_expr (expr);
14730
14731 return expr;
14732 }
14733
14734 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14735 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14736 for tsubst. */
14737
14738 static tree
14739 tsubst_init (tree init, tree decl, tree args,
14740 tsubst_flags_t complain, tree in_decl)
14741 {
14742 if (!init)
14743 return NULL_TREE;
14744
14745 init = tsubst_expr (init, args, complain, in_decl, false);
14746
14747 if (!init && TREE_TYPE (decl) != error_mark_node)
14748 {
14749 /* If we had an initializer but it
14750 instantiated to nothing,
14751 value-initialize the object. This will
14752 only occur when the initializer was a
14753 pack expansion where the parameter packs
14754 used in that expansion were of length
14755 zero. */
14756 init = build_value_init (TREE_TYPE (decl),
14757 complain);
14758 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14759 init = get_target_expr_sfinae (init, complain);
14760 if (TREE_CODE (init) == TARGET_EXPR)
14761 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14762 }
14763
14764 return init;
14765 }
14766
14767 /* Like tsubst, but deals with expressions. This function just replaces
14768 template parms; to finish processing the resultant expression, use
14769 tsubst_copy_and_build or tsubst_expr. */
14770
14771 static tree
14772 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14773 {
14774 enum tree_code code;
14775 tree r;
14776
14777 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14778 return t;
14779
14780 code = TREE_CODE (t);
14781
14782 switch (code)
14783 {
14784 case PARM_DECL:
14785 r = retrieve_local_specialization (t);
14786
14787 if (r == NULL_TREE)
14788 {
14789 /* We get here for a use of 'this' in an NSDMI. */
14790 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14791 return current_class_ptr;
14792
14793 /* This can happen for a parameter name used later in a function
14794 declaration (such as in a late-specified return type). Just
14795 make a dummy decl, since it's only used for its type. */
14796 gcc_assert (cp_unevaluated_operand != 0);
14797 r = tsubst_decl (t, args, complain);
14798 /* Give it the template pattern as its context; its true context
14799 hasn't been instantiated yet and this is good enough for
14800 mangling. */
14801 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14802 }
14803
14804 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14805 r = argument_pack_select_arg (r);
14806 if (!mark_used (r, complain) && !(complain & tf_error))
14807 return error_mark_node;
14808 return r;
14809
14810 case CONST_DECL:
14811 {
14812 tree enum_type;
14813 tree v;
14814
14815 if (DECL_TEMPLATE_PARM_P (t))
14816 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14817 /* There is no need to substitute into namespace-scope
14818 enumerators. */
14819 if (DECL_NAMESPACE_SCOPE_P (t))
14820 return t;
14821 /* If ARGS is NULL, then T is known to be non-dependent. */
14822 if (args == NULL_TREE)
14823 return scalar_constant_value (t);
14824
14825 /* Unfortunately, we cannot just call lookup_name here.
14826 Consider:
14827
14828 template <int I> int f() {
14829 enum E { a = I };
14830 struct S { void g() { E e = a; } };
14831 };
14832
14833 When we instantiate f<7>::S::g(), say, lookup_name is not
14834 clever enough to find f<7>::a. */
14835 enum_type
14836 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14837 /*entering_scope=*/0);
14838
14839 for (v = TYPE_VALUES (enum_type);
14840 v != NULL_TREE;
14841 v = TREE_CHAIN (v))
14842 if (TREE_PURPOSE (v) == DECL_NAME (t))
14843 return TREE_VALUE (v);
14844
14845 /* We didn't find the name. That should never happen; if
14846 name-lookup found it during preliminary parsing, we
14847 should find it again here during instantiation. */
14848 gcc_unreachable ();
14849 }
14850 return t;
14851
14852 case FIELD_DECL:
14853 if (DECL_CONTEXT (t))
14854 {
14855 tree ctx;
14856
14857 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14858 /*entering_scope=*/1);
14859 if (ctx != DECL_CONTEXT (t))
14860 {
14861 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14862 if (!r)
14863 {
14864 if (complain & tf_error)
14865 error ("using invalid field %qD", t);
14866 return error_mark_node;
14867 }
14868 return r;
14869 }
14870 }
14871
14872 return t;
14873
14874 case VAR_DECL:
14875 case FUNCTION_DECL:
14876 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14877 r = tsubst (t, args, complain, in_decl);
14878 else if (local_variable_p (t)
14879 && uses_template_parms (DECL_CONTEXT (t)))
14880 {
14881 r = retrieve_local_specialization (t);
14882 if (r == NULL_TREE)
14883 {
14884 /* First try name lookup to find the instantiation. */
14885 r = lookup_name (DECL_NAME (t));
14886 if (r && !is_capture_proxy (r))
14887 {
14888 /* Make sure that the one we found is the one we want. */
14889 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14890 if (ctx != DECL_CONTEXT (r))
14891 r = NULL_TREE;
14892 }
14893
14894 if (r)
14895 /* OK */;
14896 else
14897 {
14898 /* This can happen for a variable used in a
14899 late-specified return type of a local lambda, or for a
14900 local static or constant. Building a new VAR_DECL
14901 should be OK in all those cases. */
14902 r = tsubst_decl (t, args, complain);
14903 if (local_specializations)
14904 /* Avoid infinite recursion (79640). */
14905 register_local_specialization (r, t);
14906 if (decl_maybe_constant_var_p (r))
14907 {
14908 /* We can't call cp_finish_decl, so handle the
14909 initializer by hand. */
14910 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14911 complain, in_decl);
14912 if (!processing_template_decl)
14913 init = maybe_constant_init (init);
14914 if (processing_template_decl
14915 ? potential_constant_expression (init)
14916 : reduced_constant_expression_p (init))
14917 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14918 = TREE_CONSTANT (r) = true;
14919 DECL_INITIAL (r) = init;
14920 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14921 TREE_TYPE (r)
14922 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14923 complain, adc_variable_type);
14924 }
14925 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14926 || decl_constant_var_p (r)
14927 || errorcount || sorrycount);
14928 if (!processing_template_decl
14929 && !TREE_STATIC (r))
14930 r = process_outer_var_ref (r, complain);
14931 }
14932 /* Remember this for subsequent uses. */
14933 if (local_specializations)
14934 register_local_specialization (r, t);
14935 }
14936 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14937 r = argument_pack_select_arg (r);
14938 }
14939 else
14940 r = t;
14941 if (!mark_used (r, complain))
14942 return error_mark_node;
14943 return r;
14944
14945 case NAMESPACE_DECL:
14946 return t;
14947
14948 case OVERLOAD:
14949 /* An OVERLOAD will always be a non-dependent overload set; an
14950 overload set from function scope will just be represented with an
14951 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14952 gcc_assert (!uses_template_parms (t));
14953 /* We must have marked any lookups as persistent. */
14954 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14955 return t;
14956
14957 case BASELINK:
14958 return tsubst_baselink (t, current_nonlambda_class_type (),
14959 args, complain, in_decl);
14960
14961 case TEMPLATE_DECL:
14962 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14963 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14964 args, complain, in_decl);
14965 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14966 return tsubst (t, args, complain, in_decl);
14967 else if (DECL_CLASS_SCOPE_P (t)
14968 && uses_template_parms (DECL_CONTEXT (t)))
14969 {
14970 /* Template template argument like the following example need
14971 special treatment:
14972
14973 template <template <class> class TT> struct C {};
14974 template <class T> struct D {
14975 template <class U> struct E {};
14976 C<E> c; // #1
14977 };
14978 D<int> d; // #2
14979
14980 We are processing the template argument `E' in #1 for
14981 the template instantiation #2. Originally, `E' is a
14982 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14983 have to substitute this with one having context `D<int>'. */
14984
14985 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14986 if (dependent_scope_p (context))
14987 {
14988 /* When rewriting a constructor into a deduction guide, a
14989 non-dependent name can become dependent, so memtmpl<args>
14990 becomes context::template memtmpl<args>. */
14991 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14992 return build_qualified_name (type, context, DECL_NAME (t),
14993 /*template*/true);
14994 }
14995 return lookup_field (context, DECL_NAME(t), 0, false);
14996 }
14997 else
14998 /* Ordinary template template argument. */
14999 return t;
15000
15001 case NON_LVALUE_EXPR:
15002 case VIEW_CONVERT_EXPR:
15003 {
15004 /* Handle location wrappers by substituting the wrapped node
15005 first, *then* reusing the resulting type. Doing the type
15006 first ensures that we handle template parameters and
15007 parameter pack expansions. */
15008 gcc_assert (location_wrapper_p (t));
15009 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15010 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15011 }
15012
15013 case CAST_EXPR:
15014 case REINTERPRET_CAST_EXPR:
15015 case CONST_CAST_EXPR:
15016 case STATIC_CAST_EXPR:
15017 case DYNAMIC_CAST_EXPR:
15018 case IMPLICIT_CONV_EXPR:
15019 case CONVERT_EXPR:
15020 case NOP_EXPR:
15021 {
15022 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15023 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15024 return build1 (code, type, op0);
15025 }
15026
15027 case SIZEOF_EXPR:
15028 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15029 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15030 {
15031 tree expanded, op = TREE_OPERAND (t, 0);
15032 int len = 0;
15033
15034 if (SIZEOF_EXPR_TYPE_P (t))
15035 op = TREE_TYPE (op);
15036
15037 ++cp_unevaluated_operand;
15038 ++c_inhibit_evaluation_warnings;
15039 /* We only want to compute the number of arguments. */
15040 if (PACK_EXPANSION_P (op))
15041 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15042 else
15043 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15044 args, complain, in_decl);
15045 --cp_unevaluated_operand;
15046 --c_inhibit_evaluation_warnings;
15047
15048 if (TREE_CODE (expanded) == TREE_VEC)
15049 {
15050 len = TREE_VEC_LENGTH (expanded);
15051 /* Set TREE_USED for the benefit of -Wunused. */
15052 for (int i = 0; i < len; i++)
15053 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15054 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15055 }
15056
15057 if (expanded == error_mark_node)
15058 return error_mark_node;
15059 else if (PACK_EXPANSION_P (expanded)
15060 || (TREE_CODE (expanded) == TREE_VEC
15061 && pack_expansion_args_count (expanded)))
15062
15063 {
15064 if (PACK_EXPANSION_P (expanded))
15065 /* OK. */;
15066 else if (TREE_VEC_LENGTH (expanded) == 1)
15067 expanded = TREE_VEC_ELT (expanded, 0);
15068 else
15069 expanded = make_argument_pack (expanded);
15070
15071 if (TYPE_P (expanded))
15072 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15073 complain & tf_error);
15074 else
15075 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15076 complain & tf_error);
15077 }
15078 else
15079 return build_int_cst (size_type_node, len);
15080 }
15081 if (SIZEOF_EXPR_TYPE_P (t))
15082 {
15083 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15084 args, complain, in_decl);
15085 r = build1 (NOP_EXPR, r, error_mark_node);
15086 r = build1 (SIZEOF_EXPR,
15087 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15088 SIZEOF_EXPR_TYPE_P (r) = 1;
15089 return r;
15090 }
15091 /* Fall through */
15092
15093 case INDIRECT_REF:
15094 case NEGATE_EXPR:
15095 case TRUTH_NOT_EXPR:
15096 case BIT_NOT_EXPR:
15097 case ADDR_EXPR:
15098 case UNARY_PLUS_EXPR: /* Unary + */
15099 case ALIGNOF_EXPR:
15100 case AT_ENCODE_EXPR:
15101 case ARROW_EXPR:
15102 case THROW_EXPR:
15103 case TYPEID_EXPR:
15104 case REALPART_EXPR:
15105 case IMAGPART_EXPR:
15106 case PAREN_EXPR:
15107 {
15108 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15109 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15110 return build1 (code, type, op0);
15111 }
15112
15113 case COMPONENT_REF:
15114 {
15115 tree object;
15116 tree name;
15117
15118 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15119 name = TREE_OPERAND (t, 1);
15120 if (TREE_CODE (name) == BIT_NOT_EXPR)
15121 {
15122 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15123 complain, in_decl);
15124 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15125 }
15126 else if (TREE_CODE (name) == SCOPE_REF
15127 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15128 {
15129 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15130 complain, in_decl);
15131 name = TREE_OPERAND (name, 1);
15132 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15133 complain, in_decl);
15134 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15135 name = build_qualified_name (/*type=*/NULL_TREE,
15136 base, name,
15137 /*template_p=*/false);
15138 }
15139 else if (BASELINK_P (name))
15140 name = tsubst_baselink (name,
15141 non_reference (TREE_TYPE (object)),
15142 args, complain,
15143 in_decl);
15144 else
15145 name = tsubst_copy (name, args, complain, in_decl);
15146 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15147 }
15148
15149 case PLUS_EXPR:
15150 case MINUS_EXPR:
15151 case MULT_EXPR:
15152 case TRUNC_DIV_EXPR:
15153 case CEIL_DIV_EXPR:
15154 case FLOOR_DIV_EXPR:
15155 case ROUND_DIV_EXPR:
15156 case EXACT_DIV_EXPR:
15157 case BIT_AND_EXPR:
15158 case BIT_IOR_EXPR:
15159 case BIT_XOR_EXPR:
15160 case TRUNC_MOD_EXPR:
15161 case FLOOR_MOD_EXPR:
15162 case TRUTH_ANDIF_EXPR:
15163 case TRUTH_ORIF_EXPR:
15164 case TRUTH_AND_EXPR:
15165 case TRUTH_OR_EXPR:
15166 case RSHIFT_EXPR:
15167 case LSHIFT_EXPR:
15168 case RROTATE_EXPR:
15169 case LROTATE_EXPR:
15170 case EQ_EXPR:
15171 case NE_EXPR:
15172 case MAX_EXPR:
15173 case MIN_EXPR:
15174 case LE_EXPR:
15175 case GE_EXPR:
15176 case LT_EXPR:
15177 case GT_EXPR:
15178 case COMPOUND_EXPR:
15179 case DOTSTAR_EXPR:
15180 case MEMBER_REF:
15181 case PREDECREMENT_EXPR:
15182 case PREINCREMENT_EXPR:
15183 case POSTDECREMENT_EXPR:
15184 case POSTINCREMENT_EXPR:
15185 {
15186 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15187 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15188 return build_nt (code, op0, op1);
15189 }
15190
15191 case SCOPE_REF:
15192 {
15193 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15194 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15195 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15196 QUALIFIED_NAME_IS_TEMPLATE (t));
15197 }
15198
15199 case ARRAY_REF:
15200 {
15201 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15202 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15203 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15204 }
15205
15206 case CALL_EXPR:
15207 {
15208 int n = VL_EXP_OPERAND_LENGTH (t);
15209 tree result = build_vl_exp (CALL_EXPR, n);
15210 int i;
15211 for (i = 0; i < n; i++)
15212 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15213 complain, in_decl);
15214 return result;
15215 }
15216
15217 case COND_EXPR:
15218 case MODOP_EXPR:
15219 case PSEUDO_DTOR_EXPR:
15220 case VEC_PERM_EXPR:
15221 {
15222 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15223 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15224 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15225 r = build_nt (code, op0, op1, op2);
15226 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15227 return r;
15228 }
15229
15230 case NEW_EXPR:
15231 {
15232 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15233 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15234 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15235 r = build_nt (code, op0, op1, op2);
15236 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15237 return r;
15238 }
15239
15240 case DELETE_EXPR:
15241 {
15242 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15243 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15244 r = build_nt (code, op0, op1);
15245 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15246 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15247 return r;
15248 }
15249
15250 case TEMPLATE_ID_EXPR:
15251 {
15252 /* Substituted template arguments */
15253 tree fn = TREE_OPERAND (t, 0);
15254 tree targs = TREE_OPERAND (t, 1);
15255
15256 fn = tsubst_copy (fn, args, complain, in_decl);
15257 if (targs)
15258 targs = tsubst_template_args (targs, args, complain, in_decl);
15259
15260 return lookup_template_function (fn, targs);
15261 }
15262
15263 case TREE_LIST:
15264 {
15265 tree purpose, value, chain;
15266
15267 if (t == void_list_node)
15268 return t;
15269
15270 purpose = TREE_PURPOSE (t);
15271 if (purpose)
15272 purpose = tsubst_copy (purpose, args, complain, in_decl);
15273 value = TREE_VALUE (t);
15274 if (value)
15275 value = tsubst_copy (value, args, complain, in_decl);
15276 chain = TREE_CHAIN (t);
15277 if (chain && chain != void_type_node)
15278 chain = tsubst_copy (chain, args, complain, in_decl);
15279 if (purpose == TREE_PURPOSE (t)
15280 && value == TREE_VALUE (t)
15281 && chain == TREE_CHAIN (t))
15282 return t;
15283 return tree_cons (purpose, value, chain);
15284 }
15285
15286 case RECORD_TYPE:
15287 case UNION_TYPE:
15288 case ENUMERAL_TYPE:
15289 case INTEGER_TYPE:
15290 case TEMPLATE_TYPE_PARM:
15291 case TEMPLATE_TEMPLATE_PARM:
15292 case BOUND_TEMPLATE_TEMPLATE_PARM:
15293 case TEMPLATE_PARM_INDEX:
15294 case POINTER_TYPE:
15295 case REFERENCE_TYPE:
15296 case OFFSET_TYPE:
15297 case FUNCTION_TYPE:
15298 case METHOD_TYPE:
15299 case ARRAY_TYPE:
15300 case TYPENAME_TYPE:
15301 case UNBOUND_CLASS_TEMPLATE:
15302 case TYPEOF_TYPE:
15303 case DECLTYPE_TYPE:
15304 case TYPE_DECL:
15305 return tsubst (t, args, complain, in_decl);
15306
15307 case USING_DECL:
15308 t = DECL_NAME (t);
15309 /* Fall through. */
15310 case IDENTIFIER_NODE:
15311 if (IDENTIFIER_CONV_OP_P (t))
15312 {
15313 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15314 return make_conv_op_name (new_type);
15315 }
15316 else
15317 return t;
15318
15319 case CONSTRUCTOR:
15320 /* This is handled by tsubst_copy_and_build. */
15321 gcc_unreachable ();
15322
15323 case VA_ARG_EXPR:
15324 {
15325 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15326 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15327 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15328 }
15329
15330 case CLEANUP_POINT_EXPR:
15331 /* We shouldn't have built any of these during initial template
15332 generation. Instead, they should be built during instantiation
15333 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15334 gcc_unreachable ();
15335
15336 case OFFSET_REF:
15337 {
15338 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15339 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15340 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15341 r = build2 (code, type, op0, op1);
15342 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15343 if (!mark_used (TREE_OPERAND (r, 1), complain)
15344 && !(complain & tf_error))
15345 return error_mark_node;
15346 return r;
15347 }
15348
15349 case EXPR_PACK_EXPANSION:
15350 error ("invalid use of pack expansion expression");
15351 return error_mark_node;
15352
15353 case NONTYPE_ARGUMENT_PACK:
15354 error ("use %<...%> to expand argument pack");
15355 return error_mark_node;
15356
15357 case VOID_CST:
15358 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15359 return t;
15360
15361 case INTEGER_CST:
15362 case REAL_CST:
15363 case STRING_CST:
15364 case COMPLEX_CST:
15365 {
15366 /* Instantiate any typedefs in the type. */
15367 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15368 r = fold_convert (type, t);
15369 gcc_assert (TREE_CODE (r) == code);
15370 return r;
15371 }
15372
15373 case PTRMEM_CST:
15374 /* These can sometimes show up in a partial instantiation, but never
15375 involve template parms. */
15376 gcc_assert (!uses_template_parms (t));
15377 return t;
15378
15379 case UNARY_LEFT_FOLD_EXPR:
15380 return tsubst_unary_left_fold (t, args, complain, in_decl);
15381 case UNARY_RIGHT_FOLD_EXPR:
15382 return tsubst_unary_right_fold (t, args, complain, in_decl);
15383 case BINARY_LEFT_FOLD_EXPR:
15384 return tsubst_binary_left_fold (t, args, complain, in_decl);
15385 case BINARY_RIGHT_FOLD_EXPR:
15386 return tsubst_binary_right_fold (t, args, complain, in_decl);
15387 case PREDICT_EXPR:
15388 return t;
15389
15390 case DEBUG_BEGIN_STMT:
15391 /* ??? There's no point in copying it for now, but maybe some
15392 day it will contain more information, such as a pointer back
15393 to the containing function, inlined copy or so. */
15394 return t;
15395
15396 default:
15397 /* We shouldn't get here, but keep going if !flag_checking. */
15398 if (flag_checking)
15399 gcc_unreachable ();
15400 return t;
15401 }
15402 }
15403
15404 /* Helper function for tsubst_omp_clauses, used for instantiation of
15405 OMP_CLAUSE_DECL of clauses. */
15406
15407 static tree
15408 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15409 tree in_decl)
15410 {
15411 if (decl == NULL_TREE)
15412 return NULL_TREE;
15413
15414 /* Handle an OpenMP array section represented as a TREE_LIST (or
15415 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15416 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15417 TREE_LIST. We can handle it exactly the same as an array section
15418 (purpose, value, and a chain), even though the nomenclature
15419 (low_bound, length, etc) is different. */
15420 if (TREE_CODE (decl) == TREE_LIST)
15421 {
15422 tree low_bound
15423 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15424 /*integral_constant_expression_p=*/false);
15425 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15426 /*integral_constant_expression_p=*/false);
15427 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15428 in_decl);
15429 if (TREE_PURPOSE (decl) == low_bound
15430 && TREE_VALUE (decl) == length
15431 && TREE_CHAIN (decl) == chain)
15432 return decl;
15433 tree ret = tree_cons (low_bound, length, chain);
15434 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15435 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15436 return ret;
15437 }
15438 tree ret = tsubst_expr (decl, args, complain, in_decl,
15439 /*integral_constant_expression_p=*/false);
15440 /* Undo convert_from_reference tsubst_expr could have called. */
15441 if (decl
15442 && REFERENCE_REF_P (ret)
15443 && !REFERENCE_REF_P (decl))
15444 ret = TREE_OPERAND (ret, 0);
15445 return ret;
15446 }
15447
15448 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15449
15450 static tree
15451 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15452 tree args, tsubst_flags_t complain, tree in_decl)
15453 {
15454 tree new_clauses = NULL_TREE, nc, oc;
15455 tree linear_no_step = NULL_TREE;
15456
15457 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15458 {
15459 nc = copy_node (oc);
15460 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15461 new_clauses = nc;
15462
15463 switch (OMP_CLAUSE_CODE (nc))
15464 {
15465 case OMP_CLAUSE_LASTPRIVATE:
15466 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15467 {
15468 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15469 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15470 in_decl, /*integral_constant_expression_p=*/false);
15471 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15472 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15473 }
15474 /* FALLTHRU */
15475 case OMP_CLAUSE_PRIVATE:
15476 case OMP_CLAUSE_SHARED:
15477 case OMP_CLAUSE_FIRSTPRIVATE:
15478 case OMP_CLAUSE_COPYIN:
15479 case OMP_CLAUSE_COPYPRIVATE:
15480 case OMP_CLAUSE_UNIFORM:
15481 case OMP_CLAUSE_DEPEND:
15482 case OMP_CLAUSE_FROM:
15483 case OMP_CLAUSE_TO:
15484 case OMP_CLAUSE_MAP:
15485 case OMP_CLAUSE_USE_DEVICE_PTR:
15486 case OMP_CLAUSE_IS_DEVICE_PTR:
15487 OMP_CLAUSE_DECL (nc)
15488 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15489 in_decl);
15490 break;
15491 case OMP_CLAUSE_TILE:
15492 case OMP_CLAUSE_IF:
15493 case OMP_CLAUSE_NUM_THREADS:
15494 case OMP_CLAUSE_SCHEDULE:
15495 case OMP_CLAUSE_COLLAPSE:
15496 case OMP_CLAUSE_FINAL:
15497 case OMP_CLAUSE_DEVICE:
15498 case OMP_CLAUSE_DIST_SCHEDULE:
15499 case OMP_CLAUSE_NUM_TEAMS:
15500 case OMP_CLAUSE_THREAD_LIMIT:
15501 case OMP_CLAUSE_SAFELEN:
15502 case OMP_CLAUSE_SIMDLEN:
15503 case OMP_CLAUSE_NUM_TASKS:
15504 case OMP_CLAUSE_GRAINSIZE:
15505 case OMP_CLAUSE_PRIORITY:
15506 case OMP_CLAUSE_ORDERED:
15507 case OMP_CLAUSE_HINT:
15508 case OMP_CLAUSE_NUM_GANGS:
15509 case OMP_CLAUSE_NUM_WORKERS:
15510 case OMP_CLAUSE_VECTOR_LENGTH:
15511 case OMP_CLAUSE_WORKER:
15512 case OMP_CLAUSE_VECTOR:
15513 case OMP_CLAUSE_ASYNC:
15514 case OMP_CLAUSE_WAIT:
15515 OMP_CLAUSE_OPERAND (nc, 0)
15516 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15517 in_decl, /*integral_constant_expression_p=*/false);
15518 break;
15519 case OMP_CLAUSE_REDUCTION:
15520 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15521 {
15522 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15523 if (TREE_CODE (placeholder) == SCOPE_REF)
15524 {
15525 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15526 complain, in_decl);
15527 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15528 = build_qualified_name (NULL_TREE, scope,
15529 TREE_OPERAND (placeholder, 1),
15530 false);
15531 }
15532 else
15533 gcc_assert (identifier_p (placeholder));
15534 }
15535 OMP_CLAUSE_DECL (nc)
15536 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15537 in_decl);
15538 break;
15539 case OMP_CLAUSE_GANG:
15540 case OMP_CLAUSE_ALIGNED:
15541 OMP_CLAUSE_DECL (nc)
15542 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15543 in_decl);
15544 OMP_CLAUSE_OPERAND (nc, 1)
15545 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15546 in_decl, /*integral_constant_expression_p=*/false);
15547 break;
15548 case OMP_CLAUSE_LINEAR:
15549 OMP_CLAUSE_DECL (nc)
15550 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15551 in_decl);
15552 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15553 {
15554 gcc_assert (!linear_no_step);
15555 linear_no_step = nc;
15556 }
15557 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15558 OMP_CLAUSE_LINEAR_STEP (nc)
15559 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15560 complain, in_decl);
15561 else
15562 OMP_CLAUSE_LINEAR_STEP (nc)
15563 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15564 in_decl,
15565 /*integral_constant_expression_p=*/false);
15566 break;
15567 case OMP_CLAUSE_NOWAIT:
15568 case OMP_CLAUSE_DEFAULT:
15569 case OMP_CLAUSE_UNTIED:
15570 case OMP_CLAUSE_MERGEABLE:
15571 case OMP_CLAUSE_INBRANCH:
15572 case OMP_CLAUSE_NOTINBRANCH:
15573 case OMP_CLAUSE_PROC_BIND:
15574 case OMP_CLAUSE_FOR:
15575 case OMP_CLAUSE_PARALLEL:
15576 case OMP_CLAUSE_SECTIONS:
15577 case OMP_CLAUSE_TASKGROUP:
15578 case OMP_CLAUSE_NOGROUP:
15579 case OMP_CLAUSE_THREADS:
15580 case OMP_CLAUSE_SIMD:
15581 case OMP_CLAUSE_DEFAULTMAP:
15582 case OMP_CLAUSE_INDEPENDENT:
15583 case OMP_CLAUSE_AUTO:
15584 case OMP_CLAUSE_SEQ:
15585 break;
15586 default:
15587 gcc_unreachable ();
15588 }
15589 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15590 switch (OMP_CLAUSE_CODE (nc))
15591 {
15592 case OMP_CLAUSE_SHARED:
15593 case OMP_CLAUSE_PRIVATE:
15594 case OMP_CLAUSE_FIRSTPRIVATE:
15595 case OMP_CLAUSE_LASTPRIVATE:
15596 case OMP_CLAUSE_COPYPRIVATE:
15597 case OMP_CLAUSE_LINEAR:
15598 case OMP_CLAUSE_REDUCTION:
15599 case OMP_CLAUSE_USE_DEVICE_PTR:
15600 case OMP_CLAUSE_IS_DEVICE_PTR:
15601 /* tsubst_expr on SCOPE_REF results in returning
15602 finish_non_static_data_member result. Undo that here. */
15603 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15604 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15605 == IDENTIFIER_NODE))
15606 {
15607 tree t = OMP_CLAUSE_DECL (nc);
15608 tree v = t;
15609 while (v)
15610 switch (TREE_CODE (v))
15611 {
15612 case COMPONENT_REF:
15613 case MEM_REF:
15614 case INDIRECT_REF:
15615 CASE_CONVERT:
15616 case POINTER_PLUS_EXPR:
15617 v = TREE_OPERAND (v, 0);
15618 continue;
15619 case PARM_DECL:
15620 if (DECL_CONTEXT (v) == current_function_decl
15621 && DECL_ARTIFICIAL (v)
15622 && DECL_NAME (v) == this_identifier)
15623 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15624 /* FALLTHRU */
15625 default:
15626 v = NULL_TREE;
15627 break;
15628 }
15629 }
15630 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15631 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15632 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15633 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15634 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15635 {
15636 tree decl = OMP_CLAUSE_DECL (nc);
15637 if (VAR_P (decl))
15638 {
15639 retrofit_lang_decl (decl);
15640 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15641 }
15642 }
15643 break;
15644 default:
15645 break;
15646 }
15647 }
15648
15649 new_clauses = nreverse (new_clauses);
15650 if (ort != C_ORT_OMP_DECLARE_SIMD)
15651 {
15652 new_clauses = finish_omp_clauses (new_clauses, ort);
15653 if (linear_no_step)
15654 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15655 if (nc == linear_no_step)
15656 {
15657 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15658 break;
15659 }
15660 }
15661 return new_clauses;
15662 }
15663
15664 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15665
15666 static tree
15667 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15668 tree in_decl)
15669 {
15670 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15671
15672 tree purpose, value, chain;
15673
15674 if (t == NULL)
15675 return t;
15676
15677 if (TREE_CODE (t) != TREE_LIST)
15678 return tsubst_copy_and_build (t, args, complain, in_decl,
15679 /*function_p=*/false,
15680 /*integral_constant_expression_p=*/false);
15681
15682 if (t == void_list_node)
15683 return t;
15684
15685 purpose = TREE_PURPOSE (t);
15686 if (purpose)
15687 purpose = RECUR (purpose);
15688 value = TREE_VALUE (t);
15689 if (value)
15690 {
15691 if (TREE_CODE (value) != LABEL_DECL)
15692 value = RECUR (value);
15693 else
15694 {
15695 value = lookup_label (DECL_NAME (value));
15696 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15697 TREE_USED (value) = 1;
15698 }
15699 }
15700 chain = TREE_CHAIN (t);
15701 if (chain && chain != void_type_node)
15702 chain = RECUR (chain);
15703 return tree_cons (purpose, value, chain);
15704 #undef RECUR
15705 }
15706
15707 /* Used to temporarily communicate the list of #pragma omp parallel
15708 clauses to #pragma omp for instantiation if they are combined
15709 together. */
15710
15711 static tree *omp_parallel_combined_clauses;
15712
15713 /* Substitute one OMP_FOR iterator. */
15714
15715 static void
15716 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15717 tree initv, tree condv, tree incrv, tree *clauses,
15718 tree args, tsubst_flags_t complain, tree in_decl,
15719 bool integral_constant_expression_p)
15720 {
15721 #define RECUR(NODE) \
15722 tsubst_expr ((NODE), args, complain, in_decl, \
15723 integral_constant_expression_p)
15724 tree decl, init, cond, incr;
15725
15726 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15727 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15728
15729 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15730 {
15731 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15732 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15733 }
15734
15735 decl = TREE_OPERAND (init, 0);
15736 init = TREE_OPERAND (init, 1);
15737 tree decl_expr = NULL_TREE;
15738 if (init && TREE_CODE (init) == DECL_EXPR)
15739 {
15740 /* We need to jump through some hoops to handle declarations in the
15741 init-statement, since we might need to handle auto deduction,
15742 but we need to keep control of initialization. */
15743 decl_expr = init;
15744 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15745 decl = tsubst_decl (decl, args, complain);
15746 }
15747 else
15748 {
15749 if (TREE_CODE (decl) == SCOPE_REF)
15750 {
15751 decl = RECUR (decl);
15752 if (TREE_CODE (decl) == COMPONENT_REF)
15753 {
15754 tree v = decl;
15755 while (v)
15756 switch (TREE_CODE (v))
15757 {
15758 case COMPONENT_REF:
15759 case MEM_REF:
15760 case INDIRECT_REF:
15761 CASE_CONVERT:
15762 case POINTER_PLUS_EXPR:
15763 v = TREE_OPERAND (v, 0);
15764 continue;
15765 case PARM_DECL:
15766 if (DECL_CONTEXT (v) == current_function_decl
15767 && DECL_ARTIFICIAL (v)
15768 && DECL_NAME (v) == this_identifier)
15769 {
15770 decl = TREE_OPERAND (decl, 1);
15771 decl = omp_privatize_field (decl, false);
15772 }
15773 /* FALLTHRU */
15774 default:
15775 v = NULL_TREE;
15776 break;
15777 }
15778 }
15779 }
15780 else
15781 decl = RECUR (decl);
15782 }
15783 init = RECUR (init);
15784
15785 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15786 if (auto_node && init)
15787 TREE_TYPE (decl)
15788 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15789
15790 gcc_assert (!type_dependent_expression_p (decl));
15791
15792 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15793 {
15794 if (decl_expr)
15795 {
15796 /* Declare the variable, but don't let that initialize it. */
15797 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15798 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15799 RECUR (decl_expr);
15800 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15801 }
15802
15803 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15804 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15805 if (TREE_CODE (incr) == MODIFY_EXPR)
15806 {
15807 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15808 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15809 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15810 NOP_EXPR, rhs, complain);
15811 }
15812 else
15813 incr = RECUR (incr);
15814 TREE_VEC_ELT (declv, i) = decl;
15815 TREE_VEC_ELT (initv, i) = init;
15816 TREE_VEC_ELT (condv, i) = cond;
15817 TREE_VEC_ELT (incrv, i) = incr;
15818 return;
15819 }
15820
15821 if (decl_expr)
15822 {
15823 /* Declare and initialize the variable. */
15824 RECUR (decl_expr);
15825 init = NULL_TREE;
15826 }
15827 else if (init)
15828 {
15829 tree *pc;
15830 int j;
15831 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15832 {
15833 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15834 {
15835 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15836 && OMP_CLAUSE_DECL (*pc) == decl)
15837 break;
15838 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15839 && OMP_CLAUSE_DECL (*pc) == decl)
15840 {
15841 if (j)
15842 break;
15843 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15844 tree c = *pc;
15845 *pc = OMP_CLAUSE_CHAIN (c);
15846 OMP_CLAUSE_CHAIN (c) = *clauses;
15847 *clauses = c;
15848 }
15849 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15850 && OMP_CLAUSE_DECL (*pc) == decl)
15851 {
15852 error ("iteration variable %qD should not be firstprivate",
15853 decl);
15854 *pc = OMP_CLAUSE_CHAIN (*pc);
15855 }
15856 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15857 && OMP_CLAUSE_DECL (*pc) == decl)
15858 {
15859 error ("iteration variable %qD should not be reduction",
15860 decl);
15861 *pc = OMP_CLAUSE_CHAIN (*pc);
15862 }
15863 else
15864 pc = &OMP_CLAUSE_CHAIN (*pc);
15865 }
15866 if (*pc)
15867 break;
15868 }
15869 if (*pc == NULL_TREE)
15870 {
15871 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15872 OMP_CLAUSE_DECL (c) = decl;
15873 c = finish_omp_clauses (c, C_ORT_OMP);
15874 if (c)
15875 {
15876 OMP_CLAUSE_CHAIN (c) = *clauses;
15877 *clauses = c;
15878 }
15879 }
15880 }
15881 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15882 if (COMPARISON_CLASS_P (cond))
15883 {
15884 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15885 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15886 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15887 }
15888 else
15889 cond = RECUR (cond);
15890 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15891 switch (TREE_CODE (incr))
15892 {
15893 case PREINCREMENT_EXPR:
15894 case PREDECREMENT_EXPR:
15895 case POSTINCREMENT_EXPR:
15896 case POSTDECREMENT_EXPR:
15897 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15898 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15899 break;
15900 case MODIFY_EXPR:
15901 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15902 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15903 {
15904 tree rhs = TREE_OPERAND (incr, 1);
15905 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15906 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15907 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15908 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15909 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15910 rhs0, rhs1));
15911 }
15912 else
15913 incr = RECUR (incr);
15914 break;
15915 case MODOP_EXPR:
15916 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15917 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15918 {
15919 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15920 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15921 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15922 TREE_TYPE (decl), lhs,
15923 RECUR (TREE_OPERAND (incr, 2))));
15924 }
15925 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15926 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15927 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15928 {
15929 tree rhs = TREE_OPERAND (incr, 2);
15930 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15931 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15932 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15933 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15934 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15935 rhs0, rhs1));
15936 }
15937 else
15938 incr = RECUR (incr);
15939 break;
15940 default:
15941 incr = RECUR (incr);
15942 break;
15943 }
15944
15945 TREE_VEC_ELT (declv, i) = decl;
15946 TREE_VEC_ELT (initv, i) = init;
15947 TREE_VEC_ELT (condv, i) = cond;
15948 TREE_VEC_ELT (incrv, i) = incr;
15949 #undef RECUR
15950 }
15951
15952 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15953 of OMP_TARGET's body. */
15954
15955 static tree
15956 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15957 {
15958 *walk_subtrees = 0;
15959 switch (TREE_CODE (*tp))
15960 {
15961 case OMP_TEAMS:
15962 return *tp;
15963 case BIND_EXPR:
15964 case STATEMENT_LIST:
15965 *walk_subtrees = 1;
15966 break;
15967 default:
15968 break;
15969 }
15970 return NULL_TREE;
15971 }
15972
15973 /* Helper function for tsubst_expr. For decomposition declaration
15974 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15975 also the corresponding decls representing the identifiers
15976 of the decomposition declaration. Return DECL if successful
15977 or error_mark_node otherwise, set *FIRST to the first decl
15978 in the list chained through DECL_CHAIN and *CNT to the number
15979 of such decls. */
15980
15981 static tree
15982 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15983 tsubst_flags_t complain, tree in_decl, tree *first,
15984 unsigned int *cnt)
15985 {
15986 tree decl2, decl3, prev = decl;
15987 *cnt = 0;
15988 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15989 for (decl2 = DECL_CHAIN (pattern_decl);
15990 decl2
15991 && VAR_P (decl2)
15992 && DECL_DECOMPOSITION_P (decl2)
15993 && DECL_NAME (decl2);
15994 decl2 = DECL_CHAIN (decl2))
15995 {
15996 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15997 {
15998 gcc_assert (errorcount);
15999 return error_mark_node;
16000 }
16001 (*cnt)++;
16002 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16003 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16004 tree v = DECL_VALUE_EXPR (decl2);
16005 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16006 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16007 decl3 = tsubst (decl2, args, complain, in_decl);
16008 SET_DECL_VALUE_EXPR (decl2, v);
16009 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16010 if (VAR_P (decl3))
16011 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16012 maybe_push_decl (decl3);
16013 if (error_operand_p (decl3))
16014 decl = error_mark_node;
16015 else if (decl != error_mark_node
16016 && DECL_CHAIN (decl3) != prev)
16017 {
16018 gcc_assert (errorcount);
16019 decl = error_mark_node;
16020 }
16021 else
16022 prev = decl3;
16023 }
16024 *first = prev;
16025 return decl;
16026 }
16027
16028 /* Like tsubst_copy for expressions, etc. but also does semantic
16029 processing. */
16030
16031 tree
16032 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16033 bool integral_constant_expression_p)
16034 {
16035 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16036 #define RECUR(NODE) \
16037 tsubst_expr ((NODE), args, complain, in_decl, \
16038 integral_constant_expression_p)
16039
16040 tree stmt, tmp;
16041 tree r;
16042 location_t loc;
16043
16044 if (t == NULL_TREE || t == error_mark_node)
16045 return t;
16046
16047 loc = input_location;
16048 if (EXPR_HAS_LOCATION (t))
16049 input_location = EXPR_LOCATION (t);
16050 if (STATEMENT_CODE_P (TREE_CODE (t)))
16051 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16052
16053 switch (TREE_CODE (t))
16054 {
16055 case STATEMENT_LIST:
16056 {
16057 tree_stmt_iterator i;
16058 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16059 RECUR (tsi_stmt (i));
16060 break;
16061 }
16062
16063 case CTOR_INITIALIZER:
16064 finish_mem_initializers (tsubst_initializer_list
16065 (TREE_OPERAND (t, 0), args));
16066 break;
16067
16068 case RETURN_EXPR:
16069 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16070 break;
16071
16072 case EXPR_STMT:
16073 tmp = RECUR (EXPR_STMT_EXPR (t));
16074 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16075 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16076 else
16077 finish_expr_stmt (tmp);
16078 break;
16079
16080 case USING_STMT:
16081 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16082 /*attribs=*/NULL_TREE);
16083 break;
16084
16085 case DECL_EXPR:
16086 {
16087 tree decl, pattern_decl;
16088 tree init;
16089
16090 pattern_decl = decl = DECL_EXPR_DECL (t);
16091 if (TREE_CODE (decl) == LABEL_DECL)
16092 finish_label_decl (DECL_NAME (decl));
16093 else if (TREE_CODE (decl) == USING_DECL)
16094 {
16095 tree scope = USING_DECL_SCOPE (decl);
16096 tree name = DECL_NAME (decl);
16097
16098 scope = tsubst (scope, args, complain, in_decl);
16099 decl = lookup_qualified_name (scope, name,
16100 /*is_type_p=*/false,
16101 /*complain=*/false);
16102 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16103 qualified_name_lookup_error (scope, name, decl, input_location);
16104 else
16105 finish_local_using_decl (decl, scope, name);
16106 }
16107 else if (is_capture_proxy (decl)
16108 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16109 {
16110 /* We're in tsubst_lambda_expr, we've already inserted a new
16111 capture proxy, so look it up and register it. */
16112 tree inst;
16113 if (DECL_PACK_P (decl))
16114 {
16115 inst = (retrieve_local_specialization
16116 (DECL_CAPTURED_VARIABLE (decl)));
16117 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16118 }
16119 else
16120 {
16121 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16122 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16123 gcc_assert (inst != decl && is_capture_proxy (inst));
16124 }
16125 register_local_specialization (inst, decl);
16126 break;
16127 }
16128 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16129 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16130 /* Don't copy the old closure; we'll create a new one in
16131 tsubst_lambda_expr. */
16132 break;
16133 else
16134 {
16135 init = DECL_INITIAL (decl);
16136 decl = tsubst (decl, args, complain, in_decl);
16137 if (decl != error_mark_node)
16138 {
16139 /* By marking the declaration as instantiated, we avoid
16140 trying to instantiate it. Since instantiate_decl can't
16141 handle local variables, and since we've already done
16142 all that needs to be done, that's the right thing to
16143 do. */
16144 if (VAR_P (decl))
16145 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16146 if (VAR_P (decl)
16147 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16148 /* Anonymous aggregates are a special case. */
16149 finish_anon_union (decl);
16150 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16151 {
16152 DECL_CONTEXT (decl) = current_function_decl;
16153 if (DECL_NAME (decl) == this_identifier)
16154 {
16155 tree lam = DECL_CONTEXT (current_function_decl);
16156 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16157 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16158 }
16159 insert_capture_proxy (decl);
16160 }
16161 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16162 /* We already did a pushtag. */;
16163 else if (TREE_CODE (decl) == FUNCTION_DECL
16164 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16165 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16166 {
16167 DECL_CONTEXT (decl) = NULL_TREE;
16168 pushdecl (decl);
16169 DECL_CONTEXT (decl) = current_function_decl;
16170 cp_check_omp_declare_reduction (decl);
16171 }
16172 else
16173 {
16174 int const_init = false;
16175 maybe_push_decl (decl);
16176 if (VAR_P (decl)
16177 && DECL_PRETTY_FUNCTION_P (decl))
16178 {
16179 /* For __PRETTY_FUNCTION__ we have to adjust the
16180 initializer. */
16181 const char *const name
16182 = cxx_printable_name (current_function_decl, 2);
16183 init = cp_fname_init (name, &TREE_TYPE (decl));
16184 }
16185 else
16186 init = tsubst_init (init, decl, args, complain, in_decl);
16187
16188 if (VAR_P (decl))
16189 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16190 (pattern_decl));
16191 if (VAR_P (decl)
16192 && DECL_DECOMPOSITION_P (decl)
16193 && TREE_TYPE (pattern_decl) != error_mark_node)
16194 {
16195 unsigned int cnt;
16196 tree first;
16197 tree ndecl
16198 = tsubst_decomp_names (decl, pattern_decl, args,
16199 complain, in_decl, &first, &cnt);
16200 if (ndecl != error_mark_node)
16201 cp_maybe_mangle_decomp (ndecl, first, cnt);
16202 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16203 if (ndecl != error_mark_node)
16204 cp_finish_decomp (ndecl, first, cnt);
16205 }
16206 else
16207 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16208 }
16209 }
16210 }
16211
16212 break;
16213 }
16214
16215 case FOR_STMT:
16216 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16217 RECUR (FOR_INIT_STMT (t));
16218 finish_init_stmt (stmt);
16219 tmp = RECUR (FOR_COND (t));
16220 finish_for_cond (tmp, stmt, false, 0);
16221 tmp = RECUR (FOR_EXPR (t));
16222 finish_for_expr (tmp, stmt);
16223 {
16224 bool prev = note_iteration_stmt_body_start ();
16225 RECUR (FOR_BODY (t));
16226 note_iteration_stmt_body_end (prev);
16227 }
16228 finish_for_stmt (stmt);
16229 break;
16230
16231 case RANGE_FOR_STMT:
16232 {
16233 /* Construct another range_for, if this is not a final
16234 substitution (for inside inside a generic lambda of a
16235 template). Otherwise convert to a regular for. */
16236 tree decl, expr;
16237 stmt = (processing_template_decl
16238 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16239 : begin_for_stmt (NULL_TREE, NULL_TREE));
16240 decl = RANGE_FOR_DECL (t);
16241 decl = tsubst (decl, args, complain, in_decl);
16242 maybe_push_decl (decl);
16243 expr = RECUR (RANGE_FOR_EXPR (t));
16244
16245 tree decomp_first = NULL_TREE;
16246 unsigned decomp_cnt = 0;
16247 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16248 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16249 complain, in_decl,
16250 &decomp_first, &decomp_cnt);
16251
16252 if (processing_template_decl)
16253 {
16254 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16255 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16256 finish_range_for_decl (stmt, decl, expr);
16257 }
16258 else
16259 {
16260 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16261 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16262 stmt = cp_convert_range_for (stmt, decl, expr,
16263 decomp_first, decomp_cnt,
16264 RANGE_FOR_IVDEP (t), unroll);
16265 }
16266
16267 bool prev = note_iteration_stmt_body_start ();
16268 RECUR (RANGE_FOR_BODY (t));
16269 note_iteration_stmt_body_end (prev);
16270 finish_for_stmt (stmt);
16271 }
16272 break;
16273
16274 case WHILE_STMT:
16275 stmt = begin_while_stmt ();
16276 tmp = RECUR (WHILE_COND (t));
16277 finish_while_stmt_cond (tmp, stmt, false, 0);
16278 {
16279 bool prev = note_iteration_stmt_body_start ();
16280 RECUR (WHILE_BODY (t));
16281 note_iteration_stmt_body_end (prev);
16282 }
16283 finish_while_stmt (stmt);
16284 break;
16285
16286 case DO_STMT:
16287 stmt = begin_do_stmt ();
16288 {
16289 bool prev = note_iteration_stmt_body_start ();
16290 RECUR (DO_BODY (t));
16291 note_iteration_stmt_body_end (prev);
16292 }
16293 finish_do_body (stmt);
16294 tmp = RECUR (DO_COND (t));
16295 finish_do_stmt (tmp, stmt, false, 0);
16296 break;
16297
16298 case IF_STMT:
16299 stmt = begin_if_stmt ();
16300 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16301 tmp = RECUR (IF_COND (t));
16302 tmp = finish_if_stmt_cond (tmp, stmt);
16303 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16304 /* Don't instantiate the THEN_CLAUSE. */;
16305 else
16306 {
16307 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16308 if (inhibit)
16309 ++c_inhibit_evaluation_warnings;
16310 RECUR (THEN_CLAUSE (t));
16311 if (inhibit)
16312 --c_inhibit_evaluation_warnings;
16313 }
16314 finish_then_clause (stmt);
16315
16316 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16317 /* Don't instantiate the ELSE_CLAUSE. */;
16318 else if (ELSE_CLAUSE (t))
16319 {
16320 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16321 begin_else_clause (stmt);
16322 if (inhibit)
16323 ++c_inhibit_evaluation_warnings;
16324 RECUR (ELSE_CLAUSE (t));
16325 if (inhibit)
16326 --c_inhibit_evaluation_warnings;
16327 finish_else_clause (stmt);
16328 }
16329
16330 finish_if_stmt (stmt);
16331 break;
16332
16333 case BIND_EXPR:
16334 if (BIND_EXPR_BODY_BLOCK (t))
16335 stmt = begin_function_body ();
16336 else
16337 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16338 ? BCS_TRY_BLOCK : 0);
16339
16340 RECUR (BIND_EXPR_BODY (t));
16341
16342 if (BIND_EXPR_BODY_BLOCK (t))
16343 finish_function_body (stmt);
16344 else
16345 finish_compound_stmt (stmt);
16346 break;
16347
16348 case BREAK_STMT:
16349 finish_break_stmt ();
16350 break;
16351
16352 case CONTINUE_STMT:
16353 finish_continue_stmt ();
16354 break;
16355
16356 case SWITCH_STMT:
16357 stmt = begin_switch_stmt ();
16358 tmp = RECUR (SWITCH_STMT_COND (t));
16359 finish_switch_cond (tmp, stmt);
16360 RECUR (SWITCH_STMT_BODY (t));
16361 finish_switch_stmt (stmt);
16362 break;
16363
16364 case CASE_LABEL_EXPR:
16365 {
16366 tree low = RECUR (CASE_LOW (t));
16367 tree high = RECUR (CASE_HIGH (t));
16368 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16369 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16370 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16371 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16372 }
16373 break;
16374
16375 case LABEL_EXPR:
16376 {
16377 tree decl = LABEL_EXPR_LABEL (t);
16378 tree label;
16379
16380 label = finish_label_stmt (DECL_NAME (decl));
16381 if (TREE_CODE (label) == LABEL_DECL)
16382 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16383 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16384 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16385 }
16386 break;
16387
16388 case GOTO_EXPR:
16389 tmp = GOTO_DESTINATION (t);
16390 if (TREE_CODE (tmp) != LABEL_DECL)
16391 /* Computed goto's must be tsubst'd into. On the other hand,
16392 non-computed gotos must not be; the identifier in question
16393 will have no binding. */
16394 tmp = RECUR (tmp);
16395 else
16396 tmp = DECL_NAME (tmp);
16397 finish_goto_stmt (tmp);
16398 break;
16399
16400 case ASM_EXPR:
16401 {
16402 tree string = RECUR (ASM_STRING (t));
16403 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16404 complain, in_decl);
16405 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16406 complain, in_decl);
16407 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16408 complain, in_decl);
16409 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16410 complain, in_decl);
16411 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16412 clobbers, labels);
16413 tree asm_expr = tmp;
16414 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16415 asm_expr = TREE_OPERAND (asm_expr, 0);
16416 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16417 }
16418 break;
16419
16420 case TRY_BLOCK:
16421 if (CLEANUP_P (t))
16422 {
16423 stmt = begin_try_block ();
16424 RECUR (TRY_STMTS (t));
16425 finish_cleanup_try_block (stmt);
16426 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16427 }
16428 else
16429 {
16430 tree compound_stmt = NULL_TREE;
16431
16432 if (FN_TRY_BLOCK_P (t))
16433 stmt = begin_function_try_block (&compound_stmt);
16434 else
16435 stmt = begin_try_block ();
16436
16437 RECUR (TRY_STMTS (t));
16438
16439 if (FN_TRY_BLOCK_P (t))
16440 finish_function_try_block (stmt);
16441 else
16442 finish_try_block (stmt);
16443
16444 RECUR (TRY_HANDLERS (t));
16445 if (FN_TRY_BLOCK_P (t))
16446 finish_function_handler_sequence (stmt, compound_stmt);
16447 else
16448 finish_handler_sequence (stmt);
16449 }
16450 break;
16451
16452 case HANDLER:
16453 {
16454 tree decl = HANDLER_PARMS (t);
16455
16456 if (decl)
16457 {
16458 decl = tsubst (decl, args, complain, in_decl);
16459 /* Prevent instantiate_decl from trying to instantiate
16460 this variable. We've already done all that needs to be
16461 done. */
16462 if (decl != error_mark_node)
16463 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16464 }
16465 stmt = begin_handler ();
16466 finish_handler_parms (decl, stmt);
16467 RECUR (HANDLER_BODY (t));
16468 finish_handler (stmt);
16469 }
16470 break;
16471
16472 case TAG_DEFN:
16473 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16474 if (CLASS_TYPE_P (tmp))
16475 {
16476 /* Local classes are not independent templates; they are
16477 instantiated along with their containing function. And this
16478 way we don't have to deal with pushing out of one local class
16479 to instantiate a member of another local class. */
16480 /* Closures are handled by the LAMBDA_EXPR. */
16481 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16482 complete_type (tmp);
16483 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16484 if ((VAR_P (fld)
16485 || (TREE_CODE (fld) == FUNCTION_DECL
16486 && !DECL_ARTIFICIAL (fld)))
16487 && DECL_TEMPLATE_INSTANTIATION (fld))
16488 instantiate_decl (fld, /*defer_ok=*/false,
16489 /*expl_inst_class=*/false);
16490 }
16491 break;
16492
16493 case STATIC_ASSERT:
16494 {
16495 tree condition;
16496
16497 ++c_inhibit_evaluation_warnings;
16498 condition =
16499 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16500 args,
16501 complain, in_decl,
16502 /*integral_constant_expression_p=*/true);
16503 --c_inhibit_evaluation_warnings;
16504
16505 finish_static_assert (condition,
16506 STATIC_ASSERT_MESSAGE (t),
16507 STATIC_ASSERT_SOURCE_LOCATION (t),
16508 /*member_p=*/false);
16509 }
16510 break;
16511
16512 case OACC_KERNELS:
16513 case OACC_PARALLEL:
16514 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16515 in_decl);
16516 stmt = begin_omp_parallel ();
16517 RECUR (OMP_BODY (t));
16518 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16519 break;
16520
16521 case OMP_PARALLEL:
16522 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16523 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16524 complain, in_decl);
16525 if (OMP_PARALLEL_COMBINED (t))
16526 omp_parallel_combined_clauses = &tmp;
16527 stmt = begin_omp_parallel ();
16528 RECUR (OMP_PARALLEL_BODY (t));
16529 gcc_assert (omp_parallel_combined_clauses == NULL);
16530 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16531 = OMP_PARALLEL_COMBINED (t);
16532 pop_omp_privatization_clauses (r);
16533 break;
16534
16535 case OMP_TASK:
16536 r = push_omp_privatization_clauses (false);
16537 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16538 complain, in_decl);
16539 stmt = begin_omp_task ();
16540 RECUR (OMP_TASK_BODY (t));
16541 finish_omp_task (tmp, stmt);
16542 pop_omp_privatization_clauses (r);
16543 break;
16544
16545 case OMP_FOR:
16546 case OMP_SIMD:
16547 case OMP_DISTRIBUTE:
16548 case OMP_TASKLOOP:
16549 case OACC_LOOP:
16550 {
16551 tree clauses, body, pre_body;
16552 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16553 tree orig_declv = NULL_TREE;
16554 tree incrv = NULL_TREE;
16555 enum c_omp_region_type ort = C_ORT_OMP;
16556 int i;
16557
16558 if (TREE_CODE (t) == OACC_LOOP)
16559 ort = C_ORT_ACC;
16560
16561 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16562 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16563 in_decl);
16564 if (OMP_FOR_INIT (t) != NULL_TREE)
16565 {
16566 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16567 if (OMP_FOR_ORIG_DECLS (t))
16568 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16569 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16570 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16571 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16572 }
16573
16574 stmt = begin_omp_structured_block ();
16575
16576 pre_body = push_stmt_list ();
16577 RECUR (OMP_FOR_PRE_BODY (t));
16578 pre_body = pop_stmt_list (pre_body);
16579
16580 if (OMP_FOR_INIT (t) != NULL_TREE)
16581 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16582 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16583 incrv, &clauses, args, complain, in_decl,
16584 integral_constant_expression_p);
16585 omp_parallel_combined_clauses = NULL;
16586
16587 body = push_stmt_list ();
16588 RECUR (OMP_FOR_BODY (t));
16589 body = pop_stmt_list (body);
16590
16591 if (OMP_FOR_INIT (t) != NULL_TREE)
16592 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16593 orig_declv, initv, condv, incrv, body, pre_body,
16594 NULL, clauses);
16595 else
16596 {
16597 t = make_node (TREE_CODE (t));
16598 TREE_TYPE (t) = void_type_node;
16599 OMP_FOR_BODY (t) = body;
16600 OMP_FOR_PRE_BODY (t) = pre_body;
16601 OMP_FOR_CLAUSES (t) = clauses;
16602 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16603 add_stmt (t);
16604 }
16605
16606 add_stmt (finish_omp_structured_block (stmt));
16607 pop_omp_privatization_clauses (r);
16608 }
16609 break;
16610
16611 case OMP_SECTIONS:
16612 omp_parallel_combined_clauses = NULL;
16613 /* FALLTHRU */
16614 case OMP_SINGLE:
16615 case OMP_TEAMS:
16616 case OMP_CRITICAL:
16617 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16618 && OMP_TEAMS_COMBINED (t));
16619 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16620 in_decl);
16621 stmt = push_stmt_list ();
16622 RECUR (OMP_BODY (t));
16623 stmt = pop_stmt_list (stmt);
16624
16625 t = copy_node (t);
16626 OMP_BODY (t) = stmt;
16627 OMP_CLAUSES (t) = tmp;
16628 add_stmt (t);
16629 pop_omp_privatization_clauses (r);
16630 break;
16631
16632 case OACC_DATA:
16633 case OMP_TARGET_DATA:
16634 case OMP_TARGET:
16635 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16636 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16637 in_decl);
16638 keep_next_level (true);
16639 stmt = begin_omp_structured_block ();
16640
16641 RECUR (OMP_BODY (t));
16642 stmt = finish_omp_structured_block (stmt);
16643
16644 t = copy_node (t);
16645 OMP_BODY (t) = stmt;
16646 OMP_CLAUSES (t) = tmp;
16647 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16648 {
16649 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16650 if (teams)
16651 {
16652 /* For combined target teams, ensure the num_teams and
16653 thread_limit clause expressions are evaluated on the host,
16654 before entering the target construct. */
16655 tree c;
16656 for (c = OMP_TEAMS_CLAUSES (teams);
16657 c; c = OMP_CLAUSE_CHAIN (c))
16658 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16659 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16660 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16661 {
16662 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16663 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16664 if (expr == error_mark_node)
16665 continue;
16666 tmp = TARGET_EXPR_SLOT (expr);
16667 add_stmt (expr);
16668 OMP_CLAUSE_OPERAND (c, 0) = expr;
16669 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16670 OMP_CLAUSE_FIRSTPRIVATE);
16671 OMP_CLAUSE_DECL (tc) = tmp;
16672 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16673 OMP_TARGET_CLAUSES (t) = tc;
16674 }
16675 }
16676 }
16677 add_stmt (t);
16678 break;
16679
16680 case OACC_DECLARE:
16681 t = copy_node (t);
16682 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16683 complain, in_decl);
16684 OACC_DECLARE_CLAUSES (t) = tmp;
16685 add_stmt (t);
16686 break;
16687
16688 case OMP_TARGET_UPDATE:
16689 case OMP_TARGET_ENTER_DATA:
16690 case OMP_TARGET_EXIT_DATA:
16691 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16692 complain, in_decl);
16693 t = copy_node (t);
16694 OMP_STANDALONE_CLAUSES (t) = tmp;
16695 add_stmt (t);
16696 break;
16697
16698 case OACC_ENTER_DATA:
16699 case OACC_EXIT_DATA:
16700 case OACC_UPDATE:
16701 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16702 complain, in_decl);
16703 t = copy_node (t);
16704 OMP_STANDALONE_CLAUSES (t) = tmp;
16705 add_stmt (t);
16706 break;
16707
16708 case OMP_ORDERED:
16709 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16710 complain, in_decl);
16711 stmt = push_stmt_list ();
16712 RECUR (OMP_BODY (t));
16713 stmt = pop_stmt_list (stmt);
16714
16715 t = copy_node (t);
16716 OMP_BODY (t) = stmt;
16717 OMP_ORDERED_CLAUSES (t) = tmp;
16718 add_stmt (t);
16719 break;
16720
16721 case OMP_SECTION:
16722 case OMP_MASTER:
16723 case OMP_TASKGROUP:
16724 stmt = push_stmt_list ();
16725 RECUR (OMP_BODY (t));
16726 stmt = pop_stmt_list (stmt);
16727
16728 t = copy_node (t);
16729 OMP_BODY (t) = stmt;
16730 add_stmt (t);
16731 break;
16732
16733 case OMP_ATOMIC:
16734 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16735 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16736 {
16737 tree op1 = TREE_OPERAND (t, 1);
16738 tree rhs1 = NULL_TREE;
16739 tree lhs, rhs;
16740 if (TREE_CODE (op1) == COMPOUND_EXPR)
16741 {
16742 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16743 op1 = TREE_OPERAND (op1, 1);
16744 }
16745 lhs = RECUR (TREE_OPERAND (op1, 0));
16746 rhs = RECUR (TREE_OPERAND (op1, 1));
16747 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16748 NULL_TREE, NULL_TREE, rhs1,
16749 OMP_ATOMIC_SEQ_CST (t));
16750 }
16751 else
16752 {
16753 tree op1 = TREE_OPERAND (t, 1);
16754 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16755 tree rhs1 = NULL_TREE;
16756 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16757 enum tree_code opcode = NOP_EXPR;
16758 if (code == OMP_ATOMIC_READ)
16759 {
16760 v = RECUR (TREE_OPERAND (op1, 0));
16761 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16762 }
16763 else if (code == OMP_ATOMIC_CAPTURE_OLD
16764 || code == OMP_ATOMIC_CAPTURE_NEW)
16765 {
16766 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16767 v = RECUR (TREE_OPERAND (op1, 0));
16768 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16769 if (TREE_CODE (op11) == COMPOUND_EXPR)
16770 {
16771 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16772 op11 = TREE_OPERAND (op11, 1);
16773 }
16774 lhs = RECUR (TREE_OPERAND (op11, 0));
16775 rhs = RECUR (TREE_OPERAND (op11, 1));
16776 opcode = TREE_CODE (op11);
16777 if (opcode == MODIFY_EXPR)
16778 opcode = NOP_EXPR;
16779 }
16780 else
16781 {
16782 code = OMP_ATOMIC;
16783 lhs = RECUR (TREE_OPERAND (op1, 0));
16784 rhs = RECUR (TREE_OPERAND (op1, 1));
16785 }
16786 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16787 OMP_ATOMIC_SEQ_CST (t));
16788 }
16789 break;
16790
16791 case TRANSACTION_EXPR:
16792 {
16793 int flags = 0;
16794 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16795 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16796
16797 if (TRANSACTION_EXPR_IS_STMT (t))
16798 {
16799 tree body = TRANSACTION_EXPR_BODY (t);
16800 tree noex = NULL_TREE;
16801 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16802 {
16803 noex = MUST_NOT_THROW_COND (body);
16804 if (noex == NULL_TREE)
16805 noex = boolean_true_node;
16806 body = TREE_OPERAND (body, 0);
16807 }
16808 stmt = begin_transaction_stmt (input_location, NULL, flags);
16809 RECUR (body);
16810 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16811 }
16812 else
16813 {
16814 stmt = build_transaction_expr (EXPR_LOCATION (t),
16815 RECUR (TRANSACTION_EXPR_BODY (t)),
16816 flags, NULL_TREE);
16817 RETURN (stmt);
16818 }
16819 }
16820 break;
16821
16822 case MUST_NOT_THROW_EXPR:
16823 {
16824 tree op0 = RECUR (TREE_OPERAND (t, 0));
16825 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16826 RETURN (build_must_not_throw_expr (op0, cond));
16827 }
16828
16829 case EXPR_PACK_EXPANSION:
16830 error ("invalid use of pack expansion expression");
16831 RETURN (error_mark_node);
16832
16833 case NONTYPE_ARGUMENT_PACK:
16834 error ("use %<...%> to expand argument pack");
16835 RETURN (error_mark_node);
16836
16837 case COMPOUND_EXPR:
16838 tmp = RECUR (TREE_OPERAND (t, 0));
16839 if (tmp == NULL_TREE)
16840 /* If the first operand was a statement, we're done with it. */
16841 RETURN (RECUR (TREE_OPERAND (t, 1)));
16842 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16843 RECUR (TREE_OPERAND (t, 1)),
16844 complain));
16845
16846 case ANNOTATE_EXPR:
16847 tmp = RECUR (TREE_OPERAND (t, 0));
16848 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16849 TREE_TYPE (tmp), tmp,
16850 RECUR (TREE_OPERAND (t, 1)),
16851 RECUR (TREE_OPERAND (t, 2))));
16852
16853 default:
16854 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16855
16856 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16857 /*function_p=*/false,
16858 integral_constant_expression_p));
16859 }
16860
16861 RETURN (NULL_TREE);
16862 out:
16863 input_location = loc;
16864 return r;
16865 #undef RECUR
16866 #undef RETURN
16867 }
16868
16869 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16870 function. For description of the body see comment above
16871 cp_parser_omp_declare_reduction_exprs. */
16872
16873 static void
16874 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16875 {
16876 if (t == NULL_TREE || t == error_mark_node)
16877 return;
16878
16879 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16880
16881 tree_stmt_iterator tsi;
16882 int i;
16883 tree stmts[7];
16884 memset (stmts, 0, sizeof stmts);
16885 for (i = 0, tsi = tsi_start (t);
16886 i < 7 && !tsi_end_p (tsi);
16887 i++, tsi_next (&tsi))
16888 stmts[i] = tsi_stmt (tsi);
16889 gcc_assert (tsi_end_p (tsi));
16890
16891 if (i >= 3)
16892 {
16893 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16894 && TREE_CODE (stmts[1]) == DECL_EXPR);
16895 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16896 args, complain, in_decl);
16897 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16898 args, complain, in_decl);
16899 DECL_CONTEXT (omp_out) = current_function_decl;
16900 DECL_CONTEXT (omp_in) = current_function_decl;
16901 keep_next_level (true);
16902 tree block = begin_omp_structured_block ();
16903 tsubst_expr (stmts[2], args, complain, in_decl, false);
16904 block = finish_omp_structured_block (block);
16905 block = maybe_cleanup_point_expr_void (block);
16906 add_decl_expr (omp_out);
16907 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16908 TREE_NO_WARNING (omp_out) = 1;
16909 add_decl_expr (omp_in);
16910 finish_expr_stmt (block);
16911 }
16912 if (i >= 6)
16913 {
16914 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16915 && TREE_CODE (stmts[4]) == DECL_EXPR);
16916 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16917 args, complain, in_decl);
16918 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16919 args, complain, in_decl);
16920 DECL_CONTEXT (omp_priv) = current_function_decl;
16921 DECL_CONTEXT (omp_orig) = current_function_decl;
16922 keep_next_level (true);
16923 tree block = begin_omp_structured_block ();
16924 tsubst_expr (stmts[5], args, complain, in_decl, false);
16925 block = finish_omp_structured_block (block);
16926 block = maybe_cleanup_point_expr_void (block);
16927 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16928 add_decl_expr (omp_priv);
16929 add_decl_expr (omp_orig);
16930 finish_expr_stmt (block);
16931 if (i == 7)
16932 add_decl_expr (omp_orig);
16933 }
16934 }
16935
16936 /* T is a postfix-expression that is not being used in a function
16937 call. Return the substituted version of T. */
16938
16939 static tree
16940 tsubst_non_call_postfix_expression (tree t, tree args,
16941 tsubst_flags_t complain,
16942 tree in_decl)
16943 {
16944 if (TREE_CODE (t) == SCOPE_REF)
16945 t = tsubst_qualified_id (t, args, complain, in_decl,
16946 /*done=*/false, /*address_p=*/false);
16947 else
16948 t = tsubst_copy_and_build (t, args, complain, in_decl,
16949 /*function_p=*/false,
16950 /*integral_constant_expression_p=*/false);
16951
16952 return t;
16953 }
16954
16955 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16956 instantiation context. Instantiating a pack expansion containing a lambda
16957 might result in multiple lambdas all based on the same lambda in the
16958 template. */
16959
16960 tree
16961 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16962 {
16963 tree oldfn = lambda_function (t);
16964 in_decl = oldfn;
16965
16966 tree r = build_lambda_expr ();
16967
16968 LAMBDA_EXPR_LOCATION (r)
16969 = LAMBDA_EXPR_LOCATION (t);
16970 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16971 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16972 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16973
16974 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16975 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16976 else
16977 record_lambda_scope (r);
16978
16979 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16980 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16981
16982 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16983 cap = TREE_CHAIN (cap))
16984 {
16985 tree field = TREE_PURPOSE (cap);
16986 if (PACK_EXPANSION_P (field))
16987 field = PACK_EXPANSION_PATTERN (field);
16988 field = tsubst_decl (field, args, complain);
16989
16990 if (field == error_mark_node)
16991 return error_mark_node;
16992
16993 tree init = TREE_VALUE (cap);
16994 if (PACK_EXPANSION_P (init))
16995 init = tsubst_pack_expansion (init, args, complain, in_decl);
16996 else
16997 init = tsubst_copy_and_build (init, args, complain, in_decl,
16998 /*fn*/false, /*constexpr*/false);
16999
17000 if (TREE_CODE (field) == TREE_VEC)
17001 {
17002 int len = TREE_VEC_LENGTH (field);
17003 gcc_assert (TREE_CODE (init) == TREE_VEC
17004 && TREE_VEC_LENGTH (init) == len);
17005 for (int i = 0; i < len; ++i)
17006 LAMBDA_EXPR_CAPTURE_LIST (r)
17007 = tree_cons (TREE_VEC_ELT (field, i),
17008 TREE_VEC_ELT (init, i),
17009 LAMBDA_EXPR_CAPTURE_LIST (r));
17010 }
17011 else
17012 {
17013 LAMBDA_EXPR_CAPTURE_LIST (r)
17014 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17015
17016 if (id_equal (DECL_NAME (field), "__this"))
17017 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17018 }
17019 }
17020
17021 tree type = begin_lambda_type (r);
17022
17023 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17024 determine_visibility (TYPE_NAME (type));
17025
17026 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17027
17028 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17029 ? DECL_TI_TEMPLATE (oldfn)
17030 : NULL_TREE);
17031
17032 tree fntype = static_fn_type (oldfn);
17033 if (oldtmpl)
17034 ++processing_template_decl;
17035 fntype = tsubst (fntype, args, complain, in_decl);
17036 if (oldtmpl)
17037 --processing_template_decl;
17038
17039 if (fntype == error_mark_node)
17040 r = error_mark_node;
17041 else
17042 {
17043 /* Fix the type of 'this'. */
17044 fntype = build_memfn_type (fntype, type,
17045 type_memfn_quals (fntype),
17046 type_memfn_rqual (fntype));
17047 tree fn, tmpl;
17048 if (oldtmpl)
17049 {
17050 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17051 fn = DECL_TEMPLATE_RESULT (tmpl);
17052 finish_member_declaration (tmpl);
17053 }
17054 else
17055 {
17056 tmpl = NULL_TREE;
17057 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17058 finish_member_declaration (fn);
17059 }
17060
17061 /* Let finish_function set this. */
17062 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17063
17064 bool nested = cfun;
17065 if (nested)
17066 push_function_context ();
17067
17068 local_specialization_stack s (lss_copy);
17069
17070 tree body = start_lambda_function (fn, r);
17071
17072 register_parameter_specializations (oldfn, fn);
17073
17074 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17075 /*constexpr*/false);
17076
17077 finish_lambda_function (body);
17078
17079 if (nested)
17080 pop_function_context ();
17081
17082 /* The capture list was built up in reverse order; fix that now. */
17083 LAMBDA_EXPR_CAPTURE_LIST (r)
17084 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17085
17086 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17087
17088 maybe_add_lambda_conv_op (type);
17089 }
17090
17091 finish_struct (type, /*attr*/NULL_TREE);
17092
17093 insert_pending_capture_proxies ();
17094
17095 return r;
17096 }
17097
17098 /* Like tsubst but deals with expressions and performs semantic
17099 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17100
17101 tree
17102 tsubst_copy_and_build (tree t,
17103 tree args,
17104 tsubst_flags_t complain,
17105 tree in_decl,
17106 bool function_p,
17107 bool integral_constant_expression_p)
17108 {
17109 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17110 #define RECUR(NODE) \
17111 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17112 /*function_p=*/false, \
17113 integral_constant_expression_p)
17114
17115 tree retval, op1;
17116 location_t loc;
17117
17118 if (t == NULL_TREE || t == error_mark_node)
17119 return t;
17120
17121 loc = input_location;
17122 if (EXPR_HAS_LOCATION (t))
17123 input_location = EXPR_LOCATION (t);
17124
17125 /* N3276 decltype magic only applies to calls at the top level or on the
17126 right side of a comma. */
17127 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17128 complain &= ~tf_decltype;
17129
17130 switch (TREE_CODE (t))
17131 {
17132 case USING_DECL:
17133 t = DECL_NAME (t);
17134 /* Fall through. */
17135 case IDENTIFIER_NODE:
17136 {
17137 tree decl;
17138 cp_id_kind idk;
17139 bool non_integral_constant_expression_p;
17140 const char *error_msg;
17141
17142 if (IDENTIFIER_CONV_OP_P (t))
17143 {
17144 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17145 t = make_conv_op_name (new_type);
17146 }
17147
17148 /* Look up the name. */
17149 decl = lookup_name (t);
17150
17151 /* By convention, expressions use ERROR_MARK_NODE to indicate
17152 failure, not NULL_TREE. */
17153 if (decl == NULL_TREE)
17154 decl = error_mark_node;
17155
17156 decl = finish_id_expression (t, decl, NULL_TREE,
17157 &idk,
17158 integral_constant_expression_p,
17159 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17160 &non_integral_constant_expression_p,
17161 /*template_p=*/false,
17162 /*done=*/true,
17163 /*address_p=*/false,
17164 /*template_arg_p=*/false,
17165 &error_msg,
17166 input_location);
17167 if (error_msg)
17168 error (error_msg);
17169 if (!function_p && identifier_p (decl))
17170 {
17171 if (complain & tf_error)
17172 unqualified_name_lookup_error (decl);
17173 decl = error_mark_node;
17174 }
17175 RETURN (decl);
17176 }
17177
17178 case TEMPLATE_ID_EXPR:
17179 {
17180 tree object;
17181 tree templ = RECUR (TREE_OPERAND (t, 0));
17182 tree targs = TREE_OPERAND (t, 1);
17183
17184 if (targs)
17185 targs = tsubst_template_args (targs, args, complain, in_decl);
17186 if (targs == error_mark_node)
17187 return error_mark_node;
17188
17189 if (TREE_CODE (templ) == SCOPE_REF)
17190 {
17191 tree name = TREE_OPERAND (templ, 1);
17192 tree tid = lookup_template_function (name, targs);
17193 TREE_OPERAND (templ, 1) = tid;
17194 return templ;
17195 }
17196
17197 if (variable_template_p (templ))
17198 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17199
17200 if (TREE_CODE (templ) == COMPONENT_REF)
17201 {
17202 object = TREE_OPERAND (templ, 0);
17203 templ = TREE_OPERAND (templ, 1);
17204 }
17205 else
17206 object = NULL_TREE;
17207 templ = lookup_template_function (templ, targs);
17208
17209 if (object)
17210 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17211 object, templ, NULL_TREE));
17212 else
17213 RETURN (baselink_for_fns (templ));
17214 }
17215
17216 case INDIRECT_REF:
17217 {
17218 tree r = RECUR (TREE_OPERAND (t, 0));
17219
17220 if (REFERENCE_REF_P (t))
17221 {
17222 /* A type conversion to reference type will be enclosed in
17223 such an indirect ref, but the substitution of the cast
17224 will have also added such an indirect ref. */
17225 r = convert_from_reference (r);
17226 }
17227 else
17228 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17229 complain|decltype_flag);
17230
17231 if (REF_PARENTHESIZED_P (t))
17232 r = force_paren_expr (r);
17233
17234 RETURN (r);
17235 }
17236
17237 case NOP_EXPR:
17238 {
17239 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17240 tree op0 = RECUR (TREE_OPERAND (t, 0));
17241 RETURN (build_nop (type, op0));
17242 }
17243
17244 case IMPLICIT_CONV_EXPR:
17245 {
17246 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17247 tree expr = RECUR (TREE_OPERAND (t, 0));
17248 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17249 {
17250 retval = copy_node (t);
17251 TREE_TYPE (retval) = type;
17252 TREE_OPERAND (retval, 0) = expr;
17253 RETURN (retval);
17254 }
17255 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17256 /* We'll pass this to convert_nontype_argument again, we don't need
17257 to actually perform any conversion here. */
17258 RETURN (expr);
17259 int flags = LOOKUP_IMPLICIT;
17260 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17261 flags = LOOKUP_NORMAL;
17262 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17263 flags));
17264 }
17265
17266 case CONVERT_EXPR:
17267 {
17268 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17269 tree op0 = RECUR (TREE_OPERAND (t, 0));
17270 RETURN (build1 (CONVERT_EXPR, type, op0));
17271 }
17272
17273 case CAST_EXPR:
17274 case REINTERPRET_CAST_EXPR:
17275 case CONST_CAST_EXPR:
17276 case DYNAMIC_CAST_EXPR:
17277 case STATIC_CAST_EXPR:
17278 {
17279 tree type;
17280 tree op, r = NULL_TREE;
17281
17282 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17283 if (integral_constant_expression_p
17284 && !cast_valid_in_integral_constant_expression_p (type))
17285 {
17286 if (complain & tf_error)
17287 error ("a cast to a type other than an integral or "
17288 "enumeration type cannot appear in a constant-expression");
17289 RETURN (error_mark_node);
17290 }
17291
17292 op = RECUR (TREE_OPERAND (t, 0));
17293
17294 warning_sentinel s(warn_useless_cast);
17295 warning_sentinel s2(warn_ignored_qualifiers);
17296 switch (TREE_CODE (t))
17297 {
17298 case CAST_EXPR:
17299 r = build_functional_cast (type, op, complain);
17300 break;
17301 case REINTERPRET_CAST_EXPR:
17302 r = build_reinterpret_cast (type, op, complain);
17303 break;
17304 case CONST_CAST_EXPR:
17305 r = build_const_cast (type, op, complain);
17306 break;
17307 case DYNAMIC_CAST_EXPR:
17308 r = build_dynamic_cast (type, op, complain);
17309 break;
17310 case STATIC_CAST_EXPR:
17311 r = build_static_cast (type, op, complain);
17312 break;
17313 default:
17314 gcc_unreachable ();
17315 }
17316
17317 RETURN (r);
17318 }
17319
17320 case POSTDECREMENT_EXPR:
17321 case POSTINCREMENT_EXPR:
17322 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17323 args, complain, in_decl);
17324 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17325 complain|decltype_flag));
17326
17327 case PREDECREMENT_EXPR:
17328 case PREINCREMENT_EXPR:
17329 case NEGATE_EXPR:
17330 case BIT_NOT_EXPR:
17331 case ABS_EXPR:
17332 case TRUTH_NOT_EXPR:
17333 case UNARY_PLUS_EXPR: /* Unary + */
17334 case REALPART_EXPR:
17335 case IMAGPART_EXPR:
17336 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17337 RECUR (TREE_OPERAND (t, 0)),
17338 complain|decltype_flag));
17339
17340 case FIX_TRUNC_EXPR:
17341 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17342 false, complain));
17343
17344 case ADDR_EXPR:
17345 op1 = TREE_OPERAND (t, 0);
17346 if (TREE_CODE (op1) == LABEL_DECL)
17347 RETURN (finish_label_address_expr (DECL_NAME (op1),
17348 EXPR_LOCATION (op1)));
17349 if (TREE_CODE (op1) == SCOPE_REF)
17350 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17351 /*done=*/true, /*address_p=*/true);
17352 else
17353 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17354 in_decl);
17355 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17356 complain|decltype_flag));
17357
17358 case PLUS_EXPR:
17359 case MINUS_EXPR:
17360 case MULT_EXPR:
17361 case TRUNC_DIV_EXPR:
17362 case CEIL_DIV_EXPR:
17363 case FLOOR_DIV_EXPR:
17364 case ROUND_DIV_EXPR:
17365 case EXACT_DIV_EXPR:
17366 case BIT_AND_EXPR:
17367 case BIT_IOR_EXPR:
17368 case BIT_XOR_EXPR:
17369 case TRUNC_MOD_EXPR:
17370 case FLOOR_MOD_EXPR:
17371 case TRUTH_ANDIF_EXPR:
17372 case TRUTH_ORIF_EXPR:
17373 case TRUTH_AND_EXPR:
17374 case TRUTH_OR_EXPR:
17375 case RSHIFT_EXPR:
17376 case LSHIFT_EXPR:
17377 case RROTATE_EXPR:
17378 case LROTATE_EXPR:
17379 case EQ_EXPR:
17380 case NE_EXPR:
17381 case MAX_EXPR:
17382 case MIN_EXPR:
17383 case LE_EXPR:
17384 case GE_EXPR:
17385 case LT_EXPR:
17386 case GT_EXPR:
17387 case MEMBER_REF:
17388 case DOTSTAR_EXPR:
17389 {
17390 warning_sentinel s1(warn_type_limits);
17391 warning_sentinel s2(warn_div_by_zero);
17392 warning_sentinel s3(warn_logical_op);
17393 warning_sentinel s4(warn_tautological_compare);
17394 tree op0 = RECUR (TREE_OPERAND (t, 0));
17395 tree op1 = RECUR (TREE_OPERAND (t, 1));
17396 tree r = build_x_binary_op
17397 (input_location, TREE_CODE (t),
17398 op0,
17399 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17400 ? ERROR_MARK
17401 : TREE_CODE (TREE_OPERAND (t, 0))),
17402 op1,
17403 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17404 ? ERROR_MARK
17405 : TREE_CODE (TREE_OPERAND (t, 1))),
17406 /*overload=*/NULL,
17407 complain|decltype_flag);
17408 if (EXPR_P (r) && TREE_NO_WARNING (t))
17409 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17410
17411 RETURN (r);
17412 }
17413
17414 case POINTER_PLUS_EXPR:
17415 {
17416 tree op0 = RECUR (TREE_OPERAND (t, 0));
17417 tree op1 = RECUR (TREE_OPERAND (t, 1));
17418 return fold_build_pointer_plus (op0, op1);
17419 }
17420
17421 case SCOPE_REF:
17422 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17423 /*address_p=*/false));
17424 case ARRAY_REF:
17425 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17426 args, complain, in_decl);
17427 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17428 RECUR (TREE_OPERAND (t, 1)),
17429 complain|decltype_flag));
17430
17431 case SIZEOF_EXPR:
17432 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17433 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17434 RETURN (tsubst_copy (t, args, complain, in_decl));
17435 /* Fall through */
17436
17437 case ALIGNOF_EXPR:
17438 {
17439 tree r;
17440
17441 op1 = TREE_OPERAND (t, 0);
17442 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17443 op1 = TREE_TYPE (op1);
17444 if (!args)
17445 {
17446 /* When there are no ARGS, we are trying to evaluate a
17447 non-dependent expression from the parser. Trying to do
17448 the substitutions may not work. */
17449 if (!TYPE_P (op1))
17450 op1 = TREE_TYPE (op1);
17451 }
17452 else
17453 {
17454 ++cp_unevaluated_operand;
17455 ++c_inhibit_evaluation_warnings;
17456 if (TYPE_P (op1))
17457 op1 = tsubst (op1, args, complain, in_decl);
17458 else
17459 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17460 /*function_p=*/false,
17461 /*integral_constant_expression_p=*/
17462 false);
17463 --cp_unevaluated_operand;
17464 --c_inhibit_evaluation_warnings;
17465 }
17466 if (TYPE_P (op1))
17467 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17468 complain & tf_error);
17469 else
17470 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17471 complain & tf_error);
17472 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17473 {
17474 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17475 {
17476 if (!processing_template_decl && TYPE_P (op1))
17477 {
17478 r = build_min (SIZEOF_EXPR, size_type_node,
17479 build1 (NOP_EXPR, op1, error_mark_node));
17480 SIZEOF_EXPR_TYPE_P (r) = 1;
17481 }
17482 else
17483 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17484 TREE_SIDE_EFFECTS (r) = 0;
17485 TREE_READONLY (r) = 1;
17486 }
17487 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17488 }
17489 RETURN (r);
17490 }
17491
17492 case AT_ENCODE_EXPR:
17493 {
17494 op1 = TREE_OPERAND (t, 0);
17495 ++cp_unevaluated_operand;
17496 ++c_inhibit_evaluation_warnings;
17497 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17498 /*function_p=*/false,
17499 /*integral_constant_expression_p=*/false);
17500 --cp_unevaluated_operand;
17501 --c_inhibit_evaluation_warnings;
17502 RETURN (objc_build_encode_expr (op1));
17503 }
17504
17505 case NOEXCEPT_EXPR:
17506 op1 = TREE_OPERAND (t, 0);
17507 ++cp_unevaluated_operand;
17508 ++c_inhibit_evaluation_warnings;
17509 ++cp_noexcept_operand;
17510 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17511 /*function_p=*/false,
17512 /*integral_constant_expression_p=*/false);
17513 --cp_unevaluated_operand;
17514 --c_inhibit_evaluation_warnings;
17515 --cp_noexcept_operand;
17516 RETURN (finish_noexcept_expr (op1, complain));
17517
17518 case MODOP_EXPR:
17519 {
17520 warning_sentinel s(warn_div_by_zero);
17521 tree lhs = RECUR (TREE_OPERAND (t, 0));
17522 tree rhs = RECUR (TREE_OPERAND (t, 2));
17523 tree r = build_x_modify_expr
17524 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17525 complain|decltype_flag);
17526 /* TREE_NO_WARNING must be set if either the expression was
17527 parenthesized or it uses an operator such as >>= rather
17528 than plain assignment. In the former case, it was already
17529 set and must be copied. In the latter case,
17530 build_x_modify_expr sets it and it must not be reset
17531 here. */
17532 if (TREE_NO_WARNING (t))
17533 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17534
17535 RETURN (r);
17536 }
17537
17538 case ARROW_EXPR:
17539 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17540 args, complain, in_decl);
17541 /* Remember that there was a reference to this entity. */
17542 if (DECL_P (op1)
17543 && !mark_used (op1, complain) && !(complain & tf_error))
17544 RETURN (error_mark_node);
17545 RETURN (build_x_arrow (input_location, op1, complain));
17546
17547 case NEW_EXPR:
17548 {
17549 tree placement = RECUR (TREE_OPERAND (t, 0));
17550 tree init = RECUR (TREE_OPERAND (t, 3));
17551 vec<tree, va_gc> *placement_vec;
17552 vec<tree, va_gc> *init_vec;
17553 tree ret;
17554
17555 if (placement == NULL_TREE)
17556 placement_vec = NULL;
17557 else
17558 {
17559 placement_vec = make_tree_vector ();
17560 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17561 vec_safe_push (placement_vec, TREE_VALUE (placement));
17562 }
17563
17564 /* If there was an initializer in the original tree, but it
17565 instantiated to an empty list, then we should pass a
17566 non-NULL empty vector to tell build_new that it was an
17567 empty initializer() rather than no initializer. This can
17568 only happen when the initializer is a pack expansion whose
17569 parameter packs are of length zero. */
17570 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17571 init_vec = NULL;
17572 else
17573 {
17574 init_vec = make_tree_vector ();
17575 if (init == void_node)
17576 gcc_assert (init_vec != NULL);
17577 else
17578 {
17579 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17580 vec_safe_push (init_vec, TREE_VALUE (init));
17581 }
17582 }
17583
17584 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17585 tree op2 = RECUR (TREE_OPERAND (t, 2));
17586 ret = build_new (&placement_vec, op1, op2, &init_vec,
17587 NEW_EXPR_USE_GLOBAL (t),
17588 complain);
17589
17590 if (placement_vec != NULL)
17591 release_tree_vector (placement_vec);
17592 if (init_vec != NULL)
17593 release_tree_vector (init_vec);
17594
17595 RETURN (ret);
17596 }
17597
17598 case DELETE_EXPR:
17599 {
17600 tree op0 = RECUR (TREE_OPERAND (t, 0));
17601 tree op1 = RECUR (TREE_OPERAND (t, 1));
17602 RETURN (delete_sanity (op0, op1,
17603 DELETE_EXPR_USE_VEC (t),
17604 DELETE_EXPR_USE_GLOBAL (t),
17605 complain));
17606 }
17607
17608 case COMPOUND_EXPR:
17609 {
17610 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17611 complain & ~tf_decltype, in_decl,
17612 /*function_p=*/false,
17613 integral_constant_expression_p);
17614 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17615 op0,
17616 RECUR (TREE_OPERAND (t, 1)),
17617 complain|decltype_flag));
17618 }
17619
17620 case CALL_EXPR:
17621 {
17622 tree function;
17623 vec<tree, va_gc> *call_args;
17624 unsigned int nargs, i;
17625 bool qualified_p;
17626 bool koenig_p;
17627 tree ret;
17628
17629 function = CALL_EXPR_FN (t);
17630 /* Internal function with no arguments. */
17631 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17632 RETURN (t);
17633
17634 /* When we parsed the expression, we determined whether or
17635 not Koenig lookup should be performed. */
17636 koenig_p = KOENIG_LOOKUP_P (t);
17637 if (function == NULL_TREE)
17638 {
17639 koenig_p = false;
17640 qualified_p = false;
17641 }
17642 else if (TREE_CODE (function) == SCOPE_REF)
17643 {
17644 qualified_p = true;
17645 function = tsubst_qualified_id (function, args, complain, in_decl,
17646 /*done=*/false,
17647 /*address_p=*/false);
17648 }
17649 else if (koenig_p && identifier_p (function))
17650 {
17651 /* Do nothing; calling tsubst_copy_and_build on an identifier
17652 would incorrectly perform unqualified lookup again.
17653
17654 Note that we can also have an IDENTIFIER_NODE if the earlier
17655 unqualified lookup found a member function; in that case
17656 koenig_p will be false and we do want to do the lookup
17657 again to find the instantiated member function.
17658
17659 FIXME but doing that causes c++/15272, so we need to stop
17660 using IDENTIFIER_NODE in that situation. */
17661 qualified_p = false;
17662 }
17663 else
17664 {
17665 if (TREE_CODE (function) == COMPONENT_REF)
17666 {
17667 tree op = TREE_OPERAND (function, 1);
17668
17669 qualified_p = (TREE_CODE (op) == SCOPE_REF
17670 || (BASELINK_P (op)
17671 && BASELINK_QUALIFIED_P (op)));
17672 }
17673 else
17674 qualified_p = false;
17675
17676 if (TREE_CODE (function) == ADDR_EXPR
17677 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17678 /* Avoid error about taking the address of a constructor. */
17679 function = TREE_OPERAND (function, 0);
17680
17681 function = tsubst_copy_and_build (function, args, complain,
17682 in_decl,
17683 !qualified_p,
17684 integral_constant_expression_p);
17685
17686 if (BASELINK_P (function))
17687 qualified_p = true;
17688 }
17689
17690 nargs = call_expr_nargs (t);
17691 call_args = make_tree_vector ();
17692 for (i = 0; i < nargs; ++i)
17693 {
17694 tree arg = CALL_EXPR_ARG (t, i);
17695
17696 if (!PACK_EXPANSION_P (arg))
17697 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17698 else
17699 {
17700 /* Expand the pack expansion and push each entry onto
17701 CALL_ARGS. */
17702 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17703 if (TREE_CODE (arg) == TREE_VEC)
17704 {
17705 unsigned int len, j;
17706
17707 len = TREE_VEC_LENGTH (arg);
17708 for (j = 0; j < len; ++j)
17709 {
17710 tree value = TREE_VEC_ELT (arg, j);
17711 if (value != NULL_TREE)
17712 value = convert_from_reference (value);
17713 vec_safe_push (call_args, value);
17714 }
17715 }
17716 else
17717 {
17718 /* A partial substitution. Add one entry. */
17719 vec_safe_push (call_args, arg);
17720 }
17721 }
17722 }
17723
17724 /* We do not perform argument-dependent lookup if normal
17725 lookup finds a non-function, in accordance with the
17726 expected resolution of DR 218. */
17727 if (koenig_p
17728 && ((is_overloaded_fn (function)
17729 /* If lookup found a member function, the Koenig lookup is
17730 not appropriate, even if an unqualified-name was used
17731 to denote the function. */
17732 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17733 || identifier_p (function))
17734 /* Only do this when substitution turns a dependent call
17735 into a non-dependent call. */
17736 && type_dependent_expression_p_push (t)
17737 && !any_type_dependent_arguments_p (call_args))
17738 function = perform_koenig_lookup (function, call_args, tf_none);
17739
17740 if (function != NULL_TREE
17741 && identifier_p (function)
17742 && !any_type_dependent_arguments_p (call_args))
17743 {
17744 if (koenig_p && (complain & tf_warning_or_error))
17745 {
17746 /* For backwards compatibility and good diagnostics, try
17747 the unqualified lookup again if we aren't in SFINAE
17748 context. */
17749 tree unq = (tsubst_copy_and_build
17750 (function, args, complain, in_decl, true,
17751 integral_constant_expression_p));
17752 if (unq == error_mark_node)
17753 {
17754 release_tree_vector (call_args);
17755 RETURN (error_mark_node);
17756 }
17757
17758 if (unq != function)
17759 {
17760 /* In a lambda fn, we have to be careful to not
17761 introduce new this captures. Legacy code can't
17762 be using lambdas anyway, so it's ok to be
17763 stricter. */
17764 bool in_lambda = (current_class_type
17765 && LAMBDA_TYPE_P (current_class_type));
17766 char const *const msg
17767 = G_("%qD was not declared in this scope, "
17768 "and no declarations were found by "
17769 "argument-dependent lookup at the point "
17770 "of instantiation");
17771
17772 bool diag = true;
17773 if (in_lambda)
17774 error_at (EXPR_LOC_OR_LOC (t, input_location),
17775 msg, function);
17776 else
17777 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17778 msg, function);
17779 if (diag)
17780 {
17781 tree fn = unq;
17782
17783 if (INDIRECT_REF_P (fn))
17784 fn = TREE_OPERAND (fn, 0);
17785 if (is_overloaded_fn (fn))
17786 fn = get_first_fn (fn);
17787
17788 if (!DECL_P (fn))
17789 /* Can't say anything more. */;
17790 else if (DECL_CLASS_SCOPE_P (fn))
17791 {
17792 location_t loc = EXPR_LOC_OR_LOC (t,
17793 input_location);
17794 inform (loc,
17795 "declarations in dependent base %qT are "
17796 "not found by unqualified lookup",
17797 DECL_CLASS_CONTEXT (fn));
17798 if (current_class_ptr)
17799 inform (loc,
17800 "use %<this->%D%> instead", function);
17801 else
17802 inform (loc,
17803 "use %<%T::%D%> instead",
17804 current_class_name, function);
17805 }
17806 else
17807 inform (DECL_SOURCE_LOCATION (fn),
17808 "%qD declared here, later in the "
17809 "translation unit", fn);
17810 if (in_lambda)
17811 {
17812 release_tree_vector (call_args);
17813 RETURN (error_mark_node);
17814 }
17815 }
17816
17817 function = unq;
17818 }
17819 }
17820 if (identifier_p (function))
17821 {
17822 if (complain & tf_error)
17823 unqualified_name_lookup_error (function);
17824 release_tree_vector (call_args);
17825 RETURN (error_mark_node);
17826 }
17827 }
17828
17829 /* Remember that there was a reference to this entity. */
17830 if (function != NULL_TREE
17831 && DECL_P (function)
17832 && !mark_used (function, complain) && !(complain & tf_error))
17833 {
17834 release_tree_vector (call_args);
17835 RETURN (error_mark_node);
17836 }
17837
17838 /* Put back tf_decltype for the actual call. */
17839 complain |= decltype_flag;
17840
17841 if (function == NULL_TREE)
17842 switch (CALL_EXPR_IFN (t))
17843 {
17844 case IFN_LAUNDER:
17845 gcc_assert (nargs == 1);
17846 if (vec_safe_length (call_args) != 1)
17847 {
17848 error_at (EXPR_LOC_OR_LOC (t, input_location),
17849 "wrong number of arguments to "
17850 "%<__builtin_launder%>");
17851 ret = error_mark_node;
17852 }
17853 else
17854 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17855 input_location),
17856 (*call_args)[0], complain);
17857 break;
17858
17859 default:
17860 /* Unsupported internal function with arguments. */
17861 gcc_unreachable ();
17862 }
17863 else if (TREE_CODE (function) == OFFSET_REF)
17864 ret = build_offset_ref_call_from_tree (function, &call_args,
17865 complain);
17866 else if (TREE_CODE (function) == COMPONENT_REF)
17867 {
17868 tree instance = TREE_OPERAND (function, 0);
17869 tree fn = TREE_OPERAND (function, 1);
17870
17871 if (processing_template_decl
17872 && (type_dependent_expression_p (instance)
17873 || (!BASELINK_P (fn)
17874 && TREE_CODE (fn) != FIELD_DECL)
17875 || type_dependent_expression_p (fn)
17876 || any_type_dependent_arguments_p (call_args)))
17877 ret = build_min_nt_call_vec (function, call_args);
17878 else if (!BASELINK_P (fn))
17879 ret = finish_call_expr (function, &call_args,
17880 /*disallow_virtual=*/false,
17881 /*koenig_p=*/false,
17882 complain);
17883 else
17884 ret = (build_new_method_call
17885 (instance, fn,
17886 &call_args, NULL_TREE,
17887 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17888 /*fn_p=*/NULL,
17889 complain));
17890 }
17891 else
17892 ret = finish_call_expr (function, &call_args,
17893 /*disallow_virtual=*/qualified_p,
17894 koenig_p,
17895 complain);
17896
17897 release_tree_vector (call_args);
17898
17899 if (ret != error_mark_node)
17900 {
17901 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17902 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17903 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17904 bool thk = CALL_FROM_THUNK_P (t);
17905 if (op || ord || rev || thk)
17906 {
17907 function = extract_call_expr (ret);
17908 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17909 CALL_EXPR_ORDERED_ARGS (function) = ord;
17910 CALL_EXPR_REVERSE_ARGS (function) = rev;
17911 if (thk)
17912 {
17913 if (TREE_CODE (function) == CALL_EXPR)
17914 CALL_FROM_THUNK_P (function) = true;
17915 else
17916 AGGR_INIT_FROM_THUNK_P (function) = true;
17917 /* The thunk location is not interesting. */
17918 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17919 }
17920 }
17921 }
17922
17923 RETURN (ret);
17924 }
17925
17926 case COND_EXPR:
17927 {
17928 tree cond = RECUR (TREE_OPERAND (t, 0));
17929 tree folded_cond = fold_non_dependent_expr (cond);
17930 tree exp1, exp2;
17931
17932 if (TREE_CODE (folded_cond) == INTEGER_CST)
17933 {
17934 if (integer_zerop (folded_cond))
17935 {
17936 ++c_inhibit_evaluation_warnings;
17937 exp1 = RECUR (TREE_OPERAND (t, 1));
17938 --c_inhibit_evaluation_warnings;
17939 exp2 = RECUR (TREE_OPERAND (t, 2));
17940 }
17941 else
17942 {
17943 exp1 = RECUR (TREE_OPERAND (t, 1));
17944 ++c_inhibit_evaluation_warnings;
17945 exp2 = RECUR (TREE_OPERAND (t, 2));
17946 --c_inhibit_evaluation_warnings;
17947 }
17948 cond = folded_cond;
17949 }
17950 else
17951 {
17952 exp1 = RECUR (TREE_OPERAND (t, 1));
17953 exp2 = RECUR (TREE_OPERAND (t, 2));
17954 }
17955
17956 warning_sentinel s(warn_duplicated_branches);
17957 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17958 cond, exp1, exp2, complain));
17959 }
17960
17961 case PSEUDO_DTOR_EXPR:
17962 {
17963 tree op0 = RECUR (TREE_OPERAND (t, 0));
17964 tree op1 = RECUR (TREE_OPERAND (t, 1));
17965 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17966 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17967 input_location));
17968 }
17969
17970 case TREE_LIST:
17971 {
17972 tree purpose, value, chain;
17973
17974 if (t == void_list_node)
17975 RETURN (t);
17976
17977 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17978 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17979 {
17980 /* We have pack expansions, so expand those and
17981 create a new list out of it. */
17982 tree purposevec = NULL_TREE;
17983 tree valuevec = NULL_TREE;
17984 tree chain;
17985 int i, len = -1;
17986
17987 /* Expand the argument expressions. */
17988 if (TREE_PURPOSE (t))
17989 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17990 complain, in_decl);
17991 if (TREE_VALUE (t))
17992 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17993 complain, in_decl);
17994
17995 /* Build the rest of the list. */
17996 chain = TREE_CHAIN (t);
17997 if (chain && chain != void_type_node)
17998 chain = RECUR (chain);
17999
18000 /* Determine the number of arguments. */
18001 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18002 {
18003 len = TREE_VEC_LENGTH (purposevec);
18004 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18005 }
18006 else if (TREE_CODE (valuevec) == TREE_VEC)
18007 len = TREE_VEC_LENGTH (valuevec);
18008 else
18009 {
18010 /* Since we only performed a partial substitution into
18011 the argument pack, we only RETURN (a single list
18012 node. */
18013 if (purposevec == TREE_PURPOSE (t)
18014 && valuevec == TREE_VALUE (t)
18015 && chain == TREE_CHAIN (t))
18016 RETURN (t);
18017
18018 RETURN (tree_cons (purposevec, valuevec, chain));
18019 }
18020
18021 /* Convert the argument vectors into a TREE_LIST */
18022 i = len;
18023 while (i > 0)
18024 {
18025 /* Grab the Ith values. */
18026 i--;
18027 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18028 : NULL_TREE;
18029 value
18030 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18031 : NULL_TREE;
18032
18033 /* Build the list (backwards). */
18034 chain = tree_cons (purpose, value, chain);
18035 }
18036
18037 RETURN (chain);
18038 }
18039
18040 purpose = TREE_PURPOSE (t);
18041 if (purpose)
18042 purpose = RECUR (purpose);
18043 value = TREE_VALUE (t);
18044 if (value)
18045 value = RECUR (value);
18046 chain = TREE_CHAIN (t);
18047 if (chain && chain != void_type_node)
18048 chain = RECUR (chain);
18049 if (purpose == TREE_PURPOSE (t)
18050 && value == TREE_VALUE (t)
18051 && chain == TREE_CHAIN (t))
18052 RETURN (t);
18053 RETURN (tree_cons (purpose, value, chain));
18054 }
18055
18056 case COMPONENT_REF:
18057 {
18058 tree object;
18059 tree object_type;
18060 tree member;
18061 tree r;
18062
18063 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18064 args, complain, in_decl);
18065 /* Remember that there was a reference to this entity. */
18066 if (DECL_P (object)
18067 && !mark_used (object, complain) && !(complain & tf_error))
18068 RETURN (error_mark_node);
18069 object_type = TREE_TYPE (object);
18070
18071 member = TREE_OPERAND (t, 1);
18072 if (BASELINK_P (member))
18073 member = tsubst_baselink (member,
18074 non_reference (TREE_TYPE (object)),
18075 args, complain, in_decl);
18076 else
18077 member = tsubst_copy (member, args, complain, in_decl);
18078 if (member == error_mark_node)
18079 RETURN (error_mark_node);
18080
18081 if (TREE_CODE (member) == FIELD_DECL)
18082 {
18083 r = finish_non_static_data_member (member, object, NULL_TREE);
18084 if (TREE_CODE (r) == COMPONENT_REF)
18085 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18086 RETURN (r);
18087 }
18088 else if (type_dependent_expression_p (object))
18089 /* We can't do much here. */;
18090 else if (!CLASS_TYPE_P (object_type))
18091 {
18092 if (scalarish_type_p (object_type))
18093 {
18094 tree s = NULL_TREE;
18095 tree dtor = member;
18096
18097 if (TREE_CODE (dtor) == SCOPE_REF)
18098 {
18099 s = TREE_OPERAND (dtor, 0);
18100 dtor = TREE_OPERAND (dtor, 1);
18101 }
18102 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18103 {
18104 dtor = TREE_OPERAND (dtor, 0);
18105 if (TYPE_P (dtor))
18106 RETURN (finish_pseudo_destructor_expr
18107 (object, s, dtor, input_location));
18108 }
18109 }
18110 }
18111 else if (TREE_CODE (member) == SCOPE_REF
18112 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18113 {
18114 /* Lookup the template functions now that we know what the
18115 scope is. */
18116 tree scope = TREE_OPERAND (member, 0);
18117 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18118 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18119 member = lookup_qualified_name (scope, tmpl,
18120 /*is_type_p=*/false,
18121 /*complain=*/false);
18122 if (BASELINK_P (member))
18123 {
18124 BASELINK_FUNCTIONS (member)
18125 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18126 args);
18127 member = (adjust_result_of_qualified_name_lookup
18128 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18129 object_type));
18130 }
18131 else
18132 {
18133 qualified_name_lookup_error (scope, tmpl, member,
18134 input_location);
18135 RETURN (error_mark_node);
18136 }
18137 }
18138 else if (TREE_CODE (member) == SCOPE_REF
18139 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18140 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18141 {
18142 if (complain & tf_error)
18143 {
18144 if (TYPE_P (TREE_OPERAND (member, 0)))
18145 error ("%qT is not a class or namespace",
18146 TREE_OPERAND (member, 0));
18147 else
18148 error ("%qD is not a class or namespace",
18149 TREE_OPERAND (member, 0));
18150 }
18151 RETURN (error_mark_node);
18152 }
18153
18154 r = finish_class_member_access_expr (object, member,
18155 /*template_p=*/false,
18156 complain);
18157 if (TREE_CODE (r) == COMPONENT_REF)
18158 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18159 RETURN (r);
18160 }
18161
18162 case THROW_EXPR:
18163 RETURN (build_throw
18164 (RECUR (TREE_OPERAND (t, 0))));
18165
18166 case CONSTRUCTOR:
18167 {
18168 vec<constructor_elt, va_gc> *n;
18169 constructor_elt *ce;
18170 unsigned HOST_WIDE_INT idx;
18171 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18172 bool process_index_p;
18173 int newlen;
18174 bool need_copy_p = false;
18175 tree r;
18176
18177 if (type == error_mark_node)
18178 RETURN (error_mark_node);
18179
18180 /* We do not want to process the index of aggregate
18181 initializers as they are identifier nodes which will be
18182 looked up by digest_init. */
18183 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18184
18185 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18186 newlen = vec_safe_length (n);
18187 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18188 {
18189 if (ce->index && process_index_p
18190 /* An identifier index is looked up in the type
18191 being initialized, not the current scope. */
18192 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18193 ce->index = RECUR (ce->index);
18194
18195 if (PACK_EXPANSION_P (ce->value))
18196 {
18197 /* Substitute into the pack expansion. */
18198 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18199 in_decl);
18200
18201 if (ce->value == error_mark_node
18202 || PACK_EXPANSION_P (ce->value))
18203 ;
18204 else if (TREE_VEC_LENGTH (ce->value) == 1)
18205 /* Just move the argument into place. */
18206 ce->value = TREE_VEC_ELT (ce->value, 0);
18207 else
18208 {
18209 /* Update the length of the final CONSTRUCTOR
18210 arguments vector, and note that we will need to
18211 copy.*/
18212 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18213 need_copy_p = true;
18214 }
18215 }
18216 else
18217 ce->value = RECUR (ce->value);
18218 }
18219
18220 if (need_copy_p)
18221 {
18222 vec<constructor_elt, va_gc> *old_n = n;
18223
18224 vec_alloc (n, newlen);
18225 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18226 {
18227 if (TREE_CODE (ce->value) == TREE_VEC)
18228 {
18229 int i, len = TREE_VEC_LENGTH (ce->value);
18230 for (i = 0; i < len; ++i)
18231 CONSTRUCTOR_APPEND_ELT (n, 0,
18232 TREE_VEC_ELT (ce->value, i));
18233 }
18234 else
18235 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18236 }
18237 }
18238
18239 r = build_constructor (init_list_type_node, n);
18240 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18241
18242 if (TREE_HAS_CONSTRUCTOR (t))
18243 {
18244 fcl_t cl = fcl_functional;
18245 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18246 cl = fcl_c99;
18247 RETURN (finish_compound_literal (type, r, complain, cl));
18248 }
18249
18250 TREE_TYPE (r) = type;
18251 RETURN (r);
18252 }
18253
18254 case TYPEID_EXPR:
18255 {
18256 tree operand_0 = TREE_OPERAND (t, 0);
18257 if (TYPE_P (operand_0))
18258 {
18259 operand_0 = tsubst (operand_0, args, complain, in_decl);
18260 RETURN (get_typeid (operand_0, complain));
18261 }
18262 else
18263 {
18264 operand_0 = RECUR (operand_0);
18265 RETURN (build_typeid (operand_0, complain));
18266 }
18267 }
18268
18269 case VAR_DECL:
18270 if (!args)
18271 RETURN (t);
18272 /* Fall through */
18273
18274 case PARM_DECL:
18275 {
18276 tree r = tsubst_copy (t, args, complain, in_decl);
18277 /* ??? We're doing a subset of finish_id_expression here. */
18278 if (VAR_P (r)
18279 && !processing_template_decl
18280 && !cp_unevaluated_operand
18281 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18282 && CP_DECL_THREAD_LOCAL_P (r))
18283 {
18284 if (tree wrap = get_tls_wrapper_fn (r))
18285 /* Replace an evaluated use of the thread_local variable with
18286 a call to its wrapper. */
18287 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18288 }
18289 else if (outer_automatic_var_p (r))
18290 r = process_outer_var_ref (r, complain);
18291
18292 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18293 /* If the original type was a reference, we'll be wrapped in
18294 the appropriate INDIRECT_REF. */
18295 r = convert_from_reference (r);
18296 RETURN (r);
18297 }
18298
18299 case VA_ARG_EXPR:
18300 {
18301 tree op0 = RECUR (TREE_OPERAND (t, 0));
18302 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18303 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18304 }
18305
18306 case OFFSETOF_EXPR:
18307 {
18308 tree object_ptr
18309 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18310 in_decl, /*function_p=*/false,
18311 /*integral_constant_expression_p=*/false);
18312 RETURN (finish_offsetof (object_ptr,
18313 RECUR (TREE_OPERAND (t, 0)),
18314 EXPR_LOCATION (t)));
18315 }
18316
18317 case ADDRESSOF_EXPR:
18318 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18319 RECUR (TREE_OPERAND (t, 0)), complain));
18320
18321 case TRAIT_EXPR:
18322 {
18323 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18324 complain, in_decl);
18325
18326 tree type2 = TRAIT_EXPR_TYPE2 (t);
18327 if (type2 && TREE_CODE (type2) == TREE_LIST)
18328 type2 = RECUR (type2);
18329 else if (type2)
18330 type2 = tsubst (type2, args, complain, in_decl);
18331
18332 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18333 }
18334
18335 case STMT_EXPR:
18336 {
18337 tree old_stmt_expr = cur_stmt_expr;
18338 tree stmt_expr = begin_stmt_expr ();
18339
18340 cur_stmt_expr = stmt_expr;
18341 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18342 integral_constant_expression_p);
18343 stmt_expr = finish_stmt_expr (stmt_expr, false);
18344 cur_stmt_expr = old_stmt_expr;
18345
18346 /* If the resulting list of expression statement is empty,
18347 fold it further into void_node. */
18348 if (empty_expr_stmt_p (stmt_expr))
18349 stmt_expr = void_node;
18350
18351 RETURN (stmt_expr);
18352 }
18353
18354 case LAMBDA_EXPR:
18355 {
18356 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18357
18358 RETURN (build_lambda_object (r));
18359 }
18360
18361 case TARGET_EXPR:
18362 /* We can get here for a constant initializer of non-dependent type.
18363 FIXME stop folding in cp_parser_initializer_clause. */
18364 {
18365 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18366 complain);
18367 RETURN (r);
18368 }
18369
18370 case TRANSACTION_EXPR:
18371 RETURN (tsubst_expr(t, args, complain, in_decl,
18372 integral_constant_expression_p));
18373
18374 case PAREN_EXPR:
18375 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18376
18377 case VEC_PERM_EXPR:
18378 {
18379 tree op0 = RECUR (TREE_OPERAND (t, 0));
18380 tree op1 = RECUR (TREE_OPERAND (t, 1));
18381 tree op2 = RECUR (TREE_OPERAND (t, 2));
18382 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18383 complain));
18384 }
18385
18386 case REQUIRES_EXPR:
18387 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18388
18389 case NON_LVALUE_EXPR:
18390 case VIEW_CONVERT_EXPR:
18391 /* We should only see these for location wrapper nodes, or within
18392 instantiate_non_dependent_expr (when args is NULL_TREE). */
18393 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18394 if (location_wrapper_p (t))
18395 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18396 EXPR_LOCATION (t)));
18397 /* fallthrough. */
18398
18399 default:
18400 /* Handle Objective-C++ constructs, if appropriate. */
18401 {
18402 tree subst
18403 = objcp_tsubst_copy_and_build (t, args, complain,
18404 in_decl, /*function_p=*/false);
18405 if (subst)
18406 RETURN (subst);
18407 }
18408 RETURN (tsubst_copy (t, args, complain, in_decl));
18409 }
18410
18411 #undef RECUR
18412 #undef RETURN
18413 out:
18414 input_location = loc;
18415 return retval;
18416 }
18417
18418 /* Verify that the instantiated ARGS are valid. For type arguments,
18419 make sure that the type's linkage is ok. For non-type arguments,
18420 make sure they are constants if they are integral or enumerations.
18421 Emit an error under control of COMPLAIN, and return TRUE on error. */
18422
18423 static bool
18424 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18425 {
18426 if (dependent_template_arg_p (t))
18427 return false;
18428 if (ARGUMENT_PACK_P (t))
18429 {
18430 tree vec = ARGUMENT_PACK_ARGS (t);
18431 int len = TREE_VEC_LENGTH (vec);
18432 bool result = false;
18433 int i;
18434
18435 for (i = 0; i < len; ++i)
18436 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18437 result = true;
18438 return result;
18439 }
18440 else if (TYPE_P (t))
18441 {
18442 /* [basic.link]: A name with no linkage (notably, the name
18443 of a class or enumeration declared in a local scope)
18444 shall not be used to declare an entity with linkage.
18445 This implies that names with no linkage cannot be used as
18446 template arguments
18447
18448 DR 757 relaxes this restriction for C++0x. */
18449 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18450 : no_linkage_check (t, /*relaxed_p=*/false));
18451
18452 if (nt)
18453 {
18454 /* DR 488 makes use of a type with no linkage cause
18455 type deduction to fail. */
18456 if (complain & tf_error)
18457 {
18458 if (TYPE_UNNAMED_P (nt))
18459 error ("%qT is/uses unnamed type", t);
18460 else
18461 error ("template argument for %qD uses local type %qT",
18462 tmpl, t);
18463 }
18464 return true;
18465 }
18466 /* In order to avoid all sorts of complications, we do not
18467 allow variably-modified types as template arguments. */
18468 else if (variably_modified_type_p (t, NULL_TREE))
18469 {
18470 if (complain & tf_error)
18471 error ("%qT is a variably modified type", t);
18472 return true;
18473 }
18474 }
18475 /* Class template and alias template arguments should be OK. */
18476 else if (DECL_TYPE_TEMPLATE_P (t))
18477 ;
18478 /* A non-type argument of integral or enumerated type must be a
18479 constant. */
18480 else if (TREE_TYPE (t)
18481 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18482 && !REFERENCE_REF_P (t)
18483 && !TREE_CONSTANT (t))
18484 {
18485 if (complain & tf_error)
18486 error ("integral expression %qE is not constant", t);
18487 return true;
18488 }
18489 return false;
18490 }
18491
18492 static bool
18493 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18494 {
18495 int ix, len = DECL_NTPARMS (tmpl);
18496 bool result = false;
18497
18498 for (ix = 0; ix != len; ix++)
18499 {
18500 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18501 result = true;
18502 }
18503 if (result && (complain & tf_error))
18504 error (" trying to instantiate %qD", tmpl);
18505 return result;
18506 }
18507
18508 /* We're out of SFINAE context now, so generate diagnostics for the access
18509 errors we saw earlier when instantiating D from TMPL and ARGS. */
18510
18511 static void
18512 recheck_decl_substitution (tree d, tree tmpl, tree args)
18513 {
18514 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18515 tree type = TREE_TYPE (pattern);
18516 location_t loc = input_location;
18517
18518 push_access_scope (d);
18519 push_deferring_access_checks (dk_no_deferred);
18520 input_location = DECL_SOURCE_LOCATION (pattern);
18521 tsubst (type, args, tf_warning_or_error, d);
18522 input_location = loc;
18523 pop_deferring_access_checks ();
18524 pop_access_scope (d);
18525 }
18526
18527 /* Instantiate the indicated variable, function, or alias template TMPL with
18528 the template arguments in TARG_PTR. */
18529
18530 static tree
18531 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18532 {
18533 tree targ_ptr = orig_args;
18534 tree fndecl;
18535 tree gen_tmpl;
18536 tree spec;
18537 bool access_ok = true;
18538
18539 if (tmpl == error_mark_node)
18540 return error_mark_node;
18541
18542 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18543
18544 /* If this function is a clone, handle it specially. */
18545 if (DECL_CLONED_FUNCTION_P (tmpl))
18546 {
18547 tree spec;
18548 tree clone;
18549
18550 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18551 DECL_CLONED_FUNCTION. */
18552 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18553 targ_ptr, complain);
18554 if (spec == error_mark_node)
18555 return error_mark_node;
18556
18557 /* Look for the clone. */
18558 FOR_EACH_CLONE (clone, spec)
18559 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18560 return clone;
18561 /* We should always have found the clone by now. */
18562 gcc_unreachable ();
18563 return NULL_TREE;
18564 }
18565
18566 if (targ_ptr == error_mark_node)
18567 return error_mark_node;
18568
18569 /* Check to see if we already have this specialization. */
18570 gen_tmpl = most_general_template (tmpl);
18571 if (TMPL_ARGS_DEPTH (targ_ptr)
18572 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18573 /* targ_ptr only has the innermost template args, so add the outer ones
18574 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18575 the case of a non-dependent call within a template definition). */
18576 targ_ptr = (add_outermost_template_args
18577 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18578 targ_ptr));
18579
18580 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18581 but it doesn't seem to be on the hot path. */
18582 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18583
18584 gcc_assert (tmpl == gen_tmpl
18585 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18586 == spec)
18587 || fndecl == NULL_TREE);
18588
18589 if (spec != NULL_TREE)
18590 {
18591 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18592 {
18593 if (complain & tf_error)
18594 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18595 return error_mark_node;
18596 }
18597 return spec;
18598 }
18599
18600 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18601 complain))
18602 return error_mark_node;
18603
18604 /* We are building a FUNCTION_DECL, during which the access of its
18605 parameters and return types have to be checked. However this
18606 FUNCTION_DECL which is the desired context for access checking
18607 is not built yet. We solve this chicken-and-egg problem by
18608 deferring all checks until we have the FUNCTION_DECL. */
18609 push_deferring_access_checks (dk_deferred);
18610
18611 /* Instantiation of the function happens in the context of the function
18612 template, not the context of the overload resolution we're doing. */
18613 push_to_top_level ();
18614 /* If there are dependent arguments, e.g. because we're doing partial
18615 ordering, make sure processing_template_decl stays set. */
18616 if (uses_template_parms (targ_ptr))
18617 ++processing_template_decl;
18618 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18619 {
18620 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18621 complain, gen_tmpl, true);
18622 push_nested_class (ctx);
18623 }
18624
18625 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18626
18627 fndecl = NULL_TREE;
18628 if (VAR_P (pattern))
18629 {
18630 /* We need to determine if we're using a partial or explicit
18631 specialization now, because the type of the variable could be
18632 different. */
18633 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18634 tree elt = most_specialized_partial_spec (tid, complain);
18635 if (elt == error_mark_node)
18636 pattern = error_mark_node;
18637 else if (elt)
18638 {
18639 tree partial_tmpl = TREE_VALUE (elt);
18640 tree partial_args = TREE_PURPOSE (elt);
18641 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18642 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18643 }
18644 }
18645
18646 /* Substitute template parameters to obtain the specialization. */
18647 if (fndecl == NULL_TREE)
18648 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18649 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18650 pop_nested_class ();
18651 pop_from_top_level ();
18652
18653 if (fndecl == error_mark_node)
18654 {
18655 pop_deferring_access_checks ();
18656 return error_mark_node;
18657 }
18658
18659 /* The DECL_TI_TEMPLATE should always be the immediate parent
18660 template, not the most general template. */
18661 DECL_TI_TEMPLATE (fndecl) = tmpl;
18662 DECL_TI_ARGS (fndecl) = targ_ptr;
18663
18664 /* Now we know the specialization, compute access previously
18665 deferred. Do no access control for inheriting constructors,
18666 as we already checked access for the inherited constructor. */
18667 if (!(flag_new_inheriting_ctors
18668 && DECL_INHERITED_CTOR (fndecl)))
18669 {
18670 push_access_scope (fndecl);
18671 if (!perform_deferred_access_checks (complain))
18672 access_ok = false;
18673 pop_access_scope (fndecl);
18674 }
18675 pop_deferring_access_checks ();
18676
18677 /* If we've just instantiated the main entry point for a function,
18678 instantiate all the alternate entry points as well. We do this
18679 by cloning the instantiation of the main entry point, not by
18680 instantiating the template clones. */
18681 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18682 clone_function_decl (fndecl, /*update_methods=*/false);
18683
18684 if (!access_ok)
18685 {
18686 if (!(complain & tf_error))
18687 {
18688 /* Remember to reinstantiate when we're out of SFINAE so the user
18689 can see the errors. */
18690 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18691 }
18692 return error_mark_node;
18693 }
18694 return fndecl;
18695 }
18696
18697 /* Wrapper for instantiate_template_1. */
18698
18699 tree
18700 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18701 {
18702 tree ret;
18703 timevar_push (TV_TEMPLATE_INST);
18704 ret = instantiate_template_1 (tmpl, orig_args, complain);
18705 timevar_pop (TV_TEMPLATE_INST);
18706 return ret;
18707 }
18708
18709 /* Instantiate the alias template TMPL with ARGS. Also push a template
18710 instantiation level, which instantiate_template doesn't do because
18711 functions and variables have sufficient context established by the
18712 callers. */
18713
18714 static tree
18715 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18716 {
18717 struct pending_template *old_last_pend = last_pending_template;
18718 struct tinst_level *old_error_tinst = last_error_tinst_level;
18719 if (tmpl == error_mark_node || args == error_mark_node)
18720 return error_mark_node;
18721 tree tinst = build_tree_list (tmpl, args);
18722 if (!push_tinst_level (tinst))
18723 {
18724 ggc_free (tinst);
18725 return error_mark_node;
18726 }
18727
18728 args =
18729 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18730 args, tmpl, complain,
18731 /*require_all_args=*/true,
18732 /*use_default_args=*/true);
18733
18734 tree r = instantiate_template (tmpl, args, complain);
18735 pop_tinst_level ();
18736 /* We can't free this if a pending_template entry or last_error_tinst_level
18737 is pointing at it. */
18738 if (last_pending_template == old_last_pend
18739 && last_error_tinst_level == old_error_tinst)
18740 ggc_free (tinst);
18741
18742 return r;
18743 }
18744
18745 /* PARM is a template parameter pack for FN. Returns true iff
18746 PARM is used in a deducible way in the argument list of FN. */
18747
18748 static bool
18749 pack_deducible_p (tree parm, tree fn)
18750 {
18751 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18752 for (; t; t = TREE_CHAIN (t))
18753 {
18754 tree type = TREE_VALUE (t);
18755 tree packs;
18756 if (!PACK_EXPANSION_P (type))
18757 continue;
18758 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18759 packs; packs = TREE_CHAIN (packs))
18760 if (template_args_equal (TREE_VALUE (packs), parm))
18761 {
18762 /* The template parameter pack is used in a function parameter
18763 pack. If this is the end of the parameter list, the
18764 template parameter pack is deducible. */
18765 if (TREE_CHAIN (t) == void_list_node)
18766 return true;
18767 else
18768 /* Otherwise, not. Well, it could be deduced from
18769 a non-pack parameter, but doing so would end up with
18770 a deduction mismatch, so don't bother. */
18771 return false;
18772 }
18773 }
18774 /* The template parameter pack isn't used in any function parameter
18775 packs, but it might be used deeper, e.g. tuple<Args...>. */
18776 return true;
18777 }
18778
18779 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18780 NARGS elements of the arguments that are being used when calling
18781 it. TARGS is a vector into which the deduced template arguments
18782 are placed.
18783
18784 Returns either a FUNCTION_DECL for the matching specialization of FN or
18785 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18786 true, diagnostics will be printed to explain why it failed.
18787
18788 If FN is a conversion operator, or we are trying to produce a specific
18789 specialization, RETURN_TYPE is the return type desired.
18790
18791 The EXPLICIT_TARGS are explicit template arguments provided via a
18792 template-id.
18793
18794 The parameter STRICT is one of:
18795
18796 DEDUCE_CALL:
18797 We are deducing arguments for a function call, as in
18798 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18799 deducing arguments for a call to the result of a conversion
18800 function template, as in [over.call.object].
18801
18802 DEDUCE_CONV:
18803 We are deducing arguments for a conversion function, as in
18804 [temp.deduct.conv].
18805
18806 DEDUCE_EXACT:
18807 We are deducing arguments when doing an explicit instantiation
18808 as in [temp.explicit], when determining an explicit specialization
18809 as in [temp.expl.spec], or when taking the address of a function
18810 template, as in [temp.deduct.funcaddr]. */
18811
18812 tree
18813 fn_type_unification (tree fn,
18814 tree explicit_targs,
18815 tree targs,
18816 const tree *args,
18817 unsigned int nargs,
18818 tree return_type,
18819 unification_kind_t strict,
18820 int flags,
18821 bool explain_p,
18822 bool decltype_p)
18823 {
18824 tree parms;
18825 tree fntype;
18826 tree decl = NULL_TREE;
18827 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18828 bool ok;
18829 static int deduction_depth;
18830 struct pending_template *old_last_pend = last_pending_template;
18831 struct tinst_level *old_error_tinst = last_error_tinst_level;
18832
18833 tree orig_fn = fn;
18834 if (flag_new_inheriting_ctors)
18835 fn = strip_inheriting_ctors (fn);
18836
18837 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18838 tree tinst;
18839 tree r = error_mark_node;
18840
18841 tree full_targs = targs;
18842 if (TMPL_ARGS_DEPTH (targs)
18843 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18844 full_targs = (add_outermost_template_args
18845 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18846 targs));
18847
18848 if (decltype_p)
18849 complain |= tf_decltype;
18850
18851 /* In C++0x, it's possible to have a function template whose type depends
18852 on itself recursively. This is most obvious with decltype, but can also
18853 occur with enumeration scope (c++/48969). So we need to catch infinite
18854 recursion and reject the substitution at deduction time; this function
18855 will return error_mark_node for any repeated substitution.
18856
18857 This also catches excessive recursion such as when f<N> depends on
18858 f<N-1> across all integers, and returns error_mark_node for all the
18859 substitutions back up to the initial one.
18860
18861 This is, of course, not reentrant. */
18862 if (excessive_deduction_depth)
18863 return error_mark_node;
18864 tinst = build_tree_list (fn, NULL_TREE);
18865 ++deduction_depth;
18866
18867 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18868
18869 fntype = TREE_TYPE (fn);
18870 if (explicit_targs)
18871 {
18872 /* [temp.deduct]
18873
18874 The specified template arguments must match the template
18875 parameters in kind (i.e., type, nontype, template), and there
18876 must not be more arguments than there are parameters;
18877 otherwise type deduction fails.
18878
18879 Nontype arguments must match the types of the corresponding
18880 nontype template parameters, or must be convertible to the
18881 types of the corresponding nontype parameters as specified in
18882 _temp.arg.nontype_, otherwise type deduction fails.
18883
18884 All references in the function type of the function template
18885 to the corresponding template parameters are replaced by the
18886 specified template argument values. If a substitution in a
18887 template parameter or in the function type of the function
18888 template results in an invalid type, type deduction fails. */
18889 int i, len = TREE_VEC_LENGTH (tparms);
18890 location_t loc = input_location;
18891 bool incomplete = false;
18892
18893 if (explicit_targs == error_mark_node)
18894 goto fail;
18895
18896 if (TMPL_ARGS_DEPTH (explicit_targs)
18897 < TMPL_ARGS_DEPTH (full_targs))
18898 explicit_targs = add_outermost_template_args (full_targs,
18899 explicit_targs);
18900
18901 /* Adjust any explicit template arguments before entering the
18902 substitution context. */
18903 explicit_targs
18904 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18905 complain,
18906 /*require_all_args=*/false,
18907 /*use_default_args=*/false));
18908 if (explicit_targs == error_mark_node)
18909 goto fail;
18910
18911 /* Substitute the explicit args into the function type. This is
18912 necessary so that, for instance, explicitly declared function
18913 arguments can match null pointed constants. If we were given
18914 an incomplete set of explicit args, we must not do semantic
18915 processing during substitution as we could create partial
18916 instantiations. */
18917 for (i = 0; i < len; i++)
18918 {
18919 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18920 bool parameter_pack = false;
18921 tree targ = TREE_VEC_ELT (explicit_targs, i);
18922
18923 /* Dig out the actual parm. */
18924 if (TREE_CODE (parm) == TYPE_DECL
18925 || TREE_CODE (parm) == TEMPLATE_DECL)
18926 {
18927 parm = TREE_TYPE (parm);
18928 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18929 }
18930 else if (TREE_CODE (parm) == PARM_DECL)
18931 {
18932 parm = DECL_INITIAL (parm);
18933 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18934 }
18935
18936 if (!parameter_pack && targ == NULL_TREE)
18937 /* No explicit argument for this template parameter. */
18938 incomplete = true;
18939
18940 if (parameter_pack && pack_deducible_p (parm, fn))
18941 {
18942 /* Mark the argument pack as "incomplete". We could
18943 still deduce more arguments during unification.
18944 We remove this mark in type_unification_real. */
18945 if (targ)
18946 {
18947 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18948 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18949 = ARGUMENT_PACK_ARGS (targ);
18950 }
18951
18952 /* We have some incomplete argument packs. */
18953 incomplete = true;
18954 }
18955 }
18956
18957 TREE_VALUE (tinst) = explicit_targs;
18958 if (!push_tinst_level (tinst))
18959 {
18960 excessive_deduction_depth = true;
18961 goto fail;
18962 }
18963 processing_template_decl += incomplete;
18964 input_location = DECL_SOURCE_LOCATION (fn);
18965 /* Ignore any access checks; we'll see them again in
18966 instantiate_template and they might have the wrong
18967 access path at this point. */
18968 push_deferring_access_checks (dk_deferred);
18969 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18970 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18971 pop_deferring_access_checks ();
18972 input_location = loc;
18973 processing_template_decl -= incomplete;
18974 pop_tinst_level ();
18975
18976 if (fntype == error_mark_node)
18977 goto fail;
18978
18979 /* Place the explicitly specified arguments in TARGS. */
18980 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18981 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18982 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18983 }
18984
18985 /* Never do unification on the 'this' parameter. */
18986 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18987
18988 if (return_type && strict == DEDUCE_CALL)
18989 {
18990 /* We're deducing for a call to the result of a template conversion
18991 function. The parms we really want are in return_type. */
18992 if (POINTER_TYPE_P (return_type))
18993 return_type = TREE_TYPE (return_type);
18994 parms = TYPE_ARG_TYPES (return_type);
18995 }
18996 else if (return_type)
18997 {
18998 tree *new_args;
18999
19000 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19001 new_args = XALLOCAVEC (tree, nargs + 1);
19002 new_args[0] = return_type;
19003 memcpy (new_args + 1, args, nargs * sizeof (tree));
19004 args = new_args;
19005 ++nargs;
19006 }
19007
19008 /* We allow incomplete unification without an error message here
19009 because the standard doesn't seem to explicitly prohibit it. Our
19010 callers must be ready to deal with unification failures in any
19011 event. */
19012
19013 TREE_VALUE (tinst) = targs;
19014 /* If we aren't explaining yet, push tinst context so we can see where
19015 any errors (e.g. from class instantiations triggered by instantiation
19016 of default template arguments) come from. If we are explaining, this
19017 context is redundant. */
19018 if (!explain_p && !push_tinst_level (tinst))
19019 {
19020 excessive_deduction_depth = true;
19021 goto fail;
19022 }
19023
19024 /* type_unification_real will pass back any access checks from default
19025 template argument substitution. */
19026 vec<deferred_access_check, va_gc> *checks;
19027 checks = NULL;
19028
19029 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19030 full_targs, parms, args, nargs, /*subr=*/0,
19031 strict, flags, &checks, explain_p);
19032 if (!explain_p)
19033 pop_tinst_level ();
19034 if (!ok)
19035 goto fail;
19036
19037 /* Now that we have bindings for all of the template arguments,
19038 ensure that the arguments deduced for the template template
19039 parameters have compatible template parameter lists. We cannot
19040 check this property before we have deduced all template
19041 arguments, because the template parameter types of a template
19042 template parameter might depend on prior template parameters
19043 deduced after the template template parameter. The following
19044 ill-formed example illustrates this issue:
19045
19046 template<typename T, template<T> class C> void f(C<5>, T);
19047
19048 template<int N> struct X {};
19049
19050 void g() {
19051 f(X<5>(), 5l); // error: template argument deduction fails
19052 }
19053
19054 The template parameter list of 'C' depends on the template type
19055 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19056 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19057 time that we deduce 'C'. */
19058 if (!template_template_parm_bindings_ok_p
19059 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19060 {
19061 unify_inconsistent_template_template_parameters (explain_p);
19062 goto fail;
19063 }
19064
19065 /* All is well so far. Now, check:
19066
19067 [temp.deduct]
19068
19069 When all template arguments have been deduced, all uses of
19070 template parameters in nondeduced contexts are replaced with
19071 the corresponding deduced argument values. If the
19072 substitution results in an invalid type, as described above,
19073 type deduction fails. */
19074 TREE_VALUE (tinst) = targs;
19075 if (!push_tinst_level (tinst))
19076 {
19077 excessive_deduction_depth = true;
19078 goto fail;
19079 }
19080
19081 /* Also collect access checks from the instantiation. */
19082 reopen_deferring_access_checks (checks);
19083
19084 decl = instantiate_template (fn, targs, complain);
19085
19086 checks = get_deferred_access_checks ();
19087 pop_deferring_access_checks ();
19088
19089 pop_tinst_level ();
19090
19091 if (decl == error_mark_node)
19092 goto fail;
19093
19094 /* Now perform any access checks encountered during substitution. */
19095 push_access_scope (decl);
19096 ok = perform_access_checks (checks, complain);
19097 pop_access_scope (decl);
19098 if (!ok)
19099 goto fail;
19100
19101 /* If we're looking for an exact match, check that what we got
19102 is indeed an exact match. It might not be if some template
19103 parameters are used in non-deduced contexts. But don't check
19104 for an exact match if we have dependent template arguments;
19105 in that case we're doing partial ordering, and we already know
19106 that we have two candidates that will provide the actual type. */
19107 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19108 {
19109 tree substed = TREE_TYPE (decl);
19110 unsigned int i;
19111
19112 tree sarg
19113 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19114 if (return_type)
19115 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19116 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19117 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19118 {
19119 unify_type_mismatch (explain_p, args[i],
19120 TREE_VALUE (sarg));
19121 goto fail;
19122 }
19123 }
19124
19125 /* After doing deduction with the inherited constructor, actually return an
19126 instantiation of the inheriting constructor. */
19127 if (orig_fn != fn)
19128 decl = instantiate_template (orig_fn, targs, complain);
19129
19130 r = decl;
19131
19132 fail:
19133 --deduction_depth;
19134 if (excessive_deduction_depth)
19135 {
19136 if (deduction_depth == 0)
19137 /* Reset once we're all the way out. */
19138 excessive_deduction_depth = false;
19139 }
19140
19141 /* We can't free this if a pending_template entry or last_error_tinst_level
19142 is pointing at it. */
19143 if (last_pending_template == old_last_pend
19144 && last_error_tinst_level == old_error_tinst)
19145 ggc_free (tinst);
19146
19147 return r;
19148 }
19149
19150 /* Adjust types before performing type deduction, as described in
19151 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19152 sections are symmetric. PARM is the type of a function parameter
19153 or the return type of the conversion function. ARG is the type of
19154 the argument passed to the call, or the type of the value
19155 initialized with the result of the conversion function.
19156 ARG_EXPR is the original argument expression, which may be null. */
19157
19158 static int
19159 maybe_adjust_types_for_deduction (unification_kind_t strict,
19160 tree* parm,
19161 tree* arg,
19162 tree arg_expr)
19163 {
19164 int result = 0;
19165
19166 switch (strict)
19167 {
19168 case DEDUCE_CALL:
19169 break;
19170
19171 case DEDUCE_CONV:
19172 /* Swap PARM and ARG throughout the remainder of this
19173 function; the handling is precisely symmetric since PARM
19174 will initialize ARG rather than vice versa. */
19175 std::swap (parm, arg);
19176 break;
19177
19178 case DEDUCE_EXACT:
19179 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19180 too, but here handle it by stripping the reference from PARM
19181 rather than by adding it to ARG. */
19182 if (TREE_CODE (*parm) == REFERENCE_TYPE
19183 && TYPE_REF_IS_RVALUE (*parm)
19184 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19185 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19186 && TREE_CODE (*arg) == REFERENCE_TYPE
19187 && !TYPE_REF_IS_RVALUE (*arg))
19188 *parm = TREE_TYPE (*parm);
19189 /* Nothing else to do in this case. */
19190 return 0;
19191
19192 default:
19193 gcc_unreachable ();
19194 }
19195
19196 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19197 {
19198 /* [temp.deduct.call]
19199
19200 If P is not a reference type:
19201
19202 --If A is an array type, the pointer type produced by the
19203 array-to-pointer standard conversion (_conv.array_) is
19204 used in place of A for type deduction; otherwise,
19205
19206 --If A is a function type, the pointer type produced by
19207 the function-to-pointer standard conversion
19208 (_conv.func_) is used in place of A for type deduction;
19209 otherwise,
19210
19211 --If A is a cv-qualified type, the top level
19212 cv-qualifiers of A's type are ignored for type
19213 deduction. */
19214 if (TREE_CODE (*arg) == ARRAY_TYPE)
19215 *arg = build_pointer_type (TREE_TYPE (*arg));
19216 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19217 *arg = build_pointer_type (*arg);
19218 else
19219 *arg = TYPE_MAIN_VARIANT (*arg);
19220 }
19221
19222 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19223 reference to a cv-unqualified template parameter that does not represent a
19224 template parameter of a class template (during class template argument
19225 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19226 an lvalue, the type "lvalue reference to A" is used in place of A for type
19227 deduction. */
19228 if (TREE_CODE (*parm) == REFERENCE_TYPE
19229 && TYPE_REF_IS_RVALUE (*parm)
19230 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19231 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19232 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19233 && (arg_expr ? lvalue_p (arg_expr)
19234 /* try_one_overload doesn't provide an arg_expr, but
19235 functions are always lvalues. */
19236 : TREE_CODE (*arg) == FUNCTION_TYPE))
19237 *arg = build_reference_type (*arg);
19238
19239 /* [temp.deduct.call]
19240
19241 If P is a cv-qualified type, the top level cv-qualifiers
19242 of P's type are ignored for type deduction. If P is a
19243 reference type, the type referred to by P is used for
19244 type deduction. */
19245 *parm = TYPE_MAIN_VARIANT (*parm);
19246 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19247 {
19248 *parm = TREE_TYPE (*parm);
19249 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19250 }
19251
19252 /* DR 322. For conversion deduction, remove a reference type on parm
19253 too (which has been swapped into ARG). */
19254 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19255 *arg = TREE_TYPE (*arg);
19256
19257 return result;
19258 }
19259
19260 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19261 template which does contain any deducible template parameters; check if
19262 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19263 unify_one_argument. */
19264
19265 static int
19266 check_non_deducible_conversion (tree parm, tree arg, int strict,
19267 int flags, bool explain_p)
19268 {
19269 tree type;
19270
19271 if (!TYPE_P (arg))
19272 type = TREE_TYPE (arg);
19273 else
19274 type = arg;
19275
19276 if (same_type_p (parm, type))
19277 return unify_success (explain_p);
19278
19279 if (strict == DEDUCE_CONV)
19280 {
19281 if (can_convert_arg (type, parm, NULL_TREE, flags,
19282 explain_p ? tf_warning_or_error : tf_none))
19283 return unify_success (explain_p);
19284 }
19285 else if (strict != DEDUCE_EXACT)
19286 {
19287 if (can_convert_arg (parm, type,
19288 TYPE_P (arg) ? NULL_TREE : arg,
19289 flags, explain_p ? tf_warning_or_error : tf_none))
19290 return unify_success (explain_p);
19291 }
19292
19293 if (strict == DEDUCE_EXACT)
19294 return unify_type_mismatch (explain_p, parm, arg);
19295 else
19296 return unify_arg_conversion (explain_p, parm, type, arg);
19297 }
19298
19299 static bool uses_deducible_template_parms (tree type);
19300
19301 /* Returns true iff the expression EXPR is one from which a template
19302 argument can be deduced. In other words, if it's an undecorated
19303 use of a template non-type parameter. */
19304
19305 static bool
19306 deducible_expression (tree expr)
19307 {
19308 /* Strip implicit conversions. */
19309 while (CONVERT_EXPR_P (expr))
19310 expr = TREE_OPERAND (expr, 0);
19311 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19312 }
19313
19314 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19315 deducible way; that is, if it has a max value of <PARM> - 1. */
19316
19317 static bool
19318 deducible_array_bound (tree domain)
19319 {
19320 if (domain == NULL_TREE)
19321 return false;
19322
19323 tree max = TYPE_MAX_VALUE (domain);
19324 if (TREE_CODE (max) != MINUS_EXPR)
19325 return false;
19326
19327 return deducible_expression (TREE_OPERAND (max, 0));
19328 }
19329
19330 /* Returns true iff the template arguments ARGS use a template parameter
19331 in a deducible way. */
19332
19333 static bool
19334 deducible_template_args (tree args)
19335 {
19336 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19337 {
19338 bool deducible;
19339 tree elt = TREE_VEC_ELT (args, i);
19340 if (ARGUMENT_PACK_P (elt))
19341 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19342 else
19343 {
19344 if (PACK_EXPANSION_P (elt))
19345 elt = PACK_EXPANSION_PATTERN (elt);
19346 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19347 deducible = true;
19348 else if (TYPE_P (elt))
19349 deducible = uses_deducible_template_parms (elt);
19350 else
19351 deducible = deducible_expression (elt);
19352 }
19353 if (deducible)
19354 return true;
19355 }
19356 return false;
19357 }
19358
19359 /* Returns true iff TYPE contains any deducible references to template
19360 parameters, as per 14.8.2.5. */
19361
19362 static bool
19363 uses_deducible_template_parms (tree type)
19364 {
19365 if (PACK_EXPANSION_P (type))
19366 type = PACK_EXPANSION_PATTERN (type);
19367
19368 /* T
19369 cv-list T
19370 TT<T>
19371 TT<i>
19372 TT<> */
19373 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19374 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19375 return true;
19376
19377 /* T*
19378 T&
19379 T&& */
19380 if (POINTER_TYPE_P (type))
19381 return uses_deducible_template_parms (TREE_TYPE (type));
19382
19383 /* T[integer-constant ]
19384 type [i] */
19385 if (TREE_CODE (type) == ARRAY_TYPE)
19386 return (uses_deducible_template_parms (TREE_TYPE (type))
19387 || deducible_array_bound (TYPE_DOMAIN (type)));
19388
19389 /* T type ::*
19390 type T::*
19391 T T::*
19392 T (type ::*)()
19393 type (T::*)()
19394 type (type ::*)(T)
19395 type (T::*)(T)
19396 T (type ::*)(T)
19397 T (T::*)()
19398 T (T::*)(T) */
19399 if (TYPE_PTRMEM_P (type))
19400 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19401 || (uses_deducible_template_parms
19402 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19403
19404 /* template-name <T> (where template-name refers to a class template)
19405 template-name <i> (where template-name refers to a class template) */
19406 if (CLASS_TYPE_P (type)
19407 && CLASSTYPE_TEMPLATE_INFO (type)
19408 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19409 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19410 (CLASSTYPE_TI_ARGS (type)));
19411
19412 /* type (T)
19413 T()
19414 T(T) */
19415 if (TREE_CODE (type) == FUNCTION_TYPE
19416 || TREE_CODE (type) == METHOD_TYPE)
19417 {
19418 if (uses_deducible_template_parms (TREE_TYPE (type)))
19419 return true;
19420 tree parm = TYPE_ARG_TYPES (type);
19421 if (TREE_CODE (type) == METHOD_TYPE)
19422 parm = TREE_CHAIN (parm);
19423 for (; parm; parm = TREE_CHAIN (parm))
19424 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19425 return true;
19426 }
19427
19428 return false;
19429 }
19430
19431 /* Subroutine of type_unification_real and unify_pack_expansion to
19432 handle unification of a single P/A pair. Parameters are as
19433 for those functions. */
19434
19435 static int
19436 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19437 int subr, unification_kind_t strict,
19438 bool explain_p)
19439 {
19440 tree arg_expr = NULL_TREE;
19441 int arg_strict;
19442
19443 if (arg == error_mark_node || parm == error_mark_node)
19444 return unify_invalid (explain_p);
19445 if (arg == unknown_type_node)
19446 /* We can't deduce anything from this, but we might get all the
19447 template args from other function args. */
19448 return unify_success (explain_p);
19449
19450 /* Implicit conversions (Clause 4) will be performed on a function
19451 argument to convert it to the type of the corresponding function
19452 parameter if the parameter type contains no template-parameters that
19453 participate in template argument deduction. */
19454 if (strict != DEDUCE_EXACT
19455 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19456 /* For function parameters with no deducible template parameters,
19457 just return. We'll check non-dependent conversions later. */
19458 return unify_success (explain_p);
19459
19460 switch (strict)
19461 {
19462 case DEDUCE_CALL:
19463 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19464 | UNIFY_ALLOW_MORE_CV_QUAL
19465 | UNIFY_ALLOW_DERIVED);
19466 break;
19467
19468 case DEDUCE_CONV:
19469 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19470 break;
19471
19472 case DEDUCE_EXACT:
19473 arg_strict = UNIFY_ALLOW_NONE;
19474 break;
19475
19476 default:
19477 gcc_unreachable ();
19478 }
19479
19480 /* We only do these transformations if this is the top-level
19481 parameter_type_list in a call or declaration matching; in other
19482 situations (nested function declarators, template argument lists) we
19483 won't be comparing a type to an expression, and we don't do any type
19484 adjustments. */
19485 if (!subr)
19486 {
19487 if (!TYPE_P (arg))
19488 {
19489 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19490 if (type_unknown_p (arg))
19491 {
19492 /* [temp.deduct.type] A template-argument can be
19493 deduced from a pointer to function or pointer
19494 to member function argument if the set of
19495 overloaded functions does not contain function
19496 templates and at most one of a set of
19497 overloaded functions provides a unique
19498 match. */
19499 resolve_overloaded_unification (tparms, targs, parm,
19500 arg, strict,
19501 arg_strict, explain_p);
19502 /* If a unique match was not found, this is a
19503 non-deduced context, so we still succeed. */
19504 return unify_success (explain_p);
19505 }
19506
19507 arg_expr = arg;
19508 arg = unlowered_expr_type (arg);
19509 if (arg == error_mark_node)
19510 return unify_invalid (explain_p);
19511 }
19512
19513 arg_strict |=
19514 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19515 }
19516 else
19517 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19518 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19519 return unify_template_argument_mismatch (explain_p, parm, arg);
19520
19521 /* For deduction from an init-list we need the actual list. */
19522 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19523 arg = arg_expr;
19524 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19525 }
19526
19527 /* for_each_template_parm callback that always returns 0. */
19528
19529 static int
19530 zero_r (tree, void *)
19531 {
19532 return 0;
19533 }
19534
19535 /* for_each_template_parm any_fn callback to handle deduction of a template
19536 type argument from the type of an array bound. */
19537
19538 static int
19539 array_deduction_r (tree t, void *data)
19540 {
19541 tree_pair_p d = (tree_pair_p)data;
19542 tree &tparms = d->purpose;
19543 tree &targs = d->value;
19544
19545 if (TREE_CODE (t) == ARRAY_TYPE)
19546 if (tree dom = TYPE_DOMAIN (t))
19547 if (tree max = TYPE_MAX_VALUE (dom))
19548 {
19549 if (TREE_CODE (max) == MINUS_EXPR)
19550 max = TREE_OPERAND (max, 0);
19551 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19552 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19553 UNIFY_ALLOW_NONE, /*explain*/false);
19554 }
19555
19556 /* Keep walking. */
19557 return 0;
19558 }
19559
19560 /* Try to deduce any not-yet-deduced template type arguments from the type of
19561 an array bound. This is handled separately from unify because 14.8.2.5 says
19562 "The type of a type parameter is only deduced from an array bound if it is
19563 not otherwise deduced." */
19564
19565 static void
19566 try_array_deduction (tree tparms, tree targs, tree parm)
19567 {
19568 tree_pair_s data = { tparms, targs };
19569 hash_set<tree> visited;
19570 for_each_template_parm (parm, zero_r, &data, &visited,
19571 /*nondeduced*/false, array_deduction_r);
19572 }
19573
19574 /* Most parms like fn_type_unification.
19575
19576 If SUBR is 1, we're being called recursively (to unify the
19577 arguments of a function or method parameter of a function
19578 template).
19579
19580 CHECKS is a pointer to a vector of access checks encountered while
19581 substituting default template arguments. */
19582
19583 static int
19584 type_unification_real (tree tparms,
19585 tree full_targs,
19586 tree xparms,
19587 const tree *xargs,
19588 unsigned int xnargs,
19589 int subr,
19590 unification_kind_t strict,
19591 int flags,
19592 vec<deferred_access_check, va_gc> **checks,
19593 bool explain_p)
19594 {
19595 tree parm, arg;
19596 int i;
19597 int ntparms = TREE_VEC_LENGTH (tparms);
19598 int saw_undeduced = 0;
19599 tree parms;
19600 const tree *args;
19601 unsigned int nargs;
19602 unsigned int ia;
19603
19604 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19605 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19606 gcc_assert (ntparms > 0);
19607
19608 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19609
19610 /* Reset the number of non-defaulted template arguments contained
19611 in TARGS. */
19612 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19613
19614 again:
19615 parms = xparms;
19616 args = xargs;
19617 nargs = xnargs;
19618
19619 ia = 0;
19620 while (parms && parms != void_list_node
19621 && ia < nargs)
19622 {
19623 parm = TREE_VALUE (parms);
19624
19625 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19626 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19627 /* For a function parameter pack that occurs at the end of the
19628 parameter-declaration-list, the type A of each remaining
19629 argument of the call is compared with the type P of the
19630 declarator-id of the function parameter pack. */
19631 break;
19632
19633 parms = TREE_CHAIN (parms);
19634
19635 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19636 /* For a function parameter pack that does not occur at the
19637 end of the parameter-declaration-list, the type of the
19638 parameter pack is a non-deduced context. */
19639 continue;
19640
19641 arg = args[ia];
19642 ++ia;
19643
19644 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19645 explain_p))
19646 return 1;
19647 }
19648
19649 if (parms
19650 && parms != void_list_node
19651 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19652 {
19653 /* Unify the remaining arguments with the pack expansion type. */
19654 tree argvec;
19655 tree parmvec = make_tree_vec (1);
19656
19657 /* Allocate a TREE_VEC and copy in all of the arguments */
19658 argvec = make_tree_vec (nargs - ia);
19659 for (i = 0; ia < nargs; ++ia, ++i)
19660 TREE_VEC_ELT (argvec, i) = args[ia];
19661
19662 /* Copy the parameter into parmvec. */
19663 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19664 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19665 /*subr=*/subr, explain_p))
19666 return 1;
19667
19668 /* Advance to the end of the list of parameters. */
19669 parms = TREE_CHAIN (parms);
19670 }
19671
19672 /* Fail if we've reached the end of the parm list, and more args
19673 are present, and the parm list isn't variadic. */
19674 if (ia < nargs && parms == void_list_node)
19675 return unify_too_many_arguments (explain_p, nargs, ia);
19676 /* Fail if parms are left and they don't have default values and
19677 they aren't all deduced as empty packs (c++/57397). This is
19678 consistent with sufficient_parms_p. */
19679 if (parms && parms != void_list_node
19680 && TREE_PURPOSE (parms) == NULL_TREE)
19681 {
19682 unsigned int count = nargs;
19683 tree p = parms;
19684 bool type_pack_p;
19685 do
19686 {
19687 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19688 if (!type_pack_p)
19689 count++;
19690 p = TREE_CHAIN (p);
19691 }
19692 while (p && p != void_list_node);
19693 if (count != nargs)
19694 return unify_too_few_arguments (explain_p, ia, count,
19695 type_pack_p);
19696 }
19697
19698 if (!subr)
19699 {
19700 tsubst_flags_t complain = (explain_p
19701 ? tf_warning_or_error
19702 : tf_none);
19703 bool tried_array_deduction = (cxx_dialect < cxx17);
19704
19705 for (i = 0; i < ntparms; i++)
19706 {
19707 tree targ = TREE_VEC_ELT (targs, i);
19708 tree tparm = TREE_VEC_ELT (tparms, i);
19709
19710 /* Clear the "incomplete" flags on all argument packs now so that
19711 substituting them into later default arguments works. */
19712 if (targ && ARGUMENT_PACK_P (targ))
19713 {
19714 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19715 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19716 }
19717
19718 if (targ || tparm == error_mark_node)
19719 continue;
19720 tparm = TREE_VALUE (tparm);
19721
19722 if (TREE_CODE (tparm) == TYPE_DECL
19723 && !tried_array_deduction)
19724 {
19725 try_array_deduction (tparms, targs, xparms);
19726 tried_array_deduction = true;
19727 if (TREE_VEC_ELT (targs, i))
19728 continue;
19729 }
19730
19731 /* If this is an undeduced nontype parameter that depends on
19732 a type parameter, try another pass; its type may have been
19733 deduced from a later argument than the one from which
19734 this parameter can be deduced. */
19735 if (TREE_CODE (tparm) == PARM_DECL
19736 && uses_template_parms (TREE_TYPE (tparm))
19737 && saw_undeduced < 2)
19738 {
19739 saw_undeduced = 1;
19740 continue;
19741 }
19742
19743 /* Core issue #226 (C++0x) [temp.deduct]:
19744
19745 If a template argument has not been deduced, its
19746 default template argument, if any, is used.
19747
19748 When we are in C++98 mode, TREE_PURPOSE will either
19749 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19750 to explicitly check cxx_dialect here. */
19751 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19752 /* OK, there is a default argument. Wait until after the
19753 conversion check to do substitution. */
19754 continue;
19755
19756 /* If the type parameter is a parameter pack, then it will
19757 be deduced to an empty parameter pack. */
19758 if (template_parameter_pack_p (tparm))
19759 {
19760 tree arg;
19761
19762 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19763 {
19764 arg = make_node (NONTYPE_ARGUMENT_PACK);
19765 TREE_CONSTANT (arg) = 1;
19766 }
19767 else
19768 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19769
19770 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19771
19772 TREE_VEC_ELT (targs, i) = arg;
19773 continue;
19774 }
19775
19776 return unify_parameter_deduction_failure (explain_p, tparm);
19777 }
19778
19779 /* DR 1391: All parameters have args, now check non-dependent parms for
19780 convertibility. */
19781 if (saw_undeduced < 2)
19782 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19783 parms && parms != void_list_node && ia < nargs; )
19784 {
19785 parm = TREE_VALUE (parms);
19786
19787 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19788 && (!TREE_CHAIN (parms)
19789 || TREE_CHAIN (parms) == void_list_node))
19790 /* For a function parameter pack that occurs at the end of the
19791 parameter-declaration-list, the type A of each remaining
19792 argument of the call is compared with the type P of the
19793 declarator-id of the function parameter pack. */
19794 break;
19795
19796 parms = TREE_CHAIN (parms);
19797
19798 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19799 /* For a function parameter pack that does not occur at the
19800 end of the parameter-declaration-list, the type of the
19801 parameter pack is a non-deduced context. */
19802 continue;
19803
19804 arg = args[ia];
19805 ++ia;
19806
19807 if (uses_template_parms (parm))
19808 continue;
19809 if (check_non_deducible_conversion (parm, arg, strict, flags,
19810 explain_p))
19811 return 1;
19812 }
19813
19814 /* Now substitute into the default template arguments. */
19815 for (i = 0; i < ntparms; i++)
19816 {
19817 tree targ = TREE_VEC_ELT (targs, i);
19818 tree tparm = TREE_VEC_ELT (tparms, i);
19819
19820 if (targ || tparm == error_mark_node)
19821 continue;
19822 tree parm = TREE_VALUE (tparm);
19823
19824 if (TREE_CODE (parm) == PARM_DECL
19825 && uses_template_parms (TREE_TYPE (parm))
19826 && saw_undeduced < 2)
19827 continue;
19828
19829 tree arg = TREE_PURPOSE (tparm);
19830 reopen_deferring_access_checks (*checks);
19831 location_t save_loc = input_location;
19832 if (DECL_P (parm))
19833 input_location = DECL_SOURCE_LOCATION (parm);
19834 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19835 if (!uses_template_parms (arg))
19836 arg = convert_template_argument (parm, arg, full_targs, complain,
19837 i, NULL_TREE);
19838 else if (saw_undeduced < 2)
19839 arg = NULL_TREE;
19840 else
19841 arg = error_mark_node;
19842 input_location = save_loc;
19843 *checks = get_deferred_access_checks ();
19844 pop_deferring_access_checks ();
19845 if (arg == error_mark_node)
19846 return 1;
19847 else if (arg)
19848 {
19849 TREE_VEC_ELT (targs, i) = arg;
19850 /* The position of the first default template argument,
19851 is also the number of non-defaulted arguments in TARGS.
19852 Record that. */
19853 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19855 }
19856 }
19857
19858 if (saw_undeduced++ == 1)
19859 goto again;
19860 }
19861
19862 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19863 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19864
19865 return unify_success (explain_p);
19866 }
19867
19868 /* Subroutine of type_unification_real. Args are like the variables
19869 at the call site. ARG is an overloaded function (or template-id);
19870 we try deducing template args from each of the overloads, and if
19871 only one succeeds, we go with that. Modifies TARGS and returns
19872 true on success. */
19873
19874 static bool
19875 resolve_overloaded_unification (tree tparms,
19876 tree targs,
19877 tree parm,
19878 tree arg,
19879 unification_kind_t strict,
19880 int sub_strict,
19881 bool explain_p)
19882 {
19883 tree tempargs = copy_node (targs);
19884 int good = 0;
19885 tree goodfn = NULL_TREE;
19886 bool addr_p;
19887
19888 if (TREE_CODE (arg) == ADDR_EXPR)
19889 {
19890 arg = TREE_OPERAND (arg, 0);
19891 addr_p = true;
19892 }
19893 else
19894 addr_p = false;
19895
19896 if (TREE_CODE (arg) == COMPONENT_REF)
19897 /* Handle `&x' where `x' is some static or non-static member
19898 function name. */
19899 arg = TREE_OPERAND (arg, 1);
19900
19901 if (TREE_CODE (arg) == OFFSET_REF)
19902 arg = TREE_OPERAND (arg, 1);
19903
19904 /* Strip baselink information. */
19905 if (BASELINK_P (arg))
19906 arg = BASELINK_FUNCTIONS (arg);
19907
19908 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19909 {
19910 /* If we got some explicit template args, we need to plug them into
19911 the affected templates before we try to unify, in case the
19912 explicit args will completely resolve the templates in question. */
19913
19914 int ok = 0;
19915 tree expl_subargs = TREE_OPERAND (arg, 1);
19916 arg = TREE_OPERAND (arg, 0);
19917
19918 for (lkp_iterator iter (arg); iter; ++iter)
19919 {
19920 tree fn = *iter;
19921 tree subargs, elem;
19922
19923 if (TREE_CODE (fn) != TEMPLATE_DECL)
19924 continue;
19925
19926 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19927 expl_subargs, NULL_TREE, tf_none,
19928 /*require_all_args=*/true,
19929 /*use_default_args=*/true);
19930 if (subargs != error_mark_node
19931 && !any_dependent_template_arguments_p (subargs))
19932 {
19933 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19934 if (try_one_overload (tparms, targs, tempargs, parm,
19935 elem, strict, sub_strict, addr_p, explain_p)
19936 && (!goodfn || !same_type_p (goodfn, elem)))
19937 {
19938 goodfn = elem;
19939 ++good;
19940 }
19941 }
19942 else if (subargs)
19943 ++ok;
19944 }
19945 /* If no templates (or more than one) are fully resolved by the
19946 explicit arguments, this template-id is a non-deduced context; it
19947 could still be OK if we deduce all template arguments for the
19948 enclosing call through other arguments. */
19949 if (good != 1)
19950 good = ok;
19951 }
19952 else if (TREE_CODE (arg) != OVERLOAD
19953 && TREE_CODE (arg) != FUNCTION_DECL)
19954 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19955 -- but the deduction does not succeed because the expression is
19956 not just the function on its own. */
19957 return false;
19958 else
19959 for (lkp_iterator iter (arg); iter; ++iter)
19960 {
19961 tree fn = *iter;
19962 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19963 strict, sub_strict, addr_p, explain_p)
19964 && (!goodfn || !decls_match (goodfn, fn)))
19965 {
19966 goodfn = fn;
19967 ++good;
19968 }
19969 }
19970
19971 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19972 to function or pointer to member function argument if the set of
19973 overloaded functions does not contain function templates and at most
19974 one of a set of overloaded functions provides a unique match.
19975
19976 So if we found multiple possibilities, we return success but don't
19977 deduce anything. */
19978
19979 if (good == 1)
19980 {
19981 int i = TREE_VEC_LENGTH (targs);
19982 for (; i--; )
19983 if (TREE_VEC_ELT (tempargs, i))
19984 {
19985 tree old = TREE_VEC_ELT (targs, i);
19986 tree new_ = TREE_VEC_ELT (tempargs, i);
19987 if (new_ && old && ARGUMENT_PACK_P (old)
19988 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19989 /* Don't forget explicit template arguments in a pack. */
19990 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19991 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19992 TREE_VEC_ELT (targs, i) = new_;
19993 }
19994 }
19995 if (good)
19996 return true;
19997
19998 return false;
19999 }
20000
20001 /* Core DR 115: In contexts where deduction is done and fails, or in
20002 contexts where deduction is not done, if a template argument list is
20003 specified and it, along with any default template arguments, identifies
20004 a single function template specialization, then the template-id is an
20005 lvalue for the function template specialization. */
20006
20007 tree
20008 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20009 {
20010 tree expr, offset, baselink;
20011 bool addr;
20012
20013 if (!type_unknown_p (orig_expr))
20014 return orig_expr;
20015
20016 expr = orig_expr;
20017 addr = false;
20018 offset = NULL_TREE;
20019 baselink = NULL_TREE;
20020
20021 if (TREE_CODE (expr) == ADDR_EXPR)
20022 {
20023 expr = TREE_OPERAND (expr, 0);
20024 addr = true;
20025 }
20026 if (TREE_CODE (expr) == OFFSET_REF)
20027 {
20028 offset = expr;
20029 expr = TREE_OPERAND (expr, 1);
20030 }
20031 if (BASELINK_P (expr))
20032 {
20033 baselink = expr;
20034 expr = BASELINK_FUNCTIONS (expr);
20035 }
20036
20037 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20038 {
20039 int good = 0;
20040 tree goodfn = NULL_TREE;
20041
20042 /* If we got some explicit template args, we need to plug them into
20043 the affected templates before we try to unify, in case the
20044 explicit args will completely resolve the templates in question. */
20045
20046 tree expl_subargs = TREE_OPERAND (expr, 1);
20047 tree arg = TREE_OPERAND (expr, 0);
20048 tree badfn = NULL_TREE;
20049 tree badargs = NULL_TREE;
20050
20051 for (lkp_iterator iter (arg); iter; ++iter)
20052 {
20053 tree fn = *iter;
20054 tree subargs, elem;
20055
20056 if (TREE_CODE (fn) != TEMPLATE_DECL)
20057 continue;
20058
20059 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20060 expl_subargs, NULL_TREE, tf_none,
20061 /*require_all_args=*/true,
20062 /*use_default_args=*/true);
20063 if (subargs != error_mark_node
20064 && !any_dependent_template_arguments_p (subargs))
20065 {
20066 elem = instantiate_template (fn, subargs, tf_none);
20067 if (elem == error_mark_node)
20068 {
20069 badfn = fn;
20070 badargs = subargs;
20071 }
20072 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20073 {
20074 goodfn = elem;
20075 ++good;
20076 }
20077 }
20078 }
20079 if (good == 1)
20080 {
20081 mark_used (goodfn);
20082 expr = goodfn;
20083 if (baselink)
20084 expr = build_baselink (BASELINK_BINFO (baselink),
20085 BASELINK_ACCESS_BINFO (baselink),
20086 expr, BASELINK_OPTYPE (baselink));
20087 if (offset)
20088 {
20089 tree base
20090 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20091 expr = build_offset_ref (base, expr, addr, complain);
20092 }
20093 if (addr)
20094 expr = cp_build_addr_expr (expr, complain);
20095 return expr;
20096 }
20097 else if (good == 0 && badargs && (complain & tf_error))
20098 /* There were no good options and at least one bad one, so let the
20099 user know what the problem is. */
20100 instantiate_template (badfn, badargs, complain);
20101 }
20102 return orig_expr;
20103 }
20104
20105 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20106 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20107 different overloads deduce different arguments for a given parm.
20108 ADDR_P is true if the expression for which deduction is being
20109 performed was of the form "& fn" rather than simply "fn".
20110
20111 Returns 1 on success. */
20112
20113 static int
20114 try_one_overload (tree tparms,
20115 tree orig_targs,
20116 tree targs,
20117 tree parm,
20118 tree arg,
20119 unification_kind_t strict,
20120 int sub_strict,
20121 bool addr_p,
20122 bool explain_p)
20123 {
20124 int nargs;
20125 tree tempargs;
20126 int i;
20127
20128 if (arg == error_mark_node)
20129 return 0;
20130
20131 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20132 to function or pointer to member function argument if the set of
20133 overloaded functions does not contain function templates and at most
20134 one of a set of overloaded functions provides a unique match.
20135
20136 So if this is a template, just return success. */
20137
20138 if (uses_template_parms (arg))
20139 return 1;
20140
20141 if (TREE_CODE (arg) == METHOD_TYPE)
20142 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20143 else if (addr_p)
20144 arg = build_pointer_type (arg);
20145
20146 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20147
20148 /* We don't copy orig_targs for this because if we have already deduced
20149 some template args from previous args, unify would complain when we
20150 try to deduce a template parameter for the same argument, even though
20151 there isn't really a conflict. */
20152 nargs = TREE_VEC_LENGTH (targs);
20153 tempargs = make_tree_vec (nargs);
20154
20155 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20156 return 0;
20157
20158 /* First make sure we didn't deduce anything that conflicts with
20159 explicitly specified args. */
20160 for (i = nargs; i--; )
20161 {
20162 tree elt = TREE_VEC_ELT (tempargs, i);
20163 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20164
20165 if (!elt)
20166 /*NOP*/;
20167 else if (uses_template_parms (elt))
20168 /* Since we're unifying against ourselves, we will fill in
20169 template args used in the function parm list with our own
20170 template parms. Discard them. */
20171 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20172 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20173 {
20174 /* Check that the argument at each index of the deduced argument pack
20175 is equivalent to the corresponding explicitly specified argument.
20176 We may have deduced more arguments than were explicitly specified,
20177 and that's OK. */
20178
20179 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20180 that's wrong if we deduce the same argument pack from multiple
20181 function arguments: it's only incomplete the first time. */
20182
20183 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20184 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20185
20186 if (TREE_VEC_LENGTH (deduced_pack)
20187 < TREE_VEC_LENGTH (explicit_pack))
20188 return 0;
20189
20190 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20191 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20192 TREE_VEC_ELT (deduced_pack, j)))
20193 return 0;
20194 }
20195 else if (oldelt && !template_args_equal (oldelt, elt))
20196 return 0;
20197 }
20198
20199 for (i = nargs; i--; )
20200 {
20201 tree elt = TREE_VEC_ELT (tempargs, i);
20202
20203 if (elt)
20204 TREE_VEC_ELT (targs, i) = elt;
20205 }
20206
20207 return 1;
20208 }
20209
20210 /* PARM is a template class (perhaps with unbound template
20211 parameters). ARG is a fully instantiated type. If ARG can be
20212 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20213 TARGS are as for unify. */
20214
20215 static tree
20216 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20217 bool explain_p)
20218 {
20219 tree copy_of_targs;
20220
20221 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20222 return NULL_TREE;
20223 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20224 /* Matches anything. */;
20225 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20226 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20227 return NULL_TREE;
20228
20229 /* We need to make a new template argument vector for the call to
20230 unify. If we used TARGS, we'd clutter it up with the result of
20231 the attempted unification, even if this class didn't work out.
20232 We also don't want to commit ourselves to all the unifications
20233 we've already done, since unification is supposed to be done on
20234 an argument-by-argument basis. In other words, consider the
20235 following pathological case:
20236
20237 template <int I, int J, int K>
20238 struct S {};
20239
20240 template <int I, int J>
20241 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20242
20243 template <int I, int J, int K>
20244 void f(S<I, J, K>, S<I, I, I>);
20245
20246 void g() {
20247 S<0, 0, 0> s0;
20248 S<0, 1, 2> s2;
20249
20250 f(s0, s2);
20251 }
20252
20253 Now, by the time we consider the unification involving `s2', we
20254 already know that we must have `f<0, 0, 0>'. But, even though
20255 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20256 because there are two ways to unify base classes of S<0, 1, 2>
20257 with S<I, I, I>. If we kept the already deduced knowledge, we
20258 would reject the possibility I=1. */
20259 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20260
20261 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20262 {
20263 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20264 return NULL_TREE;
20265 return arg;
20266 }
20267
20268 /* If unification failed, we're done. */
20269 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20270 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20271 return NULL_TREE;
20272
20273 return arg;
20274 }
20275
20276 /* Given a template type PARM and a class type ARG, find the unique
20277 base type in ARG that is an instance of PARM. We do not examine
20278 ARG itself; only its base-classes. If there is not exactly one
20279 appropriate base class, return NULL_TREE. PARM may be the type of
20280 a partial specialization, as well as a plain template type. Used
20281 by unify. */
20282
20283 static enum template_base_result
20284 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20285 bool explain_p, tree *result)
20286 {
20287 tree rval = NULL_TREE;
20288 tree binfo;
20289
20290 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20291
20292 binfo = TYPE_BINFO (complete_type (arg));
20293 if (!binfo)
20294 {
20295 /* The type could not be completed. */
20296 *result = NULL_TREE;
20297 return tbr_incomplete_type;
20298 }
20299
20300 /* Walk in inheritance graph order. The search order is not
20301 important, and this avoids multiple walks of virtual bases. */
20302 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20303 {
20304 tree r = try_class_unification (tparms, targs, parm,
20305 BINFO_TYPE (binfo), explain_p);
20306
20307 if (r)
20308 {
20309 /* If there is more than one satisfactory baseclass, then:
20310
20311 [temp.deduct.call]
20312
20313 If they yield more than one possible deduced A, the type
20314 deduction fails.
20315
20316 applies. */
20317 if (rval && !same_type_p (r, rval))
20318 {
20319 *result = NULL_TREE;
20320 return tbr_ambiguous_baseclass;
20321 }
20322
20323 rval = r;
20324 }
20325 }
20326
20327 *result = rval;
20328 return tbr_success;
20329 }
20330
20331 /* Returns the level of DECL, which declares a template parameter. */
20332
20333 static int
20334 template_decl_level (tree decl)
20335 {
20336 switch (TREE_CODE (decl))
20337 {
20338 case TYPE_DECL:
20339 case TEMPLATE_DECL:
20340 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20341
20342 case PARM_DECL:
20343 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20344
20345 default:
20346 gcc_unreachable ();
20347 }
20348 return 0;
20349 }
20350
20351 /* Decide whether ARG can be unified with PARM, considering only the
20352 cv-qualifiers of each type, given STRICT as documented for unify.
20353 Returns nonzero iff the unification is OK on that basis. */
20354
20355 static int
20356 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20357 {
20358 int arg_quals = cp_type_quals (arg);
20359 int parm_quals = cp_type_quals (parm);
20360
20361 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20362 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20363 {
20364 /* Although a CVR qualifier is ignored when being applied to a
20365 substituted template parameter ([8.3.2]/1 for example), that
20366 does not allow us to unify "const T" with "int&" because both
20367 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20368 It is ok when we're allowing additional CV qualifiers
20369 at the outer level [14.8.2.1]/3,1st bullet. */
20370 if ((TREE_CODE (arg) == REFERENCE_TYPE
20371 || TREE_CODE (arg) == FUNCTION_TYPE
20372 || TREE_CODE (arg) == METHOD_TYPE)
20373 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20374 return 0;
20375
20376 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20377 && (parm_quals & TYPE_QUAL_RESTRICT))
20378 return 0;
20379 }
20380
20381 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20382 && (arg_quals & parm_quals) != parm_quals)
20383 return 0;
20384
20385 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20386 && (parm_quals & arg_quals) != arg_quals)
20387 return 0;
20388
20389 return 1;
20390 }
20391
20392 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20393 void
20394 template_parm_level_and_index (tree parm, int* level, int* index)
20395 {
20396 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20397 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20398 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20399 {
20400 *index = TEMPLATE_TYPE_IDX (parm);
20401 *level = TEMPLATE_TYPE_LEVEL (parm);
20402 }
20403 else
20404 {
20405 *index = TEMPLATE_PARM_IDX (parm);
20406 *level = TEMPLATE_PARM_LEVEL (parm);
20407 }
20408 }
20409
20410 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20411 do { \
20412 if (unify (TP, TA, P, A, S, EP)) \
20413 return 1; \
20414 } while (0)
20415
20416 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20417 expansion at the end of PACKED_PARMS. Returns 0 if the type
20418 deduction succeeds, 1 otherwise. STRICT is the same as in
20419 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20420 function call argument list. We'll need to adjust the arguments to make them
20421 types. SUBR tells us if this is from a recursive call to
20422 type_unification_real, or for comparing two template argument
20423 lists. */
20424
20425 static int
20426 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20427 tree packed_args, unification_kind_t strict,
20428 bool subr, bool explain_p)
20429 {
20430 tree parm
20431 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20432 tree pattern = PACK_EXPANSION_PATTERN (parm);
20433 tree pack, packs = NULL_TREE;
20434 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20435
20436 /* Add in any args remembered from an earlier partial instantiation. */
20437 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20438 int levels = TMPL_ARGS_DEPTH (targs);
20439
20440 packed_args = expand_template_argument_pack (packed_args);
20441
20442 int len = TREE_VEC_LENGTH (packed_args);
20443
20444 /* Determine the parameter packs we will be deducing from the
20445 pattern, and record their current deductions. */
20446 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20447 pack; pack = TREE_CHAIN (pack))
20448 {
20449 tree parm_pack = TREE_VALUE (pack);
20450 int idx, level;
20451
20452 /* Determine the index and level of this parameter pack. */
20453 template_parm_level_and_index (parm_pack, &level, &idx);
20454 if (level < levels)
20455 continue;
20456
20457 /* Keep track of the parameter packs and their corresponding
20458 argument packs. */
20459 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20460 TREE_TYPE (packs) = make_tree_vec (len - start);
20461 }
20462
20463 /* Loop through all of the arguments that have not yet been
20464 unified and unify each with the pattern. */
20465 for (i = start; i < len; i++)
20466 {
20467 tree parm;
20468 bool any_explicit = false;
20469 tree arg = TREE_VEC_ELT (packed_args, i);
20470
20471 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20472 or the element of its argument pack at the current index if
20473 this argument was explicitly specified. */
20474 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20475 {
20476 int idx, level;
20477 tree arg, pargs;
20478 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20479
20480 arg = NULL_TREE;
20481 if (TREE_VALUE (pack)
20482 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20483 && (i - start < TREE_VEC_LENGTH (pargs)))
20484 {
20485 any_explicit = true;
20486 arg = TREE_VEC_ELT (pargs, i - start);
20487 }
20488 TMPL_ARG (targs, level, idx) = arg;
20489 }
20490
20491 /* If we had explicit template arguments, substitute them into the
20492 pattern before deduction. */
20493 if (any_explicit)
20494 {
20495 /* Some arguments might still be unspecified or dependent. */
20496 bool dependent;
20497 ++processing_template_decl;
20498 dependent = any_dependent_template_arguments_p (targs);
20499 if (!dependent)
20500 --processing_template_decl;
20501 parm = tsubst (pattern, targs,
20502 explain_p ? tf_warning_or_error : tf_none,
20503 NULL_TREE);
20504 if (dependent)
20505 --processing_template_decl;
20506 if (parm == error_mark_node)
20507 return 1;
20508 }
20509 else
20510 parm = pattern;
20511
20512 /* Unify the pattern with the current argument. */
20513 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20514 explain_p))
20515 return 1;
20516
20517 /* For each parameter pack, collect the deduced value. */
20518 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20519 {
20520 int idx, level;
20521 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20522
20523 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20524 TMPL_ARG (targs, level, idx);
20525 }
20526 }
20527
20528 /* Verify that the results of unification with the parameter packs
20529 produce results consistent with what we've seen before, and make
20530 the deduced argument packs available. */
20531 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20532 {
20533 tree old_pack = TREE_VALUE (pack);
20534 tree new_args = TREE_TYPE (pack);
20535 int i, len = TREE_VEC_LENGTH (new_args);
20536 int idx, level;
20537 bool nondeduced_p = false;
20538
20539 /* By default keep the original deduced argument pack.
20540 If necessary, more specific code is going to update the
20541 resulting deduced argument later down in this function. */
20542 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20543 TMPL_ARG (targs, level, idx) = old_pack;
20544
20545 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20546 actually deduce anything. */
20547 for (i = 0; i < len && !nondeduced_p; ++i)
20548 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20549 nondeduced_p = true;
20550 if (nondeduced_p)
20551 continue;
20552
20553 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20554 {
20555 /* If we had fewer function args than explicit template args,
20556 just use the explicits. */
20557 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20558 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20559 if (len < explicit_len)
20560 new_args = explicit_args;
20561 }
20562
20563 if (!old_pack)
20564 {
20565 tree result;
20566 /* Build the deduced *_ARGUMENT_PACK. */
20567 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20568 {
20569 result = make_node (NONTYPE_ARGUMENT_PACK);
20570 TREE_CONSTANT (result) = 1;
20571 }
20572 else
20573 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20574
20575 SET_ARGUMENT_PACK_ARGS (result, new_args);
20576
20577 /* Note the deduced argument packs for this parameter
20578 pack. */
20579 TMPL_ARG (targs, level, idx) = result;
20580 }
20581 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20582 && (ARGUMENT_PACK_ARGS (old_pack)
20583 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20584 {
20585 /* We only had the explicitly-provided arguments before, but
20586 now we have a complete set of arguments. */
20587 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20588
20589 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20590 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20591 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20592 }
20593 else
20594 {
20595 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20596 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20597
20598 if (!comp_template_args (old_args, new_args,
20599 &bad_old_arg, &bad_new_arg))
20600 /* Inconsistent unification of this parameter pack. */
20601 return unify_parameter_pack_inconsistent (explain_p,
20602 bad_old_arg,
20603 bad_new_arg);
20604 }
20605 }
20606
20607 return unify_success (explain_p);
20608 }
20609
20610 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20611 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20612 parameters and return value are as for unify. */
20613
20614 static int
20615 unify_array_domain (tree tparms, tree targs,
20616 tree parm_dom, tree arg_dom,
20617 bool explain_p)
20618 {
20619 tree parm_max;
20620 tree arg_max;
20621 bool parm_cst;
20622 bool arg_cst;
20623
20624 /* Our representation of array types uses "N - 1" as the
20625 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20626 not an integer constant. We cannot unify arbitrarily
20627 complex expressions, so we eliminate the MINUS_EXPRs
20628 here. */
20629 parm_max = TYPE_MAX_VALUE (parm_dom);
20630 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20631 if (!parm_cst)
20632 {
20633 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20634 parm_max = TREE_OPERAND (parm_max, 0);
20635 }
20636 arg_max = TYPE_MAX_VALUE (arg_dom);
20637 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20638 if (!arg_cst)
20639 {
20640 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20641 trying to unify the type of a variable with the type
20642 of a template parameter. For example:
20643
20644 template <unsigned int N>
20645 void f (char (&) [N]);
20646 int g();
20647 void h(int i) {
20648 char a[g(i)];
20649 f(a);
20650 }
20651
20652 Here, the type of the ARG will be "int [g(i)]", and
20653 may be a SAVE_EXPR, etc. */
20654 if (TREE_CODE (arg_max) != MINUS_EXPR)
20655 return unify_vla_arg (explain_p, arg_dom);
20656 arg_max = TREE_OPERAND (arg_max, 0);
20657 }
20658
20659 /* If only one of the bounds used a MINUS_EXPR, compensate
20660 by adding one to the other bound. */
20661 if (parm_cst && !arg_cst)
20662 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20663 integer_type_node,
20664 parm_max,
20665 integer_one_node);
20666 else if (arg_cst && !parm_cst)
20667 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20668 integer_type_node,
20669 arg_max,
20670 integer_one_node);
20671
20672 return unify (tparms, targs, parm_max, arg_max,
20673 UNIFY_ALLOW_INTEGER, explain_p);
20674 }
20675
20676 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20677
20678 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20679
20680 static pa_kind_t
20681 pa_kind (tree t)
20682 {
20683 if (PACK_EXPANSION_P (t))
20684 t = PACK_EXPANSION_PATTERN (t);
20685 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20686 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20687 || DECL_TYPE_TEMPLATE_P (t))
20688 return pa_tmpl;
20689 else if (TYPE_P (t))
20690 return pa_type;
20691 else
20692 return pa_expr;
20693 }
20694
20695 /* Deduce the value of template parameters. TPARMS is the (innermost)
20696 set of template parameters to a template. TARGS is the bindings
20697 for those template parameters, as determined thus far; TARGS may
20698 include template arguments for outer levels of template parameters
20699 as well. PARM is a parameter to a template function, or a
20700 subcomponent of that parameter; ARG is the corresponding argument.
20701 This function attempts to match PARM with ARG in a manner
20702 consistent with the existing assignments in TARGS. If more values
20703 are deduced, then TARGS is updated.
20704
20705 Returns 0 if the type deduction succeeds, 1 otherwise. The
20706 parameter STRICT is a bitwise or of the following flags:
20707
20708 UNIFY_ALLOW_NONE:
20709 Require an exact match between PARM and ARG.
20710 UNIFY_ALLOW_MORE_CV_QUAL:
20711 Allow the deduced ARG to be more cv-qualified (by qualification
20712 conversion) than ARG.
20713 UNIFY_ALLOW_LESS_CV_QUAL:
20714 Allow the deduced ARG to be less cv-qualified than ARG.
20715 UNIFY_ALLOW_DERIVED:
20716 Allow the deduced ARG to be a template base class of ARG,
20717 or a pointer to a template base class of the type pointed to by
20718 ARG.
20719 UNIFY_ALLOW_INTEGER:
20720 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20721 case for more information.
20722 UNIFY_ALLOW_OUTER_LEVEL:
20723 This is the outermost level of a deduction. Used to determine validity
20724 of qualification conversions. A valid qualification conversion must
20725 have const qualified pointers leading up to the inner type which
20726 requires additional CV quals, except at the outer level, where const
20727 is not required [conv.qual]. It would be normal to set this flag in
20728 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20729 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20730 This is the outermost level of a deduction, and PARM can be more CV
20731 qualified at this point.
20732 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20733 This is the outermost level of a deduction, and PARM can be less CV
20734 qualified at this point. */
20735
20736 static int
20737 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20738 bool explain_p)
20739 {
20740 int idx;
20741 tree targ;
20742 tree tparm;
20743 int strict_in = strict;
20744 tsubst_flags_t complain = (explain_p
20745 ? tf_warning_or_error
20746 : tf_none);
20747
20748 /* I don't think this will do the right thing with respect to types.
20749 But the only case I've seen it in so far has been array bounds, where
20750 signedness is the only information lost, and I think that will be
20751 okay. */
20752 while (CONVERT_EXPR_P (parm))
20753 parm = TREE_OPERAND (parm, 0);
20754
20755 if (arg == error_mark_node)
20756 return unify_invalid (explain_p);
20757 if (arg == unknown_type_node
20758 || arg == init_list_type_node)
20759 /* We can't deduce anything from this, but we might get all the
20760 template args from other function args. */
20761 return unify_success (explain_p);
20762
20763 if (parm == any_targ_node || arg == any_targ_node)
20764 return unify_success (explain_p);
20765
20766 /* If PARM uses template parameters, then we can't bail out here,
20767 even if ARG == PARM, since we won't record unifications for the
20768 template parameters. We might need them if we're trying to
20769 figure out which of two things is more specialized. */
20770 if (arg == parm && !uses_template_parms (parm))
20771 return unify_success (explain_p);
20772
20773 /* Handle init lists early, so the rest of the function can assume
20774 we're dealing with a type. */
20775 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20776 {
20777 tree elt, elttype;
20778 unsigned i;
20779 tree orig_parm = parm;
20780
20781 /* Replace T with std::initializer_list<T> for deduction. */
20782 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20783 && flag_deduce_init_list)
20784 parm = listify (parm);
20785
20786 if (!is_std_init_list (parm)
20787 && TREE_CODE (parm) != ARRAY_TYPE)
20788 /* We can only deduce from an initializer list argument if the
20789 parameter is std::initializer_list or an array; otherwise this
20790 is a non-deduced context. */
20791 return unify_success (explain_p);
20792
20793 if (TREE_CODE (parm) == ARRAY_TYPE)
20794 elttype = TREE_TYPE (parm);
20795 else
20796 {
20797 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20798 /* Deduction is defined in terms of a single type, so just punt
20799 on the (bizarre) std::initializer_list<T...>. */
20800 if (PACK_EXPANSION_P (elttype))
20801 return unify_success (explain_p);
20802 }
20803
20804 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20805 {
20806 int elt_strict = strict;
20807
20808 if (elt == error_mark_node)
20809 return unify_invalid (explain_p);
20810
20811 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20812 {
20813 tree type = TREE_TYPE (elt);
20814 if (type == error_mark_node)
20815 return unify_invalid (explain_p);
20816 /* It should only be possible to get here for a call. */
20817 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20818 elt_strict |= maybe_adjust_types_for_deduction
20819 (DEDUCE_CALL, &elttype, &type, elt);
20820 elt = type;
20821 }
20822
20823 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20824 explain_p);
20825 }
20826
20827 if (TREE_CODE (parm) == ARRAY_TYPE
20828 && deducible_array_bound (TYPE_DOMAIN (parm)))
20829 {
20830 /* Also deduce from the length of the initializer list. */
20831 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20832 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20833 if (idx == error_mark_node)
20834 return unify_invalid (explain_p);
20835 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20836 idx, explain_p);
20837 }
20838
20839 /* If the std::initializer_list<T> deduction worked, replace the
20840 deduced A with std::initializer_list<A>. */
20841 if (orig_parm != parm)
20842 {
20843 idx = TEMPLATE_TYPE_IDX (orig_parm);
20844 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20845 targ = listify (targ);
20846 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20847 }
20848 return unify_success (explain_p);
20849 }
20850
20851 /* If parm and arg aren't the same kind of thing (template, type, or
20852 expression), fail early. */
20853 if (pa_kind (parm) != pa_kind (arg))
20854 return unify_invalid (explain_p);
20855
20856 /* Immediately reject some pairs that won't unify because of
20857 cv-qualification mismatches. */
20858 if (TREE_CODE (arg) == TREE_CODE (parm)
20859 && TYPE_P (arg)
20860 /* It is the elements of the array which hold the cv quals of an array
20861 type, and the elements might be template type parms. We'll check
20862 when we recurse. */
20863 && TREE_CODE (arg) != ARRAY_TYPE
20864 /* We check the cv-qualifiers when unifying with template type
20865 parameters below. We want to allow ARG `const T' to unify with
20866 PARM `T' for example, when computing which of two templates
20867 is more specialized, for example. */
20868 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20869 && !check_cv_quals_for_unify (strict_in, arg, parm))
20870 return unify_cv_qual_mismatch (explain_p, parm, arg);
20871
20872 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20873 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20874 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20875 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20876 strict &= ~UNIFY_ALLOW_DERIVED;
20877 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20878 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20879
20880 switch (TREE_CODE (parm))
20881 {
20882 case TYPENAME_TYPE:
20883 case SCOPE_REF:
20884 case UNBOUND_CLASS_TEMPLATE:
20885 /* In a type which contains a nested-name-specifier, template
20886 argument values cannot be deduced for template parameters used
20887 within the nested-name-specifier. */
20888 return unify_success (explain_p);
20889
20890 case TEMPLATE_TYPE_PARM:
20891 case TEMPLATE_TEMPLATE_PARM:
20892 case BOUND_TEMPLATE_TEMPLATE_PARM:
20893 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20894 if (error_operand_p (tparm))
20895 return unify_invalid (explain_p);
20896
20897 if (TEMPLATE_TYPE_LEVEL (parm)
20898 != template_decl_level (tparm))
20899 /* The PARM is not one we're trying to unify. Just check
20900 to see if it matches ARG. */
20901 {
20902 if (TREE_CODE (arg) == TREE_CODE (parm)
20903 && (is_auto (parm) ? is_auto (arg)
20904 : same_type_p (parm, arg)))
20905 return unify_success (explain_p);
20906 else
20907 return unify_type_mismatch (explain_p, parm, arg);
20908 }
20909 idx = TEMPLATE_TYPE_IDX (parm);
20910 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20911 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20912 if (error_operand_p (tparm))
20913 return unify_invalid (explain_p);
20914
20915 /* Check for mixed types and values. */
20916 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20917 && TREE_CODE (tparm) != TYPE_DECL)
20918 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20919 && TREE_CODE (tparm) != TEMPLATE_DECL))
20920 gcc_unreachable ();
20921
20922 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20923 {
20924 if ((strict_in & UNIFY_ALLOW_DERIVED)
20925 && CLASS_TYPE_P (arg))
20926 {
20927 /* First try to match ARG directly. */
20928 tree t = try_class_unification (tparms, targs, parm, arg,
20929 explain_p);
20930 if (!t)
20931 {
20932 /* Otherwise, look for a suitable base of ARG, as below. */
20933 enum template_base_result r;
20934 r = get_template_base (tparms, targs, parm, arg,
20935 explain_p, &t);
20936 if (!t)
20937 return unify_no_common_base (explain_p, r, parm, arg);
20938 arg = t;
20939 }
20940 }
20941 /* ARG must be constructed from a template class or a template
20942 template parameter. */
20943 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20944 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20945 return unify_template_deduction_failure (explain_p, parm, arg);
20946
20947 /* Deduce arguments T, i from TT<T> or TT<i>. */
20948 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20949 return 1;
20950
20951 arg = TYPE_TI_TEMPLATE (arg);
20952
20953 /* Fall through to deduce template name. */
20954 }
20955
20956 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20957 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20958 {
20959 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20960
20961 /* Simple cases: Value already set, does match or doesn't. */
20962 if (targ != NULL_TREE && template_args_equal (targ, arg))
20963 return unify_success (explain_p);
20964 else if (targ)
20965 return unify_inconsistency (explain_p, parm, targ, arg);
20966 }
20967 else
20968 {
20969 /* If PARM is `const T' and ARG is only `int', we don't have
20970 a match unless we are allowing additional qualification.
20971 If ARG is `const int' and PARM is just `T' that's OK;
20972 that binds `const int' to `T'. */
20973 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20974 arg, parm))
20975 return unify_cv_qual_mismatch (explain_p, parm, arg);
20976
20977 /* Consider the case where ARG is `const volatile int' and
20978 PARM is `const T'. Then, T should be `volatile int'. */
20979 arg = cp_build_qualified_type_real
20980 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20981 if (arg == error_mark_node)
20982 return unify_invalid (explain_p);
20983
20984 /* Simple cases: Value already set, does match or doesn't. */
20985 if (targ != NULL_TREE && same_type_p (targ, arg))
20986 return unify_success (explain_p);
20987 else if (targ)
20988 return unify_inconsistency (explain_p, parm, targ, arg);
20989
20990 /* Make sure that ARG is not a variable-sized array. (Note
20991 that were talking about variable-sized arrays (like
20992 `int[n]'), rather than arrays of unknown size (like
20993 `int[]').) We'll get very confused by such a type since
20994 the bound of the array is not constant, and therefore
20995 not mangleable. Besides, such types are not allowed in
20996 ISO C++, so we can do as we please here. We do allow
20997 them for 'auto' deduction, since that isn't ABI-exposed. */
20998 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20999 return unify_vla_arg (explain_p, arg);
21000
21001 /* Strip typedefs as in convert_template_argument. */
21002 arg = canonicalize_type_argument (arg, tf_none);
21003 }
21004
21005 /* If ARG is a parameter pack or an expansion, we cannot unify
21006 against it unless PARM is also a parameter pack. */
21007 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21008 && !template_parameter_pack_p (parm))
21009 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21010
21011 /* If the argument deduction results is a METHOD_TYPE,
21012 then there is a problem.
21013 METHOD_TYPE doesn't map to any real C++ type the result of
21014 the deduction can not be of that type. */
21015 if (TREE_CODE (arg) == METHOD_TYPE)
21016 return unify_method_type_error (explain_p, arg);
21017
21018 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21019 return unify_success (explain_p);
21020
21021 case TEMPLATE_PARM_INDEX:
21022 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21023 if (error_operand_p (tparm))
21024 return unify_invalid (explain_p);
21025
21026 if (TEMPLATE_PARM_LEVEL (parm)
21027 != template_decl_level (tparm))
21028 {
21029 /* The PARM is not one we're trying to unify. Just check
21030 to see if it matches ARG. */
21031 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21032 && cp_tree_equal (parm, arg));
21033 if (result)
21034 unify_expression_unequal (explain_p, parm, arg);
21035 return result;
21036 }
21037
21038 idx = TEMPLATE_PARM_IDX (parm);
21039 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21040
21041 if (targ)
21042 {
21043 if ((strict & UNIFY_ALLOW_INTEGER)
21044 && TREE_TYPE (targ) && TREE_TYPE (arg)
21045 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21046 /* We're deducing from an array bound, the type doesn't matter. */
21047 arg = fold_convert (TREE_TYPE (targ), arg);
21048 int x = !cp_tree_equal (targ, arg);
21049 if (x)
21050 unify_inconsistency (explain_p, parm, targ, arg);
21051 return x;
21052 }
21053
21054 /* [temp.deduct.type] If, in the declaration of a function template
21055 with a non-type template-parameter, the non-type
21056 template-parameter is used in an expression in the function
21057 parameter-list and, if the corresponding template-argument is
21058 deduced, the template-argument type shall match the type of the
21059 template-parameter exactly, except that a template-argument
21060 deduced from an array bound may be of any integral type.
21061 The non-type parameter might use already deduced type parameters. */
21062 ++processing_template_decl;
21063 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21064 --processing_template_decl;
21065 if (tree a = type_uses_auto (tparm))
21066 {
21067 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21068 if (tparm == error_mark_node)
21069 return 1;
21070 }
21071
21072 if (!TREE_TYPE (arg))
21073 /* Template-parameter dependent expression. Just accept it for now.
21074 It will later be processed in convert_template_argument. */
21075 ;
21076 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21077 non_reference (tparm)))
21078 /* OK */;
21079 else if ((strict & UNIFY_ALLOW_INTEGER)
21080 && CP_INTEGRAL_TYPE_P (tparm))
21081 /* Convert the ARG to the type of PARM; the deduced non-type
21082 template argument must exactly match the types of the
21083 corresponding parameter. */
21084 arg = fold (build_nop (tparm, arg));
21085 else if (uses_template_parms (tparm))
21086 {
21087 /* We haven't deduced the type of this parameter yet. */
21088 if (cxx_dialect >= cxx17
21089 /* We deduce from array bounds in try_array_deduction. */
21090 && !(strict & UNIFY_ALLOW_INTEGER))
21091 {
21092 /* Deduce it from the non-type argument. */
21093 tree atype = TREE_TYPE (arg);
21094 RECUR_AND_CHECK_FAILURE (tparms, targs,
21095 tparm, atype,
21096 UNIFY_ALLOW_NONE, explain_p);
21097 }
21098 else
21099 /* Try again later. */
21100 return unify_success (explain_p);
21101 }
21102 else
21103 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21104
21105 /* If ARG is a parameter pack or an expansion, we cannot unify
21106 against it unless PARM is also a parameter pack. */
21107 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21108 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21109 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21110
21111 {
21112 bool removed_attr = false;
21113 arg = strip_typedefs_expr (arg, &removed_attr);
21114 }
21115 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21116 return unify_success (explain_p);
21117
21118 case PTRMEM_CST:
21119 {
21120 /* A pointer-to-member constant can be unified only with
21121 another constant. */
21122 if (TREE_CODE (arg) != PTRMEM_CST)
21123 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21124
21125 /* Just unify the class member. It would be useless (and possibly
21126 wrong, depending on the strict flags) to unify also
21127 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21128 arg refer to the same variable, even if through different
21129 classes. For instance:
21130
21131 struct A { int x; };
21132 struct B : A { };
21133
21134 Unification of &A::x and &B::x must succeed. */
21135 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21136 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21137 }
21138
21139 case POINTER_TYPE:
21140 {
21141 if (!TYPE_PTR_P (arg))
21142 return unify_type_mismatch (explain_p, parm, arg);
21143
21144 /* [temp.deduct.call]
21145
21146 A can be another pointer or pointer to member type that can
21147 be converted to the deduced A via a qualification
21148 conversion (_conv.qual_).
21149
21150 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21151 This will allow for additional cv-qualification of the
21152 pointed-to types if appropriate. */
21153
21154 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21155 /* The derived-to-base conversion only persists through one
21156 level of pointers. */
21157 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21158
21159 return unify (tparms, targs, TREE_TYPE (parm),
21160 TREE_TYPE (arg), strict, explain_p);
21161 }
21162
21163 case REFERENCE_TYPE:
21164 if (TREE_CODE (arg) != REFERENCE_TYPE)
21165 return unify_type_mismatch (explain_p, parm, arg);
21166 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21167 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21168
21169 case ARRAY_TYPE:
21170 if (TREE_CODE (arg) != ARRAY_TYPE)
21171 return unify_type_mismatch (explain_p, parm, arg);
21172 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21173 != (TYPE_DOMAIN (arg) == NULL_TREE))
21174 return unify_type_mismatch (explain_p, parm, arg);
21175 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21176 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21177 if (TYPE_DOMAIN (parm) != NULL_TREE)
21178 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21179 TYPE_DOMAIN (arg), explain_p);
21180 return unify_success (explain_p);
21181
21182 case REAL_TYPE:
21183 case COMPLEX_TYPE:
21184 case VECTOR_TYPE:
21185 case INTEGER_TYPE:
21186 case BOOLEAN_TYPE:
21187 case ENUMERAL_TYPE:
21188 case VOID_TYPE:
21189 case NULLPTR_TYPE:
21190 if (TREE_CODE (arg) != TREE_CODE (parm))
21191 return unify_type_mismatch (explain_p, parm, arg);
21192
21193 /* We have already checked cv-qualification at the top of the
21194 function. */
21195 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21196 return unify_type_mismatch (explain_p, parm, arg);
21197
21198 /* As far as unification is concerned, this wins. Later checks
21199 will invalidate it if necessary. */
21200 return unify_success (explain_p);
21201
21202 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21203 /* Type INTEGER_CST can come from ordinary constant template args. */
21204 case INTEGER_CST:
21205 while (CONVERT_EXPR_P (arg))
21206 arg = TREE_OPERAND (arg, 0);
21207
21208 if (TREE_CODE (arg) != INTEGER_CST)
21209 return unify_template_argument_mismatch (explain_p, parm, arg);
21210 return (tree_int_cst_equal (parm, arg)
21211 ? unify_success (explain_p)
21212 : unify_template_argument_mismatch (explain_p, parm, arg));
21213
21214 case TREE_VEC:
21215 {
21216 int i, len, argslen;
21217 int parm_variadic_p = 0;
21218
21219 if (TREE_CODE (arg) != TREE_VEC)
21220 return unify_template_argument_mismatch (explain_p, parm, arg);
21221
21222 len = TREE_VEC_LENGTH (parm);
21223 argslen = TREE_VEC_LENGTH (arg);
21224
21225 /* Check for pack expansions in the parameters. */
21226 for (i = 0; i < len; ++i)
21227 {
21228 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21229 {
21230 if (i == len - 1)
21231 /* We can unify against something with a trailing
21232 parameter pack. */
21233 parm_variadic_p = 1;
21234 else
21235 /* [temp.deduct.type]/9: If the template argument list of
21236 P contains a pack expansion that is not the last
21237 template argument, the entire template argument list
21238 is a non-deduced context. */
21239 return unify_success (explain_p);
21240 }
21241 }
21242
21243 /* If we don't have enough arguments to satisfy the parameters
21244 (not counting the pack expression at the end), or we have
21245 too many arguments for a parameter list that doesn't end in
21246 a pack expression, we can't unify. */
21247 if (parm_variadic_p
21248 ? argslen < len - parm_variadic_p
21249 : argslen != len)
21250 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21251
21252 /* Unify all of the parameters that precede the (optional)
21253 pack expression. */
21254 for (i = 0; i < len - parm_variadic_p; ++i)
21255 {
21256 RECUR_AND_CHECK_FAILURE (tparms, targs,
21257 TREE_VEC_ELT (parm, i),
21258 TREE_VEC_ELT (arg, i),
21259 UNIFY_ALLOW_NONE, explain_p);
21260 }
21261 if (parm_variadic_p)
21262 return unify_pack_expansion (tparms, targs, parm, arg,
21263 DEDUCE_EXACT,
21264 /*subr=*/true, explain_p);
21265 return unify_success (explain_p);
21266 }
21267
21268 case RECORD_TYPE:
21269 case UNION_TYPE:
21270 if (TREE_CODE (arg) != TREE_CODE (parm))
21271 return unify_type_mismatch (explain_p, parm, arg);
21272
21273 if (TYPE_PTRMEMFUNC_P (parm))
21274 {
21275 if (!TYPE_PTRMEMFUNC_P (arg))
21276 return unify_type_mismatch (explain_p, parm, arg);
21277
21278 return unify (tparms, targs,
21279 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21280 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21281 strict, explain_p);
21282 }
21283 else if (TYPE_PTRMEMFUNC_P (arg))
21284 return unify_type_mismatch (explain_p, parm, arg);
21285
21286 if (CLASSTYPE_TEMPLATE_INFO (parm))
21287 {
21288 tree t = NULL_TREE;
21289
21290 if (strict_in & UNIFY_ALLOW_DERIVED)
21291 {
21292 /* First, we try to unify the PARM and ARG directly. */
21293 t = try_class_unification (tparms, targs,
21294 parm, arg, explain_p);
21295
21296 if (!t)
21297 {
21298 /* Fallback to the special case allowed in
21299 [temp.deduct.call]:
21300
21301 If P is a class, and P has the form
21302 template-id, then A can be a derived class of
21303 the deduced A. Likewise, if P is a pointer to
21304 a class of the form template-id, A can be a
21305 pointer to a derived class pointed to by the
21306 deduced A. */
21307 enum template_base_result r;
21308 r = get_template_base (tparms, targs, parm, arg,
21309 explain_p, &t);
21310
21311 if (!t)
21312 {
21313 /* Don't give the derived diagnostic if we're
21314 already dealing with the same template. */
21315 bool same_template
21316 = (CLASSTYPE_TEMPLATE_INFO (arg)
21317 && (CLASSTYPE_TI_TEMPLATE (parm)
21318 == CLASSTYPE_TI_TEMPLATE (arg)));
21319 return unify_no_common_base (explain_p && !same_template,
21320 r, parm, arg);
21321 }
21322 }
21323 }
21324 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21325 && (CLASSTYPE_TI_TEMPLATE (parm)
21326 == CLASSTYPE_TI_TEMPLATE (arg)))
21327 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21328 Then, we should unify `int' and `U'. */
21329 t = arg;
21330 else
21331 /* There's no chance of unification succeeding. */
21332 return unify_type_mismatch (explain_p, parm, arg);
21333
21334 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21335 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21336 }
21337 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21338 return unify_type_mismatch (explain_p, parm, arg);
21339 return unify_success (explain_p);
21340
21341 case METHOD_TYPE:
21342 case FUNCTION_TYPE:
21343 {
21344 unsigned int nargs;
21345 tree *args;
21346 tree a;
21347 unsigned int i;
21348
21349 if (TREE_CODE (arg) != TREE_CODE (parm))
21350 return unify_type_mismatch (explain_p, parm, arg);
21351
21352 /* CV qualifications for methods can never be deduced, they must
21353 match exactly. We need to check them explicitly here,
21354 because type_unification_real treats them as any other
21355 cv-qualified parameter. */
21356 if (TREE_CODE (parm) == METHOD_TYPE
21357 && (!check_cv_quals_for_unify
21358 (UNIFY_ALLOW_NONE,
21359 class_of_this_parm (arg),
21360 class_of_this_parm (parm))))
21361 return unify_cv_qual_mismatch (explain_p, parm, arg);
21362 if (TREE_CODE (arg) == FUNCTION_TYPE
21363 && type_memfn_quals (parm) != type_memfn_quals (arg))
21364 return unify_cv_qual_mismatch (explain_p, parm, arg);
21365 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21366 return unify_type_mismatch (explain_p, parm, arg);
21367
21368 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21369 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21370
21371 nargs = list_length (TYPE_ARG_TYPES (arg));
21372 args = XALLOCAVEC (tree, nargs);
21373 for (a = TYPE_ARG_TYPES (arg), i = 0;
21374 a != NULL_TREE && a != void_list_node;
21375 a = TREE_CHAIN (a), ++i)
21376 args[i] = TREE_VALUE (a);
21377 nargs = i;
21378
21379 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21380 args, nargs, 1, DEDUCE_EXACT,
21381 LOOKUP_NORMAL, NULL, explain_p))
21382 return 1;
21383
21384 if (flag_noexcept_type)
21385 {
21386 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21387 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21388 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21389 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21390 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21391 && uses_template_parms (TREE_PURPOSE (pspec)))
21392 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21393 TREE_PURPOSE (aspec),
21394 UNIFY_ALLOW_NONE, explain_p);
21395 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21396 return unify_type_mismatch (explain_p, parm, arg);
21397 }
21398
21399 return 0;
21400 }
21401
21402 case OFFSET_TYPE:
21403 /* Unify a pointer to member with a pointer to member function, which
21404 deduces the type of the member as a function type. */
21405 if (TYPE_PTRMEMFUNC_P (arg))
21406 {
21407 /* Check top-level cv qualifiers */
21408 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21409 return unify_cv_qual_mismatch (explain_p, parm, arg);
21410
21411 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21412 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21413 UNIFY_ALLOW_NONE, explain_p);
21414
21415 /* Determine the type of the function we are unifying against. */
21416 tree fntype = static_fn_type (arg);
21417
21418 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21419 }
21420
21421 if (TREE_CODE (arg) != OFFSET_TYPE)
21422 return unify_type_mismatch (explain_p, parm, arg);
21423 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21424 TYPE_OFFSET_BASETYPE (arg),
21425 UNIFY_ALLOW_NONE, explain_p);
21426 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21427 strict, explain_p);
21428
21429 case CONST_DECL:
21430 if (DECL_TEMPLATE_PARM_P (parm))
21431 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21432 if (arg != scalar_constant_value (parm))
21433 return unify_template_argument_mismatch (explain_p, parm, arg);
21434 return unify_success (explain_p);
21435
21436 case FIELD_DECL:
21437 case TEMPLATE_DECL:
21438 /* Matched cases are handled by the ARG == PARM test above. */
21439 return unify_template_argument_mismatch (explain_p, parm, arg);
21440
21441 case VAR_DECL:
21442 /* We might get a variable as a non-type template argument in parm if the
21443 corresponding parameter is type-dependent. Make any necessary
21444 adjustments based on whether arg is a reference. */
21445 if (CONSTANT_CLASS_P (arg))
21446 parm = fold_non_dependent_expr (parm);
21447 else if (REFERENCE_REF_P (arg))
21448 {
21449 tree sub = TREE_OPERAND (arg, 0);
21450 STRIP_NOPS (sub);
21451 if (TREE_CODE (sub) == ADDR_EXPR)
21452 arg = TREE_OPERAND (sub, 0);
21453 }
21454 /* Now use the normal expression code to check whether they match. */
21455 goto expr;
21456
21457 case TYPE_ARGUMENT_PACK:
21458 case NONTYPE_ARGUMENT_PACK:
21459 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21460 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21461
21462 case TYPEOF_TYPE:
21463 case DECLTYPE_TYPE:
21464 case UNDERLYING_TYPE:
21465 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21466 or UNDERLYING_TYPE nodes. */
21467 return unify_success (explain_p);
21468
21469 case ERROR_MARK:
21470 /* Unification fails if we hit an error node. */
21471 return unify_invalid (explain_p);
21472
21473 case INDIRECT_REF:
21474 if (REFERENCE_REF_P (parm))
21475 {
21476 bool pexp = PACK_EXPANSION_P (arg);
21477 if (pexp)
21478 arg = PACK_EXPANSION_PATTERN (arg);
21479 if (REFERENCE_REF_P (arg))
21480 arg = TREE_OPERAND (arg, 0);
21481 if (pexp)
21482 arg = make_pack_expansion (arg, complain);
21483 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21484 strict, explain_p);
21485 }
21486 /* FALLTHRU */
21487
21488 default:
21489 /* An unresolved overload is a nondeduced context. */
21490 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21491 return unify_success (explain_p);
21492 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21493 expr:
21494 /* We must be looking at an expression. This can happen with
21495 something like:
21496
21497 template <int I>
21498 void foo(S<I>, S<I + 2>);
21499
21500 This is a "nondeduced context":
21501
21502 [deduct.type]
21503
21504 The nondeduced contexts are:
21505
21506 --A type that is a template-id in which one or more of
21507 the template-arguments is an expression that references
21508 a template-parameter.
21509
21510 In these cases, we assume deduction succeeded, but don't
21511 actually infer any unifications. */
21512
21513 if (!uses_template_parms (parm)
21514 && !template_args_equal (parm, arg))
21515 return unify_expression_unequal (explain_p, parm, arg);
21516 else
21517 return unify_success (explain_p);
21518 }
21519 }
21520 #undef RECUR_AND_CHECK_FAILURE
21521 \f
21522 /* Note that DECL can be defined in this translation unit, if
21523 required. */
21524
21525 static void
21526 mark_definable (tree decl)
21527 {
21528 tree clone;
21529 DECL_NOT_REALLY_EXTERN (decl) = 1;
21530 FOR_EACH_CLONE (clone, decl)
21531 DECL_NOT_REALLY_EXTERN (clone) = 1;
21532 }
21533
21534 /* Called if RESULT is explicitly instantiated, or is a member of an
21535 explicitly instantiated class. */
21536
21537 void
21538 mark_decl_instantiated (tree result, int extern_p)
21539 {
21540 SET_DECL_EXPLICIT_INSTANTIATION (result);
21541
21542 /* If this entity has already been written out, it's too late to
21543 make any modifications. */
21544 if (TREE_ASM_WRITTEN (result))
21545 return;
21546
21547 /* For anonymous namespace we don't need to do anything. */
21548 if (decl_anon_ns_mem_p (result))
21549 {
21550 gcc_assert (!TREE_PUBLIC (result));
21551 return;
21552 }
21553
21554 if (TREE_CODE (result) != FUNCTION_DECL)
21555 /* The TREE_PUBLIC flag for function declarations will have been
21556 set correctly by tsubst. */
21557 TREE_PUBLIC (result) = 1;
21558
21559 /* This might have been set by an earlier implicit instantiation. */
21560 DECL_COMDAT (result) = 0;
21561
21562 if (extern_p)
21563 DECL_NOT_REALLY_EXTERN (result) = 0;
21564 else
21565 {
21566 mark_definable (result);
21567 mark_needed (result);
21568 /* Always make artificials weak. */
21569 if (DECL_ARTIFICIAL (result) && flag_weak)
21570 comdat_linkage (result);
21571 /* For WIN32 we also want to put explicit instantiations in
21572 linkonce sections. */
21573 else if (TREE_PUBLIC (result))
21574 maybe_make_one_only (result);
21575 }
21576
21577 /* If EXTERN_P, then this function will not be emitted -- unless
21578 followed by an explicit instantiation, at which point its linkage
21579 will be adjusted. If !EXTERN_P, then this function will be
21580 emitted here. In neither circumstance do we want
21581 import_export_decl to adjust the linkage. */
21582 DECL_INTERFACE_KNOWN (result) = 1;
21583 }
21584
21585 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21586 important template arguments. If any are missing, we check whether
21587 they're important by using error_mark_node for substituting into any
21588 args that were used for partial ordering (the ones between ARGS and END)
21589 and seeing if it bubbles up. */
21590
21591 static bool
21592 check_undeduced_parms (tree targs, tree args, tree end)
21593 {
21594 bool found = false;
21595 int i;
21596 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21597 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21598 {
21599 found = true;
21600 TREE_VEC_ELT (targs, i) = error_mark_node;
21601 }
21602 if (found)
21603 {
21604 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21605 if (substed == error_mark_node)
21606 return true;
21607 }
21608 return false;
21609 }
21610
21611 /* Given two function templates PAT1 and PAT2, return:
21612
21613 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21614 -1 if PAT2 is more specialized than PAT1.
21615 0 if neither is more specialized.
21616
21617 LEN indicates the number of parameters we should consider
21618 (defaulted parameters should not be considered).
21619
21620 The 1998 std underspecified function template partial ordering, and
21621 DR214 addresses the issue. We take pairs of arguments, one from
21622 each of the templates, and deduce them against each other. One of
21623 the templates will be more specialized if all the *other*
21624 template's arguments deduce against its arguments and at least one
21625 of its arguments *does* *not* deduce against the other template's
21626 corresponding argument. Deduction is done as for class templates.
21627 The arguments used in deduction have reference and top level cv
21628 qualifiers removed. Iff both arguments were originally reference
21629 types *and* deduction succeeds in both directions, an lvalue reference
21630 wins against an rvalue reference and otherwise the template
21631 with the more cv-qualified argument wins for that pairing (if
21632 neither is more cv-qualified, they both are equal). Unlike regular
21633 deduction, after all the arguments have been deduced in this way,
21634 we do *not* verify the deduced template argument values can be
21635 substituted into non-deduced contexts.
21636
21637 The logic can be a bit confusing here, because we look at deduce1 and
21638 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21639 can find template arguments for pat1 to make arg1 look like arg2, that
21640 means that arg2 is at least as specialized as arg1. */
21641
21642 int
21643 more_specialized_fn (tree pat1, tree pat2, int len)
21644 {
21645 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21646 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21647 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21648 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21649 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21650 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21651 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21652 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21653 tree origs1, origs2;
21654 bool lose1 = false;
21655 bool lose2 = false;
21656
21657 /* Remove the this parameter from non-static member functions. If
21658 one is a non-static member function and the other is not a static
21659 member function, remove the first parameter from that function
21660 also. This situation occurs for operator functions where we
21661 locate both a member function (with this pointer) and non-member
21662 operator (with explicit first operand). */
21663 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21664 {
21665 len--; /* LEN is the number of significant arguments for DECL1 */
21666 args1 = TREE_CHAIN (args1);
21667 if (!DECL_STATIC_FUNCTION_P (decl2))
21668 args2 = TREE_CHAIN (args2);
21669 }
21670 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21671 {
21672 args2 = TREE_CHAIN (args2);
21673 if (!DECL_STATIC_FUNCTION_P (decl1))
21674 {
21675 len--;
21676 args1 = TREE_CHAIN (args1);
21677 }
21678 }
21679
21680 /* If only one is a conversion operator, they are unordered. */
21681 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21682 return 0;
21683
21684 /* Consider the return type for a conversion function */
21685 if (DECL_CONV_FN_P (decl1))
21686 {
21687 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21688 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21689 len++;
21690 }
21691
21692 processing_template_decl++;
21693
21694 origs1 = args1;
21695 origs2 = args2;
21696
21697 while (len--
21698 /* Stop when an ellipsis is seen. */
21699 && args1 != NULL_TREE && args2 != NULL_TREE)
21700 {
21701 tree arg1 = TREE_VALUE (args1);
21702 tree arg2 = TREE_VALUE (args2);
21703 int deduce1, deduce2;
21704 int quals1 = -1;
21705 int quals2 = -1;
21706 int ref1 = 0;
21707 int ref2 = 0;
21708
21709 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21710 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21711 {
21712 /* When both arguments are pack expansions, we need only
21713 unify the patterns themselves. */
21714 arg1 = PACK_EXPANSION_PATTERN (arg1);
21715 arg2 = PACK_EXPANSION_PATTERN (arg2);
21716
21717 /* This is the last comparison we need to do. */
21718 len = 0;
21719 }
21720
21721 /* DR 1847: If a particular P contains no template-parameters that
21722 participate in template argument deduction, that P is not used to
21723 determine the ordering. */
21724 if (!uses_deducible_template_parms (arg1)
21725 && !uses_deducible_template_parms (arg2))
21726 goto next;
21727
21728 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21729 {
21730 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21731 arg1 = TREE_TYPE (arg1);
21732 quals1 = cp_type_quals (arg1);
21733 }
21734
21735 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21736 {
21737 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21738 arg2 = TREE_TYPE (arg2);
21739 quals2 = cp_type_quals (arg2);
21740 }
21741
21742 arg1 = TYPE_MAIN_VARIANT (arg1);
21743 arg2 = TYPE_MAIN_VARIANT (arg2);
21744
21745 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21746 {
21747 int i, len2 = remaining_arguments (args2);
21748 tree parmvec = make_tree_vec (1);
21749 tree argvec = make_tree_vec (len2);
21750 tree ta = args2;
21751
21752 /* Setup the parameter vector, which contains only ARG1. */
21753 TREE_VEC_ELT (parmvec, 0) = arg1;
21754
21755 /* Setup the argument vector, which contains the remaining
21756 arguments. */
21757 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21758 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21759
21760 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21761 argvec, DEDUCE_EXACT,
21762 /*subr=*/true, /*explain_p=*/false)
21763 == 0);
21764
21765 /* We cannot deduce in the other direction, because ARG1 is
21766 a pack expansion but ARG2 is not. */
21767 deduce2 = 0;
21768 }
21769 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21770 {
21771 int i, len1 = remaining_arguments (args1);
21772 tree parmvec = make_tree_vec (1);
21773 tree argvec = make_tree_vec (len1);
21774 tree ta = args1;
21775
21776 /* Setup the parameter vector, which contains only ARG1. */
21777 TREE_VEC_ELT (parmvec, 0) = arg2;
21778
21779 /* Setup the argument vector, which contains the remaining
21780 arguments. */
21781 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21782 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21783
21784 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21785 argvec, DEDUCE_EXACT,
21786 /*subr=*/true, /*explain_p=*/false)
21787 == 0);
21788
21789 /* We cannot deduce in the other direction, because ARG2 is
21790 a pack expansion but ARG1 is not.*/
21791 deduce1 = 0;
21792 }
21793
21794 else
21795 {
21796 /* The normal case, where neither argument is a pack
21797 expansion. */
21798 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21799 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21800 == 0);
21801 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21802 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21803 == 0);
21804 }
21805
21806 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21807 arg2, then arg2 is not as specialized as arg1. */
21808 if (!deduce1)
21809 lose2 = true;
21810 if (!deduce2)
21811 lose1 = true;
21812
21813 /* "If, for a given type, deduction succeeds in both directions
21814 (i.e., the types are identical after the transformations above)
21815 and both P and A were reference types (before being replaced with
21816 the type referred to above):
21817 - if the type from the argument template was an lvalue reference and
21818 the type from the parameter template was not, the argument type is
21819 considered to be more specialized than the other; otherwise,
21820 - if the type from the argument template is more cv-qualified
21821 than the type from the parameter template (as described above),
21822 the argument type is considered to be more specialized than the other;
21823 otherwise,
21824 - neither type is more specialized than the other." */
21825
21826 if (deduce1 && deduce2)
21827 {
21828 if (ref1 && ref2 && ref1 != ref2)
21829 {
21830 if (ref1 > ref2)
21831 lose1 = true;
21832 else
21833 lose2 = true;
21834 }
21835 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21836 {
21837 if ((quals1 & quals2) == quals2)
21838 lose2 = true;
21839 if ((quals1 & quals2) == quals1)
21840 lose1 = true;
21841 }
21842 }
21843
21844 if (lose1 && lose2)
21845 /* We've failed to deduce something in either direction.
21846 These must be unordered. */
21847 break;
21848
21849 next:
21850
21851 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21852 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21853 /* We have already processed all of the arguments in our
21854 handing of the pack expansion type. */
21855 len = 0;
21856
21857 args1 = TREE_CHAIN (args1);
21858 args2 = TREE_CHAIN (args2);
21859 }
21860
21861 /* "In most cases, all template parameters must have values in order for
21862 deduction to succeed, but for partial ordering purposes a template
21863 parameter may remain without a value provided it is not used in the
21864 types being used for partial ordering."
21865
21866 Thus, if we are missing any of the targs1 we need to substitute into
21867 origs1, then pat2 is not as specialized as pat1. This can happen when
21868 there is a nondeduced context. */
21869 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21870 lose2 = true;
21871 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21872 lose1 = true;
21873
21874 processing_template_decl--;
21875
21876 /* If both deductions succeed, the partial ordering selects the more
21877 constrained template. */
21878 if (!lose1 && !lose2)
21879 {
21880 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21881 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21882 lose1 = !subsumes_constraints (c1, c2);
21883 lose2 = !subsumes_constraints (c2, c1);
21884 }
21885
21886 /* All things being equal, if the next argument is a pack expansion
21887 for one function but not for the other, prefer the
21888 non-variadic function. FIXME this is bogus; see c++/41958. */
21889 if (lose1 == lose2
21890 && args1 && TREE_VALUE (args1)
21891 && args2 && TREE_VALUE (args2))
21892 {
21893 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21894 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21895 }
21896
21897 if (lose1 == lose2)
21898 return 0;
21899 else if (!lose1)
21900 return 1;
21901 else
21902 return -1;
21903 }
21904
21905 /* Determine which of two partial specializations of TMPL is more
21906 specialized.
21907
21908 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21909 to the first partial specialization. The TREE_PURPOSE is the
21910 innermost set of template parameters for the partial
21911 specialization. PAT2 is similar, but for the second template.
21912
21913 Return 1 if the first partial specialization is more specialized;
21914 -1 if the second is more specialized; 0 if neither is more
21915 specialized.
21916
21917 See [temp.class.order] for information about determining which of
21918 two templates is more specialized. */
21919
21920 static int
21921 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21922 {
21923 tree targs;
21924 int winner = 0;
21925 bool any_deductions = false;
21926
21927 tree tmpl1 = TREE_VALUE (pat1);
21928 tree tmpl2 = TREE_VALUE (pat2);
21929 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21930 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21931
21932 /* Just like what happens for functions, if we are ordering between
21933 different template specializations, we may encounter dependent
21934 types in the arguments, and we need our dependency check functions
21935 to behave correctly. */
21936 ++processing_template_decl;
21937 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21938 if (targs)
21939 {
21940 --winner;
21941 any_deductions = true;
21942 }
21943
21944 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21945 if (targs)
21946 {
21947 ++winner;
21948 any_deductions = true;
21949 }
21950 --processing_template_decl;
21951
21952 /* If both deductions succeed, the partial ordering selects the more
21953 constrained template. */
21954 if (!winner && any_deductions)
21955 return more_constrained (tmpl1, tmpl2);
21956
21957 /* In the case of a tie where at least one of the templates
21958 has a parameter pack at the end, the template with the most
21959 non-packed parameters wins. */
21960 if (winner == 0
21961 && any_deductions
21962 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21963 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21964 {
21965 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21966 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21967 int len1 = TREE_VEC_LENGTH (args1);
21968 int len2 = TREE_VEC_LENGTH (args2);
21969
21970 /* We don't count the pack expansion at the end. */
21971 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21972 --len1;
21973 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21974 --len2;
21975
21976 if (len1 > len2)
21977 return 1;
21978 else if (len1 < len2)
21979 return -1;
21980 }
21981
21982 return winner;
21983 }
21984
21985 /* Return the template arguments that will produce the function signature
21986 DECL from the function template FN, with the explicit template
21987 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21988 also match. Return NULL_TREE if no satisfactory arguments could be
21989 found. */
21990
21991 static tree
21992 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21993 {
21994 int ntparms = DECL_NTPARMS (fn);
21995 tree targs = make_tree_vec (ntparms);
21996 tree decl_type = TREE_TYPE (decl);
21997 tree decl_arg_types;
21998 tree *args;
21999 unsigned int nargs, ix;
22000 tree arg;
22001
22002 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22003
22004 /* Never do unification on the 'this' parameter. */
22005 decl_arg_types = skip_artificial_parms_for (decl,
22006 TYPE_ARG_TYPES (decl_type));
22007
22008 nargs = list_length (decl_arg_types);
22009 args = XALLOCAVEC (tree, nargs);
22010 for (arg = decl_arg_types, ix = 0;
22011 arg != NULL_TREE && arg != void_list_node;
22012 arg = TREE_CHAIN (arg), ++ix)
22013 args[ix] = TREE_VALUE (arg);
22014
22015 if (fn_type_unification (fn, explicit_args, targs,
22016 args, ix,
22017 (check_rettype || DECL_CONV_FN_P (fn)
22018 ? TREE_TYPE (decl_type) : NULL_TREE),
22019 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22020 /*decltype*/false)
22021 == error_mark_node)
22022 return NULL_TREE;
22023
22024 return targs;
22025 }
22026
22027 /* Return the innermost template arguments that, when applied to a partial
22028 specialization SPEC_TMPL of TMPL, yield the ARGS.
22029
22030 For example, suppose we have:
22031
22032 template <class T, class U> struct S {};
22033 template <class T> struct S<T*, int> {};
22034
22035 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22036 partial specialization and the ARGS will be {double*, int}. The resulting
22037 vector will be {double}, indicating that `T' is bound to `double'. */
22038
22039 static tree
22040 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22041 {
22042 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22043 tree spec_args
22044 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22045 int i, ntparms = TREE_VEC_LENGTH (tparms);
22046 tree deduced_args;
22047 tree innermost_deduced_args;
22048
22049 innermost_deduced_args = make_tree_vec (ntparms);
22050 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22051 {
22052 deduced_args = copy_node (args);
22053 SET_TMPL_ARGS_LEVEL (deduced_args,
22054 TMPL_ARGS_DEPTH (deduced_args),
22055 innermost_deduced_args);
22056 }
22057 else
22058 deduced_args = innermost_deduced_args;
22059
22060 bool tried_array_deduction = (cxx_dialect < cxx17);
22061 again:
22062 if (unify (tparms, deduced_args,
22063 INNERMOST_TEMPLATE_ARGS (spec_args),
22064 INNERMOST_TEMPLATE_ARGS (args),
22065 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22066 return NULL_TREE;
22067
22068 for (i = 0; i < ntparms; ++i)
22069 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22070 {
22071 if (!tried_array_deduction)
22072 {
22073 try_array_deduction (tparms, innermost_deduced_args,
22074 INNERMOST_TEMPLATE_ARGS (spec_args));
22075 tried_array_deduction = true;
22076 if (TREE_VEC_ELT (innermost_deduced_args, i))
22077 goto again;
22078 }
22079 return NULL_TREE;
22080 }
22081
22082 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22083 if (!push_tinst_level (tinst))
22084 {
22085 excessive_deduction_depth = true;
22086 return NULL_TREE;
22087 }
22088
22089 /* Verify that nondeduced template arguments agree with the type
22090 obtained from argument deduction.
22091
22092 For example:
22093
22094 struct A { typedef int X; };
22095 template <class T, class U> struct C {};
22096 template <class T> struct C<T, typename T::X> {};
22097
22098 Then with the instantiation `C<A, int>', we can deduce that
22099 `T' is `A' but unify () does not check whether `typename T::X'
22100 is `int'. */
22101 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22102
22103 if (spec_args != error_mark_node)
22104 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22105 INNERMOST_TEMPLATE_ARGS (spec_args),
22106 tmpl, tf_none, false, false);
22107
22108 pop_tinst_level ();
22109
22110 if (spec_args == error_mark_node
22111 /* We only need to check the innermost arguments; the other
22112 arguments will always agree. */
22113 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22114 INNERMOST_TEMPLATE_ARGS (args)))
22115 return NULL_TREE;
22116
22117 /* Now that we have bindings for all of the template arguments,
22118 ensure that the arguments deduced for the template template
22119 parameters have compatible template parameter lists. See the use
22120 of template_template_parm_bindings_ok_p in fn_type_unification
22121 for more information. */
22122 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22123 return NULL_TREE;
22124
22125 return deduced_args;
22126 }
22127
22128 // Compare two function templates T1 and T2 by deducing bindings
22129 // from one against the other. If both deductions succeed, compare
22130 // constraints to see which is more constrained.
22131 static int
22132 more_specialized_inst (tree t1, tree t2)
22133 {
22134 int fate = 0;
22135 int count = 0;
22136
22137 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22138 {
22139 --fate;
22140 ++count;
22141 }
22142
22143 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22144 {
22145 ++fate;
22146 ++count;
22147 }
22148
22149 // If both deductions succeed, then one may be more constrained.
22150 if (count == 2 && fate == 0)
22151 fate = more_constrained (t1, t2);
22152
22153 return fate;
22154 }
22155
22156 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22157 Return the TREE_LIST node with the most specialized template, if
22158 any. If there is no most specialized template, the error_mark_node
22159 is returned.
22160
22161 Note that this function does not look at, or modify, the
22162 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22163 returned is one of the elements of INSTANTIATIONS, callers may
22164 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22165 and retrieve it from the value returned. */
22166
22167 tree
22168 most_specialized_instantiation (tree templates)
22169 {
22170 tree fn, champ;
22171
22172 ++processing_template_decl;
22173
22174 champ = templates;
22175 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22176 {
22177 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22178 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22179 if (fate == -1)
22180 champ = fn;
22181 else if (!fate)
22182 {
22183 /* Equally specialized, move to next function. If there
22184 is no next function, nothing's most specialized. */
22185 fn = TREE_CHAIN (fn);
22186 champ = fn;
22187 if (!fn)
22188 break;
22189 }
22190 }
22191
22192 if (champ)
22193 /* Now verify that champ is better than everything earlier in the
22194 instantiation list. */
22195 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22196 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22197 {
22198 champ = NULL_TREE;
22199 break;
22200 }
22201 }
22202
22203 processing_template_decl--;
22204
22205 if (!champ)
22206 return error_mark_node;
22207
22208 return champ;
22209 }
22210
22211 /* If DECL is a specialization of some template, return the most
22212 general such template. Otherwise, returns NULL_TREE.
22213
22214 For example, given:
22215
22216 template <class T> struct S { template <class U> void f(U); };
22217
22218 if TMPL is `template <class U> void S<int>::f(U)' this will return
22219 the full template. This function will not trace past partial
22220 specializations, however. For example, given in addition:
22221
22222 template <class T> struct S<T*> { template <class U> void f(U); };
22223
22224 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22225 `template <class T> template <class U> S<T*>::f(U)'. */
22226
22227 tree
22228 most_general_template (tree decl)
22229 {
22230 if (TREE_CODE (decl) != TEMPLATE_DECL)
22231 {
22232 if (tree tinfo = get_template_info (decl))
22233 decl = TI_TEMPLATE (tinfo);
22234 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22235 template friend, or a FIELD_DECL for a capture pack. */
22236 if (TREE_CODE (decl) != TEMPLATE_DECL)
22237 return NULL_TREE;
22238 }
22239
22240 /* Look for more and more general templates. */
22241 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22242 {
22243 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22244 (See cp-tree.h for details.) */
22245 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22246 break;
22247
22248 if (CLASS_TYPE_P (TREE_TYPE (decl))
22249 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22250 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22251 break;
22252
22253 /* Stop if we run into an explicitly specialized class template. */
22254 if (!DECL_NAMESPACE_SCOPE_P (decl)
22255 && DECL_CONTEXT (decl)
22256 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22257 break;
22258
22259 decl = DECL_TI_TEMPLATE (decl);
22260 }
22261
22262 return decl;
22263 }
22264
22265 /* Return the most specialized of the template partial specializations
22266 which can produce TARGET, a specialization of some class or variable
22267 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22268 a TEMPLATE_DECL node corresponding to the partial specialization, while
22269 the TREE_PURPOSE is the set of template arguments that must be
22270 substituted into the template pattern in order to generate TARGET.
22271
22272 If the choice of partial specialization is ambiguous, a diagnostic
22273 is issued, and the error_mark_node is returned. If there are no
22274 partial specializations matching TARGET, then NULL_TREE is
22275 returned, indicating that the primary template should be used. */
22276
22277 static tree
22278 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22279 {
22280 tree list = NULL_TREE;
22281 tree t;
22282 tree champ;
22283 int fate;
22284 bool ambiguous_p;
22285 tree outer_args = NULL_TREE;
22286 tree tmpl, args;
22287
22288 if (TYPE_P (target))
22289 {
22290 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22291 tmpl = TI_TEMPLATE (tinfo);
22292 args = TI_ARGS (tinfo);
22293 }
22294 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22295 {
22296 tmpl = TREE_OPERAND (target, 0);
22297 args = TREE_OPERAND (target, 1);
22298 }
22299 else if (VAR_P (target))
22300 {
22301 tree tinfo = DECL_TEMPLATE_INFO (target);
22302 tmpl = TI_TEMPLATE (tinfo);
22303 args = TI_ARGS (tinfo);
22304 }
22305 else
22306 gcc_unreachable ();
22307
22308 tree main_tmpl = most_general_template (tmpl);
22309
22310 /* For determining which partial specialization to use, only the
22311 innermost args are interesting. */
22312 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22313 {
22314 outer_args = strip_innermost_template_args (args, 1);
22315 args = INNERMOST_TEMPLATE_ARGS (args);
22316 }
22317
22318 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22319 {
22320 tree spec_args;
22321 tree spec_tmpl = TREE_VALUE (t);
22322
22323 if (outer_args)
22324 {
22325 /* Substitute in the template args from the enclosing class. */
22326 ++processing_template_decl;
22327 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22328 --processing_template_decl;
22329 }
22330
22331 if (spec_tmpl == error_mark_node)
22332 return error_mark_node;
22333
22334 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22335 if (spec_args)
22336 {
22337 if (outer_args)
22338 spec_args = add_to_template_args (outer_args, spec_args);
22339
22340 /* Keep the candidate only if the constraints are satisfied,
22341 or if we're not compiling with concepts. */
22342 if (!flag_concepts
22343 || constraints_satisfied_p (spec_tmpl, spec_args))
22344 {
22345 list = tree_cons (spec_args, TREE_VALUE (t), list);
22346 TREE_TYPE (list) = TREE_TYPE (t);
22347 }
22348 }
22349 }
22350
22351 if (! list)
22352 return NULL_TREE;
22353
22354 ambiguous_p = false;
22355 t = list;
22356 champ = t;
22357 t = TREE_CHAIN (t);
22358 for (; t; t = TREE_CHAIN (t))
22359 {
22360 fate = more_specialized_partial_spec (tmpl, champ, t);
22361 if (fate == 1)
22362 ;
22363 else
22364 {
22365 if (fate == 0)
22366 {
22367 t = TREE_CHAIN (t);
22368 if (! t)
22369 {
22370 ambiguous_p = true;
22371 break;
22372 }
22373 }
22374 champ = t;
22375 }
22376 }
22377
22378 if (!ambiguous_p)
22379 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22380 {
22381 fate = more_specialized_partial_spec (tmpl, champ, t);
22382 if (fate != 1)
22383 {
22384 ambiguous_p = true;
22385 break;
22386 }
22387 }
22388
22389 if (ambiguous_p)
22390 {
22391 const char *str;
22392 char *spaces = NULL;
22393 if (!(complain & tf_error))
22394 return error_mark_node;
22395 if (TYPE_P (target))
22396 error ("ambiguous template instantiation for %q#T", target);
22397 else
22398 error ("ambiguous template instantiation for %q#D", target);
22399 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22400 for (t = list; t; t = TREE_CHAIN (t))
22401 {
22402 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22403 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22404 "%s %#qS", spaces ? spaces : str, subst);
22405 spaces = spaces ? spaces : get_spaces (str);
22406 }
22407 free (spaces);
22408 return error_mark_node;
22409 }
22410
22411 return champ;
22412 }
22413
22414 /* Explicitly instantiate DECL. */
22415
22416 void
22417 do_decl_instantiation (tree decl, tree storage)
22418 {
22419 tree result = NULL_TREE;
22420 int extern_p = 0;
22421
22422 if (!decl || decl == error_mark_node)
22423 /* An error occurred, for which grokdeclarator has already issued
22424 an appropriate message. */
22425 return;
22426 else if (! DECL_LANG_SPECIFIC (decl))
22427 {
22428 error ("explicit instantiation of non-template %q#D", decl);
22429 return;
22430 }
22431
22432 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22433 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22434
22435 if (VAR_P (decl) && !var_templ)
22436 {
22437 /* There is an asymmetry here in the way VAR_DECLs and
22438 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22439 the latter, the DECL we get back will be marked as a
22440 template instantiation, and the appropriate
22441 DECL_TEMPLATE_INFO will be set up. This does not happen for
22442 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22443 should handle VAR_DECLs as it currently handles
22444 FUNCTION_DECLs. */
22445 if (!DECL_CLASS_SCOPE_P (decl))
22446 {
22447 error ("%qD is not a static data member of a class template", decl);
22448 return;
22449 }
22450 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22451 if (!result || !VAR_P (result))
22452 {
22453 error ("no matching template for %qD found", decl);
22454 return;
22455 }
22456 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22457 {
22458 error ("type %qT for explicit instantiation %qD does not match "
22459 "declared type %qT", TREE_TYPE (result), decl,
22460 TREE_TYPE (decl));
22461 return;
22462 }
22463 }
22464 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22465 {
22466 error ("explicit instantiation of %q#D", decl);
22467 return;
22468 }
22469 else
22470 result = decl;
22471
22472 /* Check for various error cases. Note that if the explicit
22473 instantiation is valid the RESULT will currently be marked as an
22474 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22475 until we get here. */
22476
22477 if (DECL_TEMPLATE_SPECIALIZATION (result))
22478 {
22479 /* DR 259 [temp.spec].
22480
22481 Both an explicit instantiation and a declaration of an explicit
22482 specialization shall not appear in a program unless the explicit
22483 instantiation follows a declaration of the explicit specialization.
22484
22485 For a given set of template parameters, if an explicit
22486 instantiation of a template appears after a declaration of an
22487 explicit specialization for that template, the explicit
22488 instantiation has no effect. */
22489 return;
22490 }
22491 else if (DECL_EXPLICIT_INSTANTIATION (result))
22492 {
22493 /* [temp.spec]
22494
22495 No program shall explicitly instantiate any template more
22496 than once.
22497
22498 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22499 the first instantiation was `extern' and the second is not,
22500 and EXTERN_P for the opposite case. */
22501 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22502 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22503 /* If an "extern" explicit instantiation follows an ordinary
22504 explicit instantiation, the template is instantiated. */
22505 if (extern_p)
22506 return;
22507 }
22508 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22509 {
22510 error ("no matching template for %qD found", result);
22511 return;
22512 }
22513 else if (!DECL_TEMPLATE_INFO (result))
22514 {
22515 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22516 return;
22517 }
22518
22519 if (storage == NULL_TREE)
22520 ;
22521 else if (storage == ridpointers[(int) RID_EXTERN])
22522 {
22523 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22524 pedwarn (input_location, OPT_Wpedantic,
22525 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22526 "instantiations");
22527 extern_p = 1;
22528 }
22529 else
22530 error ("storage class %qD applied to template instantiation", storage);
22531
22532 check_explicit_instantiation_namespace (result);
22533 mark_decl_instantiated (result, extern_p);
22534 if (! extern_p)
22535 instantiate_decl (result, /*defer_ok=*/true,
22536 /*expl_inst_class_mem_p=*/false);
22537 }
22538
22539 static void
22540 mark_class_instantiated (tree t, int extern_p)
22541 {
22542 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22543 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22544 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22545 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22546 if (! extern_p)
22547 {
22548 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22549 rest_of_type_compilation (t, 1);
22550 }
22551 }
22552
22553 /* Called from do_type_instantiation through binding_table_foreach to
22554 do recursive instantiation for the type bound in ENTRY. */
22555 static void
22556 bt_instantiate_type_proc (binding_entry entry, void *data)
22557 {
22558 tree storage = *(tree *) data;
22559
22560 if (MAYBE_CLASS_TYPE_P (entry->type)
22561 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22562 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22563 }
22564
22565 /* Perform an explicit instantiation of template class T. STORAGE, if
22566 non-null, is the RID for extern, inline or static. COMPLAIN is
22567 nonzero if this is called from the parser, zero if called recursively,
22568 since the standard is unclear (as detailed below). */
22569
22570 void
22571 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22572 {
22573 int extern_p = 0;
22574 int nomem_p = 0;
22575 int static_p = 0;
22576 int previous_instantiation_extern_p = 0;
22577
22578 if (TREE_CODE (t) == TYPE_DECL)
22579 t = TREE_TYPE (t);
22580
22581 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22582 {
22583 tree tmpl =
22584 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22585 if (tmpl)
22586 error ("explicit instantiation of non-class template %qD", tmpl);
22587 else
22588 error ("explicit instantiation of non-template type %qT", t);
22589 return;
22590 }
22591
22592 complete_type (t);
22593
22594 if (!COMPLETE_TYPE_P (t))
22595 {
22596 if (complain & tf_error)
22597 error ("explicit instantiation of %q#T before definition of template",
22598 t);
22599 return;
22600 }
22601
22602 if (storage != NULL_TREE)
22603 {
22604 if (!in_system_header_at (input_location))
22605 {
22606 if (storage == ridpointers[(int) RID_EXTERN])
22607 {
22608 if (cxx_dialect == cxx98)
22609 pedwarn (input_location, OPT_Wpedantic,
22610 "ISO C++ 1998 forbids the use of %<extern%> on "
22611 "explicit instantiations");
22612 }
22613 else
22614 pedwarn (input_location, OPT_Wpedantic,
22615 "ISO C++ forbids the use of %qE"
22616 " on explicit instantiations", storage);
22617 }
22618
22619 if (storage == ridpointers[(int) RID_INLINE])
22620 nomem_p = 1;
22621 else if (storage == ridpointers[(int) RID_EXTERN])
22622 extern_p = 1;
22623 else if (storage == ridpointers[(int) RID_STATIC])
22624 static_p = 1;
22625 else
22626 {
22627 error ("storage class %qD applied to template instantiation",
22628 storage);
22629 extern_p = 0;
22630 }
22631 }
22632
22633 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22634 {
22635 /* DR 259 [temp.spec].
22636
22637 Both an explicit instantiation and a declaration of an explicit
22638 specialization shall not appear in a program unless the explicit
22639 instantiation follows a declaration of the explicit specialization.
22640
22641 For a given set of template parameters, if an explicit
22642 instantiation of a template appears after a declaration of an
22643 explicit specialization for that template, the explicit
22644 instantiation has no effect. */
22645 return;
22646 }
22647 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22648 {
22649 /* [temp.spec]
22650
22651 No program shall explicitly instantiate any template more
22652 than once.
22653
22654 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22655 instantiation was `extern'. If EXTERN_P then the second is.
22656 These cases are OK. */
22657 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22658
22659 if (!previous_instantiation_extern_p && !extern_p
22660 && (complain & tf_error))
22661 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22662
22663 /* If we've already instantiated the template, just return now. */
22664 if (!CLASSTYPE_INTERFACE_ONLY (t))
22665 return;
22666 }
22667
22668 check_explicit_instantiation_namespace (TYPE_NAME (t));
22669 mark_class_instantiated (t, extern_p);
22670
22671 if (nomem_p)
22672 return;
22673
22674 /* In contrast to implicit instantiation, where only the
22675 declarations, and not the definitions, of members are
22676 instantiated, we have here:
22677
22678 [temp.explicit]
22679
22680 The explicit instantiation of a class template specialization
22681 implies the instantiation of all of its members not
22682 previously explicitly specialized in the translation unit
22683 containing the explicit instantiation.
22684
22685 Of course, we can't instantiate member template classes, since we
22686 don't have any arguments for them. Note that the standard is
22687 unclear on whether the instantiation of the members are
22688 *explicit* instantiations or not. However, the most natural
22689 interpretation is that it should be an explicit
22690 instantiation. */
22691 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22692 if ((VAR_P (fld)
22693 || (TREE_CODE (fld) == FUNCTION_DECL
22694 && !static_p
22695 && user_provided_p (fld)))
22696 && DECL_TEMPLATE_INSTANTIATION (fld))
22697 {
22698 mark_decl_instantiated (fld, extern_p);
22699 if (! extern_p)
22700 instantiate_decl (fld, /*defer_ok=*/true,
22701 /*expl_inst_class_mem_p=*/true);
22702 }
22703
22704 if (CLASSTYPE_NESTED_UTDS (t))
22705 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22706 bt_instantiate_type_proc, &storage);
22707 }
22708
22709 /* Given a function DECL, which is a specialization of TMPL, modify
22710 DECL to be a re-instantiation of TMPL with the same template
22711 arguments. TMPL should be the template into which tsubst'ing
22712 should occur for DECL, not the most general template.
22713
22714 One reason for doing this is a scenario like this:
22715
22716 template <class T>
22717 void f(const T&, int i);
22718
22719 void g() { f(3, 7); }
22720
22721 template <class T>
22722 void f(const T& t, const int i) { }
22723
22724 Note that when the template is first instantiated, with
22725 instantiate_template, the resulting DECL will have no name for the
22726 first parameter, and the wrong type for the second. So, when we go
22727 to instantiate the DECL, we regenerate it. */
22728
22729 static void
22730 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22731 {
22732 /* The arguments used to instantiate DECL, from the most general
22733 template. */
22734 tree code_pattern;
22735
22736 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22737
22738 /* Make sure that we can see identifiers, and compute access
22739 correctly. */
22740 push_access_scope (decl);
22741
22742 if (TREE_CODE (decl) == FUNCTION_DECL)
22743 {
22744 tree decl_parm;
22745 tree pattern_parm;
22746 tree specs;
22747 int args_depth;
22748 int parms_depth;
22749
22750 args_depth = TMPL_ARGS_DEPTH (args);
22751 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22752 if (args_depth > parms_depth)
22753 args = get_innermost_template_args (args, parms_depth);
22754
22755 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22756 args, tf_error, NULL_TREE,
22757 /*defer_ok*/false);
22758 if (specs && specs != error_mark_node)
22759 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22760 specs);
22761
22762 /* Merge parameter declarations. */
22763 decl_parm = skip_artificial_parms_for (decl,
22764 DECL_ARGUMENTS (decl));
22765 pattern_parm
22766 = skip_artificial_parms_for (code_pattern,
22767 DECL_ARGUMENTS (code_pattern));
22768 while (decl_parm && !DECL_PACK_P (pattern_parm))
22769 {
22770 tree parm_type;
22771 tree attributes;
22772
22773 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22774 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22775 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22776 NULL_TREE);
22777 parm_type = type_decays_to (parm_type);
22778 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22779 TREE_TYPE (decl_parm) = parm_type;
22780 attributes = DECL_ATTRIBUTES (pattern_parm);
22781 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22782 {
22783 DECL_ATTRIBUTES (decl_parm) = attributes;
22784 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22785 }
22786 decl_parm = DECL_CHAIN (decl_parm);
22787 pattern_parm = DECL_CHAIN (pattern_parm);
22788 }
22789 /* Merge any parameters that match with the function parameter
22790 pack. */
22791 if (pattern_parm && DECL_PACK_P (pattern_parm))
22792 {
22793 int i, len;
22794 tree expanded_types;
22795 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22796 the parameters in this function parameter pack. */
22797 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22798 args, tf_error, NULL_TREE);
22799 len = TREE_VEC_LENGTH (expanded_types);
22800 for (i = 0; i < len; i++)
22801 {
22802 tree parm_type;
22803 tree attributes;
22804
22805 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22806 /* Rename the parameter to include the index. */
22807 DECL_NAME (decl_parm) =
22808 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22809 parm_type = TREE_VEC_ELT (expanded_types, i);
22810 parm_type = type_decays_to (parm_type);
22811 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22812 TREE_TYPE (decl_parm) = parm_type;
22813 attributes = DECL_ATTRIBUTES (pattern_parm);
22814 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22815 {
22816 DECL_ATTRIBUTES (decl_parm) = attributes;
22817 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22818 }
22819 decl_parm = DECL_CHAIN (decl_parm);
22820 }
22821 }
22822 /* Merge additional specifiers from the CODE_PATTERN. */
22823 if (DECL_DECLARED_INLINE_P (code_pattern)
22824 && !DECL_DECLARED_INLINE_P (decl))
22825 DECL_DECLARED_INLINE_P (decl) = 1;
22826 }
22827 else if (VAR_P (decl))
22828 {
22829 start_lambda_scope (decl);
22830 DECL_INITIAL (decl) =
22831 tsubst_expr (DECL_INITIAL (code_pattern), args,
22832 tf_error, DECL_TI_TEMPLATE (decl),
22833 /*integral_constant_expression_p=*/false);
22834 finish_lambda_scope ();
22835 if (VAR_HAD_UNKNOWN_BOUND (decl))
22836 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22837 tf_error, DECL_TI_TEMPLATE (decl));
22838 }
22839 else
22840 gcc_unreachable ();
22841
22842 pop_access_scope (decl);
22843 }
22844
22845 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22846 substituted to get DECL. */
22847
22848 tree
22849 template_for_substitution (tree decl)
22850 {
22851 tree tmpl = DECL_TI_TEMPLATE (decl);
22852
22853 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22854 for the instantiation. This is not always the most general
22855 template. Consider, for example:
22856
22857 template <class T>
22858 struct S { template <class U> void f();
22859 template <> void f<int>(); };
22860
22861 and an instantiation of S<double>::f<int>. We want TD to be the
22862 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22863 while (/* An instantiation cannot have a definition, so we need a
22864 more general template. */
22865 DECL_TEMPLATE_INSTANTIATION (tmpl)
22866 /* We must also deal with friend templates. Given:
22867
22868 template <class T> struct S {
22869 template <class U> friend void f() {};
22870 };
22871
22872 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22873 so far as the language is concerned, but that's still
22874 where we get the pattern for the instantiation from. On
22875 other hand, if the definition comes outside the class, say:
22876
22877 template <class T> struct S {
22878 template <class U> friend void f();
22879 };
22880 template <class U> friend void f() {}
22881
22882 we don't need to look any further. That's what the check for
22883 DECL_INITIAL is for. */
22884 || (TREE_CODE (decl) == FUNCTION_DECL
22885 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22886 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22887 {
22888 /* The present template, TD, should not be a definition. If it
22889 were a definition, we should be using it! Note that we
22890 cannot restructure the loop to just keep going until we find
22891 a template with a definition, since that might go too far if
22892 a specialization was declared, but not defined. */
22893
22894 /* Fetch the more general template. */
22895 tmpl = DECL_TI_TEMPLATE (tmpl);
22896 }
22897
22898 return tmpl;
22899 }
22900
22901 /* Returns true if we need to instantiate this template instance even if we
22902 know we aren't going to emit it. */
22903
22904 bool
22905 always_instantiate_p (tree decl)
22906 {
22907 /* We always instantiate inline functions so that we can inline them. An
22908 explicit instantiation declaration prohibits implicit instantiation of
22909 non-inline functions. With high levels of optimization, we would
22910 normally inline non-inline functions -- but we're not allowed to do
22911 that for "extern template" functions. Therefore, we check
22912 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22913 return ((TREE_CODE (decl) == FUNCTION_DECL
22914 && (DECL_DECLARED_INLINE_P (decl)
22915 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22916 /* And we need to instantiate static data members so that
22917 their initializers are available in integral constant
22918 expressions. */
22919 || (VAR_P (decl)
22920 && decl_maybe_constant_var_p (decl)));
22921 }
22922
22923 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22924 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22925 error, true otherwise. */
22926
22927 bool
22928 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22929 {
22930 tree fntype, spec, noex, clone;
22931
22932 /* Don't instantiate a noexcept-specification from template context. */
22933 if (processing_template_decl)
22934 return true;
22935
22936 if (DECL_CLONED_FUNCTION_P (fn))
22937 fn = DECL_CLONED_FUNCTION (fn);
22938 fntype = TREE_TYPE (fn);
22939 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22940
22941 if (!spec || !TREE_PURPOSE (spec))
22942 return true;
22943
22944 noex = TREE_PURPOSE (spec);
22945
22946 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22947 {
22948 static hash_set<tree>* fns = new hash_set<tree>;
22949 bool added = false;
22950 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22951 spec = get_defaulted_eh_spec (fn, complain);
22952 else if (!(added = !fns->add (fn)))
22953 {
22954 /* If hash_set::add returns true, the element was already there. */
22955 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22956 DECL_SOURCE_LOCATION (fn));
22957 error_at (loc,
22958 "exception specification of %qD depends on itself",
22959 fn);
22960 spec = noexcept_false_spec;
22961 }
22962 else if (push_tinst_level (fn))
22963 {
22964 push_access_scope (fn);
22965 push_deferring_access_checks (dk_no_deferred);
22966 input_location = DECL_SOURCE_LOCATION (fn);
22967 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22968 DEFERRED_NOEXCEPT_ARGS (noex),
22969 tf_warning_or_error, fn,
22970 /*function_p=*/false,
22971 /*integral_constant_expression_p=*/true);
22972 pop_deferring_access_checks ();
22973 pop_access_scope (fn);
22974 pop_tinst_level ();
22975 spec = build_noexcept_spec (noex, tf_warning_or_error);
22976 if (spec == error_mark_node)
22977 spec = noexcept_false_spec;
22978 }
22979 else
22980 spec = noexcept_false_spec;
22981
22982 if (added)
22983 fns->remove (fn);
22984
22985 if (spec == error_mark_node)
22986 return false;
22987
22988 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22989 }
22990
22991 FOR_EACH_CLONE (clone, fn)
22992 {
22993 if (TREE_TYPE (clone) == fntype)
22994 TREE_TYPE (clone) = TREE_TYPE (fn);
22995 else
22996 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22997 }
22998
22999 return true;
23000 }
23001
23002 /* We're starting to process the function INST, an instantiation of PATTERN;
23003 add their parameters to local_specializations. */
23004
23005 static void
23006 register_parameter_specializations (tree pattern, tree inst)
23007 {
23008 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23009 tree spec_parm = DECL_ARGUMENTS (inst);
23010 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23011 {
23012 register_local_specialization (spec_parm, tmpl_parm);
23013 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23014 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23015 }
23016 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23017 {
23018 if (!DECL_PACK_P (tmpl_parm))
23019 {
23020 register_local_specialization (spec_parm, tmpl_parm);
23021 spec_parm = DECL_CHAIN (spec_parm);
23022 }
23023 else
23024 {
23025 /* Register the (value) argument pack as a specialization of
23026 TMPL_PARM, then move on. */
23027 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23028 register_local_specialization (argpack, tmpl_parm);
23029 }
23030 }
23031 gcc_assert (!spec_parm);
23032 }
23033
23034 /* Produce the definition of D, a _DECL generated from a template. If
23035 DEFER_OK is true, then we don't have to actually do the
23036 instantiation now; we just have to do it sometime. Normally it is
23037 an error if this is an explicit instantiation but D is undefined.
23038 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23039 instantiated class template. */
23040
23041 tree
23042 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23043 {
23044 tree tmpl = DECL_TI_TEMPLATE (d);
23045 tree gen_args;
23046 tree args;
23047 tree td;
23048 tree code_pattern;
23049 tree spec;
23050 tree gen_tmpl;
23051 bool pattern_defined;
23052 location_t saved_loc = input_location;
23053 int saved_unevaluated_operand = cp_unevaluated_operand;
23054 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23055 bool external_p;
23056 bool deleted_p;
23057
23058 /* This function should only be used to instantiate templates for
23059 functions and static member variables. */
23060 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23061
23062 /* A concept is never instantiated. */
23063 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23064
23065 /* Variables are never deferred; if instantiation is required, they
23066 are instantiated right away. That allows for better code in the
23067 case that an expression refers to the value of the variable --
23068 if the variable has a constant value the referring expression can
23069 take advantage of that fact. */
23070 if (VAR_P (d))
23071 defer_ok = false;
23072
23073 /* Don't instantiate cloned functions. Instead, instantiate the
23074 functions they cloned. */
23075 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23076 d = DECL_CLONED_FUNCTION (d);
23077
23078 if (DECL_TEMPLATE_INSTANTIATED (d)
23079 || (TREE_CODE (d) == FUNCTION_DECL
23080 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23081 || DECL_TEMPLATE_SPECIALIZATION (d))
23082 /* D has already been instantiated or explicitly specialized, so
23083 there's nothing for us to do here.
23084
23085 It might seem reasonable to check whether or not D is an explicit
23086 instantiation, and, if so, stop here. But when an explicit
23087 instantiation is deferred until the end of the compilation,
23088 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23089 the instantiation. */
23090 return d;
23091
23092 /* Check to see whether we know that this template will be
23093 instantiated in some other file, as with "extern template"
23094 extension. */
23095 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23096
23097 /* In general, we do not instantiate such templates. */
23098 if (external_p && !always_instantiate_p (d))
23099 return d;
23100
23101 gen_tmpl = most_general_template (tmpl);
23102 gen_args = DECL_TI_ARGS (d);
23103
23104 if (tmpl != gen_tmpl)
23105 /* We should already have the extra args. */
23106 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23107 == TMPL_ARGS_DEPTH (gen_args));
23108 /* And what's in the hash table should match D. */
23109 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23110 || spec == NULL_TREE);
23111
23112 /* This needs to happen before any tsubsting. */
23113 if (! push_tinst_level (d))
23114 return d;
23115
23116 timevar_push (TV_TEMPLATE_INST);
23117
23118 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23119 for the instantiation. */
23120 td = template_for_substitution (d);
23121 args = gen_args;
23122
23123 if (VAR_P (d))
23124 {
23125 /* Look up an explicit specialization, if any. */
23126 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23127 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23128 if (elt && elt != error_mark_node)
23129 {
23130 td = TREE_VALUE (elt);
23131 args = TREE_PURPOSE (elt);
23132 }
23133 }
23134
23135 code_pattern = DECL_TEMPLATE_RESULT (td);
23136
23137 /* We should never be trying to instantiate a member of a class
23138 template or partial specialization. */
23139 gcc_assert (d != code_pattern);
23140
23141 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23142 || DECL_TEMPLATE_SPECIALIZATION (td))
23143 /* In the case of a friend template whose definition is provided
23144 outside the class, we may have too many arguments. Drop the
23145 ones we don't need. The same is true for specializations. */
23146 args = get_innermost_template_args
23147 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23148
23149 if (TREE_CODE (d) == FUNCTION_DECL)
23150 {
23151 deleted_p = DECL_DELETED_FN (code_pattern);
23152 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23153 && DECL_INITIAL (code_pattern) != error_mark_node)
23154 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23155 || deleted_p);
23156 }
23157 else
23158 {
23159 deleted_p = false;
23160 if (DECL_CLASS_SCOPE_P (code_pattern))
23161 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23162 || DECL_INLINE_VAR_P (code_pattern));
23163 else
23164 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23165 }
23166
23167 /* We may be in the middle of deferred access check. Disable it now. */
23168 push_deferring_access_checks (dk_no_deferred);
23169
23170 /* Unless an explicit instantiation directive has already determined
23171 the linkage of D, remember that a definition is available for
23172 this entity. */
23173 if (pattern_defined
23174 && !DECL_INTERFACE_KNOWN (d)
23175 && !DECL_NOT_REALLY_EXTERN (d))
23176 mark_definable (d);
23177
23178 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23179 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23180 input_location = DECL_SOURCE_LOCATION (d);
23181
23182 /* If D is a member of an explicitly instantiated class template,
23183 and no definition is available, treat it like an implicit
23184 instantiation. */
23185 if (!pattern_defined && expl_inst_class_mem_p
23186 && DECL_EXPLICIT_INSTANTIATION (d))
23187 {
23188 /* Leave linkage flags alone on instantiations with anonymous
23189 visibility. */
23190 if (TREE_PUBLIC (d))
23191 {
23192 DECL_NOT_REALLY_EXTERN (d) = 0;
23193 DECL_INTERFACE_KNOWN (d) = 0;
23194 }
23195 SET_DECL_IMPLICIT_INSTANTIATION (d);
23196 }
23197
23198 /* Defer all other templates, unless we have been explicitly
23199 forbidden from doing so. */
23200 if (/* If there is no definition, we cannot instantiate the
23201 template. */
23202 ! pattern_defined
23203 /* If it's OK to postpone instantiation, do so. */
23204 || defer_ok
23205 /* If this is a static data member that will be defined
23206 elsewhere, we don't want to instantiate the entire data
23207 member, but we do want to instantiate the initializer so that
23208 we can substitute that elsewhere. */
23209 || (external_p && VAR_P (d))
23210 /* Handle here a deleted function too, avoid generating
23211 its body (c++/61080). */
23212 || deleted_p)
23213 {
23214 /* The definition of the static data member is now required so
23215 we must substitute the initializer. */
23216 if (VAR_P (d)
23217 && !DECL_INITIAL (d)
23218 && DECL_INITIAL (code_pattern))
23219 {
23220 tree ns;
23221 tree init;
23222 bool const_init = false;
23223 bool enter_context = DECL_CLASS_SCOPE_P (d);
23224
23225 ns = decl_namespace_context (d);
23226 push_nested_namespace (ns);
23227 if (enter_context)
23228 push_nested_class (DECL_CONTEXT (d));
23229 init = tsubst_expr (DECL_INITIAL (code_pattern),
23230 args,
23231 tf_warning_or_error, NULL_TREE,
23232 /*integral_constant_expression_p=*/false);
23233 /* If instantiating the initializer involved instantiating this
23234 again, don't call cp_finish_decl twice. */
23235 if (!DECL_INITIAL (d))
23236 {
23237 /* Make sure the initializer is still constant, in case of
23238 circular dependency (template/instantiate6.C). */
23239 const_init
23240 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23241 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23242 /*asmspec_tree=*/NULL_TREE,
23243 LOOKUP_ONLYCONVERTING);
23244 }
23245 if (enter_context)
23246 pop_nested_class ();
23247 pop_nested_namespace (ns);
23248 }
23249
23250 /* We restore the source position here because it's used by
23251 add_pending_template. */
23252 input_location = saved_loc;
23253
23254 if (at_eof && !pattern_defined
23255 && DECL_EXPLICIT_INSTANTIATION (d)
23256 && DECL_NOT_REALLY_EXTERN (d))
23257 /* [temp.explicit]
23258
23259 The definition of a non-exported function template, a
23260 non-exported member function template, or a non-exported
23261 member function or static data member of a class template
23262 shall be present in every translation unit in which it is
23263 explicitly instantiated. */
23264 permerror (input_location, "explicit instantiation of %qD "
23265 "but no definition available", d);
23266
23267 /* If we're in unevaluated context, we just wanted to get the
23268 constant value; this isn't an odr use, so don't queue
23269 a full instantiation. */
23270 if (cp_unevaluated_operand != 0)
23271 goto out;
23272 /* ??? Historically, we have instantiated inline functions, even
23273 when marked as "extern template". */
23274 if (!(external_p && VAR_P (d)))
23275 add_pending_template (d);
23276 goto out;
23277 }
23278 /* Tell the repository that D is available in this translation unit
23279 -- and see if it is supposed to be instantiated here. */
23280 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23281 {
23282 /* In a PCH file, despite the fact that the repository hasn't
23283 requested instantiation in the PCH it is still possible that
23284 an instantiation will be required in a file that includes the
23285 PCH. */
23286 if (pch_file)
23287 add_pending_template (d);
23288 /* Instantiate inline functions so that the inliner can do its
23289 job, even though we'll not be emitting a copy of this
23290 function. */
23291 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23292 goto out;
23293 }
23294
23295 bool push_to_top, nested;
23296 tree fn_context;
23297 fn_context = decl_function_context (d);
23298 nested = current_function_decl != NULL_TREE;
23299 push_to_top = !(nested && fn_context == current_function_decl);
23300
23301 vec<tree> omp_privatization_save;
23302 if (nested)
23303 save_omp_privatization_clauses (omp_privatization_save);
23304
23305 if (push_to_top)
23306 push_to_top_level ();
23307 else
23308 {
23309 push_function_context ();
23310 cp_unevaluated_operand = 0;
23311 c_inhibit_evaluation_warnings = 0;
23312 }
23313
23314 /* Mark D as instantiated so that recursive calls to
23315 instantiate_decl do not try to instantiate it again. */
23316 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23317
23318 /* Regenerate the declaration in case the template has been modified
23319 by a subsequent redeclaration. */
23320 regenerate_decl_from_template (d, td, args);
23321
23322 /* We already set the file and line above. Reset them now in case
23323 they changed as a result of calling regenerate_decl_from_template. */
23324 input_location = DECL_SOURCE_LOCATION (d);
23325
23326 if (VAR_P (d))
23327 {
23328 tree init;
23329 bool const_init = false;
23330
23331 /* Clear out DECL_RTL; whatever was there before may not be right
23332 since we've reset the type of the declaration. */
23333 SET_DECL_RTL (d, NULL);
23334 DECL_IN_AGGR_P (d) = 0;
23335
23336 /* The initializer is placed in DECL_INITIAL by
23337 regenerate_decl_from_template so we don't need to
23338 push/pop_access_scope again here. Pull it out so that
23339 cp_finish_decl can process it. */
23340 init = DECL_INITIAL (d);
23341 DECL_INITIAL (d) = NULL_TREE;
23342 DECL_INITIALIZED_P (d) = 0;
23343
23344 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23345 initializer. That function will defer actual emission until
23346 we have a chance to determine linkage. */
23347 DECL_EXTERNAL (d) = 0;
23348
23349 /* Enter the scope of D so that access-checking works correctly. */
23350 bool enter_context = DECL_CLASS_SCOPE_P (d);
23351 if (enter_context)
23352 push_nested_class (DECL_CONTEXT (d));
23353
23354 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23355 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23356
23357 if (enter_context)
23358 pop_nested_class ();
23359
23360 if (variable_template_p (gen_tmpl))
23361 note_variable_template_instantiation (d);
23362 }
23363 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23364 synthesize_method (d);
23365 else if (TREE_CODE (d) == FUNCTION_DECL)
23366 {
23367 /* Set up the list of local specializations. */
23368 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23369 tree block = NULL_TREE;
23370
23371 /* Set up context. */
23372 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23373 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23374 block = push_stmt_list ();
23375 else
23376 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23377
23378 /* Some typedefs referenced from within the template code need to be
23379 access checked at template instantiation time, i.e now. These
23380 types were added to the template at parsing time. Let's get those
23381 and perform the access checks then. */
23382 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23383 args);
23384
23385 /* Create substitution entries for the parameters. */
23386 register_parameter_specializations (code_pattern, d);
23387
23388 /* Substitute into the body of the function. */
23389 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23390 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23391 tf_warning_or_error, tmpl);
23392 else
23393 {
23394 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23395 tf_warning_or_error, tmpl,
23396 /*integral_constant_expression_p=*/false);
23397
23398 /* Set the current input_location to the end of the function
23399 so that finish_function knows where we are. */
23400 input_location
23401 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23402
23403 /* Remember if we saw an infinite loop in the template. */
23404 current_function_infinite_loop
23405 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23406 }
23407
23408 /* Finish the function. */
23409 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23410 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23411 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23412 else
23413 {
23414 d = finish_function (/*inline_p=*/false);
23415 expand_or_defer_fn (d);
23416 }
23417
23418 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23419 cp_check_omp_declare_reduction (d);
23420 }
23421
23422 /* We're not deferring instantiation any more. */
23423 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23424
23425 if (push_to_top)
23426 pop_from_top_level ();
23427 else
23428 pop_function_context ();
23429
23430 if (nested)
23431 restore_omp_privatization_clauses (omp_privatization_save);
23432
23433 out:
23434 pop_deferring_access_checks ();
23435 timevar_pop (TV_TEMPLATE_INST);
23436 pop_tinst_level ();
23437 input_location = saved_loc;
23438 cp_unevaluated_operand = saved_unevaluated_operand;
23439 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23440
23441 return d;
23442 }
23443
23444 /* Run through the list of templates that we wish we could
23445 instantiate, and instantiate any we can. RETRIES is the
23446 number of times we retry pending template instantiation. */
23447
23448 void
23449 instantiate_pending_templates (int retries)
23450 {
23451 int reconsider;
23452 location_t saved_loc = input_location;
23453
23454 /* Instantiating templates may trigger vtable generation. This in turn
23455 may require further template instantiations. We place a limit here
23456 to avoid infinite loop. */
23457 if (pending_templates && retries >= max_tinst_depth)
23458 {
23459 tree decl = pending_templates->tinst->decl;
23460
23461 fatal_error (input_location,
23462 "template instantiation depth exceeds maximum of %d"
23463 " instantiating %q+D, possibly from virtual table generation"
23464 " (use -ftemplate-depth= to increase the maximum)",
23465 max_tinst_depth, decl);
23466 if (TREE_CODE (decl) == FUNCTION_DECL)
23467 /* Pretend that we defined it. */
23468 DECL_INITIAL (decl) = error_mark_node;
23469 return;
23470 }
23471
23472 do
23473 {
23474 struct pending_template **t = &pending_templates;
23475 struct pending_template *last = NULL;
23476 reconsider = 0;
23477 while (*t)
23478 {
23479 tree instantiation = reopen_tinst_level ((*t)->tinst);
23480 bool complete = false;
23481
23482 if (TYPE_P (instantiation))
23483 {
23484 if (!COMPLETE_TYPE_P (instantiation))
23485 {
23486 instantiate_class_template (instantiation);
23487 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23488 for (tree fld = TYPE_FIELDS (instantiation);
23489 fld; fld = TREE_CHAIN (fld))
23490 if ((VAR_P (fld)
23491 || (TREE_CODE (fld) == FUNCTION_DECL
23492 && !DECL_ARTIFICIAL (fld)))
23493 && DECL_TEMPLATE_INSTANTIATION (fld))
23494 instantiate_decl (fld,
23495 /*defer_ok=*/false,
23496 /*expl_inst_class_mem_p=*/false);
23497
23498 if (COMPLETE_TYPE_P (instantiation))
23499 reconsider = 1;
23500 }
23501
23502 complete = COMPLETE_TYPE_P (instantiation);
23503 }
23504 else
23505 {
23506 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23507 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23508 {
23509 instantiation
23510 = instantiate_decl (instantiation,
23511 /*defer_ok=*/false,
23512 /*expl_inst_class_mem_p=*/false);
23513 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23514 reconsider = 1;
23515 }
23516
23517 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23518 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23519 }
23520
23521 if (complete)
23522 /* If INSTANTIATION has been instantiated, then we don't
23523 need to consider it again in the future. */
23524 *t = (*t)->next;
23525 else
23526 {
23527 last = *t;
23528 t = &(*t)->next;
23529 }
23530 tinst_depth = 0;
23531 current_tinst_level = NULL;
23532 }
23533 last_pending_template = last;
23534 }
23535 while (reconsider);
23536
23537 input_location = saved_loc;
23538 }
23539
23540 /* Substitute ARGVEC into T, which is a list of initializers for
23541 either base class or a non-static data member. The TREE_PURPOSEs
23542 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23543 instantiate_decl. */
23544
23545 static tree
23546 tsubst_initializer_list (tree t, tree argvec)
23547 {
23548 tree inits = NULL_TREE;
23549
23550 for (; t; t = TREE_CHAIN (t))
23551 {
23552 tree decl;
23553 tree init;
23554 tree expanded_bases = NULL_TREE;
23555 tree expanded_arguments = NULL_TREE;
23556 int i, len = 1;
23557
23558 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23559 {
23560 tree expr;
23561 tree arg;
23562
23563 /* Expand the base class expansion type into separate base
23564 classes. */
23565 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23566 tf_warning_or_error,
23567 NULL_TREE);
23568 if (expanded_bases == error_mark_node)
23569 continue;
23570
23571 /* We'll be building separate TREE_LISTs of arguments for
23572 each base. */
23573 len = TREE_VEC_LENGTH (expanded_bases);
23574 expanded_arguments = make_tree_vec (len);
23575 for (i = 0; i < len; i++)
23576 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23577
23578 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23579 expand each argument in the TREE_VALUE of t. */
23580 expr = make_node (EXPR_PACK_EXPANSION);
23581 PACK_EXPANSION_LOCAL_P (expr) = true;
23582 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23583 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23584
23585 if (TREE_VALUE (t) == void_type_node)
23586 /* VOID_TYPE_NODE is used to indicate
23587 value-initialization. */
23588 {
23589 for (i = 0; i < len; i++)
23590 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23591 }
23592 else
23593 {
23594 /* Substitute parameter packs into each argument in the
23595 TREE_LIST. */
23596 in_base_initializer = 1;
23597 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23598 {
23599 tree expanded_exprs;
23600
23601 /* Expand the argument. */
23602 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23603 expanded_exprs
23604 = tsubst_pack_expansion (expr, argvec,
23605 tf_warning_or_error,
23606 NULL_TREE);
23607 if (expanded_exprs == error_mark_node)
23608 continue;
23609
23610 /* Prepend each of the expanded expressions to the
23611 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23612 for (i = 0; i < len; i++)
23613 {
23614 TREE_VEC_ELT (expanded_arguments, i) =
23615 tree_cons (NULL_TREE,
23616 TREE_VEC_ELT (expanded_exprs, i),
23617 TREE_VEC_ELT (expanded_arguments, i));
23618 }
23619 }
23620 in_base_initializer = 0;
23621
23622 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23623 since we built them backwards. */
23624 for (i = 0; i < len; i++)
23625 {
23626 TREE_VEC_ELT (expanded_arguments, i) =
23627 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23628 }
23629 }
23630 }
23631
23632 for (i = 0; i < len; ++i)
23633 {
23634 if (expanded_bases)
23635 {
23636 decl = TREE_VEC_ELT (expanded_bases, i);
23637 decl = expand_member_init (decl);
23638 init = TREE_VEC_ELT (expanded_arguments, i);
23639 }
23640 else
23641 {
23642 tree tmp;
23643 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23644 tf_warning_or_error, NULL_TREE);
23645
23646 decl = expand_member_init (decl);
23647 if (decl && !DECL_P (decl))
23648 in_base_initializer = 1;
23649
23650 init = TREE_VALUE (t);
23651 tmp = init;
23652 if (init != void_type_node)
23653 init = tsubst_expr (init, argvec,
23654 tf_warning_or_error, NULL_TREE,
23655 /*integral_constant_expression_p=*/false);
23656 if (init == NULL_TREE && tmp != NULL_TREE)
23657 /* If we had an initializer but it instantiated to nothing,
23658 value-initialize the object. This will only occur when
23659 the initializer was a pack expansion where the parameter
23660 packs used in that expansion were of length zero. */
23661 init = void_type_node;
23662 in_base_initializer = 0;
23663 }
23664
23665 if (decl)
23666 {
23667 init = build_tree_list (decl, init);
23668 TREE_CHAIN (init) = inits;
23669 inits = init;
23670 }
23671 }
23672 }
23673 return inits;
23674 }
23675
23676 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23677
23678 static void
23679 set_current_access_from_decl (tree decl)
23680 {
23681 if (TREE_PRIVATE (decl))
23682 current_access_specifier = access_private_node;
23683 else if (TREE_PROTECTED (decl))
23684 current_access_specifier = access_protected_node;
23685 else
23686 current_access_specifier = access_public_node;
23687 }
23688
23689 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23690 is the instantiation (which should have been created with
23691 start_enum) and ARGS are the template arguments to use. */
23692
23693 static void
23694 tsubst_enum (tree tag, tree newtag, tree args)
23695 {
23696 tree e;
23697
23698 if (SCOPED_ENUM_P (newtag))
23699 begin_scope (sk_scoped_enum, newtag);
23700
23701 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23702 {
23703 tree value;
23704 tree decl;
23705
23706 decl = TREE_VALUE (e);
23707 /* Note that in a template enum, the TREE_VALUE is the
23708 CONST_DECL, not the corresponding INTEGER_CST. */
23709 value = tsubst_expr (DECL_INITIAL (decl),
23710 args, tf_warning_or_error, NULL_TREE,
23711 /*integral_constant_expression_p=*/true);
23712
23713 /* Give this enumeration constant the correct access. */
23714 set_current_access_from_decl (decl);
23715
23716 /* Actually build the enumerator itself. Here we're assuming that
23717 enumerators can't have dependent attributes. */
23718 build_enumerator (DECL_NAME (decl), value, newtag,
23719 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23720 }
23721
23722 if (SCOPED_ENUM_P (newtag))
23723 finish_scope ();
23724
23725 finish_enum_value_list (newtag);
23726 finish_enum (newtag);
23727
23728 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23729 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23730 }
23731
23732 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23733 its type -- but without substituting the innermost set of template
23734 arguments. So, innermost set of template parameters will appear in
23735 the type. */
23736
23737 tree
23738 get_mostly_instantiated_function_type (tree decl)
23739 {
23740 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23741 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23742 }
23743
23744 /* Return truthvalue if we're processing a template different from
23745 the last one involved in diagnostics. */
23746 bool
23747 problematic_instantiation_changed (void)
23748 {
23749 return current_tinst_level != last_error_tinst_level;
23750 }
23751
23752 /* Remember current template involved in diagnostics. */
23753 void
23754 record_last_problematic_instantiation (void)
23755 {
23756 last_error_tinst_level = current_tinst_level;
23757 }
23758
23759 struct tinst_level *
23760 current_instantiation (void)
23761 {
23762 return current_tinst_level;
23763 }
23764
23765 /* Return TRUE if current_function_decl is being instantiated, false
23766 otherwise. */
23767
23768 bool
23769 instantiating_current_function_p (void)
23770 {
23771 return (current_instantiation ()
23772 && current_instantiation ()->decl == current_function_decl);
23773 }
23774
23775 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23776 type. Return false for ok, true for disallowed. Issue error and
23777 inform messages under control of COMPLAIN. */
23778
23779 static bool
23780 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23781 {
23782 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23783 return false;
23784 else if (POINTER_TYPE_P (type))
23785 return false;
23786 else if (TYPE_PTRMEM_P (type))
23787 return false;
23788 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23789 return false;
23790 else if (TREE_CODE (type) == TYPENAME_TYPE)
23791 return false;
23792 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23793 return false;
23794 else if (TREE_CODE (type) == NULLPTR_TYPE)
23795 return false;
23796 /* A bound template template parm could later be instantiated to have a valid
23797 nontype parm type via an alias template. */
23798 else if (cxx_dialect >= cxx11
23799 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23800 return false;
23801
23802 if (complain & tf_error)
23803 {
23804 if (type == error_mark_node)
23805 inform (input_location, "invalid template non-type parameter");
23806 else
23807 error ("%q#T is not a valid type for a template non-type parameter",
23808 type);
23809 }
23810 return true;
23811 }
23812
23813 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23814 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23815
23816 static bool
23817 dependent_type_p_r (tree type)
23818 {
23819 tree scope;
23820
23821 /* [temp.dep.type]
23822
23823 A type is dependent if it is:
23824
23825 -- a template parameter. Template template parameters are types
23826 for us (since TYPE_P holds true for them) so we handle
23827 them here. */
23828 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23829 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23830 return true;
23831 /* -- a qualified-id with a nested-name-specifier which contains a
23832 class-name that names a dependent type or whose unqualified-id
23833 names a dependent type. */
23834 if (TREE_CODE (type) == TYPENAME_TYPE)
23835 return true;
23836
23837 /* An alias template specialization can be dependent even if the
23838 resulting type is not. */
23839 if (dependent_alias_template_spec_p (type))
23840 return true;
23841
23842 /* -- a cv-qualified type where the cv-unqualified type is
23843 dependent.
23844 No code is necessary for this bullet; the code below handles
23845 cv-qualified types, and we don't want to strip aliases with
23846 TYPE_MAIN_VARIANT because of DR 1558. */
23847 /* -- a compound type constructed from any dependent type. */
23848 if (TYPE_PTRMEM_P (type))
23849 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23850 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23851 (type)));
23852 else if (TYPE_PTR_P (type)
23853 || TREE_CODE (type) == REFERENCE_TYPE)
23854 return dependent_type_p (TREE_TYPE (type));
23855 else if (TREE_CODE (type) == FUNCTION_TYPE
23856 || TREE_CODE (type) == METHOD_TYPE)
23857 {
23858 tree arg_type;
23859
23860 if (dependent_type_p (TREE_TYPE (type)))
23861 return true;
23862 for (arg_type = TYPE_ARG_TYPES (type);
23863 arg_type;
23864 arg_type = TREE_CHAIN (arg_type))
23865 if (dependent_type_p (TREE_VALUE (arg_type)))
23866 return true;
23867 if (cxx_dialect >= cxx17)
23868 /* A value-dependent noexcept-specifier makes the type dependent. */
23869 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23870 if (tree noex = TREE_PURPOSE (spec))
23871 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23872 affect overload resolution and treating it as dependent breaks
23873 things. */
23874 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23875 && value_dependent_expression_p (noex))
23876 return true;
23877 return false;
23878 }
23879 /* -- an array type constructed from any dependent type or whose
23880 size is specified by a constant expression that is
23881 value-dependent.
23882
23883 We checked for type- and value-dependence of the bounds in
23884 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23885 if (TREE_CODE (type) == ARRAY_TYPE)
23886 {
23887 if (TYPE_DOMAIN (type)
23888 && dependent_type_p (TYPE_DOMAIN (type)))
23889 return true;
23890 return dependent_type_p (TREE_TYPE (type));
23891 }
23892
23893 /* -- a template-id in which either the template name is a template
23894 parameter ... */
23895 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23896 return true;
23897 /* ... or any of the template arguments is a dependent type or
23898 an expression that is type-dependent or value-dependent. */
23899 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23900 && (any_dependent_template_arguments_p
23901 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23902 return true;
23903
23904 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23905 dependent; if the argument of the `typeof' expression is not
23906 type-dependent, then it should already been have resolved. */
23907 if (TREE_CODE (type) == TYPEOF_TYPE
23908 || TREE_CODE (type) == DECLTYPE_TYPE
23909 || TREE_CODE (type) == UNDERLYING_TYPE)
23910 return true;
23911
23912 /* A template argument pack is dependent if any of its packed
23913 arguments are. */
23914 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23915 {
23916 tree args = ARGUMENT_PACK_ARGS (type);
23917 int i, len = TREE_VEC_LENGTH (args);
23918 for (i = 0; i < len; ++i)
23919 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23920 return true;
23921 }
23922
23923 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23924 be template parameters. */
23925 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23926 return true;
23927
23928 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23929 return true;
23930
23931 /* The standard does not specifically mention types that are local
23932 to template functions or local classes, but they should be
23933 considered dependent too. For example:
23934
23935 template <int I> void f() {
23936 enum E { a = I };
23937 S<sizeof (E)> s;
23938 }
23939
23940 The size of `E' cannot be known until the value of `I' has been
23941 determined. Therefore, `E' must be considered dependent. */
23942 scope = TYPE_CONTEXT (type);
23943 if (scope && TYPE_P (scope))
23944 return dependent_type_p (scope);
23945 /* Don't use type_dependent_expression_p here, as it can lead
23946 to infinite recursion trying to determine whether a lambda
23947 nested in a lambda is dependent (c++/47687). */
23948 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23949 && DECL_LANG_SPECIFIC (scope)
23950 && DECL_TEMPLATE_INFO (scope)
23951 && (any_dependent_template_arguments_p
23952 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23953 return true;
23954
23955 /* Other types are non-dependent. */
23956 return false;
23957 }
23958
23959 /* Returns TRUE if TYPE is dependent, in the sense of
23960 [temp.dep.type]. Note that a NULL type is considered dependent. */
23961
23962 bool
23963 dependent_type_p (tree type)
23964 {
23965 /* If there are no template parameters in scope, then there can't be
23966 any dependent types. */
23967 if (!processing_template_decl)
23968 {
23969 /* If we are not processing a template, then nobody should be
23970 providing us with a dependent type. */
23971 gcc_assert (type);
23972 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23973 return false;
23974 }
23975
23976 /* If the type is NULL, we have not computed a type for the entity
23977 in question; in that case, the type is dependent. */
23978 if (!type)
23979 return true;
23980
23981 /* Erroneous types can be considered non-dependent. */
23982 if (type == error_mark_node)
23983 return false;
23984
23985 /* Getting here with global_type_node means we improperly called this
23986 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23987 gcc_checking_assert (type != global_type_node);
23988
23989 /* If we have not already computed the appropriate value for TYPE,
23990 do so now. */
23991 if (!TYPE_DEPENDENT_P_VALID (type))
23992 {
23993 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23994 TYPE_DEPENDENT_P_VALID (type) = 1;
23995 }
23996
23997 return TYPE_DEPENDENT_P (type);
23998 }
23999
24000 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24001 lookup. In other words, a dependent type that is not the current
24002 instantiation. */
24003
24004 bool
24005 dependent_scope_p (tree scope)
24006 {
24007 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24008 && !currently_open_class (scope));
24009 }
24010
24011 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24012 an unknown base of 'this' (and is therefore instantiation-dependent). */
24013
24014 static bool
24015 unknown_base_ref_p (tree t)
24016 {
24017 if (!current_class_ptr)
24018 return false;
24019
24020 tree mem = TREE_OPERAND (t, 1);
24021 if (shared_member_p (mem))
24022 return false;
24023
24024 tree cur = current_nonlambda_class_type ();
24025 if (!any_dependent_bases_p (cur))
24026 return false;
24027
24028 tree ctx = TREE_OPERAND (t, 0);
24029 if (DERIVED_FROM_P (ctx, cur))
24030 return false;
24031
24032 return true;
24033 }
24034
24035 /* T is a SCOPE_REF; return whether we need to consider it
24036 instantiation-dependent so that we can check access at instantiation
24037 time even though we know which member it resolves to. */
24038
24039 static bool
24040 instantiation_dependent_scope_ref_p (tree t)
24041 {
24042 if (DECL_P (TREE_OPERAND (t, 1))
24043 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24044 && !unknown_base_ref_p (t)
24045 && accessible_in_template_p (TREE_OPERAND (t, 0),
24046 TREE_OPERAND (t, 1)))
24047 return false;
24048 else
24049 return true;
24050 }
24051
24052 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24053 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24054 expression. */
24055
24056 /* Note that this predicate is not appropriate for general expressions;
24057 only constant expressions (that satisfy potential_constant_expression)
24058 can be tested for value dependence. */
24059
24060 bool
24061 value_dependent_expression_p (tree expression)
24062 {
24063 if (!processing_template_decl || expression == NULL_TREE)
24064 return false;
24065
24066 /* A type-dependent expression is also value-dependent. */
24067 if (type_dependent_expression_p (expression))
24068 return true;
24069
24070 switch (TREE_CODE (expression))
24071 {
24072 case BASELINK:
24073 /* A dependent member function of the current instantiation. */
24074 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24075
24076 case FUNCTION_DECL:
24077 /* A dependent member function of the current instantiation. */
24078 if (DECL_CLASS_SCOPE_P (expression)
24079 && dependent_type_p (DECL_CONTEXT (expression)))
24080 return true;
24081 break;
24082
24083 case IDENTIFIER_NODE:
24084 /* A name that has not been looked up -- must be dependent. */
24085 return true;
24086
24087 case TEMPLATE_PARM_INDEX:
24088 /* A non-type template parm. */
24089 return true;
24090
24091 case CONST_DECL:
24092 /* A non-type template parm. */
24093 if (DECL_TEMPLATE_PARM_P (expression))
24094 return true;
24095 return value_dependent_expression_p (DECL_INITIAL (expression));
24096
24097 case VAR_DECL:
24098 /* A constant with literal type and is initialized
24099 with an expression that is value-dependent. */
24100 if (DECL_DEPENDENT_INIT_P (expression)
24101 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24102 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24103 return true;
24104 if (DECL_HAS_VALUE_EXPR_P (expression))
24105 {
24106 tree value_expr = DECL_VALUE_EXPR (expression);
24107 if (value_dependent_expression_p (value_expr))
24108 return true;
24109 }
24110 return false;
24111
24112 case DYNAMIC_CAST_EXPR:
24113 case STATIC_CAST_EXPR:
24114 case CONST_CAST_EXPR:
24115 case REINTERPRET_CAST_EXPR:
24116 case CAST_EXPR:
24117 case IMPLICIT_CONV_EXPR:
24118 /* These expressions are value-dependent if the type to which
24119 the cast occurs is dependent or the expression being casted
24120 is value-dependent. */
24121 {
24122 tree type = TREE_TYPE (expression);
24123
24124 if (dependent_type_p (type))
24125 return true;
24126
24127 /* A functional cast has a list of operands. */
24128 expression = TREE_OPERAND (expression, 0);
24129 if (!expression)
24130 {
24131 /* If there are no operands, it must be an expression such
24132 as "int()". This should not happen for aggregate types
24133 because it would form non-constant expressions. */
24134 gcc_assert (cxx_dialect >= cxx11
24135 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24136
24137 return false;
24138 }
24139
24140 if (TREE_CODE (expression) == TREE_LIST)
24141 return any_value_dependent_elements_p (expression);
24142
24143 return value_dependent_expression_p (expression);
24144 }
24145
24146 case SIZEOF_EXPR:
24147 if (SIZEOF_EXPR_TYPE_P (expression))
24148 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24149 /* FALLTHRU */
24150 case ALIGNOF_EXPR:
24151 case TYPEID_EXPR:
24152 /* A `sizeof' expression is value-dependent if the operand is
24153 type-dependent or is a pack expansion. */
24154 expression = TREE_OPERAND (expression, 0);
24155 if (PACK_EXPANSION_P (expression))
24156 return true;
24157 else if (TYPE_P (expression))
24158 return dependent_type_p (expression);
24159 return instantiation_dependent_uneval_expression_p (expression);
24160
24161 case AT_ENCODE_EXPR:
24162 /* An 'encode' expression is value-dependent if the operand is
24163 type-dependent. */
24164 expression = TREE_OPERAND (expression, 0);
24165 return dependent_type_p (expression);
24166
24167 case NOEXCEPT_EXPR:
24168 expression = TREE_OPERAND (expression, 0);
24169 return instantiation_dependent_uneval_expression_p (expression);
24170
24171 case SCOPE_REF:
24172 /* All instantiation-dependent expressions should also be considered
24173 value-dependent. */
24174 return instantiation_dependent_scope_ref_p (expression);
24175
24176 case COMPONENT_REF:
24177 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24178 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24179
24180 case NONTYPE_ARGUMENT_PACK:
24181 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24182 is value-dependent. */
24183 {
24184 tree values = ARGUMENT_PACK_ARGS (expression);
24185 int i, len = TREE_VEC_LENGTH (values);
24186
24187 for (i = 0; i < len; ++i)
24188 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24189 return true;
24190
24191 return false;
24192 }
24193
24194 case TRAIT_EXPR:
24195 {
24196 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24197
24198 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24199 return true;
24200
24201 if (!type2)
24202 return false;
24203
24204 if (TREE_CODE (type2) != TREE_LIST)
24205 return dependent_type_p (type2);
24206
24207 for (; type2; type2 = TREE_CHAIN (type2))
24208 if (dependent_type_p (TREE_VALUE (type2)))
24209 return true;
24210
24211 return false;
24212 }
24213
24214 case MODOP_EXPR:
24215 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24216 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24217
24218 case ARRAY_REF:
24219 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24220 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24221
24222 case ADDR_EXPR:
24223 {
24224 tree op = TREE_OPERAND (expression, 0);
24225 return (value_dependent_expression_p (op)
24226 || has_value_dependent_address (op));
24227 }
24228
24229 case REQUIRES_EXPR:
24230 /* Treat all requires-expressions as value-dependent so
24231 we don't try to fold them. */
24232 return true;
24233
24234 case TYPE_REQ:
24235 return dependent_type_p (TREE_OPERAND (expression, 0));
24236
24237 case CALL_EXPR:
24238 {
24239 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24240 return true;
24241 tree fn = get_callee_fndecl (expression);
24242 int i, nargs;
24243 nargs = call_expr_nargs (expression);
24244 for (i = 0; i < nargs; ++i)
24245 {
24246 tree op = CALL_EXPR_ARG (expression, i);
24247 /* In a call to a constexpr member function, look through the
24248 implicit ADDR_EXPR on the object argument so that it doesn't
24249 cause the call to be considered value-dependent. We also
24250 look through it in potential_constant_expression. */
24251 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24252 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24253 && TREE_CODE (op) == ADDR_EXPR)
24254 op = TREE_OPERAND (op, 0);
24255 if (value_dependent_expression_p (op))
24256 return true;
24257 }
24258 return false;
24259 }
24260
24261 case TEMPLATE_ID_EXPR:
24262 return variable_concept_p (TREE_OPERAND (expression, 0));
24263
24264 case CONSTRUCTOR:
24265 {
24266 unsigned ix;
24267 tree val;
24268 if (dependent_type_p (TREE_TYPE (expression)))
24269 return true;
24270 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24271 if (value_dependent_expression_p (val))
24272 return true;
24273 return false;
24274 }
24275
24276 case STMT_EXPR:
24277 /* Treat a GNU statement expression as dependent to avoid crashing
24278 under instantiate_non_dependent_expr; it can't be constant. */
24279 return true;
24280
24281 default:
24282 /* A constant expression is value-dependent if any subexpression is
24283 value-dependent. */
24284 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24285 {
24286 case tcc_reference:
24287 case tcc_unary:
24288 case tcc_comparison:
24289 case tcc_binary:
24290 case tcc_expression:
24291 case tcc_vl_exp:
24292 {
24293 int i, len = cp_tree_operand_length (expression);
24294
24295 for (i = 0; i < len; i++)
24296 {
24297 tree t = TREE_OPERAND (expression, i);
24298
24299 /* In some cases, some of the operands may be missing.
24300 (For example, in the case of PREDECREMENT_EXPR, the
24301 amount to increment by may be missing.) That doesn't
24302 make the expression dependent. */
24303 if (t && value_dependent_expression_p (t))
24304 return true;
24305 }
24306 }
24307 break;
24308 default:
24309 break;
24310 }
24311 break;
24312 }
24313
24314 /* The expression is not value-dependent. */
24315 return false;
24316 }
24317
24318 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24319 [temp.dep.expr]. Note that an expression with no type is
24320 considered dependent. Other parts of the compiler arrange for an
24321 expression with type-dependent subexpressions to have no type, so
24322 this function doesn't have to be fully recursive. */
24323
24324 bool
24325 type_dependent_expression_p (tree expression)
24326 {
24327 if (!processing_template_decl)
24328 return false;
24329
24330 if (expression == NULL_TREE || expression == error_mark_node)
24331 return false;
24332
24333 STRIP_ANY_LOCATION_WRAPPER (expression);
24334
24335 /* An unresolved name is always dependent. */
24336 if (identifier_p (expression)
24337 || TREE_CODE (expression) == USING_DECL
24338 || TREE_CODE (expression) == WILDCARD_DECL)
24339 return true;
24340
24341 /* A fold expression is type-dependent. */
24342 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24343 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24344 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24345 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24346 return true;
24347
24348 /* Some expression forms are never type-dependent. */
24349 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24350 || TREE_CODE (expression) == SIZEOF_EXPR
24351 || TREE_CODE (expression) == ALIGNOF_EXPR
24352 || TREE_CODE (expression) == AT_ENCODE_EXPR
24353 || TREE_CODE (expression) == NOEXCEPT_EXPR
24354 || TREE_CODE (expression) == TRAIT_EXPR
24355 || TREE_CODE (expression) == TYPEID_EXPR
24356 || TREE_CODE (expression) == DELETE_EXPR
24357 || TREE_CODE (expression) == VEC_DELETE_EXPR
24358 || TREE_CODE (expression) == THROW_EXPR
24359 || TREE_CODE (expression) == REQUIRES_EXPR)
24360 return false;
24361
24362 /* The types of these expressions depends only on the type to which
24363 the cast occurs. */
24364 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24365 || TREE_CODE (expression) == STATIC_CAST_EXPR
24366 || TREE_CODE (expression) == CONST_CAST_EXPR
24367 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24368 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24369 || TREE_CODE (expression) == CAST_EXPR)
24370 return dependent_type_p (TREE_TYPE (expression));
24371
24372 /* The types of these expressions depends only on the type created
24373 by the expression. */
24374 if (TREE_CODE (expression) == NEW_EXPR
24375 || TREE_CODE (expression) == VEC_NEW_EXPR)
24376 {
24377 /* For NEW_EXPR tree nodes created inside a template, either
24378 the object type itself or a TREE_LIST may appear as the
24379 operand 1. */
24380 tree type = TREE_OPERAND (expression, 1);
24381 if (TREE_CODE (type) == TREE_LIST)
24382 /* This is an array type. We need to check array dimensions
24383 as well. */
24384 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24385 || value_dependent_expression_p
24386 (TREE_OPERAND (TREE_VALUE (type), 1));
24387 else
24388 return dependent_type_p (type);
24389 }
24390
24391 if (TREE_CODE (expression) == SCOPE_REF)
24392 {
24393 tree scope = TREE_OPERAND (expression, 0);
24394 tree name = TREE_OPERAND (expression, 1);
24395
24396 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24397 contains an identifier associated by name lookup with one or more
24398 declarations declared with a dependent type, or...a
24399 nested-name-specifier or qualified-id that names a member of an
24400 unknown specialization. */
24401 return (type_dependent_expression_p (name)
24402 || dependent_scope_p (scope));
24403 }
24404
24405 if (TREE_CODE (expression) == TEMPLATE_DECL
24406 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24407 return uses_outer_template_parms (expression);
24408
24409 if (TREE_CODE (expression) == STMT_EXPR)
24410 expression = stmt_expr_value_expr (expression);
24411
24412 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24413 {
24414 tree elt;
24415 unsigned i;
24416
24417 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24418 {
24419 if (type_dependent_expression_p (elt))
24420 return true;
24421 }
24422 return false;
24423 }
24424
24425 /* A static data member of the current instantiation with incomplete
24426 array type is type-dependent, as the definition and specializations
24427 can have different bounds. */
24428 if (VAR_P (expression)
24429 && DECL_CLASS_SCOPE_P (expression)
24430 && dependent_type_p (DECL_CONTEXT (expression))
24431 && VAR_HAD_UNKNOWN_BOUND (expression))
24432 return true;
24433
24434 /* An array of unknown bound depending on a variadic parameter, eg:
24435
24436 template<typename... Args>
24437 void foo (Args... args)
24438 {
24439 int arr[] = { args... };
24440 }
24441
24442 template<int... vals>
24443 void bar ()
24444 {
24445 int arr[] = { vals... };
24446 }
24447
24448 If the array has no length and has an initializer, it must be that
24449 we couldn't determine its length in cp_complete_array_type because
24450 it is dependent. */
24451 if (VAR_P (expression)
24452 && TREE_TYPE (expression) != NULL_TREE
24453 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24454 && !TYPE_DOMAIN (TREE_TYPE (expression))
24455 && DECL_INITIAL (expression))
24456 return true;
24457
24458 /* A function or variable template-id is type-dependent if it has any
24459 dependent template arguments. */
24460 if (VAR_OR_FUNCTION_DECL_P (expression)
24461 && DECL_LANG_SPECIFIC (expression)
24462 && DECL_TEMPLATE_INFO (expression))
24463 {
24464 /* Consider the innermost template arguments, since those are the ones
24465 that come from the template-id; the template arguments for the
24466 enclosing class do not make it type-dependent unless they are used in
24467 the type of the decl. */
24468 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24469 && (any_dependent_template_arguments_p
24470 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24471 return true;
24472 }
24473
24474 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24475 type-dependent. Checking this is important for functions with auto return
24476 type, which looks like a dependent type. */
24477 if (TREE_CODE (expression) == FUNCTION_DECL
24478 && !(DECL_CLASS_SCOPE_P (expression)
24479 && dependent_type_p (DECL_CONTEXT (expression)))
24480 && !(DECL_FRIEND_P (expression)
24481 && (!DECL_FRIEND_CONTEXT (expression)
24482 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24483 && !DECL_LOCAL_FUNCTION_P (expression))
24484 {
24485 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24486 || undeduced_auto_decl (expression));
24487 return false;
24488 }
24489
24490 /* Always dependent, on the number of arguments if nothing else. */
24491 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24492 return true;
24493
24494 if (TREE_TYPE (expression) == unknown_type_node)
24495 {
24496 if (TREE_CODE (expression) == ADDR_EXPR)
24497 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24498 if (TREE_CODE (expression) == COMPONENT_REF
24499 || TREE_CODE (expression) == OFFSET_REF)
24500 {
24501 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24502 return true;
24503 expression = TREE_OPERAND (expression, 1);
24504 if (identifier_p (expression))
24505 return false;
24506 }
24507 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24508 if (TREE_CODE (expression) == SCOPE_REF)
24509 return false;
24510
24511 if (BASELINK_P (expression))
24512 {
24513 if (BASELINK_OPTYPE (expression)
24514 && dependent_type_p (BASELINK_OPTYPE (expression)))
24515 return true;
24516 expression = BASELINK_FUNCTIONS (expression);
24517 }
24518
24519 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24520 {
24521 if (any_dependent_template_arguments_p
24522 (TREE_OPERAND (expression, 1)))
24523 return true;
24524 expression = TREE_OPERAND (expression, 0);
24525 if (identifier_p (expression))
24526 return true;
24527 }
24528
24529 gcc_assert (TREE_CODE (expression) == OVERLOAD
24530 || TREE_CODE (expression) == FUNCTION_DECL);
24531
24532 for (lkp_iterator iter (expression); iter; ++iter)
24533 if (type_dependent_expression_p (*iter))
24534 return true;
24535
24536 return false;
24537 }
24538
24539 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24540
24541 /* Dependent type attributes might not have made it from the decl to
24542 the type yet. */
24543 if (DECL_P (expression)
24544 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24545 return true;
24546
24547 return (dependent_type_p (TREE_TYPE (expression)));
24548 }
24549
24550 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24551 type-dependent if the expression refers to a member of the current
24552 instantiation and the type of the referenced member is dependent, or the
24553 class member access expression refers to a member of an unknown
24554 specialization.
24555
24556 This function returns true if the OBJECT in such a class member access
24557 expression is of an unknown specialization. */
24558
24559 bool
24560 type_dependent_object_expression_p (tree object)
24561 {
24562 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24563 dependent. */
24564 if (TREE_CODE (object) == IDENTIFIER_NODE)
24565 return true;
24566 tree scope = TREE_TYPE (object);
24567 return (!scope || dependent_scope_p (scope));
24568 }
24569
24570 /* walk_tree callback function for instantiation_dependent_expression_p,
24571 below. Returns non-zero if a dependent subexpression is found. */
24572
24573 static tree
24574 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24575 void * /*data*/)
24576 {
24577 if (TYPE_P (*tp))
24578 {
24579 /* We don't have to worry about decltype currently because decltype
24580 of an instantiation-dependent expr is a dependent type. This
24581 might change depending on the resolution of DR 1172. */
24582 *walk_subtrees = false;
24583 return NULL_TREE;
24584 }
24585 enum tree_code code = TREE_CODE (*tp);
24586 switch (code)
24587 {
24588 /* Don't treat an argument list as dependent just because it has no
24589 TREE_TYPE. */
24590 case TREE_LIST:
24591 case TREE_VEC:
24592 return NULL_TREE;
24593
24594 case TEMPLATE_PARM_INDEX:
24595 return *tp;
24596
24597 /* Handle expressions with type operands. */
24598 case SIZEOF_EXPR:
24599 case ALIGNOF_EXPR:
24600 case TYPEID_EXPR:
24601 case AT_ENCODE_EXPR:
24602 {
24603 tree op = TREE_OPERAND (*tp, 0);
24604 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24605 op = TREE_TYPE (op);
24606 if (TYPE_P (op))
24607 {
24608 if (dependent_type_p (op))
24609 return *tp;
24610 else
24611 {
24612 *walk_subtrees = false;
24613 return NULL_TREE;
24614 }
24615 }
24616 break;
24617 }
24618
24619 case COMPONENT_REF:
24620 if (identifier_p (TREE_OPERAND (*tp, 1)))
24621 /* In a template, finish_class_member_access_expr creates a
24622 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24623 type-dependent, so that we can check access control at
24624 instantiation time (PR 42277). See also Core issue 1273. */
24625 return *tp;
24626 break;
24627
24628 case SCOPE_REF:
24629 if (instantiation_dependent_scope_ref_p (*tp))
24630 return *tp;
24631 else
24632 break;
24633
24634 /* Treat statement-expressions as dependent. */
24635 case BIND_EXPR:
24636 return *tp;
24637
24638 /* Treat requires-expressions as dependent. */
24639 case REQUIRES_EXPR:
24640 return *tp;
24641
24642 case CALL_EXPR:
24643 /* Treat calls to function concepts as dependent. */
24644 if (function_concept_check_p (*tp))
24645 return *tp;
24646 break;
24647
24648 case TEMPLATE_ID_EXPR:
24649 /* And variable concepts. */
24650 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24651 return *tp;
24652 break;
24653
24654 default:
24655 break;
24656 }
24657
24658 if (type_dependent_expression_p (*tp))
24659 return *tp;
24660 else
24661 return NULL_TREE;
24662 }
24663
24664 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24665 sense defined by the ABI:
24666
24667 "An expression is instantiation-dependent if it is type-dependent
24668 or value-dependent, or it has a subexpression that is type-dependent
24669 or value-dependent."
24670
24671 Except don't actually check value-dependence for unevaluated expressions,
24672 because in sizeof(i) we don't care about the value of i. Checking
24673 type-dependence will in turn check value-dependence of array bounds/template
24674 arguments as needed. */
24675
24676 bool
24677 instantiation_dependent_uneval_expression_p (tree expression)
24678 {
24679 tree result;
24680
24681 if (!processing_template_decl)
24682 return false;
24683
24684 if (expression == error_mark_node)
24685 return false;
24686
24687 result = cp_walk_tree_without_duplicates (&expression,
24688 instantiation_dependent_r, NULL);
24689 return result != NULL_TREE;
24690 }
24691
24692 /* As above, but also check value-dependence of the expression as a whole. */
24693
24694 bool
24695 instantiation_dependent_expression_p (tree expression)
24696 {
24697 return (instantiation_dependent_uneval_expression_p (expression)
24698 || value_dependent_expression_p (expression));
24699 }
24700
24701 /* Like type_dependent_expression_p, but it also works while not processing
24702 a template definition, i.e. during substitution or mangling. */
24703
24704 bool
24705 type_dependent_expression_p_push (tree expr)
24706 {
24707 bool b;
24708 ++processing_template_decl;
24709 b = type_dependent_expression_p (expr);
24710 --processing_template_decl;
24711 return b;
24712 }
24713
24714 /* Returns TRUE if ARGS contains a type-dependent expression. */
24715
24716 bool
24717 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24718 {
24719 unsigned int i;
24720 tree arg;
24721
24722 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24723 {
24724 if (type_dependent_expression_p (arg))
24725 return true;
24726 }
24727 return false;
24728 }
24729
24730 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24731 expressions) contains any type-dependent expressions. */
24732
24733 bool
24734 any_type_dependent_elements_p (const_tree list)
24735 {
24736 for (; list; list = TREE_CHAIN (list))
24737 if (type_dependent_expression_p (TREE_VALUE (list)))
24738 return true;
24739
24740 return false;
24741 }
24742
24743 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24744 expressions) contains any value-dependent expressions. */
24745
24746 bool
24747 any_value_dependent_elements_p (const_tree list)
24748 {
24749 for (; list; list = TREE_CHAIN (list))
24750 if (value_dependent_expression_p (TREE_VALUE (list)))
24751 return true;
24752
24753 return false;
24754 }
24755
24756 /* Returns TRUE if the ARG (a template argument) is dependent. */
24757
24758 bool
24759 dependent_template_arg_p (tree arg)
24760 {
24761 if (!processing_template_decl)
24762 return false;
24763
24764 /* Assume a template argument that was wrongly written by the user
24765 is dependent. This is consistent with what
24766 any_dependent_template_arguments_p [that calls this function]
24767 does. */
24768 if (!arg || arg == error_mark_node)
24769 return true;
24770
24771 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24772 arg = argument_pack_select_arg (arg);
24773
24774 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24775 return true;
24776 if (TREE_CODE (arg) == TEMPLATE_DECL)
24777 {
24778 if (DECL_TEMPLATE_PARM_P (arg))
24779 return true;
24780 /* A member template of a dependent class is not necessarily
24781 type-dependent, but it is a dependent template argument because it
24782 will be a member of an unknown specialization to that template. */
24783 tree scope = CP_DECL_CONTEXT (arg);
24784 return TYPE_P (scope) && dependent_type_p (scope);
24785 }
24786 else if (ARGUMENT_PACK_P (arg))
24787 {
24788 tree args = ARGUMENT_PACK_ARGS (arg);
24789 int i, len = TREE_VEC_LENGTH (args);
24790 for (i = 0; i < len; ++i)
24791 {
24792 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24793 return true;
24794 }
24795
24796 return false;
24797 }
24798 else if (TYPE_P (arg))
24799 return dependent_type_p (arg);
24800 else
24801 return (type_dependent_expression_p (arg)
24802 || value_dependent_expression_p (arg));
24803 }
24804
24805 /* Returns true if ARGS (a collection of template arguments) contains
24806 any types that require structural equality testing. */
24807
24808 bool
24809 any_template_arguments_need_structural_equality_p (tree args)
24810 {
24811 int i;
24812 int j;
24813
24814 if (!args)
24815 return false;
24816 if (args == error_mark_node)
24817 return true;
24818
24819 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24820 {
24821 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24822 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24823 {
24824 tree arg = TREE_VEC_ELT (level, j);
24825 tree packed_args = NULL_TREE;
24826 int k, len = 1;
24827
24828 if (ARGUMENT_PACK_P (arg))
24829 {
24830 /* Look inside the argument pack. */
24831 packed_args = ARGUMENT_PACK_ARGS (arg);
24832 len = TREE_VEC_LENGTH (packed_args);
24833 }
24834
24835 for (k = 0; k < len; ++k)
24836 {
24837 if (packed_args)
24838 arg = TREE_VEC_ELT (packed_args, k);
24839
24840 if (error_operand_p (arg))
24841 return true;
24842 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24843 continue;
24844 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24845 return true;
24846 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24847 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24848 return true;
24849 }
24850 }
24851 }
24852
24853 return false;
24854 }
24855
24856 /* Returns true if ARGS (a collection of template arguments) contains
24857 any dependent arguments. */
24858
24859 bool
24860 any_dependent_template_arguments_p (const_tree args)
24861 {
24862 int i;
24863 int j;
24864
24865 if (!args)
24866 return false;
24867 if (args == error_mark_node)
24868 return true;
24869
24870 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24871 {
24872 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24873 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24874 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24875 return true;
24876 }
24877
24878 return false;
24879 }
24880
24881 /* Returns TRUE if the template TMPL is type-dependent. */
24882
24883 bool
24884 dependent_template_p (tree tmpl)
24885 {
24886 if (TREE_CODE (tmpl) == OVERLOAD)
24887 {
24888 for (lkp_iterator iter (tmpl); iter; ++iter)
24889 if (dependent_template_p (*iter))
24890 return true;
24891 return false;
24892 }
24893
24894 /* Template template parameters are dependent. */
24895 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24896 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24897 return true;
24898 /* So are names that have not been looked up. */
24899 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24900 return true;
24901 return false;
24902 }
24903
24904 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24905
24906 bool
24907 dependent_template_id_p (tree tmpl, tree args)
24908 {
24909 return (dependent_template_p (tmpl)
24910 || any_dependent_template_arguments_p (args));
24911 }
24912
24913 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24914 are dependent. */
24915
24916 bool
24917 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24918 {
24919 int i;
24920
24921 if (!processing_template_decl)
24922 return false;
24923
24924 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24925 {
24926 tree decl = TREE_VEC_ELT (declv, i);
24927 tree init = TREE_VEC_ELT (initv, i);
24928 tree cond = TREE_VEC_ELT (condv, i);
24929 tree incr = TREE_VEC_ELT (incrv, i);
24930
24931 if (type_dependent_expression_p (decl)
24932 || TREE_CODE (decl) == SCOPE_REF)
24933 return true;
24934
24935 if (init && type_dependent_expression_p (init))
24936 return true;
24937
24938 if (type_dependent_expression_p (cond))
24939 return true;
24940
24941 if (COMPARISON_CLASS_P (cond)
24942 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24943 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24944 return true;
24945
24946 if (TREE_CODE (incr) == MODOP_EXPR)
24947 {
24948 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24949 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24950 return true;
24951 }
24952 else if (type_dependent_expression_p (incr))
24953 return true;
24954 else if (TREE_CODE (incr) == MODIFY_EXPR)
24955 {
24956 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24957 return true;
24958 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24959 {
24960 tree t = TREE_OPERAND (incr, 1);
24961 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24962 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24963 return true;
24964 }
24965 }
24966 }
24967
24968 return false;
24969 }
24970
24971 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24972 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24973 no such TYPE can be found. Note that this function peers inside
24974 uninstantiated templates and therefore should be used only in
24975 extremely limited situations. ONLY_CURRENT_P restricts this
24976 peering to the currently open classes hierarchy (which is required
24977 when comparing types). */
24978
24979 tree
24980 resolve_typename_type (tree type, bool only_current_p)
24981 {
24982 tree scope;
24983 tree name;
24984 tree decl;
24985 int quals;
24986 tree pushed_scope;
24987 tree result;
24988
24989 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24990
24991 scope = TYPE_CONTEXT (type);
24992 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24993 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24994 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24995 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24996 identifier of the TYPENAME_TYPE anymore.
24997 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24998 TYPENAME_TYPE instead, we avoid messing up with a possible
24999 typedef variant case. */
25000 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25001
25002 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25003 it first before we can figure out what NAME refers to. */
25004 if (TREE_CODE (scope) == TYPENAME_TYPE)
25005 {
25006 if (TYPENAME_IS_RESOLVING_P (scope))
25007 /* Given a class template A with a dependent base with nested type C,
25008 typedef typename A::C::C C will land us here, as trying to resolve
25009 the initial A::C leads to the local C typedef, which leads back to
25010 A::C::C. So we break the recursion now. */
25011 return type;
25012 else
25013 scope = resolve_typename_type (scope, only_current_p);
25014 }
25015 /* If we don't know what SCOPE refers to, then we cannot resolve the
25016 TYPENAME_TYPE. */
25017 if (!CLASS_TYPE_P (scope))
25018 return type;
25019 /* If this is a typedef, we don't want to look inside (c++/11987). */
25020 if (typedef_variant_p (type))
25021 return type;
25022 /* If SCOPE isn't the template itself, it will not have a valid
25023 TYPE_FIELDS list. */
25024 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25025 /* scope is either the template itself or a compatible instantiation
25026 like X<T>, so look up the name in the original template. */
25027 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25028 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25029 gcc_checking_assert (uses_template_parms (scope));
25030 /* If scope has no fields, it can't be a current instantiation. Check this
25031 before currently_open_class to avoid infinite recursion (71515). */
25032 if (!TYPE_FIELDS (scope))
25033 return type;
25034 /* If the SCOPE is not the current instantiation, there's no reason
25035 to look inside it. */
25036 if (only_current_p && !currently_open_class (scope))
25037 return type;
25038 /* Enter the SCOPE so that name lookup will be resolved as if we
25039 were in the class definition. In particular, SCOPE will no
25040 longer be considered a dependent type. */
25041 pushed_scope = push_scope (scope);
25042 /* Look up the declaration. */
25043 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25044 tf_warning_or_error);
25045
25046 result = NULL_TREE;
25047
25048 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25049 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25050 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25051 if (!decl)
25052 /*nop*/;
25053 else if (identifier_p (fullname)
25054 && TREE_CODE (decl) == TYPE_DECL)
25055 {
25056 result = TREE_TYPE (decl);
25057 if (result == error_mark_node)
25058 result = NULL_TREE;
25059 }
25060 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25061 && DECL_CLASS_TEMPLATE_P (decl))
25062 {
25063 /* Obtain the template and the arguments. */
25064 tree tmpl = TREE_OPERAND (fullname, 0);
25065 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25066 {
25067 /* We get here with a plain identifier because a previous tentative
25068 parse of the nested-name-specifier as part of a ptr-operator saw
25069 ::template X<A>. The use of ::template is necessary in a
25070 ptr-operator, but wrong in a declarator-id.
25071
25072 [temp.names]: In a qualified-id of a declarator-id, the keyword
25073 template shall not appear at the top level. */
25074 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25075 "keyword %<template%> not allowed in declarator-id");
25076 tmpl = decl;
25077 }
25078 tree args = TREE_OPERAND (fullname, 1);
25079 /* Instantiate the template. */
25080 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25081 /*entering_scope=*/true,
25082 tf_error | tf_user);
25083 if (result == error_mark_node)
25084 result = NULL_TREE;
25085 }
25086
25087 /* Leave the SCOPE. */
25088 if (pushed_scope)
25089 pop_scope (pushed_scope);
25090
25091 /* If we failed to resolve it, return the original typename. */
25092 if (!result)
25093 return type;
25094
25095 /* If lookup found a typename type, resolve that too. */
25096 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25097 {
25098 /* Ill-formed programs can cause infinite recursion here, so we
25099 must catch that. */
25100 TYPENAME_IS_RESOLVING_P (result) = 1;
25101 result = resolve_typename_type (result, only_current_p);
25102 TYPENAME_IS_RESOLVING_P (result) = 0;
25103 }
25104
25105 /* Qualify the resulting type. */
25106 quals = cp_type_quals (type);
25107 if (quals)
25108 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25109
25110 return result;
25111 }
25112
25113 /* EXPR is an expression which is not type-dependent. Return a proxy
25114 for EXPR that can be used to compute the types of larger
25115 expressions containing EXPR. */
25116
25117 tree
25118 build_non_dependent_expr (tree expr)
25119 {
25120 tree orig_expr = expr;
25121 tree inner_expr;
25122
25123 /* When checking, try to get a constant value for all non-dependent
25124 expressions in order to expose bugs in *_dependent_expression_p
25125 and constexpr. This can affect code generation, see PR70704, so
25126 only do this for -fchecking=2. */
25127 if (flag_checking > 1
25128 && cxx_dialect >= cxx11
25129 /* Don't do this during nsdmi parsing as it can lead to
25130 unexpected recursive instantiations. */
25131 && !parsing_nsdmi ()
25132 /* Don't do this during concept expansion either and for
25133 the same reason. */
25134 && !expanding_concept ())
25135 fold_non_dependent_expr (expr);
25136
25137 STRIP_ANY_LOCATION_WRAPPER (expr);
25138
25139 /* Preserve OVERLOADs; the functions must be available to resolve
25140 types. */
25141 inner_expr = expr;
25142 if (TREE_CODE (inner_expr) == STMT_EXPR)
25143 inner_expr = stmt_expr_value_expr (inner_expr);
25144 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25145 inner_expr = TREE_OPERAND (inner_expr, 0);
25146 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25147 inner_expr = TREE_OPERAND (inner_expr, 1);
25148 if (is_overloaded_fn (inner_expr)
25149 || TREE_CODE (inner_expr) == OFFSET_REF)
25150 return orig_expr;
25151 /* There is no need to return a proxy for a variable. */
25152 if (VAR_P (expr))
25153 return orig_expr;
25154 /* Preserve string constants; conversions from string constants to
25155 "char *" are allowed, even though normally a "const char *"
25156 cannot be used to initialize a "char *". */
25157 if (TREE_CODE (expr) == STRING_CST)
25158 return orig_expr;
25159 /* Preserve void and arithmetic constants, as an optimization -- there is no
25160 reason to create a new node. */
25161 if (TREE_CODE (expr) == VOID_CST
25162 || TREE_CODE (expr) == INTEGER_CST
25163 || TREE_CODE (expr) == REAL_CST)
25164 return orig_expr;
25165 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25166 There is at least one place where we want to know that a
25167 particular expression is a throw-expression: when checking a ?:
25168 expression, there are special rules if the second or third
25169 argument is a throw-expression. */
25170 if (TREE_CODE (expr) == THROW_EXPR)
25171 return orig_expr;
25172
25173 /* Don't wrap an initializer list, we need to be able to look inside. */
25174 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25175 return orig_expr;
25176
25177 /* Don't wrap a dummy object, we need to be able to test for it. */
25178 if (is_dummy_object (expr))
25179 return orig_expr;
25180
25181 if (TREE_CODE (expr) == COND_EXPR)
25182 return build3 (COND_EXPR,
25183 TREE_TYPE (expr),
25184 TREE_OPERAND (expr, 0),
25185 (TREE_OPERAND (expr, 1)
25186 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25187 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25188 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25189 if (TREE_CODE (expr) == COMPOUND_EXPR
25190 && !COMPOUND_EXPR_OVERLOADED (expr))
25191 return build2 (COMPOUND_EXPR,
25192 TREE_TYPE (expr),
25193 TREE_OPERAND (expr, 0),
25194 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25195
25196 /* If the type is unknown, it can't really be non-dependent */
25197 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25198
25199 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25200 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25201 }
25202
25203 /* ARGS is a vector of expressions as arguments to a function call.
25204 Replace the arguments with equivalent non-dependent expressions.
25205 This modifies ARGS in place. */
25206
25207 void
25208 make_args_non_dependent (vec<tree, va_gc> *args)
25209 {
25210 unsigned int ix;
25211 tree arg;
25212
25213 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25214 {
25215 tree newarg = build_non_dependent_expr (arg);
25216 if (newarg != arg)
25217 (*args)[ix] = newarg;
25218 }
25219 }
25220
25221 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25222 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25223 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25224
25225 static tree
25226 make_auto_1 (tree name, bool set_canonical)
25227 {
25228 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25229 TYPE_NAME (au) = build_decl (input_location,
25230 TYPE_DECL, name, au);
25231 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25232 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25233 (0, processing_template_decl + 1, processing_template_decl + 1,
25234 TYPE_NAME (au), NULL_TREE);
25235 if (set_canonical)
25236 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25237 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25238 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25239
25240 return au;
25241 }
25242
25243 tree
25244 make_decltype_auto (void)
25245 {
25246 return make_auto_1 (decltype_auto_identifier, true);
25247 }
25248
25249 tree
25250 make_auto (void)
25251 {
25252 return make_auto_1 (auto_identifier, true);
25253 }
25254
25255 /* Return a C++17 deduction placeholder for class template TMPL. */
25256
25257 tree
25258 make_template_placeholder (tree tmpl)
25259 {
25260 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25261 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25262 return t;
25263 }
25264
25265 /* True iff T is a C++17 class template deduction placeholder. */
25266
25267 bool
25268 template_placeholder_p (tree t)
25269 {
25270 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25271 }
25272
25273 /* Make a "constrained auto" type-specifier. This is an
25274 auto type with constraints that must be associated after
25275 deduction. The constraint is formed from the given
25276 CONC and its optional sequence of arguments, which are
25277 non-null if written as partial-concept-id. */
25278
25279 tree
25280 make_constrained_auto (tree con, tree args)
25281 {
25282 tree type = make_auto_1 (auto_identifier, false);
25283
25284 /* Build the constraint. */
25285 tree tmpl = DECL_TI_TEMPLATE (con);
25286 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25287 expr = build_concept_check (expr, type, args);
25288
25289 tree constr = normalize_expression (expr);
25290 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25291
25292 /* Our canonical type depends on the constraint. */
25293 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25294
25295 /* Attach the constraint to the type declaration. */
25296 tree decl = TYPE_NAME (type);
25297 return decl;
25298 }
25299
25300 /* Given type ARG, return std::initializer_list<ARG>. */
25301
25302 static tree
25303 listify (tree arg)
25304 {
25305 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25306
25307 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25308 {
25309 gcc_rich_location richloc (input_location);
25310 maybe_add_include_fixit (&richloc, "<initializer_list>");
25311 error_at (&richloc,
25312 "deducing from brace-enclosed initializer list"
25313 " requires %<#include <initializer_list>%>");
25314
25315 return error_mark_node;
25316 }
25317 tree argvec = make_tree_vec (1);
25318 TREE_VEC_ELT (argvec, 0) = arg;
25319
25320 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25321 NULL_TREE, 0, tf_warning_or_error);
25322 }
25323
25324 /* Replace auto in TYPE with std::initializer_list<auto>. */
25325
25326 static tree
25327 listify_autos (tree type, tree auto_node)
25328 {
25329 tree init_auto = listify (auto_node);
25330 tree argvec = make_tree_vec (1);
25331 TREE_VEC_ELT (argvec, 0) = init_auto;
25332 if (processing_template_decl)
25333 argvec = add_to_template_args (current_template_args (), argvec);
25334 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25335 }
25336
25337 /* Hash traits for hashing possibly constrained 'auto'
25338 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25339
25340 struct auto_hash : default_hash_traits<tree>
25341 {
25342 static inline hashval_t hash (tree);
25343 static inline bool equal (tree, tree);
25344 };
25345
25346 /* Hash the 'auto' T. */
25347
25348 inline hashval_t
25349 auto_hash::hash (tree t)
25350 {
25351 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25352 /* Matching constrained-type-specifiers denote the same template
25353 parameter, so hash the constraint. */
25354 return hash_placeholder_constraint (c);
25355 else
25356 /* But unconstrained autos are all separate, so just hash the pointer. */
25357 return iterative_hash_object (t, 0);
25358 }
25359
25360 /* Compare two 'auto's. */
25361
25362 inline bool
25363 auto_hash::equal (tree t1, tree t2)
25364 {
25365 if (t1 == t2)
25366 return true;
25367
25368 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25369 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25370
25371 /* Two unconstrained autos are distinct. */
25372 if (!c1 || !c2)
25373 return false;
25374
25375 return equivalent_placeholder_constraints (c1, c2);
25376 }
25377
25378 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25379 constrained) auto, add it to the vector. */
25380
25381 static int
25382 extract_autos_r (tree t, void *data)
25383 {
25384 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25385 if (is_auto (t))
25386 {
25387 /* All the autos were built with index 0; fix that up now. */
25388 tree *p = hash.find_slot (t, INSERT);
25389 unsigned idx;
25390 if (*p)
25391 /* If this is a repeated constrained-type-specifier, use the index we
25392 chose before. */
25393 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25394 else
25395 {
25396 /* Otherwise this is new, so use the current count. */
25397 *p = t;
25398 idx = hash.elements () - 1;
25399 }
25400 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25401 }
25402
25403 /* Always keep walking. */
25404 return 0;
25405 }
25406
25407 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25408 says they can appear anywhere in the type. */
25409
25410 static tree
25411 extract_autos (tree type)
25412 {
25413 hash_set<tree> visited;
25414 hash_table<auto_hash> hash (2);
25415
25416 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25417
25418 tree tree_vec = make_tree_vec (hash.elements());
25419 for (hash_table<auto_hash>::iterator iter = hash.begin();
25420 iter != hash.end(); ++iter)
25421 {
25422 tree elt = *iter;
25423 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25424 TREE_VEC_ELT (tree_vec, i)
25425 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25426 }
25427
25428 return tree_vec;
25429 }
25430
25431 /* The stem for deduction guide names. */
25432 const char *const dguide_base = "__dguide_";
25433
25434 /* Return the name for a deduction guide for class template TMPL. */
25435
25436 tree
25437 dguide_name (tree tmpl)
25438 {
25439 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25440 tree tname = TYPE_IDENTIFIER (type);
25441 char *buf = (char *) alloca (1 + strlen (dguide_base)
25442 + IDENTIFIER_LENGTH (tname));
25443 memcpy (buf, dguide_base, strlen (dguide_base));
25444 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25445 IDENTIFIER_LENGTH (tname) + 1);
25446 tree dname = get_identifier (buf);
25447 TREE_TYPE (dname) = type;
25448 return dname;
25449 }
25450
25451 /* True if NAME is the name of a deduction guide. */
25452
25453 bool
25454 dguide_name_p (tree name)
25455 {
25456 return (TREE_TYPE (name)
25457 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25458 strlen (dguide_base)));
25459 }
25460
25461 /* True if FN is a deduction guide. */
25462
25463 bool
25464 deduction_guide_p (const_tree fn)
25465 {
25466 if (DECL_P (fn))
25467 if (tree name = DECL_NAME (fn))
25468 return dguide_name_p (name);
25469 return false;
25470 }
25471
25472 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25473
25474 bool
25475 copy_guide_p (const_tree fn)
25476 {
25477 gcc_assert (deduction_guide_p (fn));
25478 if (!DECL_ARTIFICIAL (fn))
25479 return false;
25480 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25481 return (TREE_CHAIN (parms) == void_list_node
25482 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25483 }
25484
25485 /* True if FN is a guide generated from a constructor template. */
25486
25487 bool
25488 template_guide_p (const_tree fn)
25489 {
25490 gcc_assert (deduction_guide_p (fn));
25491 if (!DECL_ARTIFICIAL (fn))
25492 return false;
25493 tree tmpl = DECL_TI_TEMPLATE (fn);
25494 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25495 return PRIMARY_TEMPLATE_P (org);
25496 return false;
25497 }
25498
25499 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25500 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25501 template parameter types. Note that the handling of template template
25502 parameters relies on current_template_parms being set appropriately for the
25503 new template. */
25504
25505 static tree
25506 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25507 tree tsubst_args, tsubst_flags_t complain)
25508 {
25509 tree oldidx = get_template_parm_index (olddecl);
25510
25511 tree newtype;
25512 if (TREE_CODE (olddecl) == TYPE_DECL
25513 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25514 {
25515 tree oldtype = TREE_TYPE (olddecl);
25516 newtype = cxx_make_type (TREE_CODE (oldtype));
25517 TYPE_MAIN_VARIANT (newtype) = newtype;
25518 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25519 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25520 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25521 }
25522 else
25523 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25524 complain, NULL_TREE);
25525
25526 tree newdecl
25527 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25528 DECL_NAME (olddecl), newtype);
25529 SET_DECL_TEMPLATE_PARM_P (newdecl);
25530
25531 tree newidx;
25532 if (TREE_CODE (olddecl) == TYPE_DECL
25533 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25534 {
25535 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25536 = build_template_parm_index (index, level, level,
25537 newdecl, newtype);
25538 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25539 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25540 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25541 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25542
25543 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25544 {
25545 DECL_TEMPLATE_RESULT (newdecl)
25546 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25547 DECL_NAME (olddecl), newtype);
25548 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25549 // First create a copy (ttargs) of tsubst_args with an
25550 // additional level for the template template parameter's own
25551 // template parameters (ttparms).
25552 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25553 (DECL_TEMPLATE_PARMS (olddecl)));
25554 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25555 tree ttargs = make_tree_vec (depth + 1);
25556 for (int i = 0; i < depth; ++i)
25557 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25558 TREE_VEC_ELT (ttargs, depth)
25559 = template_parms_level_to_args (ttparms);
25560 // Substitute ttargs into ttparms to fix references to
25561 // other template parameters.
25562 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25563 complain);
25564 // Now substitute again with args based on tparms, to reduce
25565 // the level of the ttparms.
25566 ttargs = current_template_args ();
25567 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25568 complain);
25569 // Finally, tack the adjusted parms onto tparms.
25570 ttparms = tree_cons (size_int (depth), ttparms,
25571 current_template_parms);
25572 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25573 }
25574 }
25575 else
25576 {
25577 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25578 tree newconst
25579 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25580 TREE_CODE (oldconst),
25581 DECL_NAME (oldconst), newtype);
25582 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25583 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25584 SET_DECL_TEMPLATE_PARM_P (newconst);
25585 newidx = build_template_parm_index (index, level, level,
25586 newconst, newtype);
25587 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25588 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25589 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25590 }
25591
25592 return newdecl;
25593 }
25594
25595 /* Returns a C++17 class deduction guide template based on the constructor
25596 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25597 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25598
25599 static tree
25600 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25601 {
25602 tree type, tparms, targs, fparms, fargs, ci;
25603 bool memtmpl = false;
25604 bool explicit_p;
25605 location_t loc;
25606 tree fn_tmpl = NULL_TREE;
25607
25608 if (TYPE_P (ctor))
25609 {
25610 type = ctor;
25611 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25612 if (copy_p)
25613 {
25614 type = TREE_TYPE (type);
25615 fparms = tree_cons (NULL_TREE, type, void_list_node);
25616 }
25617 else
25618 fparms = void_list_node;
25619
25620 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25621 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25622 targs = CLASSTYPE_TI_ARGS (type);
25623 ci = NULL_TREE;
25624 fargs = NULL_TREE;
25625 loc = DECL_SOURCE_LOCATION (ctmpl);
25626 explicit_p = false;
25627 }
25628 else
25629 {
25630 ++processing_template_decl;
25631
25632 fn_tmpl
25633 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25634 : DECL_TI_TEMPLATE (ctor));
25635 if (outer_args)
25636 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25637 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25638
25639 type = DECL_CONTEXT (ctor);
25640
25641 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25642 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25643 fully specialized args for the enclosing class. Strip those off, as
25644 the deduction guide won't have those template parameters. */
25645 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25646 TMPL_PARMS_DEPTH (tparms));
25647 /* Discard the 'this' parameter. */
25648 fparms = FUNCTION_ARG_CHAIN (ctor);
25649 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25650 ci = get_constraints (ctor);
25651 loc = DECL_SOURCE_LOCATION (ctor);
25652 explicit_p = DECL_NONCONVERTING_P (ctor);
25653
25654 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25655 {
25656 memtmpl = true;
25657
25658 /* For a member template constructor, we need to flatten the two
25659 template parameter lists into one, and then adjust the function
25660 signature accordingly. This gets...complicated. */
25661 tree save_parms = current_template_parms;
25662
25663 /* For a member template we should have two levels of parms/args, one
25664 for the class and one for the constructor. We stripped
25665 specialized args for further enclosing classes above. */
25666 const int depth = 2;
25667 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25668
25669 /* Template args for translating references to the two-level template
25670 parameters into references to the one-level template parameters we
25671 are creating. */
25672 tree tsubst_args = copy_node (targs);
25673 TMPL_ARGS_LEVEL (tsubst_args, depth)
25674 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25675
25676 /* Template parms for the constructor template. */
25677 tree ftparms = TREE_VALUE (tparms);
25678 unsigned flen = TREE_VEC_LENGTH (ftparms);
25679 /* Template parms for the class template. */
25680 tparms = TREE_CHAIN (tparms);
25681 tree ctparms = TREE_VALUE (tparms);
25682 unsigned clen = TREE_VEC_LENGTH (ctparms);
25683 /* Template parms for the deduction guide start as a copy of the
25684 template parms for the class. We set current_template_parms for
25685 lookup_template_class_1. */
25686 current_template_parms = tparms = copy_node (tparms);
25687 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25688 for (unsigned i = 0; i < clen; ++i)
25689 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25690
25691 /* Now we need to rewrite the constructor parms to append them to the
25692 class parms. */
25693 for (unsigned i = 0; i < flen; ++i)
25694 {
25695 unsigned index = i + clen;
25696 unsigned level = 1;
25697 tree oldelt = TREE_VEC_ELT (ftparms, i);
25698 tree olddecl = TREE_VALUE (oldelt);
25699 tree newdecl = rewrite_template_parm (olddecl, index, level,
25700 tsubst_args, complain);
25701 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25702 tsubst_args, complain, ctor);
25703 tree list = build_tree_list (newdef, newdecl);
25704 TEMPLATE_PARM_CONSTRAINTS (list)
25705 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25706 tsubst_args, complain, ctor);
25707 TREE_VEC_ELT (new_vec, index) = list;
25708 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25709 }
25710
25711 /* Now we have a final set of template parms to substitute into the
25712 function signature. */
25713 targs = template_parms_to_args (tparms);
25714 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25715 complain, ctor);
25716 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25717 if (ci)
25718 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25719
25720 current_template_parms = save_parms;
25721 }
25722 --processing_template_decl;
25723 }
25724
25725 if (!memtmpl)
25726 {
25727 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25728 tparms = copy_node (tparms);
25729 INNERMOST_TEMPLATE_PARMS (tparms)
25730 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25731 }
25732
25733 tree fntype = build_function_type (type, fparms);
25734 tree ded_fn = build_lang_decl_loc (loc,
25735 FUNCTION_DECL,
25736 dguide_name (type), fntype);
25737 DECL_ARGUMENTS (ded_fn) = fargs;
25738 DECL_ARTIFICIAL (ded_fn) = true;
25739 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25740 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25741 DECL_ARTIFICIAL (ded_tmpl) = true;
25742 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25743 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25744 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25745 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25746 if (DECL_P (ctor))
25747 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25748 if (ci)
25749 set_constraints (ded_tmpl, ci);
25750
25751 return ded_tmpl;
25752 }
25753
25754 /* Deduce template arguments for the class template placeholder PTYPE for
25755 template TMPL based on the initializer INIT, and return the resulting
25756 type. */
25757
25758 static tree
25759 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25760 tsubst_flags_t complain)
25761 {
25762 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25763 {
25764 /* We should have handled this in the caller. */
25765 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25766 return ptype;
25767 if (complain & tf_error)
25768 error ("non-class template %qT used without template arguments", tmpl);
25769 return error_mark_node;
25770 }
25771
25772 tree type = TREE_TYPE (tmpl);
25773
25774 bool try_list_ctor = false;
25775
25776 vec<tree,va_gc> *args;
25777 if (init == NULL_TREE
25778 || TREE_CODE (init) == TREE_LIST)
25779 args = make_tree_vector_from_list (init);
25780 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25781 {
25782 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25783 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25784 {
25785 /* As an exception, the first phase in 16.3.1.7 (considering the
25786 initializer list as a single argument) is omitted if the
25787 initializer list consists of a single expression of type cv U,
25788 where U is a specialization of C or a class derived from a
25789 specialization of C. */
25790 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25791 tree etype = TREE_TYPE (elt);
25792
25793 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25794 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25795 int err = unify (tparms, targs, type, etype,
25796 UNIFY_ALLOW_DERIVED, /*explain*/false);
25797 if (err == 0)
25798 try_list_ctor = false;
25799 ggc_free (targs);
25800 }
25801 if (try_list_ctor || is_std_init_list (type))
25802 args = make_tree_vector_single (init);
25803 else
25804 args = make_tree_vector_from_ctor (init);
25805 }
25806 else
25807 args = make_tree_vector_single (init);
25808
25809 tree dname = dguide_name (tmpl);
25810 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25811 /*type*/false, /*complain*/false,
25812 /*hidden*/false);
25813 bool elided = false;
25814 if (cands == error_mark_node)
25815 cands = NULL_TREE;
25816
25817 /* Prune explicit deduction guides in copy-initialization context. */
25818 if (flags & LOOKUP_ONLYCONVERTING)
25819 {
25820 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25821 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25822 elided = true;
25823
25824 if (elided)
25825 {
25826 /* Found a nonconverting guide, prune the candidates. */
25827 tree pruned = NULL_TREE;
25828 for (lkp_iterator iter (cands); iter; ++iter)
25829 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25830 pruned = lookup_add (*iter, pruned);
25831
25832 cands = pruned;
25833 }
25834 }
25835
25836 tree outer_args = NULL_TREE;
25837 if (DECL_CLASS_SCOPE_P (tmpl)
25838 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25839 {
25840 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25841 type = TREE_TYPE (most_general_template (tmpl));
25842 }
25843
25844 bool saw_ctor = false;
25845 // FIXME cache artificial deduction guides
25846 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25847 {
25848 tree guide = build_deduction_guide (*iter, outer_args, complain);
25849 if ((flags & LOOKUP_ONLYCONVERTING)
25850 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25851 elided = true;
25852 else
25853 cands = lookup_add (guide, cands);
25854
25855 saw_ctor = true;
25856 }
25857
25858 tree call = error_mark_node;
25859
25860 /* If this is list-initialization and the class has a list constructor, first
25861 try deducing from the list as a single argument, as [over.match.list]. */
25862 tree list_cands = NULL_TREE;
25863 if (try_list_ctor && cands)
25864 for (lkp_iterator iter (cands); iter; ++iter)
25865 {
25866 tree dg = *iter;
25867 if (is_list_ctor (dg))
25868 list_cands = lookup_add (dg, list_cands);
25869 }
25870 if (list_cands)
25871 {
25872 ++cp_unevaluated_operand;
25873 call = build_new_function_call (list_cands, &args, tf_decltype);
25874 --cp_unevaluated_operand;
25875
25876 if (call == error_mark_node)
25877 {
25878 /* That didn't work, now try treating the list as a sequence of
25879 arguments. */
25880 release_tree_vector (args);
25881 args = make_tree_vector_from_ctor (init);
25882 }
25883 }
25884
25885 /* Maybe generate an implicit deduction guide. */
25886 if (call == error_mark_node && args->length () < 2)
25887 {
25888 tree gtype = NULL_TREE;
25889
25890 if (args->length () == 1)
25891 /* Generate a copy guide. */
25892 gtype = build_reference_type (type);
25893 else if (!saw_ctor)
25894 /* Generate a default guide. */
25895 gtype = type;
25896
25897 if (gtype)
25898 {
25899 tree guide = build_deduction_guide (gtype, outer_args, complain);
25900 cands = lookup_add (guide, cands);
25901 }
25902 }
25903
25904 if (elided && !cands)
25905 {
25906 error ("cannot deduce template arguments for copy-initialization"
25907 " of %qT, as it has no non-explicit deduction guides or "
25908 "user-declared constructors", type);
25909 return error_mark_node;
25910 }
25911 else if (!cands && call == error_mark_node)
25912 {
25913 error ("cannot deduce template arguments of %qT, as it has no viable "
25914 "deduction guides", type);
25915 return error_mark_node;
25916 }
25917
25918 if (call == error_mark_node)
25919 {
25920 ++cp_unevaluated_operand;
25921 call = build_new_function_call (cands, &args, tf_decltype);
25922 --cp_unevaluated_operand;
25923 }
25924
25925 if (call == error_mark_node && (complain & tf_warning_or_error))
25926 {
25927 error ("class template argument deduction failed:");
25928
25929 ++cp_unevaluated_operand;
25930 call = build_new_function_call (cands, &args, complain | tf_decltype);
25931 --cp_unevaluated_operand;
25932
25933 if (elided)
25934 inform (input_location, "explicit deduction guides not considered "
25935 "for copy-initialization");
25936 }
25937
25938 release_tree_vector (args);
25939
25940 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25941 }
25942
25943 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25944 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25945 The CONTEXT determines the context in which auto deduction is performed
25946 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25947 OUTER_TARGS are used during template argument deduction
25948 (context == adc_unify) to properly substitute the result, and is ignored
25949 in other contexts.
25950
25951 For partial-concept-ids, extra args may be appended to the list of deduced
25952 template arguments prior to determining constraint satisfaction. */
25953
25954 tree
25955 do_auto_deduction (tree type, tree init, tree auto_node,
25956 tsubst_flags_t complain, auto_deduction_context context,
25957 tree outer_targs, int flags)
25958 {
25959 tree targs;
25960
25961 if (init == error_mark_node)
25962 return error_mark_node;
25963
25964 if (init && type_dependent_expression_p (init)
25965 && context != adc_unify)
25966 /* Defining a subset of type-dependent expressions that we can deduce
25967 from ahead of time isn't worth the trouble. */
25968 return type;
25969
25970 /* Similarly, we can't deduce from another undeduced decl. */
25971 if (init && undeduced_auto_decl (init))
25972 return type;
25973
25974 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25975 /* C++17 class template argument deduction. */
25976 return do_class_deduction (type, tmpl, init, flags, complain);
25977
25978 if (TREE_TYPE (init) == NULL_TREE)
25979 /* Nothing we can do with this, even in deduction context. */
25980 return type;
25981
25982 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25983 with either a new invented type template parameter U or, if the
25984 initializer is a braced-init-list (8.5.4), with
25985 std::initializer_list<U>. */
25986 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25987 {
25988 if (!DIRECT_LIST_INIT_P (init))
25989 type = listify_autos (type, auto_node);
25990 else if (CONSTRUCTOR_NELTS (init) == 1)
25991 init = CONSTRUCTOR_ELT (init, 0)->value;
25992 else
25993 {
25994 if (complain & tf_warning_or_error)
25995 {
25996 if (permerror (input_location, "direct-list-initialization of "
25997 "%<auto%> requires exactly one element"))
25998 inform (input_location,
25999 "for deduction to %<std::initializer_list%>, use copy-"
26000 "list-initialization (i.e. add %<=%> before the %<{%>)");
26001 }
26002 type = listify_autos (type, auto_node);
26003 }
26004 }
26005
26006 if (type == error_mark_node)
26007 return error_mark_node;
26008
26009 init = resolve_nondeduced_context (init, complain);
26010
26011 if (context == adc_decomp_type
26012 && auto_node == type
26013 && init != error_mark_node
26014 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26015 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26016 and initializer has array type, deduce cv-qualified array type. */
26017 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26018 complain);
26019 else if (AUTO_IS_DECLTYPE (auto_node))
26020 {
26021 bool id = (DECL_P (init)
26022 || ((TREE_CODE (init) == COMPONENT_REF
26023 || TREE_CODE (init) == SCOPE_REF)
26024 && !REF_PARENTHESIZED_P (init)));
26025 targs = make_tree_vec (1);
26026 TREE_VEC_ELT (targs, 0)
26027 = finish_decltype_type (init, id, tf_warning_or_error);
26028 if (type != auto_node)
26029 {
26030 if (complain & tf_error)
26031 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26032 return error_mark_node;
26033 }
26034 }
26035 else
26036 {
26037 tree parms = build_tree_list (NULL_TREE, type);
26038 tree tparms;
26039
26040 if (flag_concepts)
26041 tparms = extract_autos (type);
26042 else
26043 {
26044 tparms = make_tree_vec (1);
26045 TREE_VEC_ELT (tparms, 0)
26046 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26047 }
26048
26049 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26050 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26051 DEDUCE_CALL, LOOKUP_NORMAL,
26052 NULL, /*explain_p=*/false);
26053 if (val > 0)
26054 {
26055 if (processing_template_decl)
26056 /* Try again at instantiation time. */
26057 return type;
26058 if (type && type != error_mark_node
26059 && (complain & tf_error))
26060 /* If type is error_mark_node a diagnostic must have been
26061 emitted by now. Also, having a mention to '<type error>'
26062 in the diagnostic is not really useful to the user. */
26063 {
26064 if (cfun && auto_node == current_function_auto_return_pattern
26065 && LAMBDA_FUNCTION_P (current_function_decl))
26066 error ("unable to deduce lambda return type from %qE", init);
26067 else
26068 error ("unable to deduce %qT from %qE", type, init);
26069 type_unification_real (tparms, targs, parms, &init, 1, 0,
26070 DEDUCE_CALL, LOOKUP_NORMAL,
26071 NULL, /*explain_p=*/true);
26072 }
26073 return error_mark_node;
26074 }
26075 }
26076
26077 /* Check any placeholder constraints against the deduced type. */
26078 if (flag_concepts && !processing_template_decl)
26079 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26080 {
26081 /* Use the deduced type to check the associated constraints. If we
26082 have a partial-concept-id, rebuild the argument list so that
26083 we check using the extra arguments. */
26084 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26085 tree cargs = CHECK_CONSTR_ARGS (constr);
26086 if (TREE_VEC_LENGTH (cargs) > 1)
26087 {
26088 cargs = copy_node (cargs);
26089 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26090 }
26091 else
26092 cargs = targs;
26093 if (!constraints_satisfied_p (constr, cargs))
26094 {
26095 if (complain & tf_warning_or_error)
26096 {
26097 switch (context)
26098 {
26099 case adc_unspecified:
26100 case adc_unify:
26101 error("placeholder constraints not satisfied");
26102 break;
26103 case adc_variable_type:
26104 case adc_decomp_type:
26105 error ("deduced initializer does not satisfy "
26106 "placeholder constraints");
26107 break;
26108 case adc_return_type:
26109 error ("deduced return type does not satisfy "
26110 "placeholder constraints");
26111 break;
26112 case adc_requirement:
26113 error ("deduced expression type does not satisfy "
26114 "placeholder constraints");
26115 break;
26116 }
26117 diagnose_constraints (input_location, constr, targs);
26118 }
26119 return error_mark_node;
26120 }
26121 }
26122
26123 if (processing_template_decl && context != adc_unify)
26124 outer_targs = current_template_args ();
26125 targs = add_to_template_args (outer_targs, targs);
26126 return tsubst (type, targs, complain, NULL_TREE);
26127 }
26128
26129 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26130 result. */
26131
26132 tree
26133 splice_late_return_type (tree type, tree late_return_type)
26134 {
26135 if (is_auto (type))
26136 {
26137 if (late_return_type)
26138 return late_return_type;
26139
26140 tree idx = get_template_parm_index (type);
26141 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26142 /* In an abbreviated function template we didn't know we were dealing
26143 with a function template when we saw the auto return type, so update
26144 it to have the correct level. */
26145 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26146 }
26147 return type;
26148 }
26149
26150 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26151 'decltype(auto)' or a deduced class template. */
26152
26153 bool
26154 is_auto (const_tree type)
26155 {
26156 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26157 && (TYPE_IDENTIFIER (type) == auto_identifier
26158 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26159 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26160 return true;
26161 else
26162 return false;
26163 }
26164
26165 /* for_each_template_parm callback for type_uses_auto. */
26166
26167 int
26168 is_auto_r (tree tp, void */*data*/)
26169 {
26170 return is_auto (tp);
26171 }
26172
26173 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26174 a use of `auto'. Returns NULL_TREE otherwise. */
26175
26176 tree
26177 type_uses_auto (tree type)
26178 {
26179 if (type == NULL_TREE)
26180 return NULL_TREE;
26181 else if (flag_concepts)
26182 {
26183 /* The Concepts TS allows multiple autos in one type-specifier; just
26184 return the first one we find, do_auto_deduction will collect all of
26185 them. */
26186 if (uses_template_parms (type))
26187 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26188 /*visited*/NULL, /*nondeduced*/true);
26189 else
26190 return NULL_TREE;
26191 }
26192 else
26193 return find_type_usage (type, is_auto);
26194 }
26195
26196 /* For a given template T, return the vector of typedefs referenced
26197 in T for which access check is needed at T instantiation time.
26198 T is either a FUNCTION_DECL or a RECORD_TYPE.
26199 Those typedefs were added to T by the function
26200 append_type_to_template_for_access_check. */
26201
26202 vec<qualified_typedef_usage_t, va_gc> *
26203 get_types_needing_access_check (tree t)
26204 {
26205 tree ti;
26206 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26207
26208 if (!t || t == error_mark_node)
26209 return NULL;
26210
26211 if (!(ti = get_template_info (t)))
26212 return NULL;
26213
26214 if (CLASS_TYPE_P (t)
26215 || TREE_CODE (t) == FUNCTION_DECL)
26216 {
26217 if (!TI_TEMPLATE (ti))
26218 return NULL;
26219
26220 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26221 }
26222
26223 return result;
26224 }
26225
26226 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26227 tied to T. That list of typedefs will be access checked at
26228 T instantiation time.
26229 T is either a FUNCTION_DECL or a RECORD_TYPE.
26230 TYPE_DECL is a TYPE_DECL node representing a typedef.
26231 SCOPE is the scope through which TYPE_DECL is accessed.
26232 LOCATION is the location of the usage point of TYPE_DECL.
26233
26234 This function is a subroutine of
26235 append_type_to_template_for_access_check. */
26236
26237 static void
26238 append_type_to_template_for_access_check_1 (tree t,
26239 tree type_decl,
26240 tree scope,
26241 location_t location)
26242 {
26243 qualified_typedef_usage_t typedef_usage;
26244 tree ti;
26245
26246 if (!t || t == error_mark_node)
26247 return;
26248
26249 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26250 || CLASS_TYPE_P (t))
26251 && type_decl
26252 && TREE_CODE (type_decl) == TYPE_DECL
26253 && scope);
26254
26255 if (!(ti = get_template_info (t)))
26256 return;
26257
26258 gcc_assert (TI_TEMPLATE (ti));
26259
26260 typedef_usage.typedef_decl = type_decl;
26261 typedef_usage.context = scope;
26262 typedef_usage.locus = location;
26263
26264 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26265 }
26266
26267 /* Append TYPE_DECL to the template TEMPL.
26268 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26269 At TEMPL instanciation time, TYPE_DECL will be checked to see
26270 if it can be accessed through SCOPE.
26271 LOCATION is the location of the usage point of TYPE_DECL.
26272
26273 e.g. consider the following code snippet:
26274
26275 class C
26276 {
26277 typedef int myint;
26278 };
26279
26280 template<class U> struct S
26281 {
26282 C::myint mi; // <-- usage point of the typedef C::myint
26283 };
26284
26285 S<char> s;
26286
26287 At S<char> instantiation time, we need to check the access of C::myint
26288 In other words, we need to check the access of the myint typedef through
26289 the C scope. For that purpose, this function will add the myint typedef
26290 and the scope C through which its being accessed to a list of typedefs
26291 tied to the template S. That list will be walked at template instantiation
26292 time and access check performed on each typedefs it contains.
26293 Note that this particular code snippet should yield an error because
26294 myint is private to C. */
26295
26296 void
26297 append_type_to_template_for_access_check (tree templ,
26298 tree type_decl,
26299 tree scope,
26300 location_t location)
26301 {
26302 qualified_typedef_usage_t *iter;
26303 unsigned i;
26304
26305 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26306
26307 /* Make sure we don't append the type to the template twice. */
26308 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26309 if (iter->typedef_decl == type_decl && scope == iter->context)
26310 return;
26311
26312 append_type_to_template_for_access_check_1 (templ, type_decl,
26313 scope, location);
26314 }
26315
26316 /* Convert the generic type parameters in PARM that match the types given in the
26317 range [START_IDX, END_IDX) from the current_template_parms into generic type
26318 packs. */
26319
26320 tree
26321 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26322 {
26323 tree current = current_template_parms;
26324 int depth = TMPL_PARMS_DEPTH (current);
26325 current = INNERMOST_TEMPLATE_PARMS (current);
26326 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26327
26328 for (int i = 0; i < start_idx; ++i)
26329 TREE_VEC_ELT (replacement, i)
26330 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26331
26332 for (int i = start_idx; i < end_idx; ++i)
26333 {
26334 /* Create a distinct parameter pack type from the current parm and add it
26335 to the replacement args to tsubst below into the generic function
26336 parameter. */
26337
26338 tree o = TREE_TYPE (TREE_VALUE
26339 (TREE_VEC_ELT (current, i)));
26340 tree t = copy_type (o);
26341 TEMPLATE_TYPE_PARM_INDEX (t)
26342 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26343 o, 0, 0, tf_none);
26344 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26345 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26346 TYPE_MAIN_VARIANT (t) = t;
26347 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26348 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26349 TREE_VEC_ELT (replacement, i) = t;
26350 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26351 }
26352
26353 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26354 TREE_VEC_ELT (replacement, i)
26355 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26356
26357 /* If there are more levels then build up the replacement with the outer
26358 template parms. */
26359 if (depth > 1)
26360 replacement = add_to_template_args (template_parms_to_args
26361 (TREE_CHAIN (current_template_parms)),
26362 replacement);
26363
26364 return tsubst (parm, replacement, tf_none, NULL_TREE);
26365 }
26366
26367 /* Entries in the decl_constraint hash table. */
26368 struct GTY((for_user)) constr_entry
26369 {
26370 tree decl;
26371 tree ci;
26372 };
26373
26374 /* Hashing function and equality for constraint entries. */
26375 struct constr_hasher : ggc_ptr_hash<constr_entry>
26376 {
26377 static hashval_t hash (constr_entry *e)
26378 {
26379 return (hashval_t)DECL_UID (e->decl);
26380 }
26381
26382 static bool equal (constr_entry *e1, constr_entry *e2)
26383 {
26384 return e1->decl == e2->decl;
26385 }
26386 };
26387
26388 /* A mapping from declarations to constraint information. Note that
26389 both templates and their underlying declarations are mapped to the
26390 same constraint information.
26391
26392 FIXME: This is defined in pt.c because garbage collection
26393 code is not being generated for constraint.cc. */
26394
26395 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26396
26397 /* Returns the template constraints of declaration T. If T is not
26398 constrained, return NULL_TREE. Note that T must be non-null. */
26399
26400 tree
26401 get_constraints (tree t)
26402 {
26403 if (!flag_concepts)
26404 return NULL_TREE;
26405
26406 gcc_assert (DECL_P (t));
26407 if (TREE_CODE (t) == TEMPLATE_DECL)
26408 t = DECL_TEMPLATE_RESULT (t);
26409 constr_entry elt = { t, NULL_TREE };
26410 constr_entry* found = decl_constraints->find (&elt);
26411 if (found)
26412 return found->ci;
26413 else
26414 return NULL_TREE;
26415 }
26416
26417 /* Associate the given constraint information CI with the declaration
26418 T. If T is a template, then the constraints are associated with
26419 its underlying declaration. Don't build associations if CI is
26420 NULL_TREE. */
26421
26422 void
26423 set_constraints (tree t, tree ci)
26424 {
26425 if (!ci)
26426 return;
26427 gcc_assert (t && flag_concepts);
26428 if (TREE_CODE (t) == TEMPLATE_DECL)
26429 t = DECL_TEMPLATE_RESULT (t);
26430 gcc_assert (!get_constraints (t));
26431 constr_entry elt = {t, ci};
26432 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26433 constr_entry* entry = ggc_alloc<constr_entry> ();
26434 *entry = elt;
26435 *slot = entry;
26436 }
26437
26438 /* Remove the associated constraints of the declaration T. */
26439
26440 void
26441 remove_constraints (tree t)
26442 {
26443 gcc_assert (DECL_P (t));
26444 if (TREE_CODE (t) == TEMPLATE_DECL)
26445 t = DECL_TEMPLATE_RESULT (t);
26446
26447 constr_entry elt = {t, NULL_TREE};
26448 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26449 if (slot)
26450 decl_constraints->clear_slot (slot);
26451 }
26452
26453 /* Memoized satisfaction results for declarations. This
26454 maps the pair (constraint_info, arguments) to the result computed
26455 by constraints_satisfied_p. */
26456
26457 struct GTY((for_user)) constraint_sat_entry
26458 {
26459 tree ci;
26460 tree args;
26461 tree result;
26462 };
26463
26464 /* Hashing function and equality for constraint entries. */
26465
26466 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26467 {
26468 static hashval_t hash (constraint_sat_entry *e)
26469 {
26470 hashval_t val = iterative_hash_object(e->ci, 0);
26471 return iterative_hash_template_arg (e->args, val);
26472 }
26473
26474 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26475 {
26476 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26477 }
26478 };
26479
26480 /* Memoized satisfaction results for concept checks. */
26481
26482 struct GTY((for_user)) concept_spec_entry
26483 {
26484 tree tmpl;
26485 tree args;
26486 tree result;
26487 };
26488
26489 /* Hashing function and equality for constraint entries. */
26490
26491 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26492 {
26493 static hashval_t hash (concept_spec_entry *e)
26494 {
26495 return hash_tmpl_and_args (e->tmpl, e->args);
26496 }
26497
26498 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26499 {
26500 ++comparing_specializations;
26501 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26502 --comparing_specializations;
26503 return eq;
26504 }
26505 };
26506
26507 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26508 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26509
26510 /* Search for a memoized satisfaction result. Returns one of the
26511 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26512
26513 tree
26514 lookup_constraint_satisfaction (tree ci, tree args)
26515 {
26516 constraint_sat_entry elt = { ci, args, NULL_TREE };
26517 constraint_sat_entry* found = constraint_memos->find (&elt);
26518 if (found)
26519 return found->result;
26520 else
26521 return NULL_TREE;
26522 }
26523
26524 /* Memoize the result of a satisfication test. Returns the saved result. */
26525
26526 tree
26527 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26528 {
26529 constraint_sat_entry elt = {ci, args, result};
26530 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26531 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26532 *entry = elt;
26533 *slot = entry;
26534 return result;
26535 }
26536
26537 /* Search for a memoized satisfaction result for a concept. */
26538
26539 tree
26540 lookup_concept_satisfaction (tree tmpl, tree args)
26541 {
26542 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26543 concept_spec_entry* found = concept_memos->find (&elt);
26544 if (found)
26545 return found->result;
26546 else
26547 return NULL_TREE;
26548 }
26549
26550 /* Memoize the result of a concept check. Returns the saved result. */
26551
26552 tree
26553 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26554 {
26555 concept_spec_entry elt = {tmpl, args, result};
26556 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26557 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26558 *entry = elt;
26559 *slot = entry;
26560 return result;
26561 }
26562
26563 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26564
26565 /* Returns a prior concept specialization. This returns the substituted
26566 and normalized constraints defined by the concept. */
26567
26568 tree
26569 get_concept_expansion (tree tmpl, tree args)
26570 {
26571 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26572 concept_spec_entry* found = concept_expansions->find (&elt);
26573 if (found)
26574 return found->result;
26575 else
26576 return NULL_TREE;
26577 }
26578
26579 /* Save a concept expansion for later. */
26580
26581 tree
26582 save_concept_expansion (tree tmpl, tree args, tree def)
26583 {
26584 concept_spec_entry elt = {tmpl, args, def};
26585 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26586 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26587 *entry = elt;
26588 *slot = entry;
26589 return def;
26590 }
26591
26592 static hashval_t
26593 hash_subsumption_args (tree t1, tree t2)
26594 {
26595 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26596 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26597 int val = 0;
26598 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26599 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26600 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26601 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26602 return val;
26603 }
26604
26605 /* Compare the constraints of two subsumption entries. The LEFT1 and
26606 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26607 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26608
26609 static bool
26610 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26611 {
26612 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26613 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26614 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26615 CHECK_CONSTR_ARGS (right1)))
26616 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26617 CHECK_CONSTR_ARGS (right2));
26618 return false;
26619 }
26620
26621 /* Key/value pair for learning and memoizing subsumption results. This
26622 associates a pair of check constraints (including arguments) with
26623 a boolean value indicating the result. */
26624
26625 struct GTY((for_user)) subsumption_entry
26626 {
26627 tree t1;
26628 tree t2;
26629 bool result;
26630 };
26631
26632 /* Hashing function and equality for constraint entries. */
26633
26634 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26635 {
26636 static hashval_t hash (subsumption_entry *e)
26637 {
26638 return hash_subsumption_args (e->t1, e->t2);
26639 }
26640
26641 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26642 {
26643 ++comparing_specializations;
26644 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26645 --comparing_specializations;
26646 return eq;
26647 }
26648 };
26649
26650 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26651
26652 /* Search for a previously cached subsumption result. */
26653
26654 bool*
26655 lookup_subsumption_result (tree t1, tree t2)
26656 {
26657 subsumption_entry elt = { t1, t2, false };
26658 subsumption_entry* found = subsumption_table->find (&elt);
26659 if (found)
26660 return &found->result;
26661 else
26662 return 0;
26663 }
26664
26665 /* Save a subsumption result. */
26666
26667 bool
26668 save_subsumption_result (tree t1, tree t2, bool result)
26669 {
26670 subsumption_entry elt = {t1, t2, result};
26671 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26672 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26673 *entry = elt;
26674 *slot = entry;
26675 return result;
26676 }
26677
26678 /* Set up the hash table for constraint association. */
26679
26680 void
26681 init_constraint_processing (void)
26682 {
26683 if (!flag_concepts)
26684 return;
26685
26686 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26687 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26688 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26689 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26690 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26691 }
26692
26693 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26694 0..N-1. */
26695
26696 void
26697 declare_integer_pack (void)
26698 {
26699 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26700 build_function_type_list (integer_type_node,
26701 integer_type_node,
26702 NULL_TREE),
26703 NULL_TREE, ECF_CONST);
26704 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26705 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26706 }
26707
26708 /* Set up the hash tables for template instantiations. */
26709
26710 void
26711 init_template_processing (void)
26712 {
26713 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26714 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26715
26716 if (cxx_dialect >= cxx11)
26717 declare_integer_pack ();
26718 }
26719
26720 /* Print stats about the template hash tables for -fstats. */
26721
26722 void
26723 print_template_statistics (void)
26724 {
26725 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26726 "%f collisions\n", (long) decl_specializations->size (),
26727 (long) decl_specializations->elements (),
26728 decl_specializations->collisions ());
26729 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26730 "%f collisions\n", (long) type_specializations->size (),
26731 (long) type_specializations->elements (),
26732 type_specializations->collisions ());
26733 }
26734
26735 #if CHECKING_P
26736
26737 namespace selftest {
26738
26739 /* Verify that build_non_dependent_expr () works, for various expressions,
26740 and that location wrappers don't affect the results. */
26741
26742 static void
26743 test_build_non_dependent_expr ()
26744 {
26745 location_t loc = BUILTINS_LOCATION;
26746
26747 /* Verify constants, without and with location wrappers. */
26748 tree int_cst = build_int_cst (integer_type_node, 42);
26749 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26750
26751 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26752 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26753 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26754
26755 tree string_lit = build_string (4, "foo");
26756 TREE_TYPE (string_lit) = char_array_type_node;
26757 string_lit = fix_string_type (string_lit);
26758 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26759
26760 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26761 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26762 ASSERT_EQ (wrapped_string_lit,
26763 build_non_dependent_expr (wrapped_string_lit));
26764 }
26765
26766 /* Verify that type_dependent_expression_p () works correctly, even
26767 in the presence of location wrapper nodes. */
26768
26769 static void
26770 test_type_dependent_expression_p ()
26771 {
26772 location_t loc = BUILTINS_LOCATION;
26773
26774 tree name = get_identifier ("foo");
26775
26776 /* If no templates are involved, nothing is type-dependent. */
26777 gcc_assert (!processing_template_decl);
26778 ASSERT_FALSE (type_dependent_expression_p (name));
26779
26780 ++processing_template_decl;
26781
26782 /* Within a template, an unresolved name is always type-dependent. */
26783 ASSERT_TRUE (type_dependent_expression_p (name));
26784
26785 /* Ensure it copes with NULL_TREE and errors. */
26786 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26787 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26788
26789 /* A USING_DECL in a template should be type-dependent, even if wrapped
26790 with a location wrapper (PR c++/83799). */
26791 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26792 TREE_TYPE (using_decl) = integer_type_node;
26793 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26794 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26795 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26796 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26797
26798 --processing_template_decl;
26799 }
26800
26801 /* Run all of the selftests within this file. */
26802
26803 void
26804 cp_pt_c_tests ()
26805 {
26806 test_build_non_dependent_expr ();
26807 test_type_dependent_expression_p ();
26808 }
26809
26810 } // namespace selftest
26811
26812 #endif /* #if CHECKING_P */
26813
26814 #include "gt-cp-pt.h"