]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Use releasing_vec more broadly.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int check_non_deducible_conversion (tree, tree, int, int,
159 struct conversion **, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
161 tree);
162 static int type_unification_real (tree, tree, tree, const tree *,
163 unsigned int, int, unification_kind_t,
164 vec<deferred_access_check, va_gc> **,
165 bool);
166 static void note_template_header (int);
167 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
168 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
169 static tree convert_template_argument (tree, tree, tree,
170 tsubst_flags_t, int, tree);
171 static tree for_each_template_parm (tree, tree_fn_t, void*,
172 hash_set<tree> *, bool, tree_fn_t = NULL);
173 static tree expand_template_argument_pack (tree);
174 static tree build_template_parm_index (int, int, int, tree, tree);
175 static bool inline_needs_template_parms (tree, bool);
176 static void push_inline_template_parms_recursive (tree, int);
177 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
178 static int mark_template_parm (tree, void *);
179 static int template_parm_this_level_p (tree, void *);
180 static tree tsubst_friend_function (tree, tree);
181 static tree tsubst_friend_class (tree, tree);
182 static int can_complete_type_without_circularity (tree);
183 static tree get_bindings (tree, tree, tree, bool);
184 static int template_decl_level (tree);
185 static int check_cv_quals_for_unify (int, tree, tree);
186 static void template_parm_level_and_index (tree, int*, int*);
187 static int unify_pack_expansion (tree, tree, tree,
188 tree, unification_kind_t, bool, bool);
189 static tree copy_template_args (tree);
190 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
204 tree, tree);
205 static bool template_template_parm_bindings_ok_p (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
225 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
226 static tree make_argument_pack (tree);
227 static void register_parameter_specializations (tree, tree);
228 static tree enclosing_instantiation_of (tree tctx);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
234
235 static void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_CLASS_SCOPE_P (t))
244 push_nested_class (DECL_CONTEXT (t));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 saved_access_scope = tree_cons
251 (NULL_TREE, current_function_decl, saved_access_scope);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 static void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 {
264 current_function_decl = TREE_VALUE (saved_access_scope);
265 saved_access_scope = TREE_CHAIN (saved_access_scope);
266 }
267
268 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error ("data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error ("invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 type = CP_DECL_CONTEXT (type);
394 else if (LAMBDA_TYPE_P (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
406 static bool
407 instantiates_primary_template_p (tree node)
408 {
409 tree tinfo = get_template_info (node);
410 if (!tinfo)
411 return false;
412
413 tree tmpl = TI_TEMPLATE (tinfo);
414 if (PRIMARY_TEMPLATE_P (tmpl))
415 return true;
416
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
418 return false;
419
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
423
424 tree ctxt = DECL_CONTEXT (tmpl);
425 tree ctinfo = get_template_info (ctxt);
426 if (!ctinfo)
427 return true;
428
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
431 }
432
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
435
436 static bool
437 inline_needs_template_parms (tree decl, bool nsdmi)
438 {
439 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
440 return false;
441
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
443 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
444 }
445
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
449 innermost first. */
450
451 static void
452 push_inline_template_parms_recursive (tree parmlist, int levels)
453 {
454 tree parms = TREE_VALUE (parmlist);
455 int i;
456
457 if (levels > 1)
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
459
460 ++processing_template_decl;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl),
463 parms, current_template_parms);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
465
466 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
467 NULL);
468 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
469 {
470 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
471
472 if (error_operand_p (parm))
473 continue;
474
475 gcc_assert (DECL_P (parm));
476
477 switch (TREE_CODE (parm))
478 {
479 case TYPE_DECL:
480 case TEMPLATE_DECL:
481 pushdecl (parm);
482 break;
483
484 case PARM_DECL:
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
487 break;
488
489 default:
490 gcc_unreachable ();
491 }
492 }
493 }
494
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
498
499 void
500 maybe_begin_member_template_processing (tree decl)
501 {
502 tree parms;
503 int levels = 0;
504 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
505
506 if (nsdmi)
507 {
508 tree ctx = DECL_CONTEXT (decl);
509 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx)
512 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
513 }
514
515 if (inline_needs_template_parms (decl, nsdmi))
516 {
517 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
518 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
519
520 if (DECL_TEMPLATE_SPECIALIZATION (decl))
521 {
522 --levels;
523 parms = TREE_CHAIN (parms);
524 }
525
526 push_inline_template_parms_recursive (parms, levels);
527 }
528
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels.safe_push (levels);
532 }
533
534 /* Undo the effects of maybe_begin_member_template_processing. */
535
536 void
537 maybe_end_member_template_processing (void)
538 {
539 int i;
540 int last;
541
542 if (inline_parm_levels.length () == 0)
543 return;
544
545 last = inline_parm_levels.pop ();
546 for (i = 0; i < last; ++i)
547 {
548 --processing_template_decl;
549 current_template_parms = TREE_CHAIN (current_template_parms);
550 poplevel (0, 0, 0);
551 }
552 }
553
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
556
557 static tree
558 add_to_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561 int extra_depth;
562 int i;
563 int j;
564
565 if (args == NULL_TREE || extra_args == error_mark_node)
566 return extra_args;
567
568 extra_depth = TMPL_ARGS_DEPTH (extra_args);
569 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
570
571 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
573
574 for (j = 1; j <= extra_depth; ++j, ++i)
575 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
576
577 return new_args;
578 }
579
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation. */
586
587 static tree
588 add_outermost_template_args (tree args, tree extra_args)
589 {
590 tree new_args;
591
592 /* If there are more levels of EXTRA_ARGS than there are ARGS,
593 something very fishy is going on. */
594 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
595
596 /* If *all* the new arguments will be the EXTRA_ARGS, just return
597 them. */
598 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
599 return extra_args;
600
601 /* For the moment, we make ARGS look like it contains fewer levels. */
602 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
603
604 new_args = add_to_template_args (args, extra_args);
605
606 /* Now, we restore ARGS to its full dimensions. */
607 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
608
609 return new_args;
610 }
611
612 /* Return the N levels of innermost template arguments from the ARGS. */
613
614 tree
615 get_innermost_template_args (tree args, int n)
616 {
617 tree new_args;
618 int extra_levels;
619 int i;
620
621 gcc_assert (n >= 0);
622
623 /* If N is 1, just return the innermost set of template arguments. */
624 if (n == 1)
625 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
626
627 /* If we're not removing anything, just return the arguments we were
628 given. */
629 extra_levels = TMPL_ARGS_DEPTH (args) - n;
630 gcc_assert (extra_levels >= 0);
631 if (extra_levels == 0)
632 return args;
633
634 /* Make a new set of arguments, not containing the outer arguments. */
635 new_args = make_tree_vec (n);
636 for (i = 1; i <= n; ++i)
637 SET_TMPL_ARGS_LEVEL (new_args, i,
638 TMPL_ARGS_LEVEL (args, i + extra_levels));
639
640 return new_args;
641 }
642
643 /* The inverse of get_innermost_template_args: Return all but the innermost
644 EXTRA_LEVELS levels of template arguments from the ARGS. */
645
646 static tree
647 strip_innermost_template_args (tree args, int extra_levels)
648 {
649 tree new_args;
650 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
651 int i;
652
653 gcc_assert (n >= 0);
654
655 /* If N is 1, just return the outermost set of template arguments. */
656 if (n == 1)
657 return TMPL_ARGS_LEVEL (args, 1);
658
659 /* If we're not removing anything, just return the arguments we were
660 given. */
661 gcc_assert (extra_levels >= 0);
662 if (extra_levels == 0)
663 return args;
664
665 /* Make a new set of arguments, not containing the inner arguments. */
666 new_args = make_tree_vec (n);
667 for (i = 1; i <= n; ++i)
668 SET_TMPL_ARGS_LEVEL (new_args, i,
669 TMPL_ARGS_LEVEL (args, i));
670
671 return new_args;
672 }
673
674 /* We've got a template header coming up; push to a new level for storing
675 the parms. */
676
677 void
678 begin_template_parm_list (void)
679 {
680 /* We use a non-tag-transparent scope here, which causes pushtag to
681 put tags in this scope, rather than in the enclosing class or
682 namespace scope. This is the right thing, since we want
683 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
684 global template class, push_template_decl handles putting the
685 TEMPLATE_DECL into top-level scope. For a nested template class,
686 e.g.:
687
688 template <class T> struct S1 {
689 template <class T> struct S2 {};
690 };
691
692 pushtag contains special code to insert the TEMPLATE_DECL for S2
693 at the right scope. */
694 begin_scope (sk_template_parms, NULL);
695 ++processing_template_decl;
696 ++processing_template_parmlist;
697 note_template_header (0);
698
699 /* Add a dummy parameter level while we process the parameter list. */
700 current_template_parms
701 = tree_cons (size_int (processing_template_decl),
702 make_tree_vec (0),
703 current_template_parms);
704 }
705
706 /* This routine is called when a specialization is declared. If it is
707 invalid to declare a specialization here, an error is reported and
708 false is returned, otherwise this routine will return true. */
709
710 static bool
711 check_specialization_scope (void)
712 {
713 tree scope = current_scope ();
714
715 /* [temp.expl.spec]
716
717 An explicit specialization shall be declared in the namespace of
718 which the template is a member, or, for member templates, in the
719 namespace of which the enclosing class or enclosing class
720 template is a member. An explicit specialization of a member
721 function, member class or static data member of a class template
722 shall be declared in the namespace of which the class template
723 is a member. */
724 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
725 {
726 error ("explicit specialization in non-namespace scope %qD", scope);
727 return false;
728 }
729
730 /* [temp.expl.spec]
731
732 In an explicit specialization declaration for a member of a class
733 template or a member template that appears in namespace scope,
734 the member template and some of its enclosing class templates may
735 remain unspecialized, except that the declaration shall not
736 explicitly specialize a class member template if its enclosing
737 class templates are not explicitly specialized as well. */
738 if (current_template_parms)
739 {
740 error ("enclosing class templates are not explicitly specialized");
741 return false;
742 }
743
744 return true;
745 }
746
747 /* We've just seen template <>. */
748
749 bool
750 begin_specialization (void)
751 {
752 begin_scope (sk_template_spec, NULL);
753 note_template_header (1);
754 return check_specialization_scope ();
755 }
756
757 /* Called at then end of processing a declaration preceded by
758 template<>. */
759
760 void
761 end_specialization (void)
762 {
763 finish_scope ();
764 reset_specialization ();
765 }
766
767 /* Any template <>'s that we have seen thus far are not referring to a
768 function specialization. */
769
770 void
771 reset_specialization (void)
772 {
773 processing_specialization = 0;
774 template_header_count = 0;
775 }
776
777 /* We've just seen a template header. If SPECIALIZATION is nonzero,
778 it was of the form template <>. */
779
780 static void
781 note_template_header (int specialization)
782 {
783 processing_specialization = specialization;
784 template_header_count++;
785 }
786
787 /* We're beginning an explicit instantiation. */
788
789 void
790 begin_explicit_instantiation (void)
791 {
792 gcc_assert (!processing_explicit_instantiation);
793 processing_explicit_instantiation = true;
794 }
795
796
797 void
798 end_explicit_instantiation (void)
799 {
800 gcc_assert (processing_explicit_instantiation);
801 processing_explicit_instantiation = false;
802 }
803
804 /* An explicit specialization or partial specialization of TMPL is being
805 declared. Check that the namespace in which the specialization is
806 occurring is permissible. Returns false iff it is invalid to
807 specialize TMPL in the current namespace. */
808
809 static bool
810 check_specialization_namespace (tree tmpl)
811 {
812 tree tpl_ns = decl_namespace_context (tmpl);
813
814 /* [tmpl.expl.spec]
815
816 An explicit specialization shall be declared in a namespace enclosing the
817 specialized template. An explicit specialization whose declarator-id is
818 not qualified shall be declared in the nearest enclosing namespace of the
819 template, or, if the namespace is inline (7.3.1), any namespace from its
820 enclosing namespace set. */
821 if (current_scope() != DECL_CONTEXT (tmpl)
822 && !at_namespace_scope_p ())
823 {
824 error ("specialization of %qD must appear at namespace scope", tmpl);
825 return false;
826 }
827
828 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
829 /* Same or enclosing namespace. */
830 return true;
831 else
832 {
833 auto_diagnostic_group d;
834 if (permerror (input_location,
835 "specialization of %qD in different namespace", tmpl))
836 inform (DECL_SOURCE_LOCATION (tmpl),
837 " from definition of %q#D", tmpl);
838 return false;
839 }
840 }
841
842 /* SPEC is an explicit instantiation. Check that it is valid to
843 perform this explicit instantiation in the current namespace. */
844
845 static void
846 check_explicit_instantiation_namespace (tree spec)
847 {
848 tree ns;
849
850 /* DR 275: An explicit instantiation shall appear in an enclosing
851 namespace of its template. */
852 ns = decl_namespace_context (spec);
853 if (!is_nested_namespace (current_namespace, ns))
854 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
855 "(which does not enclose namespace %qD)",
856 spec, current_namespace, ns);
857 }
858
859 // Returns the type of a template specialization only if that
860 // specialization needs to be defined. Otherwise (e.g., if the type has
861 // already been defined), the function returns NULL_TREE.
862 static tree
863 maybe_new_partial_specialization (tree type)
864 {
865 // An implicit instantiation of an incomplete type implies
866 // the definition of a new class template.
867 //
868 // template<typename T>
869 // struct S;
870 //
871 // template<typename T>
872 // struct S<T*>;
873 //
874 // Here, S<T*> is an implicit instantiation of S whose type
875 // is incomplete.
876 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
877 return type;
878
879 // It can also be the case that TYPE is a completed specialization.
880 // Continuing the previous example, suppose we also declare:
881 //
882 // template<typename T>
883 // requires Integral<T>
884 // struct S<T*>;
885 //
886 // Here, S<T*> refers to the specialization S<T*> defined
887 // above. However, we need to differentiate definitions because
888 // we intend to define a new partial specialization. In this case,
889 // we rely on the fact that the constraints are different for
890 // this declaration than that above.
891 //
892 // Note that we also get here for injected class names and
893 // late-parsed template definitions. We must ensure that we
894 // do not create new type declarations for those cases.
895 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
896 {
897 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
898 tree args = CLASSTYPE_TI_ARGS (type);
899
900 // If there are no template parameters, this cannot be a new
901 // partial template specializtion?
902 if (!current_template_parms)
903 return NULL_TREE;
904
905 // The injected-class-name is not a new partial specialization.
906 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
907 return NULL_TREE;
908
909 // If the constraints are not the same as those of the primary
910 // then, we can probably create a new specialization.
911 tree type_constr = current_template_constraints ();
912
913 if (type == TREE_TYPE (tmpl))
914 {
915 tree main_constr = get_constraints (tmpl);
916 if (equivalent_constraints (type_constr, main_constr))
917 return NULL_TREE;
918 }
919
920 // Also, if there's a pre-existing specialization with matching
921 // constraints, then this also isn't new.
922 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
923 while (specs)
924 {
925 tree spec_tmpl = TREE_VALUE (specs);
926 tree spec_args = TREE_PURPOSE (specs);
927 tree spec_constr = get_constraints (spec_tmpl);
928 if (comp_template_args (args, spec_args)
929 && equivalent_constraints (type_constr, spec_constr))
930 return NULL_TREE;
931 specs = TREE_CHAIN (specs);
932 }
933
934 // Create a new type node (and corresponding type decl)
935 // for the newly declared specialization.
936 tree t = make_class_type (TREE_CODE (type));
937 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
938 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
939
940 /* We only need a separate type node for storing the definition of this
941 partial specialization; uses of S<T*> are unconstrained, so all are
942 equivalent. So keep TYPE_CANONICAL the same. */
943 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
944
945 // Build the corresponding type decl.
946 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
947 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
948 DECL_SOURCE_LOCATION (d) = input_location;
949
950 return t;
951 }
952
953 return NULL_TREE;
954 }
955
956 /* The TYPE is being declared. If it is a template type, that means it
957 is a partial specialization. Do appropriate error-checking. */
958
959 tree
960 maybe_process_partial_specialization (tree type)
961 {
962 tree context;
963
964 if (type == error_mark_node)
965 return error_mark_node;
966
967 /* A lambda that appears in specialization context is not itself a
968 specialization. */
969 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
970 return type;
971
972 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
973 {
974 error ("name of class shadows template template parameter %qD",
975 TYPE_NAME (type));
976 return error_mark_node;
977 }
978
979 context = TYPE_CONTEXT (type);
980
981 if (TYPE_ALIAS_P (type))
982 {
983 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
984
985 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
986 error ("specialization of alias template %qD",
987 TI_TEMPLATE (tinfo));
988 else
989 error ("explicit specialization of non-template %qT", type);
990 return error_mark_node;
991 }
992 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
993 {
994 /* This is for ordinary explicit specialization and partial
995 specialization of a template class such as:
996
997 template <> class C<int>;
998
999 or:
1000
1001 template <class T> class C<T*>;
1002
1003 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1004
1005 if (tree t = maybe_new_partial_specialization (type))
1006 {
1007 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1008 && !at_namespace_scope_p ())
1009 return error_mark_node;
1010 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1011 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1012 if (processing_template_decl)
1013 {
1014 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1015 if (decl == error_mark_node)
1016 return error_mark_node;
1017 return TREE_TYPE (decl);
1018 }
1019 }
1020 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1021 error ("specialization of %qT after instantiation", type);
1022 else if (errorcount && !processing_specialization
1023 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1024 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1025 /* Trying to define a specialization either without a template<> header
1026 or in an inappropriate place. We've already given an error, so just
1027 bail now so we don't actually define the specialization. */
1028 return error_mark_node;
1029 }
1030 else if (CLASS_TYPE_P (type)
1031 && !CLASSTYPE_USE_TEMPLATE (type)
1032 && CLASSTYPE_TEMPLATE_INFO (type)
1033 && context && CLASS_TYPE_P (context)
1034 && CLASSTYPE_TEMPLATE_INFO (context))
1035 {
1036 /* This is for an explicit specialization of member class
1037 template according to [temp.expl.spec/18]:
1038
1039 template <> template <class U> class C<int>::D;
1040
1041 The context `C<int>' must be an implicit instantiation.
1042 Otherwise this is just a member class template declared
1043 earlier like:
1044
1045 template <> class C<int> { template <class U> class D; };
1046 template <> template <class U> class C<int>::D;
1047
1048 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1049 while in the second case, `C<int>::D' is a primary template
1050 and `C<T>::D' may not exist. */
1051
1052 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1053 && !COMPLETE_TYPE_P (type))
1054 {
1055 tree t;
1056 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1057
1058 if (current_namespace
1059 != decl_namespace_context (tmpl))
1060 {
1061 permerror (input_location,
1062 "specializing %q#T in different namespace", type);
1063 permerror (DECL_SOURCE_LOCATION (tmpl),
1064 " from definition of %q#D", tmpl);
1065 }
1066
1067 /* Check for invalid specialization after instantiation:
1068
1069 template <> template <> class C<int>::D<int>;
1070 template <> template <class U> class C<int>::D; */
1071
1072 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1073 t; t = TREE_CHAIN (t))
1074 {
1075 tree inst = TREE_VALUE (t);
1076 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1077 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1078 {
1079 /* We already have a full specialization of this partial
1080 instantiation, or a full specialization has been
1081 looked up but not instantiated. Reassign it to the
1082 new member specialization template. */
1083 spec_entry elt;
1084 spec_entry *entry;
1085
1086 elt.tmpl = most_general_template (tmpl);
1087 elt.args = CLASSTYPE_TI_ARGS (inst);
1088 elt.spec = inst;
1089
1090 type_specializations->remove_elt (&elt);
1091
1092 elt.tmpl = tmpl;
1093 CLASSTYPE_TI_ARGS (inst)
1094 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1095
1096 spec_entry **slot
1097 = type_specializations->find_slot (&elt, INSERT);
1098 entry = ggc_alloc<spec_entry> ();
1099 *entry = elt;
1100 *slot = entry;
1101 }
1102 else
1103 /* But if we've had an implicit instantiation, that's a
1104 problem ([temp.expl.spec]/6). */
1105 error ("specialization %qT after instantiation %qT",
1106 type, inst);
1107 }
1108
1109 /* Mark TYPE as a specialization. And as a result, we only
1110 have one level of template argument for the innermost
1111 class template. */
1112 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1113 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1114 CLASSTYPE_TI_ARGS (type)
1115 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1116 }
1117 }
1118 else if (processing_specialization)
1119 {
1120 /* Someday C++0x may allow for enum template specialization. */
1121 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1122 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1123 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1124 "of %qD not allowed by ISO C++", type);
1125 else
1126 {
1127 error ("explicit specialization of non-template %qT", type);
1128 return error_mark_node;
1129 }
1130 }
1131
1132 return type;
1133 }
1134
1135 /* Returns nonzero if we can optimize the retrieval of specializations
1136 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1137 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1138
1139 static inline bool
1140 optimize_specialization_lookup_p (tree tmpl)
1141 {
1142 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1143 && DECL_CLASS_SCOPE_P (tmpl)
1144 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1145 parameter. */
1146 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1147 /* The optimized lookup depends on the fact that the
1148 template arguments for the member function template apply
1149 purely to the containing class, which is not true if the
1150 containing class is an explicit or partial
1151 specialization. */
1152 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1153 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1154 && !DECL_CONV_FN_P (tmpl)
1155 /* It is possible to have a template that is not a member
1156 template and is not a member of a template class:
1157
1158 template <typename T>
1159 struct S { friend A::f(); };
1160
1161 Here, the friend function is a template, but the context does
1162 not have template information. The optimized lookup relies
1163 on having ARGS be the template arguments for both the class
1164 and the function template. */
1165 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1166 }
1167
1168 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1169 gone through coerce_template_parms by now. */
1170
1171 static void
1172 verify_unstripped_args_1 (tree inner)
1173 {
1174 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1175 {
1176 tree arg = TREE_VEC_ELT (inner, i);
1177 if (TREE_CODE (arg) == TEMPLATE_DECL)
1178 /* OK */;
1179 else if (TYPE_P (arg))
1180 gcc_assert (strip_typedefs (arg, NULL) == arg);
1181 else if (ARGUMENT_PACK_P (arg))
1182 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1183 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1184 /* Allow typedefs on the type of a non-type argument, since a
1185 parameter can have them. */;
1186 else
1187 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1188 }
1189 }
1190
1191 static void
1192 verify_unstripped_args (tree args)
1193 {
1194 ++processing_template_decl;
1195 if (!any_dependent_template_arguments_p (args))
1196 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1197 --processing_template_decl;
1198 }
1199
1200 /* Retrieve the specialization (in the sense of [temp.spec] - a
1201 specialization is either an instantiation or an explicit
1202 specialization) of TMPL for the given template ARGS. If there is
1203 no such specialization, return NULL_TREE. The ARGS are a vector of
1204 arguments, or a vector of vectors of arguments, in the case of
1205 templates with more than one level of parameters.
1206
1207 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1208 then we search for a partial specialization matching ARGS. This
1209 parameter is ignored if TMPL is not a class template.
1210
1211 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1212 result is a NONTYPE_ARGUMENT_PACK. */
1213
1214 static tree
1215 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1216 {
1217 if (tmpl == NULL_TREE)
1218 return NULL_TREE;
1219
1220 if (args == error_mark_node)
1221 return NULL_TREE;
1222
1223 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1224 || TREE_CODE (tmpl) == FIELD_DECL);
1225
1226 /* There should be as many levels of arguments as there are
1227 levels of parameters. */
1228 gcc_assert (TMPL_ARGS_DEPTH (args)
1229 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1230 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1231 : template_class_depth (DECL_CONTEXT (tmpl))));
1232
1233 if (flag_checking)
1234 verify_unstripped_args (args);
1235
1236 /* Lambda functions in templates aren't instantiated normally, but through
1237 tsubst_lambda_expr. */
1238 if (lambda_fn_in_template_p (tmpl))
1239 return NULL_TREE;
1240
1241 if (optimize_specialization_lookup_p (tmpl))
1242 {
1243 /* The template arguments actually apply to the containing
1244 class. Find the class specialization with those
1245 arguments. */
1246 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1247 tree class_specialization
1248 = retrieve_specialization (class_template, args, 0);
1249 if (!class_specialization)
1250 return NULL_TREE;
1251
1252 /* Find the instance of TMPL. */
1253 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1254 for (ovl_iterator iter (fns); iter; ++iter)
1255 {
1256 tree fn = *iter;
1257 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1258 /* using-declarations can add base methods to the method vec,
1259 and we don't want those here. */
1260 && DECL_CONTEXT (fn) == class_specialization)
1261 return fn;
1262 }
1263 return NULL_TREE;
1264 }
1265 else
1266 {
1267 spec_entry *found;
1268 spec_entry elt;
1269 hash_table<spec_hasher> *specializations;
1270
1271 elt.tmpl = tmpl;
1272 elt.args = args;
1273 elt.spec = NULL_TREE;
1274
1275 if (DECL_CLASS_TEMPLATE_P (tmpl))
1276 specializations = type_specializations;
1277 else
1278 specializations = decl_specializations;
1279
1280 if (hash == 0)
1281 hash = spec_hasher::hash (&elt);
1282 found = specializations->find_with_hash (&elt, hash);
1283 if (found)
1284 return found->spec;
1285 }
1286
1287 return NULL_TREE;
1288 }
1289
1290 /* Like retrieve_specialization, but for local declarations. */
1291
1292 tree
1293 retrieve_local_specialization (tree tmpl)
1294 {
1295 if (local_specializations == NULL)
1296 return NULL_TREE;
1297
1298 tree *slot = local_specializations->get (tmpl);
1299 return slot ? *slot : NULL_TREE;
1300 }
1301
1302 /* Returns nonzero iff DECL is a specialization of TMPL. */
1303
1304 int
1305 is_specialization_of (tree decl, tree tmpl)
1306 {
1307 tree t;
1308
1309 if (TREE_CODE (decl) == FUNCTION_DECL)
1310 {
1311 for (t = decl;
1312 t != NULL_TREE;
1313 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1314 if (t == tmpl)
1315 return 1;
1316 }
1317 else
1318 {
1319 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1320
1321 for (t = TREE_TYPE (decl);
1322 t != NULL_TREE;
1323 t = CLASSTYPE_USE_TEMPLATE (t)
1324 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1325 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1326 return 1;
1327 }
1328
1329 return 0;
1330 }
1331
1332 /* Returns nonzero iff DECL is a specialization of friend declaration
1333 FRIEND_DECL according to [temp.friend]. */
1334
1335 bool
1336 is_specialization_of_friend (tree decl, tree friend_decl)
1337 {
1338 bool need_template = true;
1339 int template_depth;
1340
1341 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1342 || TREE_CODE (decl) == TYPE_DECL);
1343
1344 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1345 of a template class, we want to check if DECL is a specialization
1346 if this. */
1347 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1348 && DECL_TEMPLATE_INFO (friend_decl)
1349 && !DECL_USE_TEMPLATE (friend_decl))
1350 {
1351 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1352 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1353 need_template = false;
1354 }
1355 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1356 && !PRIMARY_TEMPLATE_P (friend_decl))
1357 need_template = false;
1358
1359 /* There is nothing to do if this is not a template friend. */
1360 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1361 return false;
1362
1363 if (is_specialization_of (decl, friend_decl))
1364 return true;
1365
1366 /* [temp.friend/6]
1367 A member of a class template may be declared to be a friend of a
1368 non-template class. In this case, the corresponding member of
1369 every specialization of the class template is a friend of the
1370 class granting friendship.
1371
1372 For example, given a template friend declaration
1373
1374 template <class T> friend void A<T>::f();
1375
1376 the member function below is considered a friend
1377
1378 template <> struct A<int> {
1379 void f();
1380 };
1381
1382 For this type of template friend, TEMPLATE_DEPTH below will be
1383 nonzero. To determine if DECL is a friend of FRIEND, we first
1384 check if the enclosing class is a specialization of another. */
1385
1386 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1387 if (template_depth
1388 && DECL_CLASS_SCOPE_P (decl)
1389 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1390 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1391 {
1392 /* Next, we check the members themselves. In order to handle
1393 a few tricky cases, such as when FRIEND_DECL's are
1394
1395 template <class T> friend void A<T>::g(T t);
1396 template <class T> template <T t> friend void A<T>::h();
1397
1398 and DECL's are
1399
1400 void A<int>::g(int);
1401 template <int> void A<int>::h();
1402
1403 we need to figure out ARGS, the template arguments from
1404 the context of DECL. This is required for template substitution
1405 of `T' in the function parameter of `g' and template parameter
1406 of `h' in the above examples. Here ARGS corresponds to `int'. */
1407
1408 tree context = DECL_CONTEXT (decl);
1409 tree args = NULL_TREE;
1410 int current_depth = 0;
1411
1412 while (current_depth < template_depth)
1413 {
1414 if (CLASSTYPE_TEMPLATE_INFO (context))
1415 {
1416 if (current_depth == 0)
1417 args = TYPE_TI_ARGS (context);
1418 else
1419 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1420 current_depth++;
1421 }
1422 context = TYPE_CONTEXT (context);
1423 }
1424
1425 if (TREE_CODE (decl) == FUNCTION_DECL)
1426 {
1427 bool is_template;
1428 tree friend_type;
1429 tree decl_type;
1430 tree friend_args_type;
1431 tree decl_args_type;
1432
1433 /* Make sure that both DECL and FRIEND_DECL are templates or
1434 non-templates. */
1435 is_template = DECL_TEMPLATE_INFO (decl)
1436 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1437 if (need_template ^ is_template)
1438 return false;
1439 else if (is_template)
1440 {
1441 /* If both are templates, check template parameter list. */
1442 tree friend_parms
1443 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1444 args, tf_none);
1445 if (!comp_template_parms
1446 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1447 friend_parms))
1448 return false;
1449
1450 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1451 }
1452 else
1453 decl_type = TREE_TYPE (decl);
1454
1455 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1456 tf_none, NULL_TREE);
1457 if (friend_type == error_mark_node)
1458 return false;
1459
1460 /* Check if return types match. */
1461 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1462 return false;
1463
1464 /* Check if function parameter types match, ignoring the
1465 `this' parameter. */
1466 friend_args_type = TYPE_ARG_TYPES (friend_type);
1467 decl_args_type = TYPE_ARG_TYPES (decl_type);
1468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1469 friend_args_type = TREE_CHAIN (friend_args_type);
1470 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1471 decl_args_type = TREE_CHAIN (decl_args_type);
1472
1473 return compparms (decl_args_type, friend_args_type);
1474 }
1475 else
1476 {
1477 /* DECL is a TYPE_DECL */
1478 bool is_template;
1479 tree decl_type = TREE_TYPE (decl);
1480
1481 /* Make sure that both DECL and FRIEND_DECL are templates or
1482 non-templates. */
1483 is_template
1484 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1485 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1486
1487 if (need_template ^ is_template)
1488 return false;
1489 else if (is_template)
1490 {
1491 tree friend_parms;
1492 /* If both are templates, check the name of the two
1493 TEMPLATE_DECL's first because is_friend didn't. */
1494 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1495 != DECL_NAME (friend_decl))
1496 return false;
1497
1498 /* Now check template parameter list. */
1499 friend_parms
1500 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1501 args, tf_none);
1502 return comp_template_parms
1503 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1504 friend_parms);
1505 }
1506 else
1507 return (DECL_NAME (decl)
1508 == DECL_NAME (friend_decl));
1509 }
1510 }
1511 return false;
1512 }
1513
1514 /* Register the specialization SPEC as a specialization of TMPL with
1515 the indicated ARGS. IS_FRIEND indicates whether the specialization
1516 is actually just a friend declaration. ATTRLIST is the list of
1517 attributes that the specialization is declared with or NULL when
1518 it isn't. Returns SPEC, or an equivalent prior declaration, if
1519 available.
1520
1521 We also store instantiations of field packs in the hash table, even
1522 though they are not themselves templates, to make lookup easier. */
1523
1524 static tree
1525 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1526 hashval_t hash)
1527 {
1528 tree fn;
1529 spec_entry **slot = NULL;
1530 spec_entry elt;
1531
1532 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1533 || (TREE_CODE (tmpl) == FIELD_DECL
1534 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1535
1536 if (TREE_CODE (spec) == FUNCTION_DECL
1537 && uses_template_parms (DECL_TI_ARGS (spec)))
1538 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1539 register it; we want the corresponding TEMPLATE_DECL instead.
1540 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1541 the more obvious `uses_template_parms (spec)' to avoid problems
1542 with default function arguments. In particular, given
1543 something like this:
1544
1545 template <class T> void f(T t1, T t = T())
1546
1547 the default argument expression is not substituted for in an
1548 instantiation unless and until it is actually needed. */
1549 return spec;
1550
1551 if (optimize_specialization_lookup_p (tmpl))
1552 /* We don't put these specializations in the hash table, but we might
1553 want to give an error about a mismatch. */
1554 fn = retrieve_specialization (tmpl, args, 0);
1555 else
1556 {
1557 elt.tmpl = tmpl;
1558 elt.args = args;
1559 elt.spec = spec;
1560
1561 if (hash == 0)
1562 hash = spec_hasher::hash (&elt);
1563
1564 slot =
1565 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1566 if (*slot)
1567 fn = ((spec_entry *) *slot)->spec;
1568 else
1569 fn = NULL_TREE;
1570 }
1571
1572 /* We can sometimes try to re-register a specialization that we've
1573 already got. In particular, regenerate_decl_from_template calls
1574 duplicate_decls which will update the specialization list. But,
1575 we'll still get called again here anyhow. It's more convenient
1576 to simply allow this than to try to prevent it. */
1577 if (fn == spec)
1578 return spec;
1579 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1580 {
1581 if (DECL_TEMPLATE_INSTANTIATION (fn))
1582 {
1583 if (DECL_ODR_USED (fn)
1584 || DECL_EXPLICIT_INSTANTIATION (fn))
1585 {
1586 error ("specialization of %qD after instantiation",
1587 fn);
1588 return error_mark_node;
1589 }
1590 else
1591 {
1592 tree clone;
1593 /* This situation should occur only if the first
1594 specialization is an implicit instantiation, the
1595 second is an explicit specialization, and the
1596 implicit instantiation has not yet been used. That
1597 situation can occur if we have implicitly
1598 instantiated a member function and then specialized
1599 it later.
1600
1601 We can also wind up here if a friend declaration that
1602 looked like an instantiation turns out to be a
1603 specialization:
1604
1605 template <class T> void foo(T);
1606 class S { friend void foo<>(int) };
1607 template <> void foo(int);
1608
1609 We transform the existing DECL in place so that any
1610 pointers to it become pointers to the updated
1611 declaration.
1612
1613 If there was a definition for the template, but not
1614 for the specialization, we want this to look as if
1615 there were no definition, and vice versa. */
1616 DECL_INITIAL (fn) = NULL_TREE;
1617 duplicate_decls (spec, fn, is_friend);
1618 /* The call to duplicate_decls will have applied
1619 [temp.expl.spec]:
1620
1621 An explicit specialization of a function template
1622 is inline only if it is explicitly declared to be,
1623 and independently of whether its function template
1624 is.
1625
1626 to the primary function; now copy the inline bits to
1627 the various clones. */
1628 FOR_EACH_CLONE (clone, fn)
1629 {
1630 DECL_DECLARED_INLINE_P (clone)
1631 = DECL_DECLARED_INLINE_P (fn);
1632 DECL_SOURCE_LOCATION (clone)
1633 = DECL_SOURCE_LOCATION (fn);
1634 DECL_DELETED_FN (clone)
1635 = DECL_DELETED_FN (fn);
1636 }
1637 check_specialization_namespace (tmpl);
1638
1639 return fn;
1640 }
1641 }
1642 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1643 {
1644 tree dd = duplicate_decls (spec, fn, is_friend);
1645 if (dd == error_mark_node)
1646 /* We've already complained in duplicate_decls. */
1647 return error_mark_node;
1648
1649 if (dd == NULL_TREE && DECL_INITIAL (spec))
1650 /* Dup decl failed, but this is a new definition. Set the
1651 line number so any errors match this new
1652 definition. */
1653 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1654
1655 return fn;
1656 }
1657 }
1658 else if (fn)
1659 return duplicate_decls (spec, fn, is_friend);
1660
1661 /* A specialization must be declared in the same namespace as the
1662 template it is specializing. */
1663 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1664 && !check_specialization_namespace (tmpl))
1665 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1666
1667 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1668 {
1669 spec_entry *entry = ggc_alloc<spec_entry> ();
1670 gcc_assert (tmpl && args && spec);
1671 *entry = elt;
1672 *slot = entry;
1673 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1674 && PRIMARY_TEMPLATE_P (tmpl)
1675 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1676 || variable_template_p (tmpl))
1677 /* If TMPL is a forward declaration of a template function, keep a list
1678 of all specializations in case we need to reassign them to a friend
1679 template later in tsubst_friend_function.
1680
1681 Also keep a list of all variable template instantiations so that
1682 process_partial_specialization can check whether a later partial
1683 specialization would have used it. */
1684 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1685 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1686 }
1687
1688 return spec;
1689 }
1690
1691 /* Returns true iff two spec_entry nodes are equivalent. */
1692
1693 int comparing_specializations;
1694
1695 bool
1696 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1697 {
1698 int equal;
1699
1700 ++comparing_specializations;
1701 equal = (e1->tmpl == e2->tmpl
1702 && comp_template_args (e1->args, e2->args));
1703 if (equal && flag_concepts
1704 /* tmpl could be a FIELD_DECL for a capture pack. */
1705 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1706 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1707 && uses_template_parms (e1->args))
1708 {
1709 /* Partial specializations of a variable template can be distinguished by
1710 constraints. */
1711 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1712 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1713 equal = equivalent_constraints (c1, c2);
1714 }
1715 --comparing_specializations;
1716
1717 return equal;
1718 }
1719
1720 /* Returns a hash for a template TMPL and template arguments ARGS. */
1721
1722 static hashval_t
1723 hash_tmpl_and_args (tree tmpl, tree args)
1724 {
1725 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1726 return iterative_hash_template_arg (args, val);
1727 }
1728
1729 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1730 ignoring SPEC. */
1731
1732 hashval_t
1733 spec_hasher::hash (spec_entry *e)
1734 {
1735 return hash_tmpl_and_args (e->tmpl, e->args);
1736 }
1737
1738 /* Recursively calculate a hash value for a template argument ARG, for use
1739 in the hash tables of template specializations. */
1740
1741 hashval_t
1742 iterative_hash_template_arg (tree arg, hashval_t val)
1743 {
1744 unsigned HOST_WIDE_INT i;
1745 enum tree_code code;
1746 char tclass;
1747
1748 if (arg == NULL_TREE)
1749 return iterative_hash_object (arg, val);
1750
1751 if (!TYPE_P (arg))
1752 STRIP_NOPS (arg);
1753
1754 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1755 gcc_unreachable ();
1756
1757 code = TREE_CODE (arg);
1758 tclass = TREE_CODE_CLASS (code);
1759
1760 val = iterative_hash_object (code, val);
1761
1762 switch (code)
1763 {
1764 case ERROR_MARK:
1765 return val;
1766
1767 case IDENTIFIER_NODE:
1768 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1769
1770 case TREE_VEC:
1771 {
1772 int i, len = TREE_VEC_LENGTH (arg);
1773 for (i = 0; i < len; ++i)
1774 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1775 return val;
1776 }
1777
1778 case TYPE_PACK_EXPANSION:
1779 case EXPR_PACK_EXPANSION:
1780 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1781 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1782
1783 case TYPE_ARGUMENT_PACK:
1784 case NONTYPE_ARGUMENT_PACK:
1785 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1786
1787 case TREE_LIST:
1788 for (; arg; arg = TREE_CHAIN (arg))
1789 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1790 return val;
1791
1792 case OVERLOAD:
1793 for (lkp_iterator iter (arg); iter; ++iter)
1794 val = iterative_hash_template_arg (*iter, val);
1795 return val;
1796
1797 case CONSTRUCTOR:
1798 {
1799 tree field, value;
1800 iterative_hash_template_arg (TREE_TYPE (arg), val);
1801 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1802 {
1803 val = iterative_hash_template_arg (field, val);
1804 val = iterative_hash_template_arg (value, val);
1805 }
1806 return val;
1807 }
1808
1809 case PARM_DECL:
1810 if (!DECL_ARTIFICIAL (arg))
1811 {
1812 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1813 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1814 }
1815 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1816
1817 case TARGET_EXPR:
1818 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1819
1820 case PTRMEM_CST:
1821 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1822 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1823
1824 case TEMPLATE_PARM_INDEX:
1825 val = iterative_hash_template_arg
1826 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1827 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1828 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1829
1830 case TRAIT_EXPR:
1831 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1832 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1833 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1834
1835 case BASELINK:
1836 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1837 val);
1838 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1839 val);
1840
1841 case MODOP_EXPR:
1842 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1843 code = TREE_CODE (TREE_OPERAND (arg, 1));
1844 val = iterative_hash_object (code, val);
1845 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1846
1847 case LAMBDA_EXPR:
1848 /* [temp.over.link] Two lambda-expressions are never considered
1849 equivalent.
1850
1851 So just hash the closure type. */
1852 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1853
1854 case CAST_EXPR:
1855 case IMPLICIT_CONV_EXPR:
1856 case STATIC_CAST_EXPR:
1857 case REINTERPRET_CAST_EXPR:
1858 case CONST_CAST_EXPR:
1859 case DYNAMIC_CAST_EXPR:
1860 case NEW_EXPR:
1861 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1862 /* Now hash operands as usual. */
1863 break;
1864
1865 case CALL_EXPR:
1866 {
1867 tree fn = CALL_EXPR_FN (arg);
1868 if (tree name = dependent_name (fn))
1869 {
1870 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1871 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1872 fn = name;
1873 }
1874 val = iterative_hash_template_arg (fn, val);
1875 call_expr_arg_iterator ai;
1876 for (tree x = first_call_expr_arg (arg, &ai); x;
1877 x = next_call_expr_arg (&ai))
1878 val = iterative_hash_template_arg (x, val);
1879 return val;
1880 }
1881
1882 default:
1883 break;
1884 }
1885
1886 switch (tclass)
1887 {
1888 case tcc_type:
1889 if (alias_template_specialization_p (arg))
1890 {
1891 // We want an alias specialization that survived strip_typedefs
1892 // to hash differently from its TYPE_CANONICAL, to avoid hash
1893 // collisions that compare as different in template_args_equal.
1894 // These could be dependent specializations that strip_typedefs
1895 // left alone, or untouched specializations because
1896 // coerce_template_parms returns the unconverted template
1897 // arguments if it sees incomplete argument packs.
1898 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1899 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1900 }
1901 if (TYPE_CANONICAL (arg))
1902 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1903 val);
1904 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1905 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1906 /* Otherwise just compare the types during lookup. */
1907 return val;
1908
1909 case tcc_declaration:
1910 case tcc_constant:
1911 return iterative_hash_expr (arg, val);
1912
1913 default:
1914 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1915 {
1916 unsigned n = cp_tree_operand_length (arg);
1917 for (i = 0; i < n; ++i)
1918 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1919 return val;
1920 }
1921 }
1922 gcc_unreachable ();
1923 return 0;
1924 }
1925
1926 /* Unregister the specialization SPEC as a specialization of TMPL.
1927 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1928 if the SPEC was listed as a specialization of TMPL.
1929
1930 Note that SPEC has been ggc_freed, so we can't look inside it. */
1931
1932 bool
1933 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1934 {
1935 spec_entry *entry;
1936 spec_entry elt;
1937
1938 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1939 elt.args = TI_ARGS (tinfo);
1940 elt.spec = NULL_TREE;
1941
1942 entry = decl_specializations->find (&elt);
1943 if (entry != NULL)
1944 {
1945 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1946 gcc_assert (new_spec != NULL_TREE);
1947 entry->spec = new_spec;
1948 return 1;
1949 }
1950
1951 return 0;
1952 }
1953
1954 /* Like register_specialization, but for local declarations. We are
1955 registering SPEC, an instantiation of TMPL. */
1956
1957 void
1958 register_local_specialization (tree spec, tree tmpl)
1959 {
1960 gcc_assert (tmpl != spec);
1961 local_specializations->put (tmpl, spec);
1962 }
1963
1964 /* TYPE is a class type. Returns true if TYPE is an explicitly
1965 specialized class. */
1966
1967 bool
1968 explicit_class_specialization_p (tree type)
1969 {
1970 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1971 return false;
1972 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1973 }
1974
1975 /* Print the list of functions at FNS, going through all the overloads
1976 for each element of the list. Alternatively, FNS cannot be a
1977 TREE_LIST, in which case it will be printed together with all the
1978 overloads.
1979
1980 MORE and *STR should respectively be FALSE and NULL when the function
1981 is called from the outside. They are used internally on recursive
1982 calls. print_candidates manages the two parameters and leaves NULL
1983 in *STR when it ends. */
1984
1985 static void
1986 print_candidates_1 (tree fns, char **str, bool more = false)
1987 {
1988 if (TREE_CODE (fns) == TREE_LIST)
1989 for (; fns; fns = TREE_CHAIN (fns))
1990 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1991 else
1992 for (lkp_iterator iter (fns); iter;)
1993 {
1994 tree cand = *iter;
1995 ++iter;
1996
1997 const char *pfx = *str;
1998 if (!pfx)
1999 {
2000 if (more || iter)
2001 pfx = _("candidates are:");
2002 else
2003 pfx = _("candidate is:");
2004 *str = get_spaces (pfx);
2005 }
2006 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2007 }
2008 }
2009
2010 /* Print the list of candidate FNS in an error message. FNS can also
2011 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2012
2013 void
2014 print_candidates (tree fns)
2015 {
2016 char *str = NULL;
2017 print_candidates_1 (fns, &str);
2018 free (str);
2019 }
2020
2021 /* Get a (possibly) constrained template declaration for the
2022 purpose of ordering candidates. */
2023 static tree
2024 get_template_for_ordering (tree list)
2025 {
2026 gcc_assert (TREE_CODE (list) == TREE_LIST);
2027 tree f = TREE_VALUE (list);
2028 if (tree ti = DECL_TEMPLATE_INFO (f))
2029 return TI_TEMPLATE (ti);
2030 return f;
2031 }
2032
2033 /* Among candidates having the same signature, return the
2034 most constrained or NULL_TREE if there is no best candidate.
2035 If the signatures of candidates vary (e.g., template
2036 specialization vs. member function), then there can be no
2037 most constrained.
2038
2039 Note that we don't compare constraints on the functions
2040 themselves, but rather those of their templates. */
2041 static tree
2042 most_constrained_function (tree candidates)
2043 {
2044 // Try to find the best candidate in a first pass.
2045 tree champ = candidates;
2046 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2047 {
2048 int winner = more_constrained (get_template_for_ordering (champ),
2049 get_template_for_ordering (c));
2050 if (winner == -1)
2051 champ = c; // The candidate is more constrained
2052 else if (winner == 0)
2053 return NULL_TREE; // Neither is more constrained
2054 }
2055
2056 // Verify that the champ is better than previous candidates.
2057 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2058 if (!more_constrained (get_template_for_ordering (champ),
2059 get_template_for_ordering (c)))
2060 return NULL_TREE;
2061 }
2062
2063 return champ;
2064 }
2065
2066
2067 /* Returns the template (one of the functions given by TEMPLATE_ID)
2068 which can be specialized to match the indicated DECL with the
2069 explicit template args given in TEMPLATE_ID. The DECL may be
2070 NULL_TREE if none is available. In that case, the functions in
2071 TEMPLATE_ID are non-members.
2072
2073 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2074 specialization of a member template.
2075
2076 The TEMPLATE_COUNT is the number of references to qualifying
2077 template classes that appeared in the name of the function. See
2078 check_explicit_specialization for a more accurate description.
2079
2080 TSK indicates what kind of template declaration (if any) is being
2081 declared. TSK_TEMPLATE indicates that the declaration given by
2082 DECL, though a FUNCTION_DECL, has template parameters, and is
2083 therefore a template function.
2084
2085 The template args (those explicitly specified and those deduced)
2086 are output in a newly created vector *TARGS_OUT.
2087
2088 If it is impossible to determine the result, an error message is
2089 issued. The error_mark_node is returned to indicate failure. */
2090
2091 static tree
2092 determine_specialization (tree template_id,
2093 tree decl,
2094 tree* targs_out,
2095 int need_member_template,
2096 int template_count,
2097 tmpl_spec_kind tsk)
2098 {
2099 tree fns;
2100 tree targs;
2101 tree explicit_targs;
2102 tree candidates = NULL_TREE;
2103
2104 /* A TREE_LIST of templates of which DECL may be a specialization.
2105 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2106 corresponding TREE_PURPOSE is the set of template arguments that,
2107 when used to instantiate the template, would produce a function
2108 with the signature of DECL. */
2109 tree templates = NULL_TREE;
2110 int header_count;
2111 cp_binding_level *b;
2112
2113 *targs_out = NULL_TREE;
2114
2115 if (template_id == error_mark_node || decl == error_mark_node)
2116 return error_mark_node;
2117
2118 /* We shouldn't be specializing a member template of an
2119 unspecialized class template; we already gave an error in
2120 check_specialization_scope, now avoid crashing. */
2121 if (!VAR_P (decl)
2122 && template_count && DECL_CLASS_SCOPE_P (decl)
2123 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2124 {
2125 gcc_assert (errorcount);
2126 return error_mark_node;
2127 }
2128
2129 fns = TREE_OPERAND (template_id, 0);
2130 explicit_targs = TREE_OPERAND (template_id, 1);
2131
2132 if (fns == error_mark_node)
2133 return error_mark_node;
2134
2135 /* Check for baselinks. */
2136 if (BASELINK_P (fns))
2137 fns = BASELINK_FUNCTIONS (fns);
2138
2139 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2140 {
2141 error ("%qD is not a function template", fns);
2142 return error_mark_node;
2143 }
2144 else if (VAR_P (decl) && !variable_template_p (fns))
2145 {
2146 error ("%qD is not a variable template", fns);
2147 return error_mark_node;
2148 }
2149
2150 /* Count the number of template headers specified for this
2151 specialization. */
2152 header_count = 0;
2153 for (b = current_binding_level;
2154 b->kind == sk_template_parms;
2155 b = b->level_chain)
2156 ++header_count;
2157
2158 tree orig_fns = fns;
2159
2160 if (variable_template_p (fns))
2161 {
2162 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2163 targs = coerce_template_parms (parms, explicit_targs, fns,
2164 tf_warning_or_error,
2165 /*req_all*/true, /*use_defarg*/true);
2166 if (targs != error_mark_node)
2167 templates = tree_cons (targs, fns, templates);
2168 }
2169 else for (lkp_iterator iter (fns); iter; ++iter)
2170 {
2171 tree fn = *iter;
2172
2173 if (TREE_CODE (fn) == TEMPLATE_DECL)
2174 {
2175 tree decl_arg_types;
2176 tree fn_arg_types;
2177 tree insttype;
2178
2179 /* In case of explicit specialization, we need to check if
2180 the number of template headers appearing in the specialization
2181 is correct. This is usually done in check_explicit_specialization,
2182 but the check done there cannot be exhaustive when specializing
2183 member functions. Consider the following code:
2184
2185 template <> void A<int>::f(int);
2186 template <> template <> void A<int>::f(int);
2187
2188 Assuming that A<int> is not itself an explicit specialization
2189 already, the first line specializes "f" which is a non-template
2190 member function, whilst the second line specializes "f" which
2191 is a template member function. So both lines are syntactically
2192 correct, and check_explicit_specialization does not reject
2193 them.
2194
2195 Here, we can do better, as we are matching the specialization
2196 against the declarations. We count the number of template
2197 headers, and we check if they match TEMPLATE_COUNT + 1
2198 (TEMPLATE_COUNT is the number of qualifying template classes,
2199 plus there must be another header for the member template
2200 itself).
2201
2202 Notice that if header_count is zero, this is not a
2203 specialization but rather a template instantiation, so there
2204 is no check we can perform here. */
2205 if (header_count && header_count != template_count + 1)
2206 continue;
2207
2208 /* Check that the number of template arguments at the
2209 innermost level for DECL is the same as for FN. */
2210 if (current_binding_level->kind == sk_template_parms
2211 && !current_binding_level->explicit_spec_p
2212 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2213 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2214 (current_template_parms))))
2215 continue;
2216
2217 /* DECL might be a specialization of FN. */
2218 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2219 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2220
2221 /* For a non-static member function, we need to make sure
2222 that the const qualification is the same. Since
2223 get_bindings does not try to merge the "this" parameter,
2224 we must do the comparison explicitly. */
2225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2226 {
2227 if (!same_type_p (TREE_VALUE (fn_arg_types),
2228 TREE_VALUE (decl_arg_types)))
2229 continue;
2230
2231 /* And the ref-qualification. */
2232 if (type_memfn_rqual (TREE_TYPE (decl))
2233 != type_memfn_rqual (TREE_TYPE (fn)))
2234 continue;
2235 }
2236
2237 /* Skip the "this" parameter and, for constructors of
2238 classes with virtual bases, the VTT parameter. A
2239 full specialization of a constructor will have a VTT
2240 parameter, but a template never will. */
2241 decl_arg_types
2242 = skip_artificial_parms_for (decl, decl_arg_types);
2243 fn_arg_types
2244 = skip_artificial_parms_for (fn, fn_arg_types);
2245
2246 /* Function templates cannot be specializations; there are
2247 no partial specializations of functions. Therefore, if
2248 the type of DECL does not match FN, there is no
2249 match.
2250
2251 Note that it should never be the case that we have both
2252 candidates added here, and for regular member functions
2253 below. */
2254 if (tsk == tsk_template)
2255 {
2256 if (compparms (fn_arg_types, decl_arg_types))
2257 candidates = tree_cons (NULL_TREE, fn, candidates);
2258 continue;
2259 }
2260
2261 /* See whether this function might be a specialization of this
2262 template. Suppress access control because we might be trying
2263 to make this specialization a friend, and we have already done
2264 access control for the declaration of the specialization. */
2265 push_deferring_access_checks (dk_no_check);
2266 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2267 pop_deferring_access_checks ();
2268
2269 if (!targs)
2270 /* We cannot deduce template arguments that when used to
2271 specialize TMPL will produce DECL. */
2272 continue;
2273
2274 if (uses_template_parms (targs))
2275 /* We deduced something involving 'auto', which isn't a valid
2276 template argument. */
2277 continue;
2278
2279 /* Remove, from the set of candidates, all those functions
2280 whose constraints are not satisfied. */
2281 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2282 continue;
2283
2284 // Then, try to form the new function type.
2285 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2286 if (insttype == error_mark_node)
2287 continue;
2288 fn_arg_types
2289 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2290 if (!compparms (fn_arg_types, decl_arg_types))
2291 continue;
2292
2293 /* Save this template, and the arguments deduced. */
2294 templates = tree_cons (targs, fn, templates);
2295 }
2296 else if (need_member_template)
2297 /* FN is an ordinary member function, and we need a
2298 specialization of a member template. */
2299 ;
2300 else if (TREE_CODE (fn) != FUNCTION_DECL)
2301 /* We can get IDENTIFIER_NODEs here in certain erroneous
2302 cases. */
2303 ;
2304 else if (!DECL_FUNCTION_MEMBER_P (fn))
2305 /* This is just an ordinary non-member function. Nothing can
2306 be a specialization of that. */
2307 ;
2308 else if (DECL_ARTIFICIAL (fn))
2309 /* Cannot specialize functions that are created implicitly. */
2310 ;
2311 else
2312 {
2313 tree decl_arg_types;
2314
2315 /* This is an ordinary member function. However, since
2316 we're here, we can assume its enclosing class is a
2317 template class. For example,
2318
2319 template <typename T> struct S { void f(); };
2320 template <> void S<int>::f() {}
2321
2322 Here, S<int>::f is a non-template, but S<int> is a
2323 template class. If FN has the same type as DECL, we
2324 might be in business. */
2325
2326 if (!DECL_TEMPLATE_INFO (fn))
2327 /* Its enclosing class is an explicit specialization
2328 of a template class. This is not a candidate. */
2329 continue;
2330
2331 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2332 TREE_TYPE (TREE_TYPE (fn))))
2333 /* The return types differ. */
2334 continue;
2335
2336 /* Adjust the type of DECL in case FN is a static member. */
2337 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2338 if (DECL_STATIC_FUNCTION_P (fn)
2339 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2340 decl_arg_types = TREE_CHAIN (decl_arg_types);
2341
2342 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2343 decl_arg_types))
2344 continue;
2345
2346 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2347 && (type_memfn_rqual (TREE_TYPE (decl))
2348 != type_memfn_rqual (TREE_TYPE (fn))))
2349 continue;
2350
2351 // If the deduced arguments do not satisfy the constraints,
2352 // this is not a candidate.
2353 if (flag_concepts && !constraints_satisfied_p (fn))
2354 continue;
2355
2356 // Add the candidate.
2357 candidates = tree_cons (NULL_TREE, fn, candidates);
2358 }
2359 }
2360
2361 if (templates && TREE_CHAIN (templates))
2362 {
2363 /* We have:
2364
2365 [temp.expl.spec]
2366
2367 It is possible for a specialization with a given function
2368 signature to be instantiated from more than one function
2369 template. In such cases, explicit specification of the
2370 template arguments must be used to uniquely identify the
2371 function template specialization being specialized.
2372
2373 Note that here, there's no suggestion that we're supposed to
2374 determine which of the candidate templates is most
2375 specialized. However, we, also have:
2376
2377 [temp.func.order]
2378
2379 Partial ordering of overloaded function template
2380 declarations is used in the following contexts to select
2381 the function template to which a function template
2382 specialization refers:
2383
2384 -- when an explicit specialization refers to a function
2385 template.
2386
2387 So, we do use the partial ordering rules, at least for now.
2388 This extension can only serve to make invalid programs valid,
2389 so it's safe. And, there is strong anecdotal evidence that
2390 the committee intended the partial ordering rules to apply;
2391 the EDG front end has that behavior, and John Spicer claims
2392 that the committee simply forgot to delete the wording in
2393 [temp.expl.spec]. */
2394 tree tmpl = most_specialized_instantiation (templates);
2395 if (tmpl != error_mark_node)
2396 {
2397 templates = tmpl;
2398 TREE_CHAIN (templates) = NULL_TREE;
2399 }
2400 }
2401
2402 // Concepts allows multiple declarations of member functions
2403 // with the same signature. Like above, we need to rely on
2404 // on the partial ordering of those candidates to determine which
2405 // is the best.
2406 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2407 {
2408 if (tree cand = most_constrained_function (candidates))
2409 {
2410 candidates = cand;
2411 TREE_CHAIN (cand) = NULL_TREE;
2412 }
2413 }
2414
2415 if (templates == NULL_TREE && candidates == NULL_TREE)
2416 {
2417 error ("template-id %qD for %q+D does not match any template "
2418 "declaration", template_id, decl);
2419 if (header_count && header_count != template_count + 1)
2420 inform (input_location, "saw %d %<template<>%>, need %d for "
2421 "specializing a member function template",
2422 header_count, template_count + 1);
2423 else
2424 print_candidates (orig_fns);
2425 return error_mark_node;
2426 }
2427 else if ((templates && TREE_CHAIN (templates))
2428 || (candidates && TREE_CHAIN (candidates))
2429 || (templates && candidates))
2430 {
2431 error ("ambiguous template specialization %qD for %q+D",
2432 template_id, decl);
2433 candidates = chainon (candidates, templates);
2434 print_candidates (candidates);
2435 return error_mark_node;
2436 }
2437
2438 /* We have one, and exactly one, match. */
2439 if (candidates)
2440 {
2441 tree fn = TREE_VALUE (candidates);
2442 *targs_out = copy_node (DECL_TI_ARGS (fn));
2443
2444 // Propagate the candidate's constraints to the declaration.
2445 set_constraints (decl, get_constraints (fn));
2446
2447 /* DECL is a re-declaration or partial instantiation of a template
2448 function. */
2449 if (TREE_CODE (fn) == TEMPLATE_DECL)
2450 return fn;
2451 /* It was a specialization of an ordinary member function in a
2452 template class. */
2453 return DECL_TI_TEMPLATE (fn);
2454 }
2455
2456 /* It was a specialization of a template. */
2457 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2458 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2459 {
2460 *targs_out = copy_node (targs);
2461 SET_TMPL_ARGS_LEVEL (*targs_out,
2462 TMPL_ARGS_DEPTH (*targs_out),
2463 TREE_PURPOSE (templates));
2464 }
2465 else
2466 *targs_out = TREE_PURPOSE (templates);
2467 return TREE_VALUE (templates);
2468 }
2469
2470 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2471 but with the default argument values filled in from those in the
2472 TMPL_TYPES. */
2473
2474 static tree
2475 copy_default_args_to_explicit_spec_1 (tree spec_types,
2476 tree tmpl_types)
2477 {
2478 tree new_spec_types;
2479
2480 if (!spec_types)
2481 return NULL_TREE;
2482
2483 if (spec_types == void_list_node)
2484 return void_list_node;
2485
2486 /* Substitute into the rest of the list. */
2487 new_spec_types =
2488 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2489 TREE_CHAIN (tmpl_types));
2490
2491 /* Add the default argument for this parameter. */
2492 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2493 TREE_VALUE (spec_types),
2494 new_spec_types);
2495 }
2496
2497 /* DECL is an explicit specialization. Replicate default arguments
2498 from the template it specializes. (That way, code like:
2499
2500 template <class T> void f(T = 3);
2501 template <> void f(double);
2502 void g () { f (); }
2503
2504 works, as required.) An alternative approach would be to look up
2505 the correct default arguments at the call-site, but this approach
2506 is consistent with how implicit instantiations are handled. */
2507
2508 static void
2509 copy_default_args_to_explicit_spec (tree decl)
2510 {
2511 tree tmpl;
2512 tree spec_types;
2513 tree tmpl_types;
2514 tree new_spec_types;
2515 tree old_type;
2516 tree new_type;
2517 tree t;
2518 tree object_type = NULL_TREE;
2519 tree in_charge = NULL_TREE;
2520 tree vtt = NULL_TREE;
2521
2522 /* See if there's anything we need to do. */
2523 tmpl = DECL_TI_TEMPLATE (decl);
2524 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2525 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2526 if (TREE_PURPOSE (t))
2527 break;
2528 if (!t)
2529 return;
2530
2531 old_type = TREE_TYPE (decl);
2532 spec_types = TYPE_ARG_TYPES (old_type);
2533
2534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2535 {
2536 /* Remove the this pointer, but remember the object's type for
2537 CV quals. */
2538 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2539 spec_types = TREE_CHAIN (spec_types);
2540 tmpl_types = TREE_CHAIN (tmpl_types);
2541
2542 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2543 {
2544 /* DECL may contain more parameters than TMPL due to the extra
2545 in-charge parameter in constructors and destructors. */
2546 in_charge = spec_types;
2547 spec_types = TREE_CHAIN (spec_types);
2548 }
2549 if (DECL_HAS_VTT_PARM_P (decl))
2550 {
2551 vtt = spec_types;
2552 spec_types = TREE_CHAIN (spec_types);
2553 }
2554 }
2555
2556 /* Compute the merged default arguments. */
2557 new_spec_types =
2558 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2559
2560 /* Compute the new FUNCTION_TYPE. */
2561 if (object_type)
2562 {
2563 if (vtt)
2564 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2565 TREE_VALUE (vtt),
2566 new_spec_types);
2567
2568 if (in_charge)
2569 /* Put the in-charge parameter back. */
2570 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2571 TREE_VALUE (in_charge),
2572 new_spec_types);
2573
2574 new_type = build_method_type_directly (object_type,
2575 TREE_TYPE (old_type),
2576 new_spec_types);
2577 }
2578 else
2579 new_type = build_function_type (TREE_TYPE (old_type),
2580 new_spec_types);
2581 new_type = cp_build_type_attribute_variant (new_type,
2582 TYPE_ATTRIBUTES (old_type));
2583 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2584
2585 TREE_TYPE (decl) = new_type;
2586 }
2587
2588 /* Return the number of template headers we expect to see for a definition
2589 or specialization of CTYPE or one of its non-template members. */
2590
2591 int
2592 num_template_headers_for_class (tree ctype)
2593 {
2594 int num_templates = 0;
2595
2596 while (ctype && CLASS_TYPE_P (ctype))
2597 {
2598 /* You're supposed to have one `template <...>' for every
2599 template class, but you don't need one for a full
2600 specialization. For example:
2601
2602 template <class T> struct S{};
2603 template <> struct S<int> { void f(); };
2604 void S<int>::f () {}
2605
2606 is correct; there shouldn't be a `template <>' for the
2607 definition of `S<int>::f'. */
2608 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2609 /* If CTYPE does not have template information of any
2610 kind, then it is not a template, nor is it nested
2611 within a template. */
2612 break;
2613 if (explicit_class_specialization_p (ctype))
2614 break;
2615 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2616 ++num_templates;
2617
2618 ctype = TYPE_CONTEXT (ctype);
2619 }
2620
2621 return num_templates;
2622 }
2623
2624 /* Do a simple sanity check on the template headers that precede the
2625 variable declaration DECL. */
2626
2627 void
2628 check_template_variable (tree decl)
2629 {
2630 tree ctx = CP_DECL_CONTEXT (decl);
2631 int wanted = num_template_headers_for_class (ctx);
2632 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2633 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2634 {
2635 if (cxx_dialect < cxx14)
2636 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2637 "variable templates only available with "
2638 "%<-std=c++14%> or %<-std=gnu++14%>");
2639
2640 // Namespace-scope variable templates should have a template header.
2641 ++wanted;
2642 }
2643 if (template_header_count > wanted)
2644 {
2645 auto_diagnostic_group d;
2646 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2647 "too many template headers for %qD "
2648 "(should be %d)",
2649 decl, wanted);
2650 if (warned && CLASS_TYPE_P (ctx)
2651 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2652 inform (DECL_SOURCE_LOCATION (decl),
2653 "members of an explicitly specialized class are defined "
2654 "without a template header");
2655 }
2656 }
2657
2658 /* An explicit specialization whose declarator-id or class-head-name is not
2659 qualified shall be declared in the nearest enclosing namespace of the
2660 template, or, if the namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set.
2662
2663 If the name declared in the explicit instantiation is an unqualified name,
2664 the explicit instantiation shall appear in the namespace where its template
2665 is declared or, if that namespace is inline (7.3.1), any namespace from its
2666 enclosing namespace set. */
2667
2668 void
2669 check_unqualified_spec_or_inst (tree t, location_t loc)
2670 {
2671 tree tmpl = most_general_template (t);
2672 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2673 && !is_nested_namespace (current_namespace,
2674 CP_DECL_CONTEXT (tmpl), true))
2675 {
2676 if (processing_specialization)
2677 permerror (loc, "explicit specialization of %qD outside its "
2678 "namespace must use a nested-name-specifier", tmpl);
2679 else if (processing_explicit_instantiation
2680 && cxx_dialect >= cxx11)
2681 /* This was allowed in C++98, so only pedwarn. */
2682 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2683 "outside its namespace must use a nested-name-"
2684 "specifier", tmpl);
2685 }
2686 }
2687
2688 /* Warn for a template specialization SPEC that is missing some of a set
2689 of function or type attributes that the template TEMPL is declared with.
2690 ATTRLIST is a list of additional attributes that SPEC should be taken
2691 to ultimately be declared with. */
2692
2693 static void
2694 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2695 {
2696 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2697 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2698
2699 /* Avoid warning if the difference between the primary and
2700 the specialization is not in one of the attributes below. */
2701 const char* const blacklist[] = {
2702 "alloc_align", "alloc_size", "assume_aligned", "format",
2703 "format_arg", "malloc", "nonnull", NULL
2704 };
2705
2706 /* Put together a list of the black listed attributes that the primary
2707 template is declared with that the specialization is not, in case
2708 it's not apparent from the most recent declaration of the primary. */
2709 pretty_printer str;
2710 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2711 blacklist, &str);
2712
2713 if (!nattrs)
2714 return;
2715
2716 auto_diagnostic_group d;
2717 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2718 "explicit specialization %q#D may be missing attributes",
2719 spec))
2720 inform (DECL_SOURCE_LOCATION (tmpl),
2721 nattrs > 1
2722 ? G_("missing primary template attributes %s")
2723 : G_("missing primary template attribute %s"),
2724 pp_formatted_text (&str));
2725 }
2726
2727 /* Check to see if the function just declared, as indicated in
2728 DECLARATOR, and in DECL, is a specialization of a function
2729 template. We may also discover that the declaration is an explicit
2730 instantiation at this point.
2731
2732 Returns DECL, or an equivalent declaration that should be used
2733 instead if all goes well. Issues an error message if something is
2734 amiss. Returns error_mark_node if the error is not easily
2735 recoverable.
2736
2737 FLAGS is a bitmask consisting of the following flags:
2738
2739 2: The function has a definition.
2740 4: The function is a friend.
2741
2742 The TEMPLATE_COUNT is the number of references to qualifying
2743 template classes that appeared in the name of the function. For
2744 example, in
2745
2746 template <class T> struct S { void f(); };
2747 void S<int>::f();
2748
2749 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2750 classes are not counted in the TEMPLATE_COUNT, so that in
2751
2752 template <class T> struct S {};
2753 template <> struct S<int> { void f(); }
2754 template <> void S<int>::f();
2755
2756 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2757 invalid; there should be no template <>.)
2758
2759 If the function is a specialization, it is marked as such via
2760 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2761 is set up correctly, and it is added to the list of specializations
2762 for that template. */
2763
2764 tree
2765 check_explicit_specialization (tree declarator,
2766 tree decl,
2767 int template_count,
2768 int flags,
2769 tree attrlist)
2770 {
2771 int have_def = flags & 2;
2772 int is_friend = flags & 4;
2773 bool is_concept = flags & 8;
2774 int specialization = 0;
2775 int explicit_instantiation = 0;
2776 int member_specialization = 0;
2777 tree ctype = DECL_CLASS_CONTEXT (decl);
2778 tree dname = DECL_NAME (decl);
2779 tmpl_spec_kind tsk;
2780
2781 if (is_friend)
2782 {
2783 if (!processing_specialization)
2784 tsk = tsk_none;
2785 else
2786 tsk = tsk_excessive_parms;
2787 }
2788 else
2789 tsk = current_tmpl_spec_kind (template_count);
2790
2791 switch (tsk)
2792 {
2793 case tsk_none:
2794 if (processing_specialization && !VAR_P (decl))
2795 {
2796 specialization = 1;
2797 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2798 }
2799 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2800 {
2801 if (is_friend)
2802 /* This could be something like:
2803
2804 template <class T> void f(T);
2805 class S { friend void f<>(int); } */
2806 specialization = 1;
2807 else
2808 {
2809 /* This case handles bogus declarations like template <>
2810 template <class T> void f<int>(); */
2811
2812 error ("template-id %qD in declaration of primary template",
2813 declarator);
2814 return decl;
2815 }
2816 }
2817 break;
2818
2819 case tsk_invalid_member_spec:
2820 /* The error has already been reported in
2821 check_specialization_scope. */
2822 return error_mark_node;
2823
2824 case tsk_invalid_expl_inst:
2825 error ("template parameter list used in explicit instantiation");
2826
2827 /* Fall through. */
2828
2829 case tsk_expl_inst:
2830 if (have_def)
2831 error ("definition provided for explicit instantiation");
2832
2833 explicit_instantiation = 1;
2834 break;
2835
2836 case tsk_excessive_parms:
2837 case tsk_insufficient_parms:
2838 if (tsk == tsk_excessive_parms)
2839 error ("too many template parameter lists in declaration of %qD",
2840 decl);
2841 else if (template_header_count)
2842 error("too few template parameter lists in declaration of %qD", decl);
2843 else
2844 error("explicit specialization of %qD must be introduced by "
2845 "%<template <>%>", decl);
2846
2847 /* Fall through. */
2848 case tsk_expl_spec:
2849 if (is_concept)
2850 error ("explicit specialization declared %<concept%>");
2851
2852 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2853 /* In cases like template<> constexpr bool v = true;
2854 We'll give an error in check_template_variable. */
2855 break;
2856
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 if (ctype)
2859 member_specialization = 1;
2860 else
2861 specialization = 1;
2862 break;
2863
2864 case tsk_template:
2865 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2866 {
2867 /* This case handles bogus declarations like template <>
2868 template <class T> void f<int>(); */
2869
2870 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2871 error ("template-id %qD in declaration of primary template",
2872 declarator);
2873 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2874 {
2875 /* Partial specialization of variable template. */
2876 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2877 specialization = 1;
2878 goto ok;
2879 }
2880 else if (cxx_dialect < cxx14)
2881 error ("non-type partial specialization %qD "
2882 "is not allowed", declarator);
2883 else
2884 error ("non-class, non-variable partial specialization %qD "
2885 "is not allowed", declarator);
2886 return decl;
2887 ok:;
2888 }
2889
2890 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2891 /* This is a specialization of a member template, without
2892 specialization the containing class. Something like:
2893
2894 template <class T> struct S {
2895 template <class U> void f (U);
2896 };
2897 template <> template <class U> void S<int>::f(U) {}
2898
2899 That's a specialization -- but of the entire template. */
2900 specialization = 1;
2901 break;
2902
2903 default:
2904 gcc_unreachable ();
2905 }
2906
2907 if ((specialization || member_specialization)
2908 /* This doesn't apply to variable templates. */
2909 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2910 {
2911 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2912 for (; t; t = TREE_CHAIN (t))
2913 if (TREE_PURPOSE (t))
2914 {
2915 permerror (input_location,
2916 "default argument specified in explicit specialization");
2917 break;
2918 }
2919 }
2920
2921 if (specialization || member_specialization || explicit_instantiation)
2922 {
2923 tree tmpl = NULL_TREE;
2924 tree targs = NULL_TREE;
2925 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2926
2927 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2928 if (!was_template_id)
2929 {
2930 tree fns;
2931
2932 gcc_assert (identifier_p (declarator));
2933 if (ctype)
2934 fns = dname;
2935 else
2936 {
2937 /* If there is no class context, the explicit instantiation
2938 must be at namespace scope. */
2939 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2940
2941 /* Find the namespace binding, using the declaration
2942 context. */
2943 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2944 false, true);
2945 if (fns == error_mark_node)
2946 /* If lookup fails, look for a friend declaration so we can
2947 give a better diagnostic. */
2948 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2949 /*type*/false, /*complain*/true,
2950 /*hidden*/true);
2951
2952 if (fns == error_mark_node || !is_overloaded_fn (fns))
2953 {
2954 error ("%qD is not a template function", dname);
2955 fns = error_mark_node;
2956 }
2957 }
2958
2959 declarator = lookup_template_function (fns, NULL_TREE);
2960 }
2961
2962 if (declarator == error_mark_node)
2963 return error_mark_node;
2964
2965 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2966 {
2967 if (!explicit_instantiation)
2968 /* A specialization in class scope. This is invalid,
2969 but the error will already have been flagged by
2970 check_specialization_scope. */
2971 return error_mark_node;
2972 else
2973 {
2974 /* It's not valid to write an explicit instantiation in
2975 class scope, e.g.:
2976
2977 class C { template void f(); }
2978
2979 This case is caught by the parser. However, on
2980 something like:
2981
2982 template class C { void f(); };
2983
2984 (which is invalid) we can get here. The error will be
2985 issued later. */
2986 ;
2987 }
2988
2989 return decl;
2990 }
2991 else if (ctype != NULL_TREE
2992 && (identifier_p (TREE_OPERAND (declarator, 0))))
2993 {
2994 // We'll match variable templates in start_decl.
2995 if (VAR_P (decl))
2996 return decl;
2997
2998 /* Find the list of functions in ctype that have the same
2999 name as the declared function. */
3000 tree name = TREE_OPERAND (declarator, 0);
3001
3002 if (constructor_name_p (name, ctype))
3003 {
3004 if (DECL_CONSTRUCTOR_P (decl)
3005 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3006 : !CLASSTYPE_DESTRUCTOR (ctype))
3007 {
3008 /* From [temp.expl.spec]:
3009
3010 If such an explicit specialization for the member
3011 of a class template names an implicitly-declared
3012 special member function (clause _special_), the
3013 program is ill-formed.
3014
3015 Similar language is found in [temp.explicit]. */
3016 error ("specialization of implicitly-declared special member function");
3017 return error_mark_node;
3018 }
3019
3020 name = DECL_NAME (decl);
3021 }
3022
3023 /* For a type-conversion operator, We might be looking for
3024 `operator int' which will be a specialization of
3025 `operator T'. Grab all the conversion operators, and
3026 then select from them. */
3027 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3028 ? conv_op_identifier : name);
3029
3030 if (fns == NULL_TREE)
3031 {
3032 error ("no member function %qD declared in %qT", name, ctype);
3033 return error_mark_node;
3034 }
3035 else
3036 TREE_OPERAND (declarator, 0) = fns;
3037 }
3038
3039 /* Figure out what exactly is being specialized at this point.
3040 Note that for an explicit instantiation, even one for a
3041 member function, we cannot tell a priori whether the
3042 instantiation is for a member template, or just a member
3043 function of a template class. Even if a member template is
3044 being instantiated, the member template arguments may be
3045 elided if they can be deduced from the rest of the
3046 declaration. */
3047 tmpl = determine_specialization (declarator, decl,
3048 &targs,
3049 member_specialization,
3050 template_count,
3051 tsk);
3052
3053 if (!tmpl || tmpl == error_mark_node)
3054 /* We couldn't figure out what this declaration was
3055 specializing. */
3056 return error_mark_node;
3057 else
3058 {
3059 if (TREE_CODE (decl) == FUNCTION_DECL
3060 && DECL_HIDDEN_FRIEND_P (tmpl))
3061 {
3062 auto_diagnostic_group d;
3063 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3064 "friend declaration %qD is not visible to "
3065 "explicit specialization", tmpl))
3066 inform (DECL_SOURCE_LOCATION (tmpl),
3067 "friend declaration here");
3068 }
3069 else if (!ctype && !is_friend
3070 && CP_DECL_CONTEXT (decl) == current_namespace)
3071 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3072
3073 tree gen_tmpl = most_general_template (tmpl);
3074
3075 if (explicit_instantiation)
3076 {
3077 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3078 is done by do_decl_instantiation later. */
3079
3080 int arg_depth = TMPL_ARGS_DEPTH (targs);
3081 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3082
3083 if (arg_depth > parm_depth)
3084 {
3085 /* If TMPL is not the most general template (for
3086 example, if TMPL is a friend template that is
3087 injected into namespace scope), then there will
3088 be too many levels of TARGS. Remove some of them
3089 here. */
3090 int i;
3091 tree new_targs;
3092
3093 new_targs = make_tree_vec (parm_depth);
3094 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3095 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3096 = TREE_VEC_ELT (targs, i);
3097 targs = new_targs;
3098 }
3099
3100 return instantiate_template (tmpl, targs, tf_error);
3101 }
3102
3103 /* If we thought that the DECL was a member function, but it
3104 turns out to be specializing a static member function,
3105 make DECL a static member function as well. */
3106 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3107 && DECL_STATIC_FUNCTION_P (tmpl)
3108 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3109 revert_static_member_fn (decl);
3110
3111 /* If this is a specialization of a member template of a
3112 template class, we want to return the TEMPLATE_DECL, not
3113 the specialization of it. */
3114 if (tsk == tsk_template && !was_template_id)
3115 {
3116 tree result = DECL_TEMPLATE_RESULT (tmpl);
3117 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3118 DECL_INITIAL (result) = NULL_TREE;
3119 if (have_def)
3120 {
3121 tree parm;
3122 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3123 DECL_SOURCE_LOCATION (result)
3124 = DECL_SOURCE_LOCATION (decl);
3125 /* We want to use the argument list specified in the
3126 definition, not in the original declaration. */
3127 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3128 for (parm = DECL_ARGUMENTS (result); parm;
3129 parm = DECL_CHAIN (parm))
3130 DECL_CONTEXT (parm) = result;
3131 }
3132 return register_specialization (tmpl, gen_tmpl, targs,
3133 is_friend, 0);
3134 }
3135
3136 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3137 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3138
3139 if (was_template_id)
3140 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3141
3142 /* Inherit default function arguments from the template
3143 DECL is specializing. */
3144 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3145 copy_default_args_to_explicit_spec (decl);
3146
3147 /* This specialization has the same protection as the
3148 template it specializes. */
3149 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3150 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3151
3152 /* 7.1.1-1 [dcl.stc]
3153
3154 A storage-class-specifier shall not be specified in an
3155 explicit specialization...
3156
3157 The parser rejects these, so unless action is taken here,
3158 explicit function specializations will always appear with
3159 global linkage.
3160
3161 The action recommended by the C++ CWG in response to C++
3162 defect report 605 is to make the storage class and linkage
3163 of the explicit specialization match the templated function:
3164
3165 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3166 */
3167 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3168 {
3169 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3170 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3171
3172 /* A concept cannot be specialized. */
3173 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3174 {
3175 error ("explicit specialization of function concept %qD",
3176 gen_tmpl);
3177 return error_mark_node;
3178 }
3179
3180 /* This specialization has the same linkage and visibility as
3181 the function template it specializes. */
3182 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3183 if (! TREE_PUBLIC (decl))
3184 {
3185 DECL_INTERFACE_KNOWN (decl) = 1;
3186 DECL_NOT_REALLY_EXTERN (decl) = 1;
3187 }
3188 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3189 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3190 {
3191 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3192 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3193 }
3194 }
3195
3196 /* If DECL is a friend declaration, declared using an
3197 unqualified name, the namespace associated with DECL may
3198 have been set incorrectly. For example, in:
3199
3200 template <typename T> void f(T);
3201 namespace N {
3202 struct S { friend void f<int>(int); }
3203 }
3204
3205 we will have set the DECL_CONTEXT for the friend
3206 declaration to N, rather than to the global namespace. */
3207 if (DECL_NAMESPACE_SCOPE_P (decl))
3208 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3209
3210 if (is_friend && !have_def)
3211 /* This is not really a declaration of a specialization.
3212 It's just the name of an instantiation. But, it's not
3213 a request for an instantiation, either. */
3214 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3215 else if (TREE_CODE (decl) == FUNCTION_DECL)
3216 /* A specialization is not necessarily COMDAT. */
3217 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3218 && DECL_DECLARED_INLINE_P (decl));
3219 else if (VAR_P (decl))
3220 DECL_COMDAT (decl) = false;
3221
3222 /* If this is a full specialization, register it so that we can find
3223 it again. Partial specializations will be registered in
3224 process_partial_specialization. */
3225 if (!processing_template_decl)
3226 {
3227 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3228
3229 decl = register_specialization (decl, gen_tmpl, targs,
3230 is_friend, 0);
3231 }
3232
3233
3234 /* A 'structor should already have clones. */
3235 gcc_assert (decl == error_mark_node
3236 || variable_template_p (tmpl)
3237 || !(DECL_CONSTRUCTOR_P (decl)
3238 || DECL_DESTRUCTOR_P (decl))
3239 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3240 }
3241 }
3242
3243 return decl;
3244 }
3245
3246 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3247 parameters. These are represented in the same format used for
3248 DECL_TEMPLATE_PARMS. */
3249
3250 int
3251 comp_template_parms (const_tree parms1, const_tree parms2)
3252 {
3253 const_tree p1;
3254 const_tree p2;
3255
3256 if (parms1 == parms2)
3257 return 1;
3258
3259 for (p1 = parms1, p2 = parms2;
3260 p1 != NULL_TREE && p2 != NULL_TREE;
3261 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3262 {
3263 tree t1 = TREE_VALUE (p1);
3264 tree t2 = TREE_VALUE (p2);
3265 int i;
3266
3267 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3268 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3269
3270 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3271 return 0;
3272
3273 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3274 {
3275 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3276 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3277
3278 /* If either of the template parameters are invalid, assume
3279 they match for the sake of error recovery. */
3280 if (error_operand_p (parm1) || error_operand_p (parm2))
3281 return 1;
3282
3283 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3284 return 0;
3285
3286 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3287 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3288 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3289 continue;
3290 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3291 return 0;
3292 }
3293 }
3294
3295 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3296 /* One set of parameters has more parameters lists than the
3297 other. */
3298 return 0;
3299
3300 return 1;
3301 }
3302
3303 /* Determine whether PARM is a parameter pack. */
3304
3305 bool
3306 template_parameter_pack_p (const_tree parm)
3307 {
3308 /* Determine if we have a non-type template parameter pack. */
3309 if (TREE_CODE (parm) == PARM_DECL)
3310 return (DECL_TEMPLATE_PARM_P (parm)
3311 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3312 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3313 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3314
3315 /* If this is a list of template parameters, we could get a
3316 TYPE_DECL or a TEMPLATE_DECL. */
3317 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3318 parm = TREE_TYPE (parm);
3319
3320 /* Otherwise it must be a type template parameter. */
3321 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3322 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3323 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3324 }
3325
3326 /* Determine if T is a function parameter pack. */
3327
3328 bool
3329 function_parameter_pack_p (const_tree t)
3330 {
3331 if (t && TREE_CODE (t) == PARM_DECL)
3332 return DECL_PACK_P (t);
3333 return false;
3334 }
3335
3336 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3337 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3338
3339 tree
3340 get_function_template_decl (const_tree primary_func_tmpl_inst)
3341 {
3342 if (! primary_func_tmpl_inst
3343 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3344 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3345 return NULL;
3346
3347 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3348 }
3349
3350 /* Return true iff the function parameter PARAM_DECL was expanded
3351 from the function parameter pack PACK. */
3352
3353 bool
3354 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3355 {
3356 if (DECL_ARTIFICIAL (param_decl)
3357 || !function_parameter_pack_p (pack))
3358 return false;
3359
3360 /* The parameter pack and its pack arguments have the same
3361 DECL_PARM_INDEX. */
3362 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3363 }
3364
3365 /* Determine whether ARGS describes a variadic template args list,
3366 i.e., one that is terminated by a template argument pack. */
3367
3368 static bool
3369 template_args_variadic_p (tree args)
3370 {
3371 int nargs;
3372 tree last_parm;
3373
3374 if (args == NULL_TREE)
3375 return false;
3376
3377 args = INNERMOST_TEMPLATE_ARGS (args);
3378 nargs = TREE_VEC_LENGTH (args);
3379
3380 if (nargs == 0)
3381 return false;
3382
3383 last_parm = TREE_VEC_ELT (args, nargs - 1);
3384
3385 return ARGUMENT_PACK_P (last_parm);
3386 }
3387
3388 /* Generate a new name for the parameter pack name NAME (an
3389 IDENTIFIER_NODE) that incorporates its */
3390
3391 static tree
3392 make_ith_pack_parameter_name (tree name, int i)
3393 {
3394 /* Munge the name to include the parameter index. */
3395 #define NUMBUF_LEN 128
3396 char numbuf[NUMBUF_LEN];
3397 char* newname;
3398 int newname_len;
3399
3400 if (name == NULL_TREE)
3401 return name;
3402 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3403 newname_len = IDENTIFIER_LENGTH (name)
3404 + strlen (numbuf) + 2;
3405 newname = (char*)alloca (newname_len);
3406 snprintf (newname, newname_len,
3407 "%s#%i", IDENTIFIER_POINTER (name), i);
3408 return get_identifier (newname);
3409 }
3410
3411 /* Return true if T is a primary function, class or alias template
3412 specialization, not including the template pattern. */
3413
3414 bool
3415 primary_template_specialization_p (const_tree t)
3416 {
3417 if (!t)
3418 return false;
3419
3420 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3421 return (DECL_LANG_SPECIFIC (t)
3422 && DECL_USE_TEMPLATE (t)
3423 && DECL_TEMPLATE_INFO (t)
3424 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3425 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3426 return (CLASSTYPE_TEMPLATE_INFO (t)
3427 && CLASSTYPE_USE_TEMPLATE (t)
3428 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3429 else if (alias_template_specialization_p (t))
3430 return true;
3431 return false;
3432 }
3433
3434 /* Return true if PARM is a template template parameter. */
3435
3436 bool
3437 template_template_parameter_p (const_tree parm)
3438 {
3439 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3440 }
3441
3442 /* Return true iff PARM is a DECL representing a type template
3443 parameter. */
3444
3445 bool
3446 template_type_parameter_p (const_tree parm)
3447 {
3448 return (parm
3449 && (TREE_CODE (parm) == TYPE_DECL
3450 || TREE_CODE (parm) == TEMPLATE_DECL)
3451 && DECL_TEMPLATE_PARM_P (parm));
3452 }
3453
3454 /* Return the template parameters of T if T is a
3455 primary template instantiation, NULL otherwise. */
3456
3457 tree
3458 get_primary_template_innermost_parameters (const_tree t)
3459 {
3460 tree parms = NULL, template_info = NULL;
3461
3462 if ((template_info = get_template_info (t))
3463 && primary_template_specialization_p (t))
3464 parms = INNERMOST_TEMPLATE_PARMS
3465 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3466
3467 return parms;
3468 }
3469
3470 /* Return the template parameters of the LEVELth level from the full list
3471 of template parameters PARMS. */
3472
3473 tree
3474 get_template_parms_at_level (tree parms, int level)
3475 {
3476 tree p;
3477 if (!parms
3478 || TREE_CODE (parms) != TREE_LIST
3479 || level > TMPL_PARMS_DEPTH (parms))
3480 return NULL_TREE;
3481
3482 for (p = parms; p; p = TREE_CHAIN (p))
3483 if (TMPL_PARMS_DEPTH (p) == level)
3484 return p;
3485
3486 return NULL_TREE;
3487 }
3488
3489 /* Returns the template arguments of T if T is a template instantiation,
3490 NULL otherwise. */
3491
3492 tree
3493 get_template_innermost_arguments (const_tree t)
3494 {
3495 tree args = NULL, template_info = NULL;
3496
3497 if ((template_info = get_template_info (t))
3498 && TI_ARGS (template_info))
3499 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3500
3501 return args;
3502 }
3503
3504 /* Return the argument pack elements of T if T is a template argument pack,
3505 NULL otherwise. */
3506
3507 tree
3508 get_template_argument_pack_elems (const_tree t)
3509 {
3510 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3511 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3512 return NULL;
3513
3514 return ARGUMENT_PACK_ARGS (t);
3515 }
3516
3517 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3518 ARGUMENT_PACK_SELECT represents. */
3519
3520 static tree
3521 argument_pack_select_arg (tree t)
3522 {
3523 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3524 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3525
3526 /* If the selected argument is an expansion E, that most likely means we were
3527 called from gen_elem_of_pack_expansion_instantiation during the
3528 substituting of an argument pack (of which the Ith element is a pack
3529 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3530 In this case, the Ith element resulting from this substituting is going to
3531 be a pack expansion, which pattern is the pattern of E. Let's return the
3532 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3533 resulting pack expansion from it. */
3534 if (PACK_EXPANSION_P (arg))
3535 {
3536 /* Make sure we aren't throwing away arg info. */
3537 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3538 arg = PACK_EXPANSION_PATTERN (arg);
3539 }
3540
3541 return arg;
3542 }
3543
3544
3545 /* True iff FN is a function representing a built-in variadic parameter
3546 pack. */
3547
3548 bool
3549 builtin_pack_fn_p (tree fn)
3550 {
3551 if (!fn
3552 || TREE_CODE (fn) != FUNCTION_DECL
3553 || !DECL_IS_BUILTIN (fn))
3554 return false;
3555
3556 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3557 return true;
3558
3559 return false;
3560 }
3561
3562 /* True iff CALL is a call to a function representing a built-in variadic
3563 parameter pack. */
3564
3565 static bool
3566 builtin_pack_call_p (tree call)
3567 {
3568 if (TREE_CODE (call) != CALL_EXPR)
3569 return false;
3570 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3571 }
3572
3573 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3574
3575 static tree
3576 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3577 tree in_decl)
3578 {
3579 tree ohi = CALL_EXPR_ARG (call, 0);
3580 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3581 false/*fn*/, true/*int_cst*/);
3582
3583 if (value_dependent_expression_p (hi))
3584 {
3585 if (hi != ohi)
3586 {
3587 call = copy_node (call);
3588 CALL_EXPR_ARG (call, 0) = hi;
3589 }
3590 tree ex = make_pack_expansion (call, complain);
3591 tree vec = make_tree_vec (1);
3592 TREE_VEC_ELT (vec, 0) = ex;
3593 return vec;
3594 }
3595 else
3596 {
3597 hi = cxx_constant_value (hi);
3598 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3599
3600 /* Calculate the largest value of len that won't make the size of the vec
3601 overflow an int. The compiler will exceed resource limits long before
3602 this, but it seems a decent place to diagnose. */
3603 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3604
3605 if (len < 0 || len > max)
3606 {
3607 if ((complain & tf_error)
3608 && hi != error_mark_node)
3609 error ("argument to __integer_pack must be between 0 and %d", max);
3610 return error_mark_node;
3611 }
3612
3613 tree vec = make_tree_vec (len);
3614
3615 for (int i = 0; i < len; ++i)
3616 TREE_VEC_ELT (vec, i) = size_int (i);
3617
3618 return vec;
3619 }
3620 }
3621
3622 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3623 CALL. */
3624
3625 static tree
3626 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3627 tree in_decl)
3628 {
3629 if (!builtin_pack_call_p (call))
3630 return NULL_TREE;
3631
3632 tree fn = CALL_EXPR_FN (call);
3633
3634 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3635 return expand_integer_pack (call, args, complain, in_decl);
3636
3637 return NULL_TREE;
3638 }
3639
3640 /* Structure used to track the progress of find_parameter_packs_r. */
3641 struct find_parameter_pack_data
3642 {
3643 /* TREE_LIST that will contain all of the parameter packs found by
3644 the traversal. */
3645 tree* parameter_packs;
3646
3647 /* Set of AST nodes that have been visited by the traversal. */
3648 hash_set<tree> *visited;
3649
3650 /* True iff we're making a type pack expansion. */
3651 bool type_pack_expansion_p;
3652 };
3653
3654 /* Identifies all of the argument packs that occur in a template
3655 argument and appends them to the TREE_LIST inside DATA, which is a
3656 find_parameter_pack_data structure. This is a subroutine of
3657 make_pack_expansion and uses_parameter_packs. */
3658 static tree
3659 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3660 {
3661 tree t = *tp;
3662 struct find_parameter_pack_data* ppd =
3663 (struct find_parameter_pack_data*)data;
3664 bool parameter_pack_p = false;
3665
3666 /* Handle type aliases/typedefs. */
3667 if (TYPE_ALIAS_P (t))
3668 {
3669 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3670 cp_walk_tree (&TI_ARGS (tinfo),
3671 &find_parameter_packs_r,
3672 ppd, ppd->visited);
3673 *walk_subtrees = 0;
3674 return NULL_TREE;
3675 }
3676
3677 /* Identify whether this is a parameter pack or not. */
3678 switch (TREE_CODE (t))
3679 {
3680 case TEMPLATE_PARM_INDEX:
3681 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3682 parameter_pack_p = true;
3683 break;
3684
3685 case TEMPLATE_TYPE_PARM:
3686 t = TYPE_MAIN_VARIANT (t);
3687 /* FALLTHRU */
3688 case TEMPLATE_TEMPLATE_PARM:
3689 /* If the placeholder appears in the decl-specifier-seq of a function
3690 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3691 is a pack expansion, the invented template parameter is a template
3692 parameter pack. */
3693 if (ppd->type_pack_expansion_p && is_auto (t))
3694 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3695 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3696 parameter_pack_p = true;
3697 break;
3698
3699 case FIELD_DECL:
3700 case PARM_DECL:
3701 if (DECL_PACK_P (t))
3702 {
3703 /* We don't want to walk into the type of a PARM_DECL,
3704 because we don't want to see the type parameter pack. */
3705 *walk_subtrees = 0;
3706 parameter_pack_p = true;
3707 }
3708 break;
3709
3710 case VAR_DECL:
3711 if (DECL_PACK_P (t))
3712 {
3713 /* We don't want to walk into the type of a variadic capture proxy,
3714 because we don't want to see the type parameter pack. */
3715 *walk_subtrees = 0;
3716 parameter_pack_p = true;
3717 }
3718 else if (variable_template_specialization_p (t))
3719 {
3720 cp_walk_tree (&DECL_TI_ARGS (t),
3721 find_parameter_packs_r,
3722 ppd, ppd->visited);
3723 *walk_subtrees = 0;
3724 }
3725 break;
3726
3727 case CALL_EXPR:
3728 if (builtin_pack_call_p (t))
3729 parameter_pack_p = true;
3730 break;
3731
3732 case BASES:
3733 parameter_pack_p = true;
3734 break;
3735 default:
3736 /* Not a parameter pack. */
3737 break;
3738 }
3739
3740 if (parameter_pack_p)
3741 {
3742 /* Add this parameter pack to the list. */
3743 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3744 }
3745
3746 if (TYPE_P (t))
3747 cp_walk_tree (&TYPE_CONTEXT (t),
3748 &find_parameter_packs_r, ppd, ppd->visited);
3749
3750 /* This switch statement will return immediately if we don't find a
3751 parameter pack. */
3752 switch (TREE_CODE (t))
3753 {
3754 case TEMPLATE_PARM_INDEX:
3755 return NULL_TREE;
3756
3757 case BOUND_TEMPLATE_TEMPLATE_PARM:
3758 /* Check the template itself. */
3759 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3760 &find_parameter_packs_r, ppd, ppd->visited);
3761 /* Check the template arguments. */
3762 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3763 ppd->visited);
3764 *walk_subtrees = 0;
3765 return NULL_TREE;
3766
3767 case TEMPLATE_TYPE_PARM:
3768 case TEMPLATE_TEMPLATE_PARM:
3769 return NULL_TREE;
3770
3771 case PARM_DECL:
3772 return NULL_TREE;
3773
3774 case DECL_EXPR:
3775 /* Ignore the declaration of a capture proxy for a parameter pack. */
3776 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3777 *walk_subtrees = 0;
3778 return NULL_TREE;
3779
3780 case RECORD_TYPE:
3781 if (TYPE_PTRMEMFUNC_P (t))
3782 return NULL_TREE;
3783 /* Fall through. */
3784
3785 case UNION_TYPE:
3786 case ENUMERAL_TYPE:
3787 if (TYPE_TEMPLATE_INFO (t))
3788 cp_walk_tree (&TYPE_TI_ARGS (t),
3789 &find_parameter_packs_r, ppd, ppd->visited);
3790
3791 *walk_subtrees = 0;
3792 return NULL_TREE;
3793
3794 case TEMPLATE_DECL:
3795 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3796 return NULL_TREE;
3797 gcc_fallthrough();
3798
3799 case CONSTRUCTOR:
3800 cp_walk_tree (&TREE_TYPE (t),
3801 &find_parameter_packs_r, ppd, ppd->visited);
3802 return NULL_TREE;
3803
3804 case TYPENAME_TYPE:
3805 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3806 ppd, ppd->visited);
3807 *walk_subtrees = 0;
3808 return NULL_TREE;
3809
3810 case TYPE_PACK_EXPANSION:
3811 case EXPR_PACK_EXPANSION:
3812 *walk_subtrees = 0;
3813 return NULL_TREE;
3814
3815 case INTEGER_TYPE:
3816 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3817 ppd, ppd->visited);
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3820
3821 case IDENTIFIER_NODE:
3822 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3823 ppd->visited);
3824 *walk_subtrees = 0;
3825 return NULL_TREE;
3826
3827 case LAMBDA_EXPR:
3828 {
3829 /* Look at explicit captures. */
3830 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3831 cap; cap = TREE_CHAIN (cap))
3832 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3833 ppd->visited);
3834 /* Since we defer implicit capture, look in the parms and body. */
3835 tree fn = lambda_function (t);
3836 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3837 ppd->visited);
3838 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3839 ppd->visited);
3840 *walk_subtrees = 0;
3841 return NULL_TREE;
3842 }
3843
3844 case DECLTYPE_TYPE:
3845 {
3846 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3847 type_pack_expansion_p to false so that any placeholders
3848 within the expression don't get marked as parameter packs. */
3849 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3850 ppd->type_pack_expansion_p = false;
3851 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3852 ppd, ppd->visited);
3853 ppd->type_pack_expansion_p = type_pack_expansion_p;
3854 *walk_subtrees = 0;
3855 return NULL_TREE;
3856 }
3857
3858 case IF_STMT:
3859 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3860 ppd, ppd->visited);
3861 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3862 ppd, ppd->visited);
3863 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3864 ppd, ppd->visited);
3865 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3866 *walk_subtrees = 0;
3867 return NULL_TREE;
3868
3869 default:
3870 return NULL_TREE;
3871 }
3872
3873 return NULL_TREE;
3874 }
3875
3876 /* Determines if the expression or type T uses any parameter packs. */
3877 bool
3878 uses_parameter_packs (tree t)
3879 {
3880 tree parameter_packs = NULL_TREE;
3881 struct find_parameter_pack_data ppd;
3882 ppd.parameter_packs = &parameter_packs;
3883 ppd.visited = new hash_set<tree>;
3884 ppd.type_pack_expansion_p = false;
3885 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3886 delete ppd.visited;
3887 return parameter_packs != NULL_TREE;
3888 }
3889
3890 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3891 representation a base-class initializer into a parameter pack
3892 expansion. If all goes well, the resulting node will be an
3893 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3894 respectively. */
3895 tree
3896 make_pack_expansion (tree arg, tsubst_flags_t complain)
3897 {
3898 tree result;
3899 tree parameter_packs = NULL_TREE;
3900 bool for_types = false;
3901 struct find_parameter_pack_data ppd;
3902
3903 if (!arg || arg == error_mark_node)
3904 return arg;
3905
3906 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3907 {
3908 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3909 class initializer. In this case, the TREE_PURPOSE will be a
3910 _TYPE node (representing the base class expansion we're
3911 initializing) and the TREE_VALUE will be a TREE_LIST
3912 containing the initialization arguments.
3913
3914 The resulting expansion looks somewhat different from most
3915 expansions. Rather than returning just one _EXPANSION, we
3916 return a TREE_LIST whose TREE_PURPOSE is a
3917 TYPE_PACK_EXPANSION containing the bases that will be
3918 initialized. The TREE_VALUE will be identical to the
3919 original TREE_VALUE, which is a list of arguments that will
3920 be passed to each base. We do not introduce any new pack
3921 expansion nodes into the TREE_VALUE (although it is possible
3922 that some already exist), because the TREE_PURPOSE and
3923 TREE_VALUE all need to be expanded together with the same
3924 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3925 resulting TREE_PURPOSE will mention the parameter packs in
3926 both the bases and the arguments to the bases. */
3927 tree purpose;
3928 tree value;
3929 tree parameter_packs = NULL_TREE;
3930
3931 /* Determine which parameter packs will be used by the base
3932 class expansion. */
3933 ppd.visited = new hash_set<tree>;
3934 ppd.parameter_packs = &parameter_packs;
3935 ppd.type_pack_expansion_p = false;
3936 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3937 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3938 &ppd, ppd.visited);
3939
3940 if (parameter_packs == NULL_TREE)
3941 {
3942 if (complain & tf_error)
3943 error ("base initializer expansion %qT contains no parameter packs",
3944 arg);
3945 delete ppd.visited;
3946 return error_mark_node;
3947 }
3948
3949 if (TREE_VALUE (arg) != void_type_node)
3950 {
3951 /* Collect the sets of parameter packs used in each of the
3952 initialization arguments. */
3953 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3954 {
3955 /* Determine which parameter packs will be expanded in this
3956 argument. */
3957 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3958 &ppd, ppd.visited);
3959 }
3960 }
3961
3962 delete ppd.visited;
3963
3964 /* Create the pack expansion type for the base type. */
3965 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3966 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3967 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3968 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3969
3970 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3971 they will rarely be compared to anything. */
3972 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3973
3974 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3975 }
3976
3977 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3978 for_types = true;
3979
3980 /* Build the PACK_EXPANSION_* node. */
3981 result = for_types
3982 ? cxx_make_type (TYPE_PACK_EXPANSION)
3983 : make_node (EXPR_PACK_EXPANSION);
3984 SET_PACK_EXPANSION_PATTERN (result, arg);
3985 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3986 {
3987 /* Propagate type and const-expression information. */
3988 TREE_TYPE (result) = TREE_TYPE (arg);
3989 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3990 /* Mark this read now, since the expansion might be length 0. */
3991 mark_exp_read (arg);
3992 }
3993 else
3994 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3995 they will rarely be compared to anything. */
3996 SET_TYPE_STRUCTURAL_EQUALITY (result);
3997
3998 /* Determine which parameter packs will be expanded. */
3999 ppd.parameter_packs = &parameter_packs;
4000 ppd.visited = new hash_set<tree>;
4001 ppd.type_pack_expansion_p = TYPE_P (arg);
4002 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4003 delete ppd.visited;
4004
4005 /* Make sure we found some parameter packs. */
4006 if (parameter_packs == NULL_TREE)
4007 {
4008 if (complain & tf_error)
4009 {
4010 if (TYPE_P (arg))
4011 error ("expansion pattern %qT contains no parameter packs", arg);
4012 else
4013 error ("expansion pattern %qE contains no parameter packs", arg);
4014 }
4015 return error_mark_node;
4016 }
4017 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4018
4019 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4020
4021 return result;
4022 }
4023
4024 /* Checks T for any "bare" parameter packs, which have not yet been
4025 expanded, and issues an error if any are found. This operation can
4026 only be done on full expressions or types (e.g., an expression
4027 statement, "if" condition, etc.), because we could have expressions like:
4028
4029 foo(f(g(h(args)))...)
4030
4031 where "args" is a parameter pack. check_for_bare_parameter_packs
4032 should not be called for the subexpressions args, h(args),
4033 g(h(args)), or f(g(h(args))), because we would produce erroneous
4034 error messages.
4035
4036 Returns TRUE and emits an error if there were bare parameter packs,
4037 returns FALSE otherwise. */
4038 bool
4039 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4040 {
4041 tree parameter_packs = NULL_TREE;
4042 struct find_parameter_pack_data ppd;
4043
4044 if (!processing_template_decl || !t || t == error_mark_node)
4045 return false;
4046
4047 /* A lambda might use a parameter pack from the containing context. */
4048 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4049 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4050 return false;
4051
4052 if (TREE_CODE (t) == TYPE_DECL)
4053 t = TREE_TYPE (t);
4054
4055 ppd.parameter_packs = &parameter_packs;
4056 ppd.visited = new hash_set<tree>;
4057 ppd.type_pack_expansion_p = false;
4058 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4059 delete ppd.visited;
4060
4061 if (parameter_packs)
4062 {
4063 if (loc == UNKNOWN_LOCATION)
4064 loc = cp_expr_loc_or_loc (t, input_location);
4065 error_at (loc, "parameter packs not expanded with %<...%>:");
4066 while (parameter_packs)
4067 {
4068 tree pack = TREE_VALUE (parameter_packs);
4069 tree name = NULL_TREE;
4070
4071 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4072 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4073 name = TYPE_NAME (pack);
4074 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4075 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4076 else if (TREE_CODE (pack) == CALL_EXPR)
4077 name = DECL_NAME (CALL_EXPR_FN (pack));
4078 else
4079 name = DECL_NAME (pack);
4080
4081 if (name)
4082 inform (loc, " %qD", name);
4083 else
4084 inform (loc, " <anonymous>");
4085
4086 parameter_packs = TREE_CHAIN (parameter_packs);
4087 }
4088
4089 return true;
4090 }
4091
4092 return false;
4093 }
4094
4095 /* Expand any parameter packs that occur in the template arguments in
4096 ARGS. */
4097 tree
4098 expand_template_argument_pack (tree args)
4099 {
4100 if (args == error_mark_node)
4101 return error_mark_node;
4102
4103 tree result_args = NULL_TREE;
4104 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4105 int num_result_args = -1;
4106 int non_default_args_count = -1;
4107
4108 /* First, determine if we need to expand anything, and the number of
4109 slots we'll need. */
4110 for (in_arg = 0; in_arg < nargs; ++in_arg)
4111 {
4112 tree arg = TREE_VEC_ELT (args, in_arg);
4113 if (arg == NULL_TREE)
4114 return args;
4115 if (ARGUMENT_PACK_P (arg))
4116 {
4117 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4118 if (num_result_args < 0)
4119 num_result_args = in_arg + num_packed;
4120 else
4121 num_result_args += num_packed;
4122 }
4123 else
4124 {
4125 if (num_result_args >= 0)
4126 num_result_args++;
4127 }
4128 }
4129
4130 /* If no expansion is necessary, we're done. */
4131 if (num_result_args < 0)
4132 return args;
4133
4134 /* Expand arguments. */
4135 result_args = make_tree_vec (num_result_args);
4136 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4137 non_default_args_count =
4138 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4139 for (in_arg = 0; in_arg < nargs; ++in_arg)
4140 {
4141 tree arg = TREE_VEC_ELT (args, in_arg);
4142 if (ARGUMENT_PACK_P (arg))
4143 {
4144 tree packed = ARGUMENT_PACK_ARGS (arg);
4145 int i, num_packed = TREE_VEC_LENGTH (packed);
4146 for (i = 0; i < num_packed; ++i, ++out_arg)
4147 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4148 if (non_default_args_count > 0)
4149 non_default_args_count += num_packed - 1;
4150 }
4151 else
4152 {
4153 TREE_VEC_ELT (result_args, out_arg) = arg;
4154 ++out_arg;
4155 }
4156 }
4157 if (non_default_args_count >= 0)
4158 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4159 return result_args;
4160 }
4161
4162 /* Checks if DECL shadows a template parameter.
4163
4164 [temp.local]: A template-parameter shall not be redeclared within its
4165 scope (including nested scopes).
4166
4167 Emits an error and returns TRUE if the DECL shadows a parameter,
4168 returns FALSE otherwise. */
4169
4170 bool
4171 check_template_shadow (tree decl)
4172 {
4173 tree olddecl;
4174
4175 /* If we're not in a template, we can't possibly shadow a template
4176 parameter. */
4177 if (!current_template_parms)
4178 return true;
4179
4180 /* Figure out what we're shadowing. */
4181 decl = OVL_FIRST (decl);
4182 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4183
4184 /* If there's no previous binding for this name, we're not shadowing
4185 anything, let alone a template parameter. */
4186 if (!olddecl)
4187 return true;
4188
4189 /* If we're not shadowing a template parameter, we're done. Note
4190 that OLDDECL might be an OVERLOAD (or perhaps even an
4191 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4192 node. */
4193 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4194 return true;
4195
4196 /* We check for decl != olddecl to avoid bogus errors for using a
4197 name inside a class. We check TPFI to avoid duplicate errors for
4198 inline member templates. */
4199 if (decl == olddecl
4200 || (DECL_TEMPLATE_PARM_P (decl)
4201 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4202 return true;
4203
4204 /* Don't complain about the injected class name, as we've already
4205 complained about the class itself. */
4206 if (DECL_SELF_REFERENCE_P (decl))
4207 return false;
4208
4209 if (DECL_TEMPLATE_PARM_P (decl))
4210 error ("declaration of template parameter %q+D shadows "
4211 "template parameter", decl);
4212 else
4213 error ("declaration of %q+#D shadows template parameter", decl);
4214 inform (DECL_SOURCE_LOCATION (olddecl),
4215 "template parameter %qD declared here", olddecl);
4216 return false;
4217 }
4218
4219 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4220 ORIG_LEVEL, DECL, and TYPE. */
4221
4222 static tree
4223 build_template_parm_index (int index,
4224 int level,
4225 int orig_level,
4226 tree decl,
4227 tree type)
4228 {
4229 tree t = make_node (TEMPLATE_PARM_INDEX);
4230 TEMPLATE_PARM_IDX (t) = index;
4231 TEMPLATE_PARM_LEVEL (t) = level;
4232 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4233 TEMPLATE_PARM_DECL (t) = decl;
4234 TREE_TYPE (t) = type;
4235 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4236 TREE_READONLY (t) = TREE_READONLY (decl);
4237
4238 return t;
4239 }
4240
4241 /* Find the canonical type parameter for the given template type
4242 parameter. Returns the canonical type parameter, which may be TYPE
4243 if no such parameter existed. */
4244
4245 static tree
4246 canonical_type_parameter (tree type)
4247 {
4248 tree list;
4249 int idx = TEMPLATE_TYPE_IDX (type);
4250 if (!canonical_template_parms)
4251 vec_alloc (canonical_template_parms, idx + 1);
4252
4253 if (canonical_template_parms->length () <= (unsigned) idx)
4254 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4255
4256 list = (*canonical_template_parms)[idx];
4257 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4258 list = TREE_CHAIN (list);
4259
4260 if (list)
4261 return TREE_VALUE (list);
4262 else
4263 {
4264 (*canonical_template_parms)[idx]
4265 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4266 return type;
4267 }
4268 }
4269
4270 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4271 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4272 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4273 new one is created. */
4274
4275 static tree
4276 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4277 tsubst_flags_t complain)
4278 {
4279 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4280 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4281 != TEMPLATE_PARM_LEVEL (index) - levels)
4282 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4283 {
4284 tree orig_decl = TEMPLATE_PARM_DECL (index);
4285 tree decl, t;
4286
4287 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4288 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4289 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4290 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4291 DECL_ARTIFICIAL (decl) = 1;
4292 SET_DECL_TEMPLATE_PARM_P (decl);
4293
4294 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4295 TEMPLATE_PARM_LEVEL (index) - levels,
4296 TEMPLATE_PARM_ORIG_LEVEL (index),
4297 decl, type);
4298 TEMPLATE_PARM_DESCENDANTS (index) = t;
4299 TEMPLATE_PARM_PARAMETER_PACK (t)
4300 = TEMPLATE_PARM_PARAMETER_PACK (index);
4301
4302 /* Template template parameters need this. */
4303 if (TREE_CODE (decl) == TEMPLATE_DECL)
4304 {
4305 DECL_TEMPLATE_RESULT (decl)
4306 = build_decl (DECL_SOURCE_LOCATION (decl),
4307 TYPE_DECL, DECL_NAME (decl), type);
4308 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4309 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4310 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4311 }
4312 }
4313
4314 return TEMPLATE_PARM_DESCENDANTS (index);
4315 }
4316
4317 /* Process information from new template parameter PARM and append it
4318 to the LIST being built. This new parameter is a non-type
4319 parameter iff IS_NON_TYPE is true. This new parameter is a
4320 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4321 is in PARM_LOC. */
4322
4323 tree
4324 process_template_parm (tree list, location_t parm_loc, tree parm,
4325 bool is_non_type, bool is_parameter_pack)
4326 {
4327 tree decl = 0;
4328 int idx = 0;
4329
4330 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4331 tree defval = TREE_PURPOSE (parm);
4332 tree constr = TREE_TYPE (parm);
4333
4334 if (list)
4335 {
4336 tree p = tree_last (list);
4337
4338 if (p && TREE_VALUE (p) != error_mark_node)
4339 {
4340 p = TREE_VALUE (p);
4341 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4342 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4343 else
4344 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4345 }
4346
4347 ++idx;
4348 }
4349
4350 if (is_non_type)
4351 {
4352 parm = TREE_VALUE (parm);
4353
4354 SET_DECL_TEMPLATE_PARM_P (parm);
4355
4356 if (TREE_TYPE (parm) != error_mark_node)
4357 {
4358 /* [temp.param]
4359
4360 The top-level cv-qualifiers on the template-parameter are
4361 ignored when determining its type. */
4362 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4363 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4364 TREE_TYPE (parm) = error_mark_node;
4365 else if (uses_parameter_packs (TREE_TYPE (parm))
4366 && !is_parameter_pack
4367 /* If we're in a nested template parameter list, the template
4368 template parameter could be a parameter pack. */
4369 && processing_template_parmlist == 1)
4370 {
4371 /* This template parameter is not a parameter pack, but it
4372 should be. Complain about "bare" parameter packs. */
4373 check_for_bare_parameter_packs (TREE_TYPE (parm));
4374
4375 /* Recover by calling this a parameter pack. */
4376 is_parameter_pack = true;
4377 }
4378 }
4379
4380 /* A template parameter is not modifiable. */
4381 TREE_CONSTANT (parm) = 1;
4382 TREE_READONLY (parm) = 1;
4383 decl = build_decl (parm_loc,
4384 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4385 TREE_CONSTANT (decl) = 1;
4386 TREE_READONLY (decl) = 1;
4387 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4388 = build_template_parm_index (idx, processing_template_decl,
4389 processing_template_decl,
4390 decl, TREE_TYPE (parm));
4391
4392 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4393 = is_parameter_pack;
4394 }
4395 else
4396 {
4397 tree t;
4398 parm = TREE_VALUE (TREE_VALUE (parm));
4399
4400 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4401 {
4402 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4403 /* This is for distinguishing between real templates and template
4404 template parameters */
4405 TREE_TYPE (parm) = t;
4406 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4407 decl = parm;
4408 }
4409 else
4410 {
4411 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4412 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4413 decl = build_decl (parm_loc,
4414 TYPE_DECL, parm, t);
4415 }
4416
4417 TYPE_NAME (t) = decl;
4418 TYPE_STUB_DECL (t) = decl;
4419 parm = decl;
4420 TEMPLATE_TYPE_PARM_INDEX (t)
4421 = build_template_parm_index (idx, processing_template_decl,
4422 processing_template_decl,
4423 decl, TREE_TYPE (parm));
4424 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4425 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4426 }
4427 DECL_ARTIFICIAL (decl) = 1;
4428 SET_DECL_TEMPLATE_PARM_P (decl);
4429
4430 /* Build requirements for the type/template parameter.
4431 This must be done after SET_DECL_TEMPLATE_PARM_P or
4432 process_template_parm could fail. */
4433 tree reqs = finish_shorthand_constraint (parm, constr);
4434
4435 decl = pushdecl (decl);
4436 if (!is_non_type)
4437 parm = decl;
4438
4439 /* Build the parameter node linking the parameter declaration,
4440 its default argument (if any), and its constraints (if any). */
4441 parm = build_tree_list (defval, parm);
4442 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4443
4444 return chainon (list, parm);
4445 }
4446
4447 /* The end of a template parameter list has been reached. Process the
4448 tree list into a parameter vector, converting each parameter into a more
4449 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4450 as PARM_DECLs. */
4451
4452 tree
4453 end_template_parm_list (tree parms)
4454 {
4455 int nparms;
4456 tree parm, next;
4457 tree saved_parmlist = make_tree_vec (list_length (parms));
4458
4459 /* Pop the dummy parameter level and add the real one. */
4460 current_template_parms = TREE_CHAIN (current_template_parms);
4461
4462 current_template_parms
4463 = tree_cons (size_int (processing_template_decl),
4464 saved_parmlist, current_template_parms);
4465
4466 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4467 {
4468 next = TREE_CHAIN (parm);
4469 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4470 TREE_CHAIN (parm) = NULL_TREE;
4471 }
4472
4473 --processing_template_parmlist;
4474
4475 return saved_parmlist;
4476 }
4477
4478 // Explicitly indicate the end of the template parameter list. We assume
4479 // that the current template parameters have been constructed and/or
4480 // managed explicitly, as when creating new template template parameters
4481 // from a shorthand constraint.
4482 void
4483 end_template_parm_list ()
4484 {
4485 --processing_template_parmlist;
4486 }
4487
4488 /* end_template_decl is called after a template declaration is seen. */
4489
4490 void
4491 end_template_decl (void)
4492 {
4493 reset_specialization ();
4494
4495 if (! processing_template_decl)
4496 return;
4497
4498 /* This matches the pushlevel in begin_template_parm_list. */
4499 finish_scope ();
4500
4501 --processing_template_decl;
4502 current_template_parms = TREE_CHAIN (current_template_parms);
4503 }
4504
4505 /* Takes a TREE_LIST representing a template parameter and convert it
4506 into an argument suitable to be passed to the type substitution
4507 functions. Note that If the TREE_LIST contains an error_mark
4508 node, the returned argument is error_mark_node. */
4509
4510 tree
4511 template_parm_to_arg (tree t)
4512 {
4513
4514 if (t == NULL_TREE
4515 || TREE_CODE (t) != TREE_LIST)
4516 return t;
4517
4518 if (error_operand_p (TREE_VALUE (t)))
4519 return error_mark_node;
4520
4521 t = TREE_VALUE (t);
4522
4523 if (TREE_CODE (t) == TYPE_DECL
4524 || TREE_CODE (t) == TEMPLATE_DECL)
4525 {
4526 t = TREE_TYPE (t);
4527
4528 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4529 {
4530 /* Turn this argument into a TYPE_ARGUMENT_PACK
4531 with a single element, which expands T. */
4532 tree vec = make_tree_vec (1);
4533 if (CHECKING_P)
4534 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4535
4536 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4537
4538 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4539 SET_ARGUMENT_PACK_ARGS (t, vec);
4540 }
4541 }
4542 else
4543 {
4544 t = DECL_INITIAL (t);
4545
4546 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4547 {
4548 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4549 with a single element, which expands T. */
4550 tree vec = make_tree_vec (1);
4551 if (CHECKING_P)
4552 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4553
4554 t = convert_from_reference (t);
4555 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4556
4557 t = make_node (NONTYPE_ARGUMENT_PACK);
4558 SET_ARGUMENT_PACK_ARGS (t, vec);
4559 }
4560 else
4561 t = convert_from_reference (t);
4562 }
4563 return t;
4564 }
4565
4566 /* Given a single level of template parameters (a TREE_VEC), return it
4567 as a set of template arguments. */
4568
4569 static tree
4570 template_parms_level_to_args (tree parms)
4571 {
4572 tree a = copy_node (parms);
4573 TREE_TYPE (a) = NULL_TREE;
4574 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4575 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4576
4577 if (CHECKING_P)
4578 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4579
4580 return a;
4581 }
4582
4583 /* Given a set of template parameters, return them as a set of template
4584 arguments. The template parameters are represented as a TREE_VEC, in
4585 the form documented in cp-tree.h for template arguments. */
4586
4587 static tree
4588 template_parms_to_args (tree parms)
4589 {
4590 tree header;
4591 tree args = NULL_TREE;
4592 int length = TMPL_PARMS_DEPTH (parms);
4593 int l = length;
4594
4595 /* If there is only one level of template parameters, we do not
4596 create a TREE_VEC of TREE_VECs. Instead, we return a single
4597 TREE_VEC containing the arguments. */
4598 if (length > 1)
4599 args = make_tree_vec (length);
4600
4601 for (header = parms; header; header = TREE_CHAIN (header))
4602 {
4603 tree a = template_parms_level_to_args (TREE_VALUE (header));
4604
4605 if (length > 1)
4606 TREE_VEC_ELT (args, --l) = a;
4607 else
4608 args = a;
4609 }
4610
4611 return args;
4612 }
4613
4614 /* Within the declaration of a template, return the currently active
4615 template parameters as an argument TREE_VEC. */
4616
4617 static tree
4618 current_template_args (void)
4619 {
4620 return template_parms_to_args (current_template_parms);
4621 }
4622
4623 /* Update the declared TYPE by doing any lookups which were thought to be
4624 dependent, but are not now that we know the SCOPE of the declarator. */
4625
4626 tree
4627 maybe_update_decl_type (tree orig_type, tree scope)
4628 {
4629 tree type = orig_type;
4630
4631 if (type == NULL_TREE)
4632 return type;
4633
4634 if (TREE_CODE (orig_type) == TYPE_DECL)
4635 type = TREE_TYPE (type);
4636
4637 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4638 && dependent_type_p (type)
4639 /* Don't bother building up the args in this case. */
4640 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4641 {
4642 /* tsubst in the args corresponding to the template parameters,
4643 including auto if present. Most things will be unchanged, but
4644 make_typename_type and tsubst_qualified_id will resolve
4645 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4646 tree args = current_template_args ();
4647 tree auto_node = type_uses_auto (type);
4648 tree pushed;
4649 if (auto_node)
4650 {
4651 tree auto_vec = make_tree_vec (1);
4652 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4653 args = add_to_template_args (args, auto_vec);
4654 }
4655 pushed = push_scope (scope);
4656 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4657 if (pushed)
4658 pop_scope (scope);
4659 }
4660
4661 if (type == error_mark_node)
4662 return orig_type;
4663
4664 if (TREE_CODE (orig_type) == TYPE_DECL)
4665 {
4666 if (same_type_p (type, TREE_TYPE (orig_type)))
4667 type = orig_type;
4668 else
4669 type = TYPE_NAME (type);
4670 }
4671 return type;
4672 }
4673
4674 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4675 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4676 the new template is a member template. */
4677
4678 static tree
4679 build_template_decl (tree decl, tree parms, bool member_template_p)
4680 {
4681 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4682 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4683 DECL_TEMPLATE_PARMS (tmpl) = parms;
4684 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4685 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4686 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4687
4688 return tmpl;
4689 }
4690
4691 struct template_parm_data
4692 {
4693 /* The level of the template parameters we are currently
4694 processing. */
4695 int level;
4696
4697 /* The index of the specialization argument we are currently
4698 processing. */
4699 int current_arg;
4700
4701 /* An array whose size is the number of template parameters. The
4702 elements are nonzero if the parameter has been used in any one
4703 of the arguments processed so far. */
4704 int* parms;
4705
4706 /* An array whose size is the number of template arguments. The
4707 elements are nonzero if the argument makes use of template
4708 parameters of this level. */
4709 int* arg_uses_template_parms;
4710 };
4711
4712 /* Subroutine of push_template_decl used to see if each template
4713 parameter in a partial specialization is used in the explicit
4714 argument list. If T is of the LEVEL given in DATA (which is
4715 treated as a template_parm_data*), then DATA->PARMS is marked
4716 appropriately. */
4717
4718 static int
4719 mark_template_parm (tree t, void* data)
4720 {
4721 int level;
4722 int idx;
4723 struct template_parm_data* tpd = (struct template_parm_data*) data;
4724
4725 template_parm_level_and_index (t, &level, &idx);
4726
4727 if (level == tpd->level)
4728 {
4729 tpd->parms[idx] = 1;
4730 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4731 }
4732
4733 /* In C++17 the type of a non-type argument is a deduced context. */
4734 if (cxx_dialect >= cxx17
4735 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4736 for_each_template_parm (TREE_TYPE (t),
4737 &mark_template_parm,
4738 data,
4739 NULL,
4740 /*include_nondeduced_p=*/false);
4741
4742 /* Return zero so that for_each_template_parm will continue the
4743 traversal of the tree; we want to mark *every* template parm. */
4744 return 0;
4745 }
4746
4747 /* Process the partial specialization DECL. */
4748
4749 static tree
4750 process_partial_specialization (tree decl)
4751 {
4752 tree type = TREE_TYPE (decl);
4753 tree tinfo = get_template_info (decl);
4754 tree maintmpl = TI_TEMPLATE (tinfo);
4755 tree specargs = TI_ARGS (tinfo);
4756 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4757 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4758 tree inner_parms;
4759 tree inst;
4760 int nargs = TREE_VEC_LENGTH (inner_args);
4761 int ntparms;
4762 int i;
4763 bool did_error_intro = false;
4764 struct template_parm_data tpd;
4765 struct template_parm_data tpd2;
4766
4767 gcc_assert (current_template_parms);
4768
4769 /* A concept cannot be specialized. */
4770 if (flag_concepts && variable_concept_p (maintmpl))
4771 {
4772 error ("specialization of variable concept %q#D", maintmpl);
4773 return error_mark_node;
4774 }
4775
4776 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4777 ntparms = TREE_VEC_LENGTH (inner_parms);
4778
4779 /* We check that each of the template parameters given in the
4780 partial specialization is used in the argument list to the
4781 specialization. For example:
4782
4783 template <class T> struct S;
4784 template <class T> struct S<T*>;
4785
4786 The second declaration is OK because `T*' uses the template
4787 parameter T, whereas
4788
4789 template <class T> struct S<int>;
4790
4791 is no good. Even trickier is:
4792
4793 template <class T>
4794 struct S1
4795 {
4796 template <class U>
4797 struct S2;
4798 template <class U>
4799 struct S2<T>;
4800 };
4801
4802 The S2<T> declaration is actually invalid; it is a
4803 full-specialization. Of course,
4804
4805 template <class U>
4806 struct S2<T (*)(U)>;
4807
4808 or some such would have been OK. */
4809 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4810 tpd.parms = XALLOCAVEC (int, ntparms);
4811 memset (tpd.parms, 0, sizeof (int) * ntparms);
4812
4813 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4814 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4815 for (i = 0; i < nargs; ++i)
4816 {
4817 tpd.current_arg = i;
4818 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4819 &mark_template_parm,
4820 &tpd,
4821 NULL,
4822 /*include_nondeduced_p=*/false);
4823 }
4824 for (i = 0; i < ntparms; ++i)
4825 if (tpd.parms[i] == 0)
4826 {
4827 /* One of the template parms was not used in a deduced context in the
4828 specialization. */
4829 if (!did_error_intro)
4830 {
4831 error ("template parameters not deducible in "
4832 "partial specialization:");
4833 did_error_intro = true;
4834 }
4835
4836 inform (input_location, " %qD",
4837 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4838 }
4839
4840 if (did_error_intro)
4841 return error_mark_node;
4842
4843 /* [temp.class.spec]
4844
4845 The argument list of the specialization shall not be identical to
4846 the implicit argument list of the primary template. */
4847 tree main_args
4848 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4849 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4850 && (!flag_concepts
4851 || !strictly_subsumes (current_template_constraints (),
4852 get_constraints (maintmpl))))
4853 {
4854 if (!flag_concepts)
4855 error ("partial specialization %q+D does not specialize "
4856 "any template arguments; to define the primary template, "
4857 "remove the template argument list", decl);
4858 else
4859 error ("partial specialization %q+D does not specialize any "
4860 "template arguments and is not more constrained than "
4861 "the primary template; to define the primary template, "
4862 "remove the template argument list", decl);
4863 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4864 }
4865
4866 /* A partial specialization that replaces multiple parameters of the
4867 primary template with a pack expansion is less specialized for those
4868 parameters. */
4869 if (nargs < DECL_NTPARMS (maintmpl))
4870 {
4871 error ("partial specialization is not more specialized than the "
4872 "primary template because it replaces multiple parameters "
4873 "with a pack expansion");
4874 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4875 /* Avoid crash in process_partial_specialization. */
4876 return decl;
4877 }
4878
4879 /* If we aren't in a dependent class, we can actually try deduction. */
4880 else if (tpd.level == 1
4881 /* FIXME we should be able to handle a partial specialization of a
4882 partial instantiation, but currently we can't (c++/41727). */
4883 && TMPL_ARGS_DEPTH (specargs) == 1
4884 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4885 {
4886 auto_diagnostic_group d;
4887 if (permerror (input_location, "partial specialization %qD is not "
4888 "more specialized than", decl))
4889 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4890 maintmpl);
4891 }
4892
4893 /* [temp.class.spec]
4894
4895 A partially specialized non-type argument expression shall not
4896 involve template parameters of the partial specialization except
4897 when the argument expression is a simple identifier.
4898
4899 The type of a template parameter corresponding to a specialized
4900 non-type argument shall not be dependent on a parameter of the
4901 specialization.
4902
4903 Also, we verify that pack expansions only occur at the
4904 end of the argument list. */
4905 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4906 tpd2.parms = 0;
4907 for (i = 0; i < nargs; ++i)
4908 {
4909 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4910 tree arg = TREE_VEC_ELT (inner_args, i);
4911 tree packed_args = NULL_TREE;
4912 int j, len = 1;
4913
4914 if (ARGUMENT_PACK_P (arg))
4915 {
4916 /* Extract the arguments from the argument pack. We'll be
4917 iterating over these in the following loop. */
4918 packed_args = ARGUMENT_PACK_ARGS (arg);
4919 len = TREE_VEC_LENGTH (packed_args);
4920 }
4921
4922 for (j = 0; j < len; j++)
4923 {
4924 if (packed_args)
4925 /* Get the Jth argument in the parameter pack. */
4926 arg = TREE_VEC_ELT (packed_args, j);
4927
4928 if (PACK_EXPANSION_P (arg))
4929 {
4930 /* Pack expansions must come at the end of the
4931 argument list. */
4932 if ((packed_args && j < len - 1)
4933 || (!packed_args && i < nargs - 1))
4934 {
4935 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4936 error ("parameter pack argument %qE must be at the "
4937 "end of the template argument list", arg);
4938 else
4939 error ("parameter pack argument %qT must be at the "
4940 "end of the template argument list", arg);
4941 }
4942 }
4943
4944 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4945 /* We only care about the pattern. */
4946 arg = PACK_EXPANSION_PATTERN (arg);
4947
4948 if (/* These first two lines are the `non-type' bit. */
4949 !TYPE_P (arg)
4950 && TREE_CODE (arg) != TEMPLATE_DECL
4951 /* This next two lines are the `argument expression is not just a
4952 simple identifier' condition and also the `specialized
4953 non-type argument' bit. */
4954 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4955 && !(REFERENCE_REF_P (arg)
4956 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4957 {
4958 if ((!packed_args && tpd.arg_uses_template_parms[i])
4959 || (packed_args && uses_template_parms (arg)))
4960 error ("template argument %qE involves template parameter(s)",
4961 arg);
4962 else
4963 {
4964 /* Look at the corresponding template parameter,
4965 marking which template parameters its type depends
4966 upon. */
4967 tree type = TREE_TYPE (parm);
4968
4969 if (!tpd2.parms)
4970 {
4971 /* We haven't yet initialized TPD2. Do so now. */
4972 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4973 /* The number of parameters here is the number in the
4974 main template, which, as checked in the assertion
4975 above, is NARGS. */
4976 tpd2.parms = XALLOCAVEC (int, nargs);
4977 tpd2.level =
4978 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4979 }
4980
4981 /* Mark the template parameters. But this time, we're
4982 looking for the template parameters of the main
4983 template, not in the specialization. */
4984 tpd2.current_arg = i;
4985 tpd2.arg_uses_template_parms[i] = 0;
4986 memset (tpd2.parms, 0, sizeof (int) * nargs);
4987 for_each_template_parm (type,
4988 &mark_template_parm,
4989 &tpd2,
4990 NULL,
4991 /*include_nondeduced_p=*/false);
4992
4993 if (tpd2.arg_uses_template_parms [i])
4994 {
4995 /* The type depended on some template parameters.
4996 If they are fully specialized in the
4997 specialization, that's OK. */
4998 int j;
4999 int count = 0;
5000 for (j = 0; j < nargs; ++j)
5001 if (tpd2.parms[j] != 0
5002 && tpd.arg_uses_template_parms [j])
5003 ++count;
5004 if (count != 0)
5005 error_n (input_location, count,
5006 "type %qT of template argument %qE depends "
5007 "on a template parameter",
5008 "type %qT of template argument %qE depends "
5009 "on template parameters",
5010 type,
5011 arg);
5012 }
5013 }
5014 }
5015 }
5016 }
5017
5018 /* We should only get here once. */
5019 if (TREE_CODE (decl) == TYPE_DECL)
5020 gcc_assert (!COMPLETE_TYPE_P (type));
5021
5022 // Build the template decl.
5023 tree tmpl = build_template_decl (decl, current_template_parms,
5024 DECL_MEMBER_TEMPLATE_P (maintmpl));
5025 TREE_TYPE (tmpl) = type;
5026 DECL_TEMPLATE_RESULT (tmpl) = decl;
5027 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5028 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5029 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5030
5031 /* Give template template parms a DECL_CONTEXT of the template
5032 for which they are a parameter. */
5033 for (i = 0; i < ntparms; ++i)
5034 {
5035 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5036 if (TREE_CODE (parm) == TEMPLATE_DECL)
5037 DECL_CONTEXT (parm) = tmpl;
5038 }
5039
5040 if (VAR_P (decl))
5041 /* We didn't register this in check_explicit_specialization so we could
5042 wait until the constraints were set. */
5043 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5044 else
5045 associate_classtype_constraints (type);
5046
5047 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5048 = tree_cons (specargs, tmpl,
5049 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5050 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5051
5052 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5053 inst = TREE_CHAIN (inst))
5054 {
5055 tree instance = TREE_VALUE (inst);
5056 if (TYPE_P (instance)
5057 ? (COMPLETE_TYPE_P (instance)
5058 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5059 : DECL_TEMPLATE_INSTANTIATION (instance))
5060 {
5061 tree spec = most_specialized_partial_spec (instance, tf_none);
5062 tree inst_decl = (DECL_P (instance)
5063 ? instance : TYPE_NAME (instance));
5064 if (!spec)
5065 /* OK */;
5066 else if (spec == error_mark_node)
5067 permerror (input_location,
5068 "declaration of %qD ambiguates earlier template "
5069 "instantiation for %qD", decl, inst_decl);
5070 else if (TREE_VALUE (spec) == tmpl)
5071 permerror (input_location,
5072 "partial specialization of %qD after instantiation "
5073 "of %qD", decl, inst_decl);
5074 }
5075 }
5076
5077 return decl;
5078 }
5079
5080 /* PARM is a template parameter of some form; return the corresponding
5081 TEMPLATE_PARM_INDEX. */
5082
5083 static tree
5084 get_template_parm_index (tree parm)
5085 {
5086 if (TREE_CODE (parm) == PARM_DECL
5087 || TREE_CODE (parm) == CONST_DECL)
5088 parm = DECL_INITIAL (parm);
5089 else if (TREE_CODE (parm) == TYPE_DECL
5090 || TREE_CODE (parm) == TEMPLATE_DECL)
5091 parm = TREE_TYPE (parm);
5092 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5093 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5094 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5095 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5096 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5097 return parm;
5098 }
5099
5100 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5101 parameter packs used by the template parameter PARM. */
5102
5103 static void
5104 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5105 {
5106 /* A type parm can't refer to another parm. */
5107 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5108 return;
5109 else if (TREE_CODE (parm) == PARM_DECL)
5110 {
5111 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5112 ppd, ppd->visited);
5113 return;
5114 }
5115
5116 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5117
5118 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5119 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5120 {
5121 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5122 if (template_parameter_pack_p (p))
5123 /* Any packs in the type are expanded by this parameter. */;
5124 else
5125 fixed_parameter_pack_p_1 (p, ppd);
5126 }
5127 }
5128
5129 /* PARM is a template parameter pack. Return any parameter packs used in
5130 its type or the type of any of its template parameters. If there are
5131 any such packs, it will be instantiated into a fixed template parameter
5132 list by partial instantiation rather than be fully deduced. */
5133
5134 tree
5135 fixed_parameter_pack_p (tree parm)
5136 {
5137 /* This can only be true in a member template. */
5138 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5139 return NULL_TREE;
5140 /* This can only be true for a parameter pack. */
5141 if (!template_parameter_pack_p (parm))
5142 return NULL_TREE;
5143 /* A type parm can't refer to another parm. */
5144 if (TREE_CODE (parm) == TYPE_DECL)
5145 return NULL_TREE;
5146
5147 tree parameter_packs = NULL_TREE;
5148 struct find_parameter_pack_data ppd;
5149 ppd.parameter_packs = &parameter_packs;
5150 ppd.visited = new hash_set<tree>;
5151 ppd.type_pack_expansion_p = false;
5152
5153 fixed_parameter_pack_p_1 (parm, &ppd);
5154
5155 delete ppd.visited;
5156 return parameter_packs;
5157 }
5158
5159 /* Check that a template declaration's use of default arguments and
5160 parameter packs is not invalid. Here, PARMS are the template
5161 parameters. IS_PRIMARY is true if DECL is the thing declared by
5162 a primary template. IS_PARTIAL is true if DECL is a partial
5163 specialization.
5164
5165 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5166 function template declaration or a friend class template
5167 declaration. In the function case, 1 indicates a declaration, 2
5168 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5169 emitted for extraneous default arguments.
5170
5171 Returns TRUE if there were no errors found, FALSE otherwise. */
5172
5173 bool
5174 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5175 bool is_partial, int is_friend_decl)
5176 {
5177 const char *msg;
5178 int last_level_to_check;
5179 tree parm_level;
5180 bool no_errors = true;
5181
5182 /* [temp.param]
5183
5184 A default template-argument shall not be specified in a
5185 function template declaration or a function template definition, nor
5186 in the template-parameter-list of the definition of a member of a
5187 class template. */
5188
5189 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5190 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5191 /* You can't have a function template declaration in a local
5192 scope, nor you can you define a member of a class template in a
5193 local scope. */
5194 return true;
5195
5196 if ((TREE_CODE (decl) == TYPE_DECL
5197 && TREE_TYPE (decl)
5198 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5199 || (TREE_CODE (decl) == FUNCTION_DECL
5200 && LAMBDA_FUNCTION_P (decl)))
5201 /* A lambda doesn't have an explicit declaration; don't complain
5202 about the parms of the enclosing class. */
5203 return true;
5204
5205 if (current_class_type
5206 && !TYPE_BEING_DEFINED (current_class_type)
5207 && DECL_LANG_SPECIFIC (decl)
5208 && DECL_DECLARES_FUNCTION_P (decl)
5209 /* If this is either a friend defined in the scope of the class
5210 or a member function. */
5211 && (DECL_FUNCTION_MEMBER_P (decl)
5212 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5213 : DECL_FRIEND_CONTEXT (decl)
5214 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5215 : false)
5216 /* And, if it was a member function, it really was defined in
5217 the scope of the class. */
5218 && (!DECL_FUNCTION_MEMBER_P (decl)
5219 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5220 /* We already checked these parameters when the template was
5221 declared, so there's no need to do it again now. This function
5222 was defined in class scope, but we're processing its body now
5223 that the class is complete. */
5224 return true;
5225
5226 /* Core issue 226 (C++0x only): the following only applies to class
5227 templates. */
5228 if (is_primary
5229 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5230 {
5231 /* [temp.param]
5232
5233 If a template-parameter has a default template-argument, all
5234 subsequent template-parameters shall have a default
5235 template-argument supplied. */
5236 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5237 {
5238 tree inner_parms = TREE_VALUE (parm_level);
5239 int ntparms = TREE_VEC_LENGTH (inner_parms);
5240 int seen_def_arg_p = 0;
5241 int i;
5242
5243 for (i = 0; i < ntparms; ++i)
5244 {
5245 tree parm = TREE_VEC_ELT (inner_parms, i);
5246
5247 if (parm == error_mark_node)
5248 continue;
5249
5250 if (TREE_PURPOSE (parm))
5251 seen_def_arg_p = 1;
5252 else if (seen_def_arg_p
5253 && !template_parameter_pack_p (TREE_VALUE (parm)))
5254 {
5255 error ("no default argument for %qD", TREE_VALUE (parm));
5256 /* For better subsequent error-recovery, we indicate that
5257 there should have been a default argument. */
5258 TREE_PURPOSE (parm) = error_mark_node;
5259 no_errors = false;
5260 }
5261 else if (!is_partial
5262 && !is_friend_decl
5263 /* Don't complain about an enclosing partial
5264 specialization. */
5265 && parm_level == parms
5266 && TREE_CODE (decl) == TYPE_DECL
5267 && i < ntparms - 1
5268 && template_parameter_pack_p (TREE_VALUE (parm))
5269 /* A fixed parameter pack will be partially
5270 instantiated into a fixed length list. */
5271 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5272 {
5273 /* A primary class template can only have one
5274 parameter pack, at the end of the template
5275 parameter list. */
5276
5277 error ("parameter pack %q+D must be at the end of the"
5278 " template parameter list", TREE_VALUE (parm));
5279
5280 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5281 = error_mark_node;
5282 no_errors = false;
5283 }
5284 }
5285 }
5286 }
5287
5288 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5289 || is_partial
5290 || !is_primary
5291 || is_friend_decl)
5292 /* For an ordinary class template, default template arguments are
5293 allowed at the innermost level, e.g.:
5294 template <class T = int>
5295 struct S {};
5296 but, in a partial specialization, they're not allowed even
5297 there, as we have in [temp.class.spec]:
5298
5299 The template parameter list of a specialization shall not
5300 contain default template argument values.
5301
5302 So, for a partial specialization, or for a function template
5303 (in C++98/C++03), we look at all of them. */
5304 ;
5305 else
5306 /* But, for a primary class template that is not a partial
5307 specialization we look at all template parameters except the
5308 innermost ones. */
5309 parms = TREE_CHAIN (parms);
5310
5311 /* Figure out what error message to issue. */
5312 if (is_friend_decl == 2)
5313 msg = G_("default template arguments may not be used in function template "
5314 "friend re-declaration");
5315 else if (is_friend_decl)
5316 msg = G_("default template arguments may not be used in template "
5317 "friend declarations");
5318 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5319 msg = G_("default template arguments may not be used in function templates "
5320 "without %<-std=c++11%> or %<-std=gnu++11%>");
5321 else if (is_partial)
5322 msg = G_("default template arguments may not be used in "
5323 "partial specializations");
5324 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5325 msg = G_("default argument for template parameter for class enclosing %qD");
5326 else
5327 /* Per [temp.param]/9, "A default template-argument shall not be
5328 specified in the template-parameter-lists of the definition of
5329 a member of a class template that appears outside of the member's
5330 class.", thus if we aren't handling a member of a class template
5331 there is no need to examine the parameters. */
5332 return true;
5333
5334 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5335 /* If we're inside a class definition, there's no need to
5336 examine the parameters to the class itself. On the one
5337 hand, they will be checked when the class is defined, and,
5338 on the other, default arguments are valid in things like:
5339 template <class T = double>
5340 struct S { template <class U> void f(U); };
5341 Here the default argument for `S' has no bearing on the
5342 declaration of `f'. */
5343 last_level_to_check = template_class_depth (current_class_type) + 1;
5344 else
5345 /* Check everything. */
5346 last_level_to_check = 0;
5347
5348 for (parm_level = parms;
5349 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5350 parm_level = TREE_CHAIN (parm_level))
5351 {
5352 tree inner_parms = TREE_VALUE (parm_level);
5353 int i;
5354 int ntparms;
5355
5356 ntparms = TREE_VEC_LENGTH (inner_parms);
5357 for (i = 0; i < ntparms; ++i)
5358 {
5359 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5360 continue;
5361
5362 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5363 {
5364 if (msg)
5365 {
5366 no_errors = false;
5367 if (is_friend_decl == 2)
5368 return no_errors;
5369
5370 error (msg, decl);
5371 msg = 0;
5372 }
5373
5374 /* Clear out the default argument so that we are not
5375 confused later. */
5376 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5377 }
5378 }
5379
5380 /* At this point, if we're still interested in issuing messages,
5381 they must apply to classes surrounding the object declared. */
5382 if (msg)
5383 msg = G_("default argument for template parameter for class "
5384 "enclosing %qD");
5385 }
5386
5387 return no_errors;
5388 }
5389
5390 /* Worker for push_template_decl_real, called via
5391 for_each_template_parm. DATA is really an int, indicating the
5392 level of the parameters we are interested in. If T is a template
5393 parameter of that level, return nonzero. */
5394
5395 static int
5396 template_parm_this_level_p (tree t, void* data)
5397 {
5398 int this_level = *(int *)data;
5399 int level;
5400
5401 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5402 level = TEMPLATE_PARM_LEVEL (t);
5403 else
5404 level = TEMPLATE_TYPE_LEVEL (t);
5405 return level == this_level;
5406 }
5407
5408 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5409 DATA is really an int, indicating the innermost outer level of parameters.
5410 If T is a template parameter of that level or further out, return
5411 nonzero. */
5412
5413 static int
5414 template_parm_outer_level (tree t, void *data)
5415 {
5416 int this_level = *(int *)data;
5417 int level;
5418
5419 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5420 level = TEMPLATE_PARM_LEVEL (t);
5421 else
5422 level = TEMPLATE_TYPE_LEVEL (t);
5423 return level <= this_level;
5424 }
5425
5426 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5427 parameters given by current_template_args, or reuses a
5428 previously existing one, if appropriate. Returns the DECL, or an
5429 equivalent one, if it is replaced via a call to duplicate_decls.
5430
5431 If IS_FRIEND is true, DECL is a friend declaration. */
5432
5433 tree
5434 push_template_decl_real (tree decl, bool is_friend)
5435 {
5436 tree tmpl;
5437 tree args;
5438 tree info;
5439 tree ctx;
5440 bool is_primary;
5441 bool is_partial;
5442 int new_template_p = 0;
5443 /* True if the template is a member template, in the sense of
5444 [temp.mem]. */
5445 bool member_template_p = false;
5446
5447 if (decl == error_mark_node || !current_template_parms)
5448 return error_mark_node;
5449
5450 /* See if this is a partial specialization. */
5451 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5452 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5453 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5454 || (VAR_P (decl)
5455 && DECL_LANG_SPECIFIC (decl)
5456 && DECL_TEMPLATE_SPECIALIZATION (decl)
5457 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5458
5459 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5460 is_friend = true;
5461
5462 if (is_friend)
5463 /* For a friend, we want the context of the friend, not
5464 the type of which it is a friend. */
5465 ctx = CP_DECL_CONTEXT (decl);
5466 else if (CP_DECL_CONTEXT (decl)
5467 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5468 /* In the case of a virtual function, we want the class in which
5469 it is defined. */
5470 ctx = CP_DECL_CONTEXT (decl);
5471 else
5472 /* Otherwise, if we're currently defining some class, the DECL
5473 is assumed to be a member of the class. */
5474 ctx = current_scope ();
5475
5476 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5477 ctx = NULL_TREE;
5478
5479 if (!DECL_CONTEXT (decl))
5480 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5481
5482 /* See if this is a primary template. */
5483 if (is_friend && ctx
5484 && uses_template_parms_level (ctx, processing_template_decl))
5485 /* A friend template that specifies a class context, i.e.
5486 template <typename T> friend void A<T>::f();
5487 is not primary. */
5488 is_primary = false;
5489 else if (TREE_CODE (decl) == TYPE_DECL
5490 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5491 is_primary = false;
5492 else
5493 is_primary = template_parm_scope_p ();
5494
5495 if (is_primary)
5496 {
5497 warning (OPT_Wtemplates, "template %qD declared", decl);
5498
5499 if (DECL_CLASS_SCOPE_P (decl))
5500 member_template_p = true;
5501 if (TREE_CODE (decl) == TYPE_DECL
5502 && anon_aggrname_p (DECL_NAME (decl)))
5503 {
5504 error ("template class without a name");
5505 return error_mark_node;
5506 }
5507 else if (TREE_CODE (decl) == FUNCTION_DECL)
5508 {
5509 if (member_template_p)
5510 {
5511 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5512 error ("member template %qD may not have virt-specifiers", decl);
5513 }
5514 if (DECL_DESTRUCTOR_P (decl))
5515 {
5516 /* [temp.mem]
5517
5518 A destructor shall not be a member template. */
5519 error ("destructor %qD declared as member template", decl);
5520 return error_mark_node;
5521 }
5522 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5523 && (!prototype_p (TREE_TYPE (decl))
5524 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5525 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5526 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5527 == void_list_node)))
5528 {
5529 /* [basic.stc.dynamic.allocation]
5530
5531 An allocation function can be a function
5532 template. ... Template allocation functions shall
5533 have two or more parameters. */
5534 error ("invalid template declaration of %qD", decl);
5535 return error_mark_node;
5536 }
5537 }
5538 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5539 && CLASS_TYPE_P (TREE_TYPE (decl)))
5540 {
5541 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5542 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5543 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5544 {
5545 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5546 if (TREE_CODE (t) == TYPE_DECL)
5547 t = TREE_TYPE (t);
5548 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5549 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5550 }
5551 }
5552 else if (TREE_CODE (decl) == TYPE_DECL
5553 && TYPE_DECL_ALIAS_P (decl))
5554 /* alias-declaration */
5555 gcc_assert (!DECL_ARTIFICIAL (decl));
5556 else if (VAR_P (decl))
5557 /* C++14 variable template. */;
5558 else
5559 {
5560 error ("template declaration of %q#D", decl);
5561 return error_mark_node;
5562 }
5563 }
5564
5565 /* Check to see that the rules regarding the use of default
5566 arguments are not being violated. We check args for a friend
5567 functions when we know whether it's a definition, introducing
5568 declaration or re-declaration. */
5569 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5570 check_default_tmpl_args (decl, current_template_parms,
5571 is_primary, is_partial, is_friend);
5572
5573 /* Ensure that there are no parameter packs in the type of this
5574 declaration that have not been expanded. */
5575 if (TREE_CODE (decl) == FUNCTION_DECL)
5576 {
5577 /* Check each of the arguments individually to see if there are
5578 any bare parameter packs. */
5579 tree type = TREE_TYPE (decl);
5580 tree arg = DECL_ARGUMENTS (decl);
5581 tree argtype = TYPE_ARG_TYPES (type);
5582
5583 while (arg && argtype)
5584 {
5585 if (!DECL_PACK_P (arg)
5586 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5587 {
5588 /* This is a PARM_DECL that contains unexpanded parameter
5589 packs. We have already complained about this in the
5590 check_for_bare_parameter_packs call, so just replace
5591 these types with ERROR_MARK_NODE. */
5592 TREE_TYPE (arg) = error_mark_node;
5593 TREE_VALUE (argtype) = error_mark_node;
5594 }
5595
5596 arg = DECL_CHAIN (arg);
5597 argtype = TREE_CHAIN (argtype);
5598 }
5599
5600 /* Check for bare parameter packs in the return type and the
5601 exception specifiers. */
5602 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5603 /* Errors were already issued, set return type to int
5604 as the frontend doesn't expect error_mark_node as
5605 the return type. */
5606 TREE_TYPE (type) = integer_type_node;
5607 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5608 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5609 }
5610 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5611 && TYPE_DECL_ALIAS_P (decl))
5612 ? DECL_ORIGINAL_TYPE (decl)
5613 : TREE_TYPE (decl)))
5614 {
5615 TREE_TYPE (decl) = error_mark_node;
5616 return error_mark_node;
5617 }
5618
5619 if (is_partial)
5620 return process_partial_specialization (decl);
5621
5622 args = current_template_args ();
5623
5624 if (!ctx
5625 || TREE_CODE (ctx) == FUNCTION_DECL
5626 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5627 || (TREE_CODE (decl) == TYPE_DECL
5628 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5629 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5630 {
5631 if (DECL_LANG_SPECIFIC (decl)
5632 && DECL_TEMPLATE_INFO (decl)
5633 && DECL_TI_TEMPLATE (decl))
5634 tmpl = DECL_TI_TEMPLATE (decl);
5635 /* If DECL is a TYPE_DECL for a class-template, then there won't
5636 be DECL_LANG_SPECIFIC. The information equivalent to
5637 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5638 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5639 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5640 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5641 {
5642 /* Since a template declaration already existed for this
5643 class-type, we must be redeclaring it here. Make sure
5644 that the redeclaration is valid. */
5645 redeclare_class_template (TREE_TYPE (decl),
5646 current_template_parms,
5647 current_template_constraints ());
5648 /* We don't need to create a new TEMPLATE_DECL; just use the
5649 one we already had. */
5650 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5651 }
5652 else
5653 {
5654 tmpl = build_template_decl (decl, current_template_parms,
5655 member_template_p);
5656 new_template_p = 1;
5657
5658 if (DECL_LANG_SPECIFIC (decl)
5659 && DECL_TEMPLATE_SPECIALIZATION (decl))
5660 {
5661 /* A specialization of a member template of a template
5662 class. */
5663 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5664 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5665 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5666 }
5667 }
5668 }
5669 else
5670 {
5671 tree a, t, current, parms;
5672 int i;
5673 tree tinfo = get_template_info (decl);
5674
5675 if (!tinfo)
5676 {
5677 error ("template definition of non-template %q#D", decl);
5678 return error_mark_node;
5679 }
5680
5681 tmpl = TI_TEMPLATE (tinfo);
5682
5683 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5684 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5685 && DECL_TEMPLATE_SPECIALIZATION (decl)
5686 && DECL_MEMBER_TEMPLATE_P (tmpl))
5687 {
5688 tree new_tmpl;
5689
5690 /* The declaration is a specialization of a member
5691 template, declared outside the class. Therefore, the
5692 innermost template arguments will be NULL, so we
5693 replace them with the arguments determined by the
5694 earlier call to check_explicit_specialization. */
5695 args = DECL_TI_ARGS (decl);
5696
5697 new_tmpl
5698 = build_template_decl (decl, current_template_parms,
5699 member_template_p);
5700 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5701 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5702 DECL_TI_TEMPLATE (decl) = new_tmpl;
5703 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5704 DECL_TEMPLATE_INFO (new_tmpl)
5705 = build_template_info (tmpl, args);
5706
5707 register_specialization (new_tmpl,
5708 most_general_template (tmpl),
5709 args,
5710 is_friend, 0);
5711 return decl;
5712 }
5713
5714 /* Make sure the template headers we got make sense. */
5715
5716 parms = DECL_TEMPLATE_PARMS (tmpl);
5717 i = TMPL_PARMS_DEPTH (parms);
5718 if (TMPL_ARGS_DEPTH (args) != i)
5719 {
5720 error ("expected %d levels of template parms for %q#D, got %d",
5721 i, decl, TMPL_ARGS_DEPTH (args));
5722 DECL_INTERFACE_KNOWN (decl) = 1;
5723 return error_mark_node;
5724 }
5725 else
5726 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5727 {
5728 a = TMPL_ARGS_LEVEL (args, i);
5729 t = INNERMOST_TEMPLATE_PARMS (parms);
5730
5731 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5732 {
5733 if (current == decl)
5734 error ("got %d template parameters for %q#D",
5735 TREE_VEC_LENGTH (a), decl);
5736 else
5737 error ("got %d template parameters for %q#T",
5738 TREE_VEC_LENGTH (a), current);
5739 error (" but %d required", TREE_VEC_LENGTH (t));
5740 /* Avoid crash in import_export_decl. */
5741 DECL_INTERFACE_KNOWN (decl) = 1;
5742 return error_mark_node;
5743 }
5744
5745 if (current == decl)
5746 current = ctx;
5747 else if (current == NULL_TREE)
5748 /* Can happen in erroneous input. */
5749 break;
5750 else
5751 current = get_containing_scope (current);
5752 }
5753
5754 /* Check that the parms are used in the appropriate qualifying scopes
5755 in the declarator. */
5756 if (!comp_template_args
5757 (TI_ARGS (tinfo),
5758 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5759 {
5760 error ("template arguments to %qD do not match original "
5761 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5762 if (!uses_template_parms (TI_ARGS (tinfo)))
5763 inform (input_location, "use %<template<>%> for"
5764 " an explicit specialization");
5765 /* Avoid crash in import_export_decl. */
5766 DECL_INTERFACE_KNOWN (decl) = 1;
5767 return error_mark_node;
5768 }
5769 }
5770
5771 DECL_TEMPLATE_RESULT (tmpl) = decl;
5772 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5773
5774 /* Push template declarations for global functions and types. Note
5775 that we do not try to push a global template friend declared in a
5776 template class; such a thing may well depend on the template
5777 parameters of the class. */
5778 if (new_template_p && !ctx
5779 && !(is_friend && template_class_depth (current_class_type) > 0))
5780 {
5781 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5782 if (tmpl == error_mark_node)
5783 return error_mark_node;
5784
5785 /* Hide template friend classes that haven't been declared yet. */
5786 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5787 {
5788 DECL_ANTICIPATED (tmpl) = 1;
5789 DECL_FRIEND_P (tmpl) = 1;
5790 }
5791 }
5792
5793 if (is_primary)
5794 {
5795 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5796
5797 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5798
5799 /* Give template template parms a DECL_CONTEXT of the template
5800 for which they are a parameter. */
5801 parms = INNERMOST_TEMPLATE_PARMS (parms);
5802 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5803 {
5804 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5805 if (TREE_CODE (parm) == TEMPLATE_DECL)
5806 DECL_CONTEXT (parm) = tmpl;
5807 }
5808
5809 if (TREE_CODE (decl) == TYPE_DECL
5810 && TYPE_DECL_ALIAS_P (decl)
5811 && complex_alias_template_p (tmpl))
5812 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5813 }
5814
5815 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5816 back to its most general template. If TMPL is a specialization,
5817 ARGS may only have the innermost set of arguments. Add the missing
5818 argument levels if necessary. */
5819 if (DECL_TEMPLATE_INFO (tmpl))
5820 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5821
5822 info = build_template_info (tmpl, args);
5823
5824 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5825 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5826 else
5827 {
5828 if (is_primary)
5829 retrofit_lang_decl (decl);
5830 if (DECL_LANG_SPECIFIC (decl))
5831 DECL_TEMPLATE_INFO (decl) = info;
5832 }
5833
5834 if (flag_implicit_templates
5835 && !is_friend
5836 && TREE_PUBLIC (decl)
5837 && VAR_OR_FUNCTION_DECL_P (decl))
5838 /* Set DECL_COMDAT on template instantiations; if we force
5839 them to be emitted by explicit instantiation or -frepo,
5840 mark_needed will tell cgraph to do the right thing. */
5841 DECL_COMDAT (decl) = true;
5842
5843 return DECL_TEMPLATE_RESULT (tmpl);
5844 }
5845
5846 tree
5847 push_template_decl (tree decl)
5848 {
5849 return push_template_decl_real (decl, false);
5850 }
5851
5852 /* FN is an inheriting constructor that inherits from the constructor
5853 template INHERITED; turn FN into a constructor template with a matching
5854 template header. */
5855
5856 tree
5857 add_inherited_template_parms (tree fn, tree inherited)
5858 {
5859 tree inner_parms
5860 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5861 inner_parms = copy_node (inner_parms);
5862 tree parms
5863 = tree_cons (size_int (processing_template_decl + 1),
5864 inner_parms, current_template_parms);
5865 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5866 tree args = template_parms_to_args (parms);
5867 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5868 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5869 DECL_TEMPLATE_RESULT (tmpl) = fn;
5870 DECL_ARTIFICIAL (tmpl) = true;
5871 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5872 return tmpl;
5873 }
5874
5875 /* Called when a class template TYPE is redeclared with the indicated
5876 template PARMS, e.g.:
5877
5878 template <class T> struct S;
5879 template <class T> struct S {}; */
5880
5881 bool
5882 redeclare_class_template (tree type, tree parms, tree cons)
5883 {
5884 tree tmpl;
5885 tree tmpl_parms;
5886 int i;
5887
5888 if (!TYPE_TEMPLATE_INFO (type))
5889 {
5890 error ("%qT is not a template type", type);
5891 return false;
5892 }
5893
5894 tmpl = TYPE_TI_TEMPLATE (type);
5895 if (!PRIMARY_TEMPLATE_P (tmpl))
5896 /* The type is nested in some template class. Nothing to worry
5897 about here; there are no new template parameters for the nested
5898 type. */
5899 return true;
5900
5901 if (!parms)
5902 {
5903 error ("template specifiers not specified in declaration of %qD",
5904 tmpl);
5905 return false;
5906 }
5907
5908 parms = INNERMOST_TEMPLATE_PARMS (parms);
5909 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5910
5911 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5912 {
5913 error_n (input_location, TREE_VEC_LENGTH (parms),
5914 "redeclared with %d template parameter",
5915 "redeclared with %d template parameters",
5916 TREE_VEC_LENGTH (parms));
5917 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5918 "previous declaration %qD used %d template parameter",
5919 "previous declaration %qD used %d template parameters",
5920 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5921 return false;
5922 }
5923
5924 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5925 {
5926 tree tmpl_parm;
5927 tree parm;
5928 tree tmpl_default;
5929 tree parm_default;
5930
5931 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5932 || TREE_VEC_ELT (parms, i) == error_mark_node)
5933 continue;
5934
5935 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5936 if (error_operand_p (tmpl_parm))
5937 return false;
5938
5939 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5940 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5941 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5942
5943 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5944 TEMPLATE_DECL. */
5945 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5946 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5947 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5948 || (TREE_CODE (tmpl_parm) != PARM_DECL
5949 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5950 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5951 || (TREE_CODE (tmpl_parm) == PARM_DECL
5952 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5953 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5954 {
5955 error ("template parameter %q+#D", tmpl_parm);
5956 error ("redeclared here as %q#D", parm);
5957 return false;
5958 }
5959
5960 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5961 {
5962 /* We have in [temp.param]:
5963
5964 A template-parameter may not be given default arguments
5965 by two different declarations in the same scope. */
5966 error_at (input_location, "redefinition of default argument for %q#D", parm);
5967 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5968 "original definition appeared here");
5969 return false;
5970 }
5971
5972 if (parm_default != NULL_TREE)
5973 /* Update the previous template parameters (which are the ones
5974 that will really count) with the new default value. */
5975 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5976 else if (tmpl_default != NULL_TREE)
5977 /* Update the new parameters, too; they'll be used as the
5978 parameters for any members. */
5979 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5980
5981 /* Give each template template parm in this redeclaration a
5982 DECL_CONTEXT of the template for which they are a parameter. */
5983 if (TREE_CODE (parm) == TEMPLATE_DECL)
5984 {
5985 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5986 DECL_CONTEXT (parm) = tmpl;
5987 }
5988
5989 if (TREE_CODE (parm) == TYPE_DECL)
5990 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5991 }
5992
5993 // Cannot redeclare a class template with a different set of constraints.
5994 if (!equivalent_constraints (get_constraints (tmpl), cons))
5995 {
5996 error_at (input_location, "redeclaration %q#D with different "
5997 "constraints", tmpl);
5998 inform (DECL_SOURCE_LOCATION (tmpl),
5999 "original declaration appeared here");
6000 }
6001
6002 return true;
6003 }
6004
6005 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6006 to be used when the caller has already checked
6007 (processing_template_decl
6008 && !instantiation_dependent_expression_p (expr)
6009 && potential_constant_expression (expr))
6010 and cleared processing_template_decl. */
6011
6012 tree
6013 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6014 {
6015 return tsubst_copy_and_build (expr,
6016 /*args=*/NULL_TREE,
6017 complain,
6018 /*in_decl=*/NULL_TREE,
6019 /*function_p=*/false,
6020 /*integral_constant_expression_p=*/true);
6021 }
6022
6023 /* Simplify EXPR if it is a non-dependent expression. Returns the
6024 (possibly simplified) expression. */
6025
6026 tree
6027 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6028 {
6029 if (expr == NULL_TREE)
6030 return NULL_TREE;
6031
6032 /* If we're in a template, but EXPR isn't value dependent, simplify
6033 it. We're supposed to treat:
6034
6035 template <typename T> void f(T[1 + 1]);
6036 template <typename T> void f(T[2]);
6037
6038 as two declarations of the same function, for example. */
6039 if (processing_template_decl
6040 && is_nondependent_constant_expression (expr))
6041 {
6042 processing_template_decl_sentinel s;
6043 expr = instantiate_non_dependent_expr_internal (expr, complain);
6044 }
6045 return expr;
6046 }
6047
6048 tree
6049 instantiate_non_dependent_expr (tree expr)
6050 {
6051 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6052 }
6053
6054 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6055 an uninstantiated expression. */
6056
6057 tree
6058 instantiate_non_dependent_or_null (tree expr)
6059 {
6060 if (expr == NULL_TREE)
6061 return NULL_TREE;
6062 if (processing_template_decl)
6063 {
6064 if (!is_nondependent_constant_expression (expr))
6065 expr = NULL_TREE;
6066 else
6067 {
6068 processing_template_decl_sentinel s;
6069 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6070 }
6071 }
6072 return expr;
6073 }
6074
6075 /* True iff T is a specialization of a variable template. */
6076
6077 bool
6078 variable_template_specialization_p (tree t)
6079 {
6080 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6081 return false;
6082 tree tmpl = DECL_TI_TEMPLATE (t);
6083 return variable_template_p (tmpl);
6084 }
6085
6086 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6087 template declaration, or a TYPE_DECL for an alias declaration. */
6088
6089 bool
6090 alias_type_or_template_p (tree t)
6091 {
6092 if (t == NULL_TREE)
6093 return false;
6094 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6095 || (TYPE_P (t)
6096 && TYPE_NAME (t)
6097 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6098 || DECL_ALIAS_TEMPLATE_P (t));
6099 }
6100
6101 /* Return TRUE iff T is a specialization of an alias template. */
6102
6103 bool
6104 alias_template_specialization_p (const_tree t)
6105 {
6106 /* It's an alias template specialization if it's an alias and its
6107 TYPE_NAME is a specialization of a primary template. */
6108 if (TYPE_ALIAS_P (t))
6109 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6110 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6111
6112 return false;
6113 }
6114
6115 /* An alias template is complex from a SFINAE perspective if a template-id
6116 using that alias can be ill-formed when the expansion is not, as with
6117 the void_t template. We determine this by checking whether the
6118 expansion for the alias template uses all its template parameters. */
6119
6120 struct uses_all_template_parms_data
6121 {
6122 int level;
6123 bool *seen;
6124 };
6125
6126 static int
6127 uses_all_template_parms_r (tree t, void *data_)
6128 {
6129 struct uses_all_template_parms_data &data
6130 = *(struct uses_all_template_parms_data*)data_;
6131 tree idx = get_template_parm_index (t);
6132
6133 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6134 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6135 return 0;
6136 }
6137
6138 static bool
6139 complex_alias_template_p (const_tree tmpl)
6140 {
6141 struct uses_all_template_parms_data data;
6142 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6143 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6144 data.level = TMPL_PARMS_DEPTH (parms);
6145 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6146 data.seen = XALLOCAVEC (bool, len);
6147 for (int i = 0; i < len; ++i)
6148 data.seen[i] = false;
6149
6150 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6151 for (int i = 0; i < len; ++i)
6152 if (!data.seen[i])
6153 return true;
6154 return false;
6155 }
6156
6157 /* Return TRUE iff T is a specialization of a complex alias template with
6158 dependent template-arguments. */
6159
6160 bool
6161 dependent_alias_template_spec_p (const_tree t)
6162 {
6163 if (!alias_template_specialization_p (t))
6164 return false;
6165
6166 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6167 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6168 return false;
6169
6170 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6171 if (!any_dependent_template_arguments_p (args))
6172 return false;
6173
6174 return true;
6175 }
6176
6177 /* Return the number of innermost template parameters in TMPL. */
6178
6179 static int
6180 num_innermost_template_parms (tree tmpl)
6181 {
6182 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6183 return TREE_VEC_LENGTH (parms);
6184 }
6185
6186 /* Return either TMPL or another template that it is equivalent to under DR
6187 1286: An alias that just changes the name of a template is equivalent to
6188 the other template. */
6189
6190 static tree
6191 get_underlying_template (tree tmpl)
6192 {
6193 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6194 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6195 {
6196 /* Determine if the alias is equivalent to an underlying template. */
6197 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6198 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6199 if (!tinfo)
6200 break;
6201
6202 tree underlying = TI_TEMPLATE (tinfo);
6203 if (!PRIMARY_TEMPLATE_P (underlying)
6204 || (num_innermost_template_parms (tmpl)
6205 != num_innermost_template_parms (underlying)))
6206 break;
6207
6208 tree alias_args = INNERMOST_TEMPLATE_ARGS
6209 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6210 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6211 break;
6212
6213 /* Alias is equivalent. Strip it and repeat. */
6214 tmpl = underlying;
6215 }
6216
6217 return tmpl;
6218 }
6219
6220 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6221 must be a reference-to-function or a pointer-to-function type, as specified
6222 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6223 and check that the resulting function has external linkage. */
6224
6225 static tree
6226 convert_nontype_argument_function (tree type, tree expr,
6227 tsubst_flags_t complain)
6228 {
6229 tree fns = expr;
6230 tree fn, fn_no_ptr;
6231 linkage_kind linkage;
6232
6233 fn = instantiate_type (type, fns, tf_none);
6234 if (fn == error_mark_node)
6235 return error_mark_node;
6236
6237 if (value_dependent_expression_p (fn))
6238 goto accept;
6239
6240 fn_no_ptr = strip_fnptr_conv (fn);
6241 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6242 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6243 if (BASELINK_P (fn_no_ptr))
6244 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6245
6246 /* [temp.arg.nontype]/1
6247
6248 A template-argument for a non-type, non-template template-parameter
6249 shall be one of:
6250 [...]
6251 -- the address of an object or function with external [C++11: or
6252 internal] linkage. */
6253
6254 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6255 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6256 {
6257 if (complain & tf_error)
6258 {
6259 error ("%qE is not a valid template argument for type %qT",
6260 expr, type);
6261 if (TYPE_PTR_P (type))
6262 inform (input_location, "it must be the address of a function "
6263 "with external linkage");
6264 else
6265 inform (input_location, "it must be the name of a function with "
6266 "external linkage");
6267 }
6268 return NULL_TREE;
6269 }
6270
6271 linkage = decl_linkage (fn_no_ptr);
6272 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6273 {
6274 if (complain & tf_error)
6275 {
6276 if (cxx_dialect >= cxx11)
6277 error ("%qE is not a valid template argument for type %qT "
6278 "because %qD has no linkage",
6279 expr, type, fn_no_ptr);
6280 else
6281 error ("%qE is not a valid template argument for type %qT "
6282 "because %qD does not have external linkage",
6283 expr, type, fn_no_ptr);
6284 }
6285 return NULL_TREE;
6286 }
6287
6288 accept:
6289 if (TYPE_REF_P (type))
6290 {
6291 if (REFERENCE_REF_P (fn))
6292 fn = TREE_OPERAND (fn, 0);
6293 else
6294 fn = build_address (fn);
6295 }
6296 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6297 fn = build_nop (type, fn);
6298
6299 return fn;
6300 }
6301
6302 /* Subroutine of convert_nontype_argument.
6303 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6304 Emit an error otherwise. */
6305
6306 static bool
6307 check_valid_ptrmem_cst_expr (tree type, tree expr,
6308 tsubst_flags_t complain)
6309 {
6310 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6311 tree orig_expr = expr;
6312 STRIP_NOPS (expr);
6313 if (null_ptr_cst_p (expr))
6314 return true;
6315 if (TREE_CODE (expr) == PTRMEM_CST
6316 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6317 PTRMEM_CST_CLASS (expr)))
6318 return true;
6319 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6320 return true;
6321 if (processing_template_decl
6322 && TREE_CODE (expr) == ADDR_EXPR
6323 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6324 return true;
6325 if (complain & tf_error)
6326 {
6327 error_at (loc, "%qE is not a valid template argument for type %qT",
6328 orig_expr, type);
6329 if (TREE_CODE (expr) != PTRMEM_CST)
6330 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6331 else
6332 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6333 }
6334 return false;
6335 }
6336
6337 /* Returns TRUE iff the address of OP is value-dependent.
6338
6339 14.6.2.4 [temp.dep.temp]:
6340 A non-integral non-type template-argument is dependent if its type is
6341 dependent or it has either of the following forms
6342 qualified-id
6343 & qualified-id
6344 and contains a nested-name-specifier which specifies a class-name that
6345 names a dependent type.
6346
6347 We generalize this to just say that the address of a member of a
6348 dependent class is value-dependent; the above doesn't cover the
6349 address of a static data member named with an unqualified-id. */
6350
6351 static bool
6352 has_value_dependent_address (tree op)
6353 {
6354 /* We could use get_inner_reference here, but there's no need;
6355 this is only relevant for template non-type arguments, which
6356 can only be expressed as &id-expression. */
6357 if (DECL_P (op))
6358 {
6359 tree ctx = CP_DECL_CONTEXT (op);
6360 if (TYPE_P (ctx) && dependent_type_p (ctx))
6361 return true;
6362 }
6363
6364 return false;
6365 }
6366
6367 /* The next set of functions are used for providing helpful explanatory
6368 diagnostics for failed overload resolution. Their messages should be
6369 indented by two spaces for consistency with the messages in
6370 call.c */
6371
6372 static int
6373 unify_success (bool /*explain_p*/)
6374 {
6375 return 0;
6376 }
6377
6378 /* Other failure functions should call this one, to provide a single function
6379 for setting a breakpoint on. */
6380
6381 static int
6382 unify_invalid (bool /*explain_p*/)
6383 {
6384 return 1;
6385 }
6386
6387 static int
6388 unify_parameter_deduction_failure (bool explain_p, tree parm)
6389 {
6390 if (explain_p)
6391 inform (input_location,
6392 " couldn%'t deduce template parameter %qD", parm);
6393 return unify_invalid (explain_p);
6394 }
6395
6396 static int
6397 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6398 {
6399 if (explain_p)
6400 inform (input_location,
6401 " types %qT and %qT have incompatible cv-qualifiers",
6402 parm, arg);
6403 return unify_invalid (explain_p);
6404 }
6405
6406 static int
6407 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6408 {
6409 if (explain_p)
6410 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6411 return unify_invalid (explain_p);
6412 }
6413
6414 static int
6415 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6416 {
6417 if (explain_p)
6418 inform (input_location,
6419 " template parameter %qD is not a parameter pack, but "
6420 "argument %qD is",
6421 parm, arg);
6422 return unify_invalid (explain_p);
6423 }
6424
6425 static int
6426 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6427 {
6428 if (explain_p)
6429 inform (input_location,
6430 " template argument %qE does not match "
6431 "pointer-to-member constant %qE",
6432 arg, parm);
6433 return unify_invalid (explain_p);
6434 }
6435
6436 static int
6437 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6438 {
6439 if (explain_p)
6440 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6441 return unify_invalid (explain_p);
6442 }
6443
6444 static int
6445 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6446 {
6447 if (explain_p)
6448 inform (input_location,
6449 " inconsistent parameter pack deduction with %qT and %qT",
6450 old_arg, new_arg);
6451 return unify_invalid (explain_p);
6452 }
6453
6454 static int
6455 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6456 {
6457 if (explain_p)
6458 {
6459 if (TYPE_P (parm))
6460 inform (input_location,
6461 " deduced conflicting types for parameter %qT (%qT and %qT)",
6462 parm, first, second);
6463 else
6464 inform (input_location,
6465 " deduced conflicting values for non-type parameter "
6466 "%qE (%qE and %qE)", parm, first, second);
6467 }
6468 return unify_invalid (explain_p);
6469 }
6470
6471 static int
6472 unify_vla_arg (bool explain_p, tree arg)
6473 {
6474 if (explain_p)
6475 inform (input_location,
6476 " variable-sized array type %qT is not "
6477 "a valid template argument",
6478 arg);
6479 return unify_invalid (explain_p);
6480 }
6481
6482 static int
6483 unify_method_type_error (bool explain_p, tree arg)
6484 {
6485 if (explain_p)
6486 inform (input_location,
6487 " member function type %qT is not a valid template argument",
6488 arg);
6489 return unify_invalid (explain_p);
6490 }
6491
6492 static int
6493 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6494 {
6495 if (explain_p)
6496 {
6497 if (least_p)
6498 inform_n (input_location, wanted,
6499 " candidate expects at least %d argument, %d provided",
6500 " candidate expects at least %d arguments, %d provided",
6501 wanted, have);
6502 else
6503 inform_n (input_location, wanted,
6504 " candidate expects %d argument, %d provided",
6505 " candidate expects %d arguments, %d provided",
6506 wanted, have);
6507 }
6508 return unify_invalid (explain_p);
6509 }
6510
6511 static int
6512 unify_too_many_arguments (bool explain_p, int have, int wanted)
6513 {
6514 return unify_arity (explain_p, have, wanted);
6515 }
6516
6517 static int
6518 unify_too_few_arguments (bool explain_p, int have, int wanted,
6519 bool least_p = false)
6520 {
6521 return unify_arity (explain_p, have, wanted, least_p);
6522 }
6523
6524 static int
6525 unify_arg_conversion (bool explain_p, tree to_type,
6526 tree from_type, tree arg)
6527 {
6528 if (explain_p)
6529 inform (cp_expr_loc_or_loc (arg, input_location),
6530 " cannot convert %qE (type %qT) to type %qT",
6531 arg, from_type, to_type);
6532 return unify_invalid (explain_p);
6533 }
6534
6535 static int
6536 unify_no_common_base (bool explain_p, enum template_base_result r,
6537 tree parm, tree arg)
6538 {
6539 if (explain_p)
6540 switch (r)
6541 {
6542 case tbr_ambiguous_baseclass:
6543 inform (input_location, " %qT is an ambiguous base class of %qT",
6544 parm, arg);
6545 break;
6546 default:
6547 inform (input_location, " %qT is not derived from %qT", arg, parm);
6548 break;
6549 }
6550 return unify_invalid (explain_p);
6551 }
6552
6553 static int
6554 unify_inconsistent_template_template_parameters (bool explain_p)
6555 {
6556 if (explain_p)
6557 inform (input_location,
6558 " template parameters of a template template argument are "
6559 "inconsistent with other deduced template arguments");
6560 return unify_invalid (explain_p);
6561 }
6562
6563 static int
6564 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6565 {
6566 if (explain_p)
6567 inform (input_location,
6568 " can%'t deduce a template for %qT from non-template type %qT",
6569 parm, arg);
6570 return unify_invalid (explain_p);
6571 }
6572
6573 static int
6574 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6575 {
6576 if (explain_p)
6577 inform (input_location,
6578 " template argument %qE does not match %qE", arg, parm);
6579 return unify_invalid (explain_p);
6580 }
6581
6582 /* True if T is a C++20 template parameter object to store the argument for a
6583 template parameter of class type. */
6584
6585 bool
6586 template_parm_object_p (const_tree t)
6587 {
6588 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6589 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6590 }
6591
6592 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6593 argument for TYPE, points to an unsuitable object. */
6594
6595 static bool
6596 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6597 {
6598 switch (TREE_CODE (expr))
6599 {
6600 CASE_CONVERT:
6601 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6602 complain);
6603
6604 case TARGET_EXPR:
6605 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6606 complain);
6607
6608 case CONSTRUCTOR:
6609 {
6610 unsigned i; tree elt;
6611 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6612 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6613 return true;
6614 }
6615 break;
6616
6617 case ADDR_EXPR:
6618 {
6619 tree decl = TREE_OPERAND (expr, 0);
6620
6621 if (!VAR_P (decl))
6622 {
6623 if (complain & tf_error)
6624 error ("%qE is not a valid template argument of type %qT "
6625 "because %qE is not a variable", expr, type, decl);
6626 return true;
6627 }
6628 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6629 {
6630 if (complain & tf_error)
6631 error ("%qE is not a valid template argument of type %qT "
6632 "in C++98 because %qD does not have external linkage",
6633 expr, type, decl);
6634 return true;
6635 }
6636 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6637 && decl_linkage (decl) == lk_none)
6638 {
6639 if (complain & tf_error)
6640 error ("%qE is not a valid template argument of type %qT "
6641 "because %qD has no linkage", expr, type, decl);
6642 return true;
6643 }
6644 /* C++17: For a non-type template-parameter of reference or pointer
6645 type, the value of the constant expression shall not refer to (or
6646 for a pointer type, shall not be the address of):
6647 * a subobject (4.5),
6648 * a temporary object (15.2),
6649 * a string literal (5.13.5),
6650 * the result of a typeid expression (8.2.8), or
6651 * a predefined __func__ variable (11.4.1). */
6652 else if (DECL_ARTIFICIAL (decl))
6653 {
6654 if (complain & tf_error)
6655 error ("the address of %qD is not a valid template argument",
6656 decl);
6657 return true;
6658 }
6659 else if (!same_type_ignoring_top_level_qualifiers_p
6660 (strip_array_types (TREE_TYPE (type)),
6661 strip_array_types (TREE_TYPE (decl))))
6662 {
6663 if (complain & tf_error)
6664 error ("the address of the %qT subobject of %qD is not a "
6665 "valid template argument", TREE_TYPE (type), decl);
6666 return true;
6667 }
6668 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6669 {
6670 if (complain & tf_error)
6671 error ("the address of %qD is not a valid template argument "
6672 "because it does not have static storage duration",
6673 decl);
6674 return true;
6675 }
6676 }
6677 break;
6678
6679 default:
6680 if (!INDIRECT_TYPE_P (type))
6681 /* We're only concerned about pointers and references here. */;
6682 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6683 /* Null pointer values are OK in C++11. */;
6684 else
6685 {
6686 if (VAR_P (expr))
6687 {
6688 if (complain & tf_error)
6689 error ("%qD is not a valid template argument "
6690 "because %qD is a variable, not the address of "
6691 "a variable", expr, expr);
6692 return true;
6693 }
6694 else
6695 {
6696 if (complain & tf_error)
6697 error ("%qE is not a valid template argument for %qT "
6698 "because it is not the address of a variable",
6699 expr, type);
6700 return true;
6701 }
6702 }
6703 }
6704 return false;
6705
6706 }
6707
6708 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6709 template argument EXPR. */
6710
6711 static tree
6712 get_template_parm_object (tree expr, tsubst_flags_t complain)
6713 {
6714 if (TREE_CODE (expr) == TARGET_EXPR)
6715 expr = TARGET_EXPR_INITIAL (expr);
6716
6717 if (!TREE_CONSTANT (expr))
6718 {
6719 if ((complain & tf_error)
6720 && require_rvalue_constant_expression (expr))
6721 cxx_constant_value (expr);
6722 return error_mark_node;
6723 }
6724 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6725 return error_mark_node;
6726
6727 tree name = mangle_template_parm_object (expr);
6728 tree decl = get_global_binding (name);
6729 if (decl)
6730 return decl;
6731
6732 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6733 decl = create_temporary_var (type);
6734 TREE_STATIC (decl) = true;
6735 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6736 TREE_READONLY (decl) = true;
6737 DECL_NAME (decl) = name;
6738 SET_DECL_ASSEMBLER_NAME (decl, name);
6739 DECL_CONTEXT (decl) = global_namespace;
6740 comdat_linkage (decl);
6741 pushdecl_top_level_and_finish (decl, expr);
6742 return decl;
6743 }
6744
6745 /* Attempt to convert the non-type template parameter EXPR to the
6746 indicated TYPE. If the conversion is successful, return the
6747 converted value. If the conversion is unsuccessful, return
6748 NULL_TREE if we issued an error message, or error_mark_node if we
6749 did not. We issue error messages for out-and-out bad template
6750 parameters, but not simply because the conversion failed, since we
6751 might be just trying to do argument deduction. Both TYPE and EXPR
6752 must be non-dependent.
6753
6754 The conversion follows the special rules described in
6755 [temp.arg.nontype], and it is much more strict than an implicit
6756 conversion.
6757
6758 This function is called twice for each template argument (see
6759 lookup_template_class for a more accurate description of this
6760 problem). This means that we need to handle expressions which
6761 are not valid in a C++ source, but can be created from the
6762 first call (for instance, casts to perform conversions). These
6763 hacks can go away after we fix the double coercion problem. */
6764
6765 static tree
6766 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6767 {
6768 tree expr_type;
6769 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6770
6771 /* Detect immediately string literals as invalid non-type argument.
6772 This special-case is not needed for correctness (we would easily
6773 catch this later), but only to provide better diagnostic for this
6774 common user mistake. As suggested by DR 100, we do not mention
6775 linkage issues in the diagnostic as this is not the point. */
6776 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6777 {
6778 if (complain & tf_error)
6779 error ("%qE is not a valid template argument for type %qT "
6780 "because string literals can never be used in this context",
6781 expr, type);
6782 return NULL_TREE;
6783 }
6784
6785 /* Add the ADDR_EXPR now for the benefit of
6786 value_dependent_expression_p. */
6787 if (TYPE_PTROBV_P (type)
6788 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6789 {
6790 expr = decay_conversion (expr, complain);
6791 if (expr == error_mark_node)
6792 return error_mark_node;
6793 }
6794
6795 /* If we are in a template, EXPR may be non-dependent, but still
6796 have a syntactic, rather than semantic, form. For example, EXPR
6797 might be a SCOPE_REF, rather than the VAR_DECL to which the
6798 SCOPE_REF refers. Preserving the qualifying scope is necessary
6799 so that access checking can be performed when the template is
6800 instantiated -- but here we need the resolved form so that we can
6801 convert the argument. */
6802 bool non_dep = false;
6803 if (TYPE_REF_OBJ_P (type)
6804 && has_value_dependent_address (expr))
6805 /* If we want the address and it's value-dependent, don't fold. */;
6806 else if (processing_template_decl
6807 && is_nondependent_constant_expression (expr))
6808 non_dep = true;
6809 if (error_operand_p (expr))
6810 return error_mark_node;
6811 expr_type = TREE_TYPE (expr);
6812
6813 /* If the argument is non-dependent, perform any conversions in
6814 non-dependent context as well. */
6815 processing_template_decl_sentinel s (non_dep);
6816 if (non_dep)
6817 expr = instantiate_non_dependent_expr_internal (expr, complain);
6818
6819 if (value_dependent_expression_p (expr))
6820 expr = canonicalize_expr_argument (expr, complain);
6821
6822 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6823 to a non-type argument of "nullptr". */
6824 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6825 expr = fold_simple (convert (type, expr));
6826
6827 /* In C++11, integral or enumeration non-type template arguments can be
6828 arbitrary constant expressions. Pointer and pointer to
6829 member arguments can be general constant expressions that evaluate
6830 to a null value, but otherwise still need to be of a specific form. */
6831 if (cxx_dialect >= cxx11)
6832 {
6833 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6834 /* A PTRMEM_CST is already constant, and a valid template
6835 argument for a parameter of pointer to member type, we just want
6836 to leave it in that form rather than lower it to a
6837 CONSTRUCTOR. */;
6838 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6839 || cxx_dialect >= cxx17)
6840 {
6841 /* Calling build_converted_constant_expr might create a call to
6842 a conversion function with a value-dependent argument, which
6843 could invoke taking the address of a temporary representing
6844 the result of the conversion. */
6845 if (COMPOUND_LITERAL_P (expr)
6846 && CONSTRUCTOR_IS_DEPENDENT (expr)
6847 && MAYBE_CLASS_TYPE_P (expr_type)
6848 && TYPE_HAS_CONVERSION (expr_type))
6849 {
6850 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
6851 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
6852 return expr;
6853 }
6854 /* C++17: A template-argument for a non-type template-parameter shall
6855 be a converted constant expression (8.20) of the type of the
6856 template-parameter. */
6857 expr = build_converted_constant_expr (type, expr, complain);
6858 if (expr == error_mark_node)
6859 /* Make sure we return NULL_TREE only if we have really issued
6860 an error, as described above. */
6861 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6862 expr = maybe_constant_value (expr, NULL_TREE,
6863 /*manifestly_const_eval=*/true);
6864 expr = convert_from_reference (expr);
6865 }
6866 else if (TYPE_PTR_OR_PTRMEM_P (type))
6867 {
6868 tree folded = maybe_constant_value (expr, NULL_TREE,
6869 /*manifestly_const_eval=*/true);
6870 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6871 : null_member_pointer_value_p (folded))
6872 expr = folded;
6873 }
6874 }
6875
6876 if (TYPE_REF_P (type))
6877 expr = mark_lvalue_use (expr);
6878 else
6879 expr = mark_rvalue_use (expr);
6880
6881 /* HACK: Due to double coercion, we can get a
6882 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6883 which is the tree that we built on the first call (see
6884 below when coercing to reference to object or to reference to
6885 function). We just strip everything and get to the arg.
6886 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6887 for examples. */
6888 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6889 {
6890 tree probe_type, probe = expr;
6891 if (REFERENCE_REF_P (probe))
6892 probe = TREE_OPERAND (probe, 0);
6893 probe_type = TREE_TYPE (probe);
6894 if (TREE_CODE (probe) == NOP_EXPR)
6895 {
6896 /* ??? Maybe we could use convert_from_reference here, but we
6897 would need to relax its constraints because the NOP_EXPR
6898 could actually change the type to something more cv-qualified,
6899 and this is not folded by convert_from_reference. */
6900 tree addr = TREE_OPERAND (probe, 0);
6901 if (TYPE_REF_P (probe_type)
6902 && TREE_CODE (addr) == ADDR_EXPR
6903 && TYPE_PTR_P (TREE_TYPE (addr))
6904 && (same_type_ignoring_top_level_qualifiers_p
6905 (TREE_TYPE (probe_type),
6906 TREE_TYPE (TREE_TYPE (addr)))))
6907 {
6908 expr = TREE_OPERAND (addr, 0);
6909 expr_type = TREE_TYPE (probe_type);
6910 }
6911 }
6912 }
6913
6914 /* [temp.arg.nontype]/5, bullet 1
6915
6916 For a non-type template-parameter of integral or enumeration type,
6917 integral promotions (_conv.prom_) and integral conversions
6918 (_conv.integral_) are applied. */
6919 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6920 {
6921 if (cxx_dialect < cxx11)
6922 {
6923 tree t = build_converted_constant_expr (type, expr, complain);
6924 t = maybe_constant_value (t);
6925 if (t != error_mark_node)
6926 expr = t;
6927 }
6928
6929 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6930 return error_mark_node;
6931
6932 /* Notice that there are constant expressions like '4 % 0' which
6933 do not fold into integer constants. */
6934 if (TREE_CODE (expr) != INTEGER_CST
6935 && !value_dependent_expression_p (expr))
6936 {
6937 if (complain & tf_error)
6938 {
6939 int errs = errorcount, warns = warningcount + werrorcount;
6940 if (!require_potential_constant_expression (expr))
6941 expr = error_mark_node;
6942 else
6943 expr = cxx_constant_value (expr);
6944 if (errorcount > errs || warningcount + werrorcount > warns)
6945 inform (loc, "in template argument for type %qT", type);
6946 if (expr == error_mark_node)
6947 return NULL_TREE;
6948 /* else cxx_constant_value complained but gave us
6949 a real constant, so go ahead. */
6950 if (TREE_CODE (expr) != INTEGER_CST)
6951 {
6952 /* Some assemble time constant expressions like
6953 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6954 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6955 as we can emit them into .rodata initializers of
6956 variables, yet they can't fold into an INTEGER_CST at
6957 compile time. Refuse them here. */
6958 gcc_checking_assert (reduced_constant_expression_p (expr));
6959 error_at (loc, "template argument %qE for type %qT not "
6960 "a constant integer", expr, type);
6961 return NULL_TREE;
6962 }
6963 }
6964 else
6965 return NULL_TREE;
6966 }
6967
6968 /* Avoid typedef problems. */
6969 if (TREE_TYPE (expr) != type)
6970 expr = fold_convert (type, expr);
6971 }
6972 /* [temp.arg.nontype]/5, bullet 2
6973
6974 For a non-type template-parameter of type pointer to object,
6975 qualification conversions (_conv.qual_) and the array-to-pointer
6976 conversion (_conv.array_) are applied. */
6977 else if (TYPE_PTROBV_P (type))
6978 {
6979 tree decayed = expr;
6980
6981 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6982 decay_conversion or an explicit cast. If it's a problematic cast,
6983 we'll complain about it below. */
6984 if (TREE_CODE (expr) == NOP_EXPR)
6985 {
6986 tree probe = expr;
6987 STRIP_NOPS (probe);
6988 if (TREE_CODE (probe) == ADDR_EXPR
6989 && TYPE_PTR_P (TREE_TYPE (probe)))
6990 {
6991 expr = probe;
6992 expr_type = TREE_TYPE (expr);
6993 }
6994 }
6995
6996 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6997
6998 A template-argument for a non-type, non-template template-parameter
6999 shall be one of: [...]
7000
7001 -- the name of a non-type template-parameter;
7002 -- the address of an object or function with external linkage, [...]
7003 expressed as "& id-expression" where the & is optional if the name
7004 refers to a function or array, or if the corresponding
7005 template-parameter is a reference.
7006
7007 Here, we do not care about functions, as they are invalid anyway
7008 for a parameter of type pointer-to-object. */
7009
7010 if (value_dependent_expression_p (expr))
7011 /* Non-type template parameters are OK. */
7012 ;
7013 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7014 /* Null pointer values are OK in C++11. */;
7015 else if (TREE_CODE (expr) != ADDR_EXPR
7016 && !INDIRECT_TYPE_P (expr_type))
7017 /* Other values, like integer constants, might be valid
7018 non-type arguments of some other type. */
7019 return error_mark_node;
7020 else if (invalid_tparm_referent_p (type, expr, complain))
7021 return NULL_TREE;
7022
7023 expr = decayed;
7024
7025 expr = perform_qualification_conversions (type, expr);
7026 if (expr == error_mark_node)
7027 return error_mark_node;
7028 }
7029 /* [temp.arg.nontype]/5, bullet 3
7030
7031 For a non-type template-parameter of type reference to object, no
7032 conversions apply. The type referred to by the reference may be more
7033 cv-qualified than the (otherwise identical) type of the
7034 template-argument. The template-parameter is bound directly to the
7035 template-argument, which must be an lvalue. */
7036 else if (TYPE_REF_OBJ_P (type))
7037 {
7038 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7039 expr_type))
7040 return error_mark_node;
7041
7042 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7043 {
7044 if (complain & tf_error)
7045 error ("%qE is not a valid template argument for type %qT "
7046 "because of conflicts in cv-qualification", expr, type);
7047 return NULL_TREE;
7048 }
7049
7050 if (!lvalue_p (expr))
7051 {
7052 if (complain & tf_error)
7053 error ("%qE is not a valid template argument for type %qT "
7054 "because it is not an lvalue", expr, type);
7055 return NULL_TREE;
7056 }
7057
7058 /* [temp.arg.nontype]/1
7059
7060 A template-argument for a non-type, non-template template-parameter
7061 shall be one of: [...]
7062
7063 -- the address of an object or function with external linkage. */
7064 if (INDIRECT_REF_P (expr)
7065 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7066 {
7067 expr = TREE_OPERAND (expr, 0);
7068 if (DECL_P (expr))
7069 {
7070 if (complain & tf_error)
7071 error ("%q#D is not a valid template argument for type %qT "
7072 "because a reference variable does not have a constant "
7073 "address", expr, type);
7074 return NULL_TREE;
7075 }
7076 }
7077
7078 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7079 && value_dependent_expression_p (expr))
7080 /* OK, dependent reference. We don't want to ask whether a DECL is
7081 itself value-dependent, since what we want here is its address. */;
7082 else
7083 {
7084 expr = build_address (expr);
7085
7086 if (invalid_tparm_referent_p (type, expr, complain))
7087 return NULL_TREE;
7088 }
7089
7090 if (!same_type_p (type, TREE_TYPE (expr)))
7091 expr = build_nop (type, expr);
7092 }
7093 /* [temp.arg.nontype]/5, bullet 4
7094
7095 For a non-type template-parameter of type pointer to function, only
7096 the function-to-pointer conversion (_conv.func_) is applied. If the
7097 template-argument represents a set of overloaded functions (or a
7098 pointer to such), the matching function is selected from the set
7099 (_over.over_). */
7100 else if (TYPE_PTRFN_P (type))
7101 {
7102 /* If the argument is a template-id, we might not have enough
7103 context information to decay the pointer. */
7104 if (!type_unknown_p (expr_type))
7105 {
7106 expr = decay_conversion (expr, complain);
7107 if (expr == error_mark_node)
7108 return error_mark_node;
7109 }
7110
7111 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7112 /* Null pointer values are OK in C++11. */
7113 return perform_qualification_conversions (type, expr);
7114
7115 expr = convert_nontype_argument_function (type, expr, complain);
7116 if (!expr || expr == error_mark_node)
7117 return expr;
7118 }
7119 /* [temp.arg.nontype]/5, bullet 5
7120
7121 For a non-type template-parameter of type reference to function, no
7122 conversions apply. If the template-argument represents a set of
7123 overloaded functions, the matching function is selected from the set
7124 (_over.over_). */
7125 else if (TYPE_REFFN_P (type))
7126 {
7127 if (TREE_CODE (expr) == ADDR_EXPR)
7128 {
7129 if (complain & tf_error)
7130 {
7131 error ("%qE is not a valid template argument for type %qT "
7132 "because it is a pointer", expr, type);
7133 inform (input_location, "try using %qE instead",
7134 TREE_OPERAND (expr, 0));
7135 }
7136 return NULL_TREE;
7137 }
7138
7139 expr = convert_nontype_argument_function (type, expr, complain);
7140 if (!expr || expr == error_mark_node)
7141 return expr;
7142 }
7143 /* [temp.arg.nontype]/5, bullet 6
7144
7145 For a non-type template-parameter of type pointer to member function,
7146 no conversions apply. If the template-argument represents a set of
7147 overloaded member functions, the matching member function is selected
7148 from the set (_over.over_). */
7149 else if (TYPE_PTRMEMFUNC_P (type))
7150 {
7151 expr = instantiate_type (type, expr, tf_none);
7152 if (expr == error_mark_node)
7153 return error_mark_node;
7154
7155 /* [temp.arg.nontype] bullet 1 says the pointer to member
7156 expression must be a pointer-to-member constant. */
7157 if (!value_dependent_expression_p (expr)
7158 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7159 return NULL_TREE;
7160
7161 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7162 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7163 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7164 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7165 }
7166 /* [temp.arg.nontype]/5, bullet 7
7167
7168 For a non-type template-parameter of type pointer to data member,
7169 qualification conversions (_conv.qual_) are applied. */
7170 else if (TYPE_PTRDATAMEM_P (type))
7171 {
7172 /* [temp.arg.nontype] bullet 1 says the pointer to member
7173 expression must be a pointer-to-member constant. */
7174 if (!value_dependent_expression_p (expr)
7175 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7176 return NULL_TREE;
7177
7178 expr = perform_qualification_conversions (type, expr);
7179 if (expr == error_mark_node)
7180 return expr;
7181 }
7182 else if (NULLPTR_TYPE_P (type))
7183 {
7184 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7185 {
7186 if (complain & tf_error)
7187 error ("%qE is not a valid template argument for type %qT "
7188 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7189 return NULL_TREE;
7190 }
7191 return expr;
7192 }
7193 else if (CLASS_TYPE_P (type))
7194 {
7195 /* Replace the argument with a reference to the corresponding template
7196 parameter object. */
7197 if (!value_dependent_expression_p (expr))
7198 expr = get_template_parm_object (expr, complain);
7199 if (expr == error_mark_node)
7200 return NULL_TREE;
7201 }
7202 /* A template non-type parameter must be one of the above. */
7203 else
7204 gcc_unreachable ();
7205
7206 /* Sanity check: did we actually convert the argument to the
7207 right type? */
7208 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7209 (type, TREE_TYPE (expr)));
7210 return convert_from_reference (expr);
7211 }
7212
7213 /* Subroutine of coerce_template_template_parms, which returns 1 if
7214 PARM_PARM and ARG_PARM match using the rule for the template
7215 parameters of template template parameters. Both PARM and ARG are
7216 template parameters; the rest of the arguments are the same as for
7217 coerce_template_template_parms.
7218 */
7219 static int
7220 coerce_template_template_parm (tree parm,
7221 tree arg,
7222 tsubst_flags_t complain,
7223 tree in_decl,
7224 tree outer_args)
7225 {
7226 if (arg == NULL_TREE || error_operand_p (arg)
7227 || parm == NULL_TREE || error_operand_p (parm))
7228 return 0;
7229
7230 if (TREE_CODE (arg) != TREE_CODE (parm))
7231 return 0;
7232
7233 switch (TREE_CODE (parm))
7234 {
7235 case TEMPLATE_DECL:
7236 /* We encounter instantiations of templates like
7237 template <template <template <class> class> class TT>
7238 class C; */
7239 {
7240 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7241 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7242
7243 if (!coerce_template_template_parms
7244 (parmparm, argparm, complain, in_decl, outer_args))
7245 return 0;
7246 }
7247 /* Fall through. */
7248
7249 case TYPE_DECL:
7250 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7251 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7252 /* Argument is a parameter pack but parameter is not. */
7253 return 0;
7254 break;
7255
7256 case PARM_DECL:
7257 /* The tsubst call is used to handle cases such as
7258
7259 template <int> class C {};
7260 template <class T, template <T> class TT> class D {};
7261 D<int, C> d;
7262
7263 i.e. the parameter list of TT depends on earlier parameters. */
7264 if (!uses_template_parms (TREE_TYPE (arg)))
7265 {
7266 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7267 if (!uses_template_parms (t)
7268 && !same_type_p (t, TREE_TYPE (arg)))
7269 return 0;
7270 }
7271
7272 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7273 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7274 /* Argument is a parameter pack but parameter is not. */
7275 return 0;
7276
7277 break;
7278
7279 default:
7280 gcc_unreachable ();
7281 }
7282
7283 return 1;
7284 }
7285
7286 /* Coerce template argument list ARGLIST for use with template
7287 template-parameter TEMPL. */
7288
7289 static tree
7290 coerce_template_args_for_ttp (tree templ, tree arglist,
7291 tsubst_flags_t complain)
7292 {
7293 /* Consider an example where a template template parameter declared as
7294
7295 template <class T, class U = std::allocator<T> > class TT
7296
7297 The template parameter level of T and U are one level larger than
7298 of TT. To proper process the default argument of U, say when an
7299 instantiation `TT<int>' is seen, we need to build the full
7300 arguments containing {int} as the innermost level. Outer levels,
7301 available when not appearing as default template argument, can be
7302 obtained from the arguments of the enclosing template.
7303
7304 Suppose that TT is later substituted with std::vector. The above
7305 instantiation is `TT<int, std::allocator<T> >' with TT at
7306 level 1, and T at level 2, while the template arguments at level 1
7307 becomes {std::vector} and the inner level 2 is {int}. */
7308
7309 tree outer = DECL_CONTEXT (templ);
7310 if (outer)
7311 {
7312 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7313 /* We want arguments for the partial specialization, not arguments for
7314 the primary template. */
7315 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7316 else
7317 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7318 }
7319 else if (current_template_parms)
7320 {
7321 /* This is an argument of the current template, so we haven't set
7322 DECL_CONTEXT yet. */
7323 tree relevant_template_parms;
7324
7325 /* Parameter levels that are greater than the level of the given
7326 template template parm are irrelevant. */
7327 relevant_template_parms = current_template_parms;
7328 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7329 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7330 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7331
7332 outer = template_parms_to_args (relevant_template_parms);
7333 }
7334
7335 if (outer)
7336 arglist = add_to_template_args (outer, arglist);
7337
7338 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7339 return coerce_template_parms (parmlist, arglist, templ,
7340 complain,
7341 /*require_all_args=*/true,
7342 /*use_default_args=*/true);
7343 }
7344
7345 /* A cache of template template parameters with match-all default
7346 arguments. */
7347 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7348 static void
7349 store_defaulted_ttp (tree v, tree t)
7350 {
7351 if (!defaulted_ttp_cache)
7352 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7353 defaulted_ttp_cache->put (v, t);
7354 }
7355 static tree
7356 lookup_defaulted_ttp (tree v)
7357 {
7358 if (defaulted_ttp_cache)
7359 if (tree *p = defaulted_ttp_cache->get (v))
7360 return *p;
7361 return NULL_TREE;
7362 }
7363
7364 /* T is a bound template template-parameter. Copy its arguments into default
7365 arguments of the template template-parameter's template parameters. */
7366
7367 static tree
7368 add_defaults_to_ttp (tree otmpl)
7369 {
7370 if (tree c = lookup_defaulted_ttp (otmpl))
7371 return c;
7372
7373 tree ntmpl = copy_node (otmpl);
7374
7375 tree ntype = copy_node (TREE_TYPE (otmpl));
7376 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7377 TYPE_MAIN_VARIANT (ntype) = ntype;
7378 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7379 TYPE_NAME (ntype) = ntmpl;
7380 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7381
7382 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7383 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7384 TEMPLATE_PARM_DECL (idx) = ntmpl;
7385 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7386
7387 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7388 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7389 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7390 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7391 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7392 {
7393 tree o = TREE_VEC_ELT (vec, i);
7394 if (!template_parameter_pack_p (TREE_VALUE (o)))
7395 {
7396 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7397 TREE_PURPOSE (n) = any_targ_node;
7398 }
7399 }
7400
7401 store_defaulted_ttp (otmpl, ntmpl);
7402 return ntmpl;
7403 }
7404
7405 /* ARG is a bound potential template template-argument, and PARGS is a list
7406 of arguments for the corresponding template template-parameter. Adjust
7407 PARGS as appropriate for application to ARG's template, and if ARG is a
7408 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7409 arguments to the template template parameter. */
7410
7411 static tree
7412 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7413 {
7414 ++processing_template_decl;
7415 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7417 {
7418 /* When comparing two template template-parameters in partial ordering,
7419 rewrite the one currently being used as an argument to have default
7420 arguments for all parameters. */
7421 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7422 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7423 if (pargs != error_mark_node)
7424 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7425 TYPE_TI_ARGS (arg));
7426 }
7427 else
7428 {
7429 tree aparms
7430 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7431 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7432 /*require_all*/true,
7433 /*use_default*/true);
7434 }
7435 --processing_template_decl;
7436 return pargs;
7437 }
7438
7439 /* Subroutine of unify for the case when PARM is a
7440 BOUND_TEMPLATE_TEMPLATE_PARM. */
7441
7442 static int
7443 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7444 bool explain_p)
7445 {
7446 tree parmvec = TYPE_TI_ARGS (parm);
7447 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7448
7449 /* The template template parm might be variadic and the argument
7450 not, so flatten both argument lists. */
7451 parmvec = expand_template_argument_pack (parmvec);
7452 argvec = expand_template_argument_pack (argvec);
7453
7454 if (flag_new_ttp)
7455 {
7456 /* In keeping with P0522R0, adjust P's template arguments
7457 to apply to A's template; then flatten it again. */
7458 tree nparmvec = parmvec;
7459 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7460 nparmvec = expand_template_argument_pack (nparmvec);
7461
7462 if (unify (tparms, targs, nparmvec, argvec,
7463 UNIFY_ALLOW_NONE, explain_p))
7464 return 1;
7465
7466 /* If the P0522 adjustment eliminated a pack expansion, deduce
7467 empty packs. */
7468 if (flag_new_ttp
7469 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7470 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7471 DEDUCE_EXACT, /*sub*/true, explain_p))
7472 return 1;
7473 }
7474 else
7475 {
7476 /* Deduce arguments T, i from TT<T> or TT<i>.
7477 We check each element of PARMVEC and ARGVEC individually
7478 rather than the whole TREE_VEC since they can have
7479 different number of elements, which is allowed under N2555. */
7480
7481 int len = TREE_VEC_LENGTH (parmvec);
7482
7483 /* Check if the parameters end in a pack, making them
7484 variadic. */
7485 int parm_variadic_p = 0;
7486 if (len > 0
7487 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7488 parm_variadic_p = 1;
7489
7490 for (int i = 0; i < len - parm_variadic_p; ++i)
7491 /* If the template argument list of P contains a pack
7492 expansion that is not the last template argument, the
7493 entire template argument list is a non-deduced
7494 context. */
7495 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7496 return unify_success (explain_p);
7497
7498 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7499 return unify_too_few_arguments (explain_p,
7500 TREE_VEC_LENGTH (argvec), len);
7501
7502 for (int i = 0; i < len - parm_variadic_p; ++i)
7503 if (unify (tparms, targs,
7504 TREE_VEC_ELT (parmvec, i),
7505 TREE_VEC_ELT (argvec, i),
7506 UNIFY_ALLOW_NONE, explain_p))
7507 return 1;
7508
7509 if (parm_variadic_p
7510 && unify_pack_expansion (tparms, targs,
7511 parmvec, argvec,
7512 DEDUCE_EXACT,
7513 /*subr=*/true, explain_p))
7514 return 1;
7515 }
7516
7517 return 0;
7518 }
7519
7520 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7521 template template parameters. Both PARM_PARMS and ARG_PARMS are
7522 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7523 or PARM_DECL.
7524
7525 Consider the example:
7526 template <class T> class A;
7527 template<template <class U> class TT> class B;
7528
7529 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7530 the parameters to A, and OUTER_ARGS contains A. */
7531
7532 static int
7533 coerce_template_template_parms (tree parm_parms,
7534 tree arg_parms,
7535 tsubst_flags_t complain,
7536 tree in_decl,
7537 tree outer_args)
7538 {
7539 int nparms, nargs, i;
7540 tree parm, arg;
7541 int variadic_p = 0;
7542
7543 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7544 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7545
7546 nparms = TREE_VEC_LENGTH (parm_parms);
7547 nargs = TREE_VEC_LENGTH (arg_parms);
7548
7549 if (flag_new_ttp)
7550 {
7551 /* P0522R0: A template template-parameter P is at least as specialized as
7552 a template template-argument A if, given the following rewrite to two
7553 function templates, the function template corresponding to P is at
7554 least as specialized as the function template corresponding to A
7555 according to the partial ordering rules for function templates
7556 ([temp.func.order]). Given an invented class template X with the
7557 template parameter list of A (including default arguments):
7558
7559 * Each of the two function templates has the same template parameters,
7560 respectively, as P or A.
7561
7562 * Each function template has a single function parameter whose type is
7563 a specialization of X with template arguments corresponding to the
7564 template parameters from the respective function template where, for
7565 each template parameter PP in the template parameter list of the
7566 function template, a corresponding template argument AA is formed. If
7567 PP declares a parameter pack, then AA is the pack expansion
7568 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7569
7570 If the rewrite produces an invalid type, then P is not at least as
7571 specialized as A. */
7572
7573 /* So coerce P's args to apply to A's parms, and then deduce between A's
7574 args and the converted args. If that succeeds, A is at least as
7575 specialized as P, so they match.*/
7576 tree pargs = template_parms_level_to_args (parm_parms);
7577 pargs = add_outermost_template_args (outer_args, pargs);
7578 ++processing_template_decl;
7579 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7580 /*require_all*/true, /*use_default*/true);
7581 --processing_template_decl;
7582 if (pargs != error_mark_node)
7583 {
7584 tree targs = make_tree_vec (nargs);
7585 tree aargs = template_parms_level_to_args (arg_parms);
7586 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7587 /*explain*/false))
7588 return 1;
7589 }
7590 }
7591
7592 /* Determine whether we have a parameter pack at the end of the
7593 template template parameter's template parameter list. */
7594 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7595 {
7596 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7597
7598 if (error_operand_p (parm))
7599 return 0;
7600
7601 switch (TREE_CODE (parm))
7602 {
7603 case TEMPLATE_DECL:
7604 case TYPE_DECL:
7605 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7606 variadic_p = 1;
7607 break;
7608
7609 case PARM_DECL:
7610 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7611 variadic_p = 1;
7612 break;
7613
7614 default:
7615 gcc_unreachable ();
7616 }
7617 }
7618
7619 if (nargs != nparms
7620 && !(variadic_p && nargs >= nparms - 1))
7621 return 0;
7622
7623 /* Check all of the template parameters except the parameter pack at
7624 the end (if any). */
7625 for (i = 0; i < nparms - variadic_p; ++i)
7626 {
7627 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7628 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7629 continue;
7630
7631 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7632 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7633
7634 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7635 outer_args))
7636 return 0;
7637
7638 }
7639
7640 if (variadic_p)
7641 {
7642 /* Check each of the template parameters in the template
7643 argument against the template parameter pack at the end of
7644 the template template parameter. */
7645 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7646 return 0;
7647
7648 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7649
7650 for (; i < nargs; ++i)
7651 {
7652 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7653 continue;
7654
7655 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7656
7657 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7658 outer_args))
7659 return 0;
7660 }
7661 }
7662
7663 return 1;
7664 }
7665
7666 /* Verifies that the deduced template arguments (in TARGS) for the
7667 template template parameters (in TPARMS) represent valid bindings,
7668 by comparing the template parameter list of each template argument
7669 to the template parameter list of its corresponding template
7670 template parameter, in accordance with DR150. This
7671 routine can only be called after all template arguments have been
7672 deduced. It will return TRUE if all of the template template
7673 parameter bindings are okay, FALSE otherwise. */
7674 bool
7675 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7676 {
7677 int i, ntparms = TREE_VEC_LENGTH (tparms);
7678 bool ret = true;
7679
7680 /* We're dealing with template parms in this process. */
7681 ++processing_template_decl;
7682
7683 targs = INNERMOST_TEMPLATE_ARGS (targs);
7684
7685 for (i = 0; i < ntparms; ++i)
7686 {
7687 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7688 tree targ = TREE_VEC_ELT (targs, i);
7689
7690 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7691 {
7692 tree packed_args = NULL_TREE;
7693 int idx, len = 1;
7694
7695 if (ARGUMENT_PACK_P (targ))
7696 {
7697 /* Look inside the argument pack. */
7698 packed_args = ARGUMENT_PACK_ARGS (targ);
7699 len = TREE_VEC_LENGTH (packed_args);
7700 }
7701
7702 for (idx = 0; idx < len; ++idx)
7703 {
7704 tree targ_parms = NULL_TREE;
7705
7706 if (packed_args)
7707 /* Extract the next argument from the argument
7708 pack. */
7709 targ = TREE_VEC_ELT (packed_args, idx);
7710
7711 if (PACK_EXPANSION_P (targ))
7712 /* Look at the pattern of the pack expansion. */
7713 targ = PACK_EXPANSION_PATTERN (targ);
7714
7715 /* Extract the template parameters from the template
7716 argument. */
7717 if (TREE_CODE (targ) == TEMPLATE_DECL)
7718 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7719 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7720 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7721
7722 /* Verify that we can coerce the template template
7723 parameters from the template argument to the template
7724 parameter. This requires an exact match. */
7725 if (targ_parms
7726 && !coerce_template_template_parms
7727 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7728 targ_parms,
7729 tf_none,
7730 tparm,
7731 targs))
7732 {
7733 ret = false;
7734 goto out;
7735 }
7736 }
7737 }
7738 }
7739
7740 out:
7741
7742 --processing_template_decl;
7743 return ret;
7744 }
7745
7746 /* Since type attributes aren't mangled, we need to strip them from
7747 template type arguments. */
7748
7749 static tree
7750 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7751 {
7752 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7753 return arg;
7754 bool removed_attributes = false;
7755 tree canon = strip_typedefs (arg, &removed_attributes);
7756 if (removed_attributes
7757 && (complain & tf_warning))
7758 warning (OPT_Wignored_attributes,
7759 "ignoring attributes on template argument %qT", arg);
7760 return canon;
7761 }
7762
7763 /* And from inside dependent non-type arguments like sizeof(Type). */
7764
7765 static tree
7766 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7767 {
7768 if (!arg || arg == error_mark_node)
7769 return arg;
7770 bool removed_attributes = false;
7771 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7772 if (removed_attributes
7773 && (complain & tf_warning))
7774 warning (OPT_Wignored_attributes,
7775 "ignoring attributes in template argument %qE", arg);
7776 return canon;
7777 }
7778
7779 // A template declaration can be substituted for a constrained
7780 // template template parameter only when the argument is more
7781 // constrained than the parameter.
7782 static bool
7783 is_compatible_template_arg (tree parm, tree arg)
7784 {
7785 tree parm_cons = get_constraints (parm);
7786
7787 /* For now, allow constrained template template arguments
7788 and unconstrained template template parameters. */
7789 if (parm_cons == NULL_TREE)
7790 return true;
7791
7792 tree arg_cons = get_constraints (arg);
7793
7794 // If the template parameter is constrained, we need to rewrite its
7795 // constraints in terms of the ARG's template parameters. This ensures
7796 // that all of the template parameter types will have the same depth.
7797 //
7798 // Note that this is only valid when coerce_template_template_parm is
7799 // true for the innermost template parameters of PARM and ARG. In other
7800 // words, because coercion is successful, this conversion will be valid.
7801 if (parm_cons)
7802 {
7803 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7804 parm_cons = tsubst_constraint_info (parm_cons,
7805 INNERMOST_TEMPLATE_ARGS (args),
7806 tf_none, NULL_TREE);
7807 if (parm_cons == error_mark_node)
7808 return false;
7809 }
7810
7811 return subsumes (parm_cons, arg_cons);
7812 }
7813
7814 // Convert a placeholder argument into a binding to the original
7815 // parameter. The original parameter is saved as the TREE_TYPE of
7816 // ARG.
7817 static inline tree
7818 convert_wildcard_argument (tree parm, tree arg)
7819 {
7820 TREE_TYPE (arg) = parm;
7821 return arg;
7822 }
7823
7824 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7825 because one of them is dependent. But we need to represent the
7826 conversion for the benefit of cp_tree_equal. */
7827
7828 static tree
7829 maybe_convert_nontype_argument (tree type, tree arg)
7830 {
7831 /* Auto parms get no conversion. */
7832 if (type_uses_auto (type))
7833 return arg;
7834 /* We don't need or want to add this conversion now if we're going to use the
7835 argument for deduction. */
7836 if (value_dependent_expression_p (arg))
7837 return arg;
7838
7839 type = cv_unqualified (type);
7840 tree argtype = TREE_TYPE (arg);
7841 if (same_type_p (type, argtype))
7842 return arg;
7843
7844 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7845 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7846 return arg;
7847 }
7848
7849 /* Convert the indicated template ARG as necessary to match the
7850 indicated template PARM. Returns the converted ARG, or
7851 error_mark_node if the conversion was unsuccessful. Error and
7852 warning messages are issued under control of COMPLAIN. This
7853 conversion is for the Ith parameter in the parameter list. ARGS is
7854 the full set of template arguments deduced so far. */
7855
7856 static tree
7857 convert_template_argument (tree parm,
7858 tree arg,
7859 tree args,
7860 tsubst_flags_t complain,
7861 int i,
7862 tree in_decl)
7863 {
7864 tree orig_arg;
7865 tree val;
7866 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7867
7868 if (parm == error_mark_node || error_operand_p (arg))
7869 return error_mark_node;
7870
7871 /* Trivially convert placeholders. */
7872 if (TREE_CODE (arg) == WILDCARD_DECL)
7873 return convert_wildcard_argument (parm, arg);
7874
7875 if (arg == any_targ_node)
7876 return arg;
7877
7878 if (TREE_CODE (arg) == TREE_LIST
7879 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7880 {
7881 /* The template argument was the name of some
7882 member function. That's usually
7883 invalid, but static members are OK. In any
7884 case, grab the underlying fields/functions
7885 and issue an error later if required. */
7886 orig_arg = TREE_VALUE (arg);
7887 TREE_TYPE (arg) = unknown_type_node;
7888 }
7889
7890 orig_arg = arg;
7891
7892 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7893 requires_type = (TREE_CODE (parm) == TYPE_DECL
7894 || requires_tmpl_type);
7895
7896 /* When determining whether an argument pack expansion is a template,
7897 look at the pattern. */
7898 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7899 arg = PACK_EXPANSION_PATTERN (arg);
7900
7901 /* Deal with an injected-class-name used as a template template arg. */
7902 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7903 {
7904 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7905 if (TREE_CODE (t) == TEMPLATE_DECL)
7906 {
7907 if (cxx_dialect >= cxx11)
7908 /* OK under DR 1004. */;
7909 else if (complain & tf_warning_or_error)
7910 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7911 " used as template template argument", TYPE_NAME (arg));
7912 else if (flag_pedantic_errors)
7913 t = arg;
7914
7915 arg = t;
7916 }
7917 }
7918
7919 is_tmpl_type =
7920 ((TREE_CODE (arg) == TEMPLATE_DECL
7921 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7922 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7923 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7924 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7925
7926 if (is_tmpl_type
7927 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7928 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7929 arg = TYPE_STUB_DECL (arg);
7930
7931 is_type = TYPE_P (arg) || is_tmpl_type;
7932
7933 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7934 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7935 {
7936 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7937 {
7938 if (complain & tf_error)
7939 error ("invalid use of destructor %qE as a type", orig_arg);
7940 return error_mark_node;
7941 }
7942
7943 permerror (input_location,
7944 "to refer to a type member of a template parameter, "
7945 "use %<typename %E%>", orig_arg);
7946
7947 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7948 TREE_OPERAND (arg, 1),
7949 typename_type,
7950 complain);
7951 arg = orig_arg;
7952 is_type = 1;
7953 }
7954 if (is_type != requires_type)
7955 {
7956 if (in_decl)
7957 {
7958 if (complain & tf_error)
7959 {
7960 error ("type/value mismatch at argument %d in template "
7961 "parameter list for %qD",
7962 i + 1, in_decl);
7963 if (is_type)
7964 inform (input_location,
7965 " expected a constant of type %qT, got %qT",
7966 TREE_TYPE (parm),
7967 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7968 else if (requires_tmpl_type)
7969 inform (input_location,
7970 " expected a class template, got %qE", orig_arg);
7971 else
7972 inform (input_location,
7973 " expected a type, got %qE", orig_arg);
7974 }
7975 }
7976 return error_mark_node;
7977 }
7978 if (is_tmpl_type ^ requires_tmpl_type)
7979 {
7980 if (in_decl && (complain & tf_error))
7981 {
7982 error ("type/value mismatch at argument %d in template "
7983 "parameter list for %qD",
7984 i + 1, in_decl);
7985 if (is_tmpl_type)
7986 inform (input_location,
7987 " expected a type, got %qT", DECL_NAME (arg));
7988 else
7989 inform (input_location,
7990 " expected a class template, got %qT", orig_arg);
7991 }
7992 return error_mark_node;
7993 }
7994
7995 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7996 /* We already did the appropriate conversion when packing args. */
7997 val = orig_arg;
7998 else if (is_type)
7999 {
8000 if (requires_tmpl_type)
8001 {
8002 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8003 /* The number of argument required is not known yet.
8004 Just accept it for now. */
8005 val = orig_arg;
8006 else
8007 {
8008 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8009 tree argparm;
8010
8011 /* Strip alias templates that are equivalent to another
8012 template. */
8013 arg = get_underlying_template (arg);
8014 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8015
8016 if (coerce_template_template_parms (parmparm, argparm,
8017 complain, in_decl,
8018 args))
8019 {
8020 val = arg;
8021
8022 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8023 TEMPLATE_DECL. */
8024 if (val != error_mark_node)
8025 {
8026 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8027 val = TREE_TYPE (val);
8028 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8029 val = make_pack_expansion (val, complain);
8030 }
8031 }
8032 else
8033 {
8034 if (in_decl && (complain & tf_error))
8035 {
8036 error ("type/value mismatch at argument %d in "
8037 "template parameter list for %qD",
8038 i + 1, in_decl);
8039 inform (input_location,
8040 " expected a template of type %qD, got %qT",
8041 parm, orig_arg);
8042 }
8043
8044 val = error_mark_node;
8045 }
8046
8047 // Check that the constraints are compatible before allowing the
8048 // substitution.
8049 if (val != error_mark_node)
8050 if (!is_compatible_template_arg (parm, arg))
8051 {
8052 if (in_decl && (complain & tf_error))
8053 {
8054 error ("constraint mismatch at argument %d in "
8055 "template parameter list for %qD",
8056 i + 1, in_decl);
8057 inform (input_location, " expected %qD but got %qD",
8058 parm, arg);
8059 }
8060 val = error_mark_node;
8061 }
8062 }
8063 }
8064 else
8065 val = orig_arg;
8066 /* We only form one instance of each template specialization.
8067 Therefore, if we use a non-canonical variant (i.e., a
8068 typedef), any future messages referring to the type will use
8069 the typedef, which is confusing if those future uses do not
8070 themselves also use the typedef. */
8071 if (TYPE_P (val))
8072 val = canonicalize_type_argument (val, complain);
8073 }
8074 else
8075 {
8076 tree t = TREE_TYPE (parm);
8077
8078 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8079 > TMPL_ARGS_DEPTH (args))
8080 /* We don't have enough levels of args to do any substitution. This
8081 can happen in the context of -fnew-ttp-matching. */;
8082 else if (tree a = type_uses_auto (t))
8083 {
8084 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8085 if (t == error_mark_node)
8086 return error_mark_node;
8087 }
8088 else
8089 t = tsubst (t, args, complain, in_decl);
8090
8091 if (invalid_nontype_parm_type_p (t, complain))
8092 return error_mark_node;
8093
8094 if (t != TREE_TYPE (parm))
8095 t = canonicalize_type_argument (t, complain);
8096
8097 if (!type_dependent_expression_p (orig_arg)
8098 && !uses_template_parms (t))
8099 /* We used to call digest_init here. However, digest_init
8100 will report errors, which we don't want when complain
8101 is zero. More importantly, digest_init will try too
8102 hard to convert things: for example, `0' should not be
8103 converted to pointer type at this point according to
8104 the standard. Accepting this is not merely an
8105 extension, since deciding whether or not these
8106 conversions can occur is part of determining which
8107 function template to call, or whether a given explicit
8108 argument specification is valid. */
8109 val = convert_nontype_argument (t, orig_arg, complain);
8110 else
8111 {
8112 val = canonicalize_expr_argument (orig_arg, complain);
8113 val = maybe_convert_nontype_argument (t, val);
8114 }
8115
8116
8117 if (val == NULL_TREE)
8118 val = error_mark_node;
8119 else if (val == error_mark_node && (complain & tf_error))
8120 error ("could not convert template argument %qE from %qT to %qT",
8121 orig_arg, TREE_TYPE (orig_arg), t);
8122
8123 if (INDIRECT_REF_P (val))
8124 {
8125 /* Reject template arguments that are references to built-in
8126 functions with no library fallbacks. */
8127 const_tree inner = TREE_OPERAND (val, 0);
8128 const_tree innertype = TREE_TYPE (inner);
8129 if (innertype
8130 && TYPE_REF_P (innertype)
8131 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8132 && TREE_OPERAND_LENGTH (inner) > 0
8133 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8134 return error_mark_node;
8135 }
8136
8137 if (TREE_CODE (val) == SCOPE_REF)
8138 {
8139 /* Strip typedefs from the SCOPE_REF. */
8140 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8141 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8142 complain);
8143 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8144 QUALIFIED_NAME_IS_TEMPLATE (val));
8145 }
8146 }
8147
8148 return val;
8149 }
8150
8151 /* Coerces the remaining template arguments in INNER_ARGS (from
8152 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8153 Returns the coerced argument pack. PARM_IDX is the position of this
8154 parameter in the template parameter list. ARGS is the original
8155 template argument list. */
8156 static tree
8157 coerce_template_parameter_pack (tree parms,
8158 int parm_idx,
8159 tree args,
8160 tree inner_args,
8161 int arg_idx,
8162 tree new_args,
8163 int* lost,
8164 tree in_decl,
8165 tsubst_flags_t complain)
8166 {
8167 tree parm = TREE_VEC_ELT (parms, parm_idx);
8168 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8169 tree packed_args;
8170 tree argument_pack;
8171 tree packed_parms = NULL_TREE;
8172
8173 if (arg_idx > nargs)
8174 arg_idx = nargs;
8175
8176 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8177 {
8178 /* When the template parameter is a non-type template parameter pack
8179 or template template parameter pack whose type or template
8180 parameters use parameter packs, we know exactly how many arguments
8181 we are looking for. Build a vector of the instantiated decls for
8182 these template parameters in PACKED_PARMS. */
8183 /* We can't use make_pack_expansion here because it would interpret a
8184 _DECL as a use rather than a declaration. */
8185 tree decl = TREE_VALUE (parm);
8186 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8187 SET_PACK_EXPANSION_PATTERN (exp, decl);
8188 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8189 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8190
8191 TREE_VEC_LENGTH (args)--;
8192 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8193 TREE_VEC_LENGTH (args)++;
8194
8195 if (packed_parms == error_mark_node)
8196 return error_mark_node;
8197
8198 /* If we're doing a partial instantiation of a member template,
8199 verify that all of the types used for the non-type
8200 template parameter pack are, in fact, valid for non-type
8201 template parameters. */
8202 if (arg_idx < nargs
8203 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8204 {
8205 int j, len = TREE_VEC_LENGTH (packed_parms);
8206 for (j = 0; j < len; ++j)
8207 {
8208 tree t = TREE_VEC_ELT (packed_parms, j);
8209 if (TREE_CODE (t) == PARM_DECL
8210 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8211 return error_mark_node;
8212 }
8213 /* We don't know how many args we have yet, just
8214 use the unconverted ones for now. */
8215 return NULL_TREE;
8216 }
8217
8218 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8219 }
8220 /* Check if we have a placeholder pack, which indicates we're
8221 in the context of a introduction list. In that case we want
8222 to match this pack to the single placeholder. */
8223 else if (arg_idx < nargs
8224 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8225 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8226 {
8227 nargs = arg_idx + 1;
8228 packed_args = make_tree_vec (1);
8229 }
8230 else
8231 packed_args = make_tree_vec (nargs - arg_idx);
8232
8233 /* Convert the remaining arguments, which will be a part of the
8234 parameter pack "parm". */
8235 int first_pack_arg = arg_idx;
8236 for (; arg_idx < nargs; ++arg_idx)
8237 {
8238 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8239 tree actual_parm = TREE_VALUE (parm);
8240 int pack_idx = arg_idx - first_pack_arg;
8241
8242 if (packed_parms)
8243 {
8244 /* Once we've packed as many args as we have types, stop. */
8245 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8246 break;
8247 else if (PACK_EXPANSION_P (arg))
8248 /* We don't know how many args we have yet, just
8249 use the unconverted ones for now. */
8250 return NULL_TREE;
8251 else
8252 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8253 }
8254
8255 if (arg == error_mark_node)
8256 {
8257 if (complain & tf_error)
8258 error ("template argument %d is invalid", arg_idx + 1);
8259 }
8260 else
8261 arg = convert_template_argument (actual_parm,
8262 arg, new_args, complain, parm_idx,
8263 in_decl);
8264 if (arg == error_mark_node)
8265 (*lost)++;
8266 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8267 }
8268
8269 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8270 && TREE_VEC_LENGTH (packed_args) > 0)
8271 {
8272 if (complain & tf_error)
8273 error ("wrong number of template arguments (%d, should be %d)",
8274 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8275 return error_mark_node;
8276 }
8277
8278 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8279 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8280 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8281 else
8282 {
8283 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8284 TREE_CONSTANT (argument_pack) = 1;
8285 }
8286
8287 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8288 if (CHECKING_P)
8289 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8290 TREE_VEC_LENGTH (packed_args));
8291 return argument_pack;
8292 }
8293
8294 /* Returns the number of pack expansions in the template argument vector
8295 ARGS. */
8296
8297 static int
8298 pack_expansion_args_count (tree args)
8299 {
8300 int i;
8301 int count = 0;
8302 if (args)
8303 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8304 {
8305 tree elt = TREE_VEC_ELT (args, i);
8306 if (elt && PACK_EXPANSION_P (elt))
8307 ++count;
8308 }
8309 return count;
8310 }
8311
8312 /* Convert all template arguments to their appropriate types, and
8313 return a vector containing the innermost resulting template
8314 arguments. If any error occurs, return error_mark_node. Error and
8315 warning messages are issued under control of COMPLAIN.
8316
8317 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8318 for arguments not specified in ARGS. Otherwise, if
8319 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8320 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8321 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8322 ARGS. */
8323
8324 static tree
8325 coerce_template_parms (tree parms,
8326 tree args,
8327 tree in_decl,
8328 tsubst_flags_t complain,
8329 bool require_all_args,
8330 bool use_default_args)
8331 {
8332 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8333 tree orig_inner_args;
8334 tree inner_args;
8335 tree new_args;
8336 tree new_inner_args;
8337
8338 /* When used as a boolean value, indicates whether this is a
8339 variadic template parameter list. Since it's an int, we can also
8340 subtract it from nparms to get the number of non-variadic
8341 parameters. */
8342 int variadic_p = 0;
8343 int variadic_args_p = 0;
8344 int post_variadic_parms = 0;
8345
8346 /* Adjustment to nparms for fixed parameter packs. */
8347 int fixed_pack_adjust = 0;
8348 int fixed_packs = 0;
8349 int missing = 0;
8350
8351 /* Likewise for parameters with default arguments. */
8352 int default_p = 0;
8353
8354 if (args == error_mark_node)
8355 return error_mark_node;
8356
8357 nparms = TREE_VEC_LENGTH (parms);
8358
8359 /* Determine if there are any parameter packs or default arguments. */
8360 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8361 {
8362 tree parm = TREE_VEC_ELT (parms, parm_idx);
8363 if (variadic_p)
8364 ++post_variadic_parms;
8365 if (template_parameter_pack_p (TREE_VALUE (parm)))
8366 ++variadic_p;
8367 if (TREE_PURPOSE (parm))
8368 ++default_p;
8369 }
8370
8371 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8372 /* If there are no parameters that follow a parameter pack, we need to
8373 expand any argument packs so that we can deduce a parameter pack from
8374 some non-packed args followed by an argument pack, as in variadic85.C.
8375 If there are such parameters, we need to leave argument packs intact
8376 so the arguments are assigned properly. This can happen when dealing
8377 with a nested class inside a partial specialization of a class
8378 template, as in variadic92.C, or when deducing a template parameter pack
8379 from a sub-declarator, as in variadic114.C. */
8380 if (!post_variadic_parms)
8381 inner_args = expand_template_argument_pack (inner_args);
8382
8383 /* Count any pack expansion args. */
8384 variadic_args_p = pack_expansion_args_count (inner_args);
8385
8386 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8387 if ((nargs - variadic_args_p > nparms && !variadic_p)
8388 || (nargs < nparms - variadic_p
8389 && require_all_args
8390 && !variadic_args_p
8391 && (!use_default_args
8392 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8393 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8394 {
8395 bad_nargs:
8396 if (complain & tf_error)
8397 {
8398 if (variadic_p || default_p)
8399 {
8400 nparms -= variadic_p + default_p;
8401 error ("wrong number of template arguments "
8402 "(%d, should be at least %d)", nargs, nparms);
8403 }
8404 else
8405 error ("wrong number of template arguments "
8406 "(%d, should be %d)", nargs, nparms);
8407
8408 if (in_decl)
8409 inform (DECL_SOURCE_LOCATION (in_decl),
8410 "provided for %qD", in_decl);
8411 }
8412
8413 return error_mark_node;
8414 }
8415 /* We can't pass a pack expansion to a non-pack parameter of an alias
8416 template (DR 1430). */
8417 else if (in_decl
8418 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8419 || concept_template_p (in_decl))
8420 && variadic_args_p
8421 && nargs - variadic_args_p < nparms - variadic_p)
8422 {
8423 if (complain & tf_error)
8424 {
8425 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8426 {
8427 tree arg = TREE_VEC_ELT (inner_args, i);
8428 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8429
8430 if (PACK_EXPANSION_P (arg)
8431 && !template_parameter_pack_p (parm))
8432 {
8433 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8434 error_at (location_of (arg),
8435 "pack expansion argument for non-pack parameter "
8436 "%qD of alias template %qD", parm, in_decl);
8437 else
8438 error_at (location_of (arg),
8439 "pack expansion argument for non-pack parameter "
8440 "%qD of concept %qD", parm, in_decl);
8441 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8442 goto found;
8443 }
8444 }
8445 gcc_unreachable ();
8446 found:;
8447 }
8448 return error_mark_node;
8449 }
8450
8451 /* We need to evaluate the template arguments, even though this
8452 template-id may be nested within a "sizeof". */
8453 cp_evaluated ev;
8454
8455 new_inner_args = make_tree_vec (nparms);
8456 new_args = add_outermost_template_args (args, new_inner_args);
8457 int pack_adjust = 0;
8458 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8459 {
8460 tree arg;
8461 tree parm;
8462
8463 /* Get the Ith template parameter. */
8464 parm = TREE_VEC_ELT (parms, parm_idx);
8465
8466 if (parm == error_mark_node)
8467 {
8468 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8469 continue;
8470 }
8471
8472 /* Calculate the next argument. */
8473 if (arg_idx < nargs)
8474 arg = TREE_VEC_ELT (inner_args, arg_idx);
8475 else
8476 arg = NULL_TREE;
8477
8478 if (template_parameter_pack_p (TREE_VALUE (parm))
8479 && (arg || require_all_args || !(complain & tf_partial))
8480 && !(arg && ARGUMENT_PACK_P (arg)))
8481 {
8482 /* Some arguments will be placed in the
8483 template parameter pack PARM. */
8484 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8485 inner_args, arg_idx,
8486 new_args, &lost,
8487 in_decl, complain);
8488
8489 if (arg == NULL_TREE)
8490 {
8491 /* We don't know how many args we have yet, just use the
8492 unconverted (and still packed) ones for now. */
8493 new_inner_args = orig_inner_args;
8494 arg_idx = nargs;
8495 break;
8496 }
8497
8498 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8499
8500 /* Store this argument. */
8501 if (arg == error_mark_node)
8502 {
8503 lost++;
8504 /* We are done with all of the arguments. */
8505 arg_idx = nargs;
8506 break;
8507 }
8508 else
8509 {
8510 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8511 arg_idx += pack_adjust;
8512 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8513 {
8514 ++fixed_packs;
8515 fixed_pack_adjust += pack_adjust;
8516 }
8517 }
8518
8519 continue;
8520 }
8521 else if (arg)
8522 {
8523 if (PACK_EXPANSION_P (arg))
8524 {
8525 /* "If every valid specialization of a variadic template
8526 requires an empty template parameter pack, the template is
8527 ill-formed, no diagnostic required." So check that the
8528 pattern works with this parameter. */
8529 tree pattern = PACK_EXPANSION_PATTERN (arg);
8530 tree conv = convert_template_argument (TREE_VALUE (parm),
8531 pattern, new_args,
8532 complain, parm_idx,
8533 in_decl);
8534 if (conv == error_mark_node)
8535 {
8536 if (complain & tf_error)
8537 inform (input_location, "so any instantiation with a "
8538 "non-empty parameter pack would be ill-formed");
8539 ++lost;
8540 }
8541 else if (TYPE_P (conv) && !TYPE_P (pattern))
8542 /* Recover from missing typename. */
8543 TREE_VEC_ELT (inner_args, arg_idx)
8544 = make_pack_expansion (conv, complain);
8545
8546 /* We don't know how many args we have yet, just
8547 use the unconverted ones for now. */
8548 new_inner_args = inner_args;
8549 arg_idx = nargs;
8550 break;
8551 }
8552 }
8553 else if (require_all_args)
8554 {
8555 /* There must be a default arg in this case. */
8556 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8557 complain, in_decl);
8558 /* The position of the first default template argument,
8559 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8560 Record that. */
8561 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8562 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8563 arg_idx - pack_adjust);
8564 }
8565 else
8566 break;
8567
8568 if (arg == error_mark_node)
8569 {
8570 if (complain & tf_error)
8571 error ("template argument %d is invalid", arg_idx + 1);
8572 }
8573 else if (!arg)
8574 {
8575 /* This can occur if there was an error in the template
8576 parameter list itself (which we would already have
8577 reported) that we are trying to recover from, e.g., a class
8578 template with a parameter list such as
8579 template<typename..., typename> (cpp0x/variadic150.C). */
8580 ++lost;
8581
8582 /* This can also happen with a fixed parameter pack (71834). */
8583 if (arg_idx >= nargs)
8584 ++missing;
8585 }
8586 else
8587 arg = convert_template_argument (TREE_VALUE (parm),
8588 arg, new_args, complain,
8589 parm_idx, in_decl);
8590
8591 if (arg == error_mark_node)
8592 lost++;
8593 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8594 }
8595
8596 if (missing || arg_idx < nargs - variadic_args_p)
8597 {
8598 /* If we had fixed parameter packs, we didn't know how many arguments we
8599 actually needed earlier; now we do. */
8600 nparms += fixed_pack_adjust;
8601 variadic_p -= fixed_packs;
8602 goto bad_nargs;
8603 }
8604
8605 if (arg_idx < nargs)
8606 {
8607 /* We had some pack expansion arguments that will only work if the packs
8608 are empty, but wait until instantiation time to complain.
8609 See variadic-ttp3.C. */
8610 int len = nparms + (nargs - arg_idx);
8611 tree args = make_tree_vec (len);
8612 int i = 0;
8613 for (; i < nparms; ++i)
8614 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8615 for (; i < len; ++i, ++arg_idx)
8616 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8617 arg_idx - pack_adjust);
8618 new_inner_args = args;
8619 }
8620
8621 if (lost)
8622 {
8623 gcc_assert (!(complain & tf_error) || seen_error ());
8624 return error_mark_node;
8625 }
8626
8627 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8628 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8629 TREE_VEC_LENGTH (new_inner_args));
8630
8631 return new_inner_args;
8632 }
8633
8634 /* Convert all template arguments to their appropriate types, and
8635 return a vector containing the innermost resulting template
8636 arguments. If any error occurs, return error_mark_node. Error and
8637 warning messages are not issued.
8638
8639 Note that no function argument deduction is performed, and default
8640 arguments are used to fill in unspecified arguments. */
8641 tree
8642 coerce_template_parms (tree parms, tree args, tree in_decl)
8643 {
8644 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8645 }
8646
8647 /* Convert all template arguments to their appropriate type, and
8648 instantiate default arguments as needed. This returns a vector
8649 containing the innermost resulting template arguments, or
8650 error_mark_node if unsuccessful. */
8651 tree
8652 coerce_template_parms (tree parms, tree args, tree in_decl,
8653 tsubst_flags_t complain)
8654 {
8655 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8656 }
8657
8658 /* Like coerce_template_parms. If PARMS represents all template
8659 parameters levels, this function returns a vector of vectors
8660 representing all the resulting argument levels. Note that in this
8661 case, only the innermost arguments are coerced because the
8662 outermost ones are supposed to have been coerced already.
8663
8664 Otherwise, if PARMS represents only (the innermost) vector of
8665 parameters, this function returns a vector containing just the
8666 innermost resulting arguments. */
8667
8668 static tree
8669 coerce_innermost_template_parms (tree parms,
8670 tree args,
8671 tree in_decl,
8672 tsubst_flags_t complain,
8673 bool require_all_args,
8674 bool use_default_args)
8675 {
8676 int parms_depth = TMPL_PARMS_DEPTH (parms);
8677 int args_depth = TMPL_ARGS_DEPTH (args);
8678 tree coerced_args;
8679
8680 if (parms_depth > 1)
8681 {
8682 coerced_args = make_tree_vec (parms_depth);
8683 tree level;
8684 int cur_depth;
8685
8686 for (level = parms, cur_depth = parms_depth;
8687 parms_depth > 0 && level != NULL_TREE;
8688 level = TREE_CHAIN (level), --cur_depth)
8689 {
8690 tree l;
8691 if (cur_depth == args_depth)
8692 l = coerce_template_parms (TREE_VALUE (level),
8693 args, in_decl, complain,
8694 require_all_args,
8695 use_default_args);
8696 else
8697 l = TMPL_ARGS_LEVEL (args, cur_depth);
8698
8699 if (l == error_mark_node)
8700 return error_mark_node;
8701
8702 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8703 }
8704 }
8705 else
8706 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8707 args, in_decl, complain,
8708 require_all_args,
8709 use_default_args);
8710 return coerced_args;
8711 }
8712
8713 /* Returns 1 if template args OT and NT are equivalent. */
8714
8715 int
8716 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8717 {
8718 if (nt == ot)
8719 return 1;
8720 if (nt == NULL_TREE || ot == NULL_TREE)
8721 return false;
8722 if (nt == any_targ_node || ot == any_targ_node)
8723 return true;
8724
8725 if (TREE_CODE (nt) == TREE_VEC)
8726 /* For member templates */
8727 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8728 else if (PACK_EXPANSION_P (ot))
8729 return (PACK_EXPANSION_P (nt)
8730 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8731 PACK_EXPANSION_PATTERN (nt))
8732 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8733 PACK_EXPANSION_EXTRA_ARGS (nt)));
8734 else if (ARGUMENT_PACK_P (ot))
8735 {
8736 int i, len;
8737 tree opack, npack;
8738
8739 if (!ARGUMENT_PACK_P (nt))
8740 return 0;
8741
8742 opack = ARGUMENT_PACK_ARGS (ot);
8743 npack = ARGUMENT_PACK_ARGS (nt);
8744 len = TREE_VEC_LENGTH (opack);
8745 if (TREE_VEC_LENGTH (npack) != len)
8746 return 0;
8747 for (i = 0; i < len; ++i)
8748 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8749 TREE_VEC_ELT (npack, i)))
8750 return 0;
8751 return 1;
8752 }
8753 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8754 gcc_unreachable ();
8755 else if (TYPE_P (nt))
8756 {
8757 if (!TYPE_P (ot))
8758 return false;
8759 /* Don't treat an alias template specialization with dependent
8760 arguments as equivalent to its underlying type when used as a
8761 template argument; we need them to be distinct so that we
8762 substitute into the specialization arguments at instantiation
8763 time. And aliases can't be equivalent without being ==, so
8764 we don't need to look any deeper.
8765
8766 During partial ordering, however, we need to treat them normally so
8767 that we can order uses of the same alias with different
8768 cv-qualification (79960). */
8769 if (!partial_order
8770 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8771 return false;
8772 else
8773 return same_type_p (ot, nt);
8774 }
8775 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8776 return 0;
8777 else
8778 {
8779 /* Try to treat a template non-type argument that has been converted
8780 to the parameter type as equivalent to one that hasn't yet. */
8781 for (enum tree_code code1 = TREE_CODE (ot);
8782 CONVERT_EXPR_CODE_P (code1)
8783 || code1 == NON_LVALUE_EXPR;
8784 code1 = TREE_CODE (ot))
8785 ot = TREE_OPERAND (ot, 0);
8786 for (enum tree_code code2 = TREE_CODE (nt);
8787 CONVERT_EXPR_CODE_P (code2)
8788 || code2 == NON_LVALUE_EXPR;
8789 code2 = TREE_CODE (nt))
8790 nt = TREE_OPERAND (nt, 0);
8791
8792 return cp_tree_equal (ot, nt);
8793 }
8794 }
8795
8796 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8797 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8798 NEWARG_PTR with the offending arguments if they are non-NULL. */
8799
8800 int
8801 comp_template_args (tree oldargs, tree newargs,
8802 tree *oldarg_ptr, tree *newarg_ptr,
8803 bool partial_order)
8804 {
8805 int i;
8806
8807 if (oldargs == newargs)
8808 return 1;
8809
8810 if (!oldargs || !newargs)
8811 return 0;
8812
8813 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8814 return 0;
8815
8816 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8817 {
8818 tree nt = TREE_VEC_ELT (newargs, i);
8819 tree ot = TREE_VEC_ELT (oldargs, i);
8820
8821 if (! template_args_equal (ot, nt, partial_order))
8822 {
8823 if (oldarg_ptr != NULL)
8824 *oldarg_ptr = ot;
8825 if (newarg_ptr != NULL)
8826 *newarg_ptr = nt;
8827 return 0;
8828 }
8829 }
8830 return 1;
8831 }
8832
8833 inline bool
8834 comp_template_args_porder (tree oargs, tree nargs)
8835 {
8836 return comp_template_args (oargs, nargs, NULL, NULL, true);
8837 }
8838
8839 /* Implement a freelist interface for objects of type T.
8840
8841 Head is a separate object, rather than a regular member, so that we
8842 can define it as a GTY deletable pointer, which is highly
8843 desirable. A data member could be declared that way, but then the
8844 containing object would implicitly get GTY((user)), which would
8845 prevent us from instantiating freelists as global objects.
8846 Although this way we can create freelist global objects, they're
8847 such thin wrappers that instantiating temporaries at every use
8848 loses nothing and saves permanent storage for the freelist object.
8849
8850 Member functions next, anew, poison and reinit have default
8851 implementations that work for most of the types we're interested
8852 in, but if they don't work for some type, they should be explicitly
8853 specialized. See the comments before them for requirements, and
8854 the example specializations for the tree_list_freelist. */
8855 template <typename T>
8856 class freelist
8857 {
8858 /* Return the next object in a chain. We could just do type
8859 punning, but if we access the object with its underlying type, we
8860 avoid strict-aliasing trouble. This needs only work between
8861 poison and reinit. */
8862 static T *&next (T *obj) { return obj->next; }
8863
8864 /* Return a newly allocated, uninitialized or minimally-initialized
8865 object of type T. Any initialization performed by anew should
8866 either remain across the life of the object and the execution of
8867 poison, or be redone by reinit. */
8868 static T *anew () { return ggc_alloc<T> (); }
8869
8870 /* Optionally scribble all over the bits holding the object, so that
8871 they become (mostly?) uninitialized memory. This is called while
8872 preparing to make the object part of the free list. */
8873 static void poison (T *obj) {
8874 T *p ATTRIBUTE_UNUSED = obj;
8875 T **q ATTRIBUTE_UNUSED = &next (obj);
8876
8877 #ifdef ENABLE_GC_CHECKING
8878 /* Poison the data, to indicate the data is garbage. */
8879 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8880 memset (p, 0xa5, sizeof (*p));
8881 #endif
8882 /* Let valgrind know the object is free. */
8883 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8884
8885 /* Let valgrind know the next portion of the object is available,
8886 but uninitialized. */
8887 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8888 }
8889
8890 /* Bring an object that underwent at least one lifecycle after anew
8891 and before the most recent free and poison, back to a usable
8892 state, reinitializing whatever is needed for it to be
8893 functionally equivalent to an object just allocated and returned
8894 by anew. This may poison or clear the next field, used by
8895 freelist housekeeping after poison was called. */
8896 static void reinit (T *obj) {
8897 T **q ATTRIBUTE_UNUSED = &next (obj);
8898
8899 #ifdef ENABLE_GC_CHECKING
8900 memset (q, 0xa5, sizeof (*q));
8901 #endif
8902 /* Let valgrind know the entire object is available, but
8903 uninitialized. */
8904 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8905 }
8906
8907 /* Reference a GTY-deletable pointer that points to the first object
8908 in the free list proper. */
8909 T *&head;
8910 public:
8911 /* Construct a freelist object chaining objects off of HEAD. */
8912 freelist (T *&head) : head(head) {}
8913
8914 /* Add OBJ to the free object list. The former head becomes OBJ's
8915 successor. */
8916 void free (T *obj)
8917 {
8918 poison (obj);
8919 next (obj) = head;
8920 head = obj;
8921 }
8922
8923 /* Take an object from the free list, if one is available, or
8924 allocate a new one. Objects taken from the free list should be
8925 regarded as filled with garbage, except for bits that are
8926 configured to be preserved across free and alloc. */
8927 T *alloc ()
8928 {
8929 if (head)
8930 {
8931 T *obj = head;
8932 head = next (head);
8933 reinit (obj);
8934 return obj;
8935 }
8936 else
8937 return anew ();
8938 }
8939 };
8940
8941 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8942 want to allocate a TREE_LIST using the usual interface, and ensure
8943 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8944 build_tree_list logic in reinit, so this could go out of sync. */
8945 template <>
8946 inline tree &
8947 freelist<tree_node>::next (tree obj)
8948 {
8949 return TREE_CHAIN (obj);
8950 }
8951 template <>
8952 inline tree
8953 freelist<tree_node>::anew ()
8954 {
8955 return build_tree_list (NULL, NULL);
8956 }
8957 template <>
8958 inline void
8959 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8960 {
8961 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8962 tree p ATTRIBUTE_UNUSED = obj;
8963 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8964 tree *q ATTRIBUTE_UNUSED = &next (obj);
8965
8966 #ifdef ENABLE_GC_CHECKING
8967 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8968
8969 /* Poison the data, to indicate the data is garbage. */
8970 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8971 memset (p, 0xa5, size);
8972 #endif
8973 /* Let valgrind know the object is free. */
8974 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8975 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8976 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8977 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8978
8979 #ifdef ENABLE_GC_CHECKING
8980 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8981 /* Keep TREE_CHAIN functional. */
8982 TREE_SET_CODE (obj, TREE_LIST);
8983 #else
8984 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8985 #endif
8986 }
8987 template <>
8988 inline void
8989 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8990 {
8991 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8992
8993 #ifdef ENABLE_GC_CHECKING
8994 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8995 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8996 memset (obj, 0, sizeof (tree_list));
8997 #endif
8998
8999 /* Let valgrind know the entire object is available, but
9000 uninitialized. */
9001 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9002
9003 #ifdef ENABLE_GC_CHECKING
9004 TREE_SET_CODE (obj, TREE_LIST);
9005 #else
9006 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9007 #endif
9008 }
9009
9010 /* Point to the first object in the TREE_LIST freelist. */
9011 static GTY((deletable)) tree tree_list_freelist_head;
9012 /* Return the/an actual TREE_LIST freelist. */
9013 static inline freelist<tree_node>
9014 tree_list_freelist ()
9015 {
9016 return tree_list_freelist_head;
9017 }
9018
9019 /* Point to the first object in the tinst_level freelist. */
9020 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9021 /* Return the/an actual tinst_level freelist. */
9022 static inline freelist<tinst_level>
9023 tinst_level_freelist ()
9024 {
9025 return tinst_level_freelist_head;
9026 }
9027
9028 /* Point to the first object in the pending_template freelist. */
9029 static GTY((deletable)) pending_template *pending_template_freelist_head;
9030 /* Return the/an actual pending_template freelist. */
9031 static inline freelist<pending_template>
9032 pending_template_freelist ()
9033 {
9034 return pending_template_freelist_head;
9035 }
9036
9037 /* Build the TREE_LIST object out of a split list, store it
9038 permanently, and return it. */
9039 tree
9040 tinst_level::to_list ()
9041 {
9042 gcc_assert (split_list_p ());
9043 tree ret = tree_list_freelist ().alloc ();
9044 TREE_PURPOSE (ret) = tldcl;
9045 TREE_VALUE (ret) = targs;
9046 tldcl = ret;
9047 targs = NULL;
9048 gcc_assert (tree_list_p ());
9049 return ret;
9050 }
9051
9052 const unsigned short tinst_level::refcount_infinity;
9053
9054 /* Increment OBJ's refcount unless it is already infinite. */
9055 static tinst_level *
9056 inc_refcount_use (tinst_level *obj)
9057 {
9058 if (obj && obj->refcount != tinst_level::refcount_infinity)
9059 ++obj->refcount;
9060 return obj;
9061 }
9062
9063 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9064 void
9065 tinst_level::free (tinst_level *obj)
9066 {
9067 if (obj->tree_list_p ())
9068 tree_list_freelist ().free (obj->get_node ());
9069 tinst_level_freelist ().free (obj);
9070 }
9071
9072 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9073 OBJ's DECL and OBJ, and start over with the tinst_level object that
9074 used to be referenced by OBJ's NEXT. */
9075 static void
9076 dec_refcount_use (tinst_level *obj)
9077 {
9078 while (obj
9079 && obj->refcount != tinst_level::refcount_infinity
9080 && !--obj->refcount)
9081 {
9082 tinst_level *next = obj->next;
9083 tinst_level::free (obj);
9084 obj = next;
9085 }
9086 }
9087
9088 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9089 and of the former PTR. Omitting the second argument is equivalent
9090 to passing (T*)NULL; this is allowed because passing the
9091 zero-valued integral constant NULL confuses type deduction and/or
9092 overload resolution. */
9093 template <typename T>
9094 static void
9095 set_refcount_ptr (T *& ptr, T *obj = NULL)
9096 {
9097 T *save = ptr;
9098 ptr = inc_refcount_use (obj);
9099 dec_refcount_use (save);
9100 }
9101
9102 static void
9103 add_pending_template (tree d)
9104 {
9105 tree ti = (TYPE_P (d)
9106 ? CLASSTYPE_TEMPLATE_INFO (d)
9107 : DECL_TEMPLATE_INFO (d));
9108 struct pending_template *pt;
9109 int level;
9110
9111 if (TI_PENDING_TEMPLATE_FLAG (ti))
9112 return;
9113
9114 /* We are called both from instantiate_decl, where we've already had a
9115 tinst_level pushed, and instantiate_template, where we haven't.
9116 Compensate. */
9117 gcc_assert (TREE_CODE (d) != TREE_LIST);
9118 level = !current_tinst_level
9119 || current_tinst_level->maybe_get_node () != d;
9120
9121 if (level)
9122 push_tinst_level (d);
9123
9124 pt = pending_template_freelist ().alloc ();
9125 pt->next = NULL;
9126 pt->tinst = NULL;
9127 set_refcount_ptr (pt->tinst, current_tinst_level);
9128 if (last_pending_template)
9129 last_pending_template->next = pt;
9130 else
9131 pending_templates = pt;
9132
9133 last_pending_template = pt;
9134
9135 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9136
9137 if (level)
9138 pop_tinst_level ();
9139 }
9140
9141
9142 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9143 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9144 documentation for TEMPLATE_ID_EXPR. */
9145
9146 tree
9147 lookup_template_function (tree fns, tree arglist)
9148 {
9149 if (fns == error_mark_node || arglist == error_mark_node)
9150 return error_mark_node;
9151
9152 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9153
9154 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9155 {
9156 error ("%q#D is not a function template", fns);
9157 return error_mark_node;
9158 }
9159
9160 if (BASELINK_P (fns))
9161 {
9162 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9163 unknown_type_node,
9164 BASELINK_FUNCTIONS (fns),
9165 arglist);
9166 return fns;
9167 }
9168
9169 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9170 }
9171
9172 /* Within the scope of a template class S<T>, the name S gets bound
9173 (in build_self_reference) to a TYPE_DECL for the class, not a
9174 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9175 or one of its enclosing classes, and that type is a template,
9176 return the associated TEMPLATE_DECL. Otherwise, the original
9177 DECL is returned.
9178
9179 Also handle the case when DECL is a TREE_LIST of ambiguous
9180 injected-class-names from different bases. */
9181
9182 tree
9183 maybe_get_template_decl_from_type_decl (tree decl)
9184 {
9185 if (decl == NULL_TREE)
9186 return decl;
9187
9188 /* DR 176: A lookup that finds an injected-class-name (10.2
9189 [class.member.lookup]) can result in an ambiguity in certain cases
9190 (for example, if it is found in more than one base class). If all of
9191 the injected-class-names that are found refer to specializations of
9192 the same class template, and if the name is followed by a
9193 template-argument-list, the reference refers to the class template
9194 itself and not a specialization thereof, and is not ambiguous. */
9195 if (TREE_CODE (decl) == TREE_LIST)
9196 {
9197 tree t, tmpl = NULL_TREE;
9198 for (t = decl; t; t = TREE_CHAIN (t))
9199 {
9200 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9201 if (!tmpl)
9202 tmpl = elt;
9203 else if (tmpl != elt)
9204 break;
9205 }
9206 if (tmpl && t == NULL_TREE)
9207 return tmpl;
9208 else
9209 return decl;
9210 }
9211
9212 return (decl != NULL_TREE
9213 && DECL_SELF_REFERENCE_P (decl)
9214 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9215 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9216 }
9217
9218 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9219 parameters, find the desired type.
9220
9221 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9222
9223 IN_DECL, if non-NULL, is the template declaration we are trying to
9224 instantiate.
9225
9226 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9227 the class we are looking up.
9228
9229 Issue error and warning messages under control of COMPLAIN.
9230
9231 If the template class is really a local class in a template
9232 function, then the FUNCTION_CONTEXT is the function in which it is
9233 being instantiated.
9234
9235 ??? Note that this function is currently called *twice* for each
9236 template-id: the first time from the parser, while creating the
9237 incomplete type (finish_template_type), and the second type during the
9238 real instantiation (instantiate_template_class). This is surely something
9239 that we want to avoid. It also causes some problems with argument
9240 coercion (see convert_nontype_argument for more information on this). */
9241
9242 static tree
9243 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9244 int entering_scope, tsubst_flags_t complain)
9245 {
9246 tree templ = NULL_TREE, parmlist;
9247 tree t;
9248 spec_entry **slot;
9249 spec_entry *entry;
9250 spec_entry elt;
9251 hashval_t hash;
9252
9253 if (identifier_p (d1))
9254 {
9255 tree value = innermost_non_namespace_value (d1);
9256 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9257 templ = value;
9258 else
9259 {
9260 if (context)
9261 push_decl_namespace (context);
9262 templ = lookup_name (d1);
9263 templ = maybe_get_template_decl_from_type_decl (templ);
9264 if (context)
9265 pop_decl_namespace ();
9266 }
9267 if (templ)
9268 context = DECL_CONTEXT (templ);
9269 }
9270 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9271 {
9272 tree type = TREE_TYPE (d1);
9273
9274 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9275 an implicit typename for the second A. Deal with it. */
9276 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9277 type = TREE_TYPE (type);
9278
9279 if (CLASSTYPE_TEMPLATE_INFO (type))
9280 {
9281 templ = CLASSTYPE_TI_TEMPLATE (type);
9282 d1 = DECL_NAME (templ);
9283 }
9284 }
9285 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9286 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9287 {
9288 templ = TYPE_TI_TEMPLATE (d1);
9289 d1 = DECL_NAME (templ);
9290 }
9291 else if (DECL_TYPE_TEMPLATE_P (d1))
9292 {
9293 templ = d1;
9294 d1 = DECL_NAME (templ);
9295 context = DECL_CONTEXT (templ);
9296 }
9297 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9298 {
9299 templ = d1;
9300 d1 = DECL_NAME (templ);
9301 }
9302
9303 /* Issue an error message if we didn't find a template. */
9304 if (! templ)
9305 {
9306 if (complain & tf_error)
9307 error ("%qT is not a template", d1);
9308 return error_mark_node;
9309 }
9310
9311 if (TREE_CODE (templ) != TEMPLATE_DECL
9312 /* Make sure it's a user visible template, if it was named by
9313 the user. */
9314 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9315 && !PRIMARY_TEMPLATE_P (templ)))
9316 {
9317 if (complain & tf_error)
9318 {
9319 error ("non-template type %qT used as a template", d1);
9320 if (in_decl)
9321 error ("for template declaration %q+D", in_decl);
9322 }
9323 return error_mark_node;
9324 }
9325
9326 complain &= ~tf_user;
9327
9328 /* An alias that just changes the name of a template is equivalent to the
9329 other template, so if any of the arguments are pack expansions, strip
9330 the alias to avoid problems with a pack expansion passed to a non-pack
9331 alias template parameter (DR 1430). */
9332 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9333 templ = get_underlying_template (templ);
9334
9335 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9336 {
9337 tree parm;
9338 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9339 if (arglist2 == error_mark_node
9340 || (!uses_template_parms (arglist2)
9341 && check_instantiated_args (templ, arglist2, complain)))
9342 return error_mark_node;
9343
9344 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9345 return parm;
9346 }
9347 else
9348 {
9349 tree template_type = TREE_TYPE (templ);
9350 tree gen_tmpl;
9351 tree type_decl;
9352 tree found = NULL_TREE;
9353 int arg_depth;
9354 int parm_depth;
9355 int is_dependent_type;
9356 int use_partial_inst_tmpl = false;
9357
9358 if (template_type == error_mark_node)
9359 /* An error occurred while building the template TEMPL, and a
9360 diagnostic has most certainly been emitted for that
9361 already. Let's propagate that error. */
9362 return error_mark_node;
9363
9364 gen_tmpl = most_general_template (templ);
9365 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9366 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9367 arg_depth = TMPL_ARGS_DEPTH (arglist);
9368
9369 if (arg_depth == 1 && parm_depth > 1)
9370 {
9371 /* We've been given an incomplete set of template arguments.
9372 For example, given:
9373
9374 template <class T> struct S1 {
9375 template <class U> struct S2 {};
9376 template <class U> struct S2<U*> {};
9377 };
9378
9379 we will be called with an ARGLIST of `U*', but the
9380 TEMPLATE will be `template <class T> template
9381 <class U> struct S1<T>::S2'. We must fill in the missing
9382 arguments. */
9383 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9384 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9385 arg_depth = TMPL_ARGS_DEPTH (arglist);
9386 }
9387
9388 /* Now we should have enough arguments. */
9389 gcc_assert (parm_depth == arg_depth);
9390
9391 /* From here on, we're only interested in the most general
9392 template. */
9393
9394 /* Calculate the BOUND_ARGS. These will be the args that are
9395 actually tsubst'd into the definition to create the
9396 instantiation. */
9397 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9398 complain,
9399 /*require_all_args=*/true,
9400 /*use_default_args=*/true);
9401
9402 if (arglist == error_mark_node)
9403 /* We were unable to bind the arguments. */
9404 return error_mark_node;
9405
9406 /* In the scope of a template class, explicit references to the
9407 template class refer to the type of the template, not any
9408 instantiation of it. For example, in:
9409
9410 template <class T> class C { void f(C<T>); }
9411
9412 the `C<T>' is just the same as `C'. Outside of the
9413 class, however, such a reference is an instantiation. */
9414 if (entering_scope
9415 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9416 || currently_open_class (template_type))
9417 {
9418 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9419
9420 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9421 return template_type;
9422 }
9423
9424 /* If we already have this specialization, return it. */
9425 elt.tmpl = gen_tmpl;
9426 elt.args = arglist;
9427 elt.spec = NULL_TREE;
9428 hash = spec_hasher::hash (&elt);
9429 entry = type_specializations->find_with_hash (&elt, hash);
9430
9431 if (entry)
9432 return entry->spec;
9433
9434 /* If the the template's constraints are not satisfied,
9435 then we cannot form a valid type.
9436
9437 Note that the check is deferred until after the hash
9438 lookup. This prevents redundant checks on previously
9439 instantiated specializations. */
9440 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9441 {
9442 if (complain & tf_error)
9443 {
9444 auto_diagnostic_group d;
9445 error ("template constraint failure");
9446 diagnose_constraints (input_location, gen_tmpl, arglist);
9447 }
9448 return error_mark_node;
9449 }
9450
9451 is_dependent_type = uses_template_parms (arglist);
9452
9453 /* If the deduced arguments are invalid, then the binding
9454 failed. */
9455 if (!is_dependent_type
9456 && check_instantiated_args (gen_tmpl,
9457 INNERMOST_TEMPLATE_ARGS (arglist),
9458 complain))
9459 return error_mark_node;
9460
9461 if (!is_dependent_type
9462 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9463 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9464 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9465 {
9466 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9467 DECL_NAME (gen_tmpl),
9468 /*tag_scope=*/ts_global);
9469 return found;
9470 }
9471
9472 context = DECL_CONTEXT (gen_tmpl);
9473 if (context && TYPE_P (context))
9474 {
9475 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9476 context = complete_type (context);
9477 }
9478 else
9479 context = tsubst (context, arglist, complain, in_decl);
9480
9481 if (context == error_mark_node)
9482 return error_mark_node;
9483
9484 if (!context)
9485 context = global_namespace;
9486
9487 /* Create the type. */
9488 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9489 {
9490 /* The user referred to a specialization of an alias
9491 template represented by GEN_TMPL.
9492
9493 [temp.alias]/2 says:
9494
9495 When a template-id refers to the specialization of an
9496 alias template, it is equivalent to the associated
9497 type obtained by substitution of its
9498 template-arguments for the template-parameters in the
9499 type-id of the alias template. */
9500
9501 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9502 /* Note that the call above (by indirectly calling
9503 register_specialization in tsubst_decl) registers the
9504 TYPE_DECL representing the specialization of the alias
9505 template. So next time someone substitutes ARGLIST for
9506 the template parms into the alias template (GEN_TMPL),
9507 she'll get that TYPE_DECL back. */
9508
9509 if (t == error_mark_node)
9510 return t;
9511 }
9512 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9513 {
9514 if (!is_dependent_type)
9515 {
9516 set_current_access_from_decl (TYPE_NAME (template_type));
9517 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9518 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9519 arglist, complain, in_decl),
9520 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9521 arglist, complain, in_decl),
9522 SCOPED_ENUM_P (template_type), NULL);
9523
9524 if (t == error_mark_node)
9525 return t;
9526 }
9527 else
9528 {
9529 /* We don't want to call start_enum for this type, since
9530 the values for the enumeration constants may involve
9531 template parameters. And, no one should be interested
9532 in the enumeration constants for such a type. */
9533 t = cxx_make_type (ENUMERAL_TYPE);
9534 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9535 }
9536 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9537 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9538 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9539 }
9540 else if (CLASS_TYPE_P (template_type))
9541 {
9542 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9543 instantiated here. */
9544 gcc_assert (!LAMBDA_TYPE_P (template_type));
9545
9546 t = make_class_type (TREE_CODE (template_type));
9547 CLASSTYPE_DECLARED_CLASS (t)
9548 = CLASSTYPE_DECLARED_CLASS (template_type);
9549 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9550
9551 /* A local class. Make sure the decl gets registered properly. */
9552 if (context == current_function_decl)
9553 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9554 == error_mark_node)
9555 return error_mark_node;
9556
9557 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9558 /* This instantiation is another name for the primary
9559 template type. Set the TYPE_CANONICAL field
9560 appropriately. */
9561 TYPE_CANONICAL (t) = template_type;
9562 else if (any_template_arguments_need_structural_equality_p (arglist))
9563 /* Some of the template arguments require structural
9564 equality testing, so this template class requires
9565 structural equality testing. */
9566 SET_TYPE_STRUCTURAL_EQUALITY (t);
9567 }
9568 else
9569 gcc_unreachable ();
9570
9571 /* If we called start_enum or pushtag above, this information
9572 will already be set up. */
9573 if (!TYPE_NAME (t))
9574 {
9575 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9576
9577 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9578 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9579 DECL_SOURCE_LOCATION (type_decl)
9580 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9581 }
9582 else
9583 type_decl = TYPE_NAME (t);
9584
9585 if (CLASS_TYPE_P (template_type))
9586 {
9587 TREE_PRIVATE (type_decl)
9588 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9589 TREE_PROTECTED (type_decl)
9590 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9591 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9592 {
9593 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9594 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9595 }
9596 }
9597
9598 if (OVERLOAD_TYPE_P (t)
9599 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9600 {
9601 static const char *tags[] = {"abi_tag", "may_alias"};
9602
9603 for (unsigned ix = 0; ix != 2; ix++)
9604 {
9605 tree attributes
9606 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9607
9608 if (attributes)
9609 TYPE_ATTRIBUTES (t)
9610 = tree_cons (TREE_PURPOSE (attributes),
9611 TREE_VALUE (attributes),
9612 TYPE_ATTRIBUTES (t));
9613 }
9614 }
9615
9616 /* Let's consider the explicit specialization of a member
9617 of a class template specialization that is implicitly instantiated,
9618 e.g.:
9619 template<class T>
9620 struct S
9621 {
9622 template<class U> struct M {}; //#0
9623 };
9624
9625 template<>
9626 template<>
9627 struct S<int>::M<char> //#1
9628 {
9629 int i;
9630 };
9631 [temp.expl.spec]/4 says this is valid.
9632
9633 In this case, when we write:
9634 S<int>::M<char> m;
9635
9636 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9637 the one of #0.
9638
9639 When we encounter #1, we want to store the partial instantiation
9640 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9641
9642 For all cases other than this "explicit specialization of member of a
9643 class template", we just want to store the most general template into
9644 the CLASSTYPE_TI_TEMPLATE of M.
9645
9646 This case of "explicit specialization of member of a class template"
9647 only happens when:
9648 1/ the enclosing class is an instantiation of, and therefore not
9649 the same as, the context of the most general template, and
9650 2/ we aren't looking at the partial instantiation itself, i.e.
9651 the innermost arguments are not the same as the innermost parms of
9652 the most general template.
9653
9654 So it's only when 1/ and 2/ happens that we want to use the partial
9655 instantiation of the member template in lieu of its most general
9656 template. */
9657
9658 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9659 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9660 /* the enclosing class must be an instantiation... */
9661 && CLASS_TYPE_P (context)
9662 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9663 {
9664 TREE_VEC_LENGTH (arglist)--;
9665 ++processing_template_decl;
9666 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9667 tree partial_inst_args =
9668 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9669 arglist, complain, NULL_TREE);
9670 --processing_template_decl;
9671 TREE_VEC_LENGTH (arglist)++;
9672 if (partial_inst_args == error_mark_node)
9673 return error_mark_node;
9674 use_partial_inst_tmpl =
9675 /*...and we must not be looking at the partial instantiation
9676 itself. */
9677 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9678 partial_inst_args);
9679 }
9680
9681 if (!use_partial_inst_tmpl)
9682 /* This case is easy; there are no member templates involved. */
9683 found = gen_tmpl;
9684 else
9685 {
9686 /* This is a full instantiation of a member template. Find
9687 the partial instantiation of which this is an instance. */
9688
9689 /* Temporarily reduce by one the number of levels in the ARGLIST
9690 so as to avoid comparing the last set of arguments. */
9691 TREE_VEC_LENGTH (arglist)--;
9692 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9693 TREE_VEC_LENGTH (arglist)++;
9694 /* FOUND is either a proper class type, or an alias
9695 template specialization. In the later case, it's a
9696 TYPE_DECL, resulting from the substituting of arguments
9697 for parameters in the TYPE_DECL of the alias template
9698 done earlier. So be careful while getting the template
9699 of FOUND. */
9700 found = (TREE_CODE (found) == TEMPLATE_DECL
9701 ? found
9702 : (TREE_CODE (found) == TYPE_DECL
9703 ? DECL_TI_TEMPLATE (found)
9704 : CLASSTYPE_TI_TEMPLATE (found)));
9705
9706 if (DECL_CLASS_TEMPLATE_P (found)
9707 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9708 {
9709 /* If this partial instantiation is specialized, we want to
9710 use it for hash table lookup. */
9711 elt.tmpl = found;
9712 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9713 hash = spec_hasher::hash (&elt);
9714 }
9715 }
9716
9717 // Build template info for the new specialization.
9718 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9719
9720 elt.spec = t;
9721 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9722 gcc_checking_assert (*slot == NULL);
9723 entry = ggc_alloc<spec_entry> ();
9724 *entry = elt;
9725 *slot = entry;
9726
9727 /* Note this use of the partial instantiation so we can check it
9728 later in maybe_process_partial_specialization. */
9729 DECL_TEMPLATE_INSTANTIATIONS (found)
9730 = tree_cons (arglist, t,
9731 DECL_TEMPLATE_INSTANTIATIONS (found));
9732
9733 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9734 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9735 /* Now that the type has been registered on the instantiations
9736 list, we set up the enumerators. Because the enumeration
9737 constants may involve the enumeration type itself, we make
9738 sure to register the type first, and then create the
9739 constants. That way, doing tsubst_expr for the enumeration
9740 constants won't result in recursive calls here; we'll find
9741 the instantiation and exit above. */
9742 tsubst_enum (template_type, t, arglist);
9743
9744 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9745 /* If the type makes use of template parameters, the
9746 code that generates debugging information will crash. */
9747 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9748
9749 /* Possibly limit visibility based on template args. */
9750 TREE_PUBLIC (type_decl) = 1;
9751 determine_visibility (type_decl);
9752
9753 inherit_targ_abi_tags (t);
9754
9755 return t;
9756 }
9757 }
9758
9759 /* Wrapper for lookup_template_class_1. */
9760
9761 tree
9762 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9763 int entering_scope, tsubst_flags_t complain)
9764 {
9765 tree ret;
9766 timevar_push (TV_TEMPLATE_INST);
9767 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9768 entering_scope, complain);
9769 timevar_pop (TV_TEMPLATE_INST);
9770 return ret;
9771 }
9772
9773 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9774
9775 tree
9776 lookup_template_variable (tree templ, tree arglist)
9777 {
9778 /* The type of the expression is NULL_TREE since the template-id could refer
9779 to an explicit or partial specialization. */
9780 tree type = NULL_TREE;
9781 if (flag_concepts && variable_concept_p (templ))
9782 /* Except that concepts are always bool. */
9783 type = boolean_type_node;
9784 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9785 }
9786
9787 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9788
9789 tree
9790 finish_template_variable (tree var, tsubst_flags_t complain)
9791 {
9792 tree templ = TREE_OPERAND (var, 0);
9793 tree arglist = TREE_OPERAND (var, 1);
9794
9795 /* We never want to return a VAR_DECL for a variable concept, since they
9796 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9797 bool concept_p = flag_concepts && variable_concept_p (templ);
9798 if (concept_p && processing_template_decl)
9799 return var;
9800
9801 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9802 arglist = add_outermost_template_args (tmpl_args, arglist);
9803
9804 templ = most_general_template (templ);
9805 tree parms = DECL_TEMPLATE_PARMS (templ);
9806 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9807 /*req_all*/true,
9808 /*use_default*/true);
9809
9810 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9811 {
9812 if (complain & tf_error)
9813 {
9814 auto_diagnostic_group d;
9815 error ("use of invalid variable template %qE", var);
9816 diagnose_constraints (location_of (var), templ, arglist);
9817 }
9818 return error_mark_node;
9819 }
9820
9821 /* If a template-id refers to a specialization of a variable
9822 concept, then the expression is true if and only if the
9823 concept's constraints are satisfied by the given template
9824 arguments.
9825
9826 NOTE: This is an extension of Concepts Lite TS that
9827 allows constraints to be used in expressions. */
9828 if (concept_p)
9829 {
9830 tree decl = DECL_TEMPLATE_RESULT (templ);
9831 return evaluate_variable_concept (decl, arglist);
9832 }
9833
9834 return instantiate_template (templ, arglist, complain);
9835 }
9836
9837 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9838 TARGS template args, and instantiate it if it's not dependent. */
9839
9840 tree
9841 lookup_and_finish_template_variable (tree templ, tree targs,
9842 tsubst_flags_t complain)
9843 {
9844 templ = lookup_template_variable (templ, targs);
9845 if (!any_dependent_template_arguments_p (targs))
9846 {
9847 templ = finish_template_variable (templ, complain);
9848 mark_used (templ);
9849 }
9850
9851 return convert_from_reference (templ);
9852 }
9853
9854 \f
9855 struct pair_fn_data
9856 {
9857 tree_fn_t fn;
9858 tree_fn_t any_fn;
9859 void *data;
9860 /* True when we should also visit template parameters that occur in
9861 non-deduced contexts. */
9862 bool include_nondeduced_p;
9863 hash_set<tree> *visited;
9864 };
9865
9866 /* Called from for_each_template_parm via walk_tree. */
9867
9868 static tree
9869 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9870 {
9871 tree t = *tp;
9872 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9873 tree_fn_t fn = pfd->fn;
9874 void *data = pfd->data;
9875 tree result = NULL_TREE;
9876
9877 #define WALK_SUBTREE(NODE) \
9878 do \
9879 { \
9880 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9881 pfd->include_nondeduced_p, \
9882 pfd->any_fn); \
9883 if (result) goto out; \
9884 } \
9885 while (0)
9886
9887 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9888 return t;
9889
9890 if (TYPE_P (t)
9891 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9892 WALK_SUBTREE (TYPE_CONTEXT (t));
9893
9894 switch (TREE_CODE (t))
9895 {
9896 case RECORD_TYPE:
9897 if (TYPE_PTRMEMFUNC_P (t))
9898 break;
9899 /* Fall through. */
9900
9901 case UNION_TYPE:
9902 case ENUMERAL_TYPE:
9903 if (!TYPE_TEMPLATE_INFO (t))
9904 *walk_subtrees = 0;
9905 else
9906 WALK_SUBTREE (TYPE_TI_ARGS (t));
9907 break;
9908
9909 case INTEGER_TYPE:
9910 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9911 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9912 break;
9913
9914 case METHOD_TYPE:
9915 /* Since we're not going to walk subtrees, we have to do this
9916 explicitly here. */
9917 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9918 /* Fall through. */
9919
9920 case FUNCTION_TYPE:
9921 /* Check the return type. */
9922 WALK_SUBTREE (TREE_TYPE (t));
9923
9924 /* Check the parameter types. Since default arguments are not
9925 instantiated until they are needed, the TYPE_ARG_TYPES may
9926 contain expressions that involve template parameters. But,
9927 no-one should be looking at them yet. And, once they're
9928 instantiated, they don't contain template parameters, so
9929 there's no point in looking at them then, either. */
9930 {
9931 tree parm;
9932
9933 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9934 WALK_SUBTREE (TREE_VALUE (parm));
9935
9936 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9937 want walk_tree walking into them itself. */
9938 *walk_subtrees = 0;
9939 }
9940
9941 if (flag_noexcept_type)
9942 {
9943 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9944 if (spec)
9945 WALK_SUBTREE (TREE_PURPOSE (spec));
9946 }
9947 break;
9948
9949 case TYPEOF_TYPE:
9950 case DECLTYPE_TYPE:
9951 case UNDERLYING_TYPE:
9952 if (pfd->include_nondeduced_p
9953 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9954 pfd->visited,
9955 pfd->include_nondeduced_p,
9956 pfd->any_fn))
9957 return error_mark_node;
9958 *walk_subtrees = false;
9959 break;
9960
9961 case FUNCTION_DECL:
9962 case VAR_DECL:
9963 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9964 WALK_SUBTREE (DECL_TI_ARGS (t));
9965 /* Fall through. */
9966
9967 case PARM_DECL:
9968 case CONST_DECL:
9969 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9970 WALK_SUBTREE (DECL_INITIAL (t));
9971 if (DECL_CONTEXT (t)
9972 && pfd->include_nondeduced_p)
9973 WALK_SUBTREE (DECL_CONTEXT (t));
9974 break;
9975
9976 case BOUND_TEMPLATE_TEMPLATE_PARM:
9977 /* Record template parameters such as `T' inside `TT<T>'. */
9978 WALK_SUBTREE (TYPE_TI_ARGS (t));
9979 /* Fall through. */
9980
9981 case TEMPLATE_TEMPLATE_PARM:
9982 case TEMPLATE_TYPE_PARM:
9983 case TEMPLATE_PARM_INDEX:
9984 if (fn && (*fn)(t, data))
9985 return t;
9986 else if (!fn)
9987 return t;
9988 break;
9989
9990 case TEMPLATE_DECL:
9991 /* A template template parameter is encountered. */
9992 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9993 WALK_SUBTREE (TREE_TYPE (t));
9994
9995 /* Already substituted template template parameter */
9996 *walk_subtrees = 0;
9997 break;
9998
9999 case TYPENAME_TYPE:
10000 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10001 partial instantiation. */
10002 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10003 break;
10004
10005 case CONSTRUCTOR:
10006 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10007 && pfd->include_nondeduced_p)
10008 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10009 break;
10010
10011 case INDIRECT_REF:
10012 case COMPONENT_REF:
10013 /* If there's no type, then this thing must be some expression
10014 involving template parameters. */
10015 if (!fn && !TREE_TYPE (t))
10016 return error_mark_node;
10017 break;
10018
10019 case MODOP_EXPR:
10020 case CAST_EXPR:
10021 case IMPLICIT_CONV_EXPR:
10022 case REINTERPRET_CAST_EXPR:
10023 case CONST_CAST_EXPR:
10024 case STATIC_CAST_EXPR:
10025 case DYNAMIC_CAST_EXPR:
10026 case ARROW_EXPR:
10027 case DOTSTAR_EXPR:
10028 case TYPEID_EXPR:
10029 case PSEUDO_DTOR_EXPR:
10030 if (!fn)
10031 return error_mark_node;
10032 break;
10033
10034 default:
10035 break;
10036 }
10037
10038 #undef WALK_SUBTREE
10039
10040 /* We didn't find any template parameters we liked. */
10041 out:
10042 return result;
10043 }
10044
10045 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10046 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10047 call FN with the parameter and the DATA.
10048 If FN returns nonzero, the iteration is terminated, and
10049 for_each_template_parm returns 1. Otherwise, the iteration
10050 continues. If FN never returns a nonzero value, the value
10051 returned by for_each_template_parm is 0. If FN is NULL, it is
10052 considered to be the function which always returns 1.
10053
10054 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10055 parameters that occur in non-deduced contexts. When false, only
10056 visits those template parameters that can be deduced. */
10057
10058 static tree
10059 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10060 hash_set<tree> *visited,
10061 bool include_nondeduced_p,
10062 tree_fn_t any_fn)
10063 {
10064 struct pair_fn_data pfd;
10065 tree result;
10066
10067 /* Set up. */
10068 pfd.fn = fn;
10069 pfd.any_fn = any_fn;
10070 pfd.data = data;
10071 pfd.include_nondeduced_p = include_nondeduced_p;
10072
10073 /* Walk the tree. (Conceptually, we would like to walk without
10074 duplicates, but for_each_template_parm_r recursively calls
10075 for_each_template_parm, so we would need to reorganize a fair
10076 bit to use walk_tree_without_duplicates, so we keep our own
10077 visited list.) */
10078 if (visited)
10079 pfd.visited = visited;
10080 else
10081 pfd.visited = new hash_set<tree>;
10082 result = cp_walk_tree (&t,
10083 for_each_template_parm_r,
10084 &pfd,
10085 pfd.visited);
10086
10087 /* Clean up. */
10088 if (!visited)
10089 {
10090 delete pfd.visited;
10091 pfd.visited = 0;
10092 }
10093
10094 return result;
10095 }
10096
10097 /* Returns true if T depends on any template parameter. */
10098
10099 int
10100 uses_template_parms (tree t)
10101 {
10102 if (t == NULL_TREE)
10103 return false;
10104
10105 bool dependent_p;
10106 int saved_processing_template_decl;
10107
10108 saved_processing_template_decl = processing_template_decl;
10109 if (!saved_processing_template_decl)
10110 processing_template_decl = 1;
10111 if (TYPE_P (t))
10112 dependent_p = dependent_type_p (t);
10113 else if (TREE_CODE (t) == TREE_VEC)
10114 dependent_p = any_dependent_template_arguments_p (t);
10115 else if (TREE_CODE (t) == TREE_LIST)
10116 dependent_p = (uses_template_parms (TREE_VALUE (t))
10117 || uses_template_parms (TREE_CHAIN (t)));
10118 else if (TREE_CODE (t) == TYPE_DECL)
10119 dependent_p = dependent_type_p (TREE_TYPE (t));
10120 else if (DECL_P (t)
10121 || EXPR_P (t)
10122 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10123 || TREE_CODE (t) == OVERLOAD
10124 || BASELINK_P (t)
10125 || identifier_p (t)
10126 || TREE_CODE (t) == TRAIT_EXPR
10127 || TREE_CODE (t) == CONSTRUCTOR
10128 || CONSTANT_CLASS_P (t))
10129 dependent_p = (type_dependent_expression_p (t)
10130 || value_dependent_expression_p (t));
10131 else
10132 {
10133 gcc_assert (t == error_mark_node);
10134 dependent_p = false;
10135 }
10136
10137 processing_template_decl = saved_processing_template_decl;
10138
10139 return dependent_p;
10140 }
10141
10142 /* Returns true iff current_function_decl is an incompletely instantiated
10143 template. Useful instead of processing_template_decl because the latter
10144 is set to 0 during instantiate_non_dependent_expr. */
10145
10146 bool
10147 in_template_function (void)
10148 {
10149 tree fn = current_function_decl;
10150 bool ret;
10151 ++processing_template_decl;
10152 ret = (fn && DECL_LANG_SPECIFIC (fn)
10153 && DECL_TEMPLATE_INFO (fn)
10154 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10155 --processing_template_decl;
10156 return ret;
10157 }
10158
10159 /* Returns true if T depends on any template parameter with level LEVEL. */
10160
10161 bool
10162 uses_template_parms_level (tree t, int level)
10163 {
10164 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10165 /*include_nondeduced_p=*/true);
10166 }
10167
10168 /* Returns true if the signature of DECL depends on any template parameter from
10169 its enclosing class. */
10170
10171 bool
10172 uses_outer_template_parms (tree decl)
10173 {
10174 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10175 if (depth == 0)
10176 return false;
10177 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10178 &depth, NULL, /*include_nondeduced_p=*/true))
10179 return true;
10180 if (PRIMARY_TEMPLATE_P (decl)
10181 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10182 (DECL_TEMPLATE_PARMS (decl)),
10183 template_parm_outer_level,
10184 &depth, NULL, /*include_nondeduced_p=*/true))
10185 return true;
10186 tree ci = get_constraints (decl);
10187 if (ci)
10188 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10189 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10190 &depth, NULL, /*nondeduced*/true))
10191 return true;
10192 return false;
10193 }
10194
10195 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10196 ill-formed translation unit, i.e. a variable or function that isn't
10197 usable in a constant expression. */
10198
10199 static inline bool
10200 neglectable_inst_p (tree d)
10201 {
10202 return (d && DECL_P (d)
10203 && !undeduced_auto_decl (d)
10204 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10205 : decl_maybe_constant_var_p (d)));
10206 }
10207
10208 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10209 neglectable and instantiated from within an erroneous instantiation. */
10210
10211 static bool
10212 limit_bad_template_recursion (tree decl)
10213 {
10214 struct tinst_level *lev = current_tinst_level;
10215 int errs = errorcount + sorrycount;
10216 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10217 return false;
10218
10219 for (; lev; lev = lev->next)
10220 if (neglectable_inst_p (lev->maybe_get_node ()))
10221 break;
10222
10223 return (lev && errs > lev->errors);
10224 }
10225
10226 static int tinst_depth;
10227 extern int max_tinst_depth;
10228 int depth_reached;
10229
10230 static GTY(()) struct tinst_level *last_error_tinst_level;
10231
10232 /* We're starting to instantiate D; record the template instantiation context
10233 at LOC for diagnostics and to restore it later. */
10234
10235 static bool
10236 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10237 {
10238 struct tinst_level *new_level;
10239
10240 if (tinst_depth >= max_tinst_depth)
10241 {
10242 /* Tell error.c not to try to instantiate any templates. */
10243 at_eof = 2;
10244 fatal_error (input_location,
10245 "template instantiation depth exceeds maximum of %d"
10246 " (use %<-ftemplate-depth=%> to increase the maximum)",
10247 max_tinst_depth);
10248 return false;
10249 }
10250
10251 /* If the current instantiation caused problems, don't let it instantiate
10252 anything else. Do allow deduction substitution and decls usable in
10253 constant expressions. */
10254 if (!targs && limit_bad_template_recursion (tldcl))
10255 return false;
10256
10257 /* When not -quiet, dump template instantiations other than functions, since
10258 announce_function will take care of those. */
10259 if (!quiet_flag && !targs
10260 && TREE_CODE (tldcl) != TREE_LIST
10261 && TREE_CODE (tldcl) != FUNCTION_DECL)
10262 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10263
10264 new_level = tinst_level_freelist ().alloc ();
10265 new_level->tldcl = tldcl;
10266 new_level->targs = targs;
10267 new_level->locus = loc;
10268 new_level->errors = errorcount + sorrycount;
10269 new_level->next = NULL;
10270 new_level->refcount = 0;
10271 set_refcount_ptr (new_level->next, current_tinst_level);
10272 set_refcount_ptr (current_tinst_level, new_level);
10273
10274 ++tinst_depth;
10275 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10276 depth_reached = tinst_depth;
10277
10278 return true;
10279 }
10280
10281 /* We're starting substitution of TMPL<ARGS>; record the template
10282 substitution context for diagnostics and to restore it later. */
10283
10284 static bool
10285 push_tinst_level (tree tmpl, tree args)
10286 {
10287 return push_tinst_level_loc (tmpl, args, input_location);
10288 }
10289
10290 /* We're starting to instantiate D; record INPUT_LOCATION and the
10291 template instantiation context for diagnostics and to restore it
10292 later. */
10293
10294 bool
10295 push_tinst_level (tree d)
10296 {
10297 return push_tinst_level_loc (d, input_location);
10298 }
10299
10300 /* Likewise, but record LOC as the program location. */
10301
10302 bool
10303 push_tinst_level_loc (tree d, location_t loc)
10304 {
10305 gcc_assert (TREE_CODE (d) != TREE_LIST);
10306 return push_tinst_level_loc (d, NULL, loc);
10307 }
10308
10309 /* We're done instantiating this template; return to the instantiation
10310 context. */
10311
10312 void
10313 pop_tinst_level (void)
10314 {
10315 /* Restore the filename and line number stashed away when we started
10316 this instantiation. */
10317 input_location = current_tinst_level->locus;
10318 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10319 --tinst_depth;
10320 }
10321
10322 /* We're instantiating a deferred template; restore the template
10323 instantiation context in which the instantiation was requested, which
10324 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10325
10326 static tree
10327 reopen_tinst_level (struct tinst_level *level)
10328 {
10329 struct tinst_level *t;
10330
10331 tinst_depth = 0;
10332 for (t = level; t; t = t->next)
10333 ++tinst_depth;
10334
10335 set_refcount_ptr (current_tinst_level, level);
10336 pop_tinst_level ();
10337 if (current_tinst_level)
10338 current_tinst_level->errors = errorcount+sorrycount;
10339 return level->maybe_get_node ();
10340 }
10341
10342 /* Returns the TINST_LEVEL which gives the original instantiation
10343 context. */
10344
10345 struct tinst_level *
10346 outermost_tinst_level (void)
10347 {
10348 struct tinst_level *level = current_tinst_level;
10349 if (level)
10350 while (level->next)
10351 level = level->next;
10352 return level;
10353 }
10354
10355 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10356 vector of template arguments, as for tsubst.
10357
10358 Returns an appropriate tsubst'd friend declaration. */
10359
10360 static tree
10361 tsubst_friend_function (tree decl, tree args)
10362 {
10363 tree new_friend;
10364
10365 if (TREE_CODE (decl) == FUNCTION_DECL
10366 && DECL_TEMPLATE_INSTANTIATION (decl)
10367 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10368 /* This was a friend declared with an explicit template
10369 argument list, e.g.:
10370
10371 friend void f<>(T);
10372
10373 to indicate that f was a template instantiation, not a new
10374 function declaration. Now, we have to figure out what
10375 instantiation of what template. */
10376 {
10377 tree template_id, arglist, fns;
10378 tree new_args;
10379 tree tmpl;
10380 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10381
10382 /* Friend functions are looked up in the containing namespace scope.
10383 We must enter that scope, to avoid finding member functions of the
10384 current class with same name. */
10385 push_nested_namespace (ns);
10386 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10387 tf_warning_or_error, NULL_TREE,
10388 /*integral_constant_expression_p=*/false);
10389 pop_nested_namespace (ns);
10390 arglist = tsubst (DECL_TI_ARGS (decl), args,
10391 tf_warning_or_error, NULL_TREE);
10392 template_id = lookup_template_function (fns, arglist);
10393
10394 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10395 tmpl = determine_specialization (template_id, new_friend,
10396 &new_args,
10397 /*need_member_template=*/0,
10398 TREE_VEC_LENGTH (args),
10399 tsk_none);
10400 return instantiate_template (tmpl, new_args, tf_error);
10401 }
10402
10403 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10404
10405 /* The NEW_FRIEND will look like an instantiation, to the
10406 compiler, but is not an instantiation from the point of view of
10407 the language. For example, we might have had:
10408
10409 template <class T> struct S {
10410 template <class U> friend void f(T, U);
10411 };
10412
10413 Then, in S<int>, template <class U> void f(int, U) is not an
10414 instantiation of anything. */
10415 if (new_friend == error_mark_node)
10416 return error_mark_node;
10417
10418 DECL_USE_TEMPLATE (new_friend) = 0;
10419 if (TREE_CODE (decl) == TEMPLATE_DECL)
10420 {
10421 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10422 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10423 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10424 }
10425
10426 /* The mangled name for the NEW_FRIEND is incorrect. The function
10427 is not a template instantiation and should not be mangled like
10428 one. Therefore, we forget the mangling here; we'll recompute it
10429 later if we need it. */
10430 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10431 {
10432 SET_DECL_RTL (new_friend, NULL);
10433 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10434 }
10435
10436 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10437 {
10438 tree old_decl;
10439 tree new_friend_template_info;
10440 tree new_friend_result_template_info;
10441 tree ns;
10442 int new_friend_is_defn;
10443
10444 /* We must save some information from NEW_FRIEND before calling
10445 duplicate decls since that function will free NEW_FRIEND if
10446 possible. */
10447 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10448 new_friend_is_defn =
10449 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10450 (template_for_substitution (new_friend)))
10451 != NULL_TREE);
10452 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10453 {
10454 /* This declaration is a `primary' template. */
10455 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10456
10457 new_friend_result_template_info
10458 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10459 }
10460 else
10461 new_friend_result_template_info = NULL_TREE;
10462
10463 /* Inside pushdecl_namespace_level, we will push into the
10464 current namespace. However, the friend function should go
10465 into the namespace of the template. */
10466 ns = decl_namespace_context (new_friend);
10467 push_nested_namespace (ns);
10468 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10469 pop_nested_namespace (ns);
10470
10471 if (old_decl == error_mark_node)
10472 return error_mark_node;
10473
10474 if (old_decl != new_friend)
10475 {
10476 /* This new friend declaration matched an existing
10477 declaration. For example, given:
10478
10479 template <class T> void f(T);
10480 template <class U> class C {
10481 template <class T> friend void f(T) {}
10482 };
10483
10484 the friend declaration actually provides the definition
10485 of `f', once C has been instantiated for some type. So,
10486 old_decl will be the out-of-class template declaration,
10487 while new_friend is the in-class definition.
10488
10489 But, if `f' was called before this point, the
10490 instantiation of `f' will have DECL_TI_ARGS corresponding
10491 to `T' but not to `U', references to which might appear
10492 in the definition of `f'. Previously, the most general
10493 template for an instantiation of `f' was the out-of-class
10494 version; now it is the in-class version. Therefore, we
10495 run through all specialization of `f', adding to their
10496 DECL_TI_ARGS appropriately. In particular, they need a
10497 new set of outer arguments, corresponding to the
10498 arguments for this class instantiation.
10499
10500 The same situation can arise with something like this:
10501
10502 friend void f(int);
10503 template <class T> class C {
10504 friend void f(T) {}
10505 };
10506
10507 when `C<int>' is instantiated. Now, `f(int)' is defined
10508 in the class. */
10509
10510 if (!new_friend_is_defn)
10511 /* On the other hand, if the in-class declaration does
10512 *not* provide a definition, then we don't want to alter
10513 existing definitions. We can just leave everything
10514 alone. */
10515 ;
10516 else
10517 {
10518 tree new_template = TI_TEMPLATE (new_friend_template_info);
10519 tree new_args = TI_ARGS (new_friend_template_info);
10520
10521 /* Overwrite whatever template info was there before, if
10522 any, with the new template information pertaining to
10523 the declaration. */
10524 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10525
10526 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10527 {
10528 /* We should have called reregister_specialization in
10529 duplicate_decls. */
10530 gcc_assert (retrieve_specialization (new_template,
10531 new_args, 0)
10532 == old_decl);
10533
10534 /* Instantiate it if the global has already been used. */
10535 if (DECL_ODR_USED (old_decl))
10536 instantiate_decl (old_decl, /*defer_ok=*/true,
10537 /*expl_inst_class_mem_p=*/false);
10538 }
10539 else
10540 {
10541 tree t;
10542
10543 /* Indicate that the old function template is a partial
10544 instantiation. */
10545 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10546 = new_friend_result_template_info;
10547
10548 gcc_assert (new_template
10549 == most_general_template (new_template));
10550 gcc_assert (new_template != old_decl);
10551
10552 /* Reassign any specializations already in the hash table
10553 to the new more general template, and add the
10554 additional template args. */
10555 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10556 t != NULL_TREE;
10557 t = TREE_CHAIN (t))
10558 {
10559 tree spec = TREE_VALUE (t);
10560 spec_entry elt;
10561
10562 elt.tmpl = old_decl;
10563 elt.args = DECL_TI_ARGS (spec);
10564 elt.spec = NULL_TREE;
10565
10566 decl_specializations->remove_elt (&elt);
10567
10568 DECL_TI_ARGS (spec)
10569 = add_outermost_template_args (new_args,
10570 DECL_TI_ARGS (spec));
10571
10572 register_specialization
10573 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10574
10575 }
10576 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10577 }
10578 }
10579
10580 /* The information from NEW_FRIEND has been merged into OLD_DECL
10581 by duplicate_decls. */
10582 new_friend = old_decl;
10583 }
10584 }
10585 else
10586 {
10587 tree context = DECL_CONTEXT (new_friend);
10588 bool dependent_p;
10589
10590 /* In the code
10591 template <class T> class C {
10592 template <class U> friend void C1<U>::f (); // case 1
10593 friend void C2<T>::f (); // case 2
10594 };
10595 we only need to make sure CONTEXT is a complete type for
10596 case 2. To distinguish between the two cases, we note that
10597 CONTEXT of case 1 remains dependent type after tsubst while
10598 this isn't true for case 2. */
10599 ++processing_template_decl;
10600 dependent_p = dependent_type_p (context);
10601 --processing_template_decl;
10602
10603 if (!dependent_p
10604 && !complete_type_or_else (context, NULL_TREE))
10605 return error_mark_node;
10606
10607 if (COMPLETE_TYPE_P (context))
10608 {
10609 tree fn = new_friend;
10610 /* do_friend adds the TEMPLATE_DECL for any member friend
10611 template even if it isn't a member template, i.e.
10612 template <class T> friend A<T>::f();
10613 Look through it in that case. */
10614 if (TREE_CODE (fn) == TEMPLATE_DECL
10615 && !PRIMARY_TEMPLATE_P (fn))
10616 fn = DECL_TEMPLATE_RESULT (fn);
10617 /* Check to see that the declaration is really present, and,
10618 possibly obtain an improved declaration. */
10619 fn = check_classfn (context, fn, NULL_TREE);
10620
10621 if (fn)
10622 new_friend = fn;
10623 }
10624 }
10625
10626 return new_friend;
10627 }
10628
10629 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10630 template arguments, as for tsubst.
10631
10632 Returns an appropriate tsubst'd friend type or error_mark_node on
10633 failure. */
10634
10635 static tree
10636 tsubst_friend_class (tree friend_tmpl, tree args)
10637 {
10638 tree tmpl;
10639
10640 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10641 {
10642 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10643 return TREE_TYPE (tmpl);
10644 }
10645
10646 tree context = CP_DECL_CONTEXT (friend_tmpl);
10647 if (TREE_CODE (context) == NAMESPACE_DECL)
10648 push_nested_namespace (context);
10649 else
10650 {
10651 context = tsubst (context, args, tf_error, NULL_TREE);
10652 push_nested_class (context);
10653 }
10654
10655 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10656 /*non_class=*/false, /*block_p=*/false,
10657 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10658
10659 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10660 {
10661 /* The friend template has already been declared. Just
10662 check to see that the declarations match, and install any new
10663 default parameters. We must tsubst the default parameters,
10664 of course. We only need the innermost template parameters
10665 because that is all that redeclare_class_template will look
10666 at. */
10667 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10668 > TMPL_ARGS_DEPTH (args))
10669 {
10670 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10671 args, tf_warning_or_error);
10672 location_t saved_input_location = input_location;
10673 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10674 tree cons = get_constraints (tmpl);
10675 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10676 input_location = saved_input_location;
10677 }
10678 }
10679 else
10680 {
10681 /* The friend template has not already been declared. In this
10682 case, the instantiation of the template class will cause the
10683 injection of this template into the namespace scope. */
10684 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10685
10686 if (tmpl != error_mark_node)
10687 {
10688 /* The new TMPL is not an instantiation of anything, so we
10689 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10690 for the new type because that is supposed to be the
10691 corresponding template decl, i.e., TMPL. */
10692 DECL_USE_TEMPLATE (tmpl) = 0;
10693 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10694 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10695 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10696 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10697
10698 /* It is hidden. */
10699 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10700 DECL_ANTICIPATED (tmpl)
10701 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10702
10703 /* Inject this template into the enclosing namspace scope. */
10704 tmpl = pushdecl_namespace_level (tmpl, true);
10705 }
10706 }
10707
10708 if (TREE_CODE (context) == NAMESPACE_DECL)
10709 pop_nested_namespace (context);
10710 else
10711 pop_nested_class ();
10712
10713 return TREE_TYPE (tmpl);
10714 }
10715
10716 /* Returns zero if TYPE cannot be completed later due to circularity.
10717 Otherwise returns one. */
10718
10719 static int
10720 can_complete_type_without_circularity (tree type)
10721 {
10722 if (type == NULL_TREE || type == error_mark_node)
10723 return 0;
10724 else if (COMPLETE_TYPE_P (type))
10725 return 1;
10726 else if (TREE_CODE (type) == ARRAY_TYPE)
10727 return can_complete_type_without_circularity (TREE_TYPE (type));
10728 else if (CLASS_TYPE_P (type)
10729 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10730 return 0;
10731 else
10732 return 1;
10733 }
10734
10735 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10736 tsubst_flags_t, tree);
10737
10738 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10739 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10740
10741 static tree
10742 tsubst_attribute (tree t, tree *decl_p, tree args,
10743 tsubst_flags_t complain, tree in_decl)
10744 {
10745 gcc_assert (ATTR_IS_DEPENDENT (t));
10746
10747 tree val = TREE_VALUE (t);
10748 if (val == NULL_TREE)
10749 /* Nothing to do. */;
10750 else if ((flag_openmp || flag_openmp_simd)
10751 && is_attribute_p ("omp declare simd",
10752 get_attribute_name (t)))
10753 {
10754 tree clauses = TREE_VALUE (val);
10755 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10756 complain, in_decl);
10757 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10758 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10759 tree parms = DECL_ARGUMENTS (*decl_p);
10760 clauses
10761 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10762 if (clauses)
10763 val = build_tree_list (NULL_TREE, clauses);
10764 else
10765 val = NULL_TREE;
10766 }
10767 /* If the first attribute argument is an identifier, don't
10768 pass it through tsubst. Attributes like mode, format,
10769 cleanup and several target specific attributes expect it
10770 unmodified. */
10771 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10772 {
10773 tree chain
10774 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10775 /*integral_constant_expression_p=*/false);
10776 if (chain != TREE_CHAIN (val))
10777 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10778 }
10779 else if (PACK_EXPANSION_P (val))
10780 {
10781 /* An attribute pack expansion. */
10782 tree purp = TREE_PURPOSE (t);
10783 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10784 if (pack == error_mark_node)
10785 return error_mark_node;
10786 int len = TREE_VEC_LENGTH (pack);
10787 tree list = NULL_TREE;
10788 tree *q = &list;
10789 for (int i = 0; i < len; ++i)
10790 {
10791 tree elt = TREE_VEC_ELT (pack, i);
10792 *q = build_tree_list (purp, elt);
10793 q = &TREE_CHAIN (*q);
10794 }
10795 return list;
10796 }
10797 else
10798 val = tsubst_expr (val, args, complain, in_decl,
10799 /*integral_constant_expression_p=*/false);
10800
10801 if (val != TREE_VALUE (t))
10802 return build_tree_list (TREE_PURPOSE (t), val);
10803 return t;
10804 }
10805
10806 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10807 unchanged or a new TREE_LIST chain. */
10808
10809 static tree
10810 tsubst_attributes (tree attributes, tree args,
10811 tsubst_flags_t complain, tree in_decl)
10812 {
10813 tree last_dep = NULL_TREE;
10814
10815 for (tree t = attributes; t; t = TREE_CHAIN (t))
10816 if (ATTR_IS_DEPENDENT (t))
10817 {
10818 last_dep = t;
10819 attributes = copy_list (attributes);
10820 break;
10821 }
10822
10823 if (last_dep)
10824 for (tree *p = &attributes; *p; )
10825 {
10826 tree t = *p;
10827 if (ATTR_IS_DEPENDENT (t))
10828 {
10829 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10830 if (subst != t)
10831 {
10832 *p = subst;
10833 while (*p)
10834 p = &TREE_CHAIN (*p);
10835 *p = TREE_CHAIN (t);
10836 continue;
10837 }
10838 }
10839 p = &TREE_CHAIN (*p);
10840 }
10841
10842 return attributes;
10843 }
10844
10845 /* Apply any attributes which had to be deferred until instantiation
10846 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10847 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10848
10849 static void
10850 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10851 tree args, tsubst_flags_t complain, tree in_decl)
10852 {
10853 tree last_dep = NULL_TREE;
10854 tree t;
10855 tree *p;
10856
10857 if (attributes == NULL_TREE)
10858 return;
10859
10860 if (DECL_P (*decl_p))
10861 {
10862 if (TREE_TYPE (*decl_p) == error_mark_node)
10863 return;
10864 p = &DECL_ATTRIBUTES (*decl_p);
10865 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10866 to our attributes parameter. */
10867 gcc_assert (*p == attributes);
10868 }
10869 else
10870 {
10871 p = &TYPE_ATTRIBUTES (*decl_p);
10872 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10873 lookup_template_class_1, and should be preserved. */
10874 gcc_assert (*p != attributes);
10875 while (*p)
10876 p = &TREE_CHAIN (*p);
10877 }
10878
10879 for (t = attributes; t; t = TREE_CHAIN (t))
10880 if (ATTR_IS_DEPENDENT (t))
10881 {
10882 last_dep = t;
10883 attributes = copy_list (attributes);
10884 break;
10885 }
10886
10887 *p = attributes;
10888 if (last_dep)
10889 {
10890 tree late_attrs = NULL_TREE;
10891 tree *q = &late_attrs;
10892
10893 for (; *p; )
10894 {
10895 t = *p;
10896 if (ATTR_IS_DEPENDENT (t))
10897 {
10898 *p = TREE_CHAIN (t);
10899 TREE_CHAIN (t) = NULL_TREE;
10900 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10901 while (*q)
10902 q = &TREE_CHAIN (*q);
10903 }
10904 else
10905 p = &TREE_CHAIN (t);
10906 }
10907
10908 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10909 }
10910 }
10911
10912 /* Perform (or defer) access check for typedefs that were referenced
10913 from within the template TMPL code.
10914 This is a subroutine of instantiate_decl and instantiate_class_template.
10915 TMPL is the template to consider and TARGS is the list of arguments of
10916 that template. */
10917
10918 static void
10919 perform_typedefs_access_check (tree tmpl, tree targs)
10920 {
10921 location_t saved_location;
10922 unsigned i;
10923 qualified_typedef_usage_t *iter;
10924
10925 if (!tmpl
10926 || (!CLASS_TYPE_P (tmpl)
10927 && TREE_CODE (tmpl) != FUNCTION_DECL))
10928 return;
10929
10930 saved_location = input_location;
10931 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10932 {
10933 tree type_decl = iter->typedef_decl;
10934 tree type_scope = iter->context;
10935
10936 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10937 continue;
10938
10939 if (uses_template_parms (type_decl))
10940 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10941 if (uses_template_parms (type_scope))
10942 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10943
10944 /* Make access check error messages point to the location
10945 of the use of the typedef. */
10946 input_location = iter->locus;
10947 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10948 type_decl, type_decl,
10949 tf_warning_or_error);
10950 }
10951 input_location = saved_location;
10952 }
10953
10954 static tree
10955 instantiate_class_template_1 (tree type)
10956 {
10957 tree templ, args, pattern, t, member;
10958 tree typedecl;
10959 tree pbinfo;
10960 tree base_list;
10961 unsigned int saved_maximum_field_alignment;
10962 tree fn_context;
10963
10964 if (type == error_mark_node)
10965 return error_mark_node;
10966
10967 if (COMPLETE_OR_OPEN_TYPE_P (type)
10968 || uses_template_parms (type))
10969 return type;
10970
10971 /* Figure out which template is being instantiated. */
10972 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10973 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10974
10975 /* Mark the type as in the process of being defined. */
10976 TYPE_BEING_DEFINED (type) = 1;
10977
10978 /* We may be in the middle of deferred access check. Disable
10979 it now. */
10980 deferring_access_check_sentinel acs (dk_no_deferred);
10981
10982 /* Determine what specialization of the original template to
10983 instantiate. */
10984 t = most_specialized_partial_spec (type, tf_warning_or_error);
10985 if (t == error_mark_node)
10986 return error_mark_node;
10987 else if (t)
10988 {
10989 /* This TYPE is actually an instantiation of a partial
10990 specialization. We replace the innermost set of ARGS with
10991 the arguments appropriate for substitution. For example,
10992 given:
10993
10994 template <class T> struct S {};
10995 template <class T> struct S<T*> {};
10996
10997 and supposing that we are instantiating S<int*>, ARGS will
10998 presently be {int*} -- but we need {int}. */
10999 pattern = TREE_TYPE (t);
11000 args = TREE_PURPOSE (t);
11001 }
11002 else
11003 {
11004 pattern = TREE_TYPE (templ);
11005 args = CLASSTYPE_TI_ARGS (type);
11006 }
11007
11008 /* If the template we're instantiating is incomplete, then clearly
11009 there's nothing we can do. */
11010 if (!COMPLETE_TYPE_P (pattern))
11011 {
11012 /* We can try again later. */
11013 TYPE_BEING_DEFINED (type) = 0;
11014 return type;
11015 }
11016
11017 /* If we've recursively instantiated too many templates, stop. */
11018 if (! push_tinst_level (type))
11019 return type;
11020
11021 int saved_unevaluated_operand = cp_unevaluated_operand;
11022 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11023
11024 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11025 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11026 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11027 fn_context = error_mark_node;
11028 if (!fn_context)
11029 push_to_top_level ();
11030 else
11031 {
11032 cp_unevaluated_operand = 0;
11033 c_inhibit_evaluation_warnings = 0;
11034 }
11035 /* Use #pragma pack from the template context. */
11036 saved_maximum_field_alignment = maximum_field_alignment;
11037 maximum_field_alignment = TYPE_PRECISION (pattern);
11038
11039 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11040
11041 /* Set the input location to the most specialized template definition.
11042 This is needed if tsubsting causes an error. */
11043 typedecl = TYPE_MAIN_DECL (pattern);
11044 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11045 DECL_SOURCE_LOCATION (typedecl);
11046
11047 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11048 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11049 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11050 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11051 if (ANON_AGGR_TYPE_P (pattern))
11052 SET_ANON_AGGR_TYPE_P (type);
11053 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11054 {
11055 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11056 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11057 /* Adjust visibility for template arguments. */
11058 determine_visibility (TYPE_MAIN_DECL (type));
11059 }
11060 if (CLASS_TYPE_P (type))
11061 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11062
11063 pbinfo = TYPE_BINFO (pattern);
11064
11065 /* We should never instantiate a nested class before its enclosing
11066 class; we need to look up the nested class by name before we can
11067 instantiate it, and that lookup should instantiate the enclosing
11068 class. */
11069 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11070 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11071
11072 base_list = NULL_TREE;
11073 if (BINFO_N_BASE_BINFOS (pbinfo))
11074 {
11075 tree pbase_binfo;
11076 tree pushed_scope;
11077 int i;
11078
11079 /* We must enter the scope containing the type, as that is where
11080 the accessibility of types named in dependent bases are
11081 looked up from. */
11082 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11083
11084 /* Substitute into each of the bases to determine the actual
11085 basetypes. */
11086 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11087 {
11088 tree base;
11089 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11090 tree expanded_bases = NULL_TREE;
11091 int idx, len = 1;
11092
11093 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11094 {
11095 expanded_bases =
11096 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11097 args, tf_error, NULL_TREE);
11098 if (expanded_bases == error_mark_node)
11099 continue;
11100
11101 len = TREE_VEC_LENGTH (expanded_bases);
11102 }
11103
11104 for (idx = 0; idx < len; idx++)
11105 {
11106 if (expanded_bases)
11107 /* Extract the already-expanded base class. */
11108 base = TREE_VEC_ELT (expanded_bases, idx);
11109 else
11110 /* Substitute to figure out the base class. */
11111 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11112 NULL_TREE);
11113
11114 if (base == error_mark_node)
11115 continue;
11116
11117 base_list = tree_cons (access, base, base_list);
11118 if (BINFO_VIRTUAL_P (pbase_binfo))
11119 TREE_TYPE (base_list) = integer_type_node;
11120 }
11121 }
11122
11123 /* The list is now in reverse order; correct that. */
11124 base_list = nreverse (base_list);
11125
11126 if (pushed_scope)
11127 pop_scope (pushed_scope);
11128 }
11129 /* Now call xref_basetypes to set up all the base-class
11130 information. */
11131 xref_basetypes (type, base_list);
11132
11133 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11134 (int) ATTR_FLAG_TYPE_IN_PLACE,
11135 args, tf_error, NULL_TREE);
11136 fixup_attribute_variants (type);
11137
11138 /* Now that our base classes are set up, enter the scope of the
11139 class, so that name lookups into base classes, etc. will work
11140 correctly. This is precisely analogous to what we do in
11141 begin_class_definition when defining an ordinary non-template
11142 class, except we also need to push the enclosing classes. */
11143 push_nested_class (type);
11144
11145 /* Now members are processed in the order of declaration. */
11146 for (member = CLASSTYPE_DECL_LIST (pattern);
11147 member; member = TREE_CHAIN (member))
11148 {
11149 tree t = TREE_VALUE (member);
11150
11151 if (TREE_PURPOSE (member))
11152 {
11153 if (TYPE_P (t))
11154 {
11155 if (LAMBDA_TYPE_P (t))
11156 /* A closure type for a lambda in an NSDMI or default argument.
11157 Ignore it; it will be regenerated when needed. */
11158 continue;
11159
11160 /* Build new CLASSTYPE_NESTED_UTDS. */
11161
11162 tree newtag;
11163 bool class_template_p;
11164
11165 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11166 && TYPE_LANG_SPECIFIC (t)
11167 && CLASSTYPE_IS_TEMPLATE (t));
11168 /* If the member is a class template, then -- even after
11169 substitution -- there may be dependent types in the
11170 template argument list for the class. We increment
11171 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11172 that function will assume that no types are dependent
11173 when outside of a template. */
11174 if (class_template_p)
11175 ++processing_template_decl;
11176 newtag = tsubst (t, args, tf_error, NULL_TREE);
11177 if (class_template_p)
11178 --processing_template_decl;
11179 if (newtag == error_mark_node)
11180 continue;
11181
11182 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11183 {
11184 tree name = TYPE_IDENTIFIER (t);
11185
11186 if (class_template_p)
11187 /* Unfortunately, lookup_template_class sets
11188 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11189 instantiation (i.e., for the type of a member
11190 template class nested within a template class.)
11191 This behavior is required for
11192 maybe_process_partial_specialization to work
11193 correctly, but is not accurate in this case;
11194 the TAG is not an instantiation of anything.
11195 (The corresponding TEMPLATE_DECL is an
11196 instantiation, but the TYPE is not.) */
11197 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11198
11199 /* Now, we call pushtag to put this NEWTAG into the scope of
11200 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11201 pushtag calling push_template_decl. We don't have to do
11202 this for enums because it will already have been done in
11203 tsubst_enum. */
11204 if (name)
11205 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11206 pushtag (name, newtag, /*tag_scope=*/ts_current);
11207 }
11208 }
11209 else if (DECL_DECLARES_FUNCTION_P (t))
11210 {
11211 tree r;
11212
11213 if (TREE_CODE (t) == TEMPLATE_DECL)
11214 ++processing_template_decl;
11215 r = tsubst (t, args, tf_error, NULL_TREE);
11216 if (TREE_CODE (t) == TEMPLATE_DECL)
11217 --processing_template_decl;
11218 set_current_access_from_decl (r);
11219 finish_member_declaration (r);
11220 /* Instantiate members marked with attribute used. */
11221 if (r != error_mark_node && DECL_PRESERVE_P (r))
11222 mark_used (r);
11223 if (TREE_CODE (r) == FUNCTION_DECL
11224 && DECL_OMP_DECLARE_REDUCTION_P (r))
11225 cp_check_omp_declare_reduction (r);
11226 }
11227 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11228 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11229 /* A closure type for a lambda in an NSDMI or default argument.
11230 Ignore it; it will be regenerated when needed. */;
11231 else
11232 {
11233 /* Build new TYPE_FIELDS. */
11234 if (TREE_CODE (t) == STATIC_ASSERT)
11235 {
11236 tree condition;
11237
11238 ++c_inhibit_evaluation_warnings;
11239 condition =
11240 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11241 tf_warning_or_error, NULL_TREE,
11242 /*integral_constant_expression_p=*/true);
11243 --c_inhibit_evaluation_warnings;
11244
11245 finish_static_assert (condition,
11246 STATIC_ASSERT_MESSAGE (t),
11247 STATIC_ASSERT_SOURCE_LOCATION (t),
11248 /*member_p=*/true);
11249 }
11250 else if (TREE_CODE (t) != CONST_DECL)
11251 {
11252 tree r;
11253 tree vec = NULL_TREE;
11254 int len = 1;
11255
11256 /* The file and line for this declaration, to
11257 assist in error message reporting. Since we
11258 called push_tinst_level above, we don't need to
11259 restore these. */
11260 input_location = DECL_SOURCE_LOCATION (t);
11261
11262 if (TREE_CODE (t) == TEMPLATE_DECL)
11263 ++processing_template_decl;
11264 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11265 if (TREE_CODE (t) == TEMPLATE_DECL)
11266 --processing_template_decl;
11267
11268 if (TREE_CODE (r) == TREE_VEC)
11269 {
11270 /* A capture pack became multiple fields. */
11271 vec = r;
11272 len = TREE_VEC_LENGTH (vec);
11273 }
11274
11275 for (int i = 0; i < len; ++i)
11276 {
11277 if (vec)
11278 r = TREE_VEC_ELT (vec, i);
11279 if (VAR_P (r))
11280 {
11281 /* In [temp.inst]:
11282
11283 [t]he initialization (and any associated
11284 side-effects) of a static data member does
11285 not occur unless the static data member is
11286 itself used in a way that requires the
11287 definition of the static data member to
11288 exist.
11289
11290 Therefore, we do not substitute into the
11291 initialized for the static data member here. */
11292 finish_static_data_member_decl
11293 (r,
11294 /*init=*/NULL_TREE,
11295 /*init_const_expr_p=*/false,
11296 /*asmspec_tree=*/NULL_TREE,
11297 /*flags=*/0);
11298 /* Instantiate members marked with attribute used. */
11299 if (r != error_mark_node && DECL_PRESERVE_P (r))
11300 mark_used (r);
11301 }
11302 else if (TREE_CODE (r) == FIELD_DECL)
11303 {
11304 /* Determine whether R has a valid type and can be
11305 completed later. If R is invalid, then its type
11306 is replaced by error_mark_node. */
11307 tree rtype = TREE_TYPE (r);
11308 if (can_complete_type_without_circularity (rtype))
11309 complete_type (rtype);
11310
11311 if (!complete_or_array_type_p (rtype))
11312 {
11313 /* If R's type couldn't be completed and
11314 it isn't a flexible array member (whose
11315 type is incomplete by definition) give
11316 an error. */
11317 cxx_incomplete_type_error (r, rtype);
11318 TREE_TYPE (r) = error_mark_node;
11319 }
11320 else if (TREE_CODE (rtype) == ARRAY_TYPE
11321 && TYPE_DOMAIN (rtype) == NULL_TREE
11322 && (TREE_CODE (type) == UNION_TYPE
11323 || TREE_CODE (type) == QUAL_UNION_TYPE))
11324 {
11325 error ("flexible array member %qD in union", r);
11326 TREE_TYPE (r) = error_mark_node;
11327 }
11328 }
11329
11330 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11331 such a thing will already have been added to the field
11332 list by tsubst_enum in finish_member_declaration in the
11333 CLASSTYPE_NESTED_UTDS case above. */
11334 if (!(TREE_CODE (r) == TYPE_DECL
11335 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11336 && DECL_ARTIFICIAL (r)))
11337 {
11338 set_current_access_from_decl (r);
11339 finish_member_declaration (r);
11340 }
11341 }
11342 }
11343 }
11344 }
11345 else
11346 {
11347 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11348 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11349 {
11350 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11351
11352 tree friend_type = t;
11353 bool adjust_processing_template_decl = false;
11354
11355 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11356 {
11357 /* template <class T> friend class C; */
11358 friend_type = tsubst_friend_class (friend_type, args);
11359 adjust_processing_template_decl = true;
11360 }
11361 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11362 {
11363 /* template <class T> friend class C::D; */
11364 friend_type = tsubst (friend_type, args,
11365 tf_warning_or_error, NULL_TREE);
11366 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11367 friend_type = TREE_TYPE (friend_type);
11368 adjust_processing_template_decl = true;
11369 }
11370 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11371 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11372 {
11373 /* This could be either
11374
11375 friend class T::C;
11376
11377 when dependent_type_p is false or
11378
11379 template <class U> friend class T::C;
11380
11381 otherwise. */
11382 /* Bump processing_template_decl in case this is something like
11383 template <class T> friend struct A<T>::B. */
11384 ++processing_template_decl;
11385 friend_type = tsubst (friend_type, args,
11386 tf_warning_or_error, NULL_TREE);
11387 if (dependent_type_p (friend_type))
11388 adjust_processing_template_decl = true;
11389 --processing_template_decl;
11390 }
11391 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11392 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11393 && TYPE_HIDDEN_P (friend_type))
11394 {
11395 /* friend class C;
11396
11397 where C hasn't been declared yet. Let's lookup name
11398 from namespace scope directly, bypassing any name that
11399 come from dependent base class. */
11400 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11401
11402 /* The call to xref_tag_from_type does injection for friend
11403 classes. */
11404 push_nested_namespace (ns);
11405 friend_type =
11406 xref_tag_from_type (friend_type, NULL_TREE,
11407 /*tag_scope=*/ts_current);
11408 pop_nested_namespace (ns);
11409 }
11410 else if (uses_template_parms (friend_type))
11411 /* friend class C<T>; */
11412 friend_type = tsubst (friend_type, args,
11413 tf_warning_or_error, NULL_TREE);
11414 /* Otherwise it's
11415
11416 friend class C;
11417
11418 where C is already declared or
11419
11420 friend class C<int>;
11421
11422 We don't have to do anything in these cases. */
11423
11424 if (adjust_processing_template_decl)
11425 /* Trick make_friend_class into realizing that the friend
11426 we're adding is a template, not an ordinary class. It's
11427 important that we use make_friend_class since it will
11428 perform some error-checking and output cross-reference
11429 information. */
11430 ++processing_template_decl;
11431
11432 if (friend_type != error_mark_node)
11433 make_friend_class (type, friend_type, /*complain=*/false);
11434
11435 if (adjust_processing_template_decl)
11436 --processing_template_decl;
11437 }
11438 else
11439 {
11440 /* Build new DECL_FRIENDLIST. */
11441 tree r;
11442
11443 /* The file and line for this declaration, to
11444 assist in error message reporting. Since we
11445 called push_tinst_level above, we don't need to
11446 restore these. */
11447 input_location = DECL_SOURCE_LOCATION (t);
11448
11449 if (TREE_CODE (t) == TEMPLATE_DECL)
11450 {
11451 ++processing_template_decl;
11452 push_deferring_access_checks (dk_no_check);
11453 }
11454
11455 r = tsubst_friend_function (t, args);
11456 add_friend (type, r, /*complain=*/false);
11457 if (TREE_CODE (t) == TEMPLATE_DECL)
11458 {
11459 pop_deferring_access_checks ();
11460 --processing_template_decl;
11461 }
11462 }
11463 }
11464 }
11465
11466 if (fn_context)
11467 {
11468 /* Restore these before substituting into the lambda capture
11469 initializers. */
11470 cp_unevaluated_operand = saved_unevaluated_operand;
11471 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11472 }
11473
11474 /* Set the file and line number information to whatever is given for
11475 the class itself. This puts error messages involving generated
11476 implicit functions at a predictable point, and the same point
11477 that would be used for non-template classes. */
11478 input_location = DECL_SOURCE_LOCATION (typedecl);
11479
11480 unreverse_member_declarations (type);
11481 finish_struct_1 (type);
11482 TYPE_BEING_DEFINED (type) = 0;
11483
11484 /* We don't instantiate default arguments for member functions. 14.7.1:
11485
11486 The implicit instantiation of a class template specialization causes
11487 the implicit instantiation of the declarations, but not of the
11488 definitions or default arguments, of the class member functions,
11489 member classes, static data members and member templates.... */
11490
11491 /* Some typedefs referenced from within the template code need to be access
11492 checked at template instantiation time, i.e now. These types were
11493 added to the template at parsing time. Let's get those and perform
11494 the access checks then. */
11495 perform_typedefs_access_check (pattern, args);
11496 perform_deferred_access_checks (tf_warning_or_error);
11497 pop_nested_class ();
11498 maximum_field_alignment = saved_maximum_field_alignment;
11499 if (!fn_context)
11500 pop_from_top_level ();
11501 pop_tinst_level ();
11502
11503 /* The vtable for a template class can be emitted in any translation
11504 unit in which the class is instantiated. When there is no key
11505 method, however, finish_struct_1 will already have added TYPE to
11506 the keyed_classes. */
11507 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11508 vec_safe_push (keyed_classes, type);
11509
11510 return type;
11511 }
11512
11513 /* Wrapper for instantiate_class_template_1. */
11514
11515 tree
11516 instantiate_class_template (tree type)
11517 {
11518 tree ret;
11519 timevar_push (TV_TEMPLATE_INST);
11520 ret = instantiate_class_template_1 (type);
11521 timevar_pop (TV_TEMPLATE_INST);
11522 return ret;
11523 }
11524
11525 static tree
11526 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11527 {
11528 tree r;
11529
11530 if (!t)
11531 r = t;
11532 else if (TYPE_P (t))
11533 r = tsubst (t, args, complain, in_decl);
11534 else
11535 {
11536 if (!(complain & tf_warning))
11537 ++c_inhibit_evaluation_warnings;
11538 r = tsubst_expr (t, args, complain, in_decl,
11539 /*integral_constant_expression_p=*/true);
11540 if (!(complain & tf_warning))
11541 --c_inhibit_evaluation_warnings;
11542 }
11543 return r;
11544 }
11545
11546 /* Given a function parameter pack TMPL_PARM and some function parameters
11547 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11548 and set *SPEC_P to point at the next point in the list. */
11549
11550 tree
11551 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11552 {
11553 /* Collect all of the extra "packed" parameters into an
11554 argument pack. */
11555 tree parmvec;
11556 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11557 tree spec_parm = *spec_p;
11558 int i, len;
11559
11560 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11561 if (tmpl_parm
11562 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11563 break;
11564
11565 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11566 parmvec = make_tree_vec (len);
11567 spec_parm = *spec_p;
11568 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11569 {
11570 tree elt = spec_parm;
11571 if (DECL_PACK_P (elt))
11572 elt = make_pack_expansion (elt);
11573 TREE_VEC_ELT (parmvec, i) = elt;
11574 }
11575
11576 /* Build the argument packs. */
11577 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11578 *spec_p = spec_parm;
11579
11580 return argpack;
11581 }
11582
11583 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11584 NONTYPE_ARGUMENT_PACK. */
11585
11586 static tree
11587 make_fnparm_pack (tree spec_parm)
11588 {
11589 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11590 }
11591
11592 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11593 pack expansion with no extra args, 2 if it has extra args, or 0
11594 if it is not a pack expansion. */
11595
11596 static int
11597 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11598 {
11599 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11600 /* We're being called before this happens in tsubst_pack_expansion. */
11601 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11602 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11603 if (i >= TREE_VEC_LENGTH (vec))
11604 return 0;
11605 tree elt = TREE_VEC_ELT (vec, i);
11606 if (DECL_P (elt))
11607 /* A decl pack is itself an expansion. */
11608 elt = TREE_TYPE (elt);
11609 if (!PACK_EXPANSION_P (elt))
11610 return 0;
11611 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11612 return 2;
11613 return 1;
11614 }
11615
11616
11617 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11618
11619 static tree
11620 make_argument_pack_select (tree arg_pack, unsigned index)
11621 {
11622 tree aps = make_node (ARGUMENT_PACK_SELECT);
11623
11624 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11625 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11626
11627 return aps;
11628 }
11629
11630 /* This is a subroutine of tsubst_pack_expansion.
11631
11632 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11633 mechanism to store the (non complete list of) arguments of the
11634 substitution and return a non substituted pack expansion, in order
11635 to wait for when we have enough arguments to really perform the
11636 substitution. */
11637
11638 static bool
11639 use_pack_expansion_extra_args_p (tree parm_packs,
11640 int arg_pack_len,
11641 bool has_empty_arg)
11642 {
11643 /* If one pack has an expansion and another pack has a normal
11644 argument or if one pack has an empty argument and an another
11645 one hasn't then tsubst_pack_expansion cannot perform the
11646 substitution and need to fall back on the
11647 PACK_EXPANSION_EXTRA mechanism. */
11648 if (parm_packs == NULL_TREE)
11649 return false;
11650 else if (has_empty_arg)
11651 return true;
11652
11653 bool has_expansion_arg = false;
11654 for (int i = 0 ; i < arg_pack_len; ++i)
11655 {
11656 bool has_non_expansion_arg = false;
11657 for (tree parm_pack = parm_packs;
11658 parm_pack;
11659 parm_pack = TREE_CHAIN (parm_pack))
11660 {
11661 tree arg = TREE_VALUE (parm_pack);
11662
11663 int exp = argument_pack_element_is_expansion_p (arg, i);
11664 if (exp == 2)
11665 /* We can't substitute a pack expansion with extra args into
11666 our pattern. */
11667 return true;
11668 else if (exp)
11669 has_expansion_arg = true;
11670 else
11671 has_non_expansion_arg = true;
11672 }
11673
11674 if (has_expansion_arg && has_non_expansion_arg)
11675 return true;
11676 }
11677 return false;
11678 }
11679
11680 /* [temp.variadic]/6 says that:
11681
11682 The instantiation of a pack expansion [...]
11683 produces a list E1,E2, ..., En, where N is the number of elements
11684 in the pack expansion parameters.
11685
11686 This subroutine of tsubst_pack_expansion produces one of these Ei.
11687
11688 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11689 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11690 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11691 INDEX is the index 'i' of the element Ei to produce. ARGS,
11692 COMPLAIN, and IN_DECL are the same parameters as for the
11693 tsubst_pack_expansion function.
11694
11695 The function returns the resulting Ei upon successful completion,
11696 or error_mark_node.
11697
11698 Note that this function possibly modifies the ARGS parameter, so
11699 it's the responsibility of the caller to restore it. */
11700
11701 static tree
11702 gen_elem_of_pack_expansion_instantiation (tree pattern,
11703 tree parm_packs,
11704 unsigned index,
11705 tree args /* This parm gets
11706 modified. */,
11707 tsubst_flags_t complain,
11708 tree in_decl)
11709 {
11710 tree t;
11711 bool ith_elem_is_expansion = false;
11712
11713 /* For each parameter pack, change the substitution of the parameter
11714 pack to the ith argument in its argument pack, then expand the
11715 pattern. */
11716 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11717 {
11718 tree parm = TREE_PURPOSE (pack);
11719 tree arg_pack = TREE_VALUE (pack);
11720 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11721
11722 ith_elem_is_expansion |=
11723 argument_pack_element_is_expansion_p (arg_pack, index);
11724
11725 /* Select the Ith argument from the pack. */
11726 if (TREE_CODE (parm) == PARM_DECL
11727 || VAR_P (parm)
11728 || TREE_CODE (parm) == FIELD_DECL)
11729 {
11730 if (index == 0)
11731 {
11732 aps = make_argument_pack_select (arg_pack, index);
11733 if (!mark_used (parm, complain) && !(complain & tf_error))
11734 return error_mark_node;
11735 register_local_specialization (aps, parm);
11736 }
11737 else
11738 aps = retrieve_local_specialization (parm);
11739 }
11740 else
11741 {
11742 int idx, level;
11743 template_parm_level_and_index (parm, &level, &idx);
11744
11745 if (index == 0)
11746 {
11747 aps = make_argument_pack_select (arg_pack, index);
11748 /* Update the corresponding argument. */
11749 TMPL_ARG (args, level, idx) = aps;
11750 }
11751 else
11752 /* Re-use the ARGUMENT_PACK_SELECT. */
11753 aps = TMPL_ARG (args, level, idx);
11754 }
11755 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11756 }
11757
11758 // Any local specialization bindings arising from this substitution
11759 // cannot be reused for a different INDEX.
11760 local_specialization_stack lss (lss_copy);
11761
11762 /* Substitute into the PATTERN with the (possibly altered)
11763 arguments. */
11764 if (pattern == in_decl)
11765 /* Expanding a fixed parameter pack from
11766 coerce_template_parameter_pack. */
11767 t = tsubst_decl (pattern, args, complain);
11768 else if (pattern == error_mark_node)
11769 t = error_mark_node;
11770 else if (constraint_p (pattern))
11771 {
11772 if (processing_template_decl)
11773 t = tsubst_constraint (pattern, args, complain, in_decl);
11774 else
11775 t = (constraints_satisfied_p (pattern, args)
11776 ? boolean_true_node : boolean_false_node);
11777 }
11778 else if (!TYPE_P (pattern))
11779 t = tsubst_expr (pattern, args, complain, in_decl,
11780 /*integral_constant_expression_p=*/false);
11781 else
11782 t = tsubst (pattern, args, complain, in_decl);
11783
11784 /* If the Ith argument pack element is a pack expansion, then
11785 the Ith element resulting from the substituting is going to
11786 be a pack expansion as well. */
11787 if (ith_elem_is_expansion)
11788 t = make_pack_expansion (t, complain);
11789
11790 return t;
11791 }
11792
11793 /* When the unexpanded parameter pack in a fold expression expands to an empty
11794 sequence, the value of the expression is as follows; the program is
11795 ill-formed if the operator is not listed in this table.
11796
11797 && true
11798 || false
11799 , void() */
11800
11801 tree
11802 expand_empty_fold (tree t, tsubst_flags_t complain)
11803 {
11804 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11805 if (!FOLD_EXPR_MODIFY_P (t))
11806 switch (code)
11807 {
11808 case TRUTH_ANDIF_EXPR:
11809 return boolean_true_node;
11810 case TRUTH_ORIF_EXPR:
11811 return boolean_false_node;
11812 case COMPOUND_EXPR:
11813 return void_node;
11814 default:
11815 break;
11816 }
11817
11818 if (complain & tf_error)
11819 error_at (location_of (t),
11820 "fold of empty expansion over %O", code);
11821 return error_mark_node;
11822 }
11823
11824 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11825 form an expression that combines the two terms using the
11826 operator of T. */
11827
11828 static tree
11829 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11830 {
11831 tree op = FOLD_EXPR_OP (t);
11832 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11833
11834 // Handle compound assignment operators.
11835 if (FOLD_EXPR_MODIFY_P (t))
11836 return build_x_modify_expr (input_location, left, code, right, complain);
11837
11838 switch (code)
11839 {
11840 case COMPOUND_EXPR:
11841 return build_x_compound_expr (input_location, left, right, complain);
11842 default:
11843 return build_x_binary_op (input_location, code,
11844 left, TREE_CODE (left),
11845 right, TREE_CODE (right),
11846 /*overload=*/NULL,
11847 complain);
11848 }
11849 }
11850
11851 /* Substitute ARGS into the pack of a fold expression T. */
11852
11853 static inline tree
11854 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11855 {
11856 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11857 }
11858
11859 /* Substitute ARGS into the pack of a fold expression T. */
11860
11861 static inline tree
11862 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11863 {
11864 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11865 }
11866
11867 /* Expand a PACK of arguments into a grouped as left fold.
11868 Given a pack containing elements A0, A1, ..., An and an
11869 operator @, this builds the expression:
11870
11871 ((A0 @ A1) @ A2) ... @ An
11872
11873 Note that PACK must not be empty.
11874
11875 The operator is defined by the original fold expression T. */
11876
11877 static tree
11878 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11879 {
11880 tree left = TREE_VEC_ELT (pack, 0);
11881 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11882 {
11883 tree right = TREE_VEC_ELT (pack, i);
11884 left = fold_expression (t, left, right, complain);
11885 }
11886 return left;
11887 }
11888
11889 /* Substitute into a unary left fold expression. */
11890
11891 static tree
11892 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11893 tree in_decl)
11894 {
11895 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11896 if (pack == error_mark_node)
11897 return error_mark_node;
11898 if (PACK_EXPANSION_P (pack))
11899 {
11900 tree r = copy_node (t);
11901 FOLD_EXPR_PACK (r) = pack;
11902 return r;
11903 }
11904 if (TREE_VEC_LENGTH (pack) == 0)
11905 return expand_empty_fold (t, complain);
11906 else
11907 return expand_left_fold (t, pack, complain);
11908 }
11909
11910 /* Substitute into a binary left fold expression.
11911
11912 Do ths by building a single (non-empty) vector of argumnts and
11913 building the expression from those elements. */
11914
11915 static tree
11916 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11917 tree in_decl)
11918 {
11919 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11920 if (pack == error_mark_node)
11921 return error_mark_node;
11922 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11923 if (init == error_mark_node)
11924 return error_mark_node;
11925
11926 if (PACK_EXPANSION_P (pack))
11927 {
11928 tree r = copy_node (t);
11929 FOLD_EXPR_PACK (r) = pack;
11930 FOLD_EXPR_INIT (r) = init;
11931 return r;
11932 }
11933
11934 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11935 TREE_VEC_ELT (vec, 0) = init;
11936 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11937 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11938
11939 return expand_left_fold (t, vec, complain);
11940 }
11941
11942 /* Expand a PACK of arguments into a grouped as right fold.
11943 Given a pack containing elementns A0, A1, ..., and an
11944 operator @, this builds the expression:
11945
11946 A0@ ... (An-2 @ (An-1 @ An))
11947
11948 Note that PACK must not be empty.
11949
11950 The operator is defined by the original fold expression T. */
11951
11952 tree
11953 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11954 {
11955 // Build the expression.
11956 int n = TREE_VEC_LENGTH (pack);
11957 tree right = TREE_VEC_ELT (pack, n - 1);
11958 for (--n; n != 0; --n)
11959 {
11960 tree left = TREE_VEC_ELT (pack, n - 1);
11961 right = fold_expression (t, left, right, complain);
11962 }
11963 return right;
11964 }
11965
11966 /* Substitute into a unary right fold expression. */
11967
11968 static tree
11969 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11970 tree in_decl)
11971 {
11972 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11973 if (pack == error_mark_node)
11974 return error_mark_node;
11975 if (PACK_EXPANSION_P (pack))
11976 {
11977 tree r = copy_node (t);
11978 FOLD_EXPR_PACK (r) = pack;
11979 return r;
11980 }
11981 if (TREE_VEC_LENGTH (pack) == 0)
11982 return expand_empty_fold (t, complain);
11983 else
11984 return expand_right_fold (t, pack, complain);
11985 }
11986
11987 /* Substitute into a binary right fold expression.
11988
11989 Do ths by building a single (non-empty) vector of arguments and
11990 building the expression from those elements. */
11991
11992 static tree
11993 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11994 tree in_decl)
11995 {
11996 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11997 if (pack == error_mark_node)
11998 return error_mark_node;
11999 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12000 if (init == error_mark_node)
12001 return error_mark_node;
12002
12003 if (PACK_EXPANSION_P (pack))
12004 {
12005 tree r = copy_node (t);
12006 FOLD_EXPR_PACK (r) = pack;
12007 FOLD_EXPR_INIT (r) = init;
12008 return r;
12009 }
12010
12011 int n = TREE_VEC_LENGTH (pack);
12012 tree vec = make_tree_vec (n + 1);
12013 for (int i = 0; i < n; ++i)
12014 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12015 TREE_VEC_ELT (vec, n) = init;
12016
12017 return expand_right_fold (t, vec, complain);
12018 }
12019
12020 /* Walk through the pattern of a pack expansion, adding everything in
12021 local_specializations to a list. */
12022
12023 struct el_data
12024 {
12025 hash_set<tree> internal;
12026 tree extra;
12027 tsubst_flags_t complain;
12028
12029 el_data (tsubst_flags_t c)
12030 : extra (NULL_TREE), complain (c) {}
12031 };
12032 static tree
12033 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12034 {
12035 el_data &data = *reinterpret_cast<el_data*>(data_);
12036 tree *extra = &data.extra;
12037 tsubst_flags_t complain = data.complain;
12038
12039 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12040 /* Remember local typedefs (85214). */
12041 tp = &TYPE_NAME (*tp);
12042
12043 if (TREE_CODE (*tp) == DECL_EXPR)
12044 data.internal.add (DECL_EXPR_DECL (*tp));
12045 else if (tree spec = retrieve_local_specialization (*tp))
12046 {
12047 if (data.internal.contains (*tp))
12048 /* Don't mess with variables declared within the pattern. */
12049 return NULL_TREE;
12050 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12051 {
12052 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12053 tree args = ARGUMENT_PACK_ARGS (spec);
12054 if (TREE_VEC_LENGTH (args) == 1)
12055 {
12056 tree elt = TREE_VEC_ELT (args, 0);
12057 if (PACK_EXPANSION_P (elt))
12058 elt = PACK_EXPANSION_PATTERN (elt);
12059 if (DECL_PACK_P (elt))
12060 spec = elt;
12061 }
12062 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12063 {
12064 /* Handle lambda capture here, since we aren't doing any
12065 substitution now, and so tsubst_copy won't call
12066 process_outer_var_ref. */
12067 tree args = ARGUMENT_PACK_ARGS (spec);
12068 int len = TREE_VEC_LENGTH (args);
12069 for (int i = 0; i < len; ++i)
12070 {
12071 tree arg = TREE_VEC_ELT (args, i);
12072 tree carg = arg;
12073 if (outer_automatic_var_p (arg))
12074 carg = process_outer_var_ref (arg, complain);
12075 if (carg != arg)
12076 {
12077 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12078 proxies. */
12079 if (i == 0)
12080 {
12081 spec = copy_node (spec);
12082 args = copy_node (args);
12083 SET_ARGUMENT_PACK_ARGS (spec, args);
12084 register_local_specialization (spec, *tp);
12085 }
12086 TREE_VEC_ELT (args, i) = carg;
12087 }
12088 }
12089 }
12090 }
12091 if (outer_automatic_var_p (spec))
12092 spec = process_outer_var_ref (spec, complain);
12093 *extra = tree_cons (*tp, spec, *extra);
12094 }
12095 return NULL_TREE;
12096 }
12097 static tree
12098 extract_local_specs (tree pattern, tsubst_flags_t complain)
12099 {
12100 el_data data (complain);
12101 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12102 return data.extra;
12103 }
12104
12105 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12106 for use in PACK_EXPANSION_EXTRA_ARGS. */
12107
12108 tree
12109 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12110 {
12111 tree extra = args;
12112 if (local_specializations)
12113 if (tree locals = extract_local_specs (pattern, complain))
12114 extra = tree_cons (NULL_TREE, extra, locals);
12115 return extra;
12116 }
12117
12118 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12119 normal template args to ARGS. */
12120
12121 tree
12122 add_extra_args (tree extra, tree args)
12123 {
12124 if (extra && TREE_CODE (extra) == TREE_LIST)
12125 {
12126 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12127 {
12128 /* The partial instantiation involved local declarations collected in
12129 extract_local_specs; map from the general template to our local
12130 context. */
12131 tree gen = TREE_PURPOSE (elt);
12132 tree inst = TREE_VALUE (elt);
12133 if (DECL_P (inst))
12134 if (tree local = retrieve_local_specialization (inst))
12135 inst = local;
12136 /* else inst is already a full instantiation of the pack. */
12137 register_local_specialization (inst, gen);
12138 }
12139 gcc_assert (!TREE_PURPOSE (extra));
12140 extra = TREE_VALUE (extra);
12141 }
12142 return add_to_template_args (extra, args);
12143 }
12144
12145 /* Substitute ARGS into T, which is an pack expansion
12146 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12147 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12148 (if only a partial substitution could be performed) or
12149 ERROR_MARK_NODE if there was an error. */
12150 tree
12151 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12152 tree in_decl)
12153 {
12154 tree pattern;
12155 tree pack, packs = NULL_TREE;
12156 bool unsubstituted_packs = false;
12157 bool unsubstituted_fn_pack = false;
12158 int i, len = -1;
12159 tree result;
12160 hash_map<tree, tree> *saved_local_specializations = NULL;
12161 bool need_local_specializations = false;
12162 int levels;
12163
12164 gcc_assert (PACK_EXPANSION_P (t));
12165 pattern = PACK_EXPANSION_PATTERN (t);
12166
12167 /* Add in any args remembered from an earlier partial instantiation. */
12168 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12169
12170 levels = TMPL_ARGS_DEPTH (args);
12171
12172 /* Determine the argument packs that will instantiate the parameter
12173 packs used in the expansion expression. While we're at it,
12174 compute the number of arguments to be expanded and make sure it
12175 is consistent. */
12176 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12177 pack = TREE_CHAIN (pack))
12178 {
12179 tree parm_pack = TREE_VALUE (pack);
12180 tree arg_pack = NULL_TREE;
12181 tree orig_arg = NULL_TREE;
12182 int level = 0;
12183
12184 if (TREE_CODE (parm_pack) == BASES)
12185 {
12186 gcc_assert (parm_pack == pattern);
12187 if (BASES_DIRECT (parm_pack))
12188 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12189 args, complain,
12190 in_decl, false),
12191 complain);
12192 else
12193 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12194 args, complain, in_decl,
12195 false), complain);
12196 }
12197 else if (builtin_pack_call_p (parm_pack))
12198 {
12199 if (parm_pack != pattern)
12200 {
12201 if (complain & tf_error)
12202 sorry ("%qE is not the entire pattern of the pack expansion",
12203 parm_pack);
12204 return error_mark_node;
12205 }
12206 return expand_builtin_pack_call (parm_pack, args,
12207 complain, in_decl);
12208 }
12209 else if (TREE_CODE (parm_pack) == PARM_DECL)
12210 {
12211 /* We know we have correct local_specializations if this
12212 expansion is at function scope, or if we're dealing with a
12213 local parameter in a requires expression; for the latter,
12214 tsubst_requires_expr set it up appropriately. */
12215 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12216 arg_pack = retrieve_local_specialization (parm_pack);
12217 else
12218 /* We can't rely on local_specializations for a parameter
12219 name used later in a function declaration (such as in a
12220 late-specified return type). Even if it exists, it might
12221 have the wrong value for a recursive call. */
12222 need_local_specializations = true;
12223
12224 if (!arg_pack)
12225 {
12226 /* This parameter pack was used in an unevaluated context. Just
12227 make a dummy decl, since it's only used for its type. */
12228 ++cp_unevaluated_operand;
12229 arg_pack = tsubst_decl (parm_pack, args, complain);
12230 --cp_unevaluated_operand;
12231 if (arg_pack && DECL_PACK_P (arg_pack))
12232 /* Partial instantiation of the parm_pack, we can't build
12233 up an argument pack yet. */
12234 arg_pack = NULL_TREE;
12235 else
12236 arg_pack = make_fnparm_pack (arg_pack);
12237 }
12238 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12239 /* This argument pack isn't fully instantiated yet. We set this
12240 flag rather than clear arg_pack because we do want to do the
12241 optimization below, and we don't want to substitute directly
12242 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12243 where it isn't expected). */
12244 unsubstituted_fn_pack = true;
12245 }
12246 else if (is_capture_proxy (parm_pack))
12247 {
12248 arg_pack = retrieve_local_specialization (parm_pack);
12249 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12250 unsubstituted_fn_pack = true;
12251 }
12252 else
12253 {
12254 int idx;
12255 template_parm_level_and_index (parm_pack, &level, &idx);
12256
12257 if (level <= levels)
12258 arg_pack = TMPL_ARG (args, level, idx);
12259 }
12260
12261 orig_arg = arg_pack;
12262 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12263 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12264
12265 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12266 /* This can only happen if we forget to expand an argument
12267 pack somewhere else. Just return an error, silently. */
12268 {
12269 result = make_tree_vec (1);
12270 TREE_VEC_ELT (result, 0) = error_mark_node;
12271 return result;
12272 }
12273
12274 if (arg_pack)
12275 {
12276 int my_len =
12277 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12278
12279 /* Don't bother trying to do a partial substitution with
12280 incomplete packs; we'll try again after deduction. */
12281 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12282 return t;
12283
12284 if (len < 0)
12285 len = my_len;
12286 else if (len != my_len
12287 && !unsubstituted_fn_pack)
12288 {
12289 if (!(complain & tf_error))
12290 /* Fail quietly. */;
12291 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12292 error ("mismatched argument pack lengths while expanding %qT",
12293 pattern);
12294 else
12295 error ("mismatched argument pack lengths while expanding %qE",
12296 pattern);
12297 return error_mark_node;
12298 }
12299
12300 /* Keep track of the parameter packs and their corresponding
12301 argument packs. */
12302 packs = tree_cons (parm_pack, arg_pack, packs);
12303 TREE_TYPE (packs) = orig_arg;
12304 }
12305 else
12306 {
12307 /* We can't substitute for this parameter pack. We use a flag as
12308 well as the missing_level counter because function parameter
12309 packs don't have a level. */
12310 gcc_assert (processing_template_decl || is_auto (parm_pack));
12311 unsubstituted_packs = true;
12312 }
12313 }
12314
12315 /* If the expansion is just T..., return the matching argument pack, unless
12316 we need to call convert_from_reference on all the elements. This is an
12317 important optimization; see c++/68422. */
12318 if (!unsubstituted_packs
12319 && TREE_PURPOSE (packs) == pattern)
12320 {
12321 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12322
12323 /* If the argument pack is a single pack expansion, pull it out. */
12324 if (TREE_VEC_LENGTH (args) == 1
12325 && pack_expansion_args_count (args))
12326 return TREE_VEC_ELT (args, 0);
12327
12328 /* Types need no adjustment, nor does sizeof..., and if we still have
12329 some pack expansion args we won't do anything yet. */
12330 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12331 || PACK_EXPANSION_SIZEOF_P (t)
12332 || pack_expansion_args_count (args))
12333 return args;
12334 /* Also optimize expression pack expansions if we can tell that the
12335 elements won't have reference type. */
12336 tree type = TREE_TYPE (pattern);
12337 if (type && !TYPE_REF_P (type)
12338 && !PACK_EXPANSION_P (type)
12339 && !WILDCARD_TYPE_P (type))
12340 return args;
12341 /* Otherwise use the normal path so we get convert_from_reference. */
12342 }
12343
12344 /* We cannot expand this expansion expression, because we don't have
12345 all of the argument packs we need. */
12346 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12347 {
12348 /* We got some full packs, but we can't substitute them in until we
12349 have values for all the packs. So remember these until then. */
12350
12351 t = make_pack_expansion (pattern, complain);
12352 PACK_EXPANSION_EXTRA_ARGS (t)
12353 = build_extra_args (pattern, args, complain);
12354 return t;
12355 }
12356 else if (unsubstituted_packs)
12357 {
12358 /* There were no real arguments, we're just replacing a parameter
12359 pack with another version of itself. Substitute into the
12360 pattern and return a PACK_EXPANSION_*. The caller will need to
12361 deal with that. */
12362 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12363 t = tsubst_expr (pattern, args, complain, in_decl,
12364 /*integral_constant_expression_p=*/false);
12365 else
12366 t = tsubst (pattern, args, complain, in_decl);
12367 t = make_pack_expansion (t, complain);
12368 return t;
12369 }
12370
12371 gcc_assert (len >= 0);
12372
12373 if (need_local_specializations)
12374 {
12375 /* We're in a late-specified return type, so create our own local
12376 specializations map; the current map is either NULL or (in the
12377 case of recursive unification) might have bindings that we don't
12378 want to use or alter. */
12379 saved_local_specializations = local_specializations;
12380 local_specializations = new hash_map<tree, tree>;
12381 }
12382
12383 /* For each argument in each argument pack, substitute into the
12384 pattern. */
12385 result = make_tree_vec (len);
12386 tree elem_args = copy_template_args (args);
12387 for (i = 0; i < len; ++i)
12388 {
12389 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12390 i,
12391 elem_args, complain,
12392 in_decl);
12393 TREE_VEC_ELT (result, i) = t;
12394 if (t == error_mark_node)
12395 {
12396 result = error_mark_node;
12397 break;
12398 }
12399 }
12400
12401 /* Update ARGS to restore the substitution from parameter packs to
12402 their argument packs. */
12403 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12404 {
12405 tree parm = TREE_PURPOSE (pack);
12406
12407 if (TREE_CODE (parm) == PARM_DECL
12408 || VAR_P (parm)
12409 || TREE_CODE (parm) == FIELD_DECL)
12410 register_local_specialization (TREE_TYPE (pack), parm);
12411 else
12412 {
12413 int idx, level;
12414
12415 if (TREE_VALUE (pack) == NULL_TREE)
12416 continue;
12417
12418 template_parm_level_and_index (parm, &level, &idx);
12419
12420 /* Update the corresponding argument. */
12421 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12422 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12423 TREE_TYPE (pack);
12424 else
12425 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12426 }
12427 }
12428
12429 if (need_local_specializations)
12430 {
12431 delete local_specializations;
12432 local_specializations = saved_local_specializations;
12433 }
12434
12435 /* If the dependent pack arguments were such that we end up with only a
12436 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12437 if (len == 1 && TREE_CODE (result) == TREE_VEC
12438 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12439 return TREE_VEC_ELT (result, 0);
12440
12441 return result;
12442 }
12443
12444 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12445 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12446 parameter packs; all parms generated from a function parameter pack will
12447 have the same DECL_PARM_INDEX. */
12448
12449 tree
12450 get_pattern_parm (tree parm, tree tmpl)
12451 {
12452 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12453 tree patparm;
12454
12455 if (DECL_ARTIFICIAL (parm))
12456 {
12457 for (patparm = DECL_ARGUMENTS (pattern);
12458 patparm; patparm = DECL_CHAIN (patparm))
12459 if (DECL_ARTIFICIAL (patparm)
12460 && DECL_NAME (parm) == DECL_NAME (patparm))
12461 break;
12462 }
12463 else
12464 {
12465 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12466 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12467 gcc_assert (DECL_PARM_INDEX (patparm)
12468 == DECL_PARM_INDEX (parm));
12469 }
12470
12471 return patparm;
12472 }
12473
12474 /* Make an argument pack out of the TREE_VEC VEC. */
12475
12476 static tree
12477 make_argument_pack (tree vec)
12478 {
12479 tree pack;
12480 tree elt = TREE_VEC_ELT (vec, 0);
12481 if (TYPE_P (elt))
12482 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12483 else
12484 {
12485 pack = make_node (NONTYPE_ARGUMENT_PACK);
12486 TREE_CONSTANT (pack) = 1;
12487 }
12488 SET_ARGUMENT_PACK_ARGS (pack, vec);
12489 return pack;
12490 }
12491
12492 /* Return an exact copy of template args T that can be modified
12493 independently. */
12494
12495 static tree
12496 copy_template_args (tree t)
12497 {
12498 if (t == error_mark_node)
12499 return t;
12500
12501 int len = TREE_VEC_LENGTH (t);
12502 tree new_vec = make_tree_vec (len);
12503
12504 for (int i = 0; i < len; ++i)
12505 {
12506 tree elt = TREE_VEC_ELT (t, i);
12507 if (elt && TREE_CODE (elt) == TREE_VEC)
12508 elt = copy_template_args (elt);
12509 TREE_VEC_ELT (new_vec, i) = elt;
12510 }
12511
12512 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12513 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12514
12515 return new_vec;
12516 }
12517
12518 /* Substitute ARGS into the vector or list of template arguments T. */
12519
12520 static tree
12521 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12522 {
12523 tree orig_t = t;
12524 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12525 tree *elts;
12526
12527 if (t == error_mark_node)
12528 return error_mark_node;
12529
12530 len = TREE_VEC_LENGTH (t);
12531 elts = XALLOCAVEC (tree, len);
12532
12533 for (i = 0; i < len; i++)
12534 {
12535 tree orig_arg = TREE_VEC_ELT (t, i);
12536 tree new_arg;
12537
12538 if (TREE_CODE (orig_arg) == TREE_VEC)
12539 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12540 else if (PACK_EXPANSION_P (orig_arg))
12541 {
12542 /* Substitute into an expansion expression. */
12543 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12544
12545 if (TREE_CODE (new_arg) == TREE_VEC)
12546 /* Add to the expanded length adjustment the number of
12547 expanded arguments. We subtract one from this
12548 measurement, because the argument pack expression
12549 itself is already counted as 1 in
12550 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12551 the argument pack is empty. */
12552 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12553 }
12554 else if (ARGUMENT_PACK_P (orig_arg))
12555 {
12556 /* Substitute into each of the arguments. */
12557 new_arg = TYPE_P (orig_arg)
12558 ? cxx_make_type (TREE_CODE (orig_arg))
12559 : make_node (TREE_CODE (orig_arg));
12560
12561 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12562 args, complain, in_decl);
12563 if (pack_args == error_mark_node)
12564 new_arg = error_mark_node;
12565 else
12566 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12567
12568 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12569 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12570 }
12571 else
12572 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12573
12574 if (new_arg == error_mark_node)
12575 return error_mark_node;
12576
12577 elts[i] = new_arg;
12578 if (new_arg != orig_arg)
12579 need_new = 1;
12580 }
12581
12582 if (!need_new)
12583 return t;
12584
12585 /* Make space for the expanded arguments coming from template
12586 argument packs. */
12587 t = make_tree_vec (len + expanded_len_adjust);
12588 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12589 arguments for a member template.
12590 In that case each TREE_VEC in ORIG_T represents a level of template
12591 arguments, and ORIG_T won't carry any non defaulted argument count.
12592 It will rather be the nested TREE_VECs that will carry one.
12593 In other words, ORIG_T carries a non defaulted argument count only
12594 if it doesn't contain any nested TREE_VEC. */
12595 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12596 {
12597 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12598 count += expanded_len_adjust;
12599 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12600 }
12601 for (i = 0, out = 0; i < len; i++)
12602 {
12603 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12604 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12605 && TREE_CODE (elts[i]) == TREE_VEC)
12606 {
12607 int idx;
12608
12609 /* Now expand the template argument pack "in place". */
12610 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12611 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12612 }
12613 else
12614 {
12615 TREE_VEC_ELT (t, out) = elts[i];
12616 out++;
12617 }
12618 }
12619
12620 return t;
12621 }
12622
12623 /* Substitute ARGS into one level PARMS of template parameters. */
12624
12625 static tree
12626 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12627 {
12628 if (parms == error_mark_node)
12629 return error_mark_node;
12630
12631 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12632
12633 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12634 {
12635 tree tuple = TREE_VEC_ELT (parms, i);
12636
12637 if (tuple == error_mark_node)
12638 continue;
12639
12640 TREE_VEC_ELT (new_vec, i) =
12641 tsubst_template_parm (tuple, args, complain);
12642 }
12643
12644 return new_vec;
12645 }
12646
12647 /* Return the result of substituting ARGS into the template parameters
12648 given by PARMS. If there are m levels of ARGS and m + n levels of
12649 PARMS, then the result will contain n levels of PARMS. For
12650 example, if PARMS is `template <class T> template <class U>
12651 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12652 result will be `template <int*, double, class V>'. */
12653
12654 static tree
12655 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12656 {
12657 tree r = NULL_TREE;
12658 tree* new_parms;
12659
12660 /* When substituting into a template, we must set
12661 PROCESSING_TEMPLATE_DECL as the template parameters may be
12662 dependent if they are based on one-another, and the dependency
12663 predicates are short-circuit outside of templates. */
12664 ++processing_template_decl;
12665
12666 for (new_parms = &r;
12667 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12668 new_parms = &(TREE_CHAIN (*new_parms)),
12669 parms = TREE_CHAIN (parms))
12670 {
12671 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12672 args, complain);
12673 *new_parms =
12674 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12675 - TMPL_ARGS_DEPTH (args)),
12676 new_vec, NULL_TREE);
12677 }
12678
12679 --processing_template_decl;
12680
12681 return r;
12682 }
12683
12684 /* Return the result of substituting ARGS into one template parameter
12685 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12686 parameter and which TREE_PURPOSE is the default argument of the
12687 template parameter. */
12688
12689 static tree
12690 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12691 {
12692 tree default_value, parm_decl;
12693
12694 if (args == NULL_TREE
12695 || t == NULL_TREE
12696 || t == error_mark_node)
12697 return t;
12698
12699 gcc_assert (TREE_CODE (t) == TREE_LIST);
12700
12701 default_value = TREE_PURPOSE (t);
12702 parm_decl = TREE_VALUE (t);
12703
12704 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12705 if (TREE_CODE (parm_decl) == PARM_DECL
12706 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12707 parm_decl = error_mark_node;
12708 default_value = tsubst_template_arg (default_value, args,
12709 complain, NULL_TREE);
12710
12711 return build_tree_list (default_value, parm_decl);
12712 }
12713
12714 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12715 type T. If T is not an aggregate or enumeration type, it is
12716 handled as if by tsubst. IN_DECL is as for tsubst. If
12717 ENTERING_SCOPE is nonzero, T is the context for a template which
12718 we are presently tsubst'ing. Return the substituted value. */
12719
12720 static tree
12721 tsubst_aggr_type (tree t,
12722 tree args,
12723 tsubst_flags_t complain,
12724 tree in_decl,
12725 int entering_scope)
12726 {
12727 if (t == NULL_TREE)
12728 return NULL_TREE;
12729
12730 switch (TREE_CODE (t))
12731 {
12732 case RECORD_TYPE:
12733 if (TYPE_PTRMEMFUNC_P (t))
12734 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12735
12736 /* Fall through. */
12737 case ENUMERAL_TYPE:
12738 case UNION_TYPE:
12739 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12740 {
12741 tree argvec;
12742 tree context;
12743 tree r;
12744
12745 /* In "sizeof(X<I>)" we need to evaluate "I". */
12746 cp_evaluated ev;
12747
12748 /* First, determine the context for the type we are looking
12749 up. */
12750 context = TYPE_CONTEXT (t);
12751 if (context && TYPE_P (context))
12752 {
12753 context = tsubst_aggr_type (context, args, complain,
12754 in_decl, /*entering_scope=*/1);
12755 /* If context is a nested class inside a class template,
12756 it may still need to be instantiated (c++/33959). */
12757 context = complete_type (context);
12758 }
12759
12760 /* Then, figure out what arguments are appropriate for the
12761 type we are trying to find. For example, given:
12762
12763 template <class T> struct S;
12764 template <class T, class U> void f(T, U) { S<U> su; }
12765
12766 and supposing that we are instantiating f<int, double>,
12767 then our ARGS will be {int, double}, but, when looking up
12768 S we only want {double}. */
12769 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12770 complain, in_decl);
12771 if (argvec == error_mark_node)
12772 r = error_mark_node;
12773 else
12774 {
12775 r = lookup_template_class (t, argvec, in_decl, context,
12776 entering_scope, complain);
12777 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12778 }
12779
12780 return r;
12781 }
12782 else
12783 /* This is not a template type, so there's nothing to do. */
12784 return t;
12785
12786 default:
12787 return tsubst (t, args, complain, in_decl);
12788 }
12789 }
12790
12791 static GTY((cache)) tree_cache_map *defarg_inst;
12792
12793 /* Substitute into the default argument ARG (a default argument for
12794 FN), which has the indicated TYPE. */
12795
12796 tree
12797 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12798 tsubst_flags_t complain)
12799 {
12800 int errs = errorcount + sorrycount;
12801
12802 /* This can happen in invalid code. */
12803 if (TREE_CODE (arg) == DEFAULT_ARG)
12804 return arg;
12805
12806 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12807 parm = chain_index (parmnum, parm);
12808 tree parmtype = TREE_TYPE (parm);
12809 if (DECL_BY_REFERENCE (parm))
12810 parmtype = TREE_TYPE (parmtype);
12811 if (parmtype == error_mark_node)
12812 return error_mark_node;
12813
12814 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12815
12816 tree *slot;
12817 if (defarg_inst && (slot = defarg_inst->get (parm)))
12818 return *slot;
12819
12820 /* This default argument came from a template. Instantiate the
12821 default argument here, not in tsubst. In the case of
12822 something like:
12823
12824 template <class T>
12825 struct S {
12826 static T t();
12827 void f(T = t());
12828 };
12829
12830 we must be careful to do name lookup in the scope of S<T>,
12831 rather than in the current class. */
12832 push_to_top_level ();
12833 push_access_scope (fn);
12834 push_deferring_access_checks (dk_no_deferred);
12835 start_lambda_scope (parm);
12836
12837 /* The default argument expression may cause implicitly defined
12838 member functions to be synthesized, which will result in garbage
12839 collection. We must treat this situation as if we were within
12840 the body of function so as to avoid collecting live data on the
12841 stack. */
12842 ++function_depth;
12843 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12844 complain, NULL_TREE,
12845 /*integral_constant_expression_p=*/false);
12846 --function_depth;
12847
12848 finish_lambda_scope ();
12849
12850 /* Make sure the default argument is reasonable. */
12851 arg = check_default_argument (type, arg, complain);
12852
12853 if (errorcount+sorrycount > errs
12854 && (complain & tf_warning_or_error))
12855 inform (input_location,
12856 " when instantiating default argument for call to %qD", fn);
12857
12858 pop_deferring_access_checks ();
12859 pop_access_scope (fn);
12860 pop_from_top_level ();
12861
12862 if (arg != error_mark_node && !cp_unevaluated_operand)
12863 {
12864 if (!defarg_inst)
12865 defarg_inst = tree_cache_map::create_ggc (37);
12866 defarg_inst->put (parm, arg);
12867 }
12868
12869 return arg;
12870 }
12871
12872 /* Substitute into all the default arguments for FN. */
12873
12874 static void
12875 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12876 {
12877 tree arg;
12878 tree tmpl_args;
12879
12880 tmpl_args = DECL_TI_ARGS (fn);
12881
12882 /* If this function is not yet instantiated, we certainly don't need
12883 its default arguments. */
12884 if (uses_template_parms (tmpl_args))
12885 return;
12886 /* Don't do this again for clones. */
12887 if (DECL_CLONED_FUNCTION_P (fn))
12888 return;
12889
12890 int i = 0;
12891 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12892 arg;
12893 arg = TREE_CHAIN (arg), ++i)
12894 if (TREE_PURPOSE (arg))
12895 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12896 TREE_VALUE (arg),
12897 TREE_PURPOSE (arg),
12898 complain);
12899 }
12900
12901 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12902 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12903
12904 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12905
12906 void
12907 store_explicit_specifier (tree v, tree t)
12908 {
12909 if (!explicit_specifier_map)
12910 explicit_specifier_map = tree_cache_map::create_ggc (37);
12911 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12912 explicit_specifier_map->put (v, t);
12913 }
12914
12915 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12916
12917 static tree
12918 lookup_explicit_specifier (tree v)
12919 {
12920 return *explicit_specifier_map->get (v);
12921 }
12922
12923 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12924
12925 static tree
12926 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12927 tree lambda_fntype)
12928 {
12929 tree gen_tmpl, argvec;
12930 hashval_t hash = 0;
12931 tree in_decl = t;
12932
12933 /* Nobody should be tsubst'ing into non-template functions. */
12934 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12935
12936 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12937 {
12938 /* If T is not dependent, just return it. */
12939 if (!uses_template_parms (DECL_TI_ARGS (t))
12940 && !LAMBDA_FUNCTION_P (t))
12941 return t;
12942
12943 /* Calculate the most general template of which R is a
12944 specialization. */
12945 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12946
12947 /* We're substituting a lambda function under tsubst_lambda_expr but not
12948 directly from it; find the matching function we're already inside.
12949 But don't do this if T is a generic lambda with a single level of
12950 template parms, as in that case we're doing a normal instantiation. */
12951 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12952 && (!generic_lambda_fn_p (t)
12953 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12954 return enclosing_instantiation_of (t);
12955
12956 /* Calculate the complete set of arguments used to
12957 specialize R. */
12958 argvec = tsubst_template_args (DECL_TI_ARGS
12959 (DECL_TEMPLATE_RESULT
12960 (DECL_TI_TEMPLATE (t))),
12961 args, complain, in_decl);
12962 if (argvec == error_mark_node)
12963 return error_mark_node;
12964
12965 /* Check to see if we already have this specialization. */
12966 if (!lambda_fntype)
12967 {
12968 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12969 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12970 return spec;
12971 }
12972
12973 /* We can see more levels of arguments than parameters if
12974 there was a specialization of a member template, like
12975 this:
12976
12977 template <class T> struct S { template <class U> void f(); }
12978 template <> template <class U> void S<int>::f(U);
12979
12980 Here, we'll be substituting into the specialization,
12981 because that's where we can find the code we actually
12982 want to generate, but we'll have enough arguments for
12983 the most general template.
12984
12985 We also deal with the peculiar case:
12986
12987 template <class T> struct S {
12988 template <class U> friend void f();
12989 };
12990 template <class U> void f() {}
12991 template S<int>;
12992 template void f<double>();
12993
12994 Here, the ARGS for the instantiation of will be {int,
12995 double}. But, we only need as many ARGS as there are
12996 levels of template parameters in CODE_PATTERN. We are
12997 careful not to get fooled into reducing the ARGS in
12998 situations like:
12999
13000 template <class T> struct S { template <class U> void f(U); }
13001 template <class T> template <> void S<T>::f(int) {}
13002
13003 which we can spot because the pattern will be a
13004 specialization in this case. */
13005 int args_depth = TMPL_ARGS_DEPTH (args);
13006 int parms_depth =
13007 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13008
13009 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13010 args = get_innermost_template_args (args, parms_depth);
13011 }
13012 else
13013 {
13014 /* This special case arises when we have something like this:
13015
13016 template <class T> struct S {
13017 friend void f<int>(int, double);
13018 };
13019
13020 Here, the DECL_TI_TEMPLATE for the friend declaration
13021 will be an IDENTIFIER_NODE. We are being called from
13022 tsubst_friend_function, and we want only to create a
13023 new decl (R) with appropriate types so that we can call
13024 determine_specialization. */
13025 gen_tmpl = NULL_TREE;
13026 argvec = NULL_TREE;
13027 }
13028
13029 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13030 : NULL_TREE);
13031 tree ctx = closure ? closure : DECL_CONTEXT (t);
13032 bool member = ctx && TYPE_P (ctx);
13033
13034 if (member && !closure)
13035 ctx = tsubst_aggr_type (ctx, args,
13036 complain, t, /*entering_scope=*/1);
13037
13038 tree type = (lambda_fntype ? lambda_fntype
13039 : tsubst (TREE_TYPE (t), args,
13040 complain | tf_fndecl_type, in_decl));
13041 if (type == error_mark_node)
13042 return error_mark_node;
13043
13044 /* If we hit excessive deduction depth, the type is bogus even if
13045 it isn't error_mark_node, so don't build a decl. */
13046 if (excessive_deduction_depth)
13047 return error_mark_node;
13048
13049 /* We do NOT check for matching decls pushed separately at this
13050 point, as they may not represent instantiations of this
13051 template, and in any case are considered separate under the
13052 discrete model. */
13053 tree r = copy_decl (t);
13054 DECL_USE_TEMPLATE (r) = 0;
13055 TREE_TYPE (r) = type;
13056 /* Clear out the mangled name and RTL for the instantiation. */
13057 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13058 SET_DECL_RTL (r, NULL);
13059 /* Leave DECL_INITIAL set on deleted instantiations. */
13060 if (!DECL_DELETED_FN (r))
13061 DECL_INITIAL (r) = NULL_TREE;
13062 DECL_CONTEXT (r) = ctx;
13063
13064 /* Handle explicit(dependent-expr). */
13065 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13066 {
13067 tree spec = lookup_explicit_specifier (t);
13068 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13069 /*function_p=*/false,
13070 /*i_c_e_p=*/true);
13071 spec = build_explicit_specifier (spec, complain);
13072 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13073 }
13074
13075 /* OpenMP UDRs have the only argument a reference to the declared
13076 type. We want to diagnose if the declared type is a reference,
13077 which is invalid, but as references to references are usually
13078 quietly merged, diagnose it here. */
13079 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13080 {
13081 tree argtype
13082 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13083 argtype = tsubst (argtype, args, complain, in_decl);
13084 if (TYPE_REF_P (argtype))
13085 error_at (DECL_SOURCE_LOCATION (t),
13086 "reference type %qT in "
13087 "%<#pragma omp declare reduction%>", argtype);
13088 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13089 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13090 argtype);
13091 }
13092
13093 if (member && DECL_CONV_FN_P (r))
13094 /* Type-conversion operator. Reconstruct the name, in
13095 case it's the name of one of the template's parameters. */
13096 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13097
13098 tree parms = DECL_ARGUMENTS (t);
13099 if (closure)
13100 parms = DECL_CHAIN (parms);
13101 parms = tsubst (parms, args, complain, t);
13102 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13103 DECL_CONTEXT (parm) = r;
13104 if (closure)
13105 {
13106 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13107 DECL_CHAIN (tparm) = parms;
13108 parms = tparm;
13109 }
13110 DECL_ARGUMENTS (r) = parms;
13111 DECL_RESULT (r) = NULL_TREE;
13112
13113 TREE_STATIC (r) = 0;
13114 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13115 DECL_EXTERNAL (r) = 1;
13116 /* If this is an instantiation of a function with internal
13117 linkage, we already know what object file linkage will be
13118 assigned to the instantiation. */
13119 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13120 DECL_DEFER_OUTPUT (r) = 0;
13121 DECL_CHAIN (r) = NULL_TREE;
13122 DECL_PENDING_INLINE_INFO (r) = 0;
13123 DECL_PENDING_INLINE_P (r) = 0;
13124 DECL_SAVED_TREE (r) = NULL_TREE;
13125 DECL_STRUCT_FUNCTION (r) = NULL;
13126 TREE_USED (r) = 0;
13127 /* We'll re-clone as appropriate in instantiate_template. */
13128 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13129
13130 /* If we aren't complaining now, return on error before we register
13131 the specialization so that we'll complain eventually. */
13132 if ((complain & tf_error) == 0
13133 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13134 && !grok_op_properties (r, /*complain=*/false))
13135 return error_mark_node;
13136
13137 /* When instantiating a constrained member, substitute
13138 into the constraints to create a new constraint. */
13139 if (tree ci = get_constraints (t))
13140 if (member)
13141 {
13142 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13143 set_constraints (r, ci);
13144 }
13145
13146 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13147 SET_DECL_FRIEND_CONTEXT (r,
13148 tsubst (DECL_FRIEND_CONTEXT (t),
13149 args, complain, in_decl));
13150
13151 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13152 this in the special friend case mentioned above where
13153 GEN_TMPL is NULL. */
13154 if (gen_tmpl && !closure)
13155 {
13156 DECL_TEMPLATE_INFO (r)
13157 = build_template_info (gen_tmpl, argvec);
13158 SET_DECL_IMPLICIT_INSTANTIATION (r);
13159
13160 tree new_r
13161 = register_specialization (r, gen_tmpl, argvec, false, hash);
13162 if (new_r != r)
13163 /* We instantiated this while substituting into
13164 the type earlier (template/friend54.C). */
13165 return new_r;
13166
13167 /* We're not supposed to instantiate default arguments
13168 until they are called, for a template. But, for a
13169 declaration like:
13170
13171 template <class T> void f ()
13172 { extern void g(int i = T()); }
13173
13174 we should do the substitution when the template is
13175 instantiated. We handle the member function case in
13176 instantiate_class_template since the default arguments
13177 might refer to other members of the class. */
13178 if (!member
13179 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13180 && !uses_template_parms (argvec))
13181 tsubst_default_arguments (r, complain);
13182 }
13183 else
13184 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13185
13186 /* Copy the list of befriending classes. */
13187 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13188 *friends;
13189 friends = &TREE_CHAIN (*friends))
13190 {
13191 *friends = copy_node (*friends);
13192 TREE_VALUE (*friends)
13193 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13194 }
13195
13196 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13197 {
13198 maybe_retrofit_in_chrg (r);
13199 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13200 return error_mark_node;
13201 /* If this is an instantiation of a member template, clone it.
13202 If it isn't, that'll be handled by
13203 clone_constructors_and_destructors. */
13204 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13205 clone_function_decl (r, /*update_methods=*/false);
13206 }
13207 else if ((complain & tf_error) != 0
13208 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13209 && !grok_op_properties (r, /*complain=*/true))
13210 return error_mark_node;
13211
13212 /* Possibly limit visibility based on template args. */
13213 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13214 if (DECL_VISIBILITY_SPECIFIED (t))
13215 {
13216 DECL_VISIBILITY_SPECIFIED (r) = 0;
13217 DECL_ATTRIBUTES (r)
13218 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13219 }
13220 determine_visibility (r);
13221 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13222 && !processing_template_decl)
13223 defaulted_late_check (r);
13224
13225 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13226 args, complain, in_decl);
13227 return r;
13228 }
13229
13230 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13231
13232 static tree
13233 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13234 tree lambda_fntype)
13235 {
13236 /* We can get here when processing a member function template,
13237 member class template, or template template parameter. */
13238 tree decl = DECL_TEMPLATE_RESULT (t);
13239 tree in_decl = t;
13240 tree spec;
13241 tree tmpl_args;
13242 tree full_args;
13243 tree r;
13244 hashval_t hash = 0;
13245
13246 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13247 {
13248 /* Template template parameter is treated here. */
13249 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13250 if (new_type == error_mark_node)
13251 r = error_mark_node;
13252 /* If we get a real template back, return it. This can happen in
13253 the context of most_specialized_partial_spec. */
13254 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13255 r = new_type;
13256 else
13257 /* The new TEMPLATE_DECL was built in
13258 reduce_template_parm_level. */
13259 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13260 return r;
13261 }
13262
13263 if (!lambda_fntype)
13264 {
13265 /* We might already have an instance of this template.
13266 The ARGS are for the surrounding class type, so the
13267 full args contain the tsubst'd args for the context,
13268 plus the innermost args from the template decl. */
13269 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13270 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13271 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13272 /* Because this is a template, the arguments will still be
13273 dependent, even after substitution. If
13274 PROCESSING_TEMPLATE_DECL is not set, the dependency
13275 predicates will short-circuit. */
13276 ++processing_template_decl;
13277 full_args = tsubst_template_args (tmpl_args, args,
13278 complain, in_decl);
13279 --processing_template_decl;
13280 if (full_args == error_mark_node)
13281 return error_mark_node;
13282
13283 /* If this is a default template template argument,
13284 tsubst might not have changed anything. */
13285 if (full_args == tmpl_args)
13286 return t;
13287
13288 hash = hash_tmpl_and_args (t, full_args);
13289 spec = retrieve_specialization (t, full_args, hash);
13290 if (spec != NULL_TREE)
13291 {
13292 if (TYPE_P (spec))
13293 /* Type partial instantiations are stored as the type by
13294 lookup_template_class_1, not here as the template. */
13295 spec = CLASSTYPE_TI_TEMPLATE (spec);
13296 return spec;
13297 }
13298 }
13299
13300 /* Make a new template decl. It will be similar to the
13301 original, but will record the current template arguments.
13302 We also create a new function declaration, which is just
13303 like the old one, but points to this new template, rather
13304 than the old one. */
13305 r = copy_decl (t);
13306 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13307 DECL_CHAIN (r) = NULL_TREE;
13308
13309 // Build new template info linking to the original template decl.
13310 if (!lambda_fntype)
13311 {
13312 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13313 SET_DECL_IMPLICIT_INSTANTIATION (r);
13314 }
13315 else
13316 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13317
13318 /* The template parameters for this new template are all the
13319 template parameters for the old template, except the
13320 outermost level of parameters. */
13321 DECL_TEMPLATE_PARMS (r)
13322 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13323 complain);
13324
13325 if (TREE_CODE (decl) == TYPE_DECL
13326 && !TYPE_DECL_ALIAS_P (decl))
13327 {
13328 tree new_type;
13329 ++processing_template_decl;
13330 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13331 --processing_template_decl;
13332 if (new_type == error_mark_node)
13333 return error_mark_node;
13334
13335 TREE_TYPE (r) = new_type;
13336 /* For a partial specialization, we need to keep pointing to
13337 the primary template. */
13338 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13339 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13340 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13341 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13342 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13343 }
13344 else
13345 {
13346 tree new_decl;
13347 ++processing_template_decl;
13348 if (TREE_CODE (decl) == FUNCTION_DECL)
13349 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13350 else
13351 new_decl = tsubst (decl, args, complain, in_decl);
13352 --processing_template_decl;
13353 if (new_decl == error_mark_node)
13354 return error_mark_node;
13355
13356 DECL_TEMPLATE_RESULT (r) = new_decl;
13357 TREE_TYPE (r) = TREE_TYPE (new_decl);
13358 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13359 if (lambda_fntype)
13360 {
13361 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13362 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13363 }
13364 else
13365 {
13366 DECL_TI_TEMPLATE (new_decl) = r;
13367 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13368 }
13369 }
13370
13371 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13372 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13373
13374 if (PRIMARY_TEMPLATE_P (t))
13375 DECL_PRIMARY_TEMPLATE (r) = r;
13376
13377 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13378 && !lambda_fntype)
13379 /* Record this non-type partial instantiation. */
13380 register_specialization (r, t,
13381 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13382 false, hash);
13383
13384 return r;
13385 }
13386
13387 /* True if FN is the op() for a lambda in an uninstantiated template. */
13388
13389 bool
13390 lambda_fn_in_template_p (tree fn)
13391 {
13392 if (!fn || !LAMBDA_FUNCTION_P (fn))
13393 return false;
13394 tree closure = DECL_CONTEXT (fn);
13395 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13396 }
13397
13398 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13399 which the above is true. */
13400
13401 bool
13402 instantiated_lambda_fn_p (tree fn)
13403 {
13404 if (!fn || !LAMBDA_FUNCTION_P (fn))
13405 return false;
13406 tree closure = DECL_CONTEXT (fn);
13407 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13408 return LAMBDA_EXPR_INSTANTIATED (lam);
13409 }
13410
13411 /* We're instantiating a variable from template function TCTX. Return the
13412 corresponding current enclosing scope. This gets complicated because lambda
13413 functions in templates are regenerated rather than instantiated, but generic
13414 lambda functions are subsequently instantiated. */
13415
13416 static tree
13417 enclosing_instantiation_of (tree otctx)
13418 {
13419 tree tctx = otctx;
13420 tree fn = current_function_decl;
13421 int lambda_count = 0;
13422
13423 for (; tctx && (lambda_fn_in_template_p (tctx)
13424 || instantiated_lambda_fn_p (tctx));
13425 tctx = decl_function_context (tctx))
13426 ++lambda_count;
13427 for (; fn; fn = decl_function_context (fn))
13428 {
13429 tree ofn = fn;
13430 int flambda_count = 0;
13431 for (; fn && instantiated_lambda_fn_p (fn);
13432 fn = decl_function_context (fn))
13433 ++flambda_count;
13434 if ((fn && DECL_TEMPLATE_INFO (fn))
13435 ? most_general_template (fn) != most_general_template (tctx)
13436 : fn != tctx)
13437 continue;
13438 if (flambda_count != lambda_count)
13439 {
13440 gcc_assert (flambda_count > lambda_count);
13441 for (; flambda_count > lambda_count; --flambda_count)
13442 ofn = decl_function_context (ofn);
13443 }
13444 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13445 || DECL_CONV_FN_P (ofn));
13446 return ofn;
13447 }
13448 gcc_unreachable ();
13449 }
13450
13451 /* Substitute the ARGS into the T, which is a _DECL. Return the
13452 result of the substitution. Issue error and warning messages under
13453 control of COMPLAIN. */
13454
13455 static tree
13456 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13457 {
13458 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13459 location_t saved_loc;
13460 tree r = NULL_TREE;
13461 tree in_decl = t;
13462 hashval_t hash = 0;
13463
13464 /* Set the filename and linenumber to improve error-reporting. */
13465 saved_loc = input_location;
13466 input_location = DECL_SOURCE_LOCATION (t);
13467
13468 switch (TREE_CODE (t))
13469 {
13470 case TEMPLATE_DECL:
13471 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13472 break;
13473
13474 case FUNCTION_DECL:
13475 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13476 break;
13477
13478 case PARM_DECL:
13479 {
13480 tree type = NULL_TREE;
13481 int i, len = 1;
13482 tree expanded_types = NULL_TREE;
13483 tree prev_r = NULL_TREE;
13484 tree first_r = NULL_TREE;
13485
13486 if (DECL_PACK_P (t))
13487 {
13488 /* If there is a local specialization that isn't a
13489 parameter pack, it means that we're doing a "simple"
13490 substitution from inside tsubst_pack_expansion. Just
13491 return the local specialization (which will be a single
13492 parm). */
13493 tree spec = retrieve_local_specialization (t);
13494 if (spec
13495 && TREE_CODE (spec) == PARM_DECL
13496 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13497 RETURN (spec);
13498
13499 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13500 the parameters in this function parameter pack. */
13501 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13502 complain, in_decl);
13503 if (TREE_CODE (expanded_types) == TREE_VEC)
13504 {
13505 len = TREE_VEC_LENGTH (expanded_types);
13506
13507 /* Zero-length parameter packs are boring. Just substitute
13508 into the chain. */
13509 if (len == 0)
13510 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13511 TREE_CHAIN (t)));
13512 }
13513 else
13514 {
13515 /* All we did was update the type. Make a note of that. */
13516 type = expanded_types;
13517 expanded_types = NULL_TREE;
13518 }
13519 }
13520
13521 /* Loop through all of the parameters we'll build. When T is
13522 a function parameter pack, LEN is the number of expanded
13523 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13524 r = NULL_TREE;
13525 for (i = 0; i < len; ++i)
13526 {
13527 prev_r = r;
13528 r = copy_node (t);
13529 if (DECL_TEMPLATE_PARM_P (t))
13530 SET_DECL_TEMPLATE_PARM_P (r);
13531
13532 if (expanded_types)
13533 /* We're on the Ith parameter of the function parameter
13534 pack. */
13535 {
13536 /* Get the Ith type. */
13537 type = TREE_VEC_ELT (expanded_types, i);
13538
13539 /* Rename the parameter to include the index. */
13540 DECL_NAME (r)
13541 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13542 }
13543 else if (!type)
13544 /* We're dealing with a normal parameter. */
13545 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13546
13547 type = type_decays_to (type);
13548 TREE_TYPE (r) = type;
13549 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13550
13551 if (DECL_INITIAL (r))
13552 {
13553 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13554 DECL_INITIAL (r) = TREE_TYPE (r);
13555 else
13556 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13557 complain, in_decl);
13558 }
13559
13560 DECL_CONTEXT (r) = NULL_TREE;
13561
13562 if (!DECL_TEMPLATE_PARM_P (r))
13563 DECL_ARG_TYPE (r) = type_passed_as (type);
13564
13565 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13566 args, complain, in_decl);
13567
13568 /* Keep track of the first new parameter we
13569 generate. That's what will be returned to the
13570 caller. */
13571 if (!first_r)
13572 first_r = r;
13573
13574 /* Build a proper chain of parameters when substituting
13575 into a function parameter pack. */
13576 if (prev_r)
13577 DECL_CHAIN (prev_r) = r;
13578 }
13579
13580 /* If cp_unevaluated_operand is set, we're just looking for a
13581 single dummy parameter, so don't keep going. */
13582 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13583 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13584 complain, DECL_CHAIN (t));
13585
13586 /* FIRST_R contains the start of the chain we've built. */
13587 r = first_r;
13588 }
13589 break;
13590
13591 case FIELD_DECL:
13592 {
13593 tree type = NULL_TREE;
13594 tree vec = NULL_TREE;
13595 tree expanded_types = NULL_TREE;
13596 int len = 1;
13597
13598 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13599 {
13600 /* This field is a lambda capture pack. Return a TREE_VEC of
13601 the expanded fields to instantiate_class_template_1. */
13602 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13603 complain, in_decl);
13604 if (TREE_CODE (expanded_types) == TREE_VEC)
13605 {
13606 len = TREE_VEC_LENGTH (expanded_types);
13607 vec = make_tree_vec (len);
13608 }
13609 else
13610 {
13611 /* All we did was update the type. Make a note of that. */
13612 type = expanded_types;
13613 expanded_types = NULL_TREE;
13614 }
13615 }
13616
13617 for (int i = 0; i < len; ++i)
13618 {
13619 r = copy_decl (t);
13620 if (expanded_types)
13621 {
13622 type = TREE_VEC_ELT (expanded_types, i);
13623 DECL_NAME (r)
13624 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13625 }
13626 else if (!type)
13627 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13628
13629 if (type == error_mark_node)
13630 RETURN (error_mark_node);
13631 TREE_TYPE (r) = type;
13632 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13633
13634 if (DECL_C_BIT_FIELD (r))
13635 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13636 number of bits. */
13637 DECL_BIT_FIELD_REPRESENTATIVE (r)
13638 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13639 complain, in_decl,
13640 /*integral_constant_expression_p=*/true);
13641 if (DECL_INITIAL (t))
13642 {
13643 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13644 NSDMI in perform_member_init. Still set DECL_INITIAL
13645 so that we know there is one. */
13646 DECL_INITIAL (r) = void_node;
13647 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13648 retrofit_lang_decl (r);
13649 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13650 }
13651 /* We don't have to set DECL_CONTEXT here; it is set by
13652 finish_member_declaration. */
13653 DECL_CHAIN (r) = NULL_TREE;
13654
13655 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13656 args, complain, in_decl);
13657
13658 if (vec)
13659 TREE_VEC_ELT (vec, i) = r;
13660 }
13661
13662 if (vec)
13663 r = vec;
13664 }
13665 break;
13666
13667 case USING_DECL:
13668 /* We reach here only for member using decls. We also need to check
13669 uses_template_parms because DECL_DEPENDENT_P is not set for a
13670 using-declaration that designates a member of the current
13671 instantiation (c++/53549). */
13672 if (DECL_DEPENDENT_P (t)
13673 || uses_template_parms (USING_DECL_SCOPE (t)))
13674 {
13675 tree scope = USING_DECL_SCOPE (t);
13676 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13677 if (PACK_EXPANSION_P (scope))
13678 {
13679 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13680 int len = TREE_VEC_LENGTH (vec);
13681 r = make_tree_vec (len);
13682 for (int i = 0; i < len; ++i)
13683 {
13684 tree escope = TREE_VEC_ELT (vec, i);
13685 tree elt = do_class_using_decl (escope, name);
13686 if (!elt)
13687 {
13688 r = error_mark_node;
13689 break;
13690 }
13691 else
13692 {
13693 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13694 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13695 }
13696 TREE_VEC_ELT (r, i) = elt;
13697 }
13698 }
13699 else
13700 {
13701 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13702 complain, in_decl);
13703 r = do_class_using_decl (inst_scope, name);
13704 if (!r)
13705 r = error_mark_node;
13706 else
13707 {
13708 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13709 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13710 }
13711 }
13712 }
13713 else
13714 {
13715 r = copy_node (t);
13716 DECL_CHAIN (r) = NULL_TREE;
13717 }
13718 break;
13719
13720 case TYPE_DECL:
13721 case VAR_DECL:
13722 {
13723 tree argvec = NULL_TREE;
13724 tree gen_tmpl = NULL_TREE;
13725 tree spec;
13726 tree tmpl = NULL_TREE;
13727 tree ctx;
13728 tree type = NULL_TREE;
13729 bool local_p;
13730
13731 if (TREE_TYPE (t) == error_mark_node)
13732 RETURN (error_mark_node);
13733
13734 if (TREE_CODE (t) == TYPE_DECL
13735 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13736 {
13737 /* If this is the canonical decl, we don't have to
13738 mess with instantiations, and often we can't (for
13739 typename, template type parms and such). Note that
13740 TYPE_NAME is not correct for the above test if
13741 we've copied the type for a typedef. */
13742 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13743 if (type == error_mark_node)
13744 RETURN (error_mark_node);
13745 r = TYPE_NAME (type);
13746 break;
13747 }
13748
13749 /* Check to see if we already have the specialization we
13750 need. */
13751 spec = NULL_TREE;
13752 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13753 {
13754 /* T is a static data member or namespace-scope entity.
13755 We have to substitute into namespace-scope variables
13756 (not just variable templates) because of cases like:
13757
13758 template <class T> void f() { extern T t; }
13759
13760 where the entity referenced is not known until
13761 instantiation time. */
13762 local_p = false;
13763 ctx = DECL_CONTEXT (t);
13764 if (DECL_CLASS_SCOPE_P (t))
13765 {
13766 ctx = tsubst_aggr_type (ctx, args,
13767 complain,
13768 in_decl, /*entering_scope=*/1);
13769 /* If CTX is unchanged, then T is in fact the
13770 specialization we want. That situation occurs when
13771 referencing a static data member within in its own
13772 class. We can use pointer equality, rather than
13773 same_type_p, because DECL_CONTEXT is always
13774 canonical... */
13775 if (ctx == DECL_CONTEXT (t)
13776 /* ... unless T is a member template; in which
13777 case our caller can be willing to create a
13778 specialization of that template represented
13779 by T. */
13780 && !(DECL_TI_TEMPLATE (t)
13781 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13782 spec = t;
13783 }
13784
13785 if (!spec)
13786 {
13787 tmpl = DECL_TI_TEMPLATE (t);
13788 gen_tmpl = most_general_template (tmpl);
13789 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13790 if (argvec != error_mark_node)
13791 argvec = (coerce_innermost_template_parms
13792 (DECL_TEMPLATE_PARMS (gen_tmpl),
13793 argvec, t, complain,
13794 /*all*/true, /*defarg*/true));
13795 if (argvec == error_mark_node)
13796 RETURN (error_mark_node);
13797 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13798 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13799 }
13800 }
13801 else
13802 {
13803 /* A local variable. */
13804 local_p = true;
13805 /* Subsequent calls to pushdecl will fill this in. */
13806 ctx = NULL_TREE;
13807 /* Unless this is a reference to a static variable from an
13808 enclosing function, in which case we need to fill it in now. */
13809 if (TREE_STATIC (t))
13810 {
13811 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13812 if (fn != current_function_decl)
13813 ctx = fn;
13814 }
13815 spec = retrieve_local_specialization (t);
13816 }
13817 /* If we already have the specialization we need, there is
13818 nothing more to do. */
13819 if (spec)
13820 {
13821 r = spec;
13822 break;
13823 }
13824
13825 /* Create a new node for the specialization we need. */
13826 if (type == NULL_TREE)
13827 {
13828 if (is_typedef_decl (t))
13829 type = DECL_ORIGINAL_TYPE (t);
13830 else
13831 type = TREE_TYPE (t);
13832 if (VAR_P (t)
13833 && VAR_HAD_UNKNOWN_BOUND (t)
13834 && type != error_mark_node)
13835 type = strip_array_domain (type);
13836 tree sub_args = args;
13837 if (tree auto_node = type_uses_auto (type))
13838 {
13839 /* Mask off any template args past the variable's context so we
13840 don't replace the auto with an unrelated argument. */
13841 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13842 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13843 if (extra > 0)
13844 /* This should never happen with the new lambda instantiation
13845 model, but keep the handling just in case. */
13846 gcc_assert (!CHECKING_P),
13847 sub_args = strip_innermost_template_args (args, extra);
13848 }
13849 type = tsubst (type, sub_args, complain, in_decl);
13850 /* Substituting the type might have recursively instantiated this
13851 same alias (c++/86171). */
13852 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13853 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13854 {
13855 r = spec;
13856 break;
13857 }
13858 }
13859 r = copy_decl (t);
13860 if (VAR_P (r))
13861 {
13862 DECL_INITIALIZED_P (r) = 0;
13863 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13864 if (type == error_mark_node)
13865 RETURN (error_mark_node);
13866 if (TREE_CODE (type) == FUNCTION_TYPE)
13867 {
13868 /* It may seem that this case cannot occur, since:
13869
13870 typedef void f();
13871 void g() { f x; }
13872
13873 declares a function, not a variable. However:
13874
13875 typedef void f();
13876 template <typename T> void g() { T t; }
13877 template void g<f>();
13878
13879 is an attempt to declare a variable with function
13880 type. */
13881 error ("variable %qD has function type",
13882 /* R is not yet sufficiently initialized, so we
13883 just use its name. */
13884 DECL_NAME (r));
13885 RETURN (error_mark_node);
13886 }
13887 type = complete_type (type);
13888 /* Wait until cp_finish_decl to set this again, to handle
13889 circular dependency (template/instantiate6.C). */
13890 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13891 type = check_var_type (DECL_NAME (r), type);
13892
13893 if (DECL_HAS_VALUE_EXPR_P (t))
13894 {
13895 tree ve = DECL_VALUE_EXPR (t);
13896 ve = tsubst_expr (ve, args, complain, in_decl,
13897 /*constant_expression_p=*/false);
13898 if (REFERENCE_REF_P (ve))
13899 {
13900 gcc_assert (TYPE_REF_P (type));
13901 ve = TREE_OPERAND (ve, 0);
13902 }
13903 SET_DECL_VALUE_EXPR (r, ve);
13904 }
13905 if (CP_DECL_THREAD_LOCAL_P (r)
13906 && !processing_template_decl)
13907 set_decl_tls_model (r, decl_default_tls_model (r));
13908 }
13909 else if (DECL_SELF_REFERENCE_P (t))
13910 SET_DECL_SELF_REFERENCE_P (r);
13911 TREE_TYPE (r) = type;
13912 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13913 DECL_CONTEXT (r) = ctx;
13914 /* Clear out the mangled name and RTL for the instantiation. */
13915 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13916 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13917 SET_DECL_RTL (r, NULL);
13918 /* The initializer must not be expanded until it is required;
13919 see [temp.inst]. */
13920 DECL_INITIAL (r) = NULL_TREE;
13921 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13922 if (VAR_P (r))
13923 {
13924 if (DECL_LANG_SPECIFIC (r))
13925 SET_DECL_DEPENDENT_INIT_P (r, false);
13926
13927 SET_DECL_MODE (r, VOIDmode);
13928
13929 /* Possibly limit visibility based on template args. */
13930 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13931 if (DECL_VISIBILITY_SPECIFIED (t))
13932 {
13933 DECL_VISIBILITY_SPECIFIED (r) = 0;
13934 DECL_ATTRIBUTES (r)
13935 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13936 }
13937 determine_visibility (r);
13938 }
13939
13940 if (!local_p)
13941 {
13942 /* A static data member declaration is always marked
13943 external when it is declared in-class, even if an
13944 initializer is present. We mimic the non-template
13945 processing here. */
13946 DECL_EXTERNAL (r) = 1;
13947 if (DECL_NAMESPACE_SCOPE_P (t))
13948 DECL_NOT_REALLY_EXTERN (r) = 1;
13949
13950 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13951 SET_DECL_IMPLICIT_INSTANTIATION (r);
13952 if (!error_operand_p (r) || (complain & tf_error))
13953 register_specialization (r, gen_tmpl, argvec, false, hash);
13954 }
13955 else
13956 {
13957 if (DECL_LANG_SPECIFIC (r))
13958 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13959 if (!cp_unevaluated_operand)
13960 register_local_specialization (r, t);
13961 }
13962
13963 DECL_CHAIN (r) = NULL_TREE;
13964
13965 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13966 /*flags=*/0,
13967 args, complain, in_decl);
13968
13969 /* Preserve a typedef that names a type. */
13970 if (is_typedef_decl (r) && type != error_mark_node)
13971 {
13972 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13973 set_underlying_type (r);
13974 if (TYPE_DECL_ALIAS_P (r))
13975 /* An alias template specialization can be dependent
13976 even if its underlying type is not. */
13977 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13978 }
13979
13980 layout_decl (r, 0);
13981 }
13982 break;
13983
13984 default:
13985 gcc_unreachable ();
13986 }
13987 #undef RETURN
13988
13989 out:
13990 /* Restore the file and line information. */
13991 input_location = saved_loc;
13992
13993 return r;
13994 }
13995
13996 /* Substitute into the ARG_TYPES of a function type.
13997 If END is a TREE_CHAIN, leave it and any following types
13998 un-substituted. */
13999
14000 static tree
14001 tsubst_arg_types (tree arg_types,
14002 tree args,
14003 tree end,
14004 tsubst_flags_t complain,
14005 tree in_decl)
14006 {
14007 tree remaining_arg_types;
14008 tree type = NULL_TREE;
14009 int i = 1;
14010 tree expanded_args = NULL_TREE;
14011 tree default_arg;
14012
14013 if (!arg_types || arg_types == void_list_node || arg_types == end)
14014 return arg_types;
14015
14016 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14017 args, end, complain, in_decl);
14018 if (remaining_arg_types == error_mark_node)
14019 return error_mark_node;
14020
14021 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14022 {
14023 /* For a pack expansion, perform substitution on the
14024 entire expression. Later on, we'll handle the arguments
14025 one-by-one. */
14026 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14027 args, complain, in_decl);
14028
14029 if (TREE_CODE (expanded_args) == TREE_VEC)
14030 /* So that we'll spin through the parameters, one by one. */
14031 i = TREE_VEC_LENGTH (expanded_args);
14032 else
14033 {
14034 /* We only partially substituted into the parameter
14035 pack. Our type is TYPE_PACK_EXPANSION. */
14036 type = expanded_args;
14037 expanded_args = NULL_TREE;
14038 }
14039 }
14040
14041 while (i > 0) {
14042 --i;
14043
14044 if (expanded_args)
14045 type = TREE_VEC_ELT (expanded_args, i);
14046 else if (!type)
14047 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14048
14049 if (type == error_mark_node)
14050 return error_mark_node;
14051 if (VOID_TYPE_P (type))
14052 {
14053 if (complain & tf_error)
14054 {
14055 error ("invalid parameter type %qT", type);
14056 if (in_decl)
14057 error ("in declaration %q+D", in_decl);
14058 }
14059 return error_mark_node;
14060 }
14061 /* DR 657. */
14062 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14063 return error_mark_node;
14064
14065 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14066 top-level qualifiers as required. */
14067 type = cv_unqualified (type_decays_to (type));
14068
14069 /* We do not substitute into default arguments here. The standard
14070 mandates that they be instantiated only when needed, which is
14071 done in build_over_call. */
14072 default_arg = TREE_PURPOSE (arg_types);
14073
14074 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14075 since the new op() won't have any associated template arguments for us
14076 to refer to later. */
14077 if (lambda_fn_in_template_p (in_decl))
14078 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14079 false/*fn*/, false/*constexpr*/);
14080
14081 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
14082 {
14083 /* We've instantiated a template before its default arguments
14084 have been parsed. This can happen for a nested template
14085 class, and is not an error unless we require the default
14086 argument in a call of this function. */
14087 remaining_arg_types =
14088 tree_cons (default_arg, type, remaining_arg_types);
14089 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
14090 }
14091 else
14092 remaining_arg_types =
14093 hash_tree_cons (default_arg, type, remaining_arg_types);
14094 }
14095
14096 return remaining_arg_types;
14097 }
14098
14099 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14100 *not* handle the exception-specification for FNTYPE, because the
14101 initial substitution of explicitly provided template parameters
14102 during argument deduction forbids substitution into the
14103 exception-specification:
14104
14105 [temp.deduct]
14106
14107 All references in the function type of the function template to the
14108 corresponding template parameters are replaced by the specified tem-
14109 plate argument values. If a substitution in a template parameter or
14110 in the function type of the function template results in an invalid
14111 type, type deduction fails. [Note: The equivalent substitution in
14112 exception specifications is done only when the function is instanti-
14113 ated, at which point a program is ill-formed if the substitution
14114 results in an invalid type.] */
14115
14116 static tree
14117 tsubst_function_type (tree t,
14118 tree args,
14119 tsubst_flags_t complain,
14120 tree in_decl)
14121 {
14122 tree return_type;
14123 tree arg_types = NULL_TREE;
14124 tree fntype;
14125
14126 /* The TYPE_CONTEXT is not used for function/method types. */
14127 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14128
14129 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14130 failure. */
14131 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14132
14133 if (late_return_type_p)
14134 {
14135 /* Substitute the argument types. */
14136 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14137 complain, in_decl);
14138 if (arg_types == error_mark_node)
14139 return error_mark_node;
14140
14141 tree save_ccp = current_class_ptr;
14142 tree save_ccr = current_class_ref;
14143 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14144 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14145 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14146 if (do_inject)
14147 {
14148 /* DR 1207: 'this' is in scope in the trailing return type. */
14149 inject_this_parameter (this_type, cp_type_quals (this_type));
14150 }
14151
14152 /* Substitute the return type. */
14153 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14154
14155 if (do_inject)
14156 {
14157 current_class_ptr = save_ccp;
14158 current_class_ref = save_ccr;
14159 }
14160 }
14161 else
14162 /* Substitute the return type. */
14163 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14164
14165 if (return_type == error_mark_node)
14166 return error_mark_node;
14167 /* DR 486 clarifies that creation of a function type with an
14168 invalid return type is a deduction failure. */
14169 if (TREE_CODE (return_type) == ARRAY_TYPE
14170 || TREE_CODE (return_type) == FUNCTION_TYPE)
14171 {
14172 if (complain & tf_error)
14173 {
14174 if (TREE_CODE (return_type) == ARRAY_TYPE)
14175 error ("function returning an array");
14176 else
14177 error ("function returning a function");
14178 }
14179 return error_mark_node;
14180 }
14181 /* And DR 657. */
14182 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14183 return error_mark_node;
14184
14185 if (!late_return_type_p)
14186 {
14187 /* Substitute the argument types. */
14188 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14189 complain, in_decl);
14190 if (arg_types == error_mark_node)
14191 return error_mark_node;
14192 }
14193
14194 /* Construct a new type node and return it. */
14195 if (TREE_CODE (t) == FUNCTION_TYPE)
14196 {
14197 fntype = build_function_type (return_type, arg_types);
14198 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14199 }
14200 else
14201 {
14202 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14203 /* Don't pick up extra function qualifiers from the basetype. */
14204 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14205 if (! MAYBE_CLASS_TYPE_P (r))
14206 {
14207 /* [temp.deduct]
14208
14209 Type deduction may fail for any of the following
14210 reasons:
14211
14212 -- Attempting to create "pointer to member of T" when T
14213 is not a class type. */
14214 if (complain & tf_error)
14215 error ("creating pointer to member function of non-class type %qT",
14216 r);
14217 return error_mark_node;
14218 }
14219
14220 fntype = build_method_type_directly (r, return_type,
14221 TREE_CHAIN (arg_types));
14222 }
14223 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14224
14225 /* See comment above. */
14226 tree raises = NULL_TREE;
14227 cp_ref_qualifier rqual = type_memfn_rqual (t);
14228 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14229
14230 return fntype;
14231 }
14232
14233 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14234 ARGS into that specification, and return the substituted
14235 specification. If there is no specification, return NULL_TREE. */
14236
14237 static tree
14238 tsubst_exception_specification (tree fntype,
14239 tree args,
14240 tsubst_flags_t complain,
14241 tree in_decl,
14242 bool defer_ok)
14243 {
14244 tree specs;
14245 tree new_specs;
14246
14247 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14248 new_specs = NULL_TREE;
14249 if (specs && TREE_PURPOSE (specs))
14250 {
14251 /* A noexcept-specifier. */
14252 tree expr = TREE_PURPOSE (specs);
14253 if (TREE_CODE (expr) == INTEGER_CST)
14254 new_specs = expr;
14255 else if (defer_ok)
14256 {
14257 /* Defer instantiation of noexcept-specifiers to avoid
14258 excessive instantiations (c++/49107). */
14259 new_specs = make_node (DEFERRED_NOEXCEPT);
14260 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14261 {
14262 /* We already partially instantiated this member template,
14263 so combine the new args with the old. */
14264 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14265 = DEFERRED_NOEXCEPT_PATTERN (expr);
14266 DEFERRED_NOEXCEPT_ARGS (new_specs)
14267 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14268 }
14269 else
14270 {
14271 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14272 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14273 }
14274 }
14275 else
14276 {
14277 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14278 {
14279 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14280 args);
14281 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14282 }
14283 new_specs = tsubst_copy_and_build
14284 (expr, args, complain, in_decl, /*function_p=*/false,
14285 /*integral_constant_expression_p=*/true);
14286 }
14287 new_specs = build_noexcept_spec (new_specs, complain);
14288 }
14289 else if (specs)
14290 {
14291 if (! TREE_VALUE (specs))
14292 new_specs = specs;
14293 else
14294 while (specs)
14295 {
14296 tree spec;
14297 int i, len = 1;
14298 tree expanded_specs = NULL_TREE;
14299
14300 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14301 {
14302 /* Expand the pack expansion type. */
14303 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14304 args, complain,
14305 in_decl);
14306
14307 if (expanded_specs == error_mark_node)
14308 return error_mark_node;
14309 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14310 len = TREE_VEC_LENGTH (expanded_specs);
14311 else
14312 {
14313 /* We're substituting into a member template, so
14314 we got a TYPE_PACK_EXPANSION back. Add that
14315 expansion and move on. */
14316 gcc_assert (TREE_CODE (expanded_specs)
14317 == TYPE_PACK_EXPANSION);
14318 new_specs = add_exception_specifier (new_specs,
14319 expanded_specs,
14320 complain);
14321 specs = TREE_CHAIN (specs);
14322 continue;
14323 }
14324 }
14325
14326 for (i = 0; i < len; ++i)
14327 {
14328 if (expanded_specs)
14329 spec = TREE_VEC_ELT (expanded_specs, i);
14330 else
14331 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14332 if (spec == error_mark_node)
14333 return spec;
14334 new_specs = add_exception_specifier (new_specs, spec,
14335 complain);
14336 }
14337
14338 specs = TREE_CHAIN (specs);
14339 }
14340 }
14341 return new_specs;
14342 }
14343
14344 /* Take the tree structure T and replace template parameters used
14345 therein with the argument vector ARGS. IN_DECL is an associated
14346 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14347 Issue error and warning messages under control of COMPLAIN. Note
14348 that we must be relatively non-tolerant of extensions here, in
14349 order to preserve conformance; if we allow substitutions that
14350 should not be allowed, we may allow argument deductions that should
14351 not succeed, and therefore report ambiguous overload situations
14352 where there are none. In theory, we could allow the substitution,
14353 but indicate that it should have failed, and allow our caller to
14354 make sure that the right thing happens, but we don't try to do this
14355 yet.
14356
14357 This function is used for dealing with types, decls and the like;
14358 for expressions, use tsubst_expr or tsubst_copy. */
14359
14360 tree
14361 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14362 {
14363 enum tree_code code;
14364 tree type, r = NULL_TREE;
14365
14366 if (t == NULL_TREE || t == error_mark_node
14367 || t == integer_type_node
14368 || t == void_type_node
14369 || t == char_type_node
14370 || t == unknown_type_node
14371 || TREE_CODE (t) == NAMESPACE_DECL
14372 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14373 return t;
14374
14375 if (DECL_P (t))
14376 return tsubst_decl (t, args, complain);
14377
14378 if (args == NULL_TREE)
14379 return t;
14380
14381 code = TREE_CODE (t);
14382
14383 if (code == IDENTIFIER_NODE)
14384 type = IDENTIFIER_TYPE_VALUE (t);
14385 else
14386 type = TREE_TYPE (t);
14387
14388 gcc_assert (type != unknown_type_node);
14389
14390 /* Reuse typedefs. We need to do this to handle dependent attributes,
14391 such as attribute aligned. */
14392 if (TYPE_P (t)
14393 && typedef_variant_p (t))
14394 {
14395 tree decl = TYPE_NAME (t);
14396
14397 if (alias_template_specialization_p (t))
14398 {
14399 /* DECL represents an alias template and we want to
14400 instantiate it. */
14401 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14402 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14403 r = instantiate_alias_template (tmpl, gen_args, complain);
14404 }
14405 else if (DECL_CLASS_SCOPE_P (decl)
14406 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14407 && uses_template_parms (DECL_CONTEXT (decl)))
14408 {
14409 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14410 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14411 r = retrieve_specialization (tmpl, gen_args, 0);
14412 }
14413 else if (DECL_FUNCTION_SCOPE_P (decl)
14414 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14415 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14416 r = retrieve_local_specialization (decl);
14417 else
14418 /* The typedef is from a non-template context. */
14419 return t;
14420
14421 if (r)
14422 {
14423 r = TREE_TYPE (r);
14424 r = cp_build_qualified_type_real
14425 (r, cp_type_quals (t) | cp_type_quals (r),
14426 complain | tf_ignore_bad_quals);
14427 return r;
14428 }
14429 else
14430 {
14431 /* We don't have an instantiation yet, so drop the typedef. */
14432 int quals = cp_type_quals (t);
14433 t = DECL_ORIGINAL_TYPE (decl);
14434 t = cp_build_qualified_type_real (t, quals,
14435 complain | tf_ignore_bad_quals);
14436 }
14437 }
14438
14439 bool fndecl_type = (complain & tf_fndecl_type);
14440 complain &= ~tf_fndecl_type;
14441
14442 if (type
14443 && code != TYPENAME_TYPE
14444 && code != TEMPLATE_TYPE_PARM
14445 && code != TEMPLATE_PARM_INDEX
14446 && code != IDENTIFIER_NODE
14447 && code != FUNCTION_TYPE
14448 && code != METHOD_TYPE)
14449 type = tsubst (type, args, complain, in_decl);
14450 if (type == error_mark_node)
14451 return error_mark_node;
14452
14453 switch (code)
14454 {
14455 case RECORD_TYPE:
14456 case UNION_TYPE:
14457 case ENUMERAL_TYPE:
14458 return tsubst_aggr_type (t, args, complain, in_decl,
14459 /*entering_scope=*/0);
14460
14461 case ERROR_MARK:
14462 case IDENTIFIER_NODE:
14463 case VOID_TYPE:
14464 case REAL_TYPE:
14465 case COMPLEX_TYPE:
14466 case VECTOR_TYPE:
14467 case BOOLEAN_TYPE:
14468 case NULLPTR_TYPE:
14469 case LANG_TYPE:
14470 return t;
14471
14472 case INTEGER_TYPE:
14473 if (t == integer_type_node)
14474 return t;
14475
14476 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14477 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14478 return t;
14479
14480 {
14481 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14482
14483 max = tsubst_expr (omax, args, complain, in_decl,
14484 /*integral_constant_expression_p=*/false);
14485
14486 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14487 needed. */
14488 if (TREE_CODE (max) == NOP_EXPR
14489 && TREE_SIDE_EFFECTS (omax)
14490 && !TREE_TYPE (max))
14491 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14492
14493 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14494 with TREE_SIDE_EFFECTS that indicates this is not an integral
14495 constant expression. */
14496 if (processing_template_decl
14497 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14498 {
14499 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14500 TREE_SIDE_EFFECTS (max) = 1;
14501 }
14502
14503 return compute_array_index_type (NULL_TREE, max, complain);
14504 }
14505
14506 case TEMPLATE_TYPE_PARM:
14507 case TEMPLATE_TEMPLATE_PARM:
14508 case BOUND_TEMPLATE_TEMPLATE_PARM:
14509 case TEMPLATE_PARM_INDEX:
14510 {
14511 int idx;
14512 int level;
14513 int levels;
14514 tree arg = NULL_TREE;
14515
14516 /* Early in template argument deduction substitution, we don't
14517 want to reduce the level of 'auto', or it will be confused
14518 with a normal template parm in subsequent deduction. */
14519 if (is_auto (t) && (complain & tf_partial))
14520 return t;
14521
14522 r = NULL_TREE;
14523
14524 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14525 template_parm_level_and_index (t, &level, &idx);
14526
14527 levels = TMPL_ARGS_DEPTH (args);
14528 if (level <= levels
14529 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14530 {
14531 arg = TMPL_ARG (args, level, idx);
14532
14533 /* See through ARGUMENT_PACK_SELECT arguments. */
14534 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14535 arg = argument_pack_select_arg (arg);
14536 }
14537
14538 if (arg == error_mark_node)
14539 return error_mark_node;
14540 else if (arg != NULL_TREE)
14541 {
14542 if (ARGUMENT_PACK_P (arg))
14543 /* If ARG is an argument pack, we don't actually want to
14544 perform a substitution here, because substitutions
14545 for argument packs are only done
14546 element-by-element. We can get to this point when
14547 substituting the type of a non-type template
14548 parameter pack, when that type actually contains
14549 template parameter packs from an outer template, e.g.,
14550
14551 template<typename... Types> struct A {
14552 template<Types... Values> struct B { };
14553 }; */
14554 return t;
14555
14556 if (code == TEMPLATE_TYPE_PARM)
14557 {
14558 int quals;
14559 gcc_assert (TYPE_P (arg));
14560
14561 quals = cp_type_quals (arg) | cp_type_quals (t);
14562
14563 return cp_build_qualified_type_real
14564 (arg, quals, complain | tf_ignore_bad_quals);
14565 }
14566 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14567 {
14568 /* We are processing a type constructed from a
14569 template template parameter. */
14570 tree argvec = tsubst (TYPE_TI_ARGS (t),
14571 args, complain, in_decl);
14572 if (argvec == error_mark_node)
14573 return error_mark_node;
14574
14575 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14576 || TREE_CODE (arg) == TEMPLATE_DECL
14577 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14578
14579 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14580 /* Consider this code:
14581
14582 template <template <class> class Template>
14583 struct Internal {
14584 template <class Arg> using Bind = Template<Arg>;
14585 };
14586
14587 template <template <class> class Template, class Arg>
14588 using Instantiate = Template<Arg>; //#0
14589
14590 template <template <class> class Template,
14591 class Argument>
14592 using Bind =
14593 Instantiate<Internal<Template>::template Bind,
14594 Argument>; //#1
14595
14596 When #1 is parsed, the
14597 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14598 parameter `Template' in #0 matches the
14599 UNBOUND_CLASS_TEMPLATE representing the argument
14600 `Internal<Template>::template Bind'; We then want
14601 to assemble the type `Bind<Argument>' that can't
14602 be fully created right now, because
14603 `Internal<Template>' not being complete, the Bind
14604 template cannot be looked up in that context. So
14605 we need to "store" `Bind<Argument>' for later
14606 when the context of Bind becomes complete. Let's
14607 store that in a TYPENAME_TYPE. */
14608 return make_typename_type (TYPE_CONTEXT (arg),
14609 build_nt (TEMPLATE_ID_EXPR,
14610 TYPE_IDENTIFIER (arg),
14611 argvec),
14612 typename_type,
14613 complain);
14614
14615 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14616 are resolving nested-types in the signature of a
14617 member function templates. Otherwise ARG is a
14618 TEMPLATE_DECL and is the real template to be
14619 instantiated. */
14620 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14621 arg = TYPE_NAME (arg);
14622
14623 r = lookup_template_class (arg,
14624 argvec, in_decl,
14625 DECL_CONTEXT (arg),
14626 /*entering_scope=*/0,
14627 complain);
14628 return cp_build_qualified_type_real
14629 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14630 }
14631 else if (code == TEMPLATE_TEMPLATE_PARM)
14632 return arg;
14633 else
14634 /* TEMPLATE_PARM_INDEX. */
14635 return convert_from_reference (unshare_expr (arg));
14636 }
14637
14638 if (level == 1)
14639 /* This can happen during the attempted tsubst'ing in
14640 unify. This means that we don't yet have any information
14641 about the template parameter in question. */
14642 return t;
14643
14644 /* If we get here, we must have been looking at a parm for a
14645 more deeply nested template. Make a new version of this
14646 template parameter, but with a lower level. */
14647 switch (code)
14648 {
14649 case TEMPLATE_TYPE_PARM:
14650 case TEMPLATE_TEMPLATE_PARM:
14651 case BOUND_TEMPLATE_TEMPLATE_PARM:
14652 if (cp_type_quals (t))
14653 {
14654 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14655 r = cp_build_qualified_type_real
14656 (r, cp_type_quals (t),
14657 complain | (code == TEMPLATE_TYPE_PARM
14658 ? tf_ignore_bad_quals : 0));
14659 }
14660 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14661 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14662 && (r = (TEMPLATE_PARM_DESCENDANTS
14663 (TEMPLATE_TYPE_PARM_INDEX (t))))
14664 && (r = TREE_TYPE (r))
14665 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14666 /* Break infinite recursion when substituting the constraints
14667 of a constrained placeholder. */;
14668 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14669 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14670 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14671 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14672 r = TEMPLATE_PARM_DESCENDANTS (arg))
14673 && (TEMPLATE_PARM_LEVEL (r)
14674 == TEMPLATE_PARM_LEVEL (arg) - levels))
14675 /* Cache the simple case of lowering a type parameter. */
14676 r = TREE_TYPE (r);
14677 else
14678 {
14679 r = copy_type (t);
14680 TEMPLATE_TYPE_PARM_INDEX (r)
14681 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14682 r, levels, args, complain);
14683 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14684 TYPE_MAIN_VARIANT (r) = r;
14685 TYPE_POINTER_TO (r) = NULL_TREE;
14686 TYPE_REFERENCE_TO (r) = NULL_TREE;
14687
14688 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14689 {
14690 /* Propagate constraints on placeholders. */
14691 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14692 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14693 = tsubst_constraint (constr, args, complain, in_decl);
14694 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14695 {
14696 pl = tsubst_copy (pl, args, complain, in_decl);
14697 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14698 }
14699 }
14700
14701 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14702 /* We have reduced the level of the template
14703 template parameter, but not the levels of its
14704 template parameters, so canonical_type_parameter
14705 will not be able to find the canonical template
14706 template parameter for this level. Thus, we
14707 require structural equality checking to compare
14708 TEMPLATE_TEMPLATE_PARMs. */
14709 SET_TYPE_STRUCTURAL_EQUALITY (r);
14710 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14711 SET_TYPE_STRUCTURAL_EQUALITY (r);
14712 else
14713 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14714
14715 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14716 {
14717 tree tinfo = TYPE_TEMPLATE_INFO (t);
14718 /* We might need to substitute into the types of non-type
14719 template parameters. */
14720 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14721 complain, in_decl);
14722 if (tmpl == error_mark_node)
14723 return error_mark_node;
14724 tree argvec = tsubst (TI_ARGS (tinfo), args,
14725 complain, in_decl);
14726 if (argvec == error_mark_node)
14727 return error_mark_node;
14728
14729 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14730 = build_template_info (tmpl, argvec);
14731 }
14732 }
14733 break;
14734
14735 case TEMPLATE_PARM_INDEX:
14736 /* OK, now substitute the type of the non-type parameter. We
14737 couldn't do it earlier because it might be an auto parameter,
14738 and we wouldn't need to if we had an argument. */
14739 type = tsubst (type, args, complain, in_decl);
14740 if (type == error_mark_node)
14741 return error_mark_node;
14742 r = reduce_template_parm_level (t, type, levels, args, complain);
14743 break;
14744
14745 default:
14746 gcc_unreachable ();
14747 }
14748
14749 return r;
14750 }
14751
14752 case TREE_LIST:
14753 {
14754 tree purpose, value, chain;
14755
14756 if (t == void_list_node)
14757 return t;
14758
14759 purpose = TREE_PURPOSE (t);
14760 if (purpose)
14761 {
14762 purpose = tsubst (purpose, args, complain, in_decl);
14763 if (purpose == error_mark_node)
14764 return error_mark_node;
14765 }
14766 value = TREE_VALUE (t);
14767 if (value)
14768 {
14769 value = tsubst (value, args, complain, in_decl);
14770 if (value == error_mark_node)
14771 return error_mark_node;
14772 }
14773 chain = TREE_CHAIN (t);
14774 if (chain && chain != void_type_node)
14775 {
14776 chain = tsubst (chain, args, complain, in_decl);
14777 if (chain == error_mark_node)
14778 return error_mark_node;
14779 }
14780 if (purpose == TREE_PURPOSE (t)
14781 && value == TREE_VALUE (t)
14782 && chain == TREE_CHAIN (t))
14783 return t;
14784 return hash_tree_cons (purpose, value, chain);
14785 }
14786
14787 case TREE_BINFO:
14788 /* We should never be tsubsting a binfo. */
14789 gcc_unreachable ();
14790
14791 case TREE_VEC:
14792 /* A vector of template arguments. */
14793 gcc_assert (!type);
14794 return tsubst_template_args (t, args, complain, in_decl);
14795
14796 case POINTER_TYPE:
14797 case REFERENCE_TYPE:
14798 {
14799 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14800 return t;
14801
14802 /* [temp.deduct]
14803
14804 Type deduction may fail for any of the following
14805 reasons:
14806
14807 -- Attempting to create a pointer to reference type.
14808 -- Attempting to create a reference to a reference type or
14809 a reference to void.
14810
14811 Core issue 106 says that creating a reference to a reference
14812 during instantiation is no longer a cause for failure. We
14813 only enforce this check in strict C++98 mode. */
14814 if ((TYPE_REF_P (type)
14815 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14816 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14817 {
14818 static location_t last_loc;
14819
14820 /* We keep track of the last time we issued this error
14821 message to avoid spewing a ton of messages during a
14822 single bad template instantiation. */
14823 if (complain & tf_error
14824 && last_loc != input_location)
14825 {
14826 if (VOID_TYPE_P (type))
14827 error ("forming reference to void");
14828 else if (code == POINTER_TYPE)
14829 error ("forming pointer to reference type %qT", type);
14830 else
14831 error ("forming reference to reference type %qT", type);
14832 last_loc = input_location;
14833 }
14834
14835 return error_mark_node;
14836 }
14837 else if (TREE_CODE (type) == FUNCTION_TYPE
14838 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14839 || type_memfn_rqual (type) != REF_QUAL_NONE))
14840 {
14841 if (complain & tf_error)
14842 {
14843 if (code == POINTER_TYPE)
14844 error ("forming pointer to qualified function type %qT",
14845 type);
14846 else
14847 error ("forming reference to qualified function type %qT",
14848 type);
14849 }
14850 return error_mark_node;
14851 }
14852 else if (code == POINTER_TYPE)
14853 {
14854 r = build_pointer_type (type);
14855 if (TREE_CODE (type) == METHOD_TYPE)
14856 r = build_ptrmemfunc_type (r);
14857 }
14858 else if (TYPE_REF_P (type))
14859 /* In C++0x, during template argument substitution, when there is an
14860 attempt to create a reference to a reference type, reference
14861 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14862
14863 "If a template-argument for a template-parameter T names a type
14864 that is a reference to a type A, an attempt to create the type
14865 'lvalue reference to cv T' creates the type 'lvalue reference to
14866 A,' while an attempt to create the type type rvalue reference to
14867 cv T' creates the type T"
14868 */
14869 r = cp_build_reference_type
14870 (TREE_TYPE (type),
14871 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14872 else
14873 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14874 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14875
14876 if (r != error_mark_node)
14877 /* Will this ever be needed for TYPE_..._TO values? */
14878 layout_type (r);
14879
14880 return r;
14881 }
14882 case OFFSET_TYPE:
14883 {
14884 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14885 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14886 {
14887 /* [temp.deduct]
14888
14889 Type deduction may fail for any of the following
14890 reasons:
14891
14892 -- Attempting to create "pointer to member of T" when T
14893 is not a class type. */
14894 if (complain & tf_error)
14895 error ("creating pointer to member of non-class type %qT", r);
14896 return error_mark_node;
14897 }
14898 if (TYPE_REF_P (type))
14899 {
14900 if (complain & tf_error)
14901 error ("creating pointer to member reference type %qT", type);
14902 return error_mark_node;
14903 }
14904 if (VOID_TYPE_P (type))
14905 {
14906 if (complain & tf_error)
14907 error ("creating pointer to member of type void");
14908 return error_mark_node;
14909 }
14910 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14911 if (TREE_CODE (type) == FUNCTION_TYPE)
14912 {
14913 /* The type of the implicit object parameter gets its
14914 cv-qualifiers from the FUNCTION_TYPE. */
14915 tree memptr;
14916 tree method_type
14917 = build_memfn_type (type, r, type_memfn_quals (type),
14918 type_memfn_rqual (type));
14919 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14920 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14921 complain);
14922 }
14923 else
14924 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14925 cp_type_quals (t),
14926 complain);
14927 }
14928 case FUNCTION_TYPE:
14929 case METHOD_TYPE:
14930 {
14931 tree fntype;
14932 tree specs;
14933 fntype = tsubst_function_type (t, args, complain, in_decl);
14934 if (fntype == error_mark_node)
14935 return error_mark_node;
14936
14937 /* Substitute the exception specification. */
14938 specs = tsubst_exception_specification (t, args, complain, in_decl,
14939 /*defer_ok*/fndecl_type);
14940 if (specs == error_mark_node)
14941 return error_mark_node;
14942 if (specs)
14943 fntype = build_exception_variant (fntype, specs);
14944 return fntype;
14945 }
14946 case ARRAY_TYPE:
14947 {
14948 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14949 if (domain == error_mark_node)
14950 return error_mark_node;
14951
14952 /* As an optimization, we avoid regenerating the array type if
14953 it will obviously be the same as T. */
14954 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14955 return t;
14956
14957 /* These checks should match the ones in create_array_type_for_decl.
14958
14959 [temp.deduct]
14960
14961 The deduction may fail for any of the following reasons:
14962
14963 -- Attempting to create an array with an element type that
14964 is void, a function type, or a reference type, or [DR337]
14965 an abstract class type. */
14966 if (VOID_TYPE_P (type)
14967 || TREE_CODE (type) == FUNCTION_TYPE
14968 || (TREE_CODE (type) == ARRAY_TYPE
14969 && TYPE_DOMAIN (type) == NULL_TREE)
14970 || TYPE_REF_P (type))
14971 {
14972 if (complain & tf_error)
14973 error ("creating array of %qT", type);
14974 return error_mark_node;
14975 }
14976
14977 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14978 return error_mark_node;
14979
14980 r = build_cplus_array_type (type, domain);
14981
14982 if (!valid_array_size_p (input_location, r, in_decl,
14983 (complain & tf_error)))
14984 return error_mark_node;
14985
14986 if (TYPE_USER_ALIGN (t))
14987 {
14988 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14989 TYPE_USER_ALIGN (r) = 1;
14990 }
14991
14992 return r;
14993 }
14994
14995 case TYPENAME_TYPE:
14996 {
14997 tree ctx = TYPE_CONTEXT (t);
14998 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
14999 {
15000 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15001 if (ctx == error_mark_node
15002 || TREE_VEC_LENGTH (ctx) > 1)
15003 return error_mark_node;
15004 if (TREE_VEC_LENGTH (ctx) == 0)
15005 {
15006 if (complain & tf_error)
15007 error ("%qD is instantiated for an empty pack",
15008 TYPENAME_TYPE_FULLNAME (t));
15009 return error_mark_node;
15010 }
15011 ctx = TREE_VEC_ELT (ctx, 0);
15012 }
15013 else
15014 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15015 /*entering_scope=*/1);
15016 if (ctx == error_mark_node)
15017 return error_mark_node;
15018
15019 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15020 complain, in_decl);
15021 if (f == error_mark_node)
15022 return error_mark_node;
15023
15024 if (!MAYBE_CLASS_TYPE_P (ctx))
15025 {
15026 if (complain & tf_error)
15027 error ("%qT is not a class, struct, or union type", ctx);
15028 return error_mark_node;
15029 }
15030 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15031 {
15032 /* Normally, make_typename_type does not require that the CTX
15033 have complete type in order to allow things like:
15034
15035 template <class T> struct S { typename S<T>::X Y; };
15036
15037 But, such constructs have already been resolved by this
15038 point, so here CTX really should have complete type, unless
15039 it's a partial instantiation. */
15040 ctx = complete_type (ctx);
15041 if (!COMPLETE_TYPE_P (ctx))
15042 {
15043 if (complain & tf_error)
15044 cxx_incomplete_type_error (NULL_TREE, ctx);
15045 return error_mark_node;
15046 }
15047 }
15048
15049 f = make_typename_type (ctx, f, typename_type,
15050 complain | tf_keep_type_decl);
15051 if (f == error_mark_node)
15052 return f;
15053 if (TREE_CODE (f) == TYPE_DECL)
15054 {
15055 complain |= tf_ignore_bad_quals;
15056 f = TREE_TYPE (f);
15057 }
15058
15059 if (TREE_CODE (f) != TYPENAME_TYPE)
15060 {
15061 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15062 {
15063 if (complain & tf_error)
15064 error ("%qT resolves to %qT, which is not an enumeration type",
15065 t, f);
15066 else
15067 return error_mark_node;
15068 }
15069 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15070 {
15071 if (complain & tf_error)
15072 error ("%qT resolves to %qT, which is is not a class type",
15073 t, f);
15074 else
15075 return error_mark_node;
15076 }
15077 }
15078
15079 return cp_build_qualified_type_real
15080 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15081 }
15082
15083 case UNBOUND_CLASS_TEMPLATE:
15084 {
15085 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15086 in_decl, /*entering_scope=*/1);
15087 tree name = TYPE_IDENTIFIER (t);
15088 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15089
15090 if (ctx == error_mark_node || name == error_mark_node)
15091 return error_mark_node;
15092
15093 if (parm_list)
15094 parm_list = tsubst_template_parms (parm_list, args, complain);
15095 return make_unbound_class_template (ctx, name, parm_list, complain);
15096 }
15097
15098 case TYPEOF_TYPE:
15099 {
15100 tree type;
15101
15102 ++cp_unevaluated_operand;
15103 ++c_inhibit_evaluation_warnings;
15104
15105 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15106 complain, in_decl,
15107 /*integral_constant_expression_p=*/false);
15108
15109 --cp_unevaluated_operand;
15110 --c_inhibit_evaluation_warnings;
15111
15112 type = finish_typeof (type);
15113 return cp_build_qualified_type_real (type,
15114 cp_type_quals (t)
15115 | cp_type_quals (type),
15116 complain);
15117 }
15118
15119 case DECLTYPE_TYPE:
15120 {
15121 tree type;
15122
15123 ++cp_unevaluated_operand;
15124 ++c_inhibit_evaluation_warnings;
15125
15126 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15127 complain|tf_decltype, in_decl,
15128 /*function_p*/false,
15129 /*integral_constant_expression*/false);
15130
15131 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15132 {
15133 if (type == NULL_TREE)
15134 {
15135 if (complain & tf_error)
15136 error ("empty initializer in lambda init-capture");
15137 type = error_mark_node;
15138 }
15139 else if (TREE_CODE (type) == TREE_LIST)
15140 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15141 }
15142
15143 --cp_unevaluated_operand;
15144 --c_inhibit_evaluation_warnings;
15145
15146 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15147 type = lambda_capture_field_type (type,
15148 DECLTYPE_FOR_INIT_CAPTURE (t),
15149 DECLTYPE_FOR_REF_CAPTURE (t));
15150 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15151 type = lambda_proxy_type (type);
15152 else
15153 {
15154 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15155 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15156 && EXPR_P (type))
15157 /* In a template ~id could be either a complement expression
15158 or an unqualified-id naming a destructor; if instantiating
15159 it produces an expression, it's not an id-expression or
15160 member access. */
15161 id = false;
15162 type = finish_decltype_type (type, id, complain);
15163 }
15164 return cp_build_qualified_type_real (type,
15165 cp_type_quals (t)
15166 | cp_type_quals (type),
15167 complain | tf_ignore_bad_quals);
15168 }
15169
15170 case UNDERLYING_TYPE:
15171 {
15172 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15173 complain, in_decl);
15174 return finish_underlying_type (type);
15175 }
15176
15177 case TYPE_ARGUMENT_PACK:
15178 case NONTYPE_ARGUMENT_PACK:
15179 {
15180 tree r;
15181
15182 if (code == NONTYPE_ARGUMENT_PACK)
15183 r = make_node (code);
15184 else
15185 r = cxx_make_type (code);
15186
15187 tree pack_args = ARGUMENT_PACK_ARGS (t);
15188 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15189 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15190
15191 return r;
15192 }
15193
15194 case VOID_CST:
15195 case INTEGER_CST:
15196 case REAL_CST:
15197 case STRING_CST:
15198 case PLUS_EXPR:
15199 case MINUS_EXPR:
15200 case NEGATE_EXPR:
15201 case NOP_EXPR:
15202 case INDIRECT_REF:
15203 case ADDR_EXPR:
15204 case CALL_EXPR:
15205 case ARRAY_REF:
15206 case SCOPE_REF:
15207 /* We should use one of the expression tsubsts for these codes. */
15208 gcc_unreachable ();
15209
15210 default:
15211 sorry ("use of %qs in template", get_tree_code_name (code));
15212 return error_mark_node;
15213 }
15214 }
15215
15216 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15217 expression on the left-hand side of the "." or "->" operator. We
15218 only do the lookup if we had a dependent BASELINK. Otherwise we
15219 adjust it onto the instantiated heirarchy. */
15220
15221 static tree
15222 tsubst_baselink (tree baselink, tree object_type,
15223 tree args, tsubst_flags_t complain, tree in_decl)
15224 {
15225 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15226 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15227 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15228
15229 tree optype = BASELINK_OPTYPE (baselink);
15230 optype = tsubst (optype, args, complain, in_decl);
15231
15232 tree template_args = NULL_TREE;
15233 bool template_id_p = false;
15234 tree fns = BASELINK_FUNCTIONS (baselink);
15235 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15236 {
15237 template_id_p = true;
15238 template_args = TREE_OPERAND (fns, 1);
15239 fns = TREE_OPERAND (fns, 0);
15240 if (template_args)
15241 template_args = tsubst_template_args (template_args, args,
15242 complain, in_decl);
15243 }
15244
15245 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15246 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15247 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15248
15249 if (dependent_p)
15250 {
15251 tree name = OVL_NAME (fns);
15252 if (IDENTIFIER_CONV_OP_P (name))
15253 name = make_conv_op_name (optype);
15254
15255 if (name == complete_dtor_identifier)
15256 /* Treat as-if non-dependent below. */
15257 dependent_p = false;
15258
15259 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15260 if (!baselink)
15261 {
15262 if ((complain & tf_error)
15263 && constructor_name_p (name, qualifying_scope))
15264 error ("cannot call constructor %<%T::%D%> directly",
15265 qualifying_scope, name);
15266 return error_mark_node;
15267 }
15268
15269 if (BASELINK_P (baselink))
15270 fns = BASELINK_FUNCTIONS (baselink);
15271 }
15272 else
15273 /* We're going to overwrite pieces below, make a duplicate. */
15274 baselink = copy_node (baselink);
15275
15276 /* If lookup found a single function, mark it as used at this point.
15277 (If lookup found multiple functions the one selected later by
15278 overload resolution will be marked as used at that point.) */
15279 if (!template_id_p && !really_overloaded_fn (fns))
15280 {
15281 tree fn = OVL_FIRST (fns);
15282 bool ok = mark_used (fn, complain);
15283 if (!ok && !(complain & tf_error))
15284 return error_mark_node;
15285 if (ok && BASELINK_P (baselink))
15286 /* We might have instantiated an auto function. */
15287 TREE_TYPE (baselink) = TREE_TYPE (fn);
15288 }
15289
15290 if (BASELINK_P (baselink))
15291 {
15292 /* Add back the template arguments, if present. */
15293 if (template_id_p)
15294 BASELINK_FUNCTIONS (baselink)
15295 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15296
15297 /* Update the conversion operator type. */
15298 BASELINK_OPTYPE (baselink) = optype;
15299 }
15300
15301 if (!object_type)
15302 object_type = current_class_type;
15303
15304 if (qualified_p || !dependent_p)
15305 {
15306 baselink = adjust_result_of_qualified_name_lookup (baselink,
15307 qualifying_scope,
15308 object_type);
15309 if (!qualified_p)
15310 /* We need to call adjust_result_of_qualified_name_lookup in case the
15311 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15312 so that we still get virtual function binding. */
15313 BASELINK_QUALIFIED_P (baselink) = false;
15314 }
15315
15316 return baselink;
15317 }
15318
15319 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15320 true if the qualified-id will be a postfix-expression in-and-of
15321 itself; false if more of the postfix-expression follows the
15322 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15323 of "&". */
15324
15325 static tree
15326 tsubst_qualified_id (tree qualified_id, tree args,
15327 tsubst_flags_t complain, tree in_decl,
15328 bool done, bool address_p)
15329 {
15330 tree expr;
15331 tree scope;
15332 tree name;
15333 bool is_template;
15334 tree template_args;
15335 location_t loc = UNKNOWN_LOCATION;
15336
15337 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15338
15339 /* Figure out what name to look up. */
15340 name = TREE_OPERAND (qualified_id, 1);
15341 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15342 {
15343 is_template = true;
15344 loc = EXPR_LOCATION (name);
15345 template_args = TREE_OPERAND (name, 1);
15346 if (template_args)
15347 template_args = tsubst_template_args (template_args, args,
15348 complain, in_decl);
15349 if (template_args == error_mark_node)
15350 return error_mark_node;
15351 name = TREE_OPERAND (name, 0);
15352 }
15353 else
15354 {
15355 is_template = false;
15356 template_args = NULL_TREE;
15357 }
15358
15359 /* Substitute into the qualifying scope. When there are no ARGS, we
15360 are just trying to simplify a non-dependent expression. In that
15361 case the qualifying scope may be dependent, and, in any case,
15362 substituting will not help. */
15363 scope = TREE_OPERAND (qualified_id, 0);
15364 if (args)
15365 {
15366 scope = tsubst (scope, args, complain, in_decl);
15367 expr = tsubst_copy (name, args, complain, in_decl);
15368 }
15369 else
15370 expr = name;
15371
15372 if (dependent_scope_p (scope))
15373 {
15374 if (is_template)
15375 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15376 tree r = build_qualified_name (NULL_TREE, scope, expr,
15377 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15378 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15379 return r;
15380 }
15381
15382 if (!BASELINK_P (name) && !DECL_P (expr))
15383 {
15384 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15385 {
15386 /* A BIT_NOT_EXPR is used to represent a destructor. */
15387 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15388 {
15389 error ("qualifying type %qT does not match destructor name ~%qT",
15390 scope, TREE_OPERAND (expr, 0));
15391 expr = error_mark_node;
15392 }
15393 else
15394 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15395 /*is_type_p=*/0, false);
15396 }
15397 else
15398 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15399 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15400 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15401 {
15402 if (complain & tf_error)
15403 {
15404 error ("dependent-name %qE is parsed as a non-type, but "
15405 "instantiation yields a type", qualified_id);
15406 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15407 }
15408 return error_mark_node;
15409 }
15410 }
15411
15412 if (DECL_P (expr))
15413 {
15414 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15415 scope);
15416 /* Remember that there was a reference to this entity. */
15417 if (!mark_used (expr, complain) && !(complain & tf_error))
15418 return error_mark_node;
15419 }
15420
15421 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15422 {
15423 if (complain & tf_error)
15424 qualified_name_lookup_error (scope,
15425 TREE_OPERAND (qualified_id, 1),
15426 expr, input_location);
15427 return error_mark_node;
15428 }
15429
15430 if (is_template)
15431 {
15432 /* We may be repeating a check already done during parsing, but
15433 if it was well-formed and passed then, it will pass again
15434 now, and if it didn't, we wouldn't have got here. The case
15435 we want to catch is when we couldn't tell then, and can now,
15436 namely when templ prior to substitution was an
15437 identifier. */
15438 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15439 return error_mark_node;
15440
15441 if (variable_template_p (expr))
15442 expr = lookup_and_finish_template_variable (expr, template_args,
15443 complain);
15444 else
15445 expr = lookup_template_function (expr, template_args);
15446 }
15447
15448 if (expr == error_mark_node && complain & tf_error)
15449 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15450 expr, input_location);
15451 else if (TYPE_P (scope))
15452 {
15453 expr = (adjust_result_of_qualified_name_lookup
15454 (expr, scope, current_nonlambda_class_type ()));
15455 expr = (finish_qualified_id_expr
15456 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15457 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15458 /*template_arg_p=*/false, complain));
15459 }
15460
15461 /* Expressions do not generally have reference type. */
15462 if (TREE_CODE (expr) != SCOPE_REF
15463 /* However, if we're about to form a pointer-to-member, we just
15464 want the referenced member referenced. */
15465 && TREE_CODE (expr) != OFFSET_REF)
15466 expr = convert_from_reference (expr);
15467
15468 if (REF_PARENTHESIZED_P (qualified_id))
15469 expr = force_paren_expr (expr);
15470
15471 return expr;
15472 }
15473
15474 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15475 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15476 for tsubst. */
15477
15478 static tree
15479 tsubst_init (tree init, tree decl, tree args,
15480 tsubst_flags_t complain, tree in_decl)
15481 {
15482 if (!init)
15483 return NULL_TREE;
15484
15485 init = tsubst_expr (init, args, complain, in_decl, false);
15486
15487 tree type = TREE_TYPE (decl);
15488
15489 if (!init && type != error_mark_node)
15490 {
15491 if (tree auto_node = type_uses_auto (type))
15492 {
15493 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15494 {
15495 if (complain & tf_error)
15496 error ("initializer for %q#D expands to an empty list "
15497 "of expressions", decl);
15498 return error_mark_node;
15499 }
15500 }
15501 else if (!dependent_type_p (type))
15502 {
15503 /* If we had an initializer but it
15504 instantiated to nothing,
15505 value-initialize the object. This will
15506 only occur when the initializer was a
15507 pack expansion where the parameter packs
15508 used in that expansion were of length
15509 zero. */
15510 init = build_value_init (type, complain);
15511 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15512 init = get_target_expr_sfinae (init, complain);
15513 if (TREE_CODE (init) == TARGET_EXPR)
15514 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15515 }
15516 }
15517
15518 return init;
15519 }
15520
15521 /* Like tsubst, but deals with expressions. This function just replaces
15522 template parms; to finish processing the resultant expression, use
15523 tsubst_copy_and_build or tsubst_expr. */
15524
15525 static tree
15526 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15527 {
15528 enum tree_code code;
15529 tree r;
15530
15531 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15532 return t;
15533
15534 code = TREE_CODE (t);
15535
15536 switch (code)
15537 {
15538 case PARM_DECL:
15539 r = retrieve_local_specialization (t);
15540
15541 if (r == NULL_TREE)
15542 {
15543 /* We get here for a use of 'this' in an NSDMI. */
15544 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15545 return current_class_ptr;
15546
15547 /* This can happen for a parameter name used later in a function
15548 declaration (such as in a late-specified return type). Just
15549 make a dummy decl, since it's only used for its type. */
15550 gcc_assert (cp_unevaluated_operand != 0);
15551 r = tsubst_decl (t, args, complain);
15552 /* Give it the template pattern as its context; its true context
15553 hasn't been instantiated yet and this is good enough for
15554 mangling. */
15555 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15556 }
15557
15558 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15559 r = argument_pack_select_arg (r);
15560 if (!mark_used (r, complain) && !(complain & tf_error))
15561 return error_mark_node;
15562 return r;
15563
15564 case CONST_DECL:
15565 {
15566 tree enum_type;
15567 tree v;
15568
15569 if (DECL_TEMPLATE_PARM_P (t))
15570 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15571 /* There is no need to substitute into namespace-scope
15572 enumerators. */
15573 if (DECL_NAMESPACE_SCOPE_P (t))
15574 return t;
15575 /* If ARGS is NULL, then T is known to be non-dependent. */
15576 if (args == NULL_TREE)
15577 return scalar_constant_value (t);
15578
15579 /* Unfortunately, we cannot just call lookup_name here.
15580 Consider:
15581
15582 template <int I> int f() {
15583 enum E { a = I };
15584 struct S { void g() { E e = a; } };
15585 };
15586
15587 When we instantiate f<7>::S::g(), say, lookup_name is not
15588 clever enough to find f<7>::a. */
15589 enum_type
15590 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15591 /*entering_scope=*/0);
15592
15593 for (v = TYPE_VALUES (enum_type);
15594 v != NULL_TREE;
15595 v = TREE_CHAIN (v))
15596 if (TREE_PURPOSE (v) == DECL_NAME (t))
15597 return TREE_VALUE (v);
15598
15599 /* We didn't find the name. That should never happen; if
15600 name-lookup found it during preliminary parsing, we
15601 should find it again here during instantiation. */
15602 gcc_unreachable ();
15603 }
15604 return t;
15605
15606 case FIELD_DECL:
15607 if (DECL_CONTEXT (t))
15608 {
15609 tree ctx;
15610
15611 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15612 /*entering_scope=*/1);
15613 if (ctx != DECL_CONTEXT (t))
15614 {
15615 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15616 if (!r)
15617 {
15618 if (complain & tf_error)
15619 error ("using invalid field %qD", t);
15620 return error_mark_node;
15621 }
15622 return r;
15623 }
15624 }
15625
15626 return t;
15627
15628 case VAR_DECL:
15629 case FUNCTION_DECL:
15630 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15631 r = tsubst (t, args, complain, in_decl);
15632 else if (local_variable_p (t)
15633 && uses_template_parms (DECL_CONTEXT (t)))
15634 {
15635 r = retrieve_local_specialization (t);
15636 if (r == NULL_TREE)
15637 {
15638 /* First try name lookup to find the instantiation. */
15639 r = lookup_name (DECL_NAME (t));
15640 if (r)
15641 {
15642 if (!VAR_P (r))
15643 {
15644 /* During error-recovery we may find a non-variable,
15645 even an OVERLOAD: just bail out and avoid ICEs and
15646 duplicate diagnostics (c++/62207). */
15647 gcc_assert (seen_error ());
15648 return error_mark_node;
15649 }
15650 if (!is_capture_proxy (r))
15651 {
15652 /* Make sure the one we found is the one we want. */
15653 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15654 if (ctx != DECL_CONTEXT (r))
15655 r = NULL_TREE;
15656 }
15657 }
15658
15659 if (r)
15660 /* OK */;
15661 else
15662 {
15663 /* This can happen for a variable used in a
15664 late-specified return type of a local lambda, or for a
15665 local static or constant. Building a new VAR_DECL
15666 should be OK in all those cases. */
15667 r = tsubst_decl (t, args, complain);
15668 if (local_specializations)
15669 /* Avoid infinite recursion (79640). */
15670 register_local_specialization (r, t);
15671 if (decl_maybe_constant_var_p (r))
15672 {
15673 /* We can't call cp_finish_decl, so handle the
15674 initializer by hand. */
15675 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15676 complain, in_decl);
15677 if (!processing_template_decl)
15678 init = maybe_constant_init (init);
15679 if (processing_template_decl
15680 ? potential_constant_expression (init)
15681 : reduced_constant_expression_p (init))
15682 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15683 = TREE_CONSTANT (r) = true;
15684 DECL_INITIAL (r) = init;
15685 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15686 TREE_TYPE (r)
15687 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15688 complain, adc_variable_type);
15689 }
15690 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15691 || decl_constant_var_p (r)
15692 || seen_error ());
15693 if (!processing_template_decl
15694 && !TREE_STATIC (r))
15695 r = process_outer_var_ref (r, complain);
15696 }
15697 /* Remember this for subsequent uses. */
15698 if (local_specializations)
15699 register_local_specialization (r, t);
15700 }
15701 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15702 r = argument_pack_select_arg (r);
15703 }
15704 else
15705 r = t;
15706 if (!mark_used (r, complain))
15707 return error_mark_node;
15708 return r;
15709
15710 case NAMESPACE_DECL:
15711 return t;
15712
15713 case OVERLOAD:
15714 return t;
15715
15716 case BASELINK:
15717 return tsubst_baselink (t, current_nonlambda_class_type (),
15718 args, complain, in_decl);
15719
15720 case TEMPLATE_DECL:
15721 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15722 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15723 args, complain, in_decl);
15724 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15725 return tsubst (t, args, complain, in_decl);
15726 else if (DECL_CLASS_SCOPE_P (t)
15727 && uses_template_parms (DECL_CONTEXT (t)))
15728 {
15729 /* Template template argument like the following example need
15730 special treatment:
15731
15732 template <template <class> class TT> struct C {};
15733 template <class T> struct D {
15734 template <class U> struct E {};
15735 C<E> c; // #1
15736 };
15737 D<int> d; // #2
15738
15739 We are processing the template argument `E' in #1 for
15740 the template instantiation #2. Originally, `E' is a
15741 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15742 have to substitute this with one having context `D<int>'. */
15743
15744 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15745 if (dependent_scope_p (context))
15746 {
15747 /* When rewriting a constructor into a deduction guide, a
15748 non-dependent name can become dependent, so memtmpl<args>
15749 becomes context::template memtmpl<args>. */
15750 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15751 return build_qualified_name (type, context, DECL_NAME (t),
15752 /*template*/true);
15753 }
15754 return lookup_field (context, DECL_NAME(t), 0, false);
15755 }
15756 else
15757 /* Ordinary template template argument. */
15758 return t;
15759
15760 case NON_LVALUE_EXPR:
15761 case VIEW_CONVERT_EXPR:
15762 {
15763 /* Handle location wrappers by substituting the wrapped node
15764 first, *then* reusing the resulting type. Doing the type
15765 first ensures that we handle template parameters and
15766 parameter pack expansions. */
15767 if (location_wrapper_p (t))
15768 {
15769 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15770 complain, in_decl);
15771 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15772 }
15773 tree op = TREE_OPERAND (t, 0);
15774 if (code == VIEW_CONVERT_EXPR
15775 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15776 {
15777 /* Wrapper to make a C++20 template parameter object const. */
15778 op = tsubst_copy (op, args, complain, in_decl);
15779 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15780 {
15781 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15782 return build1 (code, type, op);
15783 }
15784 else
15785 {
15786 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15787 return op;
15788 }
15789 }
15790 /* We shouldn't see any other uses of these in templates. */
15791 gcc_unreachable ();
15792 }
15793
15794 case CAST_EXPR:
15795 case REINTERPRET_CAST_EXPR:
15796 case CONST_CAST_EXPR:
15797 case STATIC_CAST_EXPR:
15798 case DYNAMIC_CAST_EXPR:
15799 case IMPLICIT_CONV_EXPR:
15800 case CONVERT_EXPR:
15801 case NOP_EXPR:
15802 {
15803 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15804 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15805 return build1 (code, type, op0);
15806 }
15807
15808 case SIZEOF_EXPR:
15809 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15810 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15811 {
15812 tree expanded, op = TREE_OPERAND (t, 0);
15813 int len = 0;
15814
15815 if (SIZEOF_EXPR_TYPE_P (t))
15816 op = TREE_TYPE (op);
15817
15818 ++cp_unevaluated_operand;
15819 ++c_inhibit_evaluation_warnings;
15820 /* We only want to compute the number of arguments. */
15821 if (PACK_EXPANSION_P (op))
15822 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15823 else
15824 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15825 args, complain, in_decl);
15826 --cp_unevaluated_operand;
15827 --c_inhibit_evaluation_warnings;
15828
15829 if (TREE_CODE (expanded) == TREE_VEC)
15830 {
15831 len = TREE_VEC_LENGTH (expanded);
15832 /* Set TREE_USED for the benefit of -Wunused. */
15833 for (int i = 0; i < len; i++)
15834 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15835 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15836 }
15837
15838 if (expanded == error_mark_node)
15839 return error_mark_node;
15840 else if (PACK_EXPANSION_P (expanded)
15841 || (TREE_CODE (expanded) == TREE_VEC
15842 && pack_expansion_args_count (expanded)))
15843
15844 {
15845 if (PACK_EXPANSION_P (expanded))
15846 /* OK. */;
15847 else if (TREE_VEC_LENGTH (expanded) == 1)
15848 expanded = TREE_VEC_ELT (expanded, 0);
15849 else
15850 expanded = make_argument_pack (expanded);
15851
15852 if (TYPE_P (expanded))
15853 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15854 false,
15855 complain & tf_error);
15856 else
15857 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15858 complain & tf_error);
15859 }
15860 else
15861 return build_int_cst (size_type_node, len);
15862 }
15863 if (SIZEOF_EXPR_TYPE_P (t))
15864 {
15865 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15866 args, complain, in_decl);
15867 r = build1 (NOP_EXPR, r, error_mark_node);
15868 r = build1 (SIZEOF_EXPR,
15869 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15870 SIZEOF_EXPR_TYPE_P (r) = 1;
15871 return r;
15872 }
15873 /* Fall through */
15874
15875 case INDIRECT_REF:
15876 case NEGATE_EXPR:
15877 case TRUTH_NOT_EXPR:
15878 case BIT_NOT_EXPR:
15879 case ADDR_EXPR:
15880 case UNARY_PLUS_EXPR: /* Unary + */
15881 case ALIGNOF_EXPR:
15882 case AT_ENCODE_EXPR:
15883 case ARROW_EXPR:
15884 case THROW_EXPR:
15885 case TYPEID_EXPR:
15886 case REALPART_EXPR:
15887 case IMAGPART_EXPR:
15888 case PAREN_EXPR:
15889 {
15890 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15891 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15892 r = build1 (code, type, op0);
15893 if (code == ALIGNOF_EXPR)
15894 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15895 return r;
15896 }
15897
15898 case COMPONENT_REF:
15899 {
15900 tree object;
15901 tree name;
15902
15903 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15904 name = TREE_OPERAND (t, 1);
15905 if (TREE_CODE (name) == BIT_NOT_EXPR)
15906 {
15907 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15908 complain, in_decl);
15909 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15910 }
15911 else if (TREE_CODE (name) == SCOPE_REF
15912 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15913 {
15914 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15915 complain, in_decl);
15916 name = TREE_OPERAND (name, 1);
15917 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15918 complain, in_decl);
15919 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15920 name = build_qualified_name (/*type=*/NULL_TREE,
15921 base, name,
15922 /*template_p=*/false);
15923 }
15924 else if (BASELINK_P (name))
15925 name = tsubst_baselink (name,
15926 non_reference (TREE_TYPE (object)),
15927 args, complain,
15928 in_decl);
15929 else
15930 name = tsubst_copy (name, args, complain, in_decl);
15931 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15932 }
15933
15934 case PLUS_EXPR:
15935 case MINUS_EXPR:
15936 case MULT_EXPR:
15937 case TRUNC_DIV_EXPR:
15938 case CEIL_DIV_EXPR:
15939 case FLOOR_DIV_EXPR:
15940 case ROUND_DIV_EXPR:
15941 case EXACT_DIV_EXPR:
15942 case BIT_AND_EXPR:
15943 case BIT_IOR_EXPR:
15944 case BIT_XOR_EXPR:
15945 case TRUNC_MOD_EXPR:
15946 case FLOOR_MOD_EXPR:
15947 case TRUTH_ANDIF_EXPR:
15948 case TRUTH_ORIF_EXPR:
15949 case TRUTH_AND_EXPR:
15950 case TRUTH_OR_EXPR:
15951 case RSHIFT_EXPR:
15952 case LSHIFT_EXPR:
15953 case RROTATE_EXPR:
15954 case LROTATE_EXPR:
15955 case EQ_EXPR:
15956 case NE_EXPR:
15957 case MAX_EXPR:
15958 case MIN_EXPR:
15959 case LE_EXPR:
15960 case GE_EXPR:
15961 case LT_EXPR:
15962 case GT_EXPR:
15963 case COMPOUND_EXPR:
15964 case DOTSTAR_EXPR:
15965 case MEMBER_REF:
15966 case PREDECREMENT_EXPR:
15967 case PREINCREMENT_EXPR:
15968 case POSTDECREMENT_EXPR:
15969 case POSTINCREMENT_EXPR:
15970 {
15971 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15972 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15973 return build_nt (code, op0, op1);
15974 }
15975
15976 case SCOPE_REF:
15977 {
15978 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15979 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15980 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15981 QUALIFIED_NAME_IS_TEMPLATE (t));
15982 }
15983
15984 case ARRAY_REF:
15985 {
15986 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15987 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15988 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15989 }
15990
15991 case CALL_EXPR:
15992 {
15993 int n = VL_EXP_OPERAND_LENGTH (t);
15994 tree result = build_vl_exp (CALL_EXPR, n);
15995 int i;
15996 for (i = 0; i < n; i++)
15997 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15998 complain, in_decl);
15999 return result;
16000 }
16001
16002 case COND_EXPR:
16003 case MODOP_EXPR:
16004 case PSEUDO_DTOR_EXPR:
16005 case VEC_PERM_EXPR:
16006 {
16007 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16008 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16009 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16010 r = build_nt (code, op0, op1, op2);
16011 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16012 return r;
16013 }
16014
16015 case NEW_EXPR:
16016 {
16017 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16018 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16019 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16020 r = build_nt (code, op0, op1, op2);
16021 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16022 return r;
16023 }
16024
16025 case DELETE_EXPR:
16026 {
16027 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16028 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16029 r = build_nt (code, op0, op1);
16030 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16031 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16032 return r;
16033 }
16034
16035 case TEMPLATE_ID_EXPR:
16036 {
16037 /* Substituted template arguments */
16038 tree fn = TREE_OPERAND (t, 0);
16039 tree targs = TREE_OPERAND (t, 1);
16040
16041 fn = tsubst_copy (fn, args, complain, in_decl);
16042 if (targs)
16043 targs = tsubst_template_args (targs, args, complain, in_decl);
16044
16045 return lookup_template_function (fn, targs);
16046 }
16047
16048 case TREE_LIST:
16049 {
16050 tree purpose, value, chain;
16051
16052 if (t == void_list_node)
16053 return t;
16054
16055 purpose = TREE_PURPOSE (t);
16056 if (purpose)
16057 purpose = tsubst_copy (purpose, args, complain, in_decl);
16058 value = TREE_VALUE (t);
16059 if (value)
16060 value = tsubst_copy (value, args, complain, in_decl);
16061 chain = TREE_CHAIN (t);
16062 if (chain && chain != void_type_node)
16063 chain = tsubst_copy (chain, args, complain, in_decl);
16064 if (purpose == TREE_PURPOSE (t)
16065 && value == TREE_VALUE (t)
16066 && chain == TREE_CHAIN (t))
16067 return t;
16068 return tree_cons (purpose, value, chain);
16069 }
16070
16071 case RECORD_TYPE:
16072 case UNION_TYPE:
16073 case ENUMERAL_TYPE:
16074 case INTEGER_TYPE:
16075 case TEMPLATE_TYPE_PARM:
16076 case TEMPLATE_TEMPLATE_PARM:
16077 case BOUND_TEMPLATE_TEMPLATE_PARM:
16078 case TEMPLATE_PARM_INDEX:
16079 case POINTER_TYPE:
16080 case REFERENCE_TYPE:
16081 case OFFSET_TYPE:
16082 case FUNCTION_TYPE:
16083 case METHOD_TYPE:
16084 case ARRAY_TYPE:
16085 case TYPENAME_TYPE:
16086 case UNBOUND_CLASS_TEMPLATE:
16087 case TYPEOF_TYPE:
16088 case DECLTYPE_TYPE:
16089 case TYPE_DECL:
16090 return tsubst (t, args, complain, in_decl);
16091
16092 case USING_DECL:
16093 t = DECL_NAME (t);
16094 /* Fall through. */
16095 case IDENTIFIER_NODE:
16096 if (IDENTIFIER_CONV_OP_P (t))
16097 {
16098 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16099 return make_conv_op_name (new_type);
16100 }
16101 else
16102 return t;
16103
16104 case CONSTRUCTOR:
16105 /* This is handled by tsubst_copy_and_build. */
16106 gcc_unreachable ();
16107
16108 case VA_ARG_EXPR:
16109 {
16110 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16111 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16112 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16113 }
16114
16115 case CLEANUP_POINT_EXPR:
16116 /* We shouldn't have built any of these during initial template
16117 generation. Instead, they should be built during instantiation
16118 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16119 gcc_unreachable ();
16120
16121 case OFFSET_REF:
16122 {
16123 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16124 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16125 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16126 r = build2 (code, type, op0, op1);
16127 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16128 if (!mark_used (TREE_OPERAND (r, 1), complain)
16129 && !(complain & tf_error))
16130 return error_mark_node;
16131 return r;
16132 }
16133
16134 case EXPR_PACK_EXPANSION:
16135 error ("invalid use of pack expansion expression");
16136 return error_mark_node;
16137
16138 case NONTYPE_ARGUMENT_PACK:
16139 error ("use %<...%> to expand argument pack");
16140 return error_mark_node;
16141
16142 case VOID_CST:
16143 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16144 return t;
16145
16146 case INTEGER_CST:
16147 case REAL_CST:
16148 case STRING_CST:
16149 case COMPLEX_CST:
16150 {
16151 /* Instantiate any typedefs in the type. */
16152 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16153 r = fold_convert (type, t);
16154 gcc_assert (TREE_CODE (r) == code);
16155 return r;
16156 }
16157
16158 case PTRMEM_CST:
16159 /* These can sometimes show up in a partial instantiation, but never
16160 involve template parms. */
16161 gcc_assert (!uses_template_parms (t));
16162 return t;
16163
16164 case UNARY_LEFT_FOLD_EXPR:
16165 return tsubst_unary_left_fold (t, args, complain, in_decl);
16166 case UNARY_RIGHT_FOLD_EXPR:
16167 return tsubst_unary_right_fold (t, args, complain, in_decl);
16168 case BINARY_LEFT_FOLD_EXPR:
16169 return tsubst_binary_left_fold (t, args, complain, in_decl);
16170 case BINARY_RIGHT_FOLD_EXPR:
16171 return tsubst_binary_right_fold (t, args, complain, in_decl);
16172 case PREDICT_EXPR:
16173 return t;
16174
16175 case DEBUG_BEGIN_STMT:
16176 /* ??? There's no point in copying it for now, but maybe some
16177 day it will contain more information, such as a pointer back
16178 to the containing function, inlined copy or so. */
16179 return t;
16180
16181 default:
16182 /* We shouldn't get here, but keep going if !flag_checking. */
16183 if (flag_checking)
16184 gcc_unreachable ();
16185 return t;
16186 }
16187 }
16188
16189 /* Helper function for tsubst_omp_clauses, used for instantiation of
16190 OMP_CLAUSE_DECL of clauses. */
16191
16192 static tree
16193 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16194 tree in_decl, tree *iterator_cache)
16195 {
16196 if (decl == NULL_TREE)
16197 return NULL_TREE;
16198
16199 /* Handle OpenMP iterators. */
16200 if (TREE_CODE (decl) == TREE_LIST
16201 && TREE_PURPOSE (decl)
16202 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16203 {
16204 tree ret;
16205 if (iterator_cache[0] == TREE_PURPOSE (decl))
16206 ret = iterator_cache[1];
16207 else
16208 {
16209 tree *tp = &ret;
16210 begin_scope (sk_omp, NULL);
16211 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16212 {
16213 *tp = copy_node (it);
16214 TREE_VEC_ELT (*tp, 0)
16215 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16216 TREE_VEC_ELT (*tp, 1)
16217 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16218 /*integral_constant_expression_p=*/false);
16219 TREE_VEC_ELT (*tp, 2)
16220 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16221 /*integral_constant_expression_p=*/false);
16222 TREE_VEC_ELT (*tp, 3)
16223 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16224 /*integral_constant_expression_p=*/false);
16225 TREE_CHAIN (*tp) = NULL_TREE;
16226 tp = &TREE_CHAIN (*tp);
16227 }
16228 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16229 iterator_cache[0] = TREE_PURPOSE (decl);
16230 iterator_cache[1] = ret;
16231 }
16232 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16233 args, complain,
16234 in_decl, NULL));
16235 }
16236
16237 /* Handle an OpenMP array section represented as a TREE_LIST (or
16238 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16239 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16240 TREE_LIST. We can handle it exactly the same as an array section
16241 (purpose, value, and a chain), even though the nomenclature
16242 (low_bound, length, etc) is different. */
16243 if (TREE_CODE (decl) == TREE_LIST)
16244 {
16245 tree low_bound
16246 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16247 /*integral_constant_expression_p=*/false);
16248 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16249 /*integral_constant_expression_p=*/false);
16250 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16251 in_decl, NULL);
16252 if (TREE_PURPOSE (decl) == low_bound
16253 && TREE_VALUE (decl) == length
16254 && TREE_CHAIN (decl) == chain)
16255 return decl;
16256 tree ret = tree_cons (low_bound, length, chain);
16257 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16258 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16259 return ret;
16260 }
16261 tree ret = tsubst_expr (decl, args, complain, in_decl,
16262 /*integral_constant_expression_p=*/false);
16263 /* Undo convert_from_reference tsubst_expr could have called. */
16264 if (decl
16265 && REFERENCE_REF_P (ret)
16266 && !REFERENCE_REF_P (decl))
16267 ret = TREE_OPERAND (ret, 0);
16268 return ret;
16269 }
16270
16271 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16272
16273 static tree
16274 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16275 tree args, tsubst_flags_t complain, tree in_decl)
16276 {
16277 tree new_clauses = NULL_TREE, nc, oc;
16278 tree linear_no_step = NULL_TREE;
16279 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16280
16281 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16282 {
16283 nc = copy_node (oc);
16284 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16285 new_clauses = nc;
16286
16287 switch (OMP_CLAUSE_CODE (nc))
16288 {
16289 case OMP_CLAUSE_LASTPRIVATE:
16290 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16291 {
16292 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16293 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16294 in_decl, /*integral_constant_expression_p=*/false);
16295 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16296 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16297 }
16298 /* FALLTHRU */
16299 case OMP_CLAUSE_PRIVATE:
16300 case OMP_CLAUSE_SHARED:
16301 case OMP_CLAUSE_FIRSTPRIVATE:
16302 case OMP_CLAUSE_COPYIN:
16303 case OMP_CLAUSE_COPYPRIVATE:
16304 case OMP_CLAUSE_UNIFORM:
16305 case OMP_CLAUSE_DEPEND:
16306 case OMP_CLAUSE_FROM:
16307 case OMP_CLAUSE_TO:
16308 case OMP_CLAUSE_MAP:
16309 case OMP_CLAUSE_NONTEMPORAL:
16310 case OMP_CLAUSE_USE_DEVICE_PTR:
16311 case OMP_CLAUSE_IS_DEVICE_PTR:
16312 OMP_CLAUSE_DECL (nc)
16313 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16314 in_decl, iterator_cache);
16315 break;
16316 case OMP_CLAUSE_TILE:
16317 case OMP_CLAUSE_IF:
16318 case OMP_CLAUSE_NUM_THREADS:
16319 case OMP_CLAUSE_SCHEDULE:
16320 case OMP_CLAUSE_COLLAPSE:
16321 case OMP_CLAUSE_FINAL:
16322 case OMP_CLAUSE_DEVICE:
16323 case OMP_CLAUSE_DIST_SCHEDULE:
16324 case OMP_CLAUSE_NUM_TEAMS:
16325 case OMP_CLAUSE_THREAD_LIMIT:
16326 case OMP_CLAUSE_SAFELEN:
16327 case OMP_CLAUSE_SIMDLEN:
16328 case OMP_CLAUSE_NUM_TASKS:
16329 case OMP_CLAUSE_GRAINSIZE:
16330 case OMP_CLAUSE_PRIORITY:
16331 case OMP_CLAUSE_ORDERED:
16332 case OMP_CLAUSE_HINT:
16333 case OMP_CLAUSE_NUM_GANGS:
16334 case OMP_CLAUSE_NUM_WORKERS:
16335 case OMP_CLAUSE_VECTOR_LENGTH:
16336 case OMP_CLAUSE_WORKER:
16337 case OMP_CLAUSE_VECTOR:
16338 case OMP_CLAUSE_ASYNC:
16339 case OMP_CLAUSE_WAIT:
16340 OMP_CLAUSE_OPERAND (nc, 0)
16341 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16342 in_decl, /*integral_constant_expression_p=*/false);
16343 break;
16344 case OMP_CLAUSE_REDUCTION:
16345 case OMP_CLAUSE_IN_REDUCTION:
16346 case OMP_CLAUSE_TASK_REDUCTION:
16347 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16348 {
16349 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16350 if (TREE_CODE (placeholder) == SCOPE_REF)
16351 {
16352 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16353 complain, in_decl);
16354 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16355 = build_qualified_name (NULL_TREE, scope,
16356 TREE_OPERAND (placeholder, 1),
16357 false);
16358 }
16359 else
16360 gcc_assert (identifier_p (placeholder));
16361 }
16362 OMP_CLAUSE_DECL (nc)
16363 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16364 in_decl, NULL);
16365 break;
16366 case OMP_CLAUSE_GANG:
16367 case OMP_CLAUSE_ALIGNED:
16368 OMP_CLAUSE_DECL (nc)
16369 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16370 in_decl, NULL);
16371 OMP_CLAUSE_OPERAND (nc, 1)
16372 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16373 in_decl, /*integral_constant_expression_p=*/false);
16374 break;
16375 case OMP_CLAUSE_LINEAR:
16376 OMP_CLAUSE_DECL (nc)
16377 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16378 in_decl, NULL);
16379 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16380 {
16381 gcc_assert (!linear_no_step);
16382 linear_no_step = nc;
16383 }
16384 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16385 OMP_CLAUSE_LINEAR_STEP (nc)
16386 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16387 complain, in_decl, NULL);
16388 else
16389 OMP_CLAUSE_LINEAR_STEP (nc)
16390 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16391 in_decl,
16392 /*integral_constant_expression_p=*/false);
16393 break;
16394 case OMP_CLAUSE_NOWAIT:
16395 case OMP_CLAUSE_DEFAULT:
16396 case OMP_CLAUSE_UNTIED:
16397 case OMP_CLAUSE_MERGEABLE:
16398 case OMP_CLAUSE_INBRANCH:
16399 case OMP_CLAUSE_NOTINBRANCH:
16400 case OMP_CLAUSE_PROC_BIND:
16401 case OMP_CLAUSE_FOR:
16402 case OMP_CLAUSE_PARALLEL:
16403 case OMP_CLAUSE_SECTIONS:
16404 case OMP_CLAUSE_TASKGROUP:
16405 case OMP_CLAUSE_NOGROUP:
16406 case OMP_CLAUSE_THREADS:
16407 case OMP_CLAUSE_SIMD:
16408 case OMP_CLAUSE_DEFAULTMAP:
16409 case OMP_CLAUSE_INDEPENDENT:
16410 case OMP_CLAUSE_AUTO:
16411 case OMP_CLAUSE_SEQ:
16412 case OMP_CLAUSE_IF_PRESENT:
16413 case OMP_CLAUSE_FINALIZE:
16414 break;
16415 default:
16416 gcc_unreachable ();
16417 }
16418 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16419 switch (OMP_CLAUSE_CODE (nc))
16420 {
16421 case OMP_CLAUSE_SHARED:
16422 case OMP_CLAUSE_PRIVATE:
16423 case OMP_CLAUSE_FIRSTPRIVATE:
16424 case OMP_CLAUSE_LASTPRIVATE:
16425 case OMP_CLAUSE_COPYPRIVATE:
16426 case OMP_CLAUSE_LINEAR:
16427 case OMP_CLAUSE_REDUCTION:
16428 case OMP_CLAUSE_IN_REDUCTION:
16429 case OMP_CLAUSE_TASK_REDUCTION:
16430 case OMP_CLAUSE_USE_DEVICE_PTR:
16431 case OMP_CLAUSE_IS_DEVICE_PTR:
16432 /* tsubst_expr on SCOPE_REF results in returning
16433 finish_non_static_data_member result. Undo that here. */
16434 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16435 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16436 == IDENTIFIER_NODE))
16437 {
16438 tree t = OMP_CLAUSE_DECL (nc);
16439 tree v = t;
16440 while (v)
16441 switch (TREE_CODE (v))
16442 {
16443 case COMPONENT_REF:
16444 case MEM_REF:
16445 case INDIRECT_REF:
16446 CASE_CONVERT:
16447 case POINTER_PLUS_EXPR:
16448 v = TREE_OPERAND (v, 0);
16449 continue;
16450 case PARM_DECL:
16451 if (DECL_CONTEXT (v) == current_function_decl
16452 && DECL_ARTIFICIAL (v)
16453 && DECL_NAME (v) == this_identifier)
16454 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16455 /* FALLTHRU */
16456 default:
16457 v = NULL_TREE;
16458 break;
16459 }
16460 }
16461 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16462 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16463 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16464 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16465 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16466 {
16467 tree decl = OMP_CLAUSE_DECL (nc);
16468 if (VAR_P (decl))
16469 {
16470 retrofit_lang_decl (decl);
16471 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16472 }
16473 }
16474 break;
16475 default:
16476 break;
16477 }
16478 }
16479
16480 new_clauses = nreverse (new_clauses);
16481 if (ort != C_ORT_OMP_DECLARE_SIMD)
16482 {
16483 new_clauses = finish_omp_clauses (new_clauses, ort);
16484 if (linear_no_step)
16485 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16486 if (nc == linear_no_step)
16487 {
16488 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16489 break;
16490 }
16491 }
16492 return new_clauses;
16493 }
16494
16495 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16496
16497 static tree
16498 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16499 tree in_decl)
16500 {
16501 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16502
16503 tree purpose, value, chain;
16504
16505 if (t == NULL)
16506 return t;
16507
16508 if (TREE_CODE (t) != TREE_LIST)
16509 return tsubst_copy_and_build (t, args, complain, in_decl,
16510 /*function_p=*/false,
16511 /*integral_constant_expression_p=*/false);
16512
16513 if (t == void_list_node)
16514 return t;
16515
16516 purpose = TREE_PURPOSE (t);
16517 if (purpose)
16518 purpose = RECUR (purpose);
16519 value = TREE_VALUE (t);
16520 if (value)
16521 {
16522 if (TREE_CODE (value) != LABEL_DECL)
16523 value = RECUR (value);
16524 else
16525 {
16526 value = lookup_label (DECL_NAME (value));
16527 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16528 TREE_USED (value) = 1;
16529 }
16530 }
16531 chain = TREE_CHAIN (t);
16532 if (chain && chain != void_type_node)
16533 chain = RECUR (chain);
16534 return tree_cons (purpose, value, chain);
16535 #undef RECUR
16536 }
16537
16538 /* Used to temporarily communicate the list of #pragma omp parallel
16539 clauses to #pragma omp for instantiation if they are combined
16540 together. */
16541
16542 static tree *omp_parallel_combined_clauses;
16543
16544 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16545 tree *, unsigned int *);
16546
16547 /* Substitute one OMP_FOR iterator. */
16548
16549 static bool
16550 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16551 tree initv, tree condv, tree incrv, tree *clauses,
16552 tree args, tsubst_flags_t complain, tree in_decl,
16553 bool integral_constant_expression_p)
16554 {
16555 #define RECUR(NODE) \
16556 tsubst_expr ((NODE), args, complain, in_decl, \
16557 integral_constant_expression_p)
16558 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16559 bool ret = false;
16560
16561 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16562 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16563
16564 decl = TREE_OPERAND (init, 0);
16565 init = TREE_OPERAND (init, 1);
16566 tree decl_expr = NULL_TREE;
16567 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16568 if (range_for)
16569 {
16570 bool decomp = false;
16571 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16572 {
16573 tree v = DECL_VALUE_EXPR (decl);
16574 if (TREE_CODE (v) == ARRAY_REF
16575 && VAR_P (TREE_OPERAND (v, 0))
16576 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16577 {
16578 tree decomp_first = NULL_TREE;
16579 unsigned decomp_cnt = 0;
16580 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16581 maybe_push_decl (d);
16582 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16583 in_decl, &decomp_first, &decomp_cnt);
16584 decomp = true;
16585 if (d == error_mark_node)
16586 decl = error_mark_node;
16587 else
16588 for (unsigned int i = 0; i < decomp_cnt; i++)
16589 {
16590 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16591 {
16592 tree v = build_nt (ARRAY_REF, d,
16593 size_int (decomp_cnt - i - 1),
16594 NULL_TREE, NULL_TREE);
16595 SET_DECL_VALUE_EXPR (decomp_first, v);
16596 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16597 }
16598 fit_decomposition_lang_decl (decomp_first, d);
16599 decomp_first = DECL_CHAIN (decomp_first);
16600 }
16601 }
16602 }
16603 decl = tsubst_decl (decl, args, complain);
16604 if (!decomp)
16605 maybe_push_decl (decl);
16606 }
16607 else if (init && TREE_CODE (init) == DECL_EXPR)
16608 {
16609 /* We need to jump through some hoops to handle declarations in the
16610 init-statement, since we might need to handle auto deduction,
16611 but we need to keep control of initialization. */
16612 decl_expr = init;
16613 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16614 decl = tsubst_decl (decl, args, complain);
16615 }
16616 else
16617 {
16618 if (TREE_CODE (decl) == SCOPE_REF)
16619 {
16620 decl = RECUR (decl);
16621 if (TREE_CODE (decl) == COMPONENT_REF)
16622 {
16623 tree v = decl;
16624 while (v)
16625 switch (TREE_CODE (v))
16626 {
16627 case COMPONENT_REF:
16628 case MEM_REF:
16629 case INDIRECT_REF:
16630 CASE_CONVERT:
16631 case POINTER_PLUS_EXPR:
16632 v = TREE_OPERAND (v, 0);
16633 continue;
16634 case PARM_DECL:
16635 if (DECL_CONTEXT (v) == current_function_decl
16636 && DECL_ARTIFICIAL (v)
16637 && DECL_NAME (v) == this_identifier)
16638 {
16639 decl = TREE_OPERAND (decl, 1);
16640 decl = omp_privatize_field (decl, false);
16641 }
16642 /* FALLTHRU */
16643 default:
16644 v = NULL_TREE;
16645 break;
16646 }
16647 }
16648 }
16649 else
16650 decl = RECUR (decl);
16651 }
16652 init = RECUR (init);
16653
16654 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16655 {
16656 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16657 if (TREE_CODE (o) == TREE_LIST)
16658 TREE_VEC_ELT (orig_declv, i)
16659 = tree_cons (RECUR (TREE_PURPOSE (o)),
16660 RECUR (TREE_VALUE (o)),
16661 NULL_TREE);
16662 else
16663 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16664 }
16665
16666 if (range_for)
16667 {
16668 tree this_pre_body = NULL_TREE;
16669 tree orig_init = NULL_TREE;
16670 tree orig_decl = NULL_TREE;
16671 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16672 orig_init, cond, incr);
16673 if (orig_decl)
16674 {
16675 if (orig_declv == NULL_TREE)
16676 orig_declv = copy_node (declv);
16677 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16678 ret = true;
16679 }
16680 else if (orig_declv)
16681 TREE_VEC_ELT (orig_declv, i) = decl;
16682 }
16683
16684 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16685 if (!range_for && auto_node && init)
16686 TREE_TYPE (decl)
16687 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16688
16689 gcc_assert (!type_dependent_expression_p (decl));
16690
16691 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16692 {
16693 if (decl_expr)
16694 {
16695 /* Declare the variable, but don't let that initialize it. */
16696 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16697 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16698 RECUR (decl_expr);
16699 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16700 }
16701
16702 if (!range_for)
16703 {
16704 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16705 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16706 if (TREE_CODE (incr) == MODIFY_EXPR)
16707 {
16708 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16709 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16710 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16711 NOP_EXPR, rhs, complain);
16712 }
16713 else
16714 incr = RECUR (incr);
16715 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16716 TREE_VEC_ELT (orig_declv, i) = decl;
16717 }
16718 TREE_VEC_ELT (declv, i) = decl;
16719 TREE_VEC_ELT (initv, i) = init;
16720 TREE_VEC_ELT (condv, i) = cond;
16721 TREE_VEC_ELT (incrv, i) = incr;
16722 return ret;
16723 }
16724
16725 if (decl_expr)
16726 {
16727 /* Declare and initialize the variable. */
16728 RECUR (decl_expr);
16729 init = NULL_TREE;
16730 }
16731 else if (init)
16732 {
16733 tree *pc;
16734 int j;
16735 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16736 {
16737 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16738 {
16739 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16740 && OMP_CLAUSE_DECL (*pc) == decl)
16741 break;
16742 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16743 && OMP_CLAUSE_DECL (*pc) == decl)
16744 {
16745 if (j)
16746 break;
16747 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16748 tree c = *pc;
16749 *pc = OMP_CLAUSE_CHAIN (c);
16750 OMP_CLAUSE_CHAIN (c) = *clauses;
16751 *clauses = c;
16752 }
16753 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16754 && OMP_CLAUSE_DECL (*pc) == decl)
16755 {
16756 error ("iteration variable %qD should not be firstprivate",
16757 decl);
16758 *pc = OMP_CLAUSE_CHAIN (*pc);
16759 }
16760 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16761 && OMP_CLAUSE_DECL (*pc) == decl)
16762 {
16763 error ("iteration variable %qD should not be reduction",
16764 decl);
16765 *pc = OMP_CLAUSE_CHAIN (*pc);
16766 }
16767 else
16768 pc = &OMP_CLAUSE_CHAIN (*pc);
16769 }
16770 if (*pc)
16771 break;
16772 }
16773 if (*pc == NULL_TREE)
16774 {
16775 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16776 OMP_CLAUSE_DECL (c) = decl;
16777 c = finish_omp_clauses (c, C_ORT_OMP);
16778 if (c)
16779 {
16780 OMP_CLAUSE_CHAIN (c) = *clauses;
16781 *clauses = c;
16782 }
16783 }
16784 }
16785 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16786 if (COMPARISON_CLASS_P (cond))
16787 {
16788 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16789 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16790 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16791 }
16792 else
16793 cond = RECUR (cond);
16794 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16795 switch (TREE_CODE (incr))
16796 {
16797 case PREINCREMENT_EXPR:
16798 case PREDECREMENT_EXPR:
16799 case POSTINCREMENT_EXPR:
16800 case POSTDECREMENT_EXPR:
16801 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16802 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16803 break;
16804 case MODIFY_EXPR:
16805 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16806 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16807 {
16808 tree rhs = TREE_OPERAND (incr, 1);
16809 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16810 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16811 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16812 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16813 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16814 rhs0, rhs1));
16815 }
16816 else
16817 incr = RECUR (incr);
16818 break;
16819 case MODOP_EXPR:
16820 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16821 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16822 {
16823 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16824 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16825 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16826 TREE_TYPE (decl), lhs,
16827 RECUR (TREE_OPERAND (incr, 2))));
16828 }
16829 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16830 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16831 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16832 {
16833 tree rhs = TREE_OPERAND (incr, 2);
16834 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16835 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16836 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16837 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16838 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16839 rhs0, rhs1));
16840 }
16841 else
16842 incr = RECUR (incr);
16843 break;
16844 default:
16845 incr = RECUR (incr);
16846 break;
16847 }
16848
16849 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16850 TREE_VEC_ELT (orig_declv, i) = decl;
16851 TREE_VEC_ELT (declv, i) = decl;
16852 TREE_VEC_ELT (initv, i) = init;
16853 TREE_VEC_ELT (condv, i) = cond;
16854 TREE_VEC_ELT (incrv, i) = incr;
16855 return false;
16856 #undef RECUR
16857 }
16858
16859 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16860 of OMP_TARGET's body. */
16861
16862 static tree
16863 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16864 {
16865 *walk_subtrees = 0;
16866 switch (TREE_CODE (*tp))
16867 {
16868 case OMP_TEAMS:
16869 return *tp;
16870 case BIND_EXPR:
16871 case STATEMENT_LIST:
16872 *walk_subtrees = 1;
16873 break;
16874 default:
16875 break;
16876 }
16877 return NULL_TREE;
16878 }
16879
16880 /* Helper function for tsubst_expr. For decomposition declaration
16881 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16882 also the corresponding decls representing the identifiers
16883 of the decomposition declaration. Return DECL if successful
16884 or error_mark_node otherwise, set *FIRST to the first decl
16885 in the list chained through DECL_CHAIN and *CNT to the number
16886 of such decls. */
16887
16888 static tree
16889 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16890 tsubst_flags_t complain, tree in_decl, tree *first,
16891 unsigned int *cnt)
16892 {
16893 tree decl2, decl3, prev = decl;
16894 *cnt = 0;
16895 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16896 for (decl2 = DECL_CHAIN (pattern_decl);
16897 decl2
16898 && VAR_P (decl2)
16899 && DECL_DECOMPOSITION_P (decl2)
16900 && DECL_NAME (decl2);
16901 decl2 = DECL_CHAIN (decl2))
16902 {
16903 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16904 {
16905 gcc_assert (errorcount);
16906 return error_mark_node;
16907 }
16908 (*cnt)++;
16909 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16910 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16911 tree v = DECL_VALUE_EXPR (decl2);
16912 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16913 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16914 decl3 = tsubst (decl2, args, complain, in_decl);
16915 SET_DECL_VALUE_EXPR (decl2, v);
16916 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16917 if (VAR_P (decl3))
16918 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16919 else
16920 {
16921 gcc_assert (errorcount);
16922 decl = error_mark_node;
16923 continue;
16924 }
16925 maybe_push_decl (decl3);
16926 if (error_operand_p (decl3))
16927 decl = error_mark_node;
16928 else if (decl != error_mark_node
16929 && DECL_CHAIN (decl3) != prev
16930 && decl != prev)
16931 {
16932 gcc_assert (errorcount);
16933 decl = error_mark_node;
16934 }
16935 else
16936 prev = decl3;
16937 }
16938 *first = prev;
16939 return decl;
16940 }
16941
16942 /* Return the proper local_specialization for init-capture pack DECL. */
16943
16944 static tree
16945 lookup_init_capture_pack (tree decl)
16946 {
16947 /* We handle normal pack captures by forwarding to the specialization of the
16948 captured parameter. We can't do that for pack init-captures; we need them
16949 to have their own local_specialization. We created the individual
16950 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16951 when we process the DECL_EXPR for the pack init-capture in the template.
16952 So, how do we find them? We don't know the capture proxy pack when
16953 building the individual resulting proxies, and we don't know the
16954 individual proxies when instantiating the pack. What we have in common is
16955 the FIELD_DECL.
16956
16957 So...when we instantiate the FIELD_DECL, we stick the result in
16958 local_specializations. Then at the DECL_EXPR we look up that result, see
16959 how many elements it has, synthesize the names, and look them up. */
16960
16961 tree cname = DECL_NAME (decl);
16962 tree val = DECL_VALUE_EXPR (decl);
16963 tree field = TREE_OPERAND (val, 1);
16964 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16965 tree fpack = retrieve_local_specialization (field);
16966 if (fpack == error_mark_node)
16967 return error_mark_node;
16968
16969 int len = 1;
16970 tree vec = NULL_TREE;
16971 tree r = NULL_TREE;
16972 if (TREE_CODE (fpack) == TREE_VEC)
16973 {
16974 len = TREE_VEC_LENGTH (fpack);
16975 vec = make_tree_vec (len);
16976 r = make_node (NONTYPE_ARGUMENT_PACK);
16977 SET_ARGUMENT_PACK_ARGS (r, vec);
16978 }
16979 for (int i = 0; i < len; ++i)
16980 {
16981 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16982 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
16983 if (vec)
16984 TREE_VEC_ELT (vec, i) = elt;
16985 else
16986 r = elt;
16987 }
16988 return r;
16989 }
16990
16991 /* Like tsubst_copy for expressions, etc. but also does semantic
16992 processing. */
16993
16994 tree
16995 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16996 bool integral_constant_expression_p)
16997 {
16998 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16999 #define RECUR(NODE) \
17000 tsubst_expr ((NODE), args, complain, in_decl, \
17001 integral_constant_expression_p)
17002
17003 tree stmt, tmp;
17004 tree r;
17005 location_t loc;
17006
17007 if (t == NULL_TREE || t == error_mark_node)
17008 return t;
17009
17010 loc = input_location;
17011 if (location_t eloc = cp_expr_location (t))
17012 input_location = eloc;
17013 if (STATEMENT_CODE_P (TREE_CODE (t)))
17014 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17015
17016 switch (TREE_CODE (t))
17017 {
17018 case STATEMENT_LIST:
17019 {
17020 tree_stmt_iterator i;
17021 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17022 RECUR (tsi_stmt (i));
17023 break;
17024 }
17025
17026 case CTOR_INITIALIZER:
17027 finish_mem_initializers (tsubst_initializer_list
17028 (TREE_OPERAND (t, 0), args));
17029 break;
17030
17031 case RETURN_EXPR:
17032 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17033 break;
17034
17035 case EXPR_STMT:
17036 tmp = RECUR (EXPR_STMT_EXPR (t));
17037 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17038 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17039 else
17040 finish_expr_stmt (tmp);
17041 break;
17042
17043 case USING_STMT:
17044 finish_local_using_directive (USING_STMT_NAMESPACE (t),
17045 /*attribs=*/NULL_TREE);
17046 break;
17047
17048 case DECL_EXPR:
17049 {
17050 tree decl, pattern_decl;
17051 tree init;
17052
17053 pattern_decl = decl = DECL_EXPR_DECL (t);
17054 if (TREE_CODE (decl) == LABEL_DECL)
17055 finish_label_decl (DECL_NAME (decl));
17056 else if (TREE_CODE (decl) == USING_DECL)
17057 {
17058 tree scope = USING_DECL_SCOPE (decl);
17059 tree name = DECL_NAME (decl);
17060
17061 scope = tsubst (scope, args, complain, in_decl);
17062 decl = lookup_qualified_name (scope, name,
17063 /*is_type_p=*/false,
17064 /*complain=*/false);
17065 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
17066 qualified_name_lookup_error (scope, name, decl, input_location);
17067 else
17068 finish_local_using_decl (decl, scope, name);
17069 }
17070 else if (is_capture_proxy (decl)
17071 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17072 {
17073 /* We're in tsubst_lambda_expr, we've already inserted a new
17074 capture proxy, so look it up and register it. */
17075 tree inst;
17076 if (!DECL_PACK_P (decl))
17077 {
17078 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17079 /*nonclass*/1, /*block_p=*/true,
17080 /*ns_only*/0, LOOKUP_HIDDEN);
17081 gcc_assert (inst != decl && is_capture_proxy (inst));
17082 }
17083 else if (is_normal_capture_proxy (decl))
17084 {
17085 inst = (retrieve_local_specialization
17086 (DECL_CAPTURED_VARIABLE (decl)));
17087 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17088 }
17089 else
17090 inst = lookup_init_capture_pack (decl);
17091
17092 register_local_specialization (inst, decl);
17093 break;
17094 }
17095 else if (DECL_PRETTY_FUNCTION_P (decl))
17096 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17097 DECL_NAME (decl),
17098 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17099 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17100 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17101 /* Don't copy the old closure; we'll create a new one in
17102 tsubst_lambda_expr. */
17103 break;
17104 else
17105 {
17106 init = DECL_INITIAL (decl);
17107 decl = tsubst (decl, args, complain, in_decl);
17108 if (decl != error_mark_node)
17109 {
17110 /* By marking the declaration as instantiated, we avoid
17111 trying to instantiate it. Since instantiate_decl can't
17112 handle local variables, and since we've already done
17113 all that needs to be done, that's the right thing to
17114 do. */
17115 if (VAR_P (decl))
17116 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17117 if (VAR_P (decl) && !DECL_NAME (decl)
17118 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17119 /* Anonymous aggregates are a special case. */
17120 finish_anon_union (decl);
17121 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17122 {
17123 DECL_CONTEXT (decl) = current_function_decl;
17124 if (DECL_NAME (decl) == this_identifier)
17125 {
17126 tree lam = DECL_CONTEXT (current_function_decl);
17127 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17128 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17129 }
17130 insert_capture_proxy (decl);
17131 }
17132 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17133 /* We already did a pushtag. */;
17134 else if (TREE_CODE (decl) == FUNCTION_DECL
17135 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17136 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17137 {
17138 DECL_CONTEXT (decl) = NULL_TREE;
17139 pushdecl (decl);
17140 DECL_CONTEXT (decl) = current_function_decl;
17141 cp_check_omp_declare_reduction (decl);
17142 }
17143 else
17144 {
17145 int const_init = false;
17146 unsigned int cnt = 0;
17147 tree first = NULL_TREE, ndecl = error_mark_node;
17148 maybe_push_decl (decl);
17149
17150 if (VAR_P (decl)
17151 && DECL_DECOMPOSITION_P (decl)
17152 && TREE_TYPE (pattern_decl) != error_mark_node)
17153 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17154 complain, in_decl, &first,
17155 &cnt);
17156
17157 init = tsubst_init (init, decl, args, complain, in_decl);
17158
17159 if (VAR_P (decl))
17160 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17161 (pattern_decl));
17162
17163 if (ndecl != error_mark_node)
17164 cp_maybe_mangle_decomp (ndecl, first, cnt);
17165
17166 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17167
17168 if (ndecl != error_mark_node)
17169 cp_finish_decomp (ndecl, first, cnt);
17170 }
17171 }
17172 }
17173
17174 break;
17175 }
17176
17177 case FOR_STMT:
17178 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17179 RECUR (FOR_INIT_STMT (t));
17180 finish_init_stmt (stmt);
17181 tmp = RECUR (FOR_COND (t));
17182 finish_for_cond (tmp, stmt, false, 0);
17183 tmp = RECUR (FOR_EXPR (t));
17184 finish_for_expr (tmp, stmt);
17185 {
17186 bool prev = note_iteration_stmt_body_start ();
17187 RECUR (FOR_BODY (t));
17188 note_iteration_stmt_body_end (prev);
17189 }
17190 finish_for_stmt (stmt);
17191 break;
17192
17193 case RANGE_FOR_STMT:
17194 {
17195 /* Construct another range_for, if this is not a final
17196 substitution (for inside inside a generic lambda of a
17197 template). Otherwise convert to a regular for. */
17198 tree decl, expr;
17199 stmt = (processing_template_decl
17200 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17201 : begin_for_stmt (NULL_TREE, NULL_TREE));
17202 RECUR (RANGE_FOR_INIT_STMT (t));
17203 decl = RANGE_FOR_DECL (t);
17204 decl = tsubst (decl, args, complain, in_decl);
17205 maybe_push_decl (decl);
17206 expr = RECUR (RANGE_FOR_EXPR (t));
17207
17208 tree decomp_first = NULL_TREE;
17209 unsigned decomp_cnt = 0;
17210 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17211 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17212 complain, in_decl,
17213 &decomp_first, &decomp_cnt);
17214
17215 if (processing_template_decl)
17216 {
17217 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17218 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17219 finish_range_for_decl (stmt, decl, expr);
17220 if (decomp_first && decl != error_mark_node)
17221 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17222 }
17223 else
17224 {
17225 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17226 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17227 stmt = cp_convert_range_for (stmt, decl, expr,
17228 decomp_first, decomp_cnt,
17229 RANGE_FOR_IVDEP (t), unroll);
17230 }
17231
17232 bool prev = note_iteration_stmt_body_start ();
17233 RECUR (RANGE_FOR_BODY (t));
17234 note_iteration_stmt_body_end (prev);
17235 finish_for_stmt (stmt);
17236 }
17237 break;
17238
17239 case WHILE_STMT:
17240 stmt = begin_while_stmt ();
17241 tmp = RECUR (WHILE_COND (t));
17242 finish_while_stmt_cond (tmp, stmt, false, 0);
17243 {
17244 bool prev = note_iteration_stmt_body_start ();
17245 RECUR (WHILE_BODY (t));
17246 note_iteration_stmt_body_end (prev);
17247 }
17248 finish_while_stmt (stmt);
17249 break;
17250
17251 case DO_STMT:
17252 stmt = begin_do_stmt ();
17253 {
17254 bool prev = note_iteration_stmt_body_start ();
17255 RECUR (DO_BODY (t));
17256 note_iteration_stmt_body_end (prev);
17257 }
17258 finish_do_body (stmt);
17259 tmp = RECUR (DO_COND (t));
17260 finish_do_stmt (tmp, stmt, false, 0);
17261 break;
17262
17263 case IF_STMT:
17264 stmt = begin_if_stmt ();
17265 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17266 if (IF_STMT_CONSTEXPR_P (t))
17267 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17268 tmp = RECUR (IF_COND (t));
17269 tmp = finish_if_stmt_cond (tmp, stmt);
17270 if (IF_STMT_CONSTEXPR_P (t)
17271 && instantiation_dependent_expression_p (tmp))
17272 {
17273 /* We're partially instantiating a generic lambda, but the condition
17274 of the constexpr if is still dependent. Don't substitute into the
17275 branches now, just remember the template arguments. */
17276 do_poplevel (IF_SCOPE (stmt));
17277 IF_COND (stmt) = IF_COND (t);
17278 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17279 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17280 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17281 add_stmt (stmt);
17282 break;
17283 }
17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17285 /* Don't instantiate the THEN_CLAUSE. */;
17286 else
17287 {
17288 tree folded = fold_non_dependent_expr (tmp, complain);
17289 bool inhibit = integer_zerop (folded);
17290 if (inhibit)
17291 ++c_inhibit_evaluation_warnings;
17292 RECUR (THEN_CLAUSE (t));
17293 if (inhibit)
17294 --c_inhibit_evaluation_warnings;
17295 }
17296 finish_then_clause (stmt);
17297
17298 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17299 /* Don't instantiate the ELSE_CLAUSE. */;
17300 else if (ELSE_CLAUSE (t))
17301 {
17302 tree folded = fold_non_dependent_expr (tmp, complain);
17303 bool inhibit = integer_nonzerop (folded);
17304 begin_else_clause (stmt);
17305 if (inhibit)
17306 ++c_inhibit_evaluation_warnings;
17307 RECUR (ELSE_CLAUSE (t));
17308 if (inhibit)
17309 --c_inhibit_evaluation_warnings;
17310 finish_else_clause (stmt);
17311 }
17312
17313 finish_if_stmt (stmt);
17314 break;
17315
17316 case BIND_EXPR:
17317 if (BIND_EXPR_BODY_BLOCK (t))
17318 stmt = begin_function_body ();
17319 else
17320 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17321 ? BCS_TRY_BLOCK : 0);
17322
17323 RECUR (BIND_EXPR_BODY (t));
17324
17325 if (BIND_EXPR_BODY_BLOCK (t))
17326 finish_function_body (stmt);
17327 else
17328 finish_compound_stmt (stmt);
17329 break;
17330
17331 case BREAK_STMT:
17332 finish_break_stmt ();
17333 break;
17334
17335 case CONTINUE_STMT:
17336 finish_continue_stmt ();
17337 break;
17338
17339 case SWITCH_STMT:
17340 stmt = begin_switch_stmt ();
17341 tmp = RECUR (SWITCH_STMT_COND (t));
17342 finish_switch_cond (tmp, stmt);
17343 RECUR (SWITCH_STMT_BODY (t));
17344 finish_switch_stmt (stmt);
17345 break;
17346
17347 case CASE_LABEL_EXPR:
17348 {
17349 tree decl = CASE_LABEL (t);
17350 tree low = RECUR (CASE_LOW (t));
17351 tree high = RECUR (CASE_HIGH (t));
17352 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17353 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17354 {
17355 tree label = CASE_LABEL (l);
17356 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17357 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17358 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17359 }
17360 }
17361 break;
17362
17363 case LABEL_EXPR:
17364 {
17365 tree decl = LABEL_EXPR_LABEL (t);
17366 tree label;
17367
17368 label = finish_label_stmt (DECL_NAME (decl));
17369 if (TREE_CODE (label) == LABEL_DECL)
17370 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17371 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17372 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17373 }
17374 break;
17375
17376 case GOTO_EXPR:
17377 tmp = GOTO_DESTINATION (t);
17378 if (TREE_CODE (tmp) != LABEL_DECL)
17379 /* Computed goto's must be tsubst'd into. On the other hand,
17380 non-computed gotos must not be; the identifier in question
17381 will have no binding. */
17382 tmp = RECUR (tmp);
17383 else
17384 tmp = DECL_NAME (tmp);
17385 finish_goto_stmt (tmp);
17386 break;
17387
17388 case ASM_EXPR:
17389 {
17390 tree string = RECUR (ASM_STRING (t));
17391 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17392 complain, in_decl);
17393 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17394 complain, in_decl);
17395 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17396 complain, in_decl);
17397 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17398 complain, in_decl);
17399 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17400 clobbers, labels, ASM_INLINE_P (t));
17401 tree asm_expr = tmp;
17402 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17403 asm_expr = TREE_OPERAND (asm_expr, 0);
17404 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17405 }
17406 break;
17407
17408 case TRY_BLOCK:
17409 if (CLEANUP_P (t))
17410 {
17411 stmt = begin_try_block ();
17412 RECUR (TRY_STMTS (t));
17413 finish_cleanup_try_block (stmt);
17414 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17415 }
17416 else
17417 {
17418 tree compound_stmt = NULL_TREE;
17419
17420 if (FN_TRY_BLOCK_P (t))
17421 stmt = begin_function_try_block (&compound_stmt);
17422 else
17423 stmt = begin_try_block ();
17424
17425 RECUR (TRY_STMTS (t));
17426
17427 if (FN_TRY_BLOCK_P (t))
17428 finish_function_try_block (stmt);
17429 else
17430 finish_try_block (stmt);
17431
17432 RECUR (TRY_HANDLERS (t));
17433 if (FN_TRY_BLOCK_P (t))
17434 finish_function_handler_sequence (stmt, compound_stmt);
17435 else
17436 finish_handler_sequence (stmt);
17437 }
17438 break;
17439
17440 case HANDLER:
17441 {
17442 tree decl = HANDLER_PARMS (t);
17443
17444 if (decl)
17445 {
17446 decl = tsubst (decl, args, complain, in_decl);
17447 /* Prevent instantiate_decl from trying to instantiate
17448 this variable. We've already done all that needs to be
17449 done. */
17450 if (decl != error_mark_node)
17451 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17452 }
17453 stmt = begin_handler ();
17454 finish_handler_parms (decl, stmt);
17455 RECUR (HANDLER_BODY (t));
17456 finish_handler (stmt);
17457 }
17458 break;
17459
17460 case TAG_DEFN:
17461 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17462 if (CLASS_TYPE_P (tmp))
17463 {
17464 /* Local classes are not independent templates; they are
17465 instantiated along with their containing function. And this
17466 way we don't have to deal with pushing out of one local class
17467 to instantiate a member of another local class. */
17468 /* Closures are handled by the LAMBDA_EXPR. */
17469 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17470 complete_type (tmp);
17471 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17472 if ((VAR_P (fld)
17473 || (TREE_CODE (fld) == FUNCTION_DECL
17474 && !DECL_ARTIFICIAL (fld)))
17475 && DECL_TEMPLATE_INSTANTIATION (fld))
17476 instantiate_decl (fld, /*defer_ok=*/false,
17477 /*expl_inst_class=*/false);
17478 }
17479 break;
17480
17481 case STATIC_ASSERT:
17482 {
17483 tree condition;
17484
17485 ++c_inhibit_evaluation_warnings;
17486 condition =
17487 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17488 args,
17489 complain, in_decl,
17490 /*integral_constant_expression_p=*/true);
17491 --c_inhibit_evaluation_warnings;
17492
17493 finish_static_assert (condition,
17494 STATIC_ASSERT_MESSAGE (t),
17495 STATIC_ASSERT_SOURCE_LOCATION (t),
17496 /*member_p=*/false);
17497 }
17498 break;
17499
17500 case OACC_KERNELS:
17501 case OACC_PARALLEL:
17502 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17503 in_decl);
17504 stmt = begin_omp_parallel ();
17505 RECUR (OMP_BODY (t));
17506 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17507 break;
17508
17509 case OMP_PARALLEL:
17510 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17511 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17512 complain, in_decl);
17513 if (OMP_PARALLEL_COMBINED (t))
17514 omp_parallel_combined_clauses = &tmp;
17515 stmt = begin_omp_parallel ();
17516 RECUR (OMP_PARALLEL_BODY (t));
17517 gcc_assert (omp_parallel_combined_clauses == NULL);
17518 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17519 = OMP_PARALLEL_COMBINED (t);
17520 pop_omp_privatization_clauses (r);
17521 break;
17522
17523 case OMP_TASK:
17524 if (OMP_TASK_BODY (t) == NULL_TREE)
17525 {
17526 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17527 complain, in_decl);
17528 t = copy_node (t);
17529 OMP_TASK_CLAUSES (t) = tmp;
17530 add_stmt (t);
17531 break;
17532 }
17533 r = push_omp_privatization_clauses (false);
17534 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17535 complain, in_decl);
17536 stmt = begin_omp_task ();
17537 RECUR (OMP_TASK_BODY (t));
17538 finish_omp_task (tmp, stmt);
17539 pop_omp_privatization_clauses (r);
17540 break;
17541
17542 case OMP_FOR:
17543 case OMP_SIMD:
17544 case OMP_DISTRIBUTE:
17545 case OMP_TASKLOOP:
17546 case OACC_LOOP:
17547 {
17548 tree clauses, body, pre_body;
17549 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17550 tree orig_declv = NULL_TREE;
17551 tree incrv = NULL_TREE;
17552 enum c_omp_region_type ort = C_ORT_OMP;
17553 bool any_range_for = false;
17554 int i;
17555
17556 if (TREE_CODE (t) == OACC_LOOP)
17557 ort = C_ORT_ACC;
17558
17559 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17560 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17561 in_decl);
17562 if (OMP_FOR_INIT (t) != NULL_TREE)
17563 {
17564 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17565 if (OMP_FOR_ORIG_DECLS (t))
17566 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17567 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17568 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17569 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17570 }
17571
17572 keep_next_level (true);
17573 stmt = begin_omp_structured_block ();
17574
17575 pre_body = push_stmt_list ();
17576 RECUR (OMP_FOR_PRE_BODY (t));
17577 pre_body = pop_stmt_list (pre_body);
17578
17579 if (OMP_FOR_INIT (t) != NULL_TREE)
17580 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17581 any_range_for
17582 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17583 condv, incrv, &clauses, args,
17584 complain, in_decl,
17585 integral_constant_expression_p);
17586 omp_parallel_combined_clauses = NULL;
17587
17588 if (any_range_for)
17589 {
17590 gcc_assert (orig_declv);
17591 body = begin_omp_structured_block ();
17592 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17593 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17594 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17595 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17596 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17597 TREE_VEC_ELT (declv, i));
17598 }
17599 else
17600 body = push_stmt_list ();
17601 RECUR (OMP_FOR_BODY (t));
17602 if (any_range_for)
17603 body = finish_omp_structured_block (body);
17604 else
17605 body = pop_stmt_list (body);
17606
17607 if (OMP_FOR_INIT (t) != NULL_TREE)
17608 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17609 orig_declv, initv, condv, incrv, body, pre_body,
17610 NULL, clauses);
17611 else
17612 {
17613 t = make_node (TREE_CODE (t));
17614 TREE_TYPE (t) = void_type_node;
17615 OMP_FOR_BODY (t) = body;
17616 OMP_FOR_PRE_BODY (t) = pre_body;
17617 OMP_FOR_CLAUSES (t) = clauses;
17618 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17619 add_stmt (t);
17620 }
17621
17622 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17623 t));
17624 pop_omp_privatization_clauses (r);
17625 }
17626 break;
17627
17628 case OMP_SECTIONS:
17629 omp_parallel_combined_clauses = NULL;
17630 /* FALLTHRU */
17631 case OMP_SINGLE:
17632 case OMP_TEAMS:
17633 case OMP_CRITICAL:
17634 case OMP_TASKGROUP:
17635 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17636 && OMP_TEAMS_COMBINED (t));
17637 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17638 in_decl);
17639 if (TREE_CODE (t) == OMP_TEAMS)
17640 {
17641 keep_next_level (true);
17642 stmt = begin_omp_structured_block ();
17643 RECUR (OMP_BODY (t));
17644 stmt = finish_omp_structured_block (stmt);
17645 }
17646 else
17647 {
17648 stmt = push_stmt_list ();
17649 RECUR (OMP_BODY (t));
17650 stmt = pop_stmt_list (stmt);
17651 }
17652
17653 t = copy_node (t);
17654 OMP_BODY (t) = stmt;
17655 OMP_CLAUSES (t) = tmp;
17656 add_stmt (t);
17657 pop_omp_privatization_clauses (r);
17658 break;
17659
17660 case OMP_DEPOBJ:
17661 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17662 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17663 {
17664 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17665 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17666 {
17667 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17668 args, complain, in_decl);
17669 if (tmp == NULL_TREE)
17670 tmp = error_mark_node;
17671 }
17672 else
17673 {
17674 kind = (enum omp_clause_depend_kind)
17675 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17676 tmp = NULL_TREE;
17677 }
17678 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17679 }
17680 else
17681 finish_omp_depobj (EXPR_LOCATION (t), r,
17682 OMP_CLAUSE_DEPEND_SOURCE,
17683 OMP_DEPOBJ_CLAUSES (t));
17684 break;
17685
17686 case OACC_DATA:
17687 case OMP_TARGET_DATA:
17688 case OMP_TARGET:
17689 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17690 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17691 in_decl);
17692 keep_next_level (true);
17693 stmt = begin_omp_structured_block ();
17694
17695 RECUR (OMP_BODY (t));
17696 stmt = finish_omp_structured_block (stmt);
17697
17698 t = copy_node (t);
17699 OMP_BODY (t) = stmt;
17700 OMP_CLAUSES (t) = tmp;
17701 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17702 {
17703 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17704 if (teams)
17705 {
17706 /* For combined target teams, ensure the num_teams and
17707 thread_limit clause expressions are evaluated on the host,
17708 before entering the target construct. */
17709 tree c;
17710 for (c = OMP_TEAMS_CLAUSES (teams);
17711 c; c = OMP_CLAUSE_CHAIN (c))
17712 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17713 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17714 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17715 {
17716 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17717 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17718 if (expr == error_mark_node)
17719 continue;
17720 tmp = TARGET_EXPR_SLOT (expr);
17721 add_stmt (expr);
17722 OMP_CLAUSE_OPERAND (c, 0) = expr;
17723 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17724 OMP_CLAUSE_FIRSTPRIVATE);
17725 OMP_CLAUSE_DECL (tc) = tmp;
17726 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17727 OMP_TARGET_CLAUSES (t) = tc;
17728 }
17729 }
17730 }
17731 add_stmt (t);
17732 break;
17733
17734 case OACC_DECLARE:
17735 t = copy_node (t);
17736 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17737 complain, in_decl);
17738 OACC_DECLARE_CLAUSES (t) = tmp;
17739 add_stmt (t);
17740 break;
17741
17742 case OMP_TARGET_UPDATE:
17743 case OMP_TARGET_ENTER_DATA:
17744 case OMP_TARGET_EXIT_DATA:
17745 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17746 complain, in_decl);
17747 t = copy_node (t);
17748 OMP_STANDALONE_CLAUSES (t) = tmp;
17749 add_stmt (t);
17750 break;
17751
17752 case OACC_ENTER_DATA:
17753 case OACC_EXIT_DATA:
17754 case OACC_UPDATE:
17755 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17756 complain, in_decl);
17757 t = copy_node (t);
17758 OMP_STANDALONE_CLAUSES (t) = tmp;
17759 add_stmt (t);
17760 break;
17761
17762 case OMP_ORDERED:
17763 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17764 complain, in_decl);
17765 stmt = push_stmt_list ();
17766 RECUR (OMP_BODY (t));
17767 stmt = pop_stmt_list (stmt);
17768
17769 t = copy_node (t);
17770 OMP_BODY (t) = stmt;
17771 OMP_ORDERED_CLAUSES (t) = tmp;
17772 add_stmt (t);
17773 break;
17774
17775 case OMP_SECTION:
17776 case OMP_MASTER:
17777 stmt = push_stmt_list ();
17778 RECUR (OMP_BODY (t));
17779 stmt = pop_stmt_list (stmt);
17780
17781 t = copy_node (t);
17782 OMP_BODY (t) = stmt;
17783 add_stmt (t);
17784 break;
17785
17786 case OMP_ATOMIC:
17787 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17788 tmp = NULL_TREE;
17789 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17790 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17791 complain, in_decl);
17792 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17793 {
17794 tree op1 = TREE_OPERAND (t, 1);
17795 tree rhs1 = NULL_TREE;
17796 tree lhs, rhs;
17797 if (TREE_CODE (op1) == COMPOUND_EXPR)
17798 {
17799 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17800 op1 = TREE_OPERAND (op1, 1);
17801 }
17802 lhs = RECUR (TREE_OPERAND (op1, 0));
17803 rhs = RECUR (TREE_OPERAND (op1, 1));
17804 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17805 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17806 OMP_ATOMIC_MEMORY_ORDER (t));
17807 }
17808 else
17809 {
17810 tree op1 = TREE_OPERAND (t, 1);
17811 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17812 tree rhs1 = NULL_TREE;
17813 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17814 enum tree_code opcode = NOP_EXPR;
17815 if (code == OMP_ATOMIC_READ)
17816 {
17817 v = RECUR (TREE_OPERAND (op1, 0));
17818 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17819 }
17820 else if (code == OMP_ATOMIC_CAPTURE_OLD
17821 || code == OMP_ATOMIC_CAPTURE_NEW)
17822 {
17823 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17824 v = RECUR (TREE_OPERAND (op1, 0));
17825 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17826 if (TREE_CODE (op11) == COMPOUND_EXPR)
17827 {
17828 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17829 op11 = TREE_OPERAND (op11, 1);
17830 }
17831 lhs = RECUR (TREE_OPERAND (op11, 0));
17832 rhs = RECUR (TREE_OPERAND (op11, 1));
17833 opcode = TREE_CODE (op11);
17834 if (opcode == MODIFY_EXPR)
17835 opcode = NOP_EXPR;
17836 }
17837 else
17838 {
17839 code = OMP_ATOMIC;
17840 lhs = RECUR (TREE_OPERAND (op1, 0));
17841 rhs = RECUR (TREE_OPERAND (op1, 1));
17842 }
17843 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17844 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17845 }
17846 break;
17847
17848 case TRANSACTION_EXPR:
17849 {
17850 int flags = 0;
17851 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17852 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17853
17854 if (TRANSACTION_EXPR_IS_STMT (t))
17855 {
17856 tree body = TRANSACTION_EXPR_BODY (t);
17857 tree noex = NULL_TREE;
17858 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17859 {
17860 noex = MUST_NOT_THROW_COND (body);
17861 if (noex == NULL_TREE)
17862 noex = boolean_true_node;
17863 body = TREE_OPERAND (body, 0);
17864 }
17865 stmt = begin_transaction_stmt (input_location, NULL, flags);
17866 RECUR (body);
17867 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17868 }
17869 else
17870 {
17871 stmt = build_transaction_expr (EXPR_LOCATION (t),
17872 RECUR (TRANSACTION_EXPR_BODY (t)),
17873 flags, NULL_TREE);
17874 RETURN (stmt);
17875 }
17876 }
17877 break;
17878
17879 case MUST_NOT_THROW_EXPR:
17880 {
17881 tree op0 = RECUR (TREE_OPERAND (t, 0));
17882 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17883 RETURN (build_must_not_throw_expr (op0, cond));
17884 }
17885
17886 case EXPR_PACK_EXPANSION:
17887 error ("invalid use of pack expansion expression");
17888 RETURN (error_mark_node);
17889
17890 case NONTYPE_ARGUMENT_PACK:
17891 error ("use %<...%> to expand argument pack");
17892 RETURN (error_mark_node);
17893
17894 case COMPOUND_EXPR:
17895 tmp = RECUR (TREE_OPERAND (t, 0));
17896 if (tmp == NULL_TREE)
17897 /* If the first operand was a statement, we're done with it. */
17898 RETURN (RECUR (TREE_OPERAND (t, 1)));
17899 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17900 RECUR (TREE_OPERAND (t, 1)),
17901 complain));
17902
17903 case ANNOTATE_EXPR:
17904 tmp = RECUR (TREE_OPERAND (t, 0));
17905 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17906 TREE_TYPE (tmp), tmp,
17907 RECUR (TREE_OPERAND (t, 1)),
17908 RECUR (TREE_OPERAND (t, 2))));
17909
17910 case PREDICT_EXPR:
17911 RETURN (add_stmt (copy_node (t)));
17912
17913 default:
17914 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17915
17916 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17917 /*function_p=*/false,
17918 integral_constant_expression_p));
17919 }
17920
17921 RETURN (NULL_TREE);
17922 out:
17923 input_location = loc;
17924 return r;
17925 #undef RECUR
17926 #undef RETURN
17927 }
17928
17929 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17930 function. For description of the body see comment above
17931 cp_parser_omp_declare_reduction_exprs. */
17932
17933 static void
17934 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17935 {
17936 if (t == NULL_TREE || t == error_mark_node)
17937 return;
17938
17939 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17940
17941 tree_stmt_iterator tsi;
17942 int i;
17943 tree stmts[7];
17944 memset (stmts, 0, sizeof stmts);
17945 for (i = 0, tsi = tsi_start (t);
17946 i < 7 && !tsi_end_p (tsi);
17947 i++, tsi_next (&tsi))
17948 stmts[i] = tsi_stmt (tsi);
17949 gcc_assert (tsi_end_p (tsi));
17950
17951 if (i >= 3)
17952 {
17953 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17954 && TREE_CODE (stmts[1]) == DECL_EXPR);
17955 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17956 args, complain, in_decl);
17957 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17958 args, complain, in_decl);
17959 DECL_CONTEXT (omp_out) = current_function_decl;
17960 DECL_CONTEXT (omp_in) = current_function_decl;
17961 keep_next_level (true);
17962 tree block = begin_omp_structured_block ();
17963 tsubst_expr (stmts[2], args, complain, in_decl, false);
17964 block = finish_omp_structured_block (block);
17965 block = maybe_cleanup_point_expr_void (block);
17966 add_decl_expr (omp_out);
17967 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17968 TREE_NO_WARNING (omp_out) = 1;
17969 add_decl_expr (omp_in);
17970 finish_expr_stmt (block);
17971 }
17972 if (i >= 6)
17973 {
17974 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17975 && TREE_CODE (stmts[4]) == DECL_EXPR);
17976 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17977 args, complain, in_decl);
17978 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17979 args, complain, in_decl);
17980 DECL_CONTEXT (omp_priv) = current_function_decl;
17981 DECL_CONTEXT (omp_orig) = current_function_decl;
17982 keep_next_level (true);
17983 tree block = begin_omp_structured_block ();
17984 tsubst_expr (stmts[5], args, complain, in_decl, false);
17985 block = finish_omp_structured_block (block);
17986 block = maybe_cleanup_point_expr_void (block);
17987 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17988 add_decl_expr (omp_priv);
17989 add_decl_expr (omp_orig);
17990 finish_expr_stmt (block);
17991 if (i == 7)
17992 add_decl_expr (omp_orig);
17993 }
17994 }
17995
17996 /* T is a postfix-expression that is not being used in a function
17997 call. Return the substituted version of T. */
17998
17999 static tree
18000 tsubst_non_call_postfix_expression (tree t, tree args,
18001 tsubst_flags_t complain,
18002 tree in_decl)
18003 {
18004 if (TREE_CODE (t) == SCOPE_REF)
18005 t = tsubst_qualified_id (t, args, complain, in_decl,
18006 /*done=*/false, /*address_p=*/false);
18007 else
18008 t = tsubst_copy_and_build (t, args, complain, in_decl,
18009 /*function_p=*/false,
18010 /*integral_constant_expression_p=*/false);
18011
18012 return t;
18013 }
18014
18015 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18016 instantiation context. Instantiating a pack expansion containing a lambda
18017 might result in multiple lambdas all based on the same lambda in the
18018 template. */
18019
18020 tree
18021 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18022 {
18023 tree oldfn = lambda_function (t);
18024 in_decl = oldfn;
18025
18026 /* If we have already specialized this lambda expr, reuse it. See
18027 PR c++/87322. */
18028 if (local_specializations)
18029 if (tree r = retrieve_local_specialization (t))
18030 return r;
18031
18032 tree r = build_lambda_expr ();
18033
18034 if (local_specializations)
18035 register_local_specialization (r, t);
18036
18037 LAMBDA_EXPR_LOCATION (r)
18038 = LAMBDA_EXPR_LOCATION (t);
18039 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18040 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18041 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18042 LAMBDA_EXPR_INSTANTIATED (r) = true;
18043
18044 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18045 /* A lambda in a default argument outside a class gets no
18046 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18047 tsubst_default_argument calls start_lambda_scope, so we need to
18048 specifically ignore it here, and use the global scope. */
18049 record_null_lambda_scope (r);
18050 else
18051 record_lambda_scope (r);
18052
18053 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18054 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18055
18056 vec<tree,va_gc>* field_packs = NULL;
18057
18058 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18059 cap = TREE_CHAIN (cap))
18060 {
18061 tree ofield = TREE_PURPOSE (cap);
18062 if (PACK_EXPANSION_P (ofield))
18063 ofield = PACK_EXPANSION_PATTERN (ofield);
18064 tree field = tsubst_decl (ofield, args, complain);
18065
18066 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18067 {
18068 /* Remember these for when we've pushed local_specializations. */
18069 vec_safe_push (field_packs, ofield);
18070 vec_safe_push (field_packs, field);
18071 }
18072
18073 if (field == error_mark_node)
18074 return error_mark_node;
18075
18076 tree init = TREE_VALUE (cap);
18077 if (PACK_EXPANSION_P (init))
18078 init = tsubst_pack_expansion (init, args, complain, in_decl);
18079 else
18080 init = tsubst_copy_and_build (init, args, complain, in_decl,
18081 /*fn*/false, /*constexpr*/false);
18082
18083 if (TREE_CODE (field) == TREE_VEC)
18084 {
18085 int len = TREE_VEC_LENGTH (field);
18086 gcc_assert (TREE_CODE (init) == TREE_VEC
18087 && TREE_VEC_LENGTH (init) == len);
18088 for (int i = 0; i < len; ++i)
18089 LAMBDA_EXPR_CAPTURE_LIST (r)
18090 = tree_cons (TREE_VEC_ELT (field, i),
18091 TREE_VEC_ELT (init, i),
18092 LAMBDA_EXPR_CAPTURE_LIST (r));
18093 }
18094 else
18095 {
18096 LAMBDA_EXPR_CAPTURE_LIST (r)
18097 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
18098
18099 if (id_equal (DECL_NAME (field), "__this"))
18100 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18101 }
18102 }
18103
18104 tree type = begin_lambda_type (r);
18105 if (type == error_mark_node)
18106 return error_mark_node;
18107
18108 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18109 determine_visibility (TYPE_NAME (type));
18110
18111 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18112
18113 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18114 ? DECL_TI_TEMPLATE (oldfn)
18115 : NULL_TREE);
18116
18117 tree fntype = static_fn_type (oldfn);
18118 if (oldtmpl)
18119 ++processing_template_decl;
18120 fntype = tsubst (fntype, args, complain, in_decl);
18121 if (oldtmpl)
18122 --processing_template_decl;
18123
18124 if (fntype == error_mark_node)
18125 r = error_mark_node;
18126 else
18127 {
18128 /* The body of a lambda-expression is not a subexpression of the
18129 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18130 which would be skipped if cp_unevaluated_operand. */
18131 cp_evaluated ev;
18132
18133 /* Fix the type of 'this'. */
18134 fntype = build_memfn_type (fntype, type,
18135 type_memfn_quals (fntype),
18136 type_memfn_rqual (fntype));
18137 tree fn, tmpl;
18138 if (oldtmpl)
18139 {
18140 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18141 fn = DECL_TEMPLATE_RESULT (tmpl);
18142 finish_member_declaration (tmpl);
18143 }
18144 else
18145 {
18146 tmpl = NULL_TREE;
18147 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18148 finish_member_declaration (fn);
18149 }
18150
18151 /* Let finish_function set this. */
18152 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18153
18154 bool nested = cfun;
18155 if (nested)
18156 push_function_context ();
18157 else
18158 /* Still increment function_depth so that we don't GC in the
18159 middle of an expression. */
18160 ++function_depth;
18161
18162 local_specialization_stack s (lss_copy);
18163
18164 tree body = start_lambda_function (fn, r);
18165
18166 /* Now record them for lookup_init_capture_pack. */
18167 int fplen = vec_safe_length (field_packs);
18168 for (int i = 0; i < fplen; )
18169 {
18170 tree pack = (*field_packs)[i++];
18171 tree inst = (*field_packs)[i++];
18172 register_local_specialization (inst, pack);
18173 }
18174 release_tree_vector (field_packs);
18175
18176 register_parameter_specializations (oldfn, fn);
18177
18178 if (oldtmpl)
18179 {
18180 /* We might not partially instantiate some parts of the function, so
18181 copy these flags from the original template. */
18182 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18183 current_function_returns_value = ol->returns_value;
18184 current_function_returns_null = ol->returns_null;
18185 current_function_returns_abnormally = ol->returns_abnormally;
18186 current_function_infinite_loop = ol->infinite_loop;
18187 }
18188
18189 /* [temp.deduct] A lambda-expression appearing in a function type or a
18190 template parameter is not considered part of the immediate context for
18191 the purposes of template argument deduction. */
18192 complain = tf_warning_or_error;
18193
18194 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18195 /*constexpr*/false);
18196
18197 finish_lambda_function (body);
18198
18199 if (nested)
18200 pop_function_context ();
18201 else
18202 --function_depth;
18203
18204 /* The capture list was built up in reverse order; fix that now. */
18205 LAMBDA_EXPR_CAPTURE_LIST (r)
18206 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18207
18208 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18209
18210 maybe_add_lambda_conv_op (type);
18211 }
18212
18213 finish_struct (type, /*attr*/NULL_TREE);
18214
18215 insert_pending_capture_proxies ();
18216
18217 return r;
18218 }
18219
18220 /* Like tsubst but deals with expressions and performs semantic
18221 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18222
18223 tree
18224 tsubst_copy_and_build (tree t,
18225 tree args,
18226 tsubst_flags_t complain,
18227 tree in_decl,
18228 bool function_p,
18229 bool integral_constant_expression_p)
18230 {
18231 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18232 #define RECUR(NODE) \
18233 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18234 /*function_p=*/false, \
18235 integral_constant_expression_p)
18236
18237 tree retval, op1;
18238 location_t loc;
18239
18240 if (t == NULL_TREE || t == error_mark_node)
18241 return t;
18242
18243 loc = input_location;
18244 if (location_t eloc = cp_expr_location (t))
18245 input_location = eloc;
18246
18247 /* N3276 decltype magic only applies to calls at the top level or on the
18248 right side of a comma. */
18249 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18250 complain &= ~tf_decltype;
18251
18252 switch (TREE_CODE (t))
18253 {
18254 case USING_DECL:
18255 t = DECL_NAME (t);
18256 /* Fall through. */
18257 case IDENTIFIER_NODE:
18258 {
18259 tree decl;
18260 cp_id_kind idk;
18261 bool non_integral_constant_expression_p;
18262 const char *error_msg;
18263
18264 if (IDENTIFIER_CONV_OP_P (t))
18265 {
18266 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18267 t = make_conv_op_name (new_type);
18268 }
18269
18270 /* Look up the name. */
18271 decl = lookup_name (t);
18272
18273 /* By convention, expressions use ERROR_MARK_NODE to indicate
18274 failure, not NULL_TREE. */
18275 if (decl == NULL_TREE)
18276 decl = error_mark_node;
18277
18278 decl = finish_id_expression (t, decl, NULL_TREE,
18279 &idk,
18280 integral_constant_expression_p,
18281 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18282 &non_integral_constant_expression_p,
18283 /*template_p=*/false,
18284 /*done=*/true,
18285 /*address_p=*/false,
18286 /*template_arg_p=*/false,
18287 &error_msg,
18288 input_location);
18289 if (error_msg)
18290 error (error_msg);
18291 if (!function_p && identifier_p (decl))
18292 {
18293 if (complain & tf_error)
18294 unqualified_name_lookup_error (decl);
18295 decl = error_mark_node;
18296 }
18297 RETURN (decl);
18298 }
18299
18300 case TEMPLATE_ID_EXPR:
18301 {
18302 tree object;
18303 tree templ = RECUR (TREE_OPERAND (t, 0));
18304 tree targs = TREE_OPERAND (t, 1);
18305
18306 if (targs)
18307 targs = tsubst_template_args (targs, args, complain, in_decl);
18308 if (targs == error_mark_node)
18309 RETURN (error_mark_node);
18310
18311 if (TREE_CODE (templ) == SCOPE_REF)
18312 {
18313 tree name = TREE_OPERAND (templ, 1);
18314 tree tid = lookup_template_function (name, targs);
18315 TREE_OPERAND (templ, 1) = tid;
18316 RETURN (templ);
18317 }
18318
18319 if (variable_template_p (templ))
18320 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18321
18322 if (TREE_CODE (templ) == COMPONENT_REF)
18323 {
18324 object = TREE_OPERAND (templ, 0);
18325 templ = TREE_OPERAND (templ, 1);
18326 }
18327 else
18328 object = NULL_TREE;
18329 templ = lookup_template_function (templ, targs);
18330
18331 if (object)
18332 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18333 object, templ, NULL_TREE));
18334 else
18335 RETURN (baselink_for_fns (templ));
18336 }
18337
18338 case INDIRECT_REF:
18339 {
18340 tree r = RECUR (TREE_OPERAND (t, 0));
18341
18342 if (REFERENCE_REF_P (t))
18343 {
18344 /* A type conversion to reference type will be enclosed in
18345 such an indirect ref, but the substitution of the cast
18346 will have also added such an indirect ref. */
18347 r = convert_from_reference (r);
18348 }
18349 else
18350 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18351 complain|decltype_flag);
18352
18353 if (REF_PARENTHESIZED_P (t))
18354 r = force_paren_expr (r);
18355
18356 RETURN (r);
18357 }
18358
18359 case NOP_EXPR:
18360 {
18361 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18362 tree op0 = RECUR (TREE_OPERAND (t, 0));
18363 RETURN (build_nop (type, op0));
18364 }
18365
18366 case IMPLICIT_CONV_EXPR:
18367 {
18368 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18369 tree expr = RECUR (TREE_OPERAND (t, 0));
18370 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18371 {
18372 retval = copy_node (t);
18373 TREE_TYPE (retval) = type;
18374 TREE_OPERAND (retval, 0) = expr;
18375 RETURN (retval);
18376 }
18377 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18378 /* We'll pass this to convert_nontype_argument again, we don't need
18379 to actually perform any conversion here. */
18380 RETURN (expr);
18381 int flags = LOOKUP_IMPLICIT;
18382 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18383 flags = LOOKUP_NORMAL;
18384 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18385 flags |= LOOKUP_NO_NARROWING;
18386 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18387 flags));
18388 }
18389
18390 case CONVERT_EXPR:
18391 {
18392 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18393 tree op0 = RECUR (TREE_OPERAND (t, 0));
18394 if (op0 == error_mark_node)
18395 RETURN (error_mark_node);
18396 RETURN (build1 (CONVERT_EXPR, type, op0));
18397 }
18398
18399 case CAST_EXPR:
18400 case REINTERPRET_CAST_EXPR:
18401 case CONST_CAST_EXPR:
18402 case DYNAMIC_CAST_EXPR:
18403 case STATIC_CAST_EXPR:
18404 {
18405 tree type;
18406 tree op, r = NULL_TREE;
18407
18408 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18409 if (integral_constant_expression_p
18410 && !cast_valid_in_integral_constant_expression_p (type))
18411 {
18412 if (complain & tf_error)
18413 error ("a cast to a type other than an integral or "
18414 "enumeration type cannot appear in a constant-expression");
18415 RETURN (error_mark_node);
18416 }
18417
18418 op = RECUR (TREE_OPERAND (t, 0));
18419
18420 warning_sentinel s(warn_useless_cast);
18421 warning_sentinel s2(warn_ignored_qualifiers);
18422 switch (TREE_CODE (t))
18423 {
18424 case CAST_EXPR:
18425 r = build_functional_cast (type, op, complain);
18426 break;
18427 case REINTERPRET_CAST_EXPR:
18428 r = build_reinterpret_cast (type, op, complain);
18429 break;
18430 case CONST_CAST_EXPR:
18431 r = build_const_cast (type, op, complain);
18432 break;
18433 case DYNAMIC_CAST_EXPR:
18434 r = build_dynamic_cast (type, op, complain);
18435 break;
18436 case STATIC_CAST_EXPR:
18437 r = build_static_cast (type, op, complain);
18438 break;
18439 default:
18440 gcc_unreachable ();
18441 }
18442
18443 RETURN (r);
18444 }
18445
18446 case POSTDECREMENT_EXPR:
18447 case POSTINCREMENT_EXPR:
18448 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18449 args, complain, in_decl);
18450 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18451 complain|decltype_flag));
18452
18453 case PREDECREMENT_EXPR:
18454 case PREINCREMENT_EXPR:
18455 case NEGATE_EXPR:
18456 case BIT_NOT_EXPR:
18457 case ABS_EXPR:
18458 case TRUTH_NOT_EXPR:
18459 case UNARY_PLUS_EXPR: /* Unary + */
18460 case REALPART_EXPR:
18461 case IMAGPART_EXPR:
18462 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18463 RECUR (TREE_OPERAND (t, 0)),
18464 complain|decltype_flag));
18465
18466 case FIX_TRUNC_EXPR:
18467 gcc_unreachable ();
18468
18469 case ADDR_EXPR:
18470 op1 = TREE_OPERAND (t, 0);
18471 if (TREE_CODE (op1) == LABEL_DECL)
18472 RETURN (finish_label_address_expr (DECL_NAME (op1),
18473 EXPR_LOCATION (op1)));
18474 if (TREE_CODE (op1) == SCOPE_REF)
18475 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18476 /*done=*/true, /*address_p=*/true);
18477 else
18478 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18479 in_decl);
18480 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18481 complain|decltype_flag));
18482
18483 case PLUS_EXPR:
18484 case MINUS_EXPR:
18485 case MULT_EXPR:
18486 case TRUNC_DIV_EXPR:
18487 case CEIL_DIV_EXPR:
18488 case FLOOR_DIV_EXPR:
18489 case ROUND_DIV_EXPR:
18490 case EXACT_DIV_EXPR:
18491 case BIT_AND_EXPR:
18492 case BIT_IOR_EXPR:
18493 case BIT_XOR_EXPR:
18494 case TRUNC_MOD_EXPR:
18495 case FLOOR_MOD_EXPR:
18496 case TRUTH_ANDIF_EXPR:
18497 case TRUTH_ORIF_EXPR:
18498 case TRUTH_AND_EXPR:
18499 case TRUTH_OR_EXPR:
18500 case RSHIFT_EXPR:
18501 case LSHIFT_EXPR:
18502 case RROTATE_EXPR:
18503 case LROTATE_EXPR:
18504 case EQ_EXPR:
18505 case NE_EXPR:
18506 case MAX_EXPR:
18507 case MIN_EXPR:
18508 case LE_EXPR:
18509 case GE_EXPR:
18510 case LT_EXPR:
18511 case GT_EXPR:
18512 case MEMBER_REF:
18513 case DOTSTAR_EXPR:
18514 {
18515 warning_sentinel s1(warn_type_limits);
18516 warning_sentinel s2(warn_div_by_zero);
18517 warning_sentinel s3(warn_logical_op);
18518 warning_sentinel s4(warn_tautological_compare);
18519 tree op0 = RECUR (TREE_OPERAND (t, 0));
18520 tree op1 = RECUR (TREE_OPERAND (t, 1));
18521 tree r = build_x_binary_op
18522 (input_location, TREE_CODE (t),
18523 op0,
18524 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18525 ? ERROR_MARK
18526 : TREE_CODE (TREE_OPERAND (t, 0))),
18527 op1,
18528 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18529 ? ERROR_MARK
18530 : TREE_CODE (TREE_OPERAND (t, 1))),
18531 /*overload=*/NULL,
18532 complain|decltype_flag);
18533 if (EXPR_P (r) && TREE_NO_WARNING (t))
18534 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18535
18536 RETURN (r);
18537 }
18538
18539 case POINTER_PLUS_EXPR:
18540 {
18541 tree op0 = RECUR (TREE_OPERAND (t, 0));
18542 tree op1 = RECUR (TREE_OPERAND (t, 1));
18543 RETURN (fold_build_pointer_plus (op0, op1));
18544 }
18545
18546 case SCOPE_REF:
18547 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18548 /*address_p=*/false));
18549 case ARRAY_REF:
18550 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18551 args, complain, in_decl);
18552 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18553 RECUR (TREE_OPERAND (t, 1)),
18554 complain|decltype_flag));
18555
18556 case SIZEOF_EXPR:
18557 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18558 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18559 RETURN (tsubst_copy (t, args, complain, in_decl));
18560 /* Fall through */
18561
18562 case ALIGNOF_EXPR:
18563 {
18564 tree r;
18565
18566 op1 = TREE_OPERAND (t, 0);
18567 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18568 op1 = TREE_TYPE (op1);
18569 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18570 && ALIGNOF_EXPR_STD_P (t));
18571 if (!args)
18572 {
18573 /* When there are no ARGS, we are trying to evaluate a
18574 non-dependent expression from the parser. Trying to do
18575 the substitutions may not work. */
18576 if (!TYPE_P (op1))
18577 op1 = TREE_TYPE (op1);
18578 }
18579 else
18580 {
18581 ++cp_unevaluated_operand;
18582 ++c_inhibit_evaluation_warnings;
18583 if (TYPE_P (op1))
18584 op1 = tsubst (op1, args, complain, in_decl);
18585 else
18586 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18587 /*function_p=*/false,
18588 /*integral_constant_expression_p=*/
18589 false);
18590 --cp_unevaluated_operand;
18591 --c_inhibit_evaluation_warnings;
18592 }
18593 if (TYPE_P (op1))
18594 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18595 complain & tf_error);
18596 else
18597 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18598 complain & tf_error);
18599 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18600 {
18601 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18602 {
18603 if (!processing_template_decl && TYPE_P (op1))
18604 {
18605 r = build_min (SIZEOF_EXPR, size_type_node,
18606 build1 (NOP_EXPR, op1, error_mark_node));
18607 SIZEOF_EXPR_TYPE_P (r) = 1;
18608 }
18609 else
18610 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18611 TREE_SIDE_EFFECTS (r) = 0;
18612 TREE_READONLY (r) = 1;
18613 }
18614 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18615 }
18616 RETURN (r);
18617 }
18618
18619 case AT_ENCODE_EXPR:
18620 {
18621 op1 = TREE_OPERAND (t, 0);
18622 ++cp_unevaluated_operand;
18623 ++c_inhibit_evaluation_warnings;
18624 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18625 /*function_p=*/false,
18626 /*integral_constant_expression_p=*/false);
18627 --cp_unevaluated_operand;
18628 --c_inhibit_evaluation_warnings;
18629 RETURN (objc_build_encode_expr (op1));
18630 }
18631
18632 case NOEXCEPT_EXPR:
18633 op1 = TREE_OPERAND (t, 0);
18634 ++cp_unevaluated_operand;
18635 ++c_inhibit_evaluation_warnings;
18636 ++cp_noexcept_operand;
18637 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18638 /*function_p=*/false,
18639 /*integral_constant_expression_p=*/false);
18640 --cp_unevaluated_operand;
18641 --c_inhibit_evaluation_warnings;
18642 --cp_noexcept_operand;
18643 RETURN (finish_noexcept_expr (op1, complain));
18644
18645 case MODOP_EXPR:
18646 {
18647 warning_sentinel s(warn_div_by_zero);
18648 tree lhs = RECUR (TREE_OPERAND (t, 0));
18649 tree rhs = RECUR (TREE_OPERAND (t, 2));
18650 tree r = build_x_modify_expr
18651 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18652 complain|decltype_flag);
18653 /* TREE_NO_WARNING must be set if either the expression was
18654 parenthesized or it uses an operator such as >>= rather
18655 than plain assignment. In the former case, it was already
18656 set and must be copied. In the latter case,
18657 build_x_modify_expr sets it and it must not be reset
18658 here. */
18659 if (TREE_NO_WARNING (t))
18660 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18661
18662 RETURN (r);
18663 }
18664
18665 case ARROW_EXPR:
18666 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18667 args, complain, in_decl);
18668 /* Remember that there was a reference to this entity. */
18669 if (DECL_P (op1)
18670 && !mark_used (op1, complain) && !(complain & tf_error))
18671 RETURN (error_mark_node);
18672 RETURN (build_x_arrow (input_location, op1, complain));
18673
18674 case NEW_EXPR:
18675 {
18676 tree placement = RECUR (TREE_OPERAND (t, 0));
18677 tree init = RECUR (TREE_OPERAND (t, 3));
18678 vec<tree, va_gc> *placement_vec;
18679 vec<tree, va_gc> *init_vec;
18680 tree ret;
18681
18682 if (placement == NULL_TREE)
18683 placement_vec = NULL;
18684 else
18685 {
18686 placement_vec = make_tree_vector ();
18687 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18688 vec_safe_push (placement_vec, TREE_VALUE (placement));
18689 }
18690
18691 /* If there was an initializer in the original tree, but it
18692 instantiated to an empty list, then we should pass a
18693 non-NULL empty vector to tell build_new that it was an
18694 empty initializer() rather than no initializer. This can
18695 only happen when the initializer is a pack expansion whose
18696 parameter packs are of length zero. */
18697 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18698 init_vec = NULL;
18699 else
18700 {
18701 init_vec = make_tree_vector ();
18702 if (init == void_node)
18703 gcc_assert (init_vec != NULL);
18704 else
18705 {
18706 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18707 vec_safe_push (init_vec, TREE_VALUE (init));
18708 }
18709 }
18710
18711 /* Avoid passing an enclosing decl to valid_array_size_p. */
18712 in_decl = NULL_TREE;
18713
18714 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18715 tree op2 = RECUR (TREE_OPERAND (t, 2));
18716 ret = build_new (&placement_vec, op1, op2, &init_vec,
18717 NEW_EXPR_USE_GLOBAL (t),
18718 complain);
18719
18720 if (placement_vec != NULL)
18721 release_tree_vector (placement_vec);
18722 if (init_vec != NULL)
18723 release_tree_vector (init_vec);
18724
18725 RETURN (ret);
18726 }
18727
18728 case DELETE_EXPR:
18729 {
18730 tree op0 = RECUR (TREE_OPERAND (t, 0));
18731 tree op1 = RECUR (TREE_OPERAND (t, 1));
18732 RETURN (delete_sanity (op0, op1,
18733 DELETE_EXPR_USE_VEC (t),
18734 DELETE_EXPR_USE_GLOBAL (t),
18735 complain));
18736 }
18737
18738 case COMPOUND_EXPR:
18739 {
18740 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18741 complain & ~tf_decltype, in_decl,
18742 /*function_p=*/false,
18743 integral_constant_expression_p);
18744 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18745 op0,
18746 RECUR (TREE_OPERAND (t, 1)),
18747 complain|decltype_flag));
18748 }
18749
18750 case CALL_EXPR:
18751 {
18752 tree function;
18753 unsigned int nargs, i;
18754 bool qualified_p;
18755 bool koenig_p;
18756 tree ret;
18757
18758 function = CALL_EXPR_FN (t);
18759 /* Internal function with no arguments. */
18760 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18761 RETURN (t);
18762
18763 /* When we parsed the expression, we determined whether or
18764 not Koenig lookup should be performed. */
18765 koenig_p = KOENIG_LOOKUP_P (t);
18766 if (function == NULL_TREE)
18767 {
18768 koenig_p = false;
18769 qualified_p = false;
18770 }
18771 else if (TREE_CODE (function) == SCOPE_REF)
18772 {
18773 qualified_p = true;
18774 function = tsubst_qualified_id (function, args, complain, in_decl,
18775 /*done=*/false,
18776 /*address_p=*/false);
18777 }
18778 else if (koenig_p && identifier_p (function))
18779 {
18780 /* Do nothing; calling tsubst_copy_and_build on an identifier
18781 would incorrectly perform unqualified lookup again.
18782
18783 Note that we can also have an IDENTIFIER_NODE if the earlier
18784 unqualified lookup found a member function; in that case
18785 koenig_p will be false and we do want to do the lookup
18786 again to find the instantiated member function.
18787
18788 FIXME but doing that causes c++/15272, so we need to stop
18789 using IDENTIFIER_NODE in that situation. */
18790 qualified_p = false;
18791 }
18792 else
18793 {
18794 if (TREE_CODE (function) == COMPONENT_REF)
18795 {
18796 tree op = TREE_OPERAND (function, 1);
18797
18798 qualified_p = (TREE_CODE (op) == SCOPE_REF
18799 || (BASELINK_P (op)
18800 && BASELINK_QUALIFIED_P (op)));
18801 }
18802 else
18803 qualified_p = false;
18804
18805 if (TREE_CODE (function) == ADDR_EXPR
18806 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18807 /* Avoid error about taking the address of a constructor. */
18808 function = TREE_OPERAND (function, 0);
18809
18810 function = tsubst_copy_and_build (function, args, complain,
18811 in_decl,
18812 !qualified_p,
18813 integral_constant_expression_p);
18814
18815 if (BASELINK_P (function))
18816 qualified_p = true;
18817 }
18818
18819 nargs = call_expr_nargs (t);
18820 releasing_vec call_args;
18821 for (i = 0; i < nargs; ++i)
18822 {
18823 tree arg = CALL_EXPR_ARG (t, i);
18824
18825 if (!PACK_EXPANSION_P (arg))
18826 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18827 else
18828 {
18829 /* Expand the pack expansion and push each entry onto
18830 CALL_ARGS. */
18831 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18832 if (TREE_CODE (arg) == TREE_VEC)
18833 {
18834 unsigned int len, j;
18835
18836 len = TREE_VEC_LENGTH (arg);
18837 for (j = 0; j < len; ++j)
18838 {
18839 tree value = TREE_VEC_ELT (arg, j);
18840 if (value != NULL_TREE)
18841 value = convert_from_reference (value);
18842 vec_safe_push (call_args, value);
18843 }
18844 }
18845 else
18846 {
18847 /* A partial substitution. Add one entry. */
18848 vec_safe_push (call_args, arg);
18849 }
18850 }
18851 }
18852
18853 /* Stripped-down processing for a call in a thunk. Specifically, in
18854 the thunk template for a generic lambda. */
18855 if (CALL_FROM_THUNK_P (t))
18856 {
18857 tree thisarg = NULL_TREE;
18858 if (TREE_CODE (function) == COMPONENT_REF)
18859 {
18860 thisarg = TREE_OPERAND (function, 0);
18861 if (TREE_CODE (thisarg) == INDIRECT_REF)
18862 thisarg = TREE_OPERAND (thisarg, 0);
18863 function = TREE_OPERAND (function, 1);
18864 if (TREE_CODE (function) == BASELINK)
18865 function = BASELINK_FUNCTIONS (function);
18866 }
18867 /* We aren't going to do normal overload resolution, so force the
18868 template-id to resolve. */
18869 function = resolve_nondeduced_context (function, complain);
18870 for (unsigned i = 0; i < nargs; ++i)
18871 {
18872 /* In a thunk, pass through args directly, without any
18873 conversions. */
18874 tree arg = (*call_args)[i];
18875 while (TREE_CODE (arg) != PARM_DECL)
18876 arg = TREE_OPERAND (arg, 0);
18877 (*call_args)[i] = arg;
18878 }
18879 if (thisarg)
18880 {
18881 /* Shift the other args over to make room. */
18882 tree last = (*call_args)[nargs - 1];
18883 vec_safe_push (call_args, last);
18884 for (int i = nargs-1; i > 0; --i)
18885 (*call_args)[i] = (*call_args)[i-1];
18886 (*call_args)[0] = thisarg;
18887 }
18888 ret = build_call_a (function, call_args->length (),
18889 call_args->address ());
18890 /* The thunk location is not interesting. */
18891 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18892 CALL_FROM_THUNK_P (ret) = true;
18893 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18894 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18895
18896 RETURN (ret);
18897 }
18898
18899 /* We do not perform argument-dependent lookup if normal
18900 lookup finds a non-function, in accordance with the
18901 expected resolution of DR 218. */
18902 if (koenig_p
18903 && ((is_overloaded_fn (function)
18904 /* If lookup found a member function, the Koenig lookup is
18905 not appropriate, even if an unqualified-name was used
18906 to denote the function. */
18907 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18908 || identifier_p (function))
18909 /* Only do this when substitution turns a dependent call
18910 into a non-dependent call. */
18911 && type_dependent_expression_p_push (t)
18912 && !any_type_dependent_arguments_p (call_args))
18913 function = perform_koenig_lookup (function, call_args, tf_none);
18914
18915 if (function != NULL_TREE
18916 && identifier_p (function)
18917 && !any_type_dependent_arguments_p (call_args))
18918 {
18919 if (koenig_p && (complain & tf_warning_or_error))
18920 {
18921 /* For backwards compatibility and good diagnostics, try
18922 the unqualified lookup again if we aren't in SFINAE
18923 context. */
18924 tree unq = (tsubst_copy_and_build
18925 (function, args, complain, in_decl, true,
18926 integral_constant_expression_p));
18927 if (unq == error_mark_node)
18928 RETURN (error_mark_node);
18929
18930 if (unq != function)
18931 {
18932 /* In a lambda fn, we have to be careful to not
18933 introduce new this captures. Legacy code can't
18934 be using lambdas anyway, so it's ok to be
18935 stricter. */
18936 bool in_lambda = (current_class_type
18937 && LAMBDA_TYPE_P (current_class_type));
18938 char const *const msg
18939 = G_("%qD was not declared in this scope, "
18940 "and no declarations were found by "
18941 "argument-dependent lookup at the point "
18942 "of instantiation");
18943
18944 bool diag = true;
18945 if (in_lambda)
18946 error_at (cp_expr_loc_or_loc (t, input_location),
18947 msg, function);
18948 else
18949 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18950 msg, function);
18951 if (diag)
18952 {
18953 tree fn = unq;
18954
18955 if (INDIRECT_REF_P (fn))
18956 fn = TREE_OPERAND (fn, 0);
18957 if (is_overloaded_fn (fn))
18958 fn = get_first_fn (fn);
18959
18960 if (!DECL_P (fn))
18961 /* Can't say anything more. */;
18962 else if (DECL_CLASS_SCOPE_P (fn))
18963 {
18964 location_t loc = cp_expr_loc_or_loc (t,
18965 input_location);
18966 inform (loc,
18967 "declarations in dependent base %qT are "
18968 "not found by unqualified lookup",
18969 DECL_CLASS_CONTEXT (fn));
18970 if (current_class_ptr)
18971 inform (loc,
18972 "use %<this->%D%> instead", function);
18973 else
18974 inform (loc,
18975 "use %<%T::%D%> instead",
18976 current_class_name, function);
18977 }
18978 else
18979 inform (DECL_SOURCE_LOCATION (fn),
18980 "%qD declared here, later in the "
18981 "translation unit", fn);
18982 if (in_lambda)
18983 RETURN (error_mark_node);
18984 }
18985
18986 function = unq;
18987 }
18988 }
18989 if (identifier_p (function))
18990 {
18991 if (complain & tf_error)
18992 unqualified_name_lookup_error (function);
18993 RETURN (error_mark_node);
18994 }
18995 }
18996
18997 /* Remember that there was a reference to this entity. */
18998 if (function != NULL_TREE
18999 && DECL_P (function)
19000 && !mark_used (function, complain) && !(complain & tf_error))
19001 RETURN (error_mark_node);
19002
19003 /* Put back tf_decltype for the actual call. */
19004 complain |= decltype_flag;
19005
19006 if (function == NULL_TREE)
19007 switch (CALL_EXPR_IFN (t))
19008 {
19009 case IFN_LAUNDER:
19010 gcc_assert (nargs == 1);
19011 if (vec_safe_length (call_args) != 1)
19012 {
19013 error_at (cp_expr_loc_or_loc (t, input_location),
19014 "wrong number of arguments to "
19015 "%<__builtin_launder%>");
19016 ret = error_mark_node;
19017 }
19018 else
19019 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
19020 input_location),
19021 (*call_args)[0], complain);
19022 break;
19023
19024 case IFN_VEC_CONVERT:
19025 gcc_assert (nargs == 1);
19026 if (vec_safe_length (call_args) != 1)
19027 {
19028 error_at (cp_expr_loc_or_loc (t, input_location),
19029 "wrong number of arguments to "
19030 "%<__builtin_convertvector%>");
19031 ret = error_mark_node;
19032 break;
19033 }
19034 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19035 tsubst (TREE_TYPE (t), args,
19036 complain, in_decl),
19037 complain);
19038 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19039 RETURN (ret);
19040 break;
19041
19042 default:
19043 /* Unsupported internal function with arguments. */
19044 gcc_unreachable ();
19045 }
19046 else if (TREE_CODE (function) == OFFSET_REF
19047 || TREE_CODE (function) == DOTSTAR_EXPR
19048 || TREE_CODE (function) == MEMBER_REF)
19049 ret = build_offset_ref_call_from_tree (function, &call_args,
19050 complain);
19051 else if (TREE_CODE (function) == COMPONENT_REF)
19052 {
19053 tree instance = TREE_OPERAND (function, 0);
19054 tree fn = TREE_OPERAND (function, 1);
19055
19056 if (processing_template_decl
19057 && (type_dependent_expression_p (instance)
19058 || (!BASELINK_P (fn)
19059 && TREE_CODE (fn) != FIELD_DECL)
19060 || type_dependent_expression_p (fn)
19061 || any_type_dependent_arguments_p (call_args)))
19062 ret = build_min_nt_call_vec (function, call_args);
19063 else if (!BASELINK_P (fn))
19064 ret = finish_call_expr (function, &call_args,
19065 /*disallow_virtual=*/false,
19066 /*koenig_p=*/false,
19067 complain);
19068 else
19069 ret = (build_new_method_call
19070 (instance, fn,
19071 &call_args, NULL_TREE,
19072 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19073 /*fn_p=*/NULL,
19074 complain));
19075 }
19076 else
19077 ret = finish_call_expr (function, &call_args,
19078 /*disallow_virtual=*/qualified_p,
19079 koenig_p,
19080 complain);
19081
19082 if (ret != error_mark_node)
19083 {
19084 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19085 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19086 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19087 if (op || ord || rev)
19088 {
19089 function = extract_call_expr (ret);
19090 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19091 CALL_EXPR_ORDERED_ARGS (function) = ord;
19092 CALL_EXPR_REVERSE_ARGS (function) = rev;
19093 }
19094 }
19095
19096 RETURN (ret);
19097 }
19098
19099 case COND_EXPR:
19100 {
19101 tree cond = RECUR (TREE_OPERAND (t, 0));
19102 cond = mark_rvalue_use (cond);
19103 tree folded_cond = fold_non_dependent_expr (cond, complain);
19104 tree exp1, exp2;
19105
19106 if (TREE_CODE (folded_cond) == INTEGER_CST)
19107 {
19108 if (integer_zerop (folded_cond))
19109 {
19110 ++c_inhibit_evaluation_warnings;
19111 exp1 = RECUR (TREE_OPERAND (t, 1));
19112 --c_inhibit_evaluation_warnings;
19113 exp2 = RECUR (TREE_OPERAND (t, 2));
19114 }
19115 else
19116 {
19117 exp1 = RECUR (TREE_OPERAND (t, 1));
19118 ++c_inhibit_evaluation_warnings;
19119 exp2 = RECUR (TREE_OPERAND (t, 2));
19120 --c_inhibit_evaluation_warnings;
19121 }
19122 cond = folded_cond;
19123 }
19124 else
19125 {
19126 exp1 = RECUR (TREE_OPERAND (t, 1));
19127 exp2 = RECUR (TREE_OPERAND (t, 2));
19128 }
19129
19130 warning_sentinel s(warn_duplicated_branches);
19131 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19132 cond, exp1, exp2, complain));
19133 }
19134
19135 case PSEUDO_DTOR_EXPR:
19136 {
19137 tree op0 = RECUR (TREE_OPERAND (t, 0));
19138 tree op1 = RECUR (TREE_OPERAND (t, 1));
19139 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19140 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19141 input_location));
19142 }
19143
19144 case TREE_LIST:
19145 {
19146 tree purpose, value, chain;
19147
19148 if (t == void_list_node)
19149 RETURN (t);
19150
19151 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19152 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19153 {
19154 /* We have pack expansions, so expand those and
19155 create a new list out of it. */
19156 tree purposevec = NULL_TREE;
19157 tree valuevec = NULL_TREE;
19158 tree chain;
19159 int i, len = -1;
19160
19161 /* Expand the argument expressions. */
19162 if (TREE_PURPOSE (t))
19163 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19164 complain, in_decl);
19165 if (TREE_VALUE (t))
19166 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19167 complain, in_decl);
19168
19169 /* Build the rest of the list. */
19170 chain = TREE_CHAIN (t);
19171 if (chain && chain != void_type_node)
19172 chain = RECUR (chain);
19173
19174 /* Determine the number of arguments. */
19175 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19176 {
19177 len = TREE_VEC_LENGTH (purposevec);
19178 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19179 }
19180 else if (TREE_CODE (valuevec) == TREE_VEC)
19181 len = TREE_VEC_LENGTH (valuevec);
19182 else
19183 {
19184 /* Since we only performed a partial substitution into
19185 the argument pack, we only RETURN (a single list
19186 node. */
19187 if (purposevec == TREE_PURPOSE (t)
19188 && valuevec == TREE_VALUE (t)
19189 && chain == TREE_CHAIN (t))
19190 RETURN (t);
19191
19192 RETURN (tree_cons (purposevec, valuevec, chain));
19193 }
19194
19195 /* Convert the argument vectors into a TREE_LIST */
19196 i = len;
19197 while (i > 0)
19198 {
19199 /* Grab the Ith values. */
19200 i--;
19201 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19202 : NULL_TREE;
19203 value
19204 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19205 : NULL_TREE;
19206
19207 /* Build the list (backwards). */
19208 chain = tree_cons (purpose, value, chain);
19209 }
19210
19211 RETURN (chain);
19212 }
19213
19214 purpose = TREE_PURPOSE (t);
19215 if (purpose)
19216 purpose = RECUR (purpose);
19217 value = TREE_VALUE (t);
19218 if (value)
19219 value = RECUR (value);
19220 chain = TREE_CHAIN (t);
19221 if (chain && chain != void_type_node)
19222 chain = RECUR (chain);
19223 if (purpose == TREE_PURPOSE (t)
19224 && value == TREE_VALUE (t)
19225 && chain == TREE_CHAIN (t))
19226 RETURN (t);
19227 RETURN (tree_cons (purpose, value, chain));
19228 }
19229
19230 case COMPONENT_REF:
19231 {
19232 tree object;
19233 tree object_type;
19234 tree member;
19235 tree r;
19236
19237 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19238 args, complain, in_decl);
19239 /* Remember that there was a reference to this entity. */
19240 if (DECL_P (object)
19241 && !mark_used (object, complain) && !(complain & tf_error))
19242 RETURN (error_mark_node);
19243 object_type = TREE_TYPE (object);
19244
19245 member = TREE_OPERAND (t, 1);
19246 if (BASELINK_P (member))
19247 member = tsubst_baselink (member,
19248 non_reference (TREE_TYPE (object)),
19249 args, complain, in_decl);
19250 else
19251 member = tsubst_copy (member, args, complain, in_decl);
19252 if (member == error_mark_node)
19253 RETURN (error_mark_node);
19254
19255 if (TREE_CODE (member) == FIELD_DECL)
19256 {
19257 r = finish_non_static_data_member (member, object, NULL_TREE);
19258 if (TREE_CODE (r) == COMPONENT_REF)
19259 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19260 RETURN (r);
19261 }
19262 else if (type_dependent_expression_p (object))
19263 /* We can't do much here. */;
19264 else if (!CLASS_TYPE_P (object_type))
19265 {
19266 if (scalarish_type_p (object_type))
19267 {
19268 tree s = NULL_TREE;
19269 tree dtor = member;
19270
19271 if (TREE_CODE (dtor) == SCOPE_REF)
19272 {
19273 s = TREE_OPERAND (dtor, 0);
19274 dtor = TREE_OPERAND (dtor, 1);
19275 }
19276 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19277 {
19278 dtor = TREE_OPERAND (dtor, 0);
19279 if (TYPE_P (dtor))
19280 RETURN (finish_pseudo_destructor_expr
19281 (object, s, dtor, input_location));
19282 }
19283 }
19284 }
19285 else if (TREE_CODE (member) == SCOPE_REF
19286 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19287 {
19288 /* Lookup the template functions now that we know what the
19289 scope is. */
19290 tree scope = TREE_OPERAND (member, 0);
19291 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19292 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19293 member = lookup_qualified_name (scope, tmpl,
19294 /*is_type_p=*/false,
19295 /*complain=*/false);
19296 if (BASELINK_P (member))
19297 {
19298 BASELINK_FUNCTIONS (member)
19299 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19300 args);
19301 member = (adjust_result_of_qualified_name_lookup
19302 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19303 object_type));
19304 }
19305 else
19306 {
19307 qualified_name_lookup_error (scope, tmpl, member,
19308 input_location);
19309 RETURN (error_mark_node);
19310 }
19311 }
19312 else if (TREE_CODE (member) == SCOPE_REF
19313 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19314 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19315 {
19316 if (complain & tf_error)
19317 {
19318 if (TYPE_P (TREE_OPERAND (member, 0)))
19319 error ("%qT is not a class or namespace",
19320 TREE_OPERAND (member, 0));
19321 else
19322 error ("%qD is not a class or namespace",
19323 TREE_OPERAND (member, 0));
19324 }
19325 RETURN (error_mark_node);
19326 }
19327
19328 r = finish_class_member_access_expr (object, member,
19329 /*template_p=*/false,
19330 complain);
19331 if (TREE_CODE (r) == COMPONENT_REF)
19332 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19333 RETURN (r);
19334 }
19335
19336 case THROW_EXPR:
19337 RETURN (build_throw
19338 (RECUR (TREE_OPERAND (t, 0))));
19339
19340 case CONSTRUCTOR:
19341 {
19342 vec<constructor_elt, va_gc> *n;
19343 constructor_elt *ce;
19344 unsigned HOST_WIDE_INT idx;
19345 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19346 bool process_index_p;
19347 int newlen;
19348 bool need_copy_p = false;
19349 tree r;
19350
19351 if (type == error_mark_node)
19352 RETURN (error_mark_node);
19353
19354 /* We do not want to process the index of aggregate
19355 initializers as they are identifier nodes which will be
19356 looked up by digest_init. */
19357 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19358
19359 if (null_member_pointer_value_p (t))
19360 {
19361 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19362 RETURN (t);
19363 }
19364
19365 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19366 newlen = vec_safe_length (n);
19367 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19368 {
19369 if (ce->index && process_index_p
19370 /* An identifier index is looked up in the type
19371 being initialized, not the current scope. */
19372 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19373 ce->index = RECUR (ce->index);
19374
19375 if (PACK_EXPANSION_P (ce->value))
19376 {
19377 /* Substitute into the pack expansion. */
19378 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19379 in_decl);
19380
19381 if (ce->value == error_mark_node
19382 || PACK_EXPANSION_P (ce->value))
19383 ;
19384 else if (TREE_VEC_LENGTH (ce->value) == 1)
19385 /* Just move the argument into place. */
19386 ce->value = TREE_VEC_ELT (ce->value, 0);
19387 else
19388 {
19389 /* Update the length of the final CONSTRUCTOR
19390 arguments vector, and note that we will need to
19391 copy.*/
19392 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19393 need_copy_p = true;
19394 }
19395 }
19396 else
19397 ce->value = RECUR (ce->value);
19398 }
19399
19400 if (need_copy_p)
19401 {
19402 vec<constructor_elt, va_gc> *old_n = n;
19403
19404 vec_alloc (n, newlen);
19405 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19406 {
19407 if (TREE_CODE (ce->value) == TREE_VEC)
19408 {
19409 int i, len = TREE_VEC_LENGTH (ce->value);
19410 for (i = 0; i < len; ++i)
19411 CONSTRUCTOR_APPEND_ELT (n, 0,
19412 TREE_VEC_ELT (ce->value, i));
19413 }
19414 else
19415 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19416 }
19417 }
19418
19419 r = build_constructor (init_list_type_node, n);
19420 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19421 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19422 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19423
19424 if (TREE_HAS_CONSTRUCTOR (t))
19425 {
19426 fcl_t cl = fcl_functional;
19427 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19428 cl = fcl_c99;
19429 RETURN (finish_compound_literal (type, r, complain, cl));
19430 }
19431
19432 TREE_TYPE (r) = type;
19433 RETURN (r);
19434 }
19435
19436 case TYPEID_EXPR:
19437 {
19438 tree operand_0 = TREE_OPERAND (t, 0);
19439 if (TYPE_P (operand_0))
19440 {
19441 operand_0 = tsubst (operand_0, args, complain, in_decl);
19442 RETURN (get_typeid (operand_0, complain));
19443 }
19444 else
19445 {
19446 operand_0 = RECUR (operand_0);
19447 RETURN (build_typeid (operand_0, complain));
19448 }
19449 }
19450
19451 case VAR_DECL:
19452 if (!args)
19453 RETURN (t);
19454 /* Fall through */
19455
19456 case PARM_DECL:
19457 {
19458 tree r = tsubst_copy (t, args, complain, in_decl);
19459 /* ??? We're doing a subset of finish_id_expression here. */
19460 if (tree wrap = maybe_get_tls_wrapper_call (r))
19461 /* Replace an evaluated use of the thread_local variable with
19462 a call to its wrapper. */
19463 r = wrap;
19464 else if (outer_automatic_var_p (r))
19465 r = process_outer_var_ref (r, complain);
19466
19467 if (!TYPE_REF_P (TREE_TYPE (t)))
19468 /* If the original type was a reference, we'll be wrapped in
19469 the appropriate INDIRECT_REF. */
19470 r = convert_from_reference (r);
19471 RETURN (r);
19472 }
19473
19474 case VA_ARG_EXPR:
19475 {
19476 tree op0 = RECUR (TREE_OPERAND (t, 0));
19477 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19478 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19479 }
19480
19481 case OFFSETOF_EXPR:
19482 {
19483 tree object_ptr
19484 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19485 in_decl, /*function_p=*/false,
19486 /*integral_constant_expression_p=*/false);
19487 RETURN (finish_offsetof (object_ptr,
19488 RECUR (TREE_OPERAND (t, 0)),
19489 EXPR_LOCATION (t)));
19490 }
19491
19492 case ADDRESSOF_EXPR:
19493 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19494 RECUR (TREE_OPERAND (t, 0)), complain));
19495
19496 case TRAIT_EXPR:
19497 {
19498 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19499 complain, in_decl);
19500
19501 tree type2 = TRAIT_EXPR_TYPE2 (t);
19502 if (type2 && TREE_CODE (type2) == TREE_LIST)
19503 type2 = RECUR (type2);
19504 else if (type2)
19505 type2 = tsubst (type2, args, complain, in_decl);
19506
19507 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19508 }
19509
19510 case STMT_EXPR:
19511 {
19512 tree old_stmt_expr = cur_stmt_expr;
19513 tree stmt_expr = begin_stmt_expr ();
19514
19515 cur_stmt_expr = stmt_expr;
19516 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19517 integral_constant_expression_p);
19518 stmt_expr = finish_stmt_expr (stmt_expr, false);
19519 cur_stmt_expr = old_stmt_expr;
19520
19521 /* If the resulting list of expression statement is empty,
19522 fold it further into void_node. */
19523 if (empty_expr_stmt_p (stmt_expr))
19524 stmt_expr = void_node;
19525
19526 RETURN (stmt_expr);
19527 }
19528
19529 case LAMBDA_EXPR:
19530 {
19531 if (complain & tf_partial)
19532 {
19533 /* We don't have a full set of template arguments yet; don't touch
19534 the lambda at all. */
19535 gcc_assert (processing_template_decl);
19536 return t;
19537 }
19538 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19539
19540 RETURN (build_lambda_object (r));
19541 }
19542
19543 case TARGET_EXPR:
19544 /* We can get here for a constant initializer of non-dependent type.
19545 FIXME stop folding in cp_parser_initializer_clause. */
19546 {
19547 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19548 complain);
19549 RETURN (r);
19550 }
19551
19552 case TRANSACTION_EXPR:
19553 RETURN (tsubst_expr(t, args, complain, in_decl,
19554 integral_constant_expression_p));
19555
19556 case PAREN_EXPR:
19557 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19558
19559 case VEC_PERM_EXPR:
19560 {
19561 tree op0 = RECUR (TREE_OPERAND (t, 0));
19562 tree op1 = RECUR (TREE_OPERAND (t, 1));
19563 tree op2 = RECUR (TREE_OPERAND (t, 2));
19564 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19565 complain));
19566 }
19567
19568 case REQUIRES_EXPR:
19569 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19570
19571 case RANGE_EXPR:
19572 /* No need to substitute further, a RANGE_EXPR will always be built
19573 with constant operands. */
19574 RETURN (t);
19575
19576 case NON_LVALUE_EXPR:
19577 case VIEW_CONVERT_EXPR:
19578 if (location_wrapper_p (t))
19579 /* We need to do this here as well as in tsubst_copy so we get the
19580 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19581 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19582 EXPR_LOCATION (t)));
19583 /* fallthrough. */
19584
19585 default:
19586 /* Handle Objective-C++ constructs, if appropriate. */
19587 {
19588 tree subst
19589 = objcp_tsubst_copy_and_build (t, args, complain,
19590 in_decl, /*function_p=*/false);
19591 if (subst)
19592 RETURN (subst);
19593 }
19594 RETURN (tsubst_copy (t, args, complain, in_decl));
19595 }
19596
19597 #undef RECUR
19598 #undef RETURN
19599 out:
19600 input_location = loc;
19601 return retval;
19602 }
19603
19604 /* Verify that the instantiated ARGS are valid. For type arguments,
19605 make sure that the type's linkage is ok. For non-type arguments,
19606 make sure they are constants if they are integral or enumerations.
19607 Emit an error under control of COMPLAIN, and return TRUE on error. */
19608
19609 static bool
19610 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19611 {
19612 if (dependent_template_arg_p (t))
19613 return false;
19614 if (ARGUMENT_PACK_P (t))
19615 {
19616 tree vec = ARGUMENT_PACK_ARGS (t);
19617 int len = TREE_VEC_LENGTH (vec);
19618 bool result = false;
19619 int i;
19620
19621 for (i = 0; i < len; ++i)
19622 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19623 result = true;
19624 return result;
19625 }
19626 else if (TYPE_P (t))
19627 {
19628 /* [basic.link]: A name with no linkage (notably, the name
19629 of a class or enumeration declared in a local scope)
19630 shall not be used to declare an entity with linkage.
19631 This implies that names with no linkage cannot be used as
19632 template arguments
19633
19634 DR 757 relaxes this restriction for C++0x. */
19635 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19636 : no_linkage_check (t, /*relaxed_p=*/false));
19637
19638 if (nt)
19639 {
19640 /* DR 488 makes use of a type with no linkage cause
19641 type deduction to fail. */
19642 if (complain & tf_error)
19643 {
19644 if (TYPE_UNNAMED_P (nt))
19645 error ("%qT is/uses unnamed type", t);
19646 else
19647 error ("template argument for %qD uses local type %qT",
19648 tmpl, t);
19649 }
19650 return true;
19651 }
19652 /* In order to avoid all sorts of complications, we do not
19653 allow variably-modified types as template arguments. */
19654 else if (variably_modified_type_p (t, NULL_TREE))
19655 {
19656 if (complain & tf_error)
19657 error ("%qT is a variably modified type", t);
19658 return true;
19659 }
19660 }
19661 /* Class template and alias template arguments should be OK. */
19662 else if (DECL_TYPE_TEMPLATE_P (t))
19663 ;
19664 /* A non-type argument of integral or enumerated type must be a
19665 constant. */
19666 else if (TREE_TYPE (t)
19667 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19668 && !REFERENCE_REF_P (t)
19669 && !TREE_CONSTANT (t))
19670 {
19671 if (complain & tf_error)
19672 error ("integral expression %qE is not constant", t);
19673 return true;
19674 }
19675 return false;
19676 }
19677
19678 static bool
19679 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19680 {
19681 int ix, len = DECL_NTPARMS (tmpl);
19682 bool result = false;
19683
19684 for (ix = 0; ix != len; ix++)
19685 {
19686 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19687 result = true;
19688 }
19689 if (result && (complain & tf_error))
19690 error (" trying to instantiate %qD", tmpl);
19691 return result;
19692 }
19693
19694 /* We're out of SFINAE context now, so generate diagnostics for the access
19695 errors we saw earlier when instantiating D from TMPL and ARGS. */
19696
19697 static void
19698 recheck_decl_substitution (tree d, tree tmpl, tree args)
19699 {
19700 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19701 tree type = TREE_TYPE (pattern);
19702 location_t loc = input_location;
19703
19704 push_access_scope (d);
19705 push_deferring_access_checks (dk_no_deferred);
19706 input_location = DECL_SOURCE_LOCATION (pattern);
19707 tsubst (type, args, tf_warning_or_error, d);
19708 input_location = loc;
19709 pop_deferring_access_checks ();
19710 pop_access_scope (d);
19711 }
19712
19713 /* Instantiate the indicated variable, function, or alias template TMPL with
19714 the template arguments in TARG_PTR. */
19715
19716 static tree
19717 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19718 {
19719 tree targ_ptr = orig_args;
19720 tree fndecl;
19721 tree gen_tmpl;
19722 tree spec;
19723 bool access_ok = true;
19724
19725 if (tmpl == error_mark_node)
19726 return error_mark_node;
19727
19728 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19729
19730 /* If this function is a clone, handle it specially. */
19731 if (DECL_CLONED_FUNCTION_P (tmpl))
19732 {
19733 tree spec;
19734 tree clone;
19735
19736 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19737 DECL_CLONED_FUNCTION. */
19738 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19739 targ_ptr, complain);
19740 if (spec == error_mark_node)
19741 return error_mark_node;
19742
19743 /* Look for the clone. */
19744 FOR_EACH_CLONE (clone, spec)
19745 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19746 return clone;
19747 /* We should always have found the clone by now. */
19748 gcc_unreachable ();
19749 return NULL_TREE;
19750 }
19751
19752 if (targ_ptr == error_mark_node)
19753 return error_mark_node;
19754
19755 /* Check to see if we already have this specialization. */
19756 gen_tmpl = most_general_template (tmpl);
19757 if (TMPL_ARGS_DEPTH (targ_ptr)
19758 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19759 /* targ_ptr only has the innermost template args, so add the outer ones
19760 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19761 the case of a non-dependent call within a template definition). */
19762 targ_ptr = (add_outermost_template_args
19763 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19764 targ_ptr));
19765
19766 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19767 but it doesn't seem to be on the hot path. */
19768 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19769
19770 gcc_assert (tmpl == gen_tmpl
19771 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19772 == spec)
19773 || fndecl == NULL_TREE);
19774
19775 if (spec != NULL_TREE)
19776 {
19777 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19778 {
19779 if (complain & tf_error)
19780 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19781 return error_mark_node;
19782 }
19783 return spec;
19784 }
19785
19786 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19787 complain))
19788 return error_mark_node;
19789
19790 /* We are building a FUNCTION_DECL, during which the access of its
19791 parameters and return types have to be checked. However this
19792 FUNCTION_DECL which is the desired context for access checking
19793 is not built yet. We solve this chicken-and-egg problem by
19794 deferring all checks until we have the FUNCTION_DECL. */
19795 push_deferring_access_checks (dk_deferred);
19796
19797 /* Instantiation of the function happens in the context of the function
19798 template, not the context of the overload resolution we're doing. */
19799 push_to_top_level ();
19800 /* If there are dependent arguments, e.g. because we're doing partial
19801 ordering, make sure processing_template_decl stays set. */
19802 if (uses_template_parms (targ_ptr))
19803 ++processing_template_decl;
19804 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19805 {
19806 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19807 complain, gen_tmpl, true);
19808 push_nested_class (ctx);
19809 }
19810
19811 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19812
19813 fndecl = NULL_TREE;
19814 if (VAR_P (pattern))
19815 {
19816 /* We need to determine if we're using a partial or explicit
19817 specialization now, because the type of the variable could be
19818 different. */
19819 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19820 tree elt = most_specialized_partial_spec (tid, complain);
19821 if (elt == error_mark_node)
19822 pattern = error_mark_node;
19823 else if (elt)
19824 {
19825 tree partial_tmpl = TREE_VALUE (elt);
19826 tree partial_args = TREE_PURPOSE (elt);
19827 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19828 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19829 }
19830 }
19831
19832 /* Substitute template parameters to obtain the specialization. */
19833 if (fndecl == NULL_TREE)
19834 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19835 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19836 pop_nested_class ();
19837 pop_from_top_level ();
19838
19839 if (fndecl == error_mark_node)
19840 {
19841 pop_deferring_access_checks ();
19842 return error_mark_node;
19843 }
19844
19845 /* The DECL_TI_TEMPLATE should always be the immediate parent
19846 template, not the most general template. */
19847 DECL_TI_TEMPLATE (fndecl) = tmpl;
19848 DECL_TI_ARGS (fndecl) = targ_ptr;
19849
19850 /* Now we know the specialization, compute access previously
19851 deferred. Do no access control for inheriting constructors,
19852 as we already checked access for the inherited constructor. */
19853 if (!(flag_new_inheriting_ctors
19854 && DECL_INHERITED_CTOR (fndecl)))
19855 {
19856 push_access_scope (fndecl);
19857 if (!perform_deferred_access_checks (complain))
19858 access_ok = false;
19859 pop_access_scope (fndecl);
19860 }
19861 pop_deferring_access_checks ();
19862
19863 /* If we've just instantiated the main entry point for a function,
19864 instantiate all the alternate entry points as well. We do this
19865 by cloning the instantiation of the main entry point, not by
19866 instantiating the template clones. */
19867 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19868 clone_function_decl (fndecl, /*update_methods=*/false);
19869
19870 if (!access_ok)
19871 {
19872 if (!(complain & tf_error))
19873 {
19874 /* Remember to reinstantiate when we're out of SFINAE so the user
19875 can see the errors. */
19876 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19877 }
19878 return error_mark_node;
19879 }
19880 return fndecl;
19881 }
19882
19883 /* Wrapper for instantiate_template_1. */
19884
19885 tree
19886 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19887 {
19888 tree ret;
19889 timevar_push (TV_TEMPLATE_INST);
19890 ret = instantiate_template_1 (tmpl, orig_args, complain);
19891 timevar_pop (TV_TEMPLATE_INST);
19892 return ret;
19893 }
19894
19895 /* Instantiate the alias template TMPL with ARGS. Also push a template
19896 instantiation level, which instantiate_template doesn't do because
19897 functions and variables have sufficient context established by the
19898 callers. */
19899
19900 static tree
19901 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19902 {
19903 if (tmpl == error_mark_node || args == error_mark_node)
19904 return error_mark_node;
19905 if (!push_tinst_level (tmpl, args))
19906 return error_mark_node;
19907
19908 args =
19909 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19910 args, tmpl, complain,
19911 /*require_all_args=*/true,
19912 /*use_default_args=*/true);
19913
19914 tree r = instantiate_template (tmpl, args, complain);
19915 pop_tinst_level ();
19916
19917 return r;
19918 }
19919
19920 /* PARM is a template parameter pack for FN. Returns true iff
19921 PARM is used in a deducible way in the argument list of FN. */
19922
19923 static bool
19924 pack_deducible_p (tree parm, tree fn)
19925 {
19926 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19927 for (; t; t = TREE_CHAIN (t))
19928 {
19929 tree type = TREE_VALUE (t);
19930 tree packs;
19931 if (!PACK_EXPANSION_P (type))
19932 continue;
19933 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19934 packs; packs = TREE_CHAIN (packs))
19935 if (template_args_equal (TREE_VALUE (packs), parm))
19936 {
19937 /* The template parameter pack is used in a function parameter
19938 pack. If this is the end of the parameter list, the
19939 template parameter pack is deducible. */
19940 if (TREE_CHAIN (t) == void_list_node)
19941 return true;
19942 else
19943 /* Otherwise, not. Well, it could be deduced from
19944 a non-pack parameter, but doing so would end up with
19945 a deduction mismatch, so don't bother. */
19946 return false;
19947 }
19948 }
19949 /* The template parameter pack isn't used in any function parameter
19950 packs, but it might be used deeper, e.g. tuple<Args...>. */
19951 return true;
19952 }
19953
19954 /* Subroutine of fn_type_unification: check non-dependent parms for
19955 convertibility. */
19956
19957 static int
19958 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19959 tree fn, unification_kind_t strict, int flags,
19960 struct conversion **convs, bool explain_p)
19961 {
19962 /* Non-constructor methods need to leave a conversion for 'this', which
19963 isn't included in nargs here. */
19964 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19965 && !DECL_CONSTRUCTOR_P (fn));
19966
19967 for (unsigned ia = 0;
19968 parms && parms != void_list_node && ia < nargs; )
19969 {
19970 tree parm = TREE_VALUE (parms);
19971
19972 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19973 && (!TREE_CHAIN (parms)
19974 || TREE_CHAIN (parms) == void_list_node))
19975 /* For a function parameter pack that occurs at the end of the
19976 parameter-declaration-list, the type A of each remaining
19977 argument of the call is compared with the type P of the
19978 declarator-id of the function parameter pack. */
19979 break;
19980
19981 parms = TREE_CHAIN (parms);
19982
19983 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19984 /* For a function parameter pack that does not occur at the
19985 end of the parameter-declaration-list, the type of the
19986 parameter pack is a non-deduced context. */
19987 continue;
19988
19989 if (!uses_template_parms (parm))
19990 {
19991 tree arg = args[ia];
19992 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19993 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19994
19995 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19996 conv_p, explain_p))
19997 return 1;
19998 }
19999
20000 ++ia;
20001 }
20002
20003 return 0;
20004 }
20005
20006 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20007 NARGS elements of the arguments that are being used when calling
20008 it. TARGS is a vector into which the deduced template arguments
20009 are placed.
20010
20011 Returns either a FUNCTION_DECL for the matching specialization of FN or
20012 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20013 true, diagnostics will be printed to explain why it failed.
20014
20015 If FN is a conversion operator, or we are trying to produce a specific
20016 specialization, RETURN_TYPE is the return type desired.
20017
20018 The EXPLICIT_TARGS are explicit template arguments provided via a
20019 template-id.
20020
20021 The parameter STRICT is one of:
20022
20023 DEDUCE_CALL:
20024 We are deducing arguments for a function call, as in
20025 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20026 deducing arguments for a call to the result of a conversion
20027 function template, as in [over.call.object].
20028
20029 DEDUCE_CONV:
20030 We are deducing arguments for a conversion function, as in
20031 [temp.deduct.conv].
20032
20033 DEDUCE_EXACT:
20034 We are deducing arguments when doing an explicit instantiation
20035 as in [temp.explicit], when determining an explicit specialization
20036 as in [temp.expl.spec], or when taking the address of a function
20037 template, as in [temp.deduct.funcaddr]. */
20038
20039 tree
20040 fn_type_unification (tree fn,
20041 tree explicit_targs,
20042 tree targs,
20043 const tree *args,
20044 unsigned int nargs,
20045 tree return_type,
20046 unification_kind_t strict,
20047 int flags,
20048 struct conversion **convs,
20049 bool explain_p,
20050 bool decltype_p)
20051 {
20052 tree parms;
20053 tree fntype;
20054 tree decl = NULL_TREE;
20055 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20056 bool ok;
20057 static int deduction_depth;
20058 /* type_unification_real will pass back any access checks from default
20059 template argument substitution. */
20060 vec<deferred_access_check, va_gc> *checks = NULL;
20061 /* We don't have all the template args yet. */
20062 bool incomplete = true;
20063
20064 tree orig_fn = fn;
20065 if (flag_new_inheriting_ctors)
20066 fn = strip_inheriting_ctors (fn);
20067
20068 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20069 tree r = error_mark_node;
20070
20071 tree full_targs = targs;
20072 if (TMPL_ARGS_DEPTH (targs)
20073 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20074 full_targs = (add_outermost_template_args
20075 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20076 targs));
20077
20078 if (decltype_p)
20079 complain |= tf_decltype;
20080
20081 /* In C++0x, it's possible to have a function template whose type depends
20082 on itself recursively. This is most obvious with decltype, but can also
20083 occur with enumeration scope (c++/48969). So we need to catch infinite
20084 recursion and reject the substitution at deduction time; this function
20085 will return error_mark_node for any repeated substitution.
20086
20087 This also catches excessive recursion such as when f<N> depends on
20088 f<N-1> across all integers, and returns error_mark_node for all the
20089 substitutions back up to the initial one.
20090
20091 This is, of course, not reentrant. */
20092 if (excessive_deduction_depth)
20093 return error_mark_node;
20094 ++deduction_depth;
20095
20096 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20097
20098 fntype = TREE_TYPE (fn);
20099 if (explicit_targs)
20100 {
20101 /* [temp.deduct]
20102
20103 The specified template arguments must match the template
20104 parameters in kind (i.e., type, nontype, template), and there
20105 must not be more arguments than there are parameters;
20106 otherwise type deduction fails.
20107
20108 Nontype arguments must match the types of the corresponding
20109 nontype template parameters, or must be convertible to the
20110 types of the corresponding nontype parameters as specified in
20111 _temp.arg.nontype_, otherwise type deduction fails.
20112
20113 All references in the function type of the function template
20114 to the corresponding template parameters are replaced by the
20115 specified template argument values. If a substitution in a
20116 template parameter or in the function type of the function
20117 template results in an invalid type, type deduction fails. */
20118 int i, len = TREE_VEC_LENGTH (tparms);
20119 location_t loc = input_location;
20120 incomplete = false;
20121
20122 if (explicit_targs == error_mark_node)
20123 goto fail;
20124
20125 if (TMPL_ARGS_DEPTH (explicit_targs)
20126 < TMPL_ARGS_DEPTH (full_targs))
20127 explicit_targs = add_outermost_template_args (full_targs,
20128 explicit_targs);
20129
20130 /* Adjust any explicit template arguments before entering the
20131 substitution context. */
20132 explicit_targs
20133 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20134 complain|tf_partial,
20135 /*require_all_args=*/false,
20136 /*use_default_args=*/false));
20137 if (explicit_targs == error_mark_node)
20138 goto fail;
20139
20140 /* Substitute the explicit args into the function type. This is
20141 necessary so that, for instance, explicitly declared function
20142 arguments can match null pointed constants. If we were given
20143 an incomplete set of explicit args, we must not do semantic
20144 processing during substitution as we could create partial
20145 instantiations. */
20146 for (i = 0; i < len; i++)
20147 {
20148 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20149 bool parameter_pack = false;
20150 tree targ = TREE_VEC_ELT (explicit_targs, i);
20151
20152 /* Dig out the actual parm. */
20153 if (TREE_CODE (parm) == TYPE_DECL
20154 || TREE_CODE (parm) == TEMPLATE_DECL)
20155 {
20156 parm = TREE_TYPE (parm);
20157 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20158 }
20159 else if (TREE_CODE (parm) == PARM_DECL)
20160 {
20161 parm = DECL_INITIAL (parm);
20162 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20163 }
20164
20165 if (targ == NULL_TREE)
20166 /* No explicit argument for this template parameter. */
20167 incomplete = true;
20168 else if (parameter_pack && pack_deducible_p (parm, fn))
20169 {
20170 /* Mark the argument pack as "incomplete". We could
20171 still deduce more arguments during unification.
20172 We remove this mark in type_unification_real. */
20173 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20174 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20175 = ARGUMENT_PACK_ARGS (targ);
20176
20177 /* We have some incomplete argument packs. */
20178 incomplete = true;
20179 }
20180 }
20181
20182 if (incomplete)
20183 {
20184 if (!push_tinst_level (fn, explicit_targs))
20185 {
20186 excessive_deduction_depth = true;
20187 goto fail;
20188 }
20189 ++processing_template_decl;
20190 input_location = DECL_SOURCE_LOCATION (fn);
20191 /* Ignore any access checks; we'll see them again in
20192 instantiate_template and they might have the wrong
20193 access path at this point. */
20194 push_deferring_access_checks (dk_deferred);
20195 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20196 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20197 pop_deferring_access_checks ();
20198 input_location = loc;
20199 --processing_template_decl;
20200 pop_tinst_level ();
20201
20202 if (fntype == error_mark_node)
20203 goto fail;
20204 }
20205
20206 /* Place the explicitly specified arguments in TARGS. */
20207 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20208 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20209 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20210 if (!incomplete && CHECKING_P
20211 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20212 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20213 (targs, NUM_TMPL_ARGS (explicit_targs));
20214 }
20215
20216 if (return_type && strict != DEDUCE_CALL)
20217 {
20218 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20219 new_args[0] = return_type;
20220 memcpy (new_args + 1, args, nargs * sizeof (tree));
20221 args = new_args;
20222 ++nargs;
20223 }
20224
20225 if (!incomplete)
20226 goto deduced;
20227
20228 /* Never do unification on the 'this' parameter. */
20229 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20230
20231 if (return_type && strict == DEDUCE_CALL)
20232 {
20233 /* We're deducing for a call to the result of a template conversion
20234 function. The parms we really want are in return_type. */
20235 if (INDIRECT_TYPE_P (return_type))
20236 return_type = TREE_TYPE (return_type);
20237 parms = TYPE_ARG_TYPES (return_type);
20238 }
20239 else if (return_type)
20240 {
20241 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20242 }
20243
20244 /* We allow incomplete unification without an error message here
20245 because the standard doesn't seem to explicitly prohibit it. Our
20246 callers must be ready to deal with unification failures in any
20247 event. */
20248
20249 /* If we aren't explaining yet, push tinst context so we can see where
20250 any errors (e.g. from class instantiations triggered by instantiation
20251 of default template arguments) come from. If we are explaining, this
20252 context is redundant. */
20253 if (!explain_p && !push_tinst_level (fn, targs))
20254 {
20255 excessive_deduction_depth = true;
20256 goto fail;
20257 }
20258
20259 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20260 full_targs, parms, args, nargs, /*subr=*/0,
20261 strict, &checks, explain_p);
20262 if (!explain_p)
20263 pop_tinst_level ();
20264 if (!ok)
20265 goto fail;
20266
20267 /* Now that we have bindings for all of the template arguments,
20268 ensure that the arguments deduced for the template template
20269 parameters have compatible template parameter lists. We cannot
20270 check this property before we have deduced all template
20271 arguments, because the template parameter types of a template
20272 template parameter might depend on prior template parameters
20273 deduced after the template template parameter. The following
20274 ill-formed example illustrates this issue:
20275
20276 template<typename T, template<T> class C> void f(C<5>, T);
20277
20278 template<int N> struct X {};
20279
20280 void g() {
20281 f(X<5>(), 5l); // error: template argument deduction fails
20282 }
20283
20284 The template parameter list of 'C' depends on the template type
20285 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20286 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20287 time that we deduce 'C'. */
20288 if (!template_template_parm_bindings_ok_p
20289 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20290 {
20291 unify_inconsistent_template_template_parameters (explain_p);
20292 goto fail;
20293 }
20294
20295 /* DR 1391: All parameters have args, now check non-dependent parms for
20296 convertibility. */
20297 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20298 convs, explain_p))
20299 goto fail;
20300
20301 deduced:
20302 /* All is well so far. Now, check:
20303
20304 [temp.deduct]
20305
20306 When all template arguments have been deduced, all uses of
20307 template parameters in nondeduced contexts are replaced with
20308 the corresponding deduced argument values. If the
20309 substitution results in an invalid type, as described above,
20310 type deduction fails. */
20311 if (!push_tinst_level (fn, targs))
20312 {
20313 excessive_deduction_depth = true;
20314 goto fail;
20315 }
20316
20317 /* Also collect access checks from the instantiation. */
20318 reopen_deferring_access_checks (checks);
20319
20320 decl = instantiate_template (fn, targs, complain);
20321
20322 checks = get_deferred_access_checks ();
20323 pop_deferring_access_checks ();
20324
20325 pop_tinst_level ();
20326
20327 if (decl == error_mark_node)
20328 goto fail;
20329
20330 /* Now perform any access checks encountered during substitution. */
20331 push_access_scope (decl);
20332 ok = perform_access_checks (checks, complain);
20333 pop_access_scope (decl);
20334 if (!ok)
20335 goto fail;
20336
20337 /* If we're looking for an exact match, check that what we got
20338 is indeed an exact match. It might not be if some template
20339 parameters are used in non-deduced contexts. But don't check
20340 for an exact match if we have dependent template arguments;
20341 in that case we're doing partial ordering, and we already know
20342 that we have two candidates that will provide the actual type. */
20343 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20344 {
20345 tree substed = TREE_TYPE (decl);
20346 unsigned int i;
20347
20348 tree sarg
20349 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20350 if (return_type)
20351 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20352 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20353 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20354 {
20355 unify_type_mismatch (explain_p, args[i],
20356 TREE_VALUE (sarg));
20357 goto fail;
20358 }
20359 }
20360
20361 /* After doing deduction with the inherited constructor, actually return an
20362 instantiation of the inheriting constructor. */
20363 if (orig_fn != fn)
20364 decl = instantiate_template (orig_fn, targs, complain);
20365
20366 r = decl;
20367
20368 fail:
20369 --deduction_depth;
20370 if (excessive_deduction_depth)
20371 {
20372 if (deduction_depth == 0)
20373 /* Reset once we're all the way out. */
20374 excessive_deduction_depth = false;
20375 }
20376
20377 return r;
20378 }
20379
20380 /* Adjust types before performing type deduction, as described in
20381 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20382 sections are symmetric. PARM is the type of a function parameter
20383 or the return type of the conversion function. ARG is the type of
20384 the argument passed to the call, or the type of the value
20385 initialized with the result of the conversion function.
20386 ARG_EXPR is the original argument expression, which may be null. */
20387
20388 static int
20389 maybe_adjust_types_for_deduction (unification_kind_t strict,
20390 tree* parm,
20391 tree* arg,
20392 tree arg_expr)
20393 {
20394 int result = 0;
20395
20396 switch (strict)
20397 {
20398 case DEDUCE_CALL:
20399 break;
20400
20401 case DEDUCE_CONV:
20402 /* Swap PARM and ARG throughout the remainder of this
20403 function; the handling is precisely symmetric since PARM
20404 will initialize ARG rather than vice versa. */
20405 std::swap (parm, arg);
20406 break;
20407
20408 case DEDUCE_EXACT:
20409 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20410 too, but here handle it by stripping the reference from PARM
20411 rather than by adding it to ARG. */
20412 if (TYPE_REF_P (*parm)
20413 && TYPE_REF_IS_RVALUE (*parm)
20414 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20415 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20416 && TYPE_REF_P (*arg)
20417 && !TYPE_REF_IS_RVALUE (*arg))
20418 *parm = TREE_TYPE (*parm);
20419 /* Nothing else to do in this case. */
20420 return 0;
20421
20422 default:
20423 gcc_unreachable ();
20424 }
20425
20426 if (!TYPE_REF_P (*parm))
20427 {
20428 /* [temp.deduct.call]
20429
20430 If P is not a reference type:
20431
20432 --If A is an array type, the pointer type produced by the
20433 array-to-pointer standard conversion (_conv.array_) is
20434 used in place of A for type deduction; otherwise,
20435
20436 --If A is a function type, the pointer type produced by
20437 the function-to-pointer standard conversion
20438 (_conv.func_) is used in place of A for type deduction;
20439 otherwise,
20440
20441 --If A is a cv-qualified type, the top level
20442 cv-qualifiers of A's type are ignored for type
20443 deduction. */
20444 if (TREE_CODE (*arg) == ARRAY_TYPE)
20445 *arg = build_pointer_type (TREE_TYPE (*arg));
20446 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20447 *arg = build_pointer_type (*arg);
20448 else
20449 *arg = TYPE_MAIN_VARIANT (*arg);
20450 }
20451
20452 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20453 reference to a cv-unqualified template parameter that does not represent a
20454 template parameter of a class template (during class template argument
20455 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20456 an lvalue, the type "lvalue reference to A" is used in place of A for type
20457 deduction. */
20458 if (TYPE_REF_P (*parm)
20459 && TYPE_REF_IS_RVALUE (*parm)
20460 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20461 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20462 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20463 && (arg_expr ? lvalue_p (arg_expr)
20464 /* try_one_overload doesn't provide an arg_expr, but
20465 functions are always lvalues. */
20466 : TREE_CODE (*arg) == FUNCTION_TYPE))
20467 *arg = build_reference_type (*arg);
20468
20469 /* [temp.deduct.call]
20470
20471 If P is a cv-qualified type, the top level cv-qualifiers
20472 of P's type are ignored for type deduction. If P is a
20473 reference type, the type referred to by P is used for
20474 type deduction. */
20475 *parm = TYPE_MAIN_VARIANT (*parm);
20476 if (TYPE_REF_P (*parm))
20477 {
20478 *parm = TREE_TYPE (*parm);
20479 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20480 }
20481
20482 /* DR 322. For conversion deduction, remove a reference type on parm
20483 too (which has been swapped into ARG). */
20484 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20485 *arg = TREE_TYPE (*arg);
20486
20487 return result;
20488 }
20489
20490 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20491 template which doesn't contain any deducible template parameters; check if
20492 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20493 unify_one_argument. */
20494
20495 static int
20496 check_non_deducible_conversion (tree parm, tree arg, int strict,
20497 int flags, struct conversion **conv_p,
20498 bool explain_p)
20499 {
20500 tree type;
20501
20502 if (!TYPE_P (arg))
20503 type = TREE_TYPE (arg);
20504 else
20505 type = arg;
20506
20507 if (same_type_p (parm, type))
20508 return unify_success (explain_p);
20509
20510 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20511 if (strict == DEDUCE_CONV)
20512 {
20513 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20514 return unify_success (explain_p);
20515 }
20516 else if (strict != DEDUCE_EXACT)
20517 {
20518 bool ok = false;
20519 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20520 if (conv_p)
20521 /* Avoid recalculating this in add_function_candidate. */
20522 ok = (*conv_p
20523 = good_conversion (parm, type, conv_arg, flags, complain));
20524 else
20525 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20526 if (ok)
20527 return unify_success (explain_p);
20528 }
20529
20530 if (strict == DEDUCE_EXACT)
20531 return unify_type_mismatch (explain_p, parm, arg);
20532 else
20533 return unify_arg_conversion (explain_p, parm, type, arg);
20534 }
20535
20536 static bool uses_deducible_template_parms (tree type);
20537
20538 /* Returns true iff the expression EXPR is one from which a template
20539 argument can be deduced. In other words, if it's an undecorated
20540 use of a template non-type parameter. */
20541
20542 static bool
20543 deducible_expression (tree expr)
20544 {
20545 /* Strip implicit conversions. */
20546 while (CONVERT_EXPR_P (expr))
20547 expr = TREE_OPERAND (expr, 0);
20548 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20549 }
20550
20551 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20552 deducible way; that is, if it has a max value of <PARM> - 1. */
20553
20554 static bool
20555 deducible_array_bound (tree domain)
20556 {
20557 if (domain == NULL_TREE)
20558 return false;
20559
20560 tree max = TYPE_MAX_VALUE (domain);
20561 if (TREE_CODE (max) != MINUS_EXPR)
20562 return false;
20563
20564 return deducible_expression (TREE_OPERAND (max, 0));
20565 }
20566
20567 /* Returns true iff the template arguments ARGS use a template parameter
20568 in a deducible way. */
20569
20570 static bool
20571 deducible_template_args (tree args)
20572 {
20573 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20574 {
20575 bool deducible;
20576 tree elt = TREE_VEC_ELT (args, i);
20577 if (ARGUMENT_PACK_P (elt))
20578 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20579 else
20580 {
20581 if (PACK_EXPANSION_P (elt))
20582 elt = PACK_EXPANSION_PATTERN (elt);
20583 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20584 deducible = true;
20585 else if (TYPE_P (elt))
20586 deducible = uses_deducible_template_parms (elt);
20587 else
20588 deducible = deducible_expression (elt);
20589 }
20590 if (deducible)
20591 return true;
20592 }
20593 return false;
20594 }
20595
20596 /* Returns true iff TYPE contains any deducible references to template
20597 parameters, as per 14.8.2.5. */
20598
20599 static bool
20600 uses_deducible_template_parms (tree type)
20601 {
20602 if (PACK_EXPANSION_P (type))
20603 type = PACK_EXPANSION_PATTERN (type);
20604
20605 /* T
20606 cv-list T
20607 TT<T>
20608 TT<i>
20609 TT<> */
20610 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20611 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20612 return true;
20613
20614 /* T*
20615 T&
20616 T&& */
20617 if (INDIRECT_TYPE_P (type))
20618 return uses_deducible_template_parms (TREE_TYPE (type));
20619
20620 /* T[integer-constant ]
20621 type [i] */
20622 if (TREE_CODE (type) == ARRAY_TYPE)
20623 return (uses_deducible_template_parms (TREE_TYPE (type))
20624 || deducible_array_bound (TYPE_DOMAIN (type)));
20625
20626 /* T type ::*
20627 type T::*
20628 T T::*
20629 T (type ::*)()
20630 type (T::*)()
20631 type (type ::*)(T)
20632 type (T::*)(T)
20633 T (type ::*)(T)
20634 T (T::*)()
20635 T (T::*)(T) */
20636 if (TYPE_PTRMEM_P (type))
20637 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20638 || (uses_deducible_template_parms
20639 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20640
20641 /* template-name <T> (where template-name refers to a class template)
20642 template-name <i> (where template-name refers to a class template) */
20643 if (CLASS_TYPE_P (type)
20644 && CLASSTYPE_TEMPLATE_INFO (type)
20645 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20646 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20647 (CLASSTYPE_TI_ARGS (type)));
20648
20649 /* type (T)
20650 T()
20651 T(T) */
20652 if (FUNC_OR_METHOD_TYPE_P (type))
20653 {
20654 if (uses_deducible_template_parms (TREE_TYPE (type)))
20655 return true;
20656 tree parm = TYPE_ARG_TYPES (type);
20657 if (TREE_CODE (type) == METHOD_TYPE)
20658 parm = TREE_CHAIN (parm);
20659 for (; parm; parm = TREE_CHAIN (parm))
20660 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20661 return true;
20662 }
20663
20664 return false;
20665 }
20666
20667 /* Subroutine of type_unification_real and unify_pack_expansion to
20668 handle unification of a single P/A pair. Parameters are as
20669 for those functions. */
20670
20671 static int
20672 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20673 int subr, unification_kind_t strict,
20674 bool explain_p)
20675 {
20676 tree arg_expr = NULL_TREE;
20677 int arg_strict;
20678
20679 if (arg == error_mark_node || parm == error_mark_node)
20680 return unify_invalid (explain_p);
20681 if (arg == unknown_type_node)
20682 /* We can't deduce anything from this, but we might get all the
20683 template args from other function args. */
20684 return unify_success (explain_p);
20685
20686 /* Implicit conversions (Clause 4) will be performed on a function
20687 argument to convert it to the type of the corresponding function
20688 parameter if the parameter type contains no template-parameters that
20689 participate in template argument deduction. */
20690 if (strict != DEDUCE_EXACT
20691 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20692 /* For function parameters with no deducible template parameters,
20693 just return. We'll check non-dependent conversions later. */
20694 return unify_success (explain_p);
20695
20696 switch (strict)
20697 {
20698 case DEDUCE_CALL:
20699 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20700 | UNIFY_ALLOW_MORE_CV_QUAL
20701 | UNIFY_ALLOW_DERIVED);
20702 break;
20703
20704 case DEDUCE_CONV:
20705 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20706 break;
20707
20708 case DEDUCE_EXACT:
20709 arg_strict = UNIFY_ALLOW_NONE;
20710 break;
20711
20712 default:
20713 gcc_unreachable ();
20714 }
20715
20716 /* We only do these transformations if this is the top-level
20717 parameter_type_list in a call or declaration matching; in other
20718 situations (nested function declarators, template argument lists) we
20719 won't be comparing a type to an expression, and we don't do any type
20720 adjustments. */
20721 if (!subr)
20722 {
20723 if (!TYPE_P (arg))
20724 {
20725 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20726 if (type_unknown_p (arg))
20727 {
20728 /* [temp.deduct.type] A template-argument can be
20729 deduced from a pointer to function or pointer
20730 to member function argument if the set of
20731 overloaded functions does not contain function
20732 templates and at most one of a set of
20733 overloaded functions provides a unique
20734 match. */
20735 resolve_overloaded_unification (tparms, targs, parm,
20736 arg, strict,
20737 arg_strict, explain_p);
20738 /* If a unique match was not found, this is a
20739 non-deduced context, so we still succeed. */
20740 return unify_success (explain_p);
20741 }
20742
20743 arg_expr = arg;
20744 arg = unlowered_expr_type (arg);
20745 if (arg == error_mark_node)
20746 return unify_invalid (explain_p);
20747 }
20748
20749 arg_strict |=
20750 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20751 }
20752 else
20753 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20754 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20755 return unify_template_argument_mismatch (explain_p, parm, arg);
20756
20757 /* For deduction from an init-list we need the actual list. */
20758 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20759 arg = arg_expr;
20760 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20761 }
20762
20763 /* for_each_template_parm callback that always returns 0. */
20764
20765 static int
20766 zero_r (tree, void *)
20767 {
20768 return 0;
20769 }
20770
20771 /* for_each_template_parm any_fn callback to handle deduction of a template
20772 type argument from the type of an array bound. */
20773
20774 static int
20775 array_deduction_r (tree t, void *data)
20776 {
20777 tree_pair_p d = (tree_pair_p)data;
20778 tree &tparms = d->purpose;
20779 tree &targs = d->value;
20780
20781 if (TREE_CODE (t) == ARRAY_TYPE)
20782 if (tree dom = TYPE_DOMAIN (t))
20783 if (tree max = TYPE_MAX_VALUE (dom))
20784 {
20785 if (TREE_CODE (max) == MINUS_EXPR)
20786 max = TREE_OPERAND (max, 0);
20787 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20788 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20789 UNIFY_ALLOW_NONE, /*explain*/false);
20790 }
20791
20792 /* Keep walking. */
20793 return 0;
20794 }
20795
20796 /* Try to deduce any not-yet-deduced template type arguments from the type of
20797 an array bound. This is handled separately from unify because 14.8.2.5 says
20798 "The type of a type parameter is only deduced from an array bound if it is
20799 not otherwise deduced." */
20800
20801 static void
20802 try_array_deduction (tree tparms, tree targs, tree parm)
20803 {
20804 tree_pair_s data = { tparms, targs };
20805 hash_set<tree> visited;
20806 for_each_template_parm (parm, zero_r, &data, &visited,
20807 /*nondeduced*/false, array_deduction_r);
20808 }
20809
20810 /* Most parms like fn_type_unification.
20811
20812 If SUBR is 1, we're being called recursively (to unify the
20813 arguments of a function or method parameter of a function
20814 template).
20815
20816 CHECKS is a pointer to a vector of access checks encountered while
20817 substituting default template arguments. */
20818
20819 static int
20820 type_unification_real (tree tparms,
20821 tree full_targs,
20822 tree xparms,
20823 const tree *xargs,
20824 unsigned int xnargs,
20825 int subr,
20826 unification_kind_t strict,
20827 vec<deferred_access_check, va_gc> **checks,
20828 bool explain_p)
20829 {
20830 tree parm, arg;
20831 int i;
20832 int ntparms = TREE_VEC_LENGTH (tparms);
20833 int saw_undeduced = 0;
20834 tree parms;
20835 const tree *args;
20836 unsigned int nargs;
20837 unsigned int ia;
20838
20839 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20840 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20841 gcc_assert (ntparms > 0);
20842
20843 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20844
20845 /* Reset the number of non-defaulted template arguments contained
20846 in TARGS. */
20847 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20848
20849 again:
20850 parms = xparms;
20851 args = xargs;
20852 nargs = xnargs;
20853
20854 ia = 0;
20855 while (parms && parms != void_list_node
20856 && ia < nargs)
20857 {
20858 parm = TREE_VALUE (parms);
20859
20860 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20861 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20862 /* For a function parameter pack that occurs at the end of the
20863 parameter-declaration-list, the type A of each remaining
20864 argument of the call is compared with the type P of the
20865 declarator-id of the function parameter pack. */
20866 break;
20867
20868 parms = TREE_CHAIN (parms);
20869
20870 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20871 /* For a function parameter pack that does not occur at the
20872 end of the parameter-declaration-list, the type of the
20873 parameter pack is a non-deduced context. */
20874 continue;
20875
20876 arg = args[ia];
20877 ++ia;
20878
20879 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20880 explain_p))
20881 return 1;
20882 }
20883
20884 if (parms
20885 && parms != void_list_node
20886 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20887 {
20888 /* Unify the remaining arguments with the pack expansion type. */
20889 tree argvec;
20890 tree parmvec = make_tree_vec (1);
20891
20892 /* Allocate a TREE_VEC and copy in all of the arguments */
20893 argvec = make_tree_vec (nargs - ia);
20894 for (i = 0; ia < nargs; ++ia, ++i)
20895 TREE_VEC_ELT (argvec, i) = args[ia];
20896
20897 /* Copy the parameter into parmvec. */
20898 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20899 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20900 /*subr=*/subr, explain_p))
20901 return 1;
20902
20903 /* Advance to the end of the list of parameters. */
20904 parms = TREE_CHAIN (parms);
20905 }
20906
20907 /* Fail if we've reached the end of the parm list, and more args
20908 are present, and the parm list isn't variadic. */
20909 if (ia < nargs && parms == void_list_node)
20910 return unify_too_many_arguments (explain_p, nargs, ia);
20911 /* Fail if parms are left and they don't have default values and
20912 they aren't all deduced as empty packs (c++/57397). This is
20913 consistent with sufficient_parms_p. */
20914 if (parms && parms != void_list_node
20915 && TREE_PURPOSE (parms) == NULL_TREE)
20916 {
20917 unsigned int count = nargs;
20918 tree p = parms;
20919 bool type_pack_p;
20920 do
20921 {
20922 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20923 if (!type_pack_p)
20924 count++;
20925 p = TREE_CHAIN (p);
20926 }
20927 while (p && p != void_list_node);
20928 if (count != nargs)
20929 return unify_too_few_arguments (explain_p, ia, count,
20930 type_pack_p);
20931 }
20932
20933 if (!subr)
20934 {
20935 tsubst_flags_t complain = (explain_p
20936 ? tf_warning_or_error
20937 : tf_none);
20938 bool tried_array_deduction = (cxx_dialect < cxx17);
20939
20940 for (i = 0; i < ntparms; i++)
20941 {
20942 tree targ = TREE_VEC_ELT (targs, i);
20943 tree tparm = TREE_VEC_ELT (tparms, i);
20944
20945 /* Clear the "incomplete" flags on all argument packs now so that
20946 substituting them into later default arguments works. */
20947 if (targ && ARGUMENT_PACK_P (targ))
20948 {
20949 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20950 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20951 }
20952
20953 if (targ || tparm == error_mark_node)
20954 continue;
20955 tparm = TREE_VALUE (tparm);
20956
20957 if (TREE_CODE (tparm) == TYPE_DECL
20958 && !tried_array_deduction)
20959 {
20960 try_array_deduction (tparms, targs, xparms);
20961 tried_array_deduction = true;
20962 if (TREE_VEC_ELT (targs, i))
20963 continue;
20964 }
20965
20966 /* If this is an undeduced nontype parameter that depends on
20967 a type parameter, try another pass; its type may have been
20968 deduced from a later argument than the one from which
20969 this parameter can be deduced. */
20970 if (TREE_CODE (tparm) == PARM_DECL
20971 && uses_template_parms (TREE_TYPE (tparm))
20972 && saw_undeduced < 2)
20973 {
20974 saw_undeduced = 1;
20975 continue;
20976 }
20977
20978 /* Core issue #226 (C++0x) [temp.deduct]:
20979
20980 If a template argument has not been deduced, its
20981 default template argument, if any, is used.
20982
20983 When we are in C++98 mode, TREE_PURPOSE will either
20984 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20985 to explicitly check cxx_dialect here. */
20986 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20987 /* OK, there is a default argument. Wait until after the
20988 conversion check to do substitution. */
20989 continue;
20990
20991 /* If the type parameter is a parameter pack, then it will
20992 be deduced to an empty parameter pack. */
20993 if (template_parameter_pack_p (tparm))
20994 {
20995 tree arg;
20996
20997 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20998 {
20999 arg = make_node (NONTYPE_ARGUMENT_PACK);
21000 TREE_CONSTANT (arg) = 1;
21001 }
21002 else
21003 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21004
21005 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21006
21007 TREE_VEC_ELT (targs, i) = arg;
21008 continue;
21009 }
21010
21011 return unify_parameter_deduction_failure (explain_p, tparm);
21012 }
21013
21014 /* Now substitute into the default template arguments. */
21015 for (i = 0; i < ntparms; i++)
21016 {
21017 tree targ = TREE_VEC_ELT (targs, i);
21018 tree tparm = TREE_VEC_ELT (tparms, i);
21019
21020 if (targ || tparm == error_mark_node)
21021 continue;
21022 tree parm = TREE_VALUE (tparm);
21023 tree arg = TREE_PURPOSE (tparm);
21024 reopen_deferring_access_checks (*checks);
21025 location_t save_loc = input_location;
21026 if (DECL_P (parm))
21027 input_location = DECL_SOURCE_LOCATION (parm);
21028
21029 if (saw_undeduced == 1
21030 && TREE_CODE (parm) == PARM_DECL
21031 && uses_template_parms (TREE_TYPE (parm)))
21032 {
21033 /* The type of this non-type parameter depends on undeduced
21034 parameters. Don't try to use its default argument yet,
21035 since we might deduce an argument for it on the next pass,
21036 but do check whether the arguments we already have cause
21037 substitution failure, so that that happens before we try
21038 later default arguments (78489). */
21039 ++processing_template_decl;
21040 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21041 NULL_TREE);
21042 --processing_template_decl;
21043 if (type == error_mark_node)
21044 arg = error_mark_node;
21045 else
21046 arg = NULL_TREE;
21047 }
21048 else
21049 {
21050 /* Even if the call is happening in template context, getting
21051 here means it's non-dependent, and a default argument is
21052 considered a separate definition under [temp.decls], so we can
21053 do this substitution without processing_template_decl. This
21054 is important if the default argument contains something that
21055 might be instantiation-dependent like access (87480). */
21056 processing_template_decl_sentinel s;
21057 tree substed = NULL_TREE;
21058 if (saw_undeduced == 1)
21059 {
21060 /* First instatiate in template context, in case we still
21061 depend on undeduced template parameters. */
21062 ++processing_template_decl;
21063 substed = tsubst_template_arg (arg, full_targs, complain,
21064 NULL_TREE);
21065 --processing_template_decl;
21066 if (substed != error_mark_node
21067 && !uses_template_parms (substed))
21068 /* We replaced all the tparms, substitute again out of
21069 template context. */
21070 substed = NULL_TREE;
21071 }
21072 if (!substed)
21073 substed = tsubst_template_arg (arg, full_targs, complain,
21074 NULL_TREE);
21075
21076 if (!uses_template_parms (substed))
21077 arg = convert_template_argument (parm, substed, full_targs,
21078 complain, i, NULL_TREE);
21079 else if (saw_undeduced == 1)
21080 arg = NULL_TREE;
21081 else
21082 arg = error_mark_node;
21083 }
21084
21085 input_location = save_loc;
21086 *checks = get_deferred_access_checks ();
21087 pop_deferring_access_checks ();
21088
21089 if (arg == error_mark_node)
21090 return 1;
21091 else if (arg)
21092 {
21093 TREE_VEC_ELT (targs, i) = arg;
21094 /* The position of the first default template argument,
21095 is also the number of non-defaulted arguments in TARGS.
21096 Record that. */
21097 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21098 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21099 }
21100 }
21101
21102 if (saw_undeduced++ == 1)
21103 goto again;
21104 }
21105
21106 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21107 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21108
21109 return unify_success (explain_p);
21110 }
21111
21112 /* Subroutine of type_unification_real. Args are like the variables
21113 at the call site. ARG is an overloaded function (or template-id);
21114 we try deducing template args from each of the overloads, and if
21115 only one succeeds, we go with that. Modifies TARGS and returns
21116 true on success. */
21117
21118 static bool
21119 resolve_overloaded_unification (tree tparms,
21120 tree targs,
21121 tree parm,
21122 tree arg,
21123 unification_kind_t strict,
21124 int sub_strict,
21125 bool explain_p)
21126 {
21127 tree tempargs = copy_node (targs);
21128 int good = 0;
21129 tree goodfn = NULL_TREE;
21130 bool addr_p;
21131
21132 if (TREE_CODE (arg) == ADDR_EXPR)
21133 {
21134 arg = TREE_OPERAND (arg, 0);
21135 addr_p = true;
21136 }
21137 else
21138 addr_p = false;
21139
21140 if (TREE_CODE (arg) == COMPONENT_REF)
21141 /* Handle `&x' where `x' is some static or non-static member
21142 function name. */
21143 arg = TREE_OPERAND (arg, 1);
21144
21145 if (TREE_CODE (arg) == OFFSET_REF)
21146 arg = TREE_OPERAND (arg, 1);
21147
21148 /* Strip baselink information. */
21149 if (BASELINK_P (arg))
21150 arg = BASELINK_FUNCTIONS (arg);
21151
21152 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21153 {
21154 /* If we got some explicit template args, we need to plug them into
21155 the affected templates before we try to unify, in case the
21156 explicit args will completely resolve the templates in question. */
21157
21158 int ok = 0;
21159 tree expl_subargs = TREE_OPERAND (arg, 1);
21160 arg = TREE_OPERAND (arg, 0);
21161
21162 for (lkp_iterator iter (arg); iter; ++iter)
21163 {
21164 tree fn = *iter;
21165 tree subargs, elem;
21166
21167 if (TREE_CODE (fn) != TEMPLATE_DECL)
21168 continue;
21169
21170 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21171 expl_subargs, NULL_TREE, tf_none,
21172 /*require_all_args=*/true,
21173 /*use_default_args=*/true);
21174 if (subargs != error_mark_node
21175 && !any_dependent_template_arguments_p (subargs))
21176 {
21177 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21178 if (try_one_overload (tparms, targs, tempargs, parm,
21179 elem, strict, sub_strict, addr_p, explain_p)
21180 && (!goodfn || !same_type_p (goodfn, elem)))
21181 {
21182 goodfn = elem;
21183 ++good;
21184 }
21185 }
21186 else if (subargs)
21187 ++ok;
21188 }
21189 /* If no templates (or more than one) are fully resolved by the
21190 explicit arguments, this template-id is a non-deduced context; it
21191 could still be OK if we deduce all template arguments for the
21192 enclosing call through other arguments. */
21193 if (good != 1)
21194 good = ok;
21195 }
21196 else if (TREE_CODE (arg) != OVERLOAD
21197 && TREE_CODE (arg) != FUNCTION_DECL)
21198 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21199 -- but the deduction does not succeed because the expression is
21200 not just the function on its own. */
21201 return false;
21202 else
21203 for (lkp_iterator iter (arg); iter; ++iter)
21204 {
21205 tree fn = *iter;
21206 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21207 strict, sub_strict, addr_p, explain_p)
21208 && (!goodfn || !decls_match (goodfn, fn)))
21209 {
21210 goodfn = fn;
21211 ++good;
21212 }
21213 }
21214
21215 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21216 to function or pointer to member function argument if the set of
21217 overloaded functions does not contain function templates and at most
21218 one of a set of overloaded functions provides a unique match.
21219
21220 So if we found multiple possibilities, we return success but don't
21221 deduce anything. */
21222
21223 if (good == 1)
21224 {
21225 int i = TREE_VEC_LENGTH (targs);
21226 for (; i--; )
21227 if (TREE_VEC_ELT (tempargs, i))
21228 {
21229 tree old = TREE_VEC_ELT (targs, i);
21230 tree new_ = TREE_VEC_ELT (tempargs, i);
21231 if (new_ && old && ARGUMENT_PACK_P (old)
21232 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21233 /* Don't forget explicit template arguments in a pack. */
21234 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21235 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21236 TREE_VEC_ELT (targs, i) = new_;
21237 }
21238 }
21239 if (good)
21240 return true;
21241
21242 return false;
21243 }
21244
21245 /* Core DR 115: In contexts where deduction is done and fails, or in
21246 contexts where deduction is not done, if a template argument list is
21247 specified and it, along with any default template arguments, identifies
21248 a single function template specialization, then the template-id is an
21249 lvalue for the function template specialization. */
21250
21251 tree
21252 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21253 {
21254 tree expr, offset, baselink;
21255 bool addr;
21256
21257 if (!type_unknown_p (orig_expr))
21258 return orig_expr;
21259
21260 expr = orig_expr;
21261 addr = false;
21262 offset = NULL_TREE;
21263 baselink = NULL_TREE;
21264
21265 if (TREE_CODE (expr) == ADDR_EXPR)
21266 {
21267 expr = TREE_OPERAND (expr, 0);
21268 addr = true;
21269 }
21270 if (TREE_CODE (expr) == OFFSET_REF)
21271 {
21272 offset = expr;
21273 expr = TREE_OPERAND (expr, 1);
21274 }
21275 if (BASELINK_P (expr))
21276 {
21277 baselink = expr;
21278 expr = BASELINK_FUNCTIONS (expr);
21279 }
21280
21281 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21282 {
21283 int good = 0;
21284 tree goodfn = NULL_TREE;
21285
21286 /* If we got some explicit template args, we need to plug them into
21287 the affected templates before we try to unify, in case the
21288 explicit args will completely resolve the templates in question. */
21289
21290 tree expl_subargs = TREE_OPERAND (expr, 1);
21291 tree arg = TREE_OPERAND (expr, 0);
21292 tree badfn = NULL_TREE;
21293 tree badargs = NULL_TREE;
21294
21295 for (lkp_iterator iter (arg); iter; ++iter)
21296 {
21297 tree fn = *iter;
21298 tree subargs, elem;
21299
21300 if (TREE_CODE (fn) != TEMPLATE_DECL)
21301 continue;
21302
21303 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21304 expl_subargs, NULL_TREE, tf_none,
21305 /*require_all_args=*/true,
21306 /*use_default_args=*/true);
21307 if (subargs != error_mark_node
21308 && !any_dependent_template_arguments_p (subargs))
21309 {
21310 elem = instantiate_template (fn, subargs, tf_none);
21311 if (elem == error_mark_node)
21312 {
21313 badfn = fn;
21314 badargs = subargs;
21315 }
21316 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21317 {
21318 goodfn = elem;
21319 ++good;
21320 }
21321 }
21322 }
21323 if (good == 1)
21324 {
21325 mark_used (goodfn);
21326 expr = goodfn;
21327 if (baselink)
21328 expr = build_baselink (BASELINK_BINFO (baselink),
21329 BASELINK_ACCESS_BINFO (baselink),
21330 expr, BASELINK_OPTYPE (baselink));
21331 if (offset)
21332 {
21333 tree base
21334 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21335 expr = build_offset_ref (base, expr, addr, complain);
21336 }
21337 if (addr)
21338 expr = cp_build_addr_expr (expr, complain);
21339 return expr;
21340 }
21341 else if (good == 0 && badargs && (complain & tf_error))
21342 /* There were no good options and at least one bad one, so let the
21343 user know what the problem is. */
21344 instantiate_template (badfn, badargs, complain);
21345 }
21346 return orig_expr;
21347 }
21348
21349 /* As above, but error out if the expression remains overloaded. */
21350
21351 tree
21352 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21353 {
21354 exp = resolve_nondeduced_context (exp, complain);
21355 if (type_unknown_p (exp))
21356 {
21357 if (complain & tf_error)
21358 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21359 return error_mark_node;
21360 }
21361 return exp;
21362 }
21363
21364 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21365 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21366 different overloads deduce different arguments for a given parm.
21367 ADDR_P is true if the expression for which deduction is being
21368 performed was of the form "& fn" rather than simply "fn".
21369
21370 Returns 1 on success. */
21371
21372 static int
21373 try_one_overload (tree tparms,
21374 tree orig_targs,
21375 tree targs,
21376 tree parm,
21377 tree arg,
21378 unification_kind_t strict,
21379 int sub_strict,
21380 bool addr_p,
21381 bool explain_p)
21382 {
21383 int nargs;
21384 tree tempargs;
21385 int i;
21386
21387 if (arg == error_mark_node)
21388 return 0;
21389
21390 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21391 to function or pointer to member function argument if the set of
21392 overloaded functions does not contain function templates and at most
21393 one of a set of overloaded functions provides a unique match.
21394
21395 So if this is a template, just return success. */
21396
21397 if (uses_template_parms (arg))
21398 return 1;
21399
21400 if (TREE_CODE (arg) == METHOD_TYPE)
21401 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21402 else if (addr_p)
21403 arg = build_pointer_type (arg);
21404
21405 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21406
21407 /* We don't copy orig_targs for this because if we have already deduced
21408 some template args from previous args, unify would complain when we
21409 try to deduce a template parameter for the same argument, even though
21410 there isn't really a conflict. */
21411 nargs = TREE_VEC_LENGTH (targs);
21412 tempargs = make_tree_vec (nargs);
21413
21414 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21415 return 0;
21416
21417 /* First make sure we didn't deduce anything that conflicts with
21418 explicitly specified args. */
21419 for (i = nargs; i--; )
21420 {
21421 tree elt = TREE_VEC_ELT (tempargs, i);
21422 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21423
21424 if (!elt)
21425 /*NOP*/;
21426 else if (uses_template_parms (elt))
21427 /* Since we're unifying against ourselves, we will fill in
21428 template args used in the function parm list with our own
21429 template parms. Discard them. */
21430 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21431 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21432 {
21433 /* Check that the argument at each index of the deduced argument pack
21434 is equivalent to the corresponding explicitly specified argument.
21435 We may have deduced more arguments than were explicitly specified,
21436 and that's OK. */
21437
21438 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21439 that's wrong if we deduce the same argument pack from multiple
21440 function arguments: it's only incomplete the first time. */
21441
21442 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21443 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21444
21445 if (TREE_VEC_LENGTH (deduced_pack)
21446 < TREE_VEC_LENGTH (explicit_pack))
21447 return 0;
21448
21449 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21450 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21451 TREE_VEC_ELT (deduced_pack, j)))
21452 return 0;
21453 }
21454 else if (oldelt && !template_args_equal (oldelt, elt))
21455 return 0;
21456 }
21457
21458 for (i = nargs; i--; )
21459 {
21460 tree elt = TREE_VEC_ELT (tempargs, i);
21461
21462 if (elt)
21463 TREE_VEC_ELT (targs, i) = elt;
21464 }
21465
21466 return 1;
21467 }
21468
21469 /* PARM is a template class (perhaps with unbound template
21470 parameters). ARG is a fully instantiated type. If ARG can be
21471 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21472 TARGS are as for unify. */
21473
21474 static tree
21475 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21476 bool explain_p)
21477 {
21478 tree copy_of_targs;
21479
21480 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21481 return NULL_TREE;
21482 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21483 /* Matches anything. */;
21484 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21485 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21486 return NULL_TREE;
21487
21488 /* We need to make a new template argument vector for the call to
21489 unify. If we used TARGS, we'd clutter it up with the result of
21490 the attempted unification, even if this class didn't work out.
21491 We also don't want to commit ourselves to all the unifications
21492 we've already done, since unification is supposed to be done on
21493 an argument-by-argument basis. In other words, consider the
21494 following pathological case:
21495
21496 template <int I, int J, int K>
21497 struct S {};
21498
21499 template <int I, int J>
21500 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21501
21502 template <int I, int J, int K>
21503 void f(S<I, J, K>, S<I, I, I>);
21504
21505 void g() {
21506 S<0, 0, 0> s0;
21507 S<0, 1, 2> s2;
21508
21509 f(s0, s2);
21510 }
21511
21512 Now, by the time we consider the unification involving `s2', we
21513 already know that we must have `f<0, 0, 0>'. But, even though
21514 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21515 because there are two ways to unify base classes of S<0, 1, 2>
21516 with S<I, I, I>. If we kept the already deduced knowledge, we
21517 would reject the possibility I=1. */
21518 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21519
21520 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21521 {
21522 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21523 return NULL_TREE;
21524 return arg;
21525 }
21526
21527 /* If unification failed, we're done. */
21528 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21529 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21530 return NULL_TREE;
21531
21532 return arg;
21533 }
21534
21535 /* Given a template type PARM and a class type ARG, find the unique
21536 base type in ARG that is an instance of PARM. We do not examine
21537 ARG itself; only its base-classes. If there is not exactly one
21538 appropriate base class, return NULL_TREE. PARM may be the type of
21539 a partial specialization, as well as a plain template type. Used
21540 by unify. */
21541
21542 static enum template_base_result
21543 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21544 bool explain_p, tree *result)
21545 {
21546 tree rval = NULL_TREE;
21547 tree binfo;
21548
21549 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21550
21551 binfo = TYPE_BINFO (complete_type (arg));
21552 if (!binfo)
21553 {
21554 /* The type could not be completed. */
21555 *result = NULL_TREE;
21556 return tbr_incomplete_type;
21557 }
21558
21559 /* Walk in inheritance graph order. The search order is not
21560 important, and this avoids multiple walks of virtual bases. */
21561 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21562 {
21563 tree r = try_class_unification (tparms, targs, parm,
21564 BINFO_TYPE (binfo), explain_p);
21565
21566 if (r)
21567 {
21568 /* If there is more than one satisfactory baseclass, then:
21569
21570 [temp.deduct.call]
21571
21572 If they yield more than one possible deduced A, the type
21573 deduction fails.
21574
21575 applies. */
21576 if (rval && !same_type_p (r, rval))
21577 {
21578 *result = NULL_TREE;
21579 return tbr_ambiguous_baseclass;
21580 }
21581
21582 rval = r;
21583 }
21584 }
21585
21586 *result = rval;
21587 return tbr_success;
21588 }
21589
21590 /* Returns the level of DECL, which declares a template parameter. */
21591
21592 static int
21593 template_decl_level (tree decl)
21594 {
21595 switch (TREE_CODE (decl))
21596 {
21597 case TYPE_DECL:
21598 case TEMPLATE_DECL:
21599 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21600
21601 case PARM_DECL:
21602 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21603
21604 default:
21605 gcc_unreachable ();
21606 }
21607 return 0;
21608 }
21609
21610 /* Decide whether ARG can be unified with PARM, considering only the
21611 cv-qualifiers of each type, given STRICT as documented for unify.
21612 Returns nonzero iff the unification is OK on that basis. */
21613
21614 static int
21615 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21616 {
21617 int arg_quals = cp_type_quals (arg);
21618 int parm_quals = cp_type_quals (parm);
21619
21620 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21621 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21622 {
21623 /* Although a CVR qualifier is ignored when being applied to a
21624 substituted template parameter ([8.3.2]/1 for example), that
21625 does not allow us to unify "const T" with "int&" because both
21626 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21627 It is ok when we're allowing additional CV qualifiers
21628 at the outer level [14.8.2.1]/3,1st bullet. */
21629 if ((TYPE_REF_P (arg)
21630 || FUNC_OR_METHOD_TYPE_P (arg))
21631 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21632 return 0;
21633
21634 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21635 && (parm_quals & TYPE_QUAL_RESTRICT))
21636 return 0;
21637 }
21638
21639 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21640 && (arg_quals & parm_quals) != parm_quals)
21641 return 0;
21642
21643 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21644 && (parm_quals & arg_quals) != arg_quals)
21645 return 0;
21646
21647 return 1;
21648 }
21649
21650 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21651 void
21652 template_parm_level_and_index (tree parm, int* level, int* index)
21653 {
21654 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21655 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21656 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21657 {
21658 *index = TEMPLATE_TYPE_IDX (parm);
21659 *level = TEMPLATE_TYPE_LEVEL (parm);
21660 }
21661 else
21662 {
21663 *index = TEMPLATE_PARM_IDX (parm);
21664 *level = TEMPLATE_PARM_LEVEL (parm);
21665 }
21666 }
21667
21668 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21669 do { \
21670 if (unify (TP, TA, P, A, S, EP)) \
21671 return 1; \
21672 } while (0)
21673
21674 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21675 expansion at the end of PACKED_PARMS. Returns 0 if the type
21676 deduction succeeds, 1 otherwise. STRICT is the same as in
21677 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21678 function call argument list. We'll need to adjust the arguments to make them
21679 types. SUBR tells us if this is from a recursive call to
21680 type_unification_real, or for comparing two template argument
21681 lists. */
21682
21683 static int
21684 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21685 tree packed_args, unification_kind_t strict,
21686 bool subr, bool explain_p)
21687 {
21688 tree parm
21689 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21690 tree pattern = PACK_EXPANSION_PATTERN (parm);
21691 tree pack, packs = NULL_TREE;
21692 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21693
21694 /* Add in any args remembered from an earlier partial instantiation. */
21695 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21696 int levels = TMPL_ARGS_DEPTH (targs);
21697
21698 packed_args = expand_template_argument_pack (packed_args);
21699
21700 int len = TREE_VEC_LENGTH (packed_args);
21701
21702 /* Determine the parameter packs we will be deducing from the
21703 pattern, and record their current deductions. */
21704 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21705 pack; pack = TREE_CHAIN (pack))
21706 {
21707 tree parm_pack = TREE_VALUE (pack);
21708 int idx, level;
21709
21710 /* Only template parameter packs can be deduced, not e.g. function
21711 parameter packs or __bases or __integer_pack. */
21712 if (!TEMPLATE_PARM_P (parm_pack))
21713 continue;
21714
21715 /* Determine the index and level of this parameter pack. */
21716 template_parm_level_and_index (parm_pack, &level, &idx);
21717 if (level < levels)
21718 continue;
21719
21720 /* Keep track of the parameter packs and their corresponding
21721 argument packs. */
21722 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21723 TREE_TYPE (packs) = make_tree_vec (len - start);
21724 }
21725
21726 /* Loop through all of the arguments that have not yet been
21727 unified and unify each with the pattern. */
21728 for (i = start; i < len; i++)
21729 {
21730 tree parm;
21731 bool any_explicit = false;
21732 tree arg = TREE_VEC_ELT (packed_args, i);
21733
21734 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21735 or the element of its argument pack at the current index if
21736 this argument was explicitly specified. */
21737 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21738 {
21739 int idx, level;
21740 tree arg, pargs;
21741 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21742
21743 arg = NULL_TREE;
21744 if (TREE_VALUE (pack)
21745 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21746 && (i - start < TREE_VEC_LENGTH (pargs)))
21747 {
21748 any_explicit = true;
21749 arg = TREE_VEC_ELT (pargs, i - start);
21750 }
21751 TMPL_ARG (targs, level, idx) = arg;
21752 }
21753
21754 /* If we had explicit template arguments, substitute them into the
21755 pattern before deduction. */
21756 if (any_explicit)
21757 {
21758 /* Some arguments might still be unspecified or dependent. */
21759 bool dependent;
21760 ++processing_template_decl;
21761 dependent = any_dependent_template_arguments_p (targs);
21762 if (!dependent)
21763 --processing_template_decl;
21764 parm = tsubst (pattern, targs,
21765 explain_p ? tf_warning_or_error : tf_none,
21766 NULL_TREE);
21767 if (dependent)
21768 --processing_template_decl;
21769 if (parm == error_mark_node)
21770 return 1;
21771 }
21772 else
21773 parm = pattern;
21774
21775 /* Unify the pattern with the current argument. */
21776 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21777 explain_p))
21778 return 1;
21779
21780 /* For each parameter pack, collect the deduced value. */
21781 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21782 {
21783 int idx, level;
21784 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21785
21786 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21787 TMPL_ARG (targs, level, idx);
21788 }
21789 }
21790
21791 /* Verify that the results of unification with the parameter packs
21792 produce results consistent with what we've seen before, and make
21793 the deduced argument packs available. */
21794 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21795 {
21796 tree old_pack = TREE_VALUE (pack);
21797 tree new_args = TREE_TYPE (pack);
21798 int i, len = TREE_VEC_LENGTH (new_args);
21799 int idx, level;
21800 bool nondeduced_p = false;
21801
21802 /* By default keep the original deduced argument pack.
21803 If necessary, more specific code is going to update the
21804 resulting deduced argument later down in this function. */
21805 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21806 TMPL_ARG (targs, level, idx) = old_pack;
21807
21808 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21809 actually deduce anything. */
21810 for (i = 0; i < len && !nondeduced_p; ++i)
21811 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21812 nondeduced_p = true;
21813 if (nondeduced_p)
21814 continue;
21815
21816 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21817 {
21818 /* If we had fewer function args than explicit template args,
21819 just use the explicits. */
21820 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21821 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21822 if (len < explicit_len)
21823 new_args = explicit_args;
21824 }
21825
21826 if (!old_pack)
21827 {
21828 tree result;
21829 /* Build the deduced *_ARGUMENT_PACK. */
21830 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21831 {
21832 result = make_node (NONTYPE_ARGUMENT_PACK);
21833 TREE_CONSTANT (result) = 1;
21834 }
21835 else
21836 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21837
21838 SET_ARGUMENT_PACK_ARGS (result, new_args);
21839
21840 /* Note the deduced argument packs for this parameter
21841 pack. */
21842 TMPL_ARG (targs, level, idx) = result;
21843 }
21844 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21845 && (ARGUMENT_PACK_ARGS (old_pack)
21846 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21847 {
21848 /* We only had the explicitly-provided arguments before, but
21849 now we have a complete set of arguments. */
21850 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21851
21852 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21853 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21854 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21855 }
21856 else
21857 {
21858 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21859 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21860
21861 if (!comp_template_args (old_args, new_args,
21862 &bad_old_arg, &bad_new_arg))
21863 /* Inconsistent unification of this parameter pack. */
21864 return unify_parameter_pack_inconsistent (explain_p,
21865 bad_old_arg,
21866 bad_new_arg);
21867 }
21868 }
21869
21870 return unify_success (explain_p);
21871 }
21872
21873 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21874 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21875 parameters and return value are as for unify. */
21876
21877 static int
21878 unify_array_domain (tree tparms, tree targs,
21879 tree parm_dom, tree arg_dom,
21880 bool explain_p)
21881 {
21882 tree parm_max;
21883 tree arg_max;
21884 bool parm_cst;
21885 bool arg_cst;
21886
21887 /* Our representation of array types uses "N - 1" as the
21888 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21889 not an integer constant. We cannot unify arbitrarily
21890 complex expressions, so we eliminate the MINUS_EXPRs
21891 here. */
21892 parm_max = TYPE_MAX_VALUE (parm_dom);
21893 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21894 if (!parm_cst)
21895 {
21896 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21897 parm_max = TREE_OPERAND (parm_max, 0);
21898 }
21899 arg_max = TYPE_MAX_VALUE (arg_dom);
21900 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21901 if (!arg_cst)
21902 {
21903 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21904 trying to unify the type of a variable with the type
21905 of a template parameter. For example:
21906
21907 template <unsigned int N>
21908 void f (char (&) [N]);
21909 int g();
21910 void h(int i) {
21911 char a[g(i)];
21912 f(a);
21913 }
21914
21915 Here, the type of the ARG will be "int [g(i)]", and
21916 may be a SAVE_EXPR, etc. */
21917 if (TREE_CODE (arg_max) != MINUS_EXPR)
21918 return unify_vla_arg (explain_p, arg_dom);
21919 arg_max = TREE_OPERAND (arg_max, 0);
21920 }
21921
21922 /* If only one of the bounds used a MINUS_EXPR, compensate
21923 by adding one to the other bound. */
21924 if (parm_cst && !arg_cst)
21925 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21926 integer_type_node,
21927 parm_max,
21928 integer_one_node);
21929 else if (arg_cst && !parm_cst)
21930 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21931 integer_type_node,
21932 arg_max,
21933 integer_one_node);
21934
21935 return unify (tparms, targs, parm_max, arg_max,
21936 UNIFY_ALLOW_INTEGER, explain_p);
21937 }
21938
21939 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21940
21941 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21942
21943 static pa_kind_t
21944 pa_kind (tree t)
21945 {
21946 if (PACK_EXPANSION_P (t))
21947 t = PACK_EXPANSION_PATTERN (t);
21948 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21949 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21950 || DECL_TYPE_TEMPLATE_P (t))
21951 return pa_tmpl;
21952 else if (TYPE_P (t))
21953 return pa_type;
21954 else
21955 return pa_expr;
21956 }
21957
21958 /* Deduce the value of template parameters. TPARMS is the (innermost)
21959 set of template parameters to a template. TARGS is the bindings
21960 for those template parameters, as determined thus far; TARGS may
21961 include template arguments for outer levels of template parameters
21962 as well. PARM is a parameter to a template function, or a
21963 subcomponent of that parameter; ARG is the corresponding argument.
21964 This function attempts to match PARM with ARG in a manner
21965 consistent with the existing assignments in TARGS. If more values
21966 are deduced, then TARGS is updated.
21967
21968 Returns 0 if the type deduction succeeds, 1 otherwise. The
21969 parameter STRICT is a bitwise or of the following flags:
21970
21971 UNIFY_ALLOW_NONE:
21972 Require an exact match between PARM and ARG.
21973 UNIFY_ALLOW_MORE_CV_QUAL:
21974 Allow the deduced ARG to be more cv-qualified (by qualification
21975 conversion) than ARG.
21976 UNIFY_ALLOW_LESS_CV_QUAL:
21977 Allow the deduced ARG to be less cv-qualified than ARG.
21978 UNIFY_ALLOW_DERIVED:
21979 Allow the deduced ARG to be a template base class of ARG,
21980 or a pointer to a template base class of the type pointed to by
21981 ARG.
21982 UNIFY_ALLOW_INTEGER:
21983 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21984 case for more information.
21985 UNIFY_ALLOW_OUTER_LEVEL:
21986 This is the outermost level of a deduction. Used to determine validity
21987 of qualification conversions. A valid qualification conversion must
21988 have const qualified pointers leading up to the inner type which
21989 requires additional CV quals, except at the outer level, where const
21990 is not required [conv.qual]. It would be normal to set this flag in
21991 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21992 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21993 This is the outermost level of a deduction, and PARM can be more CV
21994 qualified at this point.
21995 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21996 This is the outermost level of a deduction, and PARM can be less CV
21997 qualified at this point. */
21998
21999 static int
22000 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22001 bool explain_p)
22002 {
22003 int idx;
22004 tree targ;
22005 tree tparm;
22006 int strict_in = strict;
22007 tsubst_flags_t complain = (explain_p
22008 ? tf_warning_or_error
22009 : tf_none);
22010
22011 /* I don't think this will do the right thing with respect to types.
22012 But the only case I've seen it in so far has been array bounds, where
22013 signedness is the only information lost, and I think that will be
22014 okay. */
22015 while (CONVERT_EXPR_P (parm))
22016 parm = TREE_OPERAND (parm, 0);
22017
22018 if (arg == error_mark_node)
22019 return unify_invalid (explain_p);
22020 if (arg == unknown_type_node
22021 || arg == init_list_type_node)
22022 /* We can't deduce anything from this, but we might get all the
22023 template args from other function args. */
22024 return unify_success (explain_p);
22025
22026 if (parm == any_targ_node || arg == any_targ_node)
22027 return unify_success (explain_p);
22028
22029 /* If PARM uses template parameters, then we can't bail out here,
22030 even if ARG == PARM, since we won't record unifications for the
22031 template parameters. We might need them if we're trying to
22032 figure out which of two things is more specialized. */
22033 if (arg == parm && !uses_template_parms (parm))
22034 return unify_success (explain_p);
22035
22036 /* Handle init lists early, so the rest of the function can assume
22037 we're dealing with a type. */
22038 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22039 {
22040 tree elt, elttype;
22041 unsigned i;
22042 tree orig_parm = parm;
22043
22044 /* Replace T with std::initializer_list<T> for deduction. */
22045 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22046 && flag_deduce_init_list)
22047 parm = listify (parm);
22048
22049 if (!is_std_init_list (parm)
22050 && TREE_CODE (parm) != ARRAY_TYPE)
22051 /* We can only deduce from an initializer list argument if the
22052 parameter is std::initializer_list or an array; otherwise this
22053 is a non-deduced context. */
22054 return unify_success (explain_p);
22055
22056 if (TREE_CODE (parm) == ARRAY_TYPE)
22057 elttype = TREE_TYPE (parm);
22058 else
22059 {
22060 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22061 /* Deduction is defined in terms of a single type, so just punt
22062 on the (bizarre) std::initializer_list<T...>. */
22063 if (PACK_EXPANSION_P (elttype))
22064 return unify_success (explain_p);
22065 }
22066
22067 if (strict != DEDUCE_EXACT
22068 && TYPE_P (elttype)
22069 && !uses_deducible_template_parms (elttype))
22070 /* If ELTTYPE has no deducible template parms, skip deduction from
22071 the list elements. */;
22072 else
22073 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22074 {
22075 int elt_strict = strict;
22076
22077 if (elt == error_mark_node)
22078 return unify_invalid (explain_p);
22079
22080 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22081 {
22082 tree type = TREE_TYPE (elt);
22083 if (type == error_mark_node)
22084 return unify_invalid (explain_p);
22085 /* It should only be possible to get here for a call. */
22086 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22087 elt_strict |= maybe_adjust_types_for_deduction
22088 (DEDUCE_CALL, &elttype, &type, elt);
22089 elt = type;
22090 }
22091
22092 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22093 explain_p);
22094 }
22095
22096 if (TREE_CODE (parm) == ARRAY_TYPE
22097 && deducible_array_bound (TYPE_DOMAIN (parm)))
22098 {
22099 /* Also deduce from the length of the initializer list. */
22100 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22101 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22102 if (idx == error_mark_node)
22103 return unify_invalid (explain_p);
22104 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22105 idx, explain_p);
22106 }
22107
22108 /* If the std::initializer_list<T> deduction worked, replace the
22109 deduced A with std::initializer_list<A>. */
22110 if (orig_parm != parm)
22111 {
22112 idx = TEMPLATE_TYPE_IDX (orig_parm);
22113 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22114 targ = listify (targ);
22115 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22116 }
22117 return unify_success (explain_p);
22118 }
22119
22120 /* If parm and arg aren't the same kind of thing (template, type, or
22121 expression), fail early. */
22122 if (pa_kind (parm) != pa_kind (arg))
22123 return unify_invalid (explain_p);
22124
22125 /* Immediately reject some pairs that won't unify because of
22126 cv-qualification mismatches. */
22127 if (TREE_CODE (arg) == TREE_CODE (parm)
22128 && TYPE_P (arg)
22129 /* It is the elements of the array which hold the cv quals of an array
22130 type, and the elements might be template type parms. We'll check
22131 when we recurse. */
22132 && TREE_CODE (arg) != ARRAY_TYPE
22133 /* We check the cv-qualifiers when unifying with template type
22134 parameters below. We want to allow ARG `const T' to unify with
22135 PARM `T' for example, when computing which of two templates
22136 is more specialized, for example. */
22137 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22138 && !check_cv_quals_for_unify (strict_in, arg, parm))
22139 return unify_cv_qual_mismatch (explain_p, parm, arg);
22140
22141 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22142 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22143 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22144 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22145 strict &= ~UNIFY_ALLOW_DERIVED;
22146 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22147 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22148
22149 switch (TREE_CODE (parm))
22150 {
22151 case TYPENAME_TYPE:
22152 case SCOPE_REF:
22153 case UNBOUND_CLASS_TEMPLATE:
22154 /* In a type which contains a nested-name-specifier, template
22155 argument values cannot be deduced for template parameters used
22156 within the nested-name-specifier. */
22157 return unify_success (explain_p);
22158
22159 case TEMPLATE_TYPE_PARM:
22160 case TEMPLATE_TEMPLATE_PARM:
22161 case BOUND_TEMPLATE_TEMPLATE_PARM:
22162 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22163 if (error_operand_p (tparm))
22164 return unify_invalid (explain_p);
22165
22166 if (TEMPLATE_TYPE_LEVEL (parm)
22167 != template_decl_level (tparm))
22168 /* The PARM is not one we're trying to unify. Just check
22169 to see if it matches ARG. */
22170 {
22171 if (TREE_CODE (arg) == TREE_CODE (parm)
22172 && (is_auto (parm) ? is_auto (arg)
22173 : same_type_p (parm, arg)))
22174 return unify_success (explain_p);
22175 else
22176 return unify_type_mismatch (explain_p, parm, arg);
22177 }
22178 idx = TEMPLATE_TYPE_IDX (parm);
22179 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22180 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22181 if (error_operand_p (tparm))
22182 return unify_invalid (explain_p);
22183
22184 /* Check for mixed types and values. */
22185 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22186 && TREE_CODE (tparm) != TYPE_DECL)
22187 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22188 && TREE_CODE (tparm) != TEMPLATE_DECL))
22189 gcc_unreachable ();
22190
22191 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22192 {
22193 if ((strict_in & UNIFY_ALLOW_DERIVED)
22194 && CLASS_TYPE_P (arg))
22195 {
22196 /* First try to match ARG directly. */
22197 tree t = try_class_unification (tparms, targs, parm, arg,
22198 explain_p);
22199 if (!t)
22200 {
22201 /* Otherwise, look for a suitable base of ARG, as below. */
22202 enum template_base_result r;
22203 r = get_template_base (tparms, targs, parm, arg,
22204 explain_p, &t);
22205 if (!t)
22206 return unify_no_common_base (explain_p, r, parm, arg);
22207 arg = t;
22208 }
22209 }
22210 /* ARG must be constructed from a template class or a template
22211 template parameter. */
22212 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22213 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22214 return unify_template_deduction_failure (explain_p, parm, arg);
22215
22216 /* Deduce arguments T, i from TT<T> or TT<i>. */
22217 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22218 return 1;
22219
22220 arg = TYPE_TI_TEMPLATE (arg);
22221
22222 /* Fall through to deduce template name. */
22223 }
22224
22225 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22226 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22227 {
22228 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22229
22230 /* Simple cases: Value already set, does match or doesn't. */
22231 if (targ != NULL_TREE && template_args_equal (targ, arg))
22232 return unify_success (explain_p);
22233 else if (targ)
22234 return unify_inconsistency (explain_p, parm, targ, arg);
22235 }
22236 else
22237 {
22238 /* If PARM is `const T' and ARG is only `int', we don't have
22239 a match unless we are allowing additional qualification.
22240 If ARG is `const int' and PARM is just `T' that's OK;
22241 that binds `const int' to `T'. */
22242 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22243 arg, parm))
22244 return unify_cv_qual_mismatch (explain_p, parm, arg);
22245
22246 /* Consider the case where ARG is `const volatile int' and
22247 PARM is `const T'. Then, T should be `volatile int'. */
22248 arg = cp_build_qualified_type_real
22249 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22250 if (arg == error_mark_node)
22251 return unify_invalid (explain_p);
22252
22253 /* Simple cases: Value already set, does match or doesn't. */
22254 if (targ != NULL_TREE && same_type_p (targ, arg))
22255 return unify_success (explain_p);
22256 else if (targ)
22257 return unify_inconsistency (explain_p, parm, targ, arg);
22258
22259 /* Make sure that ARG is not a variable-sized array. (Note
22260 that were talking about variable-sized arrays (like
22261 `int[n]'), rather than arrays of unknown size (like
22262 `int[]').) We'll get very confused by such a type since
22263 the bound of the array is not constant, and therefore
22264 not mangleable. Besides, such types are not allowed in
22265 ISO C++, so we can do as we please here. We do allow
22266 them for 'auto' deduction, since that isn't ABI-exposed. */
22267 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22268 return unify_vla_arg (explain_p, arg);
22269
22270 /* Strip typedefs as in convert_template_argument. */
22271 arg = canonicalize_type_argument (arg, tf_none);
22272 }
22273
22274 /* If ARG is a parameter pack or an expansion, we cannot unify
22275 against it unless PARM is also a parameter pack. */
22276 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22277 && !template_parameter_pack_p (parm))
22278 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22279
22280 /* If the argument deduction results is a METHOD_TYPE,
22281 then there is a problem.
22282 METHOD_TYPE doesn't map to any real C++ type the result of
22283 the deduction cannot be of that type. */
22284 if (TREE_CODE (arg) == METHOD_TYPE)
22285 return unify_method_type_error (explain_p, arg);
22286
22287 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22288 return unify_success (explain_p);
22289
22290 case TEMPLATE_PARM_INDEX:
22291 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22292 if (error_operand_p (tparm))
22293 return unify_invalid (explain_p);
22294
22295 if (TEMPLATE_PARM_LEVEL (parm)
22296 != template_decl_level (tparm))
22297 {
22298 /* The PARM is not one we're trying to unify. Just check
22299 to see if it matches ARG. */
22300 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22301 && cp_tree_equal (parm, arg));
22302 if (result)
22303 unify_expression_unequal (explain_p, parm, arg);
22304 return result;
22305 }
22306
22307 idx = TEMPLATE_PARM_IDX (parm);
22308 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22309
22310 if (targ)
22311 {
22312 if ((strict & UNIFY_ALLOW_INTEGER)
22313 && TREE_TYPE (targ) && TREE_TYPE (arg)
22314 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22315 /* We're deducing from an array bound, the type doesn't matter. */
22316 arg = fold_convert (TREE_TYPE (targ), arg);
22317 int x = !cp_tree_equal (targ, arg);
22318 if (x)
22319 unify_inconsistency (explain_p, parm, targ, arg);
22320 return x;
22321 }
22322
22323 /* [temp.deduct.type] If, in the declaration of a function template
22324 with a non-type template-parameter, the non-type
22325 template-parameter is used in an expression in the function
22326 parameter-list and, if the corresponding template-argument is
22327 deduced, the template-argument type shall match the type of the
22328 template-parameter exactly, except that a template-argument
22329 deduced from an array bound may be of any integral type.
22330 The non-type parameter might use already deduced type parameters. */
22331 tparm = TREE_TYPE (parm);
22332 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22333 /* We don't have enough levels of args to do any substitution. This
22334 can happen in the context of -fnew-ttp-matching. */;
22335 else
22336 {
22337 ++processing_template_decl;
22338 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22339 --processing_template_decl;
22340
22341 if (tree a = type_uses_auto (tparm))
22342 {
22343 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22344 if (tparm == error_mark_node)
22345 return 1;
22346 }
22347 }
22348
22349 if (!TREE_TYPE (arg))
22350 /* Template-parameter dependent expression. Just accept it for now.
22351 It will later be processed in convert_template_argument. */
22352 ;
22353 else if (same_type_p (non_reference (TREE_TYPE (arg)),
22354 non_reference (tparm)))
22355 /* OK */;
22356 else if ((strict & UNIFY_ALLOW_INTEGER)
22357 && CP_INTEGRAL_TYPE_P (tparm))
22358 /* Convert the ARG to the type of PARM; the deduced non-type
22359 template argument must exactly match the types of the
22360 corresponding parameter. */
22361 arg = fold (build_nop (tparm, arg));
22362 else if (uses_template_parms (tparm))
22363 {
22364 /* We haven't deduced the type of this parameter yet. */
22365 if (cxx_dialect >= cxx17
22366 /* We deduce from array bounds in try_array_deduction. */
22367 && !(strict & UNIFY_ALLOW_INTEGER))
22368 {
22369 /* Deduce it from the non-type argument. */
22370 tree atype = TREE_TYPE (arg);
22371 RECUR_AND_CHECK_FAILURE (tparms, targs,
22372 tparm, atype,
22373 UNIFY_ALLOW_NONE, explain_p);
22374 }
22375 else
22376 /* Try again later. */
22377 return unify_success (explain_p);
22378 }
22379 else
22380 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22381
22382 /* If ARG is a parameter pack or an expansion, we cannot unify
22383 against it unless PARM is also a parameter pack. */
22384 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22385 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22386 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22387
22388 {
22389 bool removed_attr = false;
22390 arg = strip_typedefs_expr (arg, &removed_attr);
22391 }
22392 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22393 return unify_success (explain_p);
22394
22395 case PTRMEM_CST:
22396 {
22397 /* A pointer-to-member constant can be unified only with
22398 another constant. */
22399 if (TREE_CODE (arg) != PTRMEM_CST)
22400 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22401
22402 /* Just unify the class member. It would be useless (and possibly
22403 wrong, depending on the strict flags) to unify also
22404 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22405 arg refer to the same variable, even if through different
22406 classes. For instance:
22407
22408 struct A { int x; };
22409 struct B : A { };
22410
22411 Unification of &A::x and &B::x must succeed. */
22412 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22413 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22414 }
22415
22416 case POINTER_TYPE:
22417 {
22418 if (!TYPE_PTR_P (arg))
22419 return unify_type_mismatch (explain_p, parm, arg);
22420
22421 /* [temp.deduct.call]
22422
22423 A can be another pointer or pointer to member type that can
22424 be converted to the deduced A via a qualification
22425 conversion (_conv.qual_).
22426
22427 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22428 This will allow for additional cv-qualification of the
22429 pointed-to types if appropriate. */
22430
22431 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22432 /* The derived-to-base conversion only persists through one
22433 level of pointers. */
22434 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22435
22436 return unify (tparms, targs, TREE_TYPE (parm),
22437 TREE_TYPE (arg), strict, explain_p);
22438 }
22439
22440 case REFERENCE_TYPE:
22441 if (!TYPE_REF_P (arg))
22442 return unify_type_mismatch (explain_p, parm, arg);
22443 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22444 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22445
22446 case ARRAY_TYPE:
22447 if (TREE_CODE (arg) != ARRAY_TYPE)
22448 return unify_type_mismatch (explain_p, parm, arg);
22449 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22450 != (TYPE_DOMAIN (arg) == NULL_TREE))
22451 return unify_type_mismatch (explain_p, parm, arg);
22452 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22453 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22454 if (TYPE_DOMAIN (parm) != NULL_TREE)
22455 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22456 TYPE_DOMAIN (arg), explain_p);
22457 return unify_success (explain_p);
22458
22459 case REAL_TYPE:
22460 case COMPLEX_TYPE:
22461 case VECTOR_TYPE:
22462 case INTEGER_TYPE:
22463 case BOOLEAN_TYPE:
22464 case ENUMERAL_TYPE:
22465 case VOID_TYPE:
22466 case NULLPTR_TYPE:
22467 if (TREE_CODE (arg) != TREE_CODE (parm))
22468 return unify_type_mismatch (explain_p, parm, arg);
22469
22470 /* We have already checked cv-qualification at the top of the
22471 function. */
22472 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22473 return unify_type_mismatch (explain_p, parm, arg);
22474
22475 /* As far as unification is concerned, this wins. Later checks
22476 will invalidate it if necessary. */
22477 return unify_success (explain_p);
22478
22479 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22480 /* Type INTEGER_CST can come from ordinary constant template args. */
22481 case INTEGER_CST:
22482 while (CONVERT_EXPR_P (arg))
22483 arg = TREE_OPERAND (arg, 0);
22484
22485 if (TREE_CODE (arg) != INTEGER_CST)
22486 return unify_template_argument_mismatch (explain_p, parm, arg);
22487 return (tree_int_cst_equal (parm, arg)
22488 ? unify_success (explain_p)
22489 : unify_template_argument_mismatch (explain_p, parm, arg));
22490
22491 case TREE_VEC:
22492 {
22493 int i, len, argslen;
22494 int parm_variadic_p = 0;
22495
22496 if (TREE_CODE (arg) != TREE_VEC)
22497 return unify_template_argument_mismatch (explain_p, parm, arg);
22498
22499 len = TREE_VEC_LENGTH (parm);
22500 argslen = TREE_VEC_LENGTH (arg);
22501
22502 /* Check for pack expansions in the parameters. */
22503 for (i = 0; i < len; ++i)
22504 {
22505 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22506 {
22507 if (i == len - 1)
22508 /* We can unify against something with a trailing
22509 parameter pack. */
22510 parm_variadic_p = 1;
22511 else
22512 /* [temp.deduct.type]/9: If the template argument list of
22513 P contains a pack expansion that is not the last
22514 template argument, the entire template argument list
22515 is a non-deduced context. */
22516 return unify_success (explain_p);
22517 }
22518 }
22519
22520 /* If we don't have enough arguments to satisfy the parameters
22521 (not counting the pack expression at the end), or we have
22522 too many arguments for a parameter list that doesn't end in
22523 a pack expression, we can't unify. */
22524 if (parm_variadic_p
22525 ? argslen < len - parm_variadic_p
22526 : argslen != len)
22527 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22528
22529 /* Unify all of the parameters that precede the (optional)
22530 pack expression. */
22531 for (i = 0; i < len - parm_variadic_p; ++i)
22532 {
22533 RECUR_AND_CHECK_FAILURE (tparms, targs,
22534 TREE_VEC_ELT (parm, i),
22535 TREE_VEC_ELT (arg, i),
22536 UNIFY_ALLOW_NONE, explain_p);
22537 }
22538 if (parm_variadic_p)
22539 return unify_pack_expansion (tparms, targs, parm, arg,
22540 DEDUCE_EXACT,
22541 /*subr=*/true, explain_p);
22542 return unify_success (explain_p);
22543 }
22544
22545 case RECORD_TYPE:
22546 case UNION_TYPE:
22547 if (TREE_CODE (arg) != TREE_CODE (parm))
22548 return unify_type_mismatch (explain_p, parm, arg);
22549
22550 if (TYPE_PTRMEMFUNC_P (parm))
22551 {
22552 if (!TYPE_PTRMEMFUNC_P (arg))
22553 return unify_type_mismatch (explain_p, parm, arg);
22554
22555 return unify (tparms, targs,
22556 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22557 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22558 strict, explain_p);
22559 }
22560 else if (TYPE_PTRMEMFUNC_P (arg))
22561 return unify_type_mismatch (explain_p, parm, arg);
22562
22563 if (CLASSTYPE_TEMPLATE_INFO (parm))
22564 {
22565 tree t = NULL_TREE;
22566
22567 if (strict_in & UNIFY_ALLOW_DERIVED)
22568 {
22569 /* First, we try to unify the PARM and ARG directly. */
22570 t = try_class_unification (tparms, targs,
22571 parm, arg, explain_p);
22572
22573 if (!t)
22574 {
22575 /* Fallback to the special case allowed in
22576 [temp.deduct.call]:
22577
22578 If P is a class, and P has the form
22579 template-id, then A can be a derived class of
22580 the deduced A. Likewise, if P is a pointer to
22581 a class of the form template-id, A can be a
22582 pointer to a derived class pointed to by the
22583 deduced A. */
22584 enum template_base_result r;
22585 r = get_template_base (tparms, targs, parm, arg,
22586 explain_p, &t);
22587
22588 if (!t)
22589 {
22590 /* Don't give the derived diagnostic if we're
22591 already dealing with the same template. */
22592 bool same_template
22593 = (CLASSTYPE_TEMPLATE_INFO (arg)
22594 && (CLASSTYPE_TI_TEMPLATE (parm)
22595 == CLASSTYPE_TI_TEMPLATE (arg)));
22596 return unify_no_common_base (explain_p && !same_template,
22597 r, parm, arg);
22598 }
22599 }
22600 }
22601 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22602 && (CLASSTYPE_TI_TEMPLATE (parm)
22603 == CLASSTYPE_TI_TEMPLATE (arg)))
22604 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22605 Then, we should unify `int' and `U'. */
22606 t = arg;
22607 else
22608 /* There's no chance of unification succeeding. */
22609 return unify_type_mismatch (explain_p, parm, arg);
22610
22611 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22612 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22613 }
22614 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22615 return unify_type_mismatch (explain_p, parm, arg);
22616 return unify_success (explain_p);
22617
22618 case METHOD_TYPE:
22619 case FUNCTION_TYPE:
22620 {
22621 unsigned int nargs;
22622 tree *args;
22623 tree a;
22624 unsigned int i;
22625
22626 if (TREE_CODE (arg) != TREE_CODE (parm))
22627 return unify_type_mismatch (explain_p, parm, arg);
22628
22629 /* CV qualifications for methods can never be deduced, they must
22630 match exactly. We need to check them explicitly here,
22631 because type_unification_real treats them as any other
22632 cv-qualified parameter. */
22633 if (TREE_CODE (parm) == METHOD_TYPE
22634 && (!check_cv_quals_for_unify
22635 (UNIFY_ALLOW_NONE,
22636 class_of_this_parm (arg),
22637 class_of_this_parm (parm))))
22638 return unify_cv_qual_mismatch (explain_p, parm, arg);
22639 if (TREE_CODE (arg) == FUNCTION_TYPE
22640 && type_memfn_quals (parm) != type_memfn_quals (arg))
22641 return unify_cv_qual_mismatch (explain_p, parm, arg);
22642 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22643 return unify_type_mismatch (explain_p, parm, arg);
22644
22645 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22646 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22647
22648 nargs = list_length (TYPE_ARG_TYPES (arg));
22649 args = XALLOCAVEC (tree, nargs);
22650 for (a = TYPE_ARG_TYPES (arg), i = 0;
22651 a != NULL_TREE && a != void_list_node;
22652 a = TREE_CHAIN (a), ++i)
22653 args[i] = TREE_VALUE (a);
22654 nargs = i;
22655
22656 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22657 args, nargs, 1, DEDUCE_EXACT,
22658 NULL, explain_p))
22659 return 1;
22660
22661 if (flag_noexcept_type)
22662 {
22663 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22664 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22665 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22666 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22667 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22668 && uses_template_parms (TREE_PURPOSE (pspec)))
22669 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22670 TREE_PURPOSE (aspec),
22671 UNIFY_ALLOW_NONE, explain_p);
22672 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22673 return unify_type_mismatch (explain_p, parm, arg);
22674 }
22675
22676 return 0;
22677 }
22678
22679 case OFFSET_TYPE:
22680 /* Unify a pointer to member with a pointer to member function, which
22681 deduces the type of the member as a function type. */
22682 if (TYPE_PTRMEMFUNC_P (arg))
22683 {
22684 /* Check top-level cv qualifiers */
22685 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22686 return unify_cv_qual_mismatch (explain_p, parm, arg);
22687
22688 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22689 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22690 UNIFY_ALLOW_NONE, explain_p);
22691
22692 /* Determine the type of the function we are unifying against. */
22693 tree fntype = static_fn_type (arg);
22694
22695 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22696 }
22697
22698 if (TREE_CODE (arg) != OFFSET_TYPE)
22699 return unify_type_mismatch (explain_p, parm, arg);
22700 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22701 TYPE_OFFSET_BASETYPE (arg),
22702 UNIFY_ALLOW_NONE, explain_p);
22703 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22704 strict, explain_p);
22705
22706 case CONST_DECL:
22707 if (DECL_TEMPLATE_PARM_P (parm))
22708 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22709 if (arg != scalar_constant_value (parm))
22710 return unify_template_argument_mismatch (explain_p, parm, arg);
22711 return unify_success (explain_p);
22712
22713 case FIELD_DECL:
22714 case TEMPLATE_DECL:
22715 /* Matched cases are handled by the ARG == PARM test above. */
22716 return unify_template_argument_mismatch (explain_p, parm, arg);
22717
22718 case VAR_DECL:
22719 /* We might get a variable as a non-type template argument in parm if the
22720 corresponding parameter is type-dependent. Make any necessary
22721 adjustments based on whether arg is a reference. */
22722 if (CONSTANT_CLASS_P (arg))
22723 parm = fold_non_dependent_expr (parm, complain);
22724 else if (REFERENCE_REF_P (arg))
22725 {
22726 tree sub = TREE_OPERAND (arg, 0);
22727 STRIP_NOPS (sub);
22728 if (TREE_CODE (sub) == ADDR_EXPR)
22729 arg = TREE_OPERAND (sub, 0);
22730 }
22731 /* Now use the normal expression code to check whether they match. */
22732 goto expr;
22733
22734 case TYPE_ARGUMENT_PACK:
22735 case NONTYPE_ARGUMENT_PACK:
22736 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22737 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22738
22739 case TYPEOF_TYPE:
22740 case DECLTYPE_TYPE:
22741 case UNDERLYING_TYPE:
22742 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22743 or UNDERLYING_TYPE nodes. */
22744 return unify_success (explain_p);
22745
22746 case ERROR_MARK:
22747 /* Unification fails if we hit an error node. */
22748 return unify_invalid (explain_p);
22749
22750 case INDIRECT_REF:
22751 if (REFERENCE_REF_P (parm))
22752 {
22753 bool pexp = PACK_EXPANSION_P (arg);
22754 if (pexp)
22755 arg = PACK_EXPANSION_PATTERN (arg);
22756 if (REFERENCE_REF_P (arg))
22757 arg = TREE_OPERAND (arg, 0);
22758 if (pexp)
22759 arg = make_pack_expansion (arg, complain);
22760 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22761 strict, explain_p);
22762 }
22763 /* FALLTHRU */
22764
22765 default:
22766 /* An unresolved overload is a nondeduced context. */
22767 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22768 return unify_success (explain_p);
22769 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22770 expr:
22771 /* We must be looking at an expression. This can happen with
22772 something like:
22773
22774 template <int I>
22775 void foo(S<I>, S<I + 2>);
22776
22777 This is a "nondeduced context":
22778
22779 [deduct.type]
22780
22781 The nondeduced contexts are:
22782
22783 --A type that is a template-id in which one or more of
22784 the template-arguments is an expression that references
22785 a template-parameter.
22786
22787 In these cases, we assume deduction succeeded, but don't
22788 actually infer any unifications. */
22789
22790 if (!uses_template_parms (parm)
22791 && !template_args_equal (parm, arg))
22792 return unify_expression_unequal (explain_p, parm, arg);
22793 else
22794 return unify_success (explain_p);
22795 }
22796 }
22797 #undef RECUR_AND_CHECK_FAILURE
22798 \f
22799 /* Note that DECL can be defined in this translation unit, if
22800 required. */
22801
22802 static void
22803 mark_definable (tree decl)
22804 {
22805 tree clone;
22806 DECL_NOT_REALLY_EXTERN (decl) = 1;
22807 FOR_EACH_CLONE (clone, decl)
22808 DECL_NOT_REALLY_EXTERN (clone) = 1;
22809 }
22810
22811 /* Called if RESULT is explicitly instantiated, or is a member of an
22812 explicitly instantiated class. */
22813
22814 void
22815 mark_decl_instantiated (tree result, int extern_p)
22816 {
22817 SET_DECL_EXPLICIT_INSTANTIATION (result);
22818
22819 /* If this entity has already been written out, it's too late to
22820 make any modifications. */
22821 if (TREE_ASM_WRITTEN (result))
22822 return;
22823
22824 /* For anonymous namespace we don't need to do anything. */
22825 if (decl_anon_ns_mem_p (result))
22826 {
22827 gcc_assert (!TREE_PUBLIC (result));
22828 return;
22829 }
22830
22831 if (TREE_CODE (result) != FUNCTION_DECL)
22832 /* The TREE_PUBLIC flag for function declarations will have been
22833 set correctly by tsubst. */
22834 TREE_PUBLIC (result) = 1;
22835
22836 /* This might have been set by an earlier implicit instantiation. */
22837 DECL_COMDAT (result) = 0;
22838
22839 if (extern_p)
22840 DECL_NOT_REALLY_EXTERN (result) = 0;
22841 else
22842 {
22843 mark_definable (result);
22844 mark_needed (result);
22845 /* Always make artificials weak. */
22846 if (DECL_ARTIFICIAL (result) && flag_weak)
22847 comdat_linkage (result);
22848 /* For WIN32 we also want to put explicit instantiations in
22849 linkonce sections. */
22850 else if (TREE_PUBLIC (result))
22851 maybe_make_one_only (result);
22852 if (TREE_CODE (result) == FUNCTION_DECL
22853 && DECL_TEMPLATE_INSTANTIATED (result))
22854 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22855 since start_preparsed_function wouldn't have if we had an earlier
22856 extern explicit instantiation. */
22857 DECL_EXTERNAL (result) = 0;
22858 }
22859
22860 /* If EXTERN_P, then this function will not be emitted -- unless
22861 followed by an explicit instantiation, at which point its linkage
22862 will be adjusted. If !EXTERN_P, then this function will be
22863 emitted here. In neither circumstance do we want
22864 import_export_decl to adjust the linkage. */
22865 DECL_INTERFACE_KNOWN (result) = 1;
22866 }
22867
22868 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22869 important template arguments. If any are missing, we check whether
22870 they're important by using error_mark_node for substituting into any
22871 args that were used for partial ordering (the ones between ARGS and END)
22872 and seeing if it bubbles up. */
22873
22874 static bool
22875 check_undeduced_parms (tree targs, tree args, tree end)
22876 {
22877 bool found = false;
22878 int i;
22879 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22880 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22881 {
22882 found = true;
22883 TREE_VEC_ELT (targs, i) = error_mark_node;
22884 }
22885 if (found)
22886 {
22887 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22888 if (substed == error_mark_node)
22889 return true;
22890 }
22891 return false;
22892 }
22893
22894 /* Given two function templates PAT1 and PAT2, return:
22895
22896 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22897 -1 if PAT2 is more specialized than PAT1.
22898 0 if neither is more specialized.
22899
22900 LEN indicates the number of parameters we should consider
22901 (defaulted parameters should not be considered).
22902
22903 The 1998 std underspecified function template partial ordering, and
22904 DR214 addresses the issue. We take pairs of arguments, one from
22905 each of the templates, and deduce them against each other. One of
22906 the templates will be more specialized if all the *other*
22907 template's arguments deduce against its arguments and at least one
22908 of its arguments *does* *not* deduce against the other template's
22909 corresponding argument. Deduction is done as for class templates.
22910 The arguments used in deduction have reference and top level cv
22911 qualifiers removed. Iff both arguments were originally reference
22912 types *and* deduction succeeds in both directions, an lvalue reference
22913 wins against an rvalue reference and otherwise the template
22914 with the more cv-qualified argument wins for that pairing (if
22915 neither is more cv-qualified, they both are equal). Unlike regular
22916 deduction, after all the arguments have been deduced in this way,
22917 we do *not* verify the deduced template argument values can be
22918 substituted into non-deduced contexts.
22919
22920 The logic can be a bit confusing here, because we look at deduce1 and
22921 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22922 can find template arguments for pat1 to make arg1 look like arg2, that
22923 means that arg2 is at least as specialized as arg1. */
22924
22925 int
22926 more_specialized_fn (tree pat1, tree pat2, int len)
22927 {
22928 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22929 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22930 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22931 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22932 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22933 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22934 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22935 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22936 tree origs1, origs2;
22937 bool lose1 = false;
22938 bool lose2 = false;
22939
22940 /* Remove the this parameter from non-static member functions. If
22941 one is a non-static member function and the other is not a static
22942 member function, remove the first parameter from that function
22943 also. This situation occurs for operator functions where we
22944 locate both a member function (with this pointer) and non-member
22945 operator (with explicit first operand). */
22946 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22947 {
22948 len--; /* LEN is the number of significant arguments for DECL1 */
22949 args1 = TREE_CHAIN (args1);
22950 if (!DECL_STATIC_FUNCTION_P (decl2))
22951 args2 = TREE_CHAIN (args2);
22952 }
22953 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22954 {
22955 args2 = TREE_CHAIN (args2);
22956 if (!DECL_STATIC_FUNCTION_P (decl1))
22957 {
22958 len--;
22959 args1 = TREE_CHAIN (args1);
22960 }
22961 }
22962
22963 /* If only one is a conversion operator, they are unordered. */
22964 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22965 return 0;
22966
22967 /* Consider the return type for a conversion function */
22968 if (DECL_CONV_FN_P (decl1))
22969 {
22970 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22971 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22972 len++;
22973 }
22974
22975 processing_template_decl++;
22976
22977 origs1 = args1;
22978 origs2 = args2;
22979
22980 while (len--
22981 /* Stop when an ellipsis is seen. */
22982 && args1 != NULL_TREE && args2 != NULL_TREE)
22983 {
22984 tree arg1 = TREE_VALUE (args1);
22985 tree arg2 = TREE_VALUE (args2);
22986 int deduce1, deduce2;
22987 int quals1 = -1;
22988 int quals2 = -1;
22989 int ref1 = 0;
22990 int ref2 = 0;
22991
22992 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22993 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22994 {
22995 /* When both arguments are pack expansions, we need only
22996 unify the patterns themselves. */
22997 arg1 = PACK_EXPANSION_PATTERN (arg1);
22998 arg2 = PACK_EXPANSION_PATTERN (arg2);
22999
23000 /* This is the last comparison we need to do. */
23001 len = 0;
23002 }
23003
23004 /* DR 1847: If a particular P contains no template-parameters that
23005 participate in template argument deduction, that P is not used to
23006 determine the ordering. */
23007 if (!uses_deducible_template_parms (arg1)
23008 && !uses_deducible_template_parms (arg2))
23009 goto next;
23010
23011 if (TYPE_REF_P (arg1))
23012 {
23013 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23014 arg1 = TREE_TYPE (arg1);
23015 quals1 = cp_type_quals (arg1);
23016 }
23017
23018 if (TYPE_REF_P (arg2))
23019 {
23020 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23021 arg2 = TREE_TYPE (arg2);
23022 quals2 = cp_type_quals (arg2);
23023 }
23024
23025 arg1 = TYPE_MAIN_VARIANT (arg1);
23026 arg2 = TYPE_MAIN_VARIANT (arg2);
23027
23028 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23029 {
23030 int i, len2 = remaining_arguments (args2);
23031 tree parmvec = make_tree_vec (1);
23032 tree argvec = make_tree_vec (len2);
23033 tree ta = args2;
23034
23035 /* Setup the parameter vector, which contains only ARG1. */
23036 TREE_VEC_ELT (parmvec, 0) = arg1;
23037
23038 /* Setup the argument vector, which contains the remaining
23039 arguments. */
23040 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23041 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23042
23043 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23044 argvec, DEDUCE_EXACT,
23045 /*subr=*/true, /*explain_p=*/false)
23046 == 0);
23047
23048 /* We cannot deduce in the other direction, because ARG1 is
23049 a pack expansion but ARG2 is not. */
23050 deduce2 = 0;
23051 }
23052 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23053 {
23054 int i, len1 = remaining_arguments (args1);
23055 tree parmvec = make_tree_vec (1);
23056 tree argvec = make_tree_vec (len1);
23057 tree ta = args1;
23058
23059 /* Setup the parameter vector, which contains only ARG1. */
23060 TREE_VEC_ELT (parmvec, 0) = arg2;
23061
23062 /* Setup the argument vector, which contains the remaining
23063 arguments. */
23064 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23065 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23066
23067 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23068 argvec, DEDUCE_EXACT,
23069 /*subr=*/true, /*explain_p=*/false)
23070 == 0);
23071
23072 /* We cannot deduce in the other direction, because ARG2 is
23073 a pack expansion but ARG1 is not.*/
23074 deduce1 = 0;
23075 }
23076
23077 else
23078 {
23079 /* The normal case, where neither argument is a pack
23080 expansion. */
23081 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23082 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23083 == 0);
23084 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23085 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23086 == 0);
23087 }
23088
23089 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23090 arg2, then arg2 is not as specialized as arg1. */
23091 if (!deduce1)
23092 lose2 = true;
23093 if (!deduce2)
23094 lose1 = true;
23095
23096 /* "If, for a given type, deduction succeeds in both directions
23097 (i.e., the types are identical after the transformations above)
23098 and both P and A were reference types (before being replaced with
23099 the type referred to above):
23100 - if the type from the argument template was an lvalue reference and
23101 the type from the parameter template was not, the argument type is
23102 considered to be more specialized than the other; otherwise,
23103 - if the type from the argument template is more cv-qualified
23104 than the type from the parameter template (as described above),
23105 the argument type is considered to be more specialized than the other;
23106 otherwise,
23107 - neither type is more specialized than the other." */
23108
23109 if (deduce1 && deduce2)
23110 {
23111 if (ref1 && ref2 && ref1 != ref2)
23112 {
23113 if (ref1 > ref2)
23114 lose1 = true;
23115 else
23116 lose2 = true;
23117 }
23118 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23119 {
23120 if ((quals1 & quals2) == quals2)
23121 lose2 = true;
23122 if ((quals1 & quals2) == quals1)
23123 lose1 = true;
23124 }
23125 }
23126
23127 if (lose1 && lose2)
23128 /* We've failed to deduce something in either direction.
23129 These must be unordered. */
23130 break;
23131
23132 next:
23133
23134 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23135 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23136 /* We have already processed all of the arguments in our
23137 handing of the pack expansion type. */
23138 len = 0;
23139
23140 args1 = TREE_CHAIN (args1);
23141 args2 = TREE_CHAIN (args2);
23142 }
23143
23144 /* "In most cases, all template parameters must have values in order for
23145 deduction to succeed, but for partial ordering purposes a template
23146 parameter may remain without a value provided it is not used in the
23147 types being used for partial ordering."
23148
23149 Thus, if we are missing any of the targs1 we need to substitute into
23150 origs1, then pat2 is not as specialized as pat1. This can happen when
23151 there is a nondeduced context. */
23152 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23153 lose2 = true;
23154 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23155 lose1 = true;
23156
23157 processing_template_decl--;
23158
23159 /* If both deductions succeed, the partial ordering selects the more
23160 constrained template. */
23161 if (!lose1 && !lose2)
23162 {
23163 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23164 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23165 lose1 = !subsumes_constraints (c1, c2);
23166 lose2 = !subsumes_constraints (c2, c1);
23167 }
23168
23169 /* All things being equal, if the next argument is a pack expansion
23170 for one function but not for the other, prefer the
23171 non-variadic function. FIXME this is bogus; see c++/41958. */
23172 if (lose1 == lose2
23173 && args1 && TREE_VALUE (args1)
23174 && args2 && TREE_VALUE (args2))
23175 {
23176 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23177 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23178 }
23179
23180 if (lose1 == lose2)
23181 return 0;
23182 else if (!lose1)
23183 return 1;
23184 else
23185 return -1;
23186 }
23187
23188 /* Determine which of two partial specializations of TMPL is more
23189 specialized.
23190
23191 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23192 to the first partial specialization. The TREE_PURPOSE is the
23193 innermost set of template parameters for the partial
23194 specialization. PAT2 is similar, but for the second template.
23195
23196 Return 1 if the first partial specialization is more specialized;
23197 -1 if the second is more specialized; 0 if neither is more
23198 specialized.
23199
23200 See [temp.class.order] for information about determining which of
23201 two templates is more specialized. */
23202
23203 static int
23204 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23205 {
23206 tree targs;
23207 int winner = 0;
23208 bool any_deductions = false;
23209
23210 tree tmpl1 = TREE_VALUE (pat1);
23211 tree tmpl2 = TREE_VALUE (pat2);
23212 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23213 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23214
23215 /* Just like what happens for functions, if we are ordering between
23216 different template specializations, we may encounter dependent
23217 types in the arguments, and we need our dependency check functions
23218 to behave correctly. */
23219 ++processing_template_decl;
23220 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23221 if (targs)
23222 {
23223 --winner;
23224 any_deductions = true;
23225 }
23226
23227 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23228 if (targs)
23229 {
23230 ++winner;
23231 any_deductions = true;
23232 }
23233 --processing_template_decl;
23234
23235 /* If both deductions succeed, the partial ordering selects the more
23236 constrained template. */
23237 if (!winner && any_deductions)
23238 return more_constrained (tmpl1, tmpl2);
23239
23240 /* In the case of a tie where at least one of the templates
23241 has a parameter pack at the end, the template with the most
23242 non-packed parameters wins. */
23243 if (winner == 0
23244 && any_deductions
23245 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23246 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23247 {
23248 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23249 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23250 int len1 = TREE_VEC_LENGTH (args1);
23251 int len2 = TREE_VEC_LENGTH (args2);
23252
23253 /* We don't count the pack expansion at the end. */
23254 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23255 --len1;
23256 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23257 --len2;
23258
23259 if (len1 > len2)
23260 return 1;
23261 else if (len1 < len2)
23262 return -1;
23263 }
23264
23265 return winner;
23266 }
23267
23268 /* Return the template arguments that will produce the function signature
23269 DECL from the function template FN, with the explicit template
23270 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23271 also match. Return NULL_TREE if no satisfactory arguments could be
23272 found. */
23273
23274 static tree
23275 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23276 {
23277 int ntparms = DECL_NTPARMS (fn);
23278 tree targs = make_tree_vec (ntparms);
23279 tree decl_type = TREE_TYPE (decl);
23280 tree decl_arg_types;
23281 tree *args;
23282 unsigned int nargs, ix;
23283 tree arg;
23284
23285 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23286
23287 /* Never do unification on the 'this' parameter. */
23288 decl_arg_types = skip_artificial_parms_for (decl,
23289 TYPE_ARG_TYPES (decl_type));
23290
23291 nargs = list_length (decl_arg_types);
23292 args = XALLOCAVEC (tree, nargs);
23293 for (arg = decl_arg_types, ix = 0;
23294 arg != NULL_TREE && arg != void_list_node;
23295 arg = TREE_CHAIN (arg), ++ix)
23296 args[ix] = TREE_VALUE (arg);
23297
23298 if (fn_type_unification (fn, explicit_args, targs,
23299 args, ix,
23300 (check_rettype || DECL_CONV_FN_P (fn)
23301 ? TREE_TYPE (decl_type) : NULL_TREE),
23302 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23303 /*explain_p=*/false,
23304 /*decltype*/false)
23305 == error_mark_node)
23306 return NULL_TREE;
23307
23308 return targs;
23309 }
23310
23311 /* Return the innermost template arguments that, when applied to a partial
23312 specialization SPEC_TMPL of TMPL, yield the ARGS.
23313
23314 For example, suppose we have:
23315
23316 template <class T, class U> struct S {};
23317 template <class T> struct S<T*, int> {};
23318
23319 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23320 partial specialization and the ARGS will be {double*, int}. The resulting
23321 vector will be {double}, indicating that `T' is bound to `double'. */
23322
23323 static tree
23324 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23325 {
23326 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23327 tree spec_args
23328 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23329 int i, ntparms = TREE_VEC_LENGTH (tparms);
23330 tree deduced_args;
23331 tree innermost_deduced_args;
23332
23333 innermost_deduced_args = make_tree_vec (ntparms);
23334 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23335 {
23336 deduced_args = copy_node (args);
23337 SET_TMPL_ARGS_LEVEL (deduced_args,
23338 TMPL_ARGS_DEPTH (deduced_args),
23339 innermost_deduced_args);
23340 }
23341 else
23342 deduced_args = innermost_deduced_args;
23343
23344 bool tried_array_deduction = (cxx_dialect < cxx17);
23345 again:
23346 if (unify (tparms, deduced_args,
23347 INNERMOST_TEMPLATE_ARGS (spec_args),
23348 INNERMOST_TEMPLATE_ARGS (args),
23349 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23350 return NULL_TREE;
23351
23352 for (i = 0; i < ntparms; ++i)
23353 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23354 {
23355 if (!tried_array_deduction)
23356 {
23357 try_array_deduction (tparms, innermost_deduced_args,
23358 INNERMOST_TEMPLATE_ARGS (spec_args));
23359 tried_array_deduction = true;
23360 if (TREE_VEC_ELT (innermost_deduced_args, i))
23361 goto again;
23362 }
23363 return NULL_TREE;
23364 }
23365
23366 if (!push_tinst_level (spec_tmpl, deduced_args))
23367 {
23368 excessive_deduction_depth = true;
23369 return NULL_TREE;
23370 }
23371
23372 /* Verify that nondeduced template arguments agree with the type
23373 obtained from argument deduction.
23374
23375 For example:
23376
23377 struct A { typedef int X; };
23378 template <class T, class U> struct C {};
23379 template <class T> struct C<T, typename T::X> {};
23380
23381 Then with the instantiation `C<A, int>', we can deduce that
23382 `T' is `A' but unify () does not check whether `typename T::X'
23383 is `int'. */
23384 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23385
23386 if (spec_args != error_mark_node)
23387 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23388 INNERMOST_TEMPLATE_ARGS (spec_args),
23389 tmpl, tf_none, false, false);
23390
23391 pop_tinst_level ();
23392
23393 if (spec_args == error_mark_node
23394 /* We only need to check the innermost arguments; the other
23395 arguments will always agree. */
23396 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23397 INNERMOST_TEMPLATE_ARGS (args)))
23398 return NULL_TREE;
23399
23400 /* Now that we have bindings for all of the template arguments,
23401 ensure that the arguments deduced for the template template
23402 parameters have compatible template parameter lists. See the use
23403 of template_template_parm_bindings_ok_p in fn_type_unification
23404 for more information. */
23405 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23406 return NULL_TREE;
23407
23408 return deduced_args;
23409 }
23410
23411 // Compare two function templates T1 and T2 by deducing bindings
23412 // from one against the other. If both deductions succeed, compare
23413 // constraints to see which is more constrained.
23414 static int
23415 more_specialized_inst (tree t1, tree t2)
23416 {
23417 int fate = 0;
23418 int count = 0;
23419
23420 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23421 {
23422 --fate;
23423 ++count;
23424 }
23425
23426 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23427 {
23428 ++fate;
23429 ++count;
23430 }
23431
23432 // If both deductions succeed, then one may be more constrained.
23433 if (count == 2 && fate == 0)
23434 fate = more_constrained (t1, t2);
23435
23436 return fate;
23437 }
23438
23439 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23440 Return the TREE_LIST node with the most specialized template, if
23441 any. If there is no most specialized template, the error_mark_node
23442 is returned.
23443
23444 Note that this function does not look at, or modify, the
23445 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23446 returned is one of the elements of INSTANTIATIONS, callers may
23447 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23448 and retrieve it from the value returned. */
23449
23450 tree
23451 most_specialized_instantiation (tree templates)
23452 {
23453 tree fn, champ;
23454
23455 ++processing_template_decl;
23456
23457 champ = templates;
23458 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23459 {
23460 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23461 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23462 if (fate == -1)
23463 champ = fn;
23464 else if (!fate)
23465 {
23466 /* Equally specialized, move to next function. If there
23467 is no next function, nothing's most specialized. */
23468 fn = TREE_CHAIN (fn);
23469 champ = fn;
23470 if (!fn)
23471 break;
23472 }
23473 }
23474
23475 if (champ)
23476 /* Now verify that champ is better than everything earlier in the
23477 instantiation list. */
23478 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23479 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23480 {
23481 champ = NULL_TREE;
23482 break;
23483 }
23484 }
23485
23486 processing_template_decl--;
23487
23488 if (!champ)
23489 return error_mark_node;
23490
23491 return champ;
23492 }
23493
23494 /* If DECL is a specialization of some template, return the most
23495 general such template. Otherwise, returns NULL_TREE.
23496
23497 For example, given:
23498
23499 template <class T> struct S { template <class U> void f(U); };
23500
23501 if TMPL is `template <class U> void S<int>::f(U)' this will return
23502 the full template. This function will not trace past partial
23503 specializations, however. For example, given in addition:
23504
23505 template <class T> struct S<T*> { template <class U> void f(U); };
23506
23507 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23508 `template <class T> template <class U> S<T*>::f(U)'. */
23509
23510 tree
23511 most_general_template (tree decl)
23512 {
23513 if (TREE_CODE (decl) != TEMPLATE_DECL)
23514 {
23515 if (tree tinfo = get_template_info (decl))
23516 decl = TI_TEMPLATE (tinfo);
23517 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23518 template friend, or a FIELD_DECL for a capture pack. */
23519 if (TREE_CODE (decl) != TEMPLATE_DECL)
23520 return NULL_TREE;
23521 }
23522
23523 /* Look for more and more general templates. */
23524 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23525 {
23526 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23527 (See cp-tree.h for details.) */
23528 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23529 break;
23530
23531 if (CLASS_TYPE_P (TREE_TYPE (decl))
23532 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23533 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23534 break;
23535
23536 /* Stop if we run into an explicitly specialized class template. */
23537 if (!DECL_NAMESPACE_SCOPE_P (decl)
23538 && DECL_CONTEXT (decl)
23539 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23540 break;
23541
23542 decl = DECL_TI_TEMPLATE (decl);
23543 }
23544
23545 return decl;
23546 }
23547
23548 /* Return the most specialized of the template partial specializations
23549 which can produce TARGET, a specialization of some class or variable
23550 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23551 a TEMPLATE_DECL node corresponding to the partial specialization, while
23552 the TREE_PURPOSE is the set of template arguments that must be
23553 substituted into the template pattern in order to generate TARGET.
23554
23555 If the choice of partial specialization is ambiguous, a diagnostic
23556 is issued, and the error_mark_node is returned. If there are no
23557 partial specializations matching TARGET, then NULL_TREE is
23558 returned, indicating that the primary template should be used. */
23559
23560 static tree
23561 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23562 {
23563 tree list = NULL_TREE;
23564 tree t;
23565 tree champ;
23566 int fate;
23567 bool ambiguous_p;
23568 tree outer_args = NULL_TREE;
23569 tree tmpl, args;
23570
23571 if (TYPE_P (target))
23572 {
23573 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23574 tmpl = TI_TEMPLATE (tinfo);
23575 args = TI_ARGS (tinfo);
23576 }
23577 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23578 {
23579 tmpl = TREE_OPERAND (target, 0);
23580 args = TREE_OPERAND (target, 1);
23581 }
23582 else if (VAR_P (target))
23583 {
23584 tree tinfo = DECL_TEMPLATE_INFO (target);
23585 tmpl = TI_TEMPLATE (tinfo);
23586 args = TI_ARGS (tinfo);
23587 }
23588 else
23589 gcc_unreachable ();
23590
23591 tree main_tmpl = most_general_template (tmpl);
23592
23593 /* For determining which partial specialization to use, only the
23594 innermost args are interesting. */
23595 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23596 {
23597 outer_args = strip_innermost_template_args (args, 1);
23598 args = INNERMOST_TEMPLATE_ARGS (args);
23599 }
23600
23601 /* The caller hasn't called push_to_top_level yet, but we need
23602 get_partial_spec_bindings to be done in non-template context so that we'll
23603 fully resolve everything. */
23604 processing_template_decl_sentinel ptds;
23605
23606 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23607 {
23608 tree spec_args;
23609 tree spec_tmpl = TREE_VALUE (t);
23610
23611 if (outer_args)
23612 {
23613 /* Substitute in the template args from the enclosing class. */
23614 ++processing_template_decl;
23615 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23616 --processing_template_decl;
23617 }
23618
23619 if (spec_tmpl == error_mark_node)
23620 return error_mark_node;
23621
23622 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23623 if (spec_args)
23624 {
23625 if (outer_args)
23626 spec_args = add_to_template_args (outer_args, spec_args);
23627
23628 /* Keep the candidate only if the constraints are satisfied,
23629 or if we're not compiling with concepts. */
23630 if (!flag_concepts
23631 || constraints_satisfied_p (spec_tmpl, spec_args))
23632 {
23633 list = tree_cons (spec_args, TREE_VALUE (t), list);
23634 TREE_TYPE (list) = TREE_TYPE (t);
23635 }
23636 }
23637 }
23638
23639 if (! list)
23640 return NULL_TREE;
23641
23642 ambiguous_p = false;
23643 t = list;
23644 champ = t;
23645 t = TREE_CHAIN (t);
23646 for (; t; t = TREE_CHAIN (t))
23647 {
23648 fate = more_specialized_partial_spec (tmpl, champ, t);
23649 if (fate == 1)
23650 ;
23651 else
23652 {
23653 if (fate == 0)
23654 {
23655 t = TREE_CHAIN (t);
23656 if (! t)
23657 {
23658 ambiguous_p = true;
23659 break;
23660 }
23661 }
23662 champ = t;
23663 }
23664 }
23665
23666 if (!ambiguous_p)
23667 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23668 {
23669 fate = more_specialized_partial_spec (tmpl, champ, t);
23670 if (fate != 1)
23671 {
23672 ambiguous_p = true;
23673 break;
23674 }
23675 }
23676
23677 if (ambiguous_p)
23678 {
23679 const char *str;
23680 char *spaces = NULL;
23681 if (!(complain & tf_error))
23682 return error_mark_node;
23683 if (TYPE_P (target))
23684 error ("ambiguous template instantiation for %q#T", target);
23685 else
23686 error ("ambiguous template instantiation for %q#D", target);
23687 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23688 for (t = list; t; t = TREE_CHAIN (t))
23689 {
23690 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23691 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23692 "%s %#qS", spaces ? spaces : str, subst);
23693 spaces = spaces ? spaces : get_spaces (str);
23694 }
23695 free (spaces);
23696 return error_mark_node;
23697 }
23698
23699 return champ;
23700 }
23701
23702 /* Explicitly instantiate DECL. */
23703
23704 void
23705 do_decl_instantiation (tree decl, tree storage)
23706 {
23707 tree result = NULL_TREE;
23708 int extern_p = 0;
23709
23710 if (!decl || decl == error_mark_node)
23711 /* An error occurred, for which grokdeclarator has already issued
23712 an appropriate message. */
23713 return;
23714 else if (! DECL_LANG_SPECIFIC (decl))
23715 {
23716 error ("explicit instantiation of non-template %q#D", decl);
23717 return;
23718 }
23719 else if (DECL_DECLARED_CONCEPT_P (decl))
23720 {
23721 if (VAR_P (decl))
23722 error ("explicit instantiation of variable concept %q#D", decl);
23723 else
23724 error ("explicit instantiation of function concept %q#D", decl);
23725 return;
23726 }
23727
23728 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23729 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23730
23731 if (VAR_P (decl) && !var_templ)
23732 {
23733 /* There is an asymmetry here in the way VAR_DECLs and
23734 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23735 the latter, the DECL we get back will be marked as a
23736 template instantiation, and the appropriate
23737 DECL_TEMPLATE_INFO will be set up. This does not happen for
23738 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23739 should handle VAR_DECLs as it currently handles
23740 FUNCTION_DECLs. */
23741 if (!DECL_CLASS_SCOPE_P (decl))
23742 {
23743 error ("%qD is not a static data member of a class template", decl);
23744 return;
23745 }
23746 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23747 if (!result || !VAR_P (result))
23748 {
23749 error ("no matching template for %qD found", decl);
23750 return;
23751 }
23752 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23753 {
23754 error ("type %qT for explicit instantiation %qD does not match "
23755 "declared type %qT", TREE_TYPE (result), decl,
23756 TREE_TYPE (decl));
23757 return;
23758 }
23759 }
23760 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23761 {
23762 error ("explicit instantiation of %q#D", decl);
23763 return;
23764 }
23765 else
23766 result = decl;
23767
23768 /* Check for various error cases. Note that if the explicit
23769 instantiation is valid the RESULT will currently be marked as an
23770 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23771 until we get here. */
23772
23773 if (DECL_TEMPLATE_SPECIALIZATION (result))
23774 {
23775 /* DR 259 [temp.spec].
23776
23777 Both an explicit instantiation and a declaration of an explicit
23778 specialization shall not appear in a program unless the explicit
23779 instantiation follows a declaration of the explicit specialization.
23780
23781 For a given set of template parameters, if an explicit
23782 instantiation of a template appears after a declaration of an
23783 explicit specialization for that template, the explicit
23784 instantiation has no effect. */
23785 return;
23786 }
23787 else if (DECL_EXPLICIT_INSTANTIATION (result))
23788 {
23789 /* [temp.spec]
23790
23791 No program shall explicitly instantiate any template more
23792 than once.
23793
23794 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23795 the first instantiation was `extern' and the second is not,
23796 and EXTERN_P for the opposite case. */
23797 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23798 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23799 /* If an "extern" explicit instantiation follows an ordinary
23800 explicit instantiation, the template is instantiated. */
23801 if (extern_p)
23802 return;
23803 }
23804 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23805 {
23806 error ("no matching template for %qD found", result);
23807 return;
23808 }
23809 else if (!DECL_TEMPLATE_INFO (result))
23810 {
23811 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23812 return;
23813 }
23814
23815 if (storage == NULL_TREE)
23816 ;
23817 else if (storage == ridpointers[(int) RID_EXTERN])
23818 {
23819 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23820 pedwarn (input_location, OPT_Wpedantic,
23821 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23822 "instantiations");
23823 extern_p = 1;
23824 }
23825 else
23826 error ("storage class %qD applied to template instantiation", storage);
23827
23828 check_explicit_instantiation_namespace (result);
23829 mark_decl_instantiated (result, extern_p);
23830 if (! extern_p)
23831 instantiate_decl (result, /*defer_ok=*/true,
23832 /*expl_inst_class_mem_p=*/false);
23833 }
23834
23835 static void
23836 mark_class_instantiated (tree t, int extern_p)
23837 {
23838 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23839 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23840 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23841 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23842 if (! extern_p)
23843 {
23844 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23845 rest_of_type_compilation (t, 1);
23846 }
23847 }
23848
23849 /* Called from do_type_instantiation through binding_table_foreach to
23850 do recursive instantiation for the type bound in ENTRY. */
23851 static void
23852 bt_instantiate_type_proc (binding_entry entry, void *data)
23853 {
23854 tree storage = *(tree *) data;
23855
23856 if (MAYBE_CLASS_TYPE_P (entry->type)
23857 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23858 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23859 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23860 }
23861
23862 /* Perform an explicit instantiation of template class T. STORAGE, if
23863 non-null, is the RID for extern, inline or static. COMPLAIN is
23864 nonzero if this is called from the parser, zero if called recursively,
23865 since the standard is unclear (as detailed below). */
23866
23867 void
23868 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23869 {
23870 int extern_p = 0;
23871 int nomem_p = 0;
23872 int static_p = 0;
23873 int previous_instantiation_extern_p = 0;
23874
23875 if (TREE_CODE (t) == TYPE_DECL)
23876 t = TREE_TYPE (t);
23877
23878 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23879 {
23880 tree tmpl =
23881 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23882 if (tmpl)
23883 error ("explicit instantiation of non-class template %qD", tmpl);
23884 else
23885 error ("explicit instantiation of non-template type %qT", t);
23886 return;
23887 }
23888
23889 complete_type (t);
23890
23891 if (!COMPLETE_TYPE_P (t))
23892 {
23893 if (complain & tf_error)
23894 error ("explicit instantiation of %q#T before definition of template",
23895 t);
23896 return;
23897 }
23898
23899 if (storage != NULL_TREE)
23900 {
23901 if (!in_system_header_at (input_location))
23902 {
23903 if (storage == ridpointers[(int) RID_EXTERN])
23904 {
23905 if (cxx_dialect == cxx98)
23906 pedwarn (input_location, OPT_Wpedantic,
23907 "ISO C++ 1998 forbids the use of %<extern%> on "
23908 "explicit instantiations");
23909 }
23910 else
23911 pedwarn (input_location, OPT_Wpedantic,
23912 "ISO C++ forbids the use of %qE"
23913 " on explicit instantiations", storage);
23914 }
23915
23916 if (storage == ridpointers[(int) RID_INLINE])
23917 nomem_p = 1;
23918 else if (storage == ridpointers[(int) RID_EXTERN])
23919 extern_p = 1;
23920 else if (storage == ridpointers[(int) RID_STATIC])
23921 static_p = 1;
23922 else
23923 {
23924 error ("storage class %qD applied to template instantiation",
23925 storage);
23926 extern_p = 0;
23927 }
23928 }
23929
23930 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23931 {
23932 /* DR 259 [temp.spec].
23933
23934 Both an explicit instantiation and a declaration of an explicit
23935 specialization shall not appear in a program unless the explicit
23936 instantiation follows a declaration of the explicit specialization.
23937
23938 For a given set of template parameters, if an explicit
23939 instantiation of a template appears after a declaration of an
23940 explicit specialization for that template, the explicit
23941 instantiation has no effect. */
23942 return;
23943 }
23944 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23945 {
23946 /* [temp.spec]
23947
23948 No program shall explicitly instantiate any template more
23949 than once.
23950
23951 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23952 instantiation was `extern'. If EXTERN_P then the second is.
23953 These cases are OK. */
23954 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23955
23956 if (!previous_instantiation_extern_p && !extern_p
23957 && (complain & tf_error))
23958 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23959
23960 /* If we've already instantiated the template, just return now. */
23961 if (!CLASSTYPE_INTERFACE_ONLY (t))
23962 return;
23963 }
23964
23965 check_explicit_instantiation_namespace (TYPE_NAME (t));
23966 mark_class_instantiated (t, extern_p);
23967
23968 if (nomem_p)
23969 return;
23970
23971 /* In contrast to implicit instantiation, where only the
23972 declarations, and not the definitions, of members are
23973 instantiated, we have here:
23974
23975 [temp.explicit]
23976
23977 The explicit instantiation of a class template specialization
23978 implies the instantiation of all of its members not
23979 previously explicitly specialized in the translation unit
23980 containing the explicit instantiation.
23981
23982 Of course, we can't instantiate member template classes, since we
23983 don't have any arguments for them. Note that the standard is
23984 unclear on whether the instantiation of the members are
23985 *explicit* instantiations or not. However, the most natural
23986 interpretation is that it should be an explicit
23987 instantiation. */
23988 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23989 if ((VAR_P (fld)
23990 || (TREE_CODE (fld) == FUNCTION_DECL
23991 && !static_p
23992 && user_provided_p (fld)))
23993 && DECL_TEMPLATE_INSTANTIATION (fld))
23994 {
23995 mark_decl_instantiated (fld, extern_p);
23996 if (! extern_p)
23997 instantiate_decl (fld, /*defer_ok=*/true,
23998 /*expl_inst_class_mem_p=*/true);
23999 }
24000
24001 if (CLASSTYPE_NESTED_UTDS (t))
24002 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24003 bt_instantiate_type_proc, &storage);
24004 }
24005
24006 /* Given a function DECL, which is a specialization of TMPL, modify
24007 DECL to be a re-instantiation of TMPL with the same template
24008 arguments. TMPL should be the template into which tsubst'ing
24009 should occur for DECL, not the most general template.
24010
24011 One reason for doing this is a scenario like this:
24012
24013 template <class T>
24014 void f(const T&, int i);
24015
24016 void g() { f(3, 7); }
24017
24018 template <class T>
24019 void f(const T& t, const int i) { }
24020
24021 Note that when the template is first instantiated, with
24022 instantiate_template, the resulting DECL will have no name for the
24023 first parameter, and the wrong type for the second. So, when we go
24024 to instantiate the DECL, we regenerate it. */
24025
24026 static void
24027 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24028 {
24029 /* The arguments used to instantiate DECL, from the most general
24030 template. */
24031 tree code_pattern;
24032
24033 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24034
24035 /* Make sure that we can see identifiers, and compute access
24036 correctly. */
24037 push_access_scope (decl);
24038
24039 if (TREE_CODE (decl) == FUNCTION_DECL)
24040 {
24041 tree decl_parm;
24042 tree pattern_parm;
24043 tree specs;
24044 int args_depth;
24045 int parms_depth;
24046
24047 args_depth = TMPL_ARGS_DEPTH (args);
24048 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24049 if (args_depth > parms_depth)
24050 args = get_innermost_template_args (args, parms_depth);
24051
24052 /* Instantiate a dynamic exception-specification. noexcept will be
24053 handled below. */
24054 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24055 if (TREE_VALUE (raises))
24056 {
24057 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24058 args, tf_error, NULL_TREE,
24059 /*defer_ok*/false);
24060 if (specs && specs != error_mark_node)
24061 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24062 specs);
24063 }
24064
24065 /* Merge parameter declarations. */
24066 decl_parm = skip_artificial_parms_for (decl,
24067 DECL_ARGUMENTS (decl));
24068 pattern_parm
24069 = skip_artificial_parms_for (code_pattern,
24070 DECL_ARGUMENTS (code_pattern));
24071 while (decl_parm && !DECL_PACK_P (pattern_parm))
24072 {
24073 tree parm_type;
24074 tree attributes;
24075
24076 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24077 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24078 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24079 NULL_TREE);
24080 parm_type = type_decays_to (parm_type);
24081 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24082 TREE_TYPE (decl_parm) = parm_type;
24083 attributes = DECL_ATTRIBUTES (pattern_parm);
24084 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24085 {
24086 DECL_ATTRIBUTES (decl_parm) = attributes;
24087 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24088 }
24089 decl_parm = DECL_CHAIN (decl_parm);
24090 pattern_parm = DECL_CHAIN (pattern_parm);
24091 }
24092 /* Merge any parameters that match with the function parameter
24093 pack. */
24094 if (pattern_parm && DECL_PACK_P (pattern_parm))
24095 {
24096 int i, len;
24097 tree expanded_types;
24098 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24099 the parameters in this function parameter pack. */
24100 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24101 args, tf_error, NULL_TREE);
24102 len = TREE_VEC_LENGTH (expanded_types);
24103 for (i = 0; i < len; i++)
24104 {
24105 tree parm_type;
24106 tree attributes;
24107
24108 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24109 /* Rename the parameter to include the index. */
24110 DECL_NAME (decl_parm) =
24111 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24112 parm_type = TREE_VEC_ELT (expanded_types, i);
24113 parm_type = type_decays_to (parm_type);
24114 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24115 TREE_TYPE (decl_parm) = parm_type;
24116 attributes = DECL_ATTRIBUTES (pattern_parm);
24117 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24118 {
24119 DECL_ATTRIBUTES (decl_parm) = attributes;
24120 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24121 }
24122 decl_parm = DECL_CHAIN (decl_parm);
24123 }
24124 }
24125 /* Merge additional specifiers from the CODE_PATTERN. */
24126 if (DECL_DECLARED_INLINE_P (code_pattern)
24127 && !DECL_DECLARED_INLINE_P (decl))
24128 DECL_DECLARED_INLINE_P (decl) = 1;
24129
24130 maybe_instantiate_noexcept (decl, tf_error);
24131 }
24132 else if (VAR_P (decl))
24133 {
24134 start_lambda_scope (decl);
24135 DECL_INITIAL (decl) =
24136 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24137 tf_error, DECL_TI_TEMPLATE (decl));
24138 finish_lambda_scope ();
24139 if (VAR_HAD_UNKNOWN_BOUND (decl))
24140 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24141 tf_error, DECL_TI_TEMPLATE (decl));
24142 }
24143 else
24144 gcc_unreachable ();
24145
24146 pop_access_scope (decl);
24147 }
24148
24149 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24150 substituted to get DECL. */
24151
24152 tree
24153 template_for_substitution (tree decl)
24154 {
24155 tree tmpl = DECL_TI_TEMPLATE (decl);
24156
24157 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24158 for the instantiation. This is not always the most general
24159 template. Consider, for example:
24160
24161 template <class T>
24162 struct S { template <class U> void f();
24163 template <> void f<int>(); };
24164
24165 and an instantiation of S<double>::f<int>. We want TD to be the
24166 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24167 while (/* An instantiation cannot have a definition, so we need a
24168 more general template. */
24169 DECL_TEMPLATE_INSTANTIATION (tmpl)
24170 /* We must also deal with friend templates. Given:
24171
24172 template <class T> struct S {
24173 template <class U> friend void f() {};
24174 };
24175
24176 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24177 so far as the language is concerned, but that's still
24178 where we get the pattern for the instantiation from. On
24179 other hand, if the definition comes outside the class, say:
24180
24181 template <class T> struct S {
24182 template <class U> friend void f();
24183 };
24184 template <class U> friend void f() {}
24185
24186 we don't need to look any further. That's what the check for
24187 DECL_INITIAL is for. */
24188 || (TREE_CODE (decl) == FUNCTION_DECL
24189 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24190 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24191 {
24192 /* The present template, TD, should not be a definition. If it
24193 were a definition, we should be using it! Note that we
24194 cannot restructure the loop to just keep going until we find
24195 a template with a definition, since that might go too far if
24196 a specialization was declared, but not defined. */
24197
24198 /* Fetch the more general template. */
24199 tmpl = DECL_TI_TEMPLATE (tmpl);
24200 }
24201
24202 return tmpl;
24203 }
24204
24205 /* Returns true if we need to instantiate this template instance even if we
24206 know we aren't going to emit it. */
24207
24208 bool
24209 always_instantiate_p (tree decl)
24210 {
24211 /* We always instantiate inline functions so that we can inline them. An
24212 explicit instantiation declaration prohibits implicit instantiation of
24213 non-inline functions. With high levels of optimization, we would
24214 normally inline non-inline functions -- but we're not allowed to do
24215 that for "extern template" functions. Therefore, we check
24216 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24217 return ((TREE_CODE (decl) == FUNCTION_DECL
24218 && (DECL_DECLARED_INLINE_P (decl)
24219 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24220 /* And we need to instantiate static data members so that
24221 their initializers are available in integral constant
24222 expressions. */
24223 || (VAR_P (decl)
24224 && decl_maybe_constant_var_p (decl)));
24225 }
24226
24227 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24228 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24229 error, true otherwise. */
24230
24231 bool
24232 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24233 {
24234 tree fntype, spec, noex, clone;
24235
24236 /* Don't instantiate a noexcept-specification from template context. */
24237 if (processing_template_decl
24238 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24239 return true;
24240
24241 if (DECL_CLONED_FUNCTION_P (fn))
24242 fn = DECL_CLONED_FUNCTION (fn);
24243
24244 tree orig_fn = NULL_TREE;
24245 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24246 its FUNCTION_DECL for the rest of this function -- push_access_scope
24247 doesn't accept TEMPLATE_DECLs. */
24248 if (DECL_FUNCTION_TEMPLATE_P (fn))
24249 {
24250 orig_fn = fn;
24251 fn = DECL_TEMPLATE_RESULT (fn);
24252 }
24253
24254 fntype = TREE_TYPE (fn);
24255 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24256
24257 if (!spec || !TREE_PURPOSE (spec))
24258 return true;
24259
24260 noex = TREE_PURPOSE (spec);
24261
24262 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24263 {
24264 static hash_set<tree>* fns = new hash_set<tree>;
24265 bool added = false;
24266 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24267 {
24268 spec = get_defaulted_eh_spec (fn, complain);
24269 if (spec == error_mark_node)
24270 /* This might have failed because of an unparsed DMI, so
24271 let's try again later. */
24272 return false;
24273 }
24274 else if (!(added = !fns->add (fn)))
24275 {
24276 /* If hash_set::add returns true, the element was already there. */
24277 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24278 DECL_SOURCE_LOCATION (fn));
24279 error_at (loc,
24280 "exception specification of %qD depends on itself",
24281 fn);
24282 spec = noexcept_false_spec;
24283 }
24284 else if (push_tinst_level (fn))
24285 {
24286 push_access_scope (fn);
24287 push_deferring_access_checks (dk_no_deferred);
24288 input_location = DECL_SOURCE_LOCATION (fn);
24289
24290 tree save_ccp = current_class_ptr;
24291 tree save_ccr = current_class_ref;
24292 /* If needed, set current_class_ptr for the benefit of
24293 tsubst_copy/PARM_DECL. */
24294 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24295 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24296 {
24297 tree this_parm = DECL_ARGUMENTS (tdecl);
24298 current_class_ptr = NULL_TREE;
24299 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24300 current_class_ptr = this_parm;
24301 }
24302
24303 /* If this function is represented by a TEMPLATE_DECL, then
24304 the deferred noexcept-specification might still contain
24305 dependent types, even after substitution. And we need the
24306 dependency check functions to work in build_noexcept_spec. */
24307 if (orig_fn)
24308 ++processing_template_decl;
24309
24310 /* Do deferred instantiation of the noexcept-specifier. */
24311 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24312 DEFERRED_NOEXCEPT_ARGS (noex),
24313 tf_warning_or_error, fn,
24314 /*function_p=*/false,
24315 /*i_c_e_p=*/true);
24316
24317 current_class_ptr = save_ccp;
24318 current_class_ref = save_ccr;
24319
24320 /* Build up the noexcept-specification. */
24321 spec = build_noexcept_spec (noex, tf_warning_or_error);
24322
24323 if (orig_fn)
24324 --processing_template_decl;
24325
24326 pop_deferring_access_checks ();
24327 pop_access_scope (fn);
24328 pop_tinst_level ();
24329 }
24330 else
24331 spec = noexcept_false_spec;
24332
24333 if (added)
24334 fns->remove (fn);
24335
24336 if (spec == error_mark_node)
24337 {
24338 /* This failed with a hard error, so let's go with false. */
24339 gcc_assert (seen_error ());
24340 spec = noexcept_false_spec;
24341 }
24342
24343 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24344 if (orig_fn)
24345 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24346 }
24347
24348 FOR_EACH_CLONE (clone, fn)
24349 {
24350 if (TREE_TYPE (clone) == fntype)
24351 TREE_TYPE (clone) = TREE_TYPE (fn);
24352 else
24353 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24354 }
24355
24356 return true;
24357 }
24358
24359 /* We're starting to process the function INST, an instantiation of PATTERN;
24360 add their parameters to local_specializations. */
24361
24362 static void
24363 register_parameter_specializations (tree pattern, tree inst)
24364 {
24365 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24366 tree spec_parm = DECL_ARGUMENTS (inst);
24367 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24368 {
24369 register_local_specialization (spec_parm, tmpl_parm);
24370 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24371 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24372 }
24373 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24374 {
24375 if (!DECL_PACK_P (tmpl_parm))
24376 {
24377 register_local_specialization (spec_parm, tmpl_parm);
24378 spec_parm = DECL_CHAIN (spec_parm);
24379 }
24380 else
24381 {
24382 /* Register the (value) argument pack as a specialization of
24383 TMPL_PARM, then move on. */
24384 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24385 register_local_specialization (argpack, tmpl_parm);
24386 }
24387 }
24388 gcc_assert (!spec_parm);
24389 }
24390
24391 /* Produce the definition of D, a _DECL generated from a template. If
24392 DEFER_OK is true, then we don't have to actually do the
24393 instantiation now; we just have to do it sometime. Normally it is
24394 an error if this is an explicit instantiation but D is undefined.
24395 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24396 instantiated class template. */
24397
24398 tree
24399 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24400 {
24401 tree tmpl = DECL_TI_TEMPLATE (d);
24402 tree gen_args;
24403 tree args;
24404 tree td;
24405 tree code_pattern;
24406 tree spec;
24407 tree gen_tmpl;
24408 bool pattern_defined;
24409 location_t saved_loc = input_location;
24410 int saved_unevaluated_operand = cp_unevaluated_operand;
24411 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24412 bool external_p;
24413 bool deleted_p;
24414
24415 /* This function should only be used to instantiate templates for
24416 functions and static member variables. */
24417 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24418
24419 /* A concept is never instantiated. */
24420 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24421
24422 /* Variables are never deferred; if instantiation is required, they
24423 are instantiated right away. That allows for better code in the
24424 case that an expression refers to the value of the variable --
24425 if the variable has a constant value the referring expression can
24426 take advantage of that fact. */
24427 if (VAR_P (d))
24428 defer_ok = false;
24429
24430 /* Don't instantiate cloned functions. Instead, instantiate the
24431 functions they cloned. */
24432 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24433 d = DECL_CLONED_FUNCTION (d);
24434
24435 if (DECL_TEMPLATE_INSTANTIATED (d)
24436 || (TREE_CODE (d) == FUNCTION_DECL
24437 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24438 || DECL_TEMPLATE_SPECIALIZATION (d))
24439 /* D has already been instantiated or explicitly specialized, so
24440 there's nothing for us to do here.
24441
24442 It might seem reasonable to check whether or not D is an explicit
24443 instantiation, and, if so, stop here. But when an explicit
24444 instantiation is deferred until the end of the compilation,
24445 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24446 the instantiation. */
24447 return d;
24448
24449 /* Check to see whether we know that this template will be
24450 instantiated in some other file, as with "extern template"
24451 extension. */
24452 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24453
24454 /* In general, we do not instantiate such templates. */
24455 if (external_p && !always_instantiate_p (d))
24456 return d;
24457
24458 gen_tmpl = most_general_template (tmpl);
24459 gen_args = DECL_TI_ARGS (d);
24460
24461 if (tmpl != gen_tmpl)
24462 /* We should already have the extra args. */
24463 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24464 == TMPL_ARGS_DEPTH (gen_args));
24465 /* And what's in the hash table should match D. */
24466 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24467 || spec == NULL_TREE);
24468
24469 /* This needs to happen before any tsubsting. */
24470 if (! push_tinst_level (d))
24471 return d;
24472
24473 timevar_push (TV_TEMPLATE_INST);
24474
24475 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24476 for the instantiation. */
24477 td = template_for_substitution (d);
24478 args = gen_args;
24479
24480 if (VAR_P (d))
24481 {
24482 /* Look up an explicit specialization, if any. */
24483 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24484 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24485 if (elt && elt != error_mark_node)
24486 {
24487 td = TREE_VALUE (elt);
24488 args = TREE_PURPOSE (elt);
24489 }
24490 }
24491
24492 code_pattern = DECL_TEMPLATE_RESULT (td);
24493
24494 /* We should never be trying to instantiate a member of a class
24495 template or partial specialization. */
24496 gcc_assert (d != code_pattern);
24497
24498 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24499 || DECL_TEMPLATE_SPECIALIZATION (td))
24500 /* In the case of a friend template whose definition is provided
24501 outside the class, we may have too many arguments. Drop the
24502 ones we don't need. The same is true for specializations. */
24503 args = get_innermost_template_args
24504 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24505
24506 if (TREE_CODE (d) == FUNCTION_DECL)
24507 {
24508 deleted_p = DECL_DELETED_FN (code_pattern);
24509 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24510 && DECL_INITIAL (code_pattern) != error_mark_node)
24511 || DECL_DEFAULTED_FN (code_pattern)
24512 || deleted_p);
24513 }
24514 else
24515 {
24516 deleted_p = false;
24517 if (DECL_CLASS_SCOPE_P (code_pattern))
24518 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24519 else
24520 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24521 }
24522
24523 /* We may be in the middle of deferred access check. Disable it now. */
24524 push_deferring_access_checks (dk_no_deferred);
24525
24526 /* Unless an explicit instantiation directive has already determined
24527 the linkage of D, remember that a definition is available for
24528 this entity. */
24529 if (pattern_defined
24530 && !DECL_INTERFACE_KNOWN (d)
24531 && !DECL_NOT_REALLY_EXTERN (d))
24532 mark_definable (d);
24533
24534 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24535 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24536 input_location = DECL_SOURCE_LOCATION (d);
24537
24538 /* If D is a member of an explicitly instantiated class template,
24539 and no definition is available, treat it like an implicit
24540 instantiation. */
24541 if (!pattern_defined && expl_inst_class_mem_p
24542 && DECL_EXPLICIT_INSTANTIATION (d))
24543 {
24544 /* Leave linkage flags alone on instantiations with anonymous
24545 visibility. */
24546 if (TREE_PUBLIC (d))
24547 {
24548 DECL_NOT_REALLY_EXTERN (d) = 0;
24549 DECL_INTERFACE_KNOWN (d) = 0;
24550 }
24551 SET_DECL_IMPLICIT_INSTANTIATION (d);
24552 }
24553
24554 /* Defer all other templates, unless we have been explicitly
24555 forbidden from doing so. */
24556 if (/* If there is no definition, we cannot instantiate the
24557 template. */
24558 ! pattern_defined
24559 /* If it's OK to postpone instantiation, do so. */
24560 || defer_ok
24561 /* If this is a static data member that will be defined
24562 elsewhere, we don't want to instantiate the entire data
24563 member, but we do want to instantiate the initializer so that
24564 we can substitute that elsewhere. */
24565 || (external_p && VAR_P (d))
24566 /* Handle here a deleted function too, avoid generating
24567 its body (c++/61080). */
24568 || deleted_p)
24569 {
24570 /* The definition of the static data member is now required so
24571 we must substitute the initializer. */
24572 if (VAR_P (d)
24573 && !DECL_INITIAL (d)
24574 && DECL_INITIAL (code_pattern))
24575 {
24576 tree ns;
24577 tree init;
24578 bool const_init = false;
24579 bool enter_context = DECL_CLASS_SCOPE_P (d);
24580
24581 ns = decl_namespace_context (d);
24582 push_nested_namespace (ns);
24583 if (enter_context)
24584 push_nested_class (DECL_CONTEXT (d));
24585 init = tsubst_expr (DECL_INITIAL (code_pattern),
24586 args,
24587 tf_warning_or_error, NULL_TREE,
24588 /*integral_constant_expression_p=*/false);
24589 /* If instantiating the initializer involved instantiating this
24590 again, don't call cp_finish_decl twice. */
24591 if (!DECL_INITIAL (d))
24592 {
24593 /* Make sure the initializer is still constant, in case of
24594 circular dependency (template/instantiate6.C). */
24595 const_init
24596 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24597 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24598 /*asmspec_tree=*/NULL_TREE,
24599 LOOKUP_ONLYCONVERTING);
24600 }
24601 if (enter_context)
24602 pop_nested_class ();
24603 pop_nested_namespace (ns);
24604 }
24605
24606 /* We restore the source position here because it's used by
24607 add_pending_template. */
24608 input_location = saved_loc;
24609
24610 if (at_eof && !pattern_defined
24611 && DECL_EXPLICIT_INSTANTIATION (d)
24612 && DECL_NOT_REALLY_EXTERN (d))
24613 /* [temp.explicit]
24614
24615 The definition of a non-exported function template, a
24616 non-exported member function template, or a non-exported
24617 member function or static data member of a class template
24618 shall be present in every translation unit in which it is
24619 explicitly instantiated. */
24620 permerror (input_location, "explicit instantiation of %qD "
24621 "but no definition available", d);
24622
24623 /* If we're in unevaluated context, we just wanted to get the
24624 constant value; this isn't an odr use, so don't queue
24625 a full instantiation. */
24626 if (cp_unevaluated_operand != 0)
24627 goto out;
24628 /* ??? Historically, we have instantiated inline functions, even
24629 when marked as "extern template". */
24630 if (!(external_p && VAR_P (d)))
24631 add_pending_template (d);
24632 goto out;
24633 }
24634 /* Tell the repository that D is available in this translation unit
24635 -- and see if it is supposed to be instantiated here. */
24636 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24637 {
24638 /* In a PCH file, despite the fact that the repository hasn't
24639 requested instantiation in the PCH it is still possible that
24640 an instantiation will be required in a file that includes the
24641 PCH. */
24642 if (pch_file)
24643 add_pending_template (d);
24644 /* Instantiate inline functions so that the inliner can do its
24645 job, even though we'll not be emitting a copy of this
24646 function. */
24647 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24648 goto out;
24649 }
24650
24651 bool push_to_top, nested;
24652 tree fn_context;
24653 fn_context = decl_function_context (d);
24654 if (LAMBDA_FUNCTION_P (d))
24655 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24656 fn_context = NULL_TREE;
24657 nested = current_function_decl != NULL_TREE;
24658 push_to_top = !(nested && fn_context == current_function_decl);
24659
24660 vec<tree> omp_privatization_save;
24661 if (nested)
24662 save_omp_privatization_clauses (omp_privatization_save);
24663
24664 if (push_to_top)
24665 push_to_top_level ();
24666 else
24667 {
24668 gcc_assert (!processing_template_decl);
24669 push_function_context ();
24670 cp_unevaluated_operand = 0;
24671 c_inhibit_evaluation_warnings = 0;
24672 }
24673
24674 /* Mark D as instantiated so that recursive calls to
24675 instantiate_decl do not try to instantiate it again. */
24676 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24677
24678 /* Regenerate the declaration in case the template has been modified
24679 by a subsequent redeclaration. */
24680 regenerate_decl_from_template (d, td, args);
24681
24682 /* We already set the file and line above. Reset them now in case
24683 they changed as a result of calling regenerate_decl_from_template. */
24684 input_location = DECL_SOURCE_LOCATION (d);
24685
24686 if (VAR_P (d))
24687 {
24688 tree init;
24689 bool const_init = false;
24690
24691 /* Clear out DECL_RTL; whatever was there before may not be right
24692 since we've reset the type of the declaration. */
24693 SET_DECL_RTL (d, NULL);
24694 DECL_IN_AGGR_P (d) = 0;
24695
24696 /* The initializer is placed in DECL_INITIAL by
24697 regenerate_decl_from_template so we don't need to
24698 push/pop_access_scope again here. Pull it out so that
24699 cp_finish_decl can process it. */
24700 init = DECL_INITIAL (d);
24701 DECL_INITIAL (d) = NULL_TREE;
24702 DECL_INITIALIZED_P (d) = 0;
24703
24704 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24705 initializer. That function will defer actual emission until
24706 we have a chance to determine linkage. */
24707 DECL_EXTERNAL (d) = 0;
24708
24709 /* Enter the scope of D so that access-checking works correctly. */
24710 bool enter_context = DECL_CLASS_SCOPE_P (d);
24711 if (enter_context)
24712 push_nested_class (DECL_CONTEXT (d));
24713
24714 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24715 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24716
24717 if (enter_context)
24718 pop_nested_class ();
24719
24720 if (variable_template_p (gen_tmpl))
24721 note_variable_template_instantiation (d);
24722 }
24723 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24724 synthesize_method (d);
24725 else if (TREE_CODE (d) == FUNCTION_DECL)
24726 {
24727 /* Set up the list of local specializations. */
24728 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24729 tree block = NULL_TREE;
24730
24731 /* Set up context. */
24732 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24733 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24734 block = push_stmt_list ();
24735 else
24736 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24737
24738 /* Some typedefs referenced from within the template code need to be
24739 access checked at template instantiation time, i.e now. These
24740 types were added to the template at parsing time. Let's get those
24741 and perform the access checks then. */
24742 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24743 args);
24744
24745 /* Create substitution entries for the parameters. */
24746 register_parameter_specializations (code_pattern, d);
24747
24748 /* Substitute into the body of the function. */
24749 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24750 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24751 tf_warning_or_error, tmpl);
24752 else
24753 {
24754 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24755 tf_warning_or_error, tmpl,
24756 /*integral_constant_expression_p=*/false);
24757
24758 /* Set the current input_location to the end of the function
24759 so that finish_function knows where we are. */
24760 input_location
24761 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24762
24763 /* Remember if we saw an infinite loop in the template. */
24764 current_function_infinite_loop
24765 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24766 }
24767
24768 /* Finish the function. */
24769 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24770 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24771 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24772 else
24773 {
24774 d = finish_function (/*inline_p=*/false);
24775 expand_or_defer_fn (d);
24776 }
24777
24778 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24779 cp_check_omp_declare_reduction (d);
24780 }
24781
24782 /* We're not deferring instantiation any more. */
24783 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24784
24785 if (push_to_top)
24786 pop_from_top_level ();
24787 else
24788 pop_function_context ();
24789
24790 if (nested)
24791 restore_omp_privatization_clauses (omp_privatization_save);
24792
24793 out:
24794 pop_deferring_access_checks ();
24795 timevar_pop (TV_TEMPLATE_INST);
24796 pop_tinst_level ();
24797 input_location = saved_loc;
24798 cp_unevaluated_operand = saved_unevaluated_operand;
24799 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24800
24801 return d;
24802 }
24803
24804 /* Run through the list of templates that we wish we could
24805 instantiate, and instantiate any we can. RETRIES is the
24806 number of times we retry pending template instantiation. */
24807
24808 void
24809 instantiate_pending_templates (int retries)
24810 {
24811 int reconsider;
24812 location_t saved_loc = input_location;
24813
24814 /* Instantiating templates may trigger vtable generation. This in turn
24815 may require further template instantiations. We place a limit here
24816 to avoid infinite loop. */
24817 if (pending_templates && retries >= max_tinst_depth)
24818 {
24819 tree decl = pending_templates->tinst->maybe_get_node ();
24820
24821 fatal_error (input_location,
24822 "template instantiation depth exceeds maximum of %d"
24823 " instantiating %q+D, possibly from virtual table generation"
24824 " (use %<-ftemplate-depth=%> to increase the maximum)",
24825 max_tinst_depth, decl);
24826 if (TREE_CODE (decl) == FUNCTION_DECL)
24827 /* Pretend that we defined it. */
24828 DECL_INITIAL (decl) = error_mark_node;
24829 return;
24830 }
24831
24832 do
24833 {
24834 struct pending_template **t = &pending_templates;
24835 struct pending_template *last = NULL;
24836 reconsider = 0;
24837 while (*t)
24838 {
24839 tree instantiation = reopen_tinst_level ((*t)->tinst);
24840 bool complete = false;
24841
24842 if (TYPE_P (instantiation))
24843 {
24844 if (!COMPLETE_TYPE_P (instantiation))
24845 {
24846 instantiate_class_template (instantiation);
24847 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24848 for (tree fld = TYPE_FIELDS (instantiation);
24849 fld; fld = TREE_CHAIN (fld))
24850 if ((VAR_P (fld)
24851 || (TREE_CODE (fld) == FUNCTION_DECL
24852 && !DECL_ARTIFICIAL (fld)))
24853 && DECL_TEMPLATE_INSTANTIATION (fld))
24854 instantiate_decl (fld,
24855 /*defer_ok=*/false,
24856 /*expl_inst_class_mem_p=*/false);
24857
24858 if (COMPLETE_TYPE_P (instantiation))
24859 reconsider = 1;
24860 }
24861
24862 complete = COMPLETE_TYPE_P (instantiation);
24863 }
24864 else
24865 {
24866 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24867 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24868 {
24869 instantiation
24870 = instantiate_decl (instantiation,
24871 /*defer_ok=*/false,
24872 /*expl_inst_class_mem_p=*/false);
24873 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24874 reconsider = 1;
24875 }
24876
24877 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24878 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24879 }
24880
24881 if (complete)
24882 {
24883 /* If INSTANTIATION has been instantiated, then we don't
24884 need to consider it again in the future. */
24885 struct pending_template *drop = *t;
24886 *t = (*t)->next;
24887 set_refcount_ptr (drop->tinst);
24888 pending_template_freelist ().free (drop);
24889 }
24890 else
24891 {
24892 last = *t;
24893 t = &(*t)->next;
24894 }
24895 tinst_depth = 0;
24896 set_refcount_ptr (current_tinst_level);
24897 }
24898 last_pending_template = last;
24899 }
24900 while (reconsider);
24901
24902 input_location = saved_loc;
24903 }
24904
24905 /* Substitute ARGVEC into T, which is a list of initializers for
24906 either base class or a non-static data member. The TREE_PURPOSEs
24907 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24908 instantiate_decl. */
24909
24910 static tree
24911 tsubst_initializer_list (tree t, tree argvec)
24912 {
24913 tree inits = NULL_TREE;
24914 tree target_ctor = error_mark_node;
24915
24916 for (; t; t = TREE_CHAIN (t))
24917 {
24918 tree decl;
24919 tree init;
24920 tree expanded_bases = NULL_TREE;
24921 tree expanded_arguments = NULL_TREE;
24922 int i, len = 1;
24923
24924 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24925 {
24926 tree expr;
24927 tree arg;
24928
24929 /* Expand the base class expansion type into separate base
24930 classes. */
24931 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24932 tf_warning_or_error,
24933 NULL_TREE);
24934 if (expanded_bases == error_mark_node)
24935 continue;
24936
24937 /* We'll be building separate TREE_LISTs of arguments for
24938 each base. */
24939 len = TREE_VEC_LENGTH (expanded_bases);
24940 expanded_arguments = make_tree_vec (len);
24941 for (i = 0; i < len; i++)
24942 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24943
24944 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24945 expand each argument in the TREE_VALUE of t. */
24946 expr = make_node (EXPR_PACK_EXPANSION);
24947 PACK_EXPANSION_LOCAL_P (expr) = true;
24948 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24949 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24950
24951 if (TREE_VALUE (t) == void_type_node)
24952 /* VOID_TYPE_NODE is used to indicate
24953 value-initialization. */
24954 {
24955 for (i = 0; i < len; i++)
24956 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24957 }
24958 else
24959 {
24960 /* Substitute parameter packs into each argument in the
24961 TREE_LIST. */
24962 in_base_initializer = 1;
24963 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24964 {
24965 tree expanded_exprs;
24966
24967 /* Expand the argument. */
24968 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24969 expanded_exprs
24970 = tsubst_pack_expansion (expr, argvec,
24971 tf_warning_or_error,
24972 NULL_TREE);
24973 if (expanded_exprs == error_mark_node)
24974 continue;
24975
24976 /* Prepend each of the expanded expressions to the
24977 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24978 for (i = 0; i < len; i++)
24979 {
24980 TREE_VEC_ELT (expanded_arguments, i) =
24981 tree_cons (NULL_TREE,
24982 TREE_VEC_ELT (expanded_exprs, i),
24983 TREE_VEC_ELT (expanded_arguments, i));
24984 }
24985 }
24986 in_base_initializer = 0;
24987
24988 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24989 since we built them backwards. */
24990 for (i = 0; i < len; i++)
24991 {
24992 TREE_VEC_ELT (expanded_arguments, i) =
24993 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24994 }
24995 }
24996 }
24997
24998 for (i = 0; i < len; ++i)
24999 {
25000 if (expanded_bases)
25001 {
25002 decl = TREE_VEC_ELT (expanded_bases, i);
25003 decl = expand_member_init (decl);
25004 init = TREE_VEC_ELT (expanded_arguments, i);
25005 }
25006 else
25007 {
25008 tree tmp;
25009 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25010 tf_warning_or_error, NULL_TREE);
25011
25012 decl = expand_member_init (decl);
25013 if (decl && !DECL_P (decl))
25014 in_base_initializer = 1;
25015
25016 init = TREE_VALUE (t);
25017 tmp = init;
25018 if (init != void_type_node)
25019 init = tsubst_expr (init, argvec,
25020 tf_warning_or_error, NULL_TREE,
25021 /*integral_constant_expression_p=*/false);
25022 if (init == NULL_TREE && tmp != NULL_TREE)
25023 /* If we had an initializer but it instantiated to nothing,
25024 value-initialize the object. This will only occur when
25025 the initializer was a pack expansion where the parameter
25026 packs used in that expansion were of length zero. */
25027 init = void_type_node;
25028 in_base_initializer = 0;
25029 }
25030
25031 if (target_ctor != error_mark_node
25032 && init != error_mark_node)
25033 {
25034 error ("mem-initializer for %qD follows constructor delegation",
25035 decl);
25036 return inits;
25037 }
25038 /* Look for a target constructor. */
25039 if (init != error_mark_node
25040 && decl && CLASS_TYPE_P (decl)
25041 && same_type_p (decl, current_class_type))
25042 {
25043 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25044 if (inits)
25045 {
25046 error ("constructor delegation follows mem-initializer for %qD",
25047 TREE_PURPOSE (inits));
25048 continue;
25049 }
25050 target_ctor = init;
25051 }
25052
25053 if (decl)
25054 {
25055 init = build_tree_list (decl, init);
25056 TREE_CHAIN (init) = inits;
25057 inits = init;
25058 }
25059 }
25060 }
25061 return inits;
25062 }
25063
25064 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25065
25066 static void
25067 set_current_access_from_decl (tree decl)
25068 {
25069 if (TREE_PRIVATE (decl))
25070 current_access_specifier = access_private_node;
25071 else if (TREE_PROTECTED (decl))
25072 current_access_specifier = access_protected_node;
25073 else
25074 current_access_specifier = access_public_node;
25075 }
25076
25077 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25078 is the instantiation (which should have been created with
25079 start_enum) and ARGS are the template arguments to use. */
25080
25081 static void
25082 tsubst_enum (tree tag, tree newtag, tree args)
25083 {
25084 tree e;
25085
25086 if (SCOPED_ENUM_P (newtag))
25087 begin_scope (sk_scoped_enum, newtag);
25088
25089 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25090 {
25091 tree value;
25092 tree decl;
25093
25094 decl = TREE_VALUE (e);
25095 /* Note that in a template enum, the TREE_VALUE is the
25096 CONST_DECL, not the corresponding INTEGER_CST. */
25097 value = tsubst_expr (DECL_INITIAL (decl),
25098 args, tf_warning_or_error, NULL_TREE,
25099 /*integral_constant_expression_p=*/true);
25100
25101 /* Give this enumeration constant the correct access. */
25102 set_current_access_from_decl (decl);
25103
25104 /* Actually build the enumerator itself. Here we're assuming that
25105 enumerators can't have dependent attributes. */
25106 build_enumerator (DECL_NAME (decl), value, newtag,
25107 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25108 }
25109
25110 if (SCOPED_ENUM_P (newtag))
25111 finish_scope ();
25112
25113 finish_enum_value_list (newtag);
25114 finish_enum (newtag);
25115
25116 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25117 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25118 }
25119
25120 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25121 its type -- but without substituting the innermost set of template
25122 arguments. So, innermost set of template parameters will appear in
25123 the type. */
25124
25125 tree
25126 get_mostly_instantiated_function_type (tree decl)
25127 {
25128 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25129 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25130 }
25131
25132 /* Return truthvalue if we're processing a template different from
25133 the last one involved in diagnostics. */
25134 bool
25135 problematic_instantiation_changed (void)
25136 {
25137 return current_tinst_level != last_error_tinst_level;
25138 }
25139
25140 /* Remember current template involved in diagnostics. */
25141 void
25142 record_last_problematic_instantiation (void)
25143 {
25144 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25145 }
25146
25147 struct tinst_level *
25148 current_instantiation (void)
25149 {
25150 return current_tinst_level;
25151 }
25152
25153 /* Return TRUE if current_function_decl is being instantiated, false
25154 otherwise. */
25155
25156 bool
25157 instantiating_current_function_p (void)
25158 {
25159 return (current_instantiation ()
25160 && (current_instantiation ()->maybe_get_node ()
25161 == current_function_decl));
25162 }
25163
25164 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25165 type. Return false for ok, true for disallowed. Issue error and
25166 inform messages under control of COMPLAIN. */
25167
25168 static bool
25169 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25170 {
25171 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25172 return false;
25173 else if (TYPE_PTR_P (type))
25174 return false;
25175 else if (TYPE_REF_P (type)
25176 && !TYPE_REF_IS_RVALUE (type))
25177 return false;
25178 else if (TYPE_PTRMEM_P (type))
25179 return false;
25180 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25181 return false;
25182 else if (TREE_CODE (type) == TYPENAME_TYPE)
25183 return false;
25184 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25185 return false;
25186 else if (TREE_CODE (type) == NULLPTR_TYPE)
25187 return false;
25188 /* A bound template template parm could later be instantiated to have a valid
25189 nontype parm type via an alias template. */
25190 else if (cxx_dialect >= cxx11
25191 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25192 return false;
25193 else if (CLASS_TYPE_P (type))
25194 {
25195 if (cxx_dialect < cxx2a)
25196 {
25197 error ("non-type template parameters of class type only available "
25198 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25199 return true;
25200 }
25201 if (!complete_type_or_else (type, NULL_TREE))
25202 return true;
25203 if (!literal_type_p (type))
25204 {
25205 error ("%qT is not a valid type for a template non-type parameter "
25206 "because it is not literal", type);
25207 explain_non_literal_class (type);
25208 return true;
25209 }
25210 if (cp_has_mutable_p (type))
25211 {
25212 error ("%qT is not a valid type for a template non-type parameter "
25213 "because it has a mutable member", type);
25214 return true;
25215 }
25216 /* FIXME check op<=> and strong structural equality once spaceship is
25217 implemented. */
25218 return false;
25219 }
25220
25221 if (complain & tf_error)
25222 {
25223 if (type == error_mark_node)
25224 inform (input_location, "invalid template non-type parameter");
25225 else
25226 error ("%q#T is not a valid type for a template non-type parameter",
25227 type);
25228 }
25229 return true;
25230 }
25231
25232 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25233 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25234
25235 static bool
25236 dependent_type_p_r (tree type)
25237 {
25238 tree scope;
25239
25240 /* [temp.dep.type]
25241
25242 A type is dependent if it is:
25243
25244 -- a template parameter. Template template parameters are types
25245 for us (since TYPE_P holds true for them) so we handle
25246 them here. */
25247 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25248 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25249 return true;
25250 /* -- a qualified-id with a nested-name-specifier which contains a
25251 class-name that names a dependent type or whose unqualified-id
25252 names a dependent type. */
25253 if (TREE_CODE (type) == TYPENAME_TYPE)
25254 return true;
25255
25256 /* An alias template specialization can be dependent even if the
25257 resulting type is not. */
25258 if (dependent_alias_template_spec_p (type))
25259 return true;
25260
25261 /* -- a cv-qualified type where the cv-unqualified type is
25262 dependent.
25263 No code is necessary for this bullet; the code below handles
25264 cv-qualified types, and we don't want to strip aliases with
25265 TYPE_MAIN_VARIANT because of DR 1558. */
25266 /* -- a compound type constructed from any dependent type. */
25267 if (TYPE_PTRMEM_P (type))
25268 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25269 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25270 (type)));
25271 else if (INDIRECT_TYPE_P (type))
25272 return dependent_type_p (TREE_TYPE (type));
25273 else if (FUNC_OR_METHOD_TYPE_P (type))
25274 {
25275 tree arg_type;
25276
25277 if (dependent_type_p (TREE_TYPE (type)))
25278 return true;
25279 for (arg_type = TYPE_ARG_TYPES (type);
25280 arg_type;
25281 arg_type = TREE_CHAIN (arg_type))
25282 if (dependent_type_p (TREE_VALUE (arg_type)))
25283 return true;
25284 if (cxx_dialect >= cxx17)
25285 /* A value-dependent noexcept-specifier makes the type dependent. */
25286 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25287 if (tree noex = TREE_PURPOSE (spec))
25288 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25289 affect overload resolution and treating it as dependent breaks
25290 things. */
25291 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25292 && value_dependent_expression_p (noex))
25293 return true;
25294 return false;
25295 }
25296 /* -- an array type constructed from any dependent type or whose
25297 size is specified by a constant expression that is
25298 value-dependent.
25299
25300 We checked for type- and value-dependence of the bounds in
25301 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25302 if (TREE_CODE (type) == ARRAY_TYPE)
25303 {
25304 if (TYPE_DOMAIN (type)
25305 && dependent_type_p (TYPE_DOMAIN (type)))
25306 return true;
25307 return dependent_type_p (TREE_TYPE (type));
25308 }
25309
25310 /* -- a template-id in which either the template name is a template
25311 parameter ... */
25312 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25313 return true;
25314 /* ... or any of the template arguments is a dependent type or
25315 an expression that is type-dependent or value-dependent. */
25316 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25317 && (any_dependent_template_arguments_p
25318 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25319 return true;
25320
25321 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25322 dependent; if the argument of the `typeof' expression is not
25323 type-dependent, then it should already been have resolved. */
25324 if (TREE_CODE (type) == TYPEOF_TYPE
25325 || TREE_CODE (type) == DECLTYPE_TYPE
25326 || TREE_CODE (type) == UNDERLYING_TYPE)
25327 return true;
25328
25329 /* A template argument pack is dependent if any of its packed
25330 arguments are. */
25331 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25332 {
25333 tree args = ARGUMENT_PACK_ARGS (type);
25334 int i, len = TREE_VEC_LENGTH (args);
25335 for (i = 0; i < len; ++i)
25336 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25337 return true;
25338 }
25339
25340 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25341 be template parameters. */
25342 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25343 return true;
25344
25345 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25346 return true;
25347
25348 /* The standard does not specifically mention types that are local
25349 to template functions or local classes, but they should be
25350 considered dependent too. For example:
25351
25352 template <int I> void f() {
25353 enum E { a = I };
25354 S<sizeof (E)> s;
25355 }
25356
25357 The size of `E' cannot be known until the value of `I' has been
25358 determined. Therefore, `E' must be considered dependent. */
25359 scope = TYPE_CONTEXT (type);
25360 if (scope && TYPE_P (scope))
25361 return dependent_type_p (scope);
25362 /* Don't use type_dependent_expression_p here, as it can lead
25363 to infinite recursion trying to determine whether a lambda
25364 nested in a lambda is dependent (c++/47687). */
25365 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25366 && DECL_LANG_SPECIFIC (scope)
25367 && DECL_TEMPLATE_INFO (scope)
25368 && (any_dependent_template_arguments_p
25369 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25370 return true;
25371
25372 /* Other types are non-dependent. */
25373 return false;
25374 }
25375
25376 /* Returns TRUE if TYPE is dependent, in the sense of
25377 [temp.dep.type]. Note that a NULL type is considered dependent. */
25378
25379 bool
25380 dependent_type_p (tree type)
25381 {
25382 /* If there are no template parameters in scope, then there can't be
25383 any dependent types. */
25384 if (!processing_template_decl)
25385 {
25386 /* If we are not processing a template, then nobody should be
25387 providing us with a dependent type. */
25388 gcc_assert (type);
25389 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25390 return false;
25391 }
25392
25393 /* If the type is NULL, we have not computed a type for the entity
25394 in question; in that case, the type is dependent. */
25395 if (!type)
25396 return true;
25397
25398 /* Erroneous types can be considered non-dependent. */
25399 if (type == error_mark_node)
25400 return false;
25401
25402 /* Getting here with global_type_node means we improperly called this
25403 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25404 gcc_checking_assert (type != global_type_node);
25405
25406 /* If we have not already computed the appropriate value for TYPE,
25407 do so now. */
25408 if (!TYPE_DEPENDENT_P_VALID (type))
25409 {
25410 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25411 TYPE_DEPENDENT_P_VALID (type) = 1;
25412 }
25413
25414 return TYPE_DEPENDENT_P (type);
25415 }
25416
25417 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25418 lookup. In other words, a dependent type that is not the current
25419 instantiation. */
25420
25421 bool
25422 dependent_scope_p (tree scope)
25423 {
25424 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25425 && !currently_open_class (scope));
25426 }
25427
25428 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25429 an unknown base of 'this' (and is therefore instantiation-dependent). */
25430
25431 static bool
25432 unknown_base_ref_p (tree t)
25433 {
25434 if (!current_class_ptr)
25435 return false;
25436
25437 tree mem = TREE_OPERAND (t, 1);
25438 if (shared_member_p (mem))
25439 return false;
25440
25441 tree cur = current_nonlambda_class_type ();
25442 if (!any_dependent_bases_p (cur))
25443 return false;
25444
25445 tree ctx = TREE_OPERAND (t, 0);
25446 if (DERIVED_FROM_P (ctx, cur))
25447 return false;
25448
25449 return true;
25450 }
25451
25452 /* T is a SCOPE_REF; return whether we need to consider it
25453 instantiation-dependent so that we can check access at instantiation
25454 time even though we know which member it resolves to. */
25455
25456 static bool
25457 instantiation_dependent_scope_ref_p (tree t)
25458 {
25459 if (DECL_P (TREE_OPERAND (t, 1))
25460 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25461 && !unknown_base_ref_p (t)
25462 && accessible_in_template_p (TREE_OPERAND (t, 0),
25463 TREE_OPERAND (t, 1)))
25464 return false;
25465 else
25466 return true;
25467 }
25468
25469 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25470 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25471 expression. */
25472
25473 /* Note that this predicate is not appropriate for general expressions;
25474 only constant expressions (that satisfy potential_constant_expression)
25475 can be tested for value dependence. */
25476
25477 bool
25478 value_dependent_expression_p (tree expression)
25479 {
25480 if (!processing_template_decl || expression == NULL_TREE)
25481 return false;
25482
25483 /* A type-dependent expression is also value-dependent. */
25484 if (type_dependent_expression_p (expression))
25485 return true;
25486
25487 switch (TREE_CODE (expression))
25488 {
25489 case BASELINK:
25490 /* A dependent member function of the current instantiation. */
25491 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25492
25493 case FUNCTION_DECL:
25494 /* A dependent member function of the current instantiation. */
25495 if (DECL_CLASS_SCOPE_P (expression)
25496 && dependent_type_p (DECL_CONTEXT (expression)))
25497 return true;
25498 break;
25499
25500 case IDENTIFIER_NODE:
25501 /* A name that has not been looked up -- must be dependent. */
25502 return true;
25503
25504 case TEMPLATE_PARM_INDEX:
25505 /* A non-type template parm. */
25506 return true;
25507
25508 case CONST_DECL:
25509 /* A non-type template parm. */
25510 if (DECL_TEMPLATE_PARM_P (expression))
25511 return true;
25512 return value_dependent_expression_p (DECL_INITIAL (expression));
25513
25514 case VAR_DECL:
25515 /* A constant with literal type and is initialized
25516 with an expression that is value-dependent. */
25517 if (DECL_DEPENDENT_INIT_P (expression)
25518 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25519 || TYPE_REF_P (TREE_TYPE (expression)))
25520 return true;
25521 if (DECL_HAS_VALUE_EXPR_P (expression))
25522 {
25523 tree value_expr = DECL_VALUE_EXPR (expression);
25524 if (value_dependent_expression_p (value_expr))
25525 return true;
25526 }
25527 return false;
25528
25529 case DYNAMIC_CAST_EXPR:
25530 case STATIC_CAST_EXPR:
25531 case CONST_CAST_EXPR:
25532 case REINTERPRET_CAST_EXPR:
25533 case CAST_EXPR:
25534 case IMPLICIT_CONV_EXPR:
25535 /* These expressions are value-dependent if the type to which
25536 the cast occurs is dependent or the expression being casted
25537 is value-dependent. */
25538 {
25539 tree type = TREE_TYPE (expression);
25540
25541 if (dependent_type_p (type))
25542 return true;
25543
25544 /* A functional cast has a list of operands. */
25545 expression = TREE_OPERAND (expression, 0);
25546 if (!expression)
25547 {
25548 /* If there are no operands, it must be an expression such
25549 as "int()". This should not happen for aggregate types
25550 because it would form non-constant expressions. */
25551 gcc_assert (cxx_dialect >= cxx11
25552 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25553
25554 return false;
25555 }
25556
25557 if (TREE_CODE (expression) == TREE_LIST)
25558 return any_value_dependent_elements_p (expression);
25559
25560 return value_dependent_expression_p (expression);
25561 }
25562
25563 case SIZEOF_EXPR:
25564 if (SIZEOF_EXPR_TYPE_P (expression))
25565 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25566 /* FALLTHRU */
25567 case ALIGNOF_EXPR:
25568 case TYPEID_EXPR:
25569 /* A `sizeof' expression is value-dependent if the operand is
25570 type-dependent or is a pack expansion. */
25571 expression = TREE_OPERAND (expression, 0);
25572 if (PACK_EXPANSION_P (expression))
25573 return true;
25574 else if (TYPE_P (expression))
25575 return dependent_type_p (expression);
25576 return instantiation_dependent_uneval_expression_p (expression);
25577
25578 case AT_ENCODE_EXPR:
25579 /* An 'encode' expression is value-dependent if the operand is
25580 type-dependent. */
25581 expression = TREE_OPERAND (expression, 0);
25582 return dependent_type_p (expression);
25583
25584 case NOEXCEPT_EXPR:
25585 expression = TREE_OPERAND (expression, 0);
25586 return instantiation_dependent_uneval_expression_p (expression);
25587
25588 case SCOPE_REF:
25589 /* All instantiation-dependent expressions should also be considered
25590 value-dependent. */
25591 return instantiation_dependent_scope_ref_p (expression);
25592
25593 case COMPONENT_REF:
25594 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25595 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25596
25597 case NONTYPE_ARGUMENT_PACK:
25598 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25599 is value-dependent. */
25600 {
25601 tree values = ARGUMENT_PACK_ARGS (expression);
25602 int i, len = TREE_VEC_LENGTH (values);
25603
25604 for (i = 0; i < len; ++i)
25605 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25606 return true;
25607
25608 return false;
25609 }
25610
25611 case TRAIT_EXPR:
25612 {
25613 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25614
25615 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25616 return true;
25617
25618 if (!type2)
25619 return false;
25620
25621 if (TREE_CODE (type2) != TREE_LIST)
25622 return dependent_type_p (type2);
25623
25624 for (; type2; type2 = TREE_CHAIN (type2))
25625 if (dependent_type_p (TREE_VALUE (type2)))
25626 return true;
25627
25628 return false;
25629 }
25630
25631 case MODOP_EXPR:
25632 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25633 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25634
25635 case ARRAY_REF:
25636 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25637 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25638
25639 case ADDR_EXPR:
25640 {
25641 tree op = TREE_OPERAND (expression, 0);
25642 return (value_dependent_expression_p (op)
25643 || has_value_dependent_address (op));
25644 }
25645
25646 case REQUIRES_EXPR:
25647 /* Treat all requires-expressions as value-dependent so
25648 we don't try to fold them. */
25649 return true;
25650
25651 case TYPE_REQ:
25652 return dependent_type_p (TREE_OPERAND (expression, 0));
25653
25654 case CALL_EXPR:
25655 {
25656 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25657 return true;
25658 tree fn = get_callee_fndecl (expression);
25659 int i, nargs;
25660 nargs = call_expr_nargs (expression);
25661 for (i = 0; i < nargs; ++i)
25662 {
25663 tree op = CALL_EXPR_ARG (expression, i);
25664 /* In a call to a constexpr member function, look through the
25665 implicit ADDR_EXPR on the object argument so that it doesn't
25666 cause the call to be considered value-dependent. We also
25667 look through it in potential_constant_expression. */
25668 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25670 && TREE_CODE (op) == ADDR_EXPR)
25671 op = TREE_OPERAND (op, 0);
25672 if (value_dependent_expression_p (op))
25673 return true;
25674 }
25675 return false;
25676 }
25677
25678 case TEMPLATE_ID_EXPR:
25679 return variable_concept_p (TREE_OPERAND (expression, 0));
25680
25681 case CONSTRUCTOR:
25682 {
25683 unsigned ix;
25684 tree val;
25685 if (dependent_type_p (TREE_TYPE (expression)))
25686 return true;
25687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25688 if (value_dependent_expression_p (val))
25689 return true;
25690 return false;
25691 }
25692
25693 case STMT_EXPR:
25694 /* Treat a GNU statement expression as dependent to avoid crashing
25695 under instantiate_non_dependent_expr; it can't be constant. */
25696 return true;
25697
25698 default:
25699 /* A constant expression is value-dependent if any subexpression is
25700 value-dependent. */
25701 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25702 {
25703 case tcc_reference:
25704 case tcc_unary:
25705 case tcc_comparison:
25706 case tcc_binary:
25707 case tcc_expression:
25708 case tcc_vl_exp:
25709 {
25710 int i, len = cp_tree_operand_length (expression);
25711
25712 for (i = 0; i < len; i++)
25713 {
25714 tree t = TREE_OPERAND (expression, i);
25715
25716 /* In some cases, some of the operands may be missing.
25717 (For example, in the case of PREDECREMENT_EXPR, the
25718 amount to increment by may be missing.) That doesn't
25719 make the expression dependent. */
25720 if (t && value_dependent_expression_p (t))
25721 return true;
25722 }
25723 }
25724 break;
25725 default:
25726 break;
25727 }
25728 break;
25729 }
25730
25731 /* The expression is not value-dependent. */
25732 return false;
25733 }
25734
25735 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25736 [temp.dep.expr]. Note that an expression with no type is
25737 considered dependent. Other parts of the compiler arrange for an
25738 expression with type-dependent subexpressions to have no type, so
25739 this function doesn't have to be fully recursive. */
25740
25741 bool
25742 type_dependent_expression_p (tree expression)
25743 {
25744 if (!processing_template_decl)
25745 return false;
25746
25747 if (expression == NULL_TREE || expression == error_mark_node)
25748 return false;
25749
25750 STRIP_ANY_LOCATION_WRAPPER (expression);
25751
25752 /* An unresolved name is always dependent. */
25753 if (identifier_p (expression)
25754 || TREE_CODE (expression) == USING_DECL
25755 || TREE_CODE (expression) == WILDCARD_DECL)
25756 return true;
25757
25758 /* A lambda-expression in template context is dependent. dependent_type_p is
25759 true for a lambda in the scope of a class or function template, but that
25760 doesn't cover all template contexts, like a default template argument. */
25761 if (TREE_CODE (expression) == LAMBDA_EXPR)
25762 return true;
25763
25764 /* A fold expression is type-dependent. */
25765 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25766 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25767 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25768 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25769 return true;
25770
25771 /* Some expression forms are never type-dependent. */
25772 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25773 || TREE_CODE (expression) == SIZEOF_EXPR
25774 || TREE_CODE (expression) == ALIGNOF_EXPR
25775 || TREE_CODE (expression) == AT_ENCODE_EXPR
25776 || TREE_CODE (expression) == NOEXCEPT_EXPR
25777 || TREE_CODE (expression) == TRAIT_EXPR
25778 || TREE_CODE (expression) == TYPEID_EXPR
25779 || TREE_CODE (expression) == DELETE_EXPR
25780 || TREE_CODE (expression) == VEC_DELETE_EXPR
25781 || TREE_CODE (expression) == THROW_EXPR
25782 || TREE_CODE (expression) == REQUIRES_EXPR)
25783 return false;
25784
25785 /* The types of these expressions depends only on the type to which
25786 the cast occurs. */
25787 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25788 || TREE_CODE (expression) == STATIC_CAST_EXPR
25789 || TREE_CODE (expression) == CONST_CAST_EXPR
25790 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25791 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25792 || TREE_CODE (expression) == CAST_EXPR)
25793 return dependent_type_p (TREE_TYPE (expression));
25794
25795 /* The types of these expressions depends only on the type created
25796 by the expression. */
25797 if (TREE_CODE (expression) == NEW_EXPR
25798 || TREE_CODE (expression) == VEC_NEW_EXPR)
25799 {
25800 /* For NEW_EXPR tree nodes created inside a template, either
25801 the object type itself or a TREE_LIST may appear as the
25802 operand 1. */
25803 tree type = TREE_OPERAND (expression, 1);
25804 if (TREE_CODE (type) == TREE_LIST)
25805 /* This is an array type. We need to check array dimensions
25806 as well. */
25807 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25808 || value_dependent_expression_p
25809 (TREE_OPERAND (TREE_VALUE (type), 1));
25810 else
25811 return dependent_type_p (type);
25812 }
25813
25814 if (TREE_CODE (expression) == SCOPE_REF)
25815 {
25816 tree scope = TREE_OPERAND (expression, 0);
25817 tree name = TREE_OPERAND (expression, 1);
25818
25819 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25820 contains an identifier associated by name lookup with one or more
25821 declarations declared with a dependent type, or...a
25822 nested-name-specifier or qualified-id that names a member of an
25823 unknown specialization. */
25824 return (type_dependent_expression_p (name)
25825 || dependent_scope_p (scope));
25826 }
25827
25828 if (TREE_CODE (expression) == TEMPLATE_DECL
25829 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25830 return uses_outer_template_parms (expression);
25831
25832 if (TREE_CODE (expression) == STMT_EXPR)
25833 expression = stmt_expr_value_expr (expression);
25834
25835 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25836 {
25837 tree elt;
25838 unsigned i;
25839
25840 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25841 {
25842 if (type_dependent_expression_p (elt))
25843 return true;
25844 }
25845 return false;
25846 }
25847
25848 /* A static data member of the current instantiation with incomplete
25849 array type is type-dependent, as the definition and specializations
25850 can have different bounds. */
25851 if (VAR_P (expression)
25852 && DECL_CLASS_SCOPE_P (expression)
25853 && dependent_type_p (DECL_CONTEXT (expression))
25854 && VAR_HAD_UNKNOWN_BOUND (expression))
25855 return true;
25856
25857 /* An array of unknown bound depending on a variadic parameter, eg:
25858
25859 template<typename... Args>
25860 void foo (Args... args)
25861 {
25862 int arr[] = { args... };
25863 }
25864
25865 template<int... vals>
25866 void bar ()
25867 {
25868 int arr[] = { vals... };
25869 }
25870
25871 If the array has no length and has an initializer, it must be that
25872 we couldn't determine its length in cp_complete_array_type because
25873 it is dependent. */
25874 if (VAR_P (expression)
25875 && TREE_TYPE (expression) != NULL_TREE
25876 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25877 && !TYPE_DOMAIN (TREE_TYPE (expression))
25878 && DECL_INITIAL (expression))
25879 return true;
25880
25881 /* A function or variable template-id is type-dependent if it has any
25882 dependent template arguments. */
25883 if (VAR_OR_FUNCTION_DECL_P (expression)
25884 && DECL_LANG_SPECIFIC (expression)
25885 && DECL_TEMPLATE_INFO (expression))
25886 {
25887 /* Consider the innermost template arguments, since those are the ones
25888 that come from the template-id; the template arguments for the
25889 enclosing class do not make it type-dependent unless they are used in
25890 the type of the decl. */
25891 if (instantiates_primary_template_p (expression)
25892 && (any_dependent_template_arguments_p
25893 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25894 return true;
25895 }
25896
25897 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25898 type-dependent. Checking this is important for functions with auto return
25899 type, which looks like a dependent type. */
25900 if (TREE_CODE (expression) == FUNCTION_DECL
25901 && !(DECL_CLASS_SCOPE_P (expression)
25902 && dependent_type_p (DECL_CONTEXT (expression)))
25903 && !(DECL_LANG_SPECIFIC (expression)
25904 && DECL_FRIEND_P (expression)
25905 && (!DECL_FRIEND_CONTEXT (expression)
25906 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25907 && !DECL_LOCAL_FUNCTION_P (expression))
25908 {
25909 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25910 || undeduced_auto_decl (expression));
25911 return false;
25912 }
25913
25914 /* Always dependent, on the number of arguments if nothing else. */
25915 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25916 return true;
25917
25918 if (TREE_TYPE (expression) == unknown_type_node)
25919 {
25920 if (TREE_CODE (expression) == ADDR_EXPR)
25921 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25922 if (TREE_CODE (expression) == COMPONENT_REF
25923 || TREE_CODE (expression) == OFFSET_REF)
25924 {
25925 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25926 return true;
25927 expression = TREE_OPERAND (expression, 1);
25928 if (identifier_p (expression))
25929 return false;
25930 }
25931 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25932 if (TREE_CODE (expression) == SCOPE_REF)
25933 return false;
25934
25935 if (BASELINK_P (expression))
25936 {
25937 if (BASELINK_OPTYPE (expression)
25938 && dependent_type_p (BASELINK_OPTYPE (expression)))
25939 return true;
25940 expression = BASELINK_FUNCTIONS (expression);
25941 }
25942
25943 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25944 {
25945 if (any_dependent_template_arguments_p
25946 (TREE_OPERAND (expression, 1)))
25947 return true;
25948 expression = TREE_OPERAND (expression, 0);
25949 if (identifier_p (expression))
25950 return true;
25951 }
25952
25953 gcc_assert (TREE_CODE (expression) == OVERLOAD
25954 || TREE_CODE (expression) == FUNCTION_DECL);
25955
25956 for (lkp_iterator iter (expression); iter; ++iter)
25957 if (type_dependent_expression_p (*iter))
25958 return true;
25959
25960 return false;
25961 }
25962
25963 /* The type of a non-type template parm declared with a placeholder type
25964 depends on the corresponding template argument, even though
25965 placeholders are not normally considered dependent. */
25966 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
25967 && is_auto (TREE_TYPE (expression)))
25968 return true;
25969
25970 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25971
25972 /* Dependent type attributes might not have made it from the decl to
25973 the type yet. */
25974 if (DECL_P (expression)
25975 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25976 return true;
25977
25978 return (dependent_type_p (TREE_TYPE (expression)));
25979 }
25980
25981 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25982 type-dependent if the expression refers to a member of the current
25983 instantiation and the type of the referenced member is dependent, or the
25984 class member access expression refers to a member of an unknown
25985 specialization.
25986
25987 This function returns true if the OBJECT in such a class member access
25988 expression is of an unknown specialization. */
25989
25990 bool
25991 type_dependent_object_expression_p (tree object)
25992 {
25993 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25994 dependent. */
25995 if (TREE_CODE (object) == IDENTIFIER_NODE)
25996 return true;
25997 tree scope = TREE_TYPE (object);
25998 return (!scope || dependent_scope_p (scope));
25999 }
26000
26001 /* walk_tree callback function for instantiation_dependent_expression_p,
26002 below. Returns non-zero if a dependent subexpression is found. */
26003
26004 static tree
26005 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26006 void * /*data*/)
26007 {
26008 if (TYPE_P (*tp))
26009 {
26010 /* We don't have to worry about decltype currently because decltype
26011 of an instantiation-dependent expr is a dependent type. This
26012 might change depending on the resolution of DR 1172. */
26013 *walk_subtrees = false;
26014 return NULL_TREE;
26015 }
26016 enum tree_code code = TREE_CODE (*tp);
26017 switch (code)
26018 {
26019 /* Don't treat an argument list as dependent just because it has no
26020 TREE_TYPE. */
26021 case TREE_LIST:
26022 case TREE_VEC:
26023 case NONTYPE_ARGUMENT_PACK:
26024 return NULL_TREE;
26025
26026 case TEMPLATE_PARM_INDEX:
26027 if (dependent_type_p (TREE_TYPE (*tp)))
26028 return *tp;
26029 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26030 return *tp;
26031 /* We'll check value-dependence separately. */
26032 return NULL_TREE;
26033
26034 /* Handle expressions with type operands. */
26035 case SIZEOF_EXPR:
26036 case ALIGNOF_EXPR:
26037 case TYPEID_EXPR:
26038 case AT_ENCODE_EXPR:
26039 {
26040 tree op = TREE_OPERAND (*tp, 0);
26041 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26042 op = TREE_TYPE (op);
26043 if (TYPE_P (op))
26044 {
26045 if (dependent_type_p (op))
26046 return *tp;
26047 else
26048 {
26049 *walk_subtrees = false;
26050 return NULL_TREE;
26051 }
26052 }
26053 break;
26054 }
26055
26056 case COMPONENT_REF:
26057 if (identifier_p (TREE_OPERAND (*tp, 1)))
26058 /* In a template, finish_class_member_access_expr creates a
26059 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26060 type-dependent, so that we can check access control at
26061 instantiation time (PR 42277). See also Core issue 1273. */
26062 return *tp;
26063 break;
26064
26065 case SCOPE_REF:
26066 if (instantiation_dependent_scope_ref_p (*tp))
26067 return *tp;
26068 else
26069 break;
26070
26071 /* Treat statement-expressions as dependent. */
26072 case BIND_EXPR:
26073 return *tp;
26074
26075 /* Treat requires-expressions as dependent. */
26076 case REQUIRES_EXPR:
26077 return *tp;
26078
26079 case CALL_EXPR:
26080 /* Treat calls to function concepts as dependent. */
26081 if (function_concept_check_p (*tp))
26082 return *tp;
26083 break;
26084
26085 case TEMPLATE_ID_EXPR:
26086 /* And variable concepts. */
26087 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26088 return *tp;
26089 break;
26090
26091 case CONSTRUCTOR:
26092 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26093 return *tp;
26094 break;
26095
26096 default:
26097 break;
26098 }
26099
26100 if (type_dependent_expression_p (*tp))
26101 return *tp;
26102 else
26103 return NULL_TREE;
26104 }
26105
26106 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26107 sense defined by the ABI:
26108
26109 "An expression is instantiation-dependent if it is type-dependent
26110 or value-dependent, or it has a subexpression that is type-dependent
26111 or value-dependent."
26112
26113 Except don't actually check value-dependence for unevaluated expressions,
26114 because in sizeof(i) we don't care about the value of i. Checking
26115 type-dependence will in turn check value-dependence of array bounds/template
26116 arguments as needed. */
26117
26118 bool
26119 instantiation_dependent_uneval_expression_p (tree expression)
26120 {
26121 tree result;
26122
26123 if (!processing_template_decl)
26124 return false;
26125
26126 if (expression == error_mark_node)
26127 return false;
26128
26129 result = cp_walk_tree_without_duplicates (&expression,
26130 instantiation_dependent_r, NULL);
26131 return result != NULL_TREE;
26132 }
26133
26134 /* As above, but also check value-dependence of the expression as a whole. */
26135
26136 bool
26137 instantiation_dependent_expression_p (tree expression)
26138 {
26139 return (instantiation_dependent_uneval_expression_p (expression)
26140 || value_dependent_expression_p (expression));
26141 }
26142
26143 /* Like type_dependent_expression_p, but it also works while not processing
26144 a template definition, i.e. during substitution or mangling. */
26145
26146 bool
26147 type_dependent_expression_p_push (tree expr)
26148 {
26149 bool b;
26150 ++processing_template_decl;
26151 b = type_dependent_expression_p (expr);
26152 --processing_template_decl;
26153 return b;
26154 }
26155
26156 /* Returns TRUE if ARGS contains a type-dependent expression. */
26157
26158 bool
26159 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26160 {
26161 unsigned int i;
26162 tree arg;
26163
26164 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26165 {
26166 if (type_dependent_expression_p (arg))
26167 return true;
26168 }
26169 return false;
26170 }
26171
26172 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26173 expressions) contains any type-dependent expressions. */
26174
26175 bool
26176 any_type_dependent_elements_p (const_tree list)
26177 {
26178 for (; list; list = TREE_CHAIN (list))
26179 if (type_dependent_expression_p (TREE_VALUE (list)))
26180 return true;
26181
26182 return false;
26183 }
26184
26185 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26186 expressions) contains any value-dependent expressions. */
26187
26188 bool
26189 any_value_dependent_elements_p (const_tree list)
26190 {
26191 for (; list; list = TREE_CHAIN (list))
26192 if (value_dependent_expression_p (TREE_VALUE (list)))
26193 return true;
26194
26195 return false;
26196 }
26197
26198 /* Returns TRUE if the ARG (a template argument) is dependent. */
26199
26200 bool
26201 dependent_template_arg_p (tree arg)
26202 {
26203 if (!processing_template_decl)
26204 return false;
26205
26206 /* Assume a template argument that was wrongly written by the user
26207 is dependent. This is consistent with what
26208 any_dependent_template_arguments_p [that calls this function]
26209 does. */
26210 if (!arg || arg == error_mark_node)
26211 return true;
26212
26213 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26214 arg = argument_pack_select_arg (arg);
26215
26216 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26217 return true;
26218 if (TREE_CODE (arg) == TEMPLATE_DECL)
26219 {
26220 if (DECL_TEMPLATE_PARM_P (arg))
26221 return true;
26222 /* A member template of a dependent class is not necessarily
26223 type-dependent, but it is a dependent template argument because it
26224 will be a member of an unknown specialization to that template. */
26225 tree scope = CP_DECL_CONTEXT (arg);
26226 return TYPE_P (scope) && dependent_type_p (scope);
26227 }
26228 else if (ARGUMENT_PACK_P (arg))
26229 {
26230 tree args = ARGUMENT_PACK_ARGS (arg);
26231 int i, len = TREE_VEC_LENGTH (args);
26232 for (i = 0; i < len; ++i)
26233 {
26234 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26235 return true;
26236 }
26237
26238 return false;
26239 }
26240 else if (TYPE_P (arg))
26241 return dependent_type_p (arg);
26242 else
26243 return (type_dependent_expression_p (arg)
26244 || value_dependent_expression_p (arg));
26245 }
26246
26247 /* Returns true if ARGS (a collection of template arguments) contains
26248 any types that require structural equality testing. */
26249
26250 bool
26251 any_template_arguments_need_structural_equality_p (tree args)
26252 {
26253 int i;
26254 int j;
26255
26256 if (!args)
26257 return false;
26258 if (args == error_mark_node)
26259 return true;
26260
26261 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26262 {
26263 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26264 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26265 {
26266 tree arg = TREE_VEC_ELT (level, j);
26267 tree packed_args = NULL_TREE;
26268 int k, len = 1;
26269
26270 if (ARGUMENT_PACK_P (arg))
26271 {
26272 /* Look inside the argument pack. */
26273 packed_args = ARGUMENT_PACK_ARGS (arg);
26274 len = TREE_VEC_LENGTH (packed_args);
26275 }
26276
26277 for (k = 0; k < len; ++k)
26278 {
26279 if (packed_args)
26280 arg = TREE_VEC_ELT (packed_args, k);
26281
26282 if (error_operand_p (arg))
26283 return true;
26284 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26285 continue;
26286 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26287 return true;
26288 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26289 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26290 return true;
26291 }
26292 }
26293 }
26294
26295 return false;
26296 }
26297
26298 /* Returns true if ARGS (a collection of template arguments) contains
26299 any dependent arguments. */
26300
26301 bool
26302 any_dependent_template_arguments_p (const_tree args)
26303 {
26304 int i;
26305 int j;
26306
26307 if (!args)
26308 return false;
26309 if (args == error_mark_node)
26310 return true;
26311
26312 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26313 {
26314 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26315 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26316 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26317 return true;
26318 }
26319
26320 return false;
26321 }
26322
26323 /* Returns true if ARGS contains any errors. */
26324
26325 bool
26326 any_erroneous_template_args_p (const_tree args)
26327 {
26328 int i;
26329 int j;
26330
26331 if (args == error_mark_node)
26332 return true;
26333
26334 if (args && TREE_CODE (args) != TREE_VEC)
26335 {
26336 if (tree ti = get_template_info (args))
26337 args = TI_ARGS (ti);
26338 else
26339 args = NULL_TREE;
26340 }
26341
26342 if (!args)
26343 return false;
26344
26345 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26346 {
26347 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26348 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26349 if (error_operand_p (TREE_VEC_ELT (level, j)))
26350 return true;
26351 }
26352
26353 return false;
26354 }
26355
26356 /* Returns TRUE if the template TMPL is type-dependent. */
26357
26358 bool
26359 dependent_template_p (tree tmpl)
26360 {
26361 if (TREE_CODE (tmpl) == OVERLOAD)
26362 {
26363 for (lkp_iterator iter (tmpl); iter; ++iter)
26364 if (dependent_template_p (*iter))
26365 return true;
26366 return false;
26367 }
26368
26369 /* Template template parameters are dependent. */
26370 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26371 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26372 return true;
26373 /* So are names that have not been looked up. */
26374 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26375 return true;
26376 return false;
26377 }
26378
26379 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26380
26381 bool
26382 dependent_template_id_p (tree tmpl, tree args)
26383 {
26384 return (dependent_template_p (tmpl)
26385 || any_dependent_template_arguments_p (args));
26386 }
26387
26388 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26389 are dependent. */
26390
26391 bool
26392 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26393 {
26394 int i;
26395
26396 if (!processing_template_decl)
26397 return false;
26398
26399 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26400 {
26401 tree decl = TREE_VEC_ELT (declv, i);
26402 tree init = TREE_VEC_ELT (initv, i);
26403 tree cond = TREE_VEC_ELT (condv, i);
26404 tree incr = TREE_VEC_ELT (incrv, i);
26405
26406 if (type_dependent_expression_p (decl)
26407 || TREE_CODE (decl) == SCOPE_REF)
26408 return true;
26409
26410 if (init && type_dependent_expression_p (init))
26411 return true;
26412
26413 if (cond == global_namespace)
26414 return true;
26415
26416 if (type_dependent_expression_p (cond))
26417 return true;
26418
26419 if (COMPARISON_CLASS_P (cond)
26420 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26421 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26422 return true;
26423
26424 if (TREE_CODE (incr) == MODOP_EXPR)
26425 {
26426 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26427 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26428 return true;
26429 }
26430 else if (type_dependent_expression_p (incr))
26431 return true;
26432 else if (TREE_CODE (incr) == MODIFY_EXPR)
26433 {
26434 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26435 return true;
26436 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26437 {
26438 tree t = TREE_OPERAND (incr, 1);
26439 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26440 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26441 return true;
26442
26443 /* If this loop has a class iterator with != comparison
26444 with increment other than i++/++i/i--/--i, make sure the
26445 increment is constant. */
26446 if (CLASS_TYPE_P (TREE_TYPE (decl))
26447 && TREE_CODE (cond) == NE_EXPR)
26448 {
26449 if (TREE_OPERAND (t, 0) == decl)
26450 t = TREE_OPERAND (t, 1);
26451 else
26452 t = TREE_OPERAND (t, 0);
26453 if (TREE_CODE (t) != INTEGER_CST)
26454 return true;
26455 }
26456 }
26457 }
26458 }
26459
26460 return false;
26461 }
26462
26463 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26464 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26465 no such TYPE can be found. Note that this function peers inside
26466 uninstantiated templates and therefore should be used only in
26467 extremely limited situations. ONLY_CURRENT_P restricts this
26468 peering to the currently open classes hierarchy (which is required
26469 when comparing types). */
26470
26471 tree
26472 resolve_typename_type (tree type, bool only_current_p)
26473 {
26474 tree scope;
26475 tree name;
26476 tree decl;
26477 int quals;
26478 tree pushed_scope;
26479 tree result;
26480
26481 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26482
26483 scope = TYPE_CONTEXT (type);
26484 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26485 gcc_checking_assert (uses_template_parms (scope));
26486
26487 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26488 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26489 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26490 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26491 identifier of the TYPENAME_TYPE anymore.
26492 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26493 TYPENAME_TYPE instead, we avoid messing up with a possible
26494 typedef variant case. */
26495 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26496
26497 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26498 it first before we can figure out what NAME refers to. */
26499 if (TREE_CODE (scope) == TYPENAME_TYPE)
26500 {
26501 if (TYPENAME_IS_RESOLVING_P (scope))
26502 /* Given a class template A with a dependent base with nested type C,
26503 typedef typename A::C::C C will land us here, as trying to resolve
26504 the initial A::C leads to the local C typedef, which leads back to
26505 A::C::C. So we break the recursion now. */
26506 return type;
26507 else
26508 scope = resolve_typename_type (scope, only_current_p);
26509 }
26510 /* If we don't know what SCOPE refers to, then we cannot resolve the
26511 TYPENAME_TYPE. */
26512 if (!CLASS_TYPE_P (scope))
26513 return type;
26514 /* If this is a typedef, we don't want to look inside (c++/11987). */
26515 if (typedef_variant_p (type))
26516 return type;
26517 /* If SCOPE isn't the template itself, it will not have a valid
26518 TYPE_FIELDS list. */
26519 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26520 /* scope is either the template itself or a compatible instantiation
26521 like X<T>, so look up the name in the original template. */
26522 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26523 /* If scope has no fields, it can't be a current instantiation. Check this
26524 before currently_open_class to avoid infinite recursion (71515). */
26525 if (!TYPE_FIELDS (scope))
26526 return type;
26527 /* If the SCOPE is not the current instantiation, there's no reason
26528 to look inside it. */
26529 if (only_current_p && !currently_open_class (scope))
26530 return type;
26531 /* Enter the SCOPE so that name lookup will be resolved as if we
26532 were in the class definition. In particular, SCOPE will no
26533 longer be considered a dependent type. */
26534 pushed_scope = push_scope (scope);
26535 /* Look up the declaration. */
26536 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26537 tf_warning_or_error);
26538
26539 result = NULL_TREE;
26540
26541 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26542 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26543 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26544 if (!decl)
26545 /*nop*/;
26546 else if (identifier_p (fullname)
26547 && TREE_CODE (decl) == TYPE_DECL)
26548 {
26549 result = TREE_TYPE (decl);
26550 if (result == error_mark_node)
26551 result = NULL_TREE;
26552 }
26553 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26554 && DECL_CLASS_TEMPLATE_P (decl))
26555 {
26556 /* Obtain the template and the arguments. */
26557 tree tmpl = TREE_OPERAND (fullname, 0);
26558 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26559 {
26560 /* We get here with a plain identifier because a previous tentative
26561 parse of the nested-name-specifier as part of a ptr-operator saw
26562 ::template X<A>. The use of ::template is necessary in a
26563 ptr-operator, but wrong in a declarator-id.
26564
26565 [temp.names]: In a qualified-id of a declarator-id, the keyword
26566 template shall not appear at the top level. */
26567 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26568 "keyword %<template%> not allowed in declarator-id");
26569 tmpl = decl;
26570 }
26571 tree args = TREE_OPERAND (fullname, 1);
26572 /* Instantiate the template. */
26573 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26574 /*entering_scope=*/true,
26575 tf_error | tf_user);
26576 if (result == error_mark_node)
26577 result = NULL_TREE;
26578 }
26579
26580 /* Leave the SCOPE. */
26581 if (pushed_scope)
26582 pop_scope (pushed_scope);
26583
26584 /* If we failed to resolve it, return the original typename. */
26585 if (!result)
26586 return type;
26587
26588 /* If lookup found a typename type, resolve that too. */
26589 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26590 {
26591 /* Ill-formed programs can cause infinite recursion here, so we
26592 must catch that. */
26593 TYPENAME_IS_RESOLVING_P (result) = 1;
26594 result = resolve_typename_type (result, only_current_p);
26595 TYPENAME_IS_RESOLVING_P (result) = 0;
26596 }
26597
26598 /* Qualify the resulting type. */
26599 quals = cp_type_quals (type);
26600 if (quals)
26601 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26602
26603 return result;
26604 }
26605
26606 /* EXPR is an expression which is not type-dependent. Return a proxy
26607 for EXPR that can be used to compute the types of larger
26608 expressions containing EXPR. */
26609
26610 tree
26611 build_non_dependent_expr (tree expr)
26612 {
26613 tree orig_expr = expr;
26614 tree inner_expr;
26615
26616 /* When checking, try to get a constant value for all non-dependent
26617 expressions in order to expose bugs in *_dependent_expression_p
26618 and constexpr. This can affect code generation, see PR70704, so
26619 only do this for -fchecking=2. */
26620 if (flag_checking > 1
26621 && cxx_dialect >= cxx11
26622 /* Don't do this during nsdmi parsing as it can lead to
26623 unexpected recursive instantiations. */
26624 && !parsing_nsdmi ()
26625 /* Don't do this during concept expansion either and for
26626 the same reason. */
26627 && !expanding_concept ())
26628 fold_non_dependent_expr (expr, tf_none);
26629
26630 STRIP_ANY_LOCATION_WRAPPER (expr);
26631
26632 /* Preserve OVERLOADs; the functions must be available to resolve
26633 types. */
26634 inner_expr = expr;
26635 if (TREE_CODE (inner_expr) == STMT_EXPR)
26636 inner_expr = stmt_expr_value_expr (inner_expr);
26637 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26638 inner_expr = TREE_OPERAND (inner_expr, 0);
26639 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26640 inner_expr = TREE_OPERAND (inner_expr, 1);
26641 if (is_overloaded_fn (inner_expr)
26642 || TREE_CODE (inner_expr) == OFFSET_REF)
26643 return orig_expr;
26644 /* There is no need to return a proxy for a variable or enumerator. */
26645 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26646 return orig_expr;
26647 /* Preserve string constants; conversions from string constants to
26648 "char *" are allowed, even though normally a "const char *"
26649 cannot be used to initialize a "char *". */
26650 if (TREE_CODE (expr) == STRING_CST)
26651 return orig_expr;
26652 /* Preserve void and arithmetic constants, as an optimization -- there is no
26653 reason to create a new node. */
26654 if (TREE_CODE (expr) == VOID_CST
26655 || TREE_CODE (expr) == INTEGER_CST
26656 || TREE_CODE (expr) == REAL_CST)
26657 return orig_expr;
26658 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26659 There is at least one place where we want to know that a
26660 particular expression is a throw-expression: when checking a ?:
26661 expression, there are special rules if the second or third
26662 argument is a throw-expression. */
26663 if (TREE_CODE (expr) == THROW_EXPR)
26664 return orig_expr;
26665
26666 /* Don't wrap an initializer list, we need to be able to look inside. */
26667 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26668 return orig_expr;
26669
26670 /* Don't wrap a dummy object, we need to be able to test for it. */
26671 if (is_dummy_object (expr))
26672 return orig_expr;
26673
26674 if (TREE_CODE (expr) == COND_EXPR)
26675 return build3 (COND_EXPR,
26676 TREE_TYPE (expr),
26677 TREE_OPERAND (expr, 0),
26678 (TREE_OPERAND (expr, 1)
26679 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26680 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26681 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26682 if (TREE_CODE (expr) == COMPOUND_EXPR
26683 && !COMPOUND_EXPR_OVERLOADED (expr))
26684 return build2 (COMPOUND_EXPR,
26685 TREE_TYPE (expr),
26686 TREE_OPERAND (expr, 0),
26687 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26688
26689 /* If the type is unknown, it can't really be non-dependent */
26690 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26691
26692 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26693 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26694 TREE_TYPE (expr), expr);
26695 }
26696
26697 /* ARGS is a vector of expressions as arguments to a function call.
26698 Replace the arguments with equivalent non-dependent expressions.
26699 This modifies ARGS in place. */
26700
26701 void
26702 make_args_non_dependent (vec<tree, va_gc> *args)
26703 {
26704 unsigned int ix;
26705 tree arg;
26706
26707 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26708 {
26709 tree newarg = build_non_dependent_expr (arg);
26710 if (newarg != arg)
26711 (*args)[ix] = newarg;
26712 }
26713 }
26714
26715 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26716 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26717 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26718
26719 static tree
26720 make_auto_1 (tree name, bool set_canonical)
26721 {
26722 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26723 TYPE_NAME (au) = build_decl (input_location,
26724 TYPE_DECL, name, au);
26725 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26726 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26727 (0, processing_template_decl + 1, processing_template_decl + 1,
26728 TYPE_NAME (au), NULL_TREE);
26729 if (set_canonical)
26730 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26731 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26732 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26733
26734 return au;
26735 }
26736
26737 tree
26738 make_decltype_auto (void)
26739 {
26740 return make_auto_1 (decltype_auto_identifier, true);
26741 }
26742
26743 tree
26744 make_auto (void)
26745 {
26746 return make_auto_1 (auto_identifier, true);
26747 }
26748
26749 /* Return a C++17 deduction placeholder for class template TMPL. */
26750
26751 tree
26752 make_template_placeholder (tree tmpl)
26753 {
26754 tree t = make_auto_1 (auto_identifier, false);
26755 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26756 /* Our canonical type depends on the placeholder. */
26757 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26758 return t;
26759 }
26760
26761 /* True iff T is a C++17 class template deduction placeholder. */
26762
26763 bool
26764 template_placeholder_p (tree t)
26765 {
26766 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26767 }
26768
26769 /* Make a "constrained auto" type-specifier. This is an
26770 auto type with constraints that must be associated after
26771 deduction. The constraint is formed from the given
26772 CONC and its optional sequence of arguments, which are
26773 non-null if written as partial-concept-id. */
26774
26775 tree
26776 make_constrained_auto (tree con, tree args)
26777 {
26778 tree type = make_auto_1 (auto_identifier, false);
26779
26780 /* Build the constraint. */
26781 tree tmpl = DECL_TI_TEMPLATE (con);
26782 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26783 expr = build_concept_check (expr, type, args);
26784
26785 tree constr = normalize_expression (expr);
26786 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26787
26788 /* Our canonical type depends on the constraint. */
26789 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26790
26791 /* Attach the constraint to the type declaration. */
26792 tree decl = TYPE_NAME (type);
26793 return decl;
26794 }
26795
26796 /* Given type ARG, return std::initializer_list<ARG>. */
26797
26798 static tree
26799 listify (tree arg)
26800 {
26801 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26802
26803 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26804 {
26805 gcc_rich_location richloc (input_location);
26806 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26807 error_at (&richloc,
26808 "deducing from brace-enclosed initializer list"
26809 " requires %<#include <initializer_list>%>");
26810
26811 return error_mark_node;
26812 }
26813 tree argvec = make_tree_vec (1);
26814 TREE_VEC_ELT (argvec, 0) = arg;
26815
26816 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26817 NULL_TREE, 0, tf_warning_or_error);
26818 }
26819
26820 /* Replace auto in TYPE with std::initializer_list<auto>. */
26821
26822 static tree
26823 listify_autos (tree type, tree auto_node)
26824 {
26825 tree init_auto = listify (auto_node);
26826 tree argvec = make_tree_vec (1);
26827 TREE_VEC_ELT (argvec, 0) = init_auto;
26828 if (processing_template_decl)
26829 argvec = add_to_template_args (current_template_args (), argvec);
26830 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26831 }
26832
26833 /* Hash traits for hashing possibly constrained 'auto'
26834 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26835
26836 struct auto_hash : default_hash_traits<tree>
26837 {
26838 static inline hashval_t hash (tree);
26839 static inline bool equal (tree, tree);
26840 };
26841
26842 /* Hash the 'auto' T. */
26843
26844 inline hashval_t
26845 auto_hash::hash (tree t)
26846 {
26847 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26848 /* Matching constrained-type-specifiers denote the same template
26849 parameter, so hash the constraint. */
26850 return hash_placeholder_constraint (c);
26851 else
26852 /* But unconstrained autos are all separate, so just hash the pointer. */
26853 return iterative_hash_object (t, 0);
26854 }
26855
26856 /* Compare two 'auto's. */
26857
26858 inline bool
26859 auto_hash::equal (tree t1, tree t2)
26860 {
26861 if (t1 == t2)
26862 return true;
26863
26864 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26865 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26866
26867 /* Two unconstrained autos are distinct. */
26868 if (!c1 || !c2)
26869 return false;
26870
26871 return equivalent_placeholder_constraints (c1, c2);
26872 }
26873
26874 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26875 constrained) auto, add it to the vector. */
26876
26877 static int
26878 extract_autos_r (tree t, void *data)
26879 {
26880 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26881 if (is_auto (t))
26882 {
26883 /* All the autos were built with index 0; fix that up now. */
26884 tree *p = hash.find_slot (t, INSERT);
26885 unsigned idx;
26886 if (*p)
26887 /* If this is a repeated constrained-type-specifier, use the index we
26888 chose before. */
26889 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26890 else
26891 {
26892 /* Otherwise this is new, so use the current count. */
26893 *p = t;
26894 idx = hash.elements () - 1;
26895 }
26896 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26897 }
26898
26899 /* Always keep walking. */
26900 return 0;
26901 }
26902
26903 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26904 says they can appear anywhere in the type. */
26905
26906 static tree
26907 extract_autos (tree type)
26908 {
26909 hash_set<tree> visited;
26910 hash_table<auto_hash> hash (2);
26911
26912 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26913
26914 tree tree_vec = make_tree_vec (hash.elements());
26915 for (hash_table<auto_hash>::iterator iter = hash.begin();
26916 iter != hash.end(); ++iter)
26917 {
26918 tree elt = *iter;
26919 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26920 TREE_VEC_ELT (tree_vec, i)
26921 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26922 }
26923
26924 return tree_vec;
26925 }
26926
26927 /* The stem for deduction guide names. */
26928 const char *const dguide_base = "__dguide_";
26929
26930 /* Return the name for a deduction guide for class template TMPL. */
26931
26932 tree
26933 dguide_name (tree tmpl)
26934 {
26935 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26936 tree tname = TYPE_IDENTIFIER (type);
26937 char *buf = (char *) alloca (1 + strlen (dguide_base)
26938 + IDENTIFIER_LENGTH (tname));
26939 memcpy (buf, dguide_base, strlen (dguide_base));
26940 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26941 IDENTIFIER_LENGTH (tname) + 1);
26942 tree dname = get_identifier (buf);
26943 TREE_TYPE (dname) = type;
26944 return dname;
26945 }
26946
26947 /* True if NAME is the name of a deduction guide. */
26948
26949 bool
26950 dguide_name_p (tree name)
26951 {
26952 return (TREE_CODE (name) == IDENTIFIER_NODE
26953 && TREE_TYPE (name)
26954 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26955 strlen (dguide_base)));
26956 }
26957
26958 /* True if FN is a deduction guide. */
26959
26960 bool
26961 deduction_guide_p (const_tree fn)
26962 {
26963 if (DECL_P (fn))
26964 if (tree name = DECL_NAME (fn))
26965 return dguide_name_p (name);
26966 return false;
26967 }
26968
26969 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26970
26971 bool
26972 copy_guide_p (const_tree fn)
26973 {
26974 gcc_assert (deduction_guide_p (fn));
26975 if (!DECL_ARTIFICIAL (fn))
26976 return false;
26977 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26978 return (TREE_CHAIN (parms) == void_list_node
26979 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26980 }
26981
26982 /* True if FN is a guide generated from a constructor template. */
26983
26984 bool
26985 template_guide_p (const_tree fn)
26986 {
26987 gcc_assert (deduction_guide_p (fn));
26988 if (!DECL_ARTIFICIAL (fn))
26989 return false;
26990 tree tmpl = DECL_TI_TEMPLATE (fn);
26991 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26992 return PRIMARY_TEMPLATE_P (org);
26993 return false;
26994 }
26995
26996 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26997 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26998 template parameter types. Note that the handling of template template
26999 parameters relies on current_template_parms being set appropriately for the
27000 new template. */
27001
27002 static tree
27003 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27004 tree tsubst_args, tsubst_flags_t complain)
27005 {
27006 if (olddecl == error_mark_node)
27007 return error_mark_node;
27008
27009 tree oldidx = get_template_parm_index (olddecl);
27010
27011 tree newtype;
27012 if (TREE_CODE (olddecl) == TYPE_DECL
27013 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27014 {
27015 tree oldtype = TREE_TYPE (olddecl);
27016 newtype = cxx_make_type (TREE_CODE (oldtype));
27017 TYPE_MAIN_VARIANT (newtype) = newtype;
27018 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27019 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27020 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27021 }
27022 else
27023 {
27024 newtype = TREE_TYPE (olddecl);
27025 if (type_uses_auto (newtype))
27026 {
27027 // Substitute once to fix references to other template parameters.
27028 newtype = tsubst (newtype, tsubst_args,
27029 complain|tf_partial, NULL_TREE);
27030 // Now substitute again to reduce the level of the auto.
27031 newtype = tsubst (newtype, current_template_args (),
27032 complain, NULL_TREE);
27033 }
27034 else
27035 newtype = tsubst (newtype, tsubst_args,
27036 complain, NULL_TREE);
27037 }
27038
27039 tree newdecl
27040 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27041 DECL_NAME (olddecl), newtype);
27042 SET_DECL_TEMPLATE_PARM_P (newdecl);
27043
27044 tree newidx;
27045 if (TREE_CODE (olddecl) == TYPE_DECL
27046 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27047 {
27048 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27049 = build_template_parm_index (index, level, level,
27050 newdecl, newtype);
27051 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27052 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27053 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27054 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27055
27056 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27057 {
27058 DECL_TEMPLATE_RESULT (newdecl)
27059 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27060 DECL_NAME (olddecl), newtype);
27061 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27062 // First create a copy (ttargs) of tsubst_args with an
27063 // additional level for the template template parameter's own
27064 // template parameters (ttparms).
27065 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27066 (DECL_TEMPLATE_PARMS (olddecl)));
27067 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27068 tree ttargs = make_tree_vec (depth + 1);
27069 for (int i = 0; i < depth; ++i)
27070 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27071 TREE_VEC_ELT (ttargs, depth)
27072 = template_parms_level_to_args (ttparms);
27073 // Substitute ttargs into ttparms to fix references to
27074 // other template parameters.
27075 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27076 complain|tf_partial);
27077 // Now substitute again with args based on tparms, to reduce
27078 // the level of the ttparms.
27079 ttargs = current_template_args ();
27080 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27081 complain);
27082 // Finally, tack the adjusted parms onto tparms.
27083 ttparms = tree_cons (size_int (depth), ttparms,
27084 current_template_parms);
27085 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27086 }
27087 }
27088 else
27089 {
27090 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27091 tree newconst
27092 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27093 TREE_CODE (oldconst),
27094 DECL_NAME (oldconst), newtype);
27095 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27096 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27097 SET_DECL_TEMPLATE_PARM_P (newconst);
27098 newidx = build_template_parm_index (index, level, level,
27099 newconst, newtype);
27100 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27101 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27102 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27103 }
27104
27105 return newdecl;
27106 }
27107
27108 /* Returns a C++17 class deduction guide template based on the constructor
27109 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27110 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27111
27112 static tree
27113 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27114 {
27115 tree type, tparms, targs, fparms, fargs, ci;
27116 bool memtmpl = false;
27117 bool explicit_p;
27118 location_t loc;
27119 tree fn_tmpl = NULL_TREE;
27120
27121 if (TYPE_P (ctor))
27122 {
27123 type = ctor;
27124 bool copy_p = TYPE_REF_P (type);
27125 if (copy_p)
27126 {
27127 type = TREE_TYPE (type);
27128 fparms = tree_cons (NULL_TREE, type, void_list_node);
27129 }
27130 else
27131 fparms = void_list_node;
27132
27133 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27134 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27135 targs = CLASSTYPE_TI_ARGS (type);
27136 ci = NULL_TREE;
27137 fargs = NULL_TREE;
27138 loc = DECL_SOURCE_LOCATION (ctmpl);
27139 explicit_p = false;
27140 }
27141 else
27142 {
27143 ++processing_template_decl;
27144 bool ok = true;
27145
27146 fn_tmpl
27147 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27148 : DECL_TI_TEMPLATE (ctor));
27149 if (outer_args)
27150 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27151 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27152
27153 type = DECL_CONTEXT (ctor);
27154
27155 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27156 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27157 fully specialized args for the enclosing class. Strip those off, as
27158 the deduction guide won't have those template parameters. */
27159 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27160 TMPL_PARMS_DEPTH (tparms));
27161 /* Discard the 'this' parameter. */
27162 fparms = FUNCTION_ARG_CHAIN (ctor);
27163 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27164 ci = get_constraints (ctor);
27165 loc = DECL_SOURCE_LOCATION (ctor);
27166 explicit_p = DECL_NONCONVERTING_P (ctor);
27167
27168 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27169 {
27170 memtmpl = true;
27171
27172 /* For a member template constructor, we need to flatten the two
27173 template parameter lists into one, and then adjust the function
27174 signature accordingly. This gets...complicated. */
27175 tree save_parms = current_template_parms;
27176
27177 /* For a member template we should have two levels of parms/args, one
27178 for the class and one for the constructor. We stripped
27179 specialized args for further enclosing classes above. */
27180 const int depth = 2;
27181 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27182
27183 /* Template args for translating references to the two-level template
27184 parameters into references to the one-level template parameters we
27185 are creating. */
27186 tree tsubst_args = copy_node (targs);
27187 TMPL_ARGS_LEVEL (tsubst_args, depth)
27188 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27189
27190 /* Template parms for the constructor template. */
27191 tree ftparms = TREE_VALUE (tparms);
27192 unsigned flen = TREE_VEC_LENGTH (ftparms);
27193 /* Template parms for the class template. */
27194 tparms = TREE_CHAIN (tparms);
27195 tree ctparms = TREE_VALUE (tparms);
27196 unsigned clen = TREE_VEC_LENGTH (ctparms);
27197 /* Template parms for the deduction guide start as a copy of the
27198 template parms for the class. We set current_template_parms for
27199 lookup_template_class_1. */
27200 current_template_parms = tparms = copy_node (tparms);
27201 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27202 for (unsigned i = 0; i < clen; ++i)
27203 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27204
27205 /* Now we need to rewrite the constructor parms to append them to the
27206 class parms. */
27207 for (unsigned i = 0; i < flen; ++i)
27208 {
27209 unsigned index = i + clen;
27210 unsigned level = 1;
27211 tree oldelt = TREE_VEC_ELT (ftparms, i);
27212 tree olddecl = TREE_VALUE (oldelt);
27213 tree newdecl = rewrite_template_parm (olddecl, index, level,
27214 tsubst_args, complain);
27215 if (newdecl == error_mark_node)
27216 ok = false;
27217 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27218 tsubst_args, complain, ctor);
27219 tree list = build_tree_list (newdef, newdecl);
27220 TEMPLATE_PARM_CONSTRAINTS (list)
27221 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27222 tsubst_args, complain, ctor);
27223 TREE_VEC_ELT (new_vec, index) = list;
27224 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27225 }
27226
27227 /* Now we have a final set of template parms to substitute into the
27228 function signature. */
27229 targs = template_parms_to_args (tparms);
27230 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27231 complain, ctor);
27232 if (fparms == error_mark_node)
27233 ok = false;
27234 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27235 if (ci)
27236 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27237
27238 current_template_parms = save_parms;
27239 }
27240
27241 --processing_template_decl;
27242 if (!ok)
27243 return error_mark_node;
27244 }
27245
27246 if (!memtmpl)
27247 {
27248 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27249 tparms = copy_node (tparms);
27250 INNERMOST_TEMPLATE_PARMS (tparms)
27251 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27252 }
27253
27254 tree fntype = build_function_type (type, fparms);
27255 tree ded_fn = build_lang_decl_loc (loc,
27256 FUNCTION_DECL,
27257 dguide_name (type), fntype);
27258 DECL_ARGUMENTS (ded_fn) = fargs;
27259 DECL_ARTIFICIAL (ded_fn) = true;
27260 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27261 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27262 DECL_ARTIFICIAL (ded_tmpl) = true;
27263 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27264 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27265 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27266 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27267 if (DECL_P (ctor))
27268 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27269 if (ci)
27270 set_constraints (ded_tmpl, ci);
27271
27272 return ded_tmpl;
27273 }
27274
27275 /* Deduce template arguments for the class template placeholder PTYPE for
27276 template TMPL based on the initializer INIT, and return the resulting
27277 type. */
27278
27279 static tree
27280 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27281 tsubst_flags_t complain)
27282 {
27283 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27284 {
27285 /* We should have handled this in the caller. */
27286 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27287 return ptype;
27288 if (complain & tf_error)
27289 error ("non-class template %qT used without template arguments", tmpl);
27290 return error_mark_node;
27291 }
27292 if (init && TREE_TYPE (init) == ptype)
27293 /* Using the template parm as its own argument. */
27294 return ptype;
27295
27296 tree type = TREE_TYPE (tmpl);
27297
27298 bool try_list_ctor = false;
27299
27300 releasing_vec rv_args = NULL;
27301 vec<tree,va_gc> *&args = *&rv_args;
27302 if (init == NULL_TREE
27303 || TREE_CODE (init) == TREE_LIST)
27304 args = make_tree_vector_from_list (init);
27305 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27306 {
27307 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27308 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27309 {
27310 /* As an exception, the first phase in 16.3.1.7 (considering the
27311 initializer list as a single argument) is omitted if the
27312 initializer list consists of a single expression of type cv U,
27313 where U is a specialization of C or a class derived from a
27314 specialization of C. */
27315 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27316 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
27317 {
27318 tree etype = TREE_TYPE (elt);
27319 tree tparms = (INNERMOST_TEMPLATE_PARMS
27320 (DECL_TEMPLATE_PARMS (tmpl)));
27321 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27322 int err = unify (tparms, targs, type, etype,
27323 UNIFY_ALLOW_DERIVED, /*explain*/false);
27324 if (err == 0)
27325 try_list_ctor = false;
27326 ggc_free (targs);
27327 }
27328 }
27329 if (try_list_ctor || is_std_init_list (type))
27330 args = make_tree_vector_single (init);
27331 else
27332 args = make_tree_vector_from_ctor (init);
27333 }
27334 else
27335 args = make_tree_vector_single (init);
27336
27337 tree dname = dguide_name (tmpl);
27338 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27339 /*type*/false, /*complain*/false,
27340 /*hidden*/false);
27341 bool elided = false;
27342 if (cands == error_mark_node)
27343 cands = NULL_TREE;
27344
27345 /* Prune explicit deduction guides in copy-initialization context. */
27346 if (flags & LOOKUP_ONLYCONVERTING)
27347 {
27348 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27349 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27350 elided = true;
27351
27352 if (elided)
27353 {
27354 /* Found a nonconverting guide, prune the candidates. */
27355 tree pruned = NULL_TREE;
27356 for (lkp_iterator iter (cands); iter; ++iter)
27357 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27358 pruned = lookup_add (*iter, pruned);
27359
27360 cands = pruned;
27361 }
27362 }
27363
27364 tree outer_args = NULL_TREE;
27365 if (DECL_CLASS_SCOPE_P (tmpl)
27366 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27367 {
27368 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27369 type = TREE_TYPE (most_general_template (tmpl));
27370 }
27371
27372 bool saw_ctor = false;
27373 // FIXME cache artificial deduction guides
27374 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27375 {
27376 /* Skip inherited constructors. */
27377 if (iter.using_p ())
27378 continue;
27379
27380 tree guide = build_deduction_guide (*iter, outer_args, complain);
27381 if (guide == error_mark_node)
27382 return error_mark_node;
27383 if ((flags & LOOKUP_ONLYCONVERTING)
27384 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27385 elided = true;
27386 else
27387 cands = lookup_add (guide, cands);
27388
27389 saw_ctor = true;
27390 }
27391
27392 tree call = error_mark_node;
27393
27394 /* If this is list-initialization and the class has a list constructor, first
27395 try deducing from the list as a single argument, as [over.match.list]. */
27396 tree list_cands = NULL_TREE;
27397 if (try_list_ctor && cands)
27398 for (lkp_iterator iter (cands); iter; ++iter)
27399 {
27400 tree dg = *iter;
27401 if (is_list_ctor (dg))
27402 list_cands = lookup_add (dg, list_cands);
27403 }
27404 if (list_cands)
27405 {
27406 ++cp_unevaluated_operand;
27407 call = build_new_function_call (list_cands, &args, tf_decltype);
27408 --cp_unevaluated_operand;
27409
27410 if (call == error_mark_node)
27411 {
27412 /* That didn't work, now try treating the list as a sequence of
27413 arguments. */
27414 release_tree_vector (args);
27415 args = make_tree_vector_from_ctor (init);
27416 }
27417 }
27418
27419 /* Maybe generate an implicit deduction guide. */
27420 if (call == error_mark_node && args->length () < 2)
27421 {
27422 tree gtype = NULL_TREE;
27423
27424 if (args->length () == 1)
27425 /* Generate a copy guide. */
27426 gtype = build_reference_type (type);
27427 else if (!saw_ctor)
27428 /* Generate a default guide. */
27429 gtype = type;
27430
27431 if (gtype)
27432 {
27433 tree guide = build_deduction_guide (gtype, outer_args, complain);
27434 if (guide == error_mark_node)
27435 return error_mark_node;
27436 cands = lookup_add (guide, cands);
27437 }
27438 }
27439
27440 if (elided && !cands)
27441 {
27442 error ("cannot deduce template arguments for copy-initialization"
27443 " of %qT, as it has no non-explicit deduction guides or "
27444 "user-declared constructors", type);
27445 return error_mark_node;
27446 }
27447 else if (!cands && call == error_mark_node)
27448 {
27449 error ("cannot deduce template arguments of %qT, as it has no viable "
27450 "deduction guides", type);
27451 return error_mark_node;
27452 }
27453
27454 if (call == error_mark_node)
27455 {
27456 ++cp_unevaluated_operand;
27457 call = build_new_function_call (cands, &args, tf_decltype);
27458 --cp_unevaluated_operand;
27459 }
27460
27461 if (call == error_mark_node && (complain & tf_warning_or_error))
27462 {
27463 error ("class template argument deduction failed:");
27464
27465 ++cp_unevaluated_operand;
27466 call = build_new_function_call (cands, &args, complain | tf_decltype);
27467 --cp_unevaluated_operand;
27468
27469 if (elided)
27470 inform (input_location, "explicit deduction guides not considered "
27471 "for copy-initialization");
27472 }
27473
27474 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27475 }
27476
27477 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27478 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27479 The CONTEXT determines the context in which auto deduction is performed
27480 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27481 OUTER_TARGS are used during template argument deduction
27482 (context == adc_unify) to properly substitute the result, and is ignored
27483 in other contexts.
27484
27485 For partial-concept-ids, extra args may be appended to the list of deduced
27486 template arguments prior to determining constraint satisfaction. */
27487
27488 tree
27489 do_auto_deduction (tree type, tree init, tree auto_node,
27490 tsubst_flags_t complain, auto_deduction_context context,
27491 tree outer_targs, int flags)
27492 {
27493 tree targs;
27494
27495 if (init == error_mark_node)
27496 return error_mark_node;
27497
27498 if (init && type_dependent_expression_p (init)
27499 && context != adc_unify)
27500 /* Defining a subset of type-dependent expressions that we can deduce
27501 from ahead of time isn't worth the trouble. */
27502 return type;
27503
27504 /* Similarly, we can't deduce from another undeduced decl. */
27505 if (init && undeduced_auto_decl (init))
27506 return type;
27507
27508 /* We may be doing a partial substitution, but we still want to replace
27509 auto_node. */
27510 complain &= ~tf_partial;
27511
27512 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27513 /* C++17 class template argument deduction. */
27514 return do_class_deduction (type, tmpl, init, flags, complain);
27515
27516 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27517 /* Nothing we can do with this, even in deduction context. */
27518 return type;
27519
27520 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27521 with either a new invented type template parameter U or, if the
27522 initializer is a braced-init-list (8.5.4), with
27523 std::initializer_list<U>. */
27524 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27525 {
27526 if (!DIRECT_LIST_INIT_P (init))
27527 type = listify_autos (type, auto_node);
27528 else if (CONSTRUCTOR_NELTS (init) == 1)
27529 init = CONSTRUCTOR_ELT (init, 0)->value;
27530 else
27531 {
27532 if (complain & tf_warning_or_error)
27533 {
27534 if (permerror (input_location, "direct-list-initialization of "
27535 "%<auto%> requires exactly one element"))
27536 inform (input_location,
27537 "for deduction to %<std::initializer_list%>, use copy-"
27538 "list-initialization (i.e. add %<=%> before the %<{%>)");
27539 }
27540 type = listify_autos (type, auto_node);
27541 }
27542 }
27543
27544 if (type == error_mark_node)
27545 return error_mark_node;
27546
27547 init = resolve_nondeduced_context (init, complain);
27548
27549 if (context == adc_decomp_type
27550 && auto_node == type
27551 && init != error_mark_node
27552 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27553 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27554 and initializer has array type, deduce cv-qualified array type. */
27555 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27556 complain);
27557 else if (AUTO_IS_DECLTYPE (auto_node))
27558 {
27559 tree stripped_init = tree_strip_any_location_wrapper (init);
27560 bool id = (DECL_P (stripped_init)
27561 || ((TREE_CODE (init) == COMPONENT_REF
27562 || TREE_CODE (init) == SCOPE_REF)
27563 && !REF_PARENTHESIZED_P (init)));
27564 targs = make_tree_vec (1);
27565 TREE_VEC_ELT (targs, 0)
27566 = finish_decltype_type (init, id, tf_warning_or_error);
27567 if (type != auto_node)
27568 {
27569 if (complain & tf_error)
27570 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27571 return error_mark_node;
27572 }
27573 }
27574 else
27575 {
27576 tree parms = build_tree_list (NULL_TREE, type);
27577 tree tparms;
27578
27579 if (flag_concepts)
27580 tparms = extract_autos (type);
27581 else
27582 {
27583 tparms = make_tree_vec (1);
27584 TREE_VEC_ELT (tparms, 0)
27585 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27586 }
27587
27588 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27589 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27590 DEDUCE_CALL,
27591 NULL, /*explain_p=*/false);
27592 if (val > 0)
27593 {
27594 if (processing_template_decl)
27595 /* Try again at instantiation time. */
27596 return type;
27597 if (type && type != error_mark_node
27598 && (complain & tf_error))
27599 /* If type is error_mark_node a diagnostic must have been
27600 emitted by now. Also, having a mention to '<type error>'
27601 in the diagnostic is not really useful to the user. */
27602 {
27603 if (cfun
27604 && FNDECL_USED_AUTO (current_function_decl)
27605 && (auto_node
27606 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
27607 && LAMBDA_FUNCTION_P (current_function_decl))
27608 error ("unable to deduce lambda return type from %qE", init);
27609 else
27610 error ("unable to deduce %qT from %qE", type, init);
27611 type_unification_real (tparms, targs, parms, &init, 1, 0,
27612 DEDUCE_CALL,
27613 NULL, /*explain_p=*/true);
27614 }
27615 return error_mark_node;
27616 }
27617 }
27618
27619 /* Check any placeholder constraints against the deduced type. */
27620 if (flag_concepts && !processing_template_decl)
27621 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27622 {
27623 /* Use the deduced type to check the associated constraints. If we
27624 have a partial-concept-id, rebuild the argument list so that
27625 we check using the extra arguments. */
27626 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27627 tree cargs = CHECK_CONSTR_ARGS (constr);
27628 if (TREE_VEC_LENGTH (cargs) > 1)
27629 {
27630 cargs = copy_node (cargs);
27631 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27632 }
27633 else
27634 cargs = targs;
27635 if (!constraints_satisfied_p (constr, cargs))
27636 {
27637 if (complain & tf_warning_or_error)
27638 {
27639 auto_diagnostic_group d;
27640 switch (context)
27641 {
27642 case adc_unspecified:
27643 case adc_unify:
27644 error("placeholder constraints not satisfied");
27645 break;
27646 case adc_variable_type:
27647 case adc_decomp_type:
27648 error ("deduced initializer does not satisfy "
27649 "placeholder constraints");
27650 break;
27651 case adc_return_type:
27652 error ("deduced return type does not satisfy "
27653 "placeholder constraints");
27654 break;
27655 case adc_requirement:
27656 error ("deduced expression type does not satisfy "
27657 "placeholder constraints");
27658 break;
27659 }
27660 diagnose_constraints (input_location, constr, targs);
27661 }
27662 return error_mark_node;
27663 }
27664 }
27665
27666 if (processing_template_decl && context != adc_unify)
27667 outer_targs = current_template_args ();
27668 targs = add_to_template_args (outer_targs, targs);
27669 return tsubst (type, targs, complain, NULL_TREE);
27670 }
27671
27672 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27673 result. */
27674
27675 tree
27676 splice_late_return_type (tree type, tree late_return_type)
27677 {
27678 if (is_auto (type))
27679 {
27680 if (late_return_type)
27681 return late_return_type;
27682
27683 tree idx = get_template_parm_index (type);
27684 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27685 /* In an abbreviated function template we didn't know we were dealing
27686 with a function template when we saw the auto return type, so update
27687 it to have the correct level. */
27688 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27689 }
27690 return type;
27691 }
27692
27693 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27694 'decltype(auto)' or a deduced class template. */
27695
27696 bool
27697 is_auto (const_tree type)
27698 {
27699 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27700 && (TYPE_IDENTIFIER (type) == auto_identifier
27701 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27702 return true;
27703 else
27704 return false;
27705 }
27706
27707 /* for_each_template_parm callback for type_uses_auto. */
27708
27709 int
27710 is_auto_r (tree tp, void */*data*/)
27711 {
27712 return is_auto (tp);
27713 }
27714
27715 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27716 a use of `auto'. Returns NULL_TREE otherwise. */
27717
27718 tree
27719 type_uses_auto (tree type)
27720 {
27721 if (type == NULL_TREE)
27722 return NULL_TREE;
27723 else if (flag_concepts)
27724 {
27725 /* The Concepts TS allows multiple autos in one type-specifier; just
27726 return the first one we find, do_auto_deduction will collect all of
27727 them. */
27728 if (uses_template_parms (type))
27729 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27730 /*visited*/NULL, /*nondeduced*/false);
27731 else
27732 return NULL_TREE;
27733 }
27734 else
27735 return find_type_usage (type, is_auto);
27736 }
27737
27738 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27739 concepts are enabled, auto is acceptable in template arguments, but
27740 only when TEMPL identifies a template class. Return TRUE if any
27741 such errors were reported. */
27742
27743 bool
27744 check_auto_in_tmpl_args (tree tmpl, tree args)
27745 {
27746 /* If there were previous errors, nevermind. */
27747 if (!args || TREE_CODE (args) != TREE_VEC)
27748 return false;
27749
27750 /* If TMPL is an identifier, we're parsing and we can't tell yet
27751 whether TMPL is supposed to be a type, a function or a variable.
27752 We'll only be able to tell during template substitution, so we
27753 expect to be called again then. If concepts are enabled and we
27754 know we have a type, we're ok. */
27755 if (flag_concepts
27756 && (identifier_p (tmpl)
27757 || (DECL_P (tmpl)
27758 && (DECL_TYPE_TEMPLATE_P (tmpl)
27759 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27760 return false;
27761
27762 /* Quickly search for any occurrences of auto; usually there won't
27763 be any, and then we'll avoid allocating the vector. */
27764 if (!type_uses_auto (args))
27765 return false;
27766
27767 bool errors = false;
27768
27769 tree vec = extract_autos (args);
27770 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27771 {
27772 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27773 error_at (DECL_SOURCE_LOCATION (xauto),
27774 "invalid use of %qT in template argument", xauto);
27775 errors = true;
27776 }
27777
27778 return errors;
27779 }
27780
27781 /* For a given template T, return the vector of typedefs referenced
27782 in T for which access check is needed at T instantiation time.
27783 T is either a FUNCTION_DECL or a RECORD_TYPE.
27784 Those typedefs were added to T by the function
27785 append_type_to_template_for_access_check. */
27786
27787 vec<qualified_typedef_usage_t, va_gc> *
27788 get_types_needing_access_check (tree t)
27789 {
27790 tree ti;
27791 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27792
27793 if (!t || t == error_mark_node)
27794 return NULL;
27795
27796 if (!(ti = get_template_info (t)))
27797 return NULL;
27798
27799 if (CLASS_TYPE_P (t)
27800 || TREE_CODE (t) == FUNCTION_DECL)
27801 {
27802 if (!TI_TEMPLATE (ti))
27803 return NULL;
27804
27805 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27806 }
27807
27808 return result;
27809 }
27810
27811 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27812 tied to T. That list of typedefs will be access checked at
27813 T instantiation time.
27814 T is either a FUNCTION_DECL or a RECORD_TYPE.
27815 TYPE_DECL is a TYPE_DECL node representing a typedef.
27816 SCOPE is the scope through which TYPE_DECL is accessed.
27817 LOCATION is the location of the usage point of TYPE_DECL.
27818
27819 This function is a subroutine of
27820 append_type_to_template_for_access_check. */
27821
27822 static void
27823 append_type_to_template_for_access_check_1 (tree t,
27824 tree type_decl,
27825 tree scope,
27826 location_t location)
27827 {
27828 qualified_typedef_usage_t typedef_usage;
27829 tree ti;
27830
27831 if (!t || t == error_mark_node)
27832 return;
27833
27834 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27835 || CLASS_TYPE_P (t))
27836 && type_decl
27837 && TREE_CODE (type_decl) == TYPE_DECL
27838 && scope);
27839
27840 if (!(ti = get_template_info (t)))
27841 return;
27842
27843 gcc_assert (TI_TEMPLATE (ti));
27844
27845 typedef_usage.typedef_decl = type_decl;
27846 typedef_usage.context = scope;
27847 typedef_usage.locus = location;
27848
27849 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27850 }
27851
27852 /* Append TYPE_DECL to the template TEMPL.
27853 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27854 At TEMPL instanciation time, TYPE_DECL will be checked to see
27855 if it can be accessed through SCOPE.
27856 LOCATION is the location of the usage point of TYPE_DECL.
27857
27858 e.g. consider the following code snippet:
27859
27860 class C
27861 {
27862 typedef int myint;
27863 };
27864
27865 template<class U> struct S
27866 {
27867 C::myint mi; // <-- usage point of the typedef C::myint
27868 };
27869
27870 S<char> s;
27871
27872 At S<char> instantiation time, we need to check the access of C::myint
27873 In other words, we need to check the access of the myint typedef through
27874 the C scope. For that purpose, this function will add the myint typedef
27875 and the scope C through which its being accessed to a list of typedefs
27876 tied to the template S. That list will be walked at template instantiation
27877 time and access check performed on each typedefs it contains.
27878 Note that this particular code snippet should yield an error because
27879 myint is private to C. */
27880
27881 void
27882 append_type_to_template_for_access_check (tree templ,
27883 tree type_decl,
27884 tree scope,
27885 location_t location)
27886 {
27887 qualified_typedef_usage_t *iter;
27888 unsigned i;
27889
27890 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27891
27892 /* Make sure we don't append the type to the template twice. */
27893 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27894 if (iter->typedef_decl == type_decl && scope == iter->context)
27895 return;
27896
27897 append_type_to_template_for_access_check_1 (templ, type_decl,
27898 scope, location);
27899 }
27900
27901 /* Convert the generic type parameters in PARM that match the types given in the
27902 range [START_IDX, END_IDX) from the current_template_parms into generic type
27903 packs. */
27904
27905 tree
27906 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27907 {
27908 tree current = current_template_parms;
27909 int depth = TMPL_PARMS_DEPTH (current);
27910 current = INNERMOST_TEMPLATE_PARMS (current);
27911 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27912
27913 for (int i = 0; i < start_idx; ++i)
27914 TREE_VEC_ELT (replacement, i)
27915 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27916
27917 for (int i = start_idx; i < end_idx; ++i)
27918 {
27919 /* Create a distinct parameter pack type from the current parm and add it
27920 to the replacement args to tsubst below into the generic function
27921 parameter. */
27922
27923 tree o = TREE_TYPE (TREE_VALUE
27924 (TREE_VEC_ELT (current, i)));
27925 tree t = copy_type (o);
27926 TEMPLATE_TYPE_PARM_INDEX (t)
27927 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27928 o, 0, 0, tf_none);
27929 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27930 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27931 TYPE_MAIN_VARIANT (t) = t;
27932 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27933 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27934 TREE_VEC_ELT (replacement, i) = t;
27935 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27936 }
27937
27938 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27939 TREE_VEC_ELT (replacement, i)
27940 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27941
27942 /* If there are more levels then build up the replacement with the outer
27943 template parms. */
27944 if (depth > 1)
27945 replacement = add_to_template_args (template_parms_to_args
27946 (TREE_CHAIN (current_template_parms)),
27947 replacement);
27948
27949 return tsubst (parm, replacement, tf_none, NULL_TREE);
27950 }
27951
27952 /* Entries in the decl_constraint hash table. */
27953 struct GTY((for_user)) constr_entry
27954 {
27955 tree decl;
27956 tree ci;
27957 };
27958
27959 /* Hashing function and equality for constraint entries. */
27960 struct constr_hasher : ggc_ptr_hash<constr_entry>
27961 {
27962 static hashval_t hash (constr_entry *e)
27963 {
27964 return (hashval_t)DECL_UID (e->decl);
27965 }
27966
27967 static bool equal (constr_entry *e1, constr_entry *e2)
27968 {
27969 return e1->decl == e2->decl;
27970 }
27971 };
27972
27973 /* A mapping from declarations to constraint information. Note that
27974 both templates and their underlying declarations are mapped to the
27975 same constraint information.
27976
27977 FIXME: This is defined in pt.c because garbage collection
27978 code is not being generated for constraint.cc. */
27979
27980 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27981
27982 /* Returns the template constraints of declaration T. If T is not
27983 constrained, return NULL_TREE. Note that T must be non-null. */
27984
27985 tree
27986 get_constraints (tree t)
27987 {
27988 if (!flag_concepts)
27989 return NULL_TREE;
27990
27991 gcc_assert (DECL_P (t));
27992 if (TREE_CODE (t) == TEMPLATE_DECL)
27993 t = DECL_TEMPLATE_RESULT (t);
27994 constr_entry elt = { t, NULL_TREE };
27995 constr_entry* found = decl_constraints->find (&elt);
27996 if (found)
27997 return found->ci;
27998 else
27999 return NULL_TREE;
28000 }
28001
28002 /* Associate the given constraint information CI with the declaration
28003 T. If T is a template, then the constraints are associated with
28004 its underlying declaration. Don't build associations if CI is
28005 NULL_TREE. */
28006
28007 void
28008 set_constraints (tree t, tree ci)
28009 {
28010 if (!ci)
28011 return;
28012 gcc_assert (t && flag_concepts);
28013 if (TREE_CODE (t) == TEMPLATE_DECL)
28014 t = DECL_TEMPLATE_RESULT (t);
28015 gcc_assert (!get_constraints (t));
28016 constr_entry elt = {t, ci};
28017 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28018 constr_entry* entry = ggc_alloc<constr_entry> ();
28019 *entry = elt;
28020 *slot = entry;
28021 }
28022
28023 /* Remove the associated constraints of the declaration T. */
28024
28025 void
28026 remove_constraints (tree t)
28027 {
28028 gcc_assert (DECL_P (t));
28029 if (TREE_CODE (t) == TEMPLATE_DECL)
28030 t = DECL_TEMPLATE_RESULT (t);
28031
28032 constr_entry elt = {t, NULL_TREE};
28033 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28034 if (slot)
28035 decl_constraints->clear_slot (slot);
28036 }
28037
28038 /* Memoized satisfaction results for declarations. This
28039 maps the pair (constraint_info, arguments) to the result computed
28040 by constraints_satisfied_p. */
28041
28042 struct GTY((for_user)) constraint_sat_entry
28043 {
28044 tree ci;
28045 tree args;
28046 tree result;
28047 };
28048
28049 /* Hashing function and equality for constraint entries. */
28050
28051 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28052 {
28053 static hashval_t hash (constraint_sat_entry *e)
28054 {
28055 hashval_t val = iterative_hash_object(e->ci, 0);
28056 return iterative_hash_template_arg (e->args, val);
28057 }
28058
28059 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28060 {
28061 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28062 }
28063 };
28064
28065 /* Memoized satisfaction results for concept checks. */
28066
28067 struct GTY((for_user)) concept_spec_entry
28068 {
28069 tree tmpl;
28070 tree args;
28071 tree result;
28072 };
28073
28074 /* Hashing function and equality for constraint entries. */
28075
28076 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28077 {
28078 static hashval_t hash (concept_spec_entry *e)
28079 {
28080 return hash_tmpl_and_args (e->tmpl, e->args);
28081 }
28082
28083 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28084 {
28085 ++comparing_specializations;
28086 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28087 --comparing_specializations;
28088 return eq;
28089 }
28090 };
28091
28092 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28093 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28094
28095 /* Search for a memoized satisfaction result. Returns one of the
28096 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28097
28098 tree
28099 lookup_constraint_satisfaction (tree ci, tree args)
28100 {
28101 constraint_sat_entry elt = { ci, args, NULL_TREE };
28102 constraint_sat_entry* found = constraint_memos->find (&elt);
28103 if (found)
28104 return found->result;
28105 else
28106 return NULL_TREE;
28107 }
28108
28109 /* Memoize the result of a satisfication test. Returns the saved result. */
28110
28111 tree
28112 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28113 {
28114 constraint_sat_entry elt = {ci, args, result};
28115 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28116 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28117 *entry = elt;
28118 *slot = entry;
28119 return result;
28120 }
28121
28122 /* Search for a memoized satisfaction result for a concept. */
28123
28124 tree
28125 lookup_concept_satisfaction (tree tmpl, tree args)
28126 {
28127 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28128 concept_spec_entry* found = concept_memos->find (&elt);
28129 if (found)
28130 return found->result;
28131 else
28132 return NULL_TREE;
28133 }
28134
28135 /* Memoize the result of a concept check. Returns the saved result. */
28136
28137 tree
28138 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28139 {
28140 concept_spec_entry elt = {tmpl, args, result};
28141 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28142 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28143 *entry = elt;
28144 *slot = entry;
28145 return result;
28146 }
28147
28148 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28149
28150 /* Returns a prior concept specialization. This returns the substituted
28151 and normalized constraints defined by the concept. */
28152
28153 tree
28154 get_concept_expansion (tree tmpl, tree args)
28155 {
28156 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28157 concept_spec_entry* found = concept_expansions->find (&elt);
28158 if (found)
28159 return found->result;
28160 else
28161 return NULL_TREE;
28162 }
28163
28164 /* Save a concept expansion for later. */
28165
28166 tree
28167 save_concept_expansion (tree tmpl, tree args, tree def)
28168 {
28169 concept_spec_entry elt = {tmpl, args, def};
28170 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28171 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28172 *entry = elt;
28173 *slot = entry;
28174 return def;
28175 }
28176
28177 static hashval_t
28178 hash_subsumption_args (tree t1, tree t2)
28179 {
28180 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28181 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28182 int val = 0;
28183 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28184 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28185 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28186 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28187 return val;
28188 }
28189
28190 /* Compare the constraints of two subsumption entries. The LEFT1 and
28191 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28192 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28193
28194 static bool
28195 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28196 {
28197 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28198 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28199 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28200 CHECK_CONSTR_ARGS (right1)))
28201 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28202 CHECK_CONSTR_ARGS (right2));
28203 return false;
28204 }
28205
28206 /* Key/value pair for learning and memoizing subsumption results. This
28207 associates a pair of check constraints (including arguments) with
28208 a boolean value indicating the result. */
28209
28210 struct GTY((for_user)) subsumption_entry
28211 {
28212 tree t1;
28213 tree t2;
28214 bool result;
28215 };
28216
28217 /* Hashing function and equality for constraint entries. */
28218
28219 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28220 {
28221 static hashval_t hash (subsumption_entry *e)
28222 {
28223 return hash_subsumption_args (e->t1, e->t2);
28224 }
28225
28226 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28227 {
28228 ++comparing_specializations;
28229 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28230 --comparing_specializations;
28231 return eq;
28232 }
28233 };
28234
28235 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28236
28237 /* Search for a previously cached subsumption result. */
28238
28239 bool*
28240 lookup_subsumption_result (tree t1, tree t2)
28241 {
28242 subsumption_entry elt = { t1, t2, false };
28243 subsumption_entry* found = subsumption_table->find (&elt);
28244 if (found)
28245 return &found->result;
28246 else
28247 return 0;
28248 }
28249
28250 /* Save a subsumption result. */
28251
28252 bool
28253 save_subsumption_result (tree t1, tree t2, bool result)
28254 {
28255 subsumption_entry elt = {t1, t2, result};
28256 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28257 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28258 *entry = elt;
28259 *slot = entry;
28260 return result;
28261 }
28262
28263 /* Set up the hash table for constraint association. */
28264
28265 void
28266 init_constraint_processing (void)
28267 {
28268 if (!flag_concepts)
28269 return;
28270
28271 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28272 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28273 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28274 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28275 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28276 }
28277
28278 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28279 0..N-1. */
28280
28281 void
28282 declare_integer_pack (void)
28283 {
28284 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28285 build_function_type_list (integer_type_node,
28286 integer_type_node,
28287 NULL_TREE),
28288 NULL_TREE, ECF_CONST);
28289 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28290 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
28291 DECL_FUNCTION_CODE (ipfn)
28292 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
28293 }
28294
28295 /* Set up the hash tables for template instantiations. */
28296
28297 void
28298 init_template_processing (void)
28299 {
28300 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
28301 type_specializations = hash_table<spec_hasher>::create_ggc (37);
28302
28303 if (cxx_dialect >= cxx11)
28304 declare_integer_pack ();
28305 }
28306
28307 /* Print stats about the template hash tables for -fstats. */
28308
28309 void
28310 print_template_statistics (void)
28311 {
28312 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28313 "%f collisions\n", (long) decl_specializations->size (),
28314 (long) decl_specializations->elements (),
28315 decl_specializations->collisions ());
28316 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28317 "%f collisions\n", (long) type_specializations->size (),
28318 (long) type_specializations->elements (),
28319 type_specializations->collisions ());
28320 }
28321
28322 #if CHECKING_P
28323
28324 namespace selftest {
28325
28326 /* Verify that build_non_dependent_expr () works, for various expressions,
28327 and that location wrappers don't affect the results. */
28328
28329 static void
28330 test_build_non_dependent_expr ()
28331 {
28332 location_t loc = BUILTINS_LOCATION;
28333
28334 /* Verify constants, without and with location wrappers. */
28335 tree int_cst = build_int_cst (integer_type_node, 42);
28336 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28337
28338 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28339 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28340 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28341
28342 tree string_lit = build_string (4, "foo");
28343 TREE_TYPE (string_lit) = char_array_type_node;
28344 string_lit = fix_string_type (string_lit);
28345 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28346
28347 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28348 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28349 ASSERT_EQ (wrapped_string_lit,
28350 build_non_dependent_expr (wrapped_string_lit));
28351 }
28352
28353 /* Verify that type_dependent_expression_p () works correctly, even
28354 in the presence of location wrapper nodes. */
28355
28356 static void
28357 test_type_dependent_expression_p ()
28358 {
28359 location_t loc = BUILTINS_LOCATION;
28360
28361 tree name = get_identifier ("foo");
28362
28363 /* If no templates are involved, nothing is type-dependent. */
28364 gcc_assert (!processing_template_decl);
28365 ASSERT_FALSE (type_dependent_expression_p (name));
28366
28367 ++processing_template_decl;
28368
28369 /* Within a template, an unresolved name is always type-dependent. */
28370 ASSERT_TRUE (type_dependent_expression_p (name));
28371
28372 /* Ensure it copes with NULL_TREE and errors. */
28373 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28374 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28375
28376 /* A USING_DECL in a template should be type-dependent, even if wrapped
28377 with a location wrapper (PR c++/83799). */
28378 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28379 TREE_TYPE (using_decl) = integer_type_node;
28380 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28381 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28382 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28383 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28384
28385 --processing_template_decl;
28386 }
28387
28388 /* Run all of the selftests within this file. */
28389
28390 void
28391 cp_pt_c_tests ()
28392 {
28393 test_build_non_dependent_expr ();
28394 test_type_dependent_expression_p ();
28395 }
28396
28397 } // namespace selftest
28398
28399 #endif /* #if CHECKING_P */
28400
28401 #include "gt-cp-pt.h"