]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
[PR c++/86374] Name lookup failure in enclosing template
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int check_non_deducible_conversion (tree, tree, int, int,
159 struct conversion **, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
161 tree);
162 static int type_unification_real (tree, tree, tree, const tree *,
163 unsigned int, int, unification_kind_t,
164 vec<deferred_access_check, va_gc> **,
165 bool);
166 static void note_template_header (int);
167 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
168 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
169 static tree convert_template_argument (tree, tree, tree,
170 tsubst_flags_t, int, tree);
171 static tree for_each_template_parm (tree, tree_fn_t, void*,
172 hash_set<tree> *, bool, tree_fn_t = NULL);
173 static tree expand_template_argument_pack (tree);
174 static tree build_template_parm_index (int, int, int, tree, tree);
175 static bool inline_needs_template_parms (tree, bool);
176 static void push_inline_template_parms_recursive (tree, int);
177 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
178 static int mark_template_parm (tree, void *);
179 static int template_parm_this_level_p (tree, void *);
180 static tree tsubst_friend_function (tree, tree);
181 static tree tsubst_friend_class (tree, tree);
182 static int can_complete_type_without_circularity (tree);
183 static tree get_bindings (tree, tree, tree, bool);
184 static int template_decl_level (tree);
185 static int check_cv_quals_for_unify (int, tree, tree);
186 static void template_parm_level_and_index (tree, int*, int*);
187 static int unify_pack_expansion (tree, tree, tree,
188 tree, unification_kind_t, bool, bool);
189 static tree copy_template_args (tree);
190 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
204 tree, tree);
205 static bool template_template_parm_bindings_ok_p (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
225 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
226 static tree make_argument_pack (tree);
227 static void register_parameter_specializations (tree, tree);
228 static tree enclosing_instantiation_of (tree tctx);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
234
235 static void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_CLASS_SCOPE_P (t))
244 push_nested_class (DECL_CONTEXT (t));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 saved_access_scope = tree_cons
251 (NULL_TREE, current_function_decl, saved_access_scope);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 static void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 {
264 current_function_decl = TREE_VALUE (saved_access_scope);
265 saved_access_scope = TREE_CHAIN (saved_access_scope);
266 }
267
268 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error ("data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error ("invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 type = CP_DECL_CONTEXT (type);
394 else if (LAMBDA_TYPE_P (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Subroutine of maybe_begin_member_template_processing.
404 Returns true if processing DECL needs us to push template parms. */
405
406 static bool
407 inline_needs_template_parms (tree decl, bool nsdmi)
408 {
409 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
410 return false;
411
412 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
413 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
414 }
415
416 /* Subroutine of maybe_begin_member_template_processing.
417 Push the template parms in PARMS, starting from LEVELS steps into the
418 chain, and ending at the beginning, since template parms are listed
419 innermost first. */
420
421 static void
422 push_inline_template_parms_recursive (tree parmlist, int levels)
423 {
424 tree parms = TREE_VALUE (parmlist);
425 int i;
426
427 if (levels > 1)
428 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
429
430 ++processing_template_decl;
431 current_template_parms
432 = tree_cons (size_int (processing_template_decl),
433 parms, current_template_parms);
434 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
435
436 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
437 NULL);
438 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
439 {
440 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
441
442 if (error_operand_p (parm))
443 continue;
444
445 gcc_assert (DECL_P (parm));
446
447 switch (TREE_CODE (parm))
448 {
449 case TYPE_DECL:
450 case TEMPLATE_DECL:
451 pushdecl (parm);
452 break;
453
454 case PARM_DECL:
455 /* Push the CONST_DECL. */
456 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
457 break;
458
459 default:
460 gcc_unreachable ();
461 }
462 }
463 }
464
465 /* Restore the template parameter context for a member template, a
466 friend template defined in a class definition, or a non-template
467 member of template class. */
468
469 void
470 maybe_begin_member_template_processing (tree decl)
471 {
472 tree parms;
473 int levels = 0;
474 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
475
476 if (nsdmi)
477 {
478 tree ctx = DECL_CONTEXT (decl);
479 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
480 /* Disregard full specializations (c++/60999). */
481 && uses_template_parms (ctx)
482 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
483 }
484
485 if (inline_needs_template_parms (decl, nsdmi))
486 {
487 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
488 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
489
490 if (DECL_TEMPLATE_SPECIALIZATION (decl))
491 {
492 --levels;
493 parms = TREE_CHAIN (parms);
494 }
495
496 push_inline_template_parms_recursive (parms, levels);
497 }
498
499 /* Remember how many levels of template parameters we pushed so that
500 we can pop them later. */
501 inline_parm_levels.safe_push (levels);
502 }
503
504 /* Undo the effects of maybe_begin_member_template_processing. */
505
506 void
507 maybe_end_member_template_processing (void)
508 {
509 int i;
510 int last;
511
512 if (inline_parm_levels.length () == 0)
513 return;
514
515 last = inline_parm_levels.pop ();
516 for (i = 0; i < last; ++i)
517 {
518 --processing_template_decl;
519 current_template_parms = TREE_CHAIN (current_template_parms);
520 poplevel (0, 0, 0);
521 }
522 }
523
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. */
526
527 static tree
528 add_to_template_args (tree args, tree extra_args)
529 {
530 tree new_args;
531 int extra_depth;
532 int i;
533 int j;
534
535 if (args == NULL_TREE || extra_args == error_mark_node)
536 return extra_args;
537
538 extra_depth = TMPL_ARGS_DEPTH (extra_args);
539 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
540
541 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
543
544 for (j = 1; j <= extra_depth; ++j, ++i)
545 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
546
547 return new_args;
548 }
549
550 /* Like add_to_template_args, but only the outermost ARGS are added to
551 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
552 (EXTRA_ARGS) levels are added. This function is used to combine
553 the template arguments from a partial instantiation with the
554 template arguments used to attain the full instantiation from the
555 partial instantiation. */
556
557 static tree
558 add_outermost_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561
562 /* If there are more levels of EXTRA_ARGS than there are ARGS,
563 something very fishy is going on. */
564 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
565
566 /* If *all* the new arguments will be the EXTRA_ARGS, just return
567 them. */
568 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
569 return extra_args;
570
571 /* For the moment, we make ARGS look like it contains fewer levels. */
572 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
573
574 new_args = add_to_template_args (args, extra_args);
575
576 /* Now, we restore ARGS to its full dimensions. */
577 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
578
579 return new_args;
580 }
581
582 /* Return the N levels of innermost template arguments from the ARGS. */
583
584 tree
585 get_innermost_template_args (tree args, int n)
586 {
587 tree new_args;
588 int extra_levels;
589 int i;
590
591 gcc_assert (n >= 0);
592
593 /* If N is 1, just return the innermost set of template arguments. */
594 if (n == 1)
595 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
596
597 /* If we're not removing anything, just return the arguments we were
598 given. */
599 extra_levels = TMPL_ARGS_DEPTH (args) - n;
600 gcc_assert (extra_levels >= 0);
601 if (extra_levels == 0)
602 return args;
603
604 /* Make a new set of arguments, not containing the outer arguments. */
605 new_args = make_tree_vec (n);
606 for (i = 1; i <= n; ++i)
607 SET_TMPL_ARGS_LEVEL (new_args, i,
608 TMPL_ARGS_LEVEL (args, i + extra_levels));
609
610 return new_args;
611 }
612
613 /* The inverse of get_innermost_template_args: Return all but the innermost
614 EXTRA_LEVELS levels of template arguments from the ARGS. */
615
616 static tree
617 strip_innermost_template_args (tree args, int extra_levels)
618 {
619 tree new_args;
620 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
621 int i;
622
623 gcc_assert (n >= 0);
624
625 /* If N is 1, just return the outermost set of template arguments. */
626 if (n == 1)
627 return TMPL_ARGS_LEVEL (args, 1);
628
629 /* If we're not removing anything, just return the arguments we were
630 given. */
631 gcc_assert (extra_levels >= 0);
632 if (extra_levels == 0)
633 return args;
634
635 /* Make a new set of arguments, not containing the inner arguments. */
636 new_args = make_tree_vec (n);
637 for (i = 1; i <= n; ++i)
638 SET_TMPL_ARGS_LEVEL (new_args, i,
639 TMPL_ARGS_LEVEL (args, i));
640
641 return new_args;
642 }
643
644 /* We've got a template header coming up; push to a new level for storing
645 the parms. */
646
647 void
648 begin_template_parm_list (void)
649 {
650 /* We use a non-tag-transparent scope here, which causes pushtag to
651 put tags in this scope, rather than in the enclosing class or
652 namespace scope. This is the right thing, since we want
653 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
654 global template class, push_template_decl handles putting the
655 TEMPLATE_DECL into top-level scope. For a nested template class,
656 e.g.:
657
658 template <class T> struct S1 {
659 template <class T> struct S2 {};
660 };
661
662 pushtag contains special code to insert the TEMPLATE_DECL for S2
663 at the right scope. */
664 begin_scope (sk_template_parms, NULL);
665 ++processing_template_decl;
666 ++processing_template_parmlist;
667 note_template_header (0);
668
669 /* Add a dummy parameter level while we process the parameter list. */
670 current_template_parms
671 = tree_cons (size_int (processing_template_decl),
672 make_tree_vec (0),
673 current_template_parms);
674 }
675
676 /* This routine is called when a specialization is declared. If it is
677 invalid to declare a specialization here, an error is reported and
678 false is returned, otherwise this routine will return true. */
679
680 static bool
681 check_specialization_scope (void)
682 {
683 tree scope = current_scope ();
684
685 /* [temp.expl.spec]
686
687 An explicit specialization shall be declared in the namespace of
688 which the template is a member, or, for member templates, in the
689 namespace of which the enclosing class or enclosing class
690 template is a member. An explicit specialization of a member
691 function, member class or static data member of a class template
692 shall be declared in the namespace of which the class template
693 is a member. */
694 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
695 {
696 error ("explicit specialization in non-namespace scope %qD", scope);
697 return false;
698 }
699
700 /* [temp.expl.spec]
701
702 In an explicit specialization declaration for a member of a class
703 template or a member template that appears in namespace scope,
704 the member template and some of its enclosing class templates may
705 remain unspecialized, except that the declaration shall not
706 explicitly specialize a class member template if its enclosing
707 class templates are not explicitly specialized as well. */
708 if (current_template_parms)
709 {
710 error ("enclosing class templates are not explicitly specialized");
711 return false;
712 }
713
714 return true;
715 }
716
717 /* We've just seen template <>. */
718
719 bool
720 begin_specialization (void)
721 {
722 begin_scope (sk_template_spec, NULL);
723 note_template_header (1);
724 return check_specialization_scope ();
725 }
726
727 /* Called at then end of processing a declaration preceded by
728 template<>. */
729
730 void
731 end_specialization (void)
732 {
733 finish_scope ();
734 reset_specialization ();
735 }
736
737 /* Any template <>'s that we have seen thus far are not referring to a
738 function specialization. */
739
740 void
741 reset_specialization (void)
742 {
743 processing_specialization = 0;
744 template_header_count = 0;
745 }
746
747 /* We've just seen a template header. If SPECIALIZATION is nonzero,
748 it was of the form template <>. */
749
750 static void
751 note_template_header (int specialization)
752 {
753 processing_specialization = specialization;
754 template_header_count++;
755 }
756
757 /* We're beginning an explicit instantiation. */
758
759 void
760 begin_explicit_instantiation (void)
761 {
762 gcc_assert (!processing_explicit_instantiation);
763 processing_explicit_instantiation = true;
764 }
765
766
767 void
768 end_explicit_instantiation (void)
769 {
770 gcc_assert (processing_explicit_instantiation);
771 processing_explicit_instantiation = false;
772 }
773
774 /* An explicit specialization or partial specialization of TMPL is being
775 declared. Check that the namespace in which the specialization is
776 occurring is permissible. Returns false iff it is invalid to
777 specialize TMPL in the current namespace. */
778
779 static bool
780 check_specialization_namespace (tree tmpl)
781 {
782 tree tpl_ns = decl_namespace_context (tmpl);
783
784 /* [tmpl.expl.spec]
785
786 An explicit specialization shall be declared in a namespace enclosing the
787 specialized template. An explicit specialization whose declarator-id is
788 not qualified shall be declared in the nearest enclosing namespace of the
789 template, or, if the namespace is inline (7.3.1), any namespace from its
790 enclosing namespace set. */
791 if (current_scope() != DECL_CONTEXT (tmpl)
792 && !at_namespace_scope_p ())
793 {
794 error ("specialization of %qD must appear at namespace scope", tmpl);
795 return false;
796 }
797
798 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
799 /* Same or enclosing namespace. */
800 return true;
801 else
802 {
803 permerror (input_location,
804 "specialization of %qD in different namespace", tmpl);
805 inform (DECL_SOURCE_LOCATION (tmpl),
806 " from definition of %q#D", tmpl);
807 return false;
808 }
809 }
810
811 /* SPEC is an explicit instantiation. Check that it is valid to
812 perform this explicit instantiation in the current namespace. */
813
814 static void
815 check_explicit_instantiation_namespace (tree spec)
816 {
817 tree ns;
818
819 /* DR 275: An explicit instantiation shall appear in an enclosing
820 namespace of its template. */
821 ns = decl_namespace_context (spec);
822 if (!is_nested_namespace (current_namespace, ns))
823 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
824 "(which does not enclose namespace %qD)",
825 spec, current_namespace, ns);
826 }
827
828 // Returns the type of a template specialization only if that
829 // specialization needs to be defined. Otherwise (e.g., if the type has
830 // already been defined), the function returns NULL_TREE.
831 static tree
832 maybe_new_partial_specialization (tree type)
833 {
834 // An implicit instantiation of an incomplete type implies
835 // the definition of a new class template.
836 //
837 // template<typename T>
838 // struct S;
839 //
840 // template<typename T>
841 // struct S<T*>;
842 //
843 // Here, S<T*> is an implicit instantiation of S whose type
844 // is incomplete.
845 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
846 return type;
847
848 // It can also be the case that TYPE is a completed specialization.
849 // Continuing the previous example, suppose we also declare:
850 //
851 // template<typename T>
852 // requires Integral<T>
853 // struct S<T*>;
854 //
855 // Here, S<T*> refers to the specialization S<T*> defined
856 // above. However, we need to differentiate definitions because
857 // we intend to define a new partial specialization. In this case,
858 // we rely on the fact that the constraints are different for
859 // this declaration than that above.
860 //
861 // Note that we also get here for injected class names and
862 // late-parsed template definitions. We must ensure that we
863 // do not create new type declarations for those cases.
864 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
865 {
866 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
867 tree args = CLASSTYPE_TI_ARGS (type);
868
869 // If there are no template parameters, this cannot be a new
870 // partial template specializtion?
871 if (!current_template_parms)
872 return NULL_TREE;
873
874 // The injected-class-name is not a new partial specialization.
875 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
876 return NULL_TREE;
877
878 // If the constraints are not the same as those of the primary
879 // then, we can probably create a new specialization.
880 tree type_constr = current_template_constraints ();
881
882 if (type == TREE_TYPE (tmpl))
883 {
884 tree main_constr = get_constraints (tmpl);
885 if (equivalent_constraints (type_constr, main_constr))
886 return NULL_TREE;
887 }
888
889 // Also, if there's a pre-existing specialization with matching
890 // constraints, then this also isn't new.
891 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
892 while (specs)
893 {
894 tree spec_tmpl = TREE_VALUE (specs);
895 tree spec_args = TREE_PURPOSE (specs);
896 tree spec_constr = get_constraints (spec_tmpl);
897 if (comp_template_args (args, spec_args)
898 && equivalent_constraints (type_constr, spec_constr))
899 return NULL_TREE;
900 specs = TREE_CHAIN (specs);
901 }
902
903 // Create a new type node (and corresponding type decl)
904 // for the newly declared specialization.
905 tree t = make_class_type (TREE_CODE (type));
906 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
907 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
908
909 /* We only need a separate type node for storing the definition of this
910 partial specialization; uses of S<T*> are unconstrained, so all are
911 equivalent. So keep TYPE_CANONICAL the same. */
912 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
913
914 // Build the corresponding type decl.
915 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
916 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
917 DECL_SOURCE_LOCATION (d) = input_location;
918
919 return t;
920 }
921
922 return NULL_TREE;
923 }
924
925 /* The TYPE is being declared. If it is a template type, that means it
926 is a partial specialization. Do appropriate error-checking. */
927
928 tree
929 maybe_process_partial_specialization (tree type)
930 {
931 tree context;
932
933 if (type == error_mark_node)
934 return error_mark_node;
935
936 /* A lambda that appears in specialization context is not itself a
937 specialization. */
938 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
939 return type;
940
941 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
942 {
943 error ("name of class shadows template template parameter %qD",
944 TYPE_NAME (type));
945 return error_mark_node;
946 }
947
948 context = TYPE_CONTEXT (type);
949
950 if (TYPE_ALIAS_P (type))
951 {
952 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
953
954 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
955 error ("specialization of alias template %qD",
956 TI_TEMPLATE (tinfo));
957 else
958 error ("explicit specialization of non-template %qT", type);
959 return error_mark_node;
960 }
961 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
962 {
963 /* This is for ordinary explicit specialization and partial
964 specialization of a template class such as:
965
966 template <> class C<int>;
967
968 or:
969
970 template <class T> class C<T*>;
971
972 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
973
974 if (tree t = maybe_new_partial_specialization (type))
975 {
976 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
977 && !at_namespace_scope_p ())
978 return error_mark_node;
979 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
980 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
981 if (processing_template_decl)
982 {
983 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
984 if (decl == error_mark_node)
985 return error_mark_node;
986 return TREE_TYPE (decl);
987 }
988 }
989 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
990 error ("specialization of %qT after instantiation", type);
991 else if (errorcount && !processing_specialization
992 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
993 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
994 /* Trying to define a specialization either without a template<> header
995 or in an inappropriate place. We've already given an error, so just
996 bail now so we don't actually define the specialization. */
997 return error_mark_node;
998 }
999 else if (CLASS_TYPE_P (type)
1000 && !CLASSTYPE_USE_TEMPLATE (type)
1001 && CLASSTYPE_TEMPLATE_INFO (type)
1002 && context && CLASS_TYPE_P (context)
1003 && CLASSTYPE_TEMPLATE_INFO (context))
1004 {
1005 /* This is for an explicit specialization of member class
1006 template according to [temp.expl.spec/18]:
1007
1008 template <> template <class U> class C<int>::D;
1009
1010 The context `C<int>' must be an implicit instantiation.
1011 Otherwise this is just a member class template declared
1012 earlier like:
1013
1014 template <> class C<int> { template <class U> class D; };
1015 template <> template <class U> class C<int>::D;
1016
1017 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1018 while in the second case, `C<int>::D' is a primary template
1019 and `C<T>::D' may not exist. */
1020
1021 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1022 && !COMPLETE_TYPE_P (type))
1023 {
1024 tree t;
1025 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1026
1027 if (current_namespace
1028 != decl_namespace_context (tmpl))
1029 {
1030 permerror (input_location,
1031 "specializing %q#T in different namespace", type);
1032 permerror (DECL_SOURCE_LOCATION (tmpl),
1033 " from definition of %q#D", tmpl);
1034 }
1035
1036 /* Check for invalid specialization after instantiation:
1037
1038 template <> template <> class C<int>::D<int>;
1039 template <> template <class U> class C<int>::D; */
1040
1041 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1042 t; t = TREE_CHAIN (t))
1043 {
1044 tree inst = TREE_VALUE (t);
1045 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1046 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1047 {
1048 /* We already have a full specialization of this partial
1049 instantiation, or a full specialization has been
1050 looked up but not instantiated. Reassign it to the
1051 new member specialization template. */
1052 spec_entry elt;
1053 spec_entry *entry;
1054
1055 elt.tmpl = most_general_template (tmpl);
1056 elt.args = CLASSTYPE_TI_ARGS (inst);
1057 elt.spec = inst;
1058
1059 type_specializations->remove_elt (&elt);
1060
1061 elt.tmpl = tmpl;
1062 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1063
1064 spec_entry **slot
1065 = type_specializations->find_slot (&elt, INSERT);
1066 entry = ggc_alloc<spec_entry> ();
1067 *entry = elt;
1068 *slot = entry;
1069 }
1070 else
1071 /* But if we've had an implicit instantiation, that's a
1072 problem ([temp.expl.spec]/6). */
1073 error ("specialization %qT after instantiation %qT",
1074 type, inst);
1075 }
1076
1077 /* Mark TYPE as a specialization. And as a result, we only
1078 have one level of template argument for the innermost
1079 class template. */
1080 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1081 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1082 CLASSTYPE_TI_ARGS (type)
1083 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1084 }
1085 }
1086 else if (processing_specialization)
1087 {
1088 /* Someday C++0x may allow for enum template specialization. */
1089 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1090 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1091 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1092 "of %qD not allowed by ISO C++", type);
1093 else
1094 {
1095 error ("explicit specialization of non-template %qT", type);
1096 return error_mark_node;
1097 }
1098 }
1099
1100 return type;
1101 }
1102
1103 /* Returns nonzero if we can optimize the retrieval of specializations
1104 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1105 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1106
1107 static inline bool
1108 optimize_specialization_lookup_p (tree tmpl)
1109 {
1110 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1111 && DECL_CLASS_SCOPE_P (tmpl)
1112 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1113 parameter. */
1114 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1115 /* The optimized lookup depends on the fact that the
1116 template arguments for the member function template apply
1117 purely to the containing class, which is not true if the
1118 containing class is an explicit or partial
1119 specialization. */
1120 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1121 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1122 && !DECL_CONV_FN_P (tmpl)
1123 /* It is possible to have a template that is not a member
1124 template and is not a member of a template class:
1125
1126 template <typename T>
1127 struct S { friend A::f(); };
1128
1129 Here, the friend function is a template, but the context does
1130 not have template information. The optimized lookup relies
1131 on having ARGS be the template arguments for both the class
1132 and the function template. */
1133 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1134 }
1135
1136 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1137 gone through coerce_template_parms by now. */
1138
1139 static void
1140 verify_unstripped_args_1 (tree inner)
1141 {
1142 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1143 {
1144 tree arg = TREE_VEC_ELT (inner, i);
1145 if (TREE_CODE (arg) == TEMPLATE_DECL)
1146 /* OK */;
1147 else if (TYPE_P (arg))
1148 gcc_assert (strip_typedefs (arg, NULL) == arg);
1149 else if (ARGUMENT_PACK_P (arg))
1150 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1151 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1152 /* Allow typedefs on the type of a non-type argument, since a
1153 parameter can have them. */;
1154 else
1155 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1156 }
1157 }
1158
1159 static void
1160 verify_unstripped_args (tree args)
1161 {
1162 ++processing_template_decl;
1163 if (!any_dependent_template_arguments_p (args))
1164 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1165 --processing_template_decl;
1166 }
1167
1168 /* Retrieve the specialization (in the sense of [temp.spec] - a
1169 specialization is either an instantiation or an explicit
1170 specialization) of TMPL for the given template ARGS. If there is
1171 no such specialization, return NULL_TREE. The ARGS are a vector of
1172 arguments, or a vector of vectors of arguments, in the case of
1173 templates with more than one level of parameters.
1174
1175 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1176 then we search for a partial specialization matching ARGS. This
1177 parameter is ignored if TMPL is not a class template.
1178
1179 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1180 result is a NONTYPE_ARGUMENT_PACK. */
1181
1182 static tree
1183 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1184 {
1185 if (tmpl == NULL_TREE)
1186 return NULL_TREE;
1187
1188 if (args == error_mark_node)
1189 return NULL_TREE;
1190
1191 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1192 || TREE_CODE (tmpl) == FIELD_DECL);
1193
1194 /* There should be as many levels of arguments as there are
1195 levels of parameters. */
1196 gcc_assert (TMPL_ARGS_DEPTH (args)
1197 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1198 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1199 : template_class_depth (DECL_CONTEXT (tmpl))));
1200
1201 if (flag_checking)
1202 verify_unstripped_args (args);
1203
1204 /* Lambda functions in templates aren't instantiated normally, but through
1205 tsubst_lambda_expr. */
1206 if (lambda_fn_in_template_p (tmpl))
1207 return NULL_TREE;
1208
1209 if (optimize_specialization_lookup_p (tmpl))
1210 {
1211 /* The template arguments actually apply to the containing
1212 class. Find the class specialization with those
1213 arguments. */
1214 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1215 tree class_specialization
1216 = retrieve_specialization (class_template, args, 0);
1217 if (!class_specialization)
1218 return NULL_TREE;
1219
1220 /* Find the instance of TMPL. */
1221 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1222 for (ovl_iterator iter (fns); iter; ++iter)
1223 {
1224 tree fn = *iter;
1225 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1226 /* using-declarations can add base methods to the method vec,
1227 and we don't want those here. */
1228 && DECL_CONTEXT (fn) == class_specialization)
1229 return fn;
1230 }
1231 return NULL_TREE;
1232 }
1233 else
1234 {
1235 spec_entry *found;
1236 spec_entry elt;
1237 hash_table<spec_hasher> *specializations;
1238
1239 elt.tmpl = tmpl;
1240 elt.args = args;
1241 elt.spec = NULL_TREE;
1242
1243 if (DECL_CLASS_TEMPLATE_P (tmpl))
1244 specializations = type_specializations;
1245 else
1246 specializations = decl_specializations;
1247
1248 if (hash == 0)
1249 hash = spec_hasher::hash (&elt);
1250 found = specializations->find_with_hash (&elt, hash);
1251 if (found)
1252 return found->spec;
1253 }
1254
1255 return NULL_TREE;
1256 }
1257
1258 /* Like retrieve_specialization, but for local declarations. */
1259
1260 tree
1261 retrieve_local_specialization (tree tmpl)
1262 {
1263 if (local_specializations == NULL)
1264 return NULL_TREE;
1265
1266 tree *slot = local_specializations->get (tmpl);
1267 return slot ? *slot : NULL_TREE;
1268 }
1269
1270 /* Returns nonzero iff DECL is a specialization of TMPL. */
1271
1272 int
1273 is_specialization_of (tree decl, tree tmpl)
1274 {
1275 tree t;
1276
1277 if (TREE_CODE (decl) == FUNCTION_DECL)
1278 {
1279 for (t = decl;
1280 t != NULL_TREE;
1281 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1282 if (t == tmpl)
1283 return 1;
1284 }
1285 else
1286 {
1287 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1288
1289 for (t = TREE_TYPE (decl);
1290 t != NULL_TREE;
1291 t = CLASSTYPE_USE_TEMPLATE (t)
1292 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1293 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1294 return 1;
1295 }
1296
1297 return 0;
1298 }
1299
1300 /* Returns nonzero iff DECL is a specialization of friend declaration
1301 FRIEND_DECL according to [temp.friend]. */
1302
1303 bool
1304 is_specialization_of_friend (tree decl, tree friend_decl)
1305 {
1306 bool need_template = true;
1307 int template_depth;
1308
1309 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1310 || TREE_CODE (decl) == TYPE_DECL);
1311
1312 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1313 of a template class, we want to check if DECL is a specialization
1314 if this. */
1315 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1316 && DECL_TEMPLATE_INFO (friend_decl)
1317 && !DECL_USE_TEMPLATE (friend_decl))
1318 {
1319 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1320 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1321 need_template = false;
1322 }
1323 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1324 && !PRIMARY_TEMPLATE_P (friend_decl))
1325 need_template = false;
1326
1327 /* There is nothing to do if this is not a template friend. */
1328 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1329 return false;
1330
1331 if (is_specialization_of (decl, friend_decl))
1332 return true;
1333
1334 /* [temp.friend/6]
1335 A member of a class template may be declared to be a friend of a
1336 non-template class. In this case, the corresponding member of
1337 every specialization of the class template is a friend of the
1338 class granting friendship.
1339
1340 For example, given a template friend declaration
1341
1342 template <class T> friend void A<T>::f();
1343
1344 the member function below is considered a friend
1345
1346 template <> struct A<int> {
1347 void f();
1348 };
1349
1350 For this type of template friend, TEMPLATE_DEPTH below will be
1351 nonzero. To determine if DECL is a friend of FRIEND, we first
1352 check if the enclosing class is a specialization of another. */
1353
1354 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1355 if (template_depth
1356 && DECL_CLASS_SCOPE_P (decl)
1357 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1358 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1359 {
1360 /* Next, we check the members themselves. In order to handle
1361 a few tricky cases, such as when FRIEND_DECL's are
1362
1363 template <class T> friend void A<T>::g(T t);
1364 template <class T> template <T t> friend void A<T>::h();
1365
1366 and DECL's are
1367
1368 void A<int>::g(int);
1369 template <int> void A<int>::h();
1370
1371 we need to figure out ARGS, the template arguments from
1372 the context of DECL. This is required for template substitution
1373 of `T' in the function parameter of `g' and template parameter
1374 of `h' in the above examples. Here ARGS corresponds to `int'. */
1375
1376 tree context = DECL_CONTEXT (decl);
1377 tree args = NULL_TREE;
1378 int current_depth = 0;
1379
1380 while (current_depth < template_depth)
1381 {
1382 if (CLASSTYPE_TEMPLATE_INFO (context))
1383 {
1384 if (current_depth == 0)
1385 args = TYPE_TI_ARGS (context);
1386 else
1387 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1388 current_depth++;
1389 }
1390 context = TYPE_CONTEXT (context);
1391 }
1392
1393 if (TREE_CODE (decl) == FUNCTION_DECL)
1394 {
1395 bool is_template;
1396 tree friend_type;
1397 tree decl_type;
1398 tree friend_args_type;
1399 tree decl_args_type;
1400
1401 /* Make sure that both DECL and FRIEND_DECL are templates or
1402 non-templates. */
1403 is_template = DECL_TEMPLATE_INFO (decl)
1404 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1405 if (need_template ^ is_template)
1406 return false;
1407 else if (is_template)
1408 {
1409 /* If both are templates, check template parameter list. */
1410 tree friend_parms
1411 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1412 args, tf_none);
1413 if (!comp_template_parms
1414 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1415 friend_parms))
1416 return false;
1417
1418 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1419 }
1420 else
1421 decl_type = TREE_TYPE (decl);
1422
1423 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1424 tf_none, NULL_TREE);
1425 if (friend_type == error_mark_node)
1426 return false;
1427
1428 /* Check if return types match. */
1429 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1430 return false;
1431
1432 /* Check if function parameter types match, ignoring the
1433 `this' parameter. */
1434 friend_args_type = TYPE_ARG_TYPES (friend_type);
1435 decl_args_type = TYPE_ARG_TYPES (decl_type);
1436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1437 friend_args_type = TREE_CHAIN (friend_args_type);
1438 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1439 decl_args_type = TREE_CHAIN (decl_args_type);
1440
1441 return compparms (decl_args_type, friend_args_type);
1442 }
1443 else
1444 {
1445 /* DECL is a TYPE_DECL */
1446 bool is_template;
1447 tree decl_type = TREE_TYPE (decl);
1448
1449 /* Make sure that both DECL and FRIEND_DECL are templates or
1450 non-templates. */
1451 is_template
1452 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1453 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1454
1455 if (need_template ^ is_template)
1456 return false;
1457 else if (is_template)
1458 {
1459 tree friend_parms;
1460 /* If both are templates, check the name of the two
1461 TEMPLATE_DECL's first because is_friend didn't. */
1462 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1463 != DECL_NAME (friend_decl))
1464 return false;
1465
1466 /* Now check template parameter list. */
1467 friend_parms
1468 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1469 args, tf_none);
1470 return comp_template_parms
1471 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1472 friend_parms);
1473 }
1474 else
1475 return (DECL_NAME (decl)
1476 == DECL_NAME (friend_decl));
1477 }
1478 }
1479 return false;
1480 }
1481
1482 /* Register the specialization SPEC as a specialization of TMPL with
1483 the indicated ARGS. IS_FRIEND indicates whether the specialization
1484 is actually just a friend declaration. ATTRLIST is the list of
1485 attributes that the specialization is declared with or NULL when
1486 it isn't. Returns SPEC, or an equivalent prior declaration, if
1487 available.
1488
1489 We also store instantiations of field packs in the hash table, even
1490 though they are not themselves templates, to make lookup easier. */
1491
1492 static tree
1493 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1494 hashval_t hash)
1495 {
1496 tree fn;
1497 spec_entry **slot = NULL;
1498 spec_entry elt;
1499
1500 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1501 || (TREE_CODE (tmpl) == FIELD_DECL
1502 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1503
1504 if (TREE_CODE (spec) == FUNCTION_DECL
1505 && uses_template_parms (DECL_TI_ARGS (spec)))
1506 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1507 register it; we want the corresponding TEMPLATE_DECL instead.
1508 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1509 the more obvious `uses_template_parms (spec)' to avoid problems
1510 with default function arguments. In particular, given
1511 something like this:
1512
1513 template <class T> void f(T t1, T t = T())
1514
1515 the default argument expression is not substituted for in an
1516 instantiation unless and until it is actually needed. */
1517 return spec;
1518
1519 if (optimize_specialization_lookup_p (tmpl))
1520 /* We don't put these specializations in the hash table, but we might
1521 want to give an error about a mismatch. */
1522 fn = retrieve_specialization (tmpl, args, 0);
1523 else
1524 {
1525 elt.tmpl = tmpl;
1526 elt.args = args;
1527 elt.spec = spec;
1528
1529 if (hash == 0)
1530 hash = spec_hasher::hash (&elt);
1531
1532 slot =
1533 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1534 if (*slot)
1535 fn = ((spec_entry *) *slot)->spec;
1536 else
1537 fn = NULL_TREE;
1538 }
1539
1540 /* We can sometimes try to re-register a specialization that we've
1541 already got. In particular, regenerate_decl_from_template calls
1542 duplicate_decls which will update the specialization list. But,
1543 we'll still get called again here anyhow. It's more convenient
1544 to simply allow this than to try to prevent it. */
1545 if (fn == spec)
1546 return spec;
1547 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1548 {
1549 if (DECL_TEMPLATE_INSTANTIATION (fn))
1550 {
1551 if (DECL_ODR_USED (fn)
1552 || DECL_EXPLICIT_INSTANTIATION (fn))
1553 {
1554 error ("specialization of %qD after instantiation",
1555 fn);
1556 return error_mark_node;
1557 }
1558 else
1559 {
1560 tree clone;
1561 /* This situation should occur only if the first
1562 specialization is an implicit instantiation, the
1563 second is an explicit specialization, and the
1564 implicit instantiation has not yet been used. That
1565 situation can occur if we have implicitly
1566 instantiated a member function and then specialized
1567 it later.
1568
1569 We can also wind up here if a friend declaration that
1570 looked like an instantiation turns out to be a
1571 specialization:
1572
1573 template <class T> void foo(T);
1574 class S { friend void foo<>(int) };
1575 template <> void foo(int);
1576
1577 We transform the existing DECL in place so that any
1578 pointers to it become pointers to the updated
1579 declaration.
1580
1581 If there was a definition for the template, but not
1582 for the specialization, we want this to look as if
1583 there were no definition, and vice versa. */
1584 DECL_INITIAL (fn) = NULL_TREE;
1585 duplicate_decls (spec, fn, is_friend);
1586 /* The call to duplicate_decls will have applied
1587 [temp.expl.spec]:
1588
1589 An explicit specialization of a function template
1590 is inline only if it is explicitly declared to be,
1591 and independently of whether its function template
1592 is.
1593
1594 to the primary function; now copy the inline bits to
1595 the various clones. */
1596 FOR_EACH_CLONE (clone, fn)
1597 {
1598 DECL_DECLARED_INLINE_P (clone)
1599 = DECL_DECLARED_INLINE_P (fn);
1600 DECL_SOURCE_LOCATION (clone)
1601 = DECL_SOURCE_LOCATION (fn);
1602 DECL_DELETED_FN (clone)
1603 = DECL_DELETED_FN (fn);
1604 }
1605 check_specialization_namespace (tmpl);
1606
1607 return fn;
1608 }
1609 }
1610 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1611 {
1612 tree dd = duplicate_decls (spec, fn, is_friend);
1613 if (dd == error_mark_node)
1614 /* We've already complained in duplicate_decls. */
1615 return error_mark_node;
1616
1617 if (dd == NULL_TREE && DECL_INITIAL (spec))
1618 /* Dup decl failed, but this is a new definition. Set the
1619 line number so any errors match this new
1620 definition. */
1621 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1622
1623 return fn;
1624 }
1625 }
1626 else if (fn)
1627 return duplicate_decls (spec, fn, is_friend);
1628
1629 /* A specialization must be declared in the same namespace as the
1630 template it is specializing. */
1631 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1632 && !check_specialization_namespace (tmpl))
1633 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1634
1635 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1636 {
1637 spec_entry *entry = ggc_alloc<spec_entry> ();
1638 gcc_assert (tmpl && args && spec);
1639 *entry = elt;
1640 *slot = entry;
1641 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1642 && PRIMARY_TEMPLATE_P (tmpl)
1643 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1644 || variable_template_p (tmpl))
1645 /* If TMPL is a forward declaration of a template function, keep a list
1646 of all specializations in case we need to reassign them to a friend
1647 template later in tsubst_friend_function.
1648
1649 Also keep a list of all variable template instantiations so that
1650 process_partial_specialization can check whether a later partial
1651 specialization would have used it. */
1652 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1653 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1654 }
1655
1656 return spec;
1657 }
1658
1659 /* Returns true iff two spec_entry nodes are equivalent. */
1660
1661 int comparing_specializations;
1662
1663 bool
1664 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1665 {
1666 int equal;
1667
1668 ++comparing_specializations;
1669 equal = (e1->tmpl == e2->tmpl
1670 && comp_template_args (e1->args, e2->args));
1671 if (equal && flag_concepts
1672 /* tmpl could be a FIELD_DECL for a capture pack. */
1673 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1674 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1675 && uses_template_parms (e1->args))
1676 {
1677 /* Partial specializations of a variable template can be distinguished by
1678 constraints. */
1679 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1680 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1681 equal = equivalent_constraints (c1, c2);
1682 }
1683 --comparing_specializations;
1684
1685 return equal;
1686 }
1687
1688 /* Returns a hash for a template TMPL and template arguments ARGS. */
1689
1690 static hashval_t
1691 hash_tmpl_and_args (tree tmpl, tree args)
1692 {
1693 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1694 return iterative_hash_template_arg (args, val);
1695 }
1696
1697 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1698 ignoring SPEC. */
1699
1700 hashval_t
1701 spec_hasher::hash (spec_entry *e)
1702 {
1703 return hash_tmpl_and_args (e->tmpl, e->args);
1704 }
1705
1706 /* Recursively calculate a hash value for a template argument ARG, for use
1707 in the hash tables of template specializations. */
1708
1709 hashval_t
1710 iterative_hash_template_arg (tree arg, hashval_t val)
1711 {
1712 unsigned HOST_WIDE_INT i;
1713 enum tree_code code;
1714 char tclass;
1715
1716 if (arg == NULL_TREE)
1717 return iterative_hash_object (arg, val);
1718
1719 if (!TYPE_P (arg))
1720 STRIP_NOPS (arg);
1721
1722 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1723 gcc_unreachable ();
1724
1725 code = TREE_CODE (arg);
1726 tclass = TREE_CODE_CLASS (code);
1727
1728 val = iterative_hash_object (code, val);
1729
1730 switch (code)
1731 {
1732 case ERROR_MARK:
1733 return val;
1734
1735 case IDENTIFIER_NODE:
1736 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1737
1738 case TREE_VEC:
1739 {
1740 int i, len = TREE_VEC_LENGTH (arg);
1741 for (i = 0; i < len; ++i)
1742 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1743 return val;
1744 }
1745
1746 case TYPE_PACK_EXPANSION:
1747 case EXPR_PACK_EXPANSION:
1748 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1749 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1750
1751 case TYPE_ARGUMENT_PACK:
1752 case NONTYPE_ARGUMENT_PACK:
1753 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1754
1755 case TREE_LIST:
1756 for (; arg; arg = TREE_CHAIN (arg))
1757 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1758 return val;
1759
1760 case OVERLOAD:
1761 for (lkp_iterator iter (arg); iter; ++iter)
1762 val = iterative_hash_template_arg (*iter, val);
1763 return val;
1764
1765 case CONSTRUCTOR:
1766 {
1767 tree field, value;
1768 iterative_hash_template_arg (TREE_TYPE (arg), val);
1769 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1770 {
1771 val = iterative_hash_template_arg (field, val);
1772 val = iterative_hash_template_arg (value, val);
1773 }
1774 return val;
1775 }
1776
1777 case PARM_DECL:
1778 if (!DECL_ARTIFICIAL (arg))
1779 {
1780 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1781 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1782 }
1783 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1784
1785 case TARGET_EXPR:
1786 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1787
1788 case PTRMEM_CST:
1789 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1790 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1791
1792 case TEMPLATE_PARM_INDEX:
1793 val = iterative_hash_template_arg
1794 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1795 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1796 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1797
1798 case TRAIT_EXPR:
1799 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1800 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1801 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1802
1803 case BASELINK:
1804 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1805 val);
1806 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1807 val);
1808
1809 case MODOP_EXPR:
1810 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1811 code = TREE_CODE (TREE_OPERAND (arg, 1));
1812 val = iterative_hash_object (code, val);
1813 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1814
1815 case LAMBDA_EXPR:
1816 /* A lambda can't appear in a template arg, but don't crash on
1817 erroneous input. */
1818 gcc_assert (seen_error ());
1819 return val;
1820
1821 case CAST_EXPR:
1822 case IMPLICIT_CONV_EXPR:
1823 case STATIC_CAST_EXPR:
1824 case REINTERPRET_CAST_EXPR:
1825 case CONST_CAST_EXPR:
1826 case DYNAMIC_CAST_EXPR:
1827 case NEW_EXPR:
1828 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1829 /* Now hash operands as usual. */
1830 break;
1831
1832 default:
1833 break;
1834 }
1835
1836 switch (tclass)
1837 {
1838 case tcc_type:
1839 if (alias_template_specialization_p (arg))
1840 {
1841 // We want an alias specialization that survived strip_typedefs
1842 // to hash differently from its TYPE_CANONICAL, to avoid hash
1843 // collisions that compare as different in template_args_equal.
1844 // These could be dependent specializations that strip_typedefs
1845 // left alone, or untouched specializations because
1846 // coerce_template_parms returns the unconverted template
1847 // arguments if it sees incomplete argument packs.
1848 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1849 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1850 }
1851 if (TYPE_CANONICAL (arg))
1852 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1853 val);
1854 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1855 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1856 /* Otherwise just compare the types during lookup. */
1857 return val;
1858
1859 case tcc_declaration:
1860 case tcc_constant:
1861 return iterative_hash_expr (arg, val);
1862
1863 default:
1864 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1865 {
1866 unsigned n = cp_tree_operand_length (arg);
1867 for (i = 0; i < n; ++i)
1868 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1869 return val;
1870 }
1871 }
1872 gcc_unreachable ();
1873 return 0;
1874 }
1875
1876 /* Unregister the specialization SPEC as a specialization of TMPL.
1877 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1878 if the SPEC was listed as a specialization of TMPL.
1879
1880 Note that SPEC has been ggc_freed, so we can't look inside it. */
1881
1882 bool
1883 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1884 {
1885 spec_entry *entry;
1886 spec_entry elt;
1887
1888 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1889 elt.args = TI_ARGS (tinfo);
1890 elt.spec = NULL_TREE;
1891
1892 entry = decl_specializations->find (&elt);
1893 if (entry != NULL)
1894 {
1895 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1896 gcc_assert (new_spec != NULL_TREE);
1897 entry->spec = new_spec;
1898 return 1;
1899 }
1900
1901 return 0;
1902 }
1903
1904 /* Like register_specialization, but for local declarations. We are
1905 registering SPEC, an instantiation of TMPL. */
1906
1907 void
1908 register_local_specialization (tree spec, tree tmpl)
1909 {
1910 gcc_assert (tmpl != spec);
1911 local_specializations->put (tmpl, spec);
1912 }
1913
1914 /* TYPE is a class type. Returns true if TYPE is an explicitly
1915 specialized class. */
1916
1917 bool
1918 explicit_class_specialization_p (tree type)
1919 {
1920 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1921 return false;
1922 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1923 }
1924
1925 /* Print the list of functions at FNS, going through all the overloads
1926 for each element of the list. Alternatively, FNS can not be a
1927 TREE_LIST, in which case it will be printed together with all the
1928 overloads.
1929
1930 MORE and *STR should respectively be FALSE and NULL when the function
1931 is called from the outside. They are used internally on recursive
1932 calls. print_candidates manages the two parameters and leaves NULL
1933 in *STR when it ends. */
1934
1935 static void
1936 print_candidates_1 (tree fns, char **str, bool more = false)
1937 {
1938 if (TREE_CODE (fns) == TREE_LIST)
1939 for (; fns; fns = TREE_CHAIN (fns))
1940 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1941 else
1942 for (lkp_iterator iter (fns); iter;)
1943 {
1944 tree cand = *iter;
1945 ++iter;
1946
1947 const char *pfx = *str;
1948 if (!pfx)
1949 {
1950 if (more || iter)
1951 pfx = _("candidates are:");
1952 else
1953 pfx = _("candidate is:");
1954 *str = get_spaces (pfx);
1955 }
1956 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1957 }
1958 }
1959
1960 /* Print the list of candidate FNS in an error message. FNS can also
1961 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1962
1963 void
1964 print_candidates (tree fns)
1965 {
1966 char *str = NULL;
1967 print_candidates_1 (fns, &str);
1968 free (str);
1969 }
1970
1971 /* Get a (possibly) constrained template declaration for the
1972 purpose of ordering candidates. */
1973 static tree
1974 get_template_for_ordering (tree list)
1975 {
1976 gcc_assert (TREE_CODE (list) == TREE_LIST);
1977 tree f = TREE_VALUE (list);
1978 if (tree ti = DECL_TEMPLATE_INFO (f))
1979 return TI_TEMPLATE (ti);
1980 return f;
1981 }
1982
1983 /* Among candidates having the same signature, return the
1984 most constrained or NULL_TREE if there is no best candidate.
1985 If the signatures of candidates vary (e.g., template
1986 specialization vs. member function), then there can be no
1987 most constrained.
1988
1989 Note that we don't compare constraints on the functions
1990 themselves, but rather those of their templates. */
1991 static tree
1992 most_constrained_function (tree candidates)
1993 {
1994 // Try to find the best candidate in a first pass.
1995 tree champ = candidates;
1996 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1997 {
1998 int winner = more_constrained (get_template_for_ordering (champ),
1999 get_template_for_ordering (c));
2000 if (winner == -1)
2001 champ = c; // The candidate is more constrained
2002 else if (winner == 0)
2003 return NULL_TREE; // Neither is more constrained
2004 }
2005
2006 // Verify that the champ is better than previous candidates.
2007 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2008 if (!more_constrained (get_template_for_ordering (champ),
2009 get_template_for_ordering (c)))
2010 return NULL_TREE;
2011 }
2012
2013 return champ;
2014 }
2015
2016
2017 /* Returns the template (one of the functions given by TEMPLATE_ID)
2018 which can be specialized to match the indicated DECL with the
2019 explicit template args given in TEMPLATE_ID. The DECL may be
2020 NULL_TREE if none is available. In that case, the functions in
2021 TEMPLATE_ID are non-members.
2022
2023 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2024 specialization of a member template.
2025
2026 The TEMPLATE_COUNT is the number of references to qualifying
2027 template classes that appeared in the name of the function. See
2028 check_explicit_specialization for a more accurate description.
2029
2030 TSK indicates what kind of template declaration (if any) is being
2031 declared. TSK_TEMPLATE indicates that the declaration given by
2032 DECL, though a FUNCTION_DECL, has template parameters, and is
2033 therefore a template function.
2034
2035 The template args (those explicitly specified and those deduced)
2036 are output in a newly created vector *TARGS_OUT.
2037
2038 If it is impossible to determine the result, an error message is
2039 issued. The error_mark_node is returned to indicate failure. */
2040
2041 static tree
2042 determine_specialization (tree template_id,
2043 tree decl,
2044 tree* targs_out,
2045 int need_member_template,
2046 int template_count,
2047 tmpl_spec_kind tsk)
2048 {
2049 tree fns;
2050 tree targs;
2051 tree explicit_targs;
2052 tree candidates = NULL_TREE;
2053
2054 /* A TREE_LIST of templates of which DECL may be a specialization.
2055 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2056 corresponding TREE_PURPOSE is the set of template arguments that,
2057 when used to instantiate the template, would produce a function
2058 with the signature of DECL. */
2059 tree templates = NULL_TREE;
2060 int header_count;
2061 cp_binding_level *b;
2062
2063 *targs_out = NULL_TREE;
2064
2065 if (template_id == error_mark_node || decl == error_mark_node)
2066 return error_mark_node;
2067
2068 /* We shouldn't be specializing a member template of an
2069 unspecialized class template; we already gave an error in
2070 check_specialization_scope, now avoid crashing. */
2071 if (!VAR_P (decl)
2072 && template_count && DECL_CLASS_SCOPE_P (decl)
2073 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2074 {
2075 gcc_assert (errorcount);
2076 return error_mark_node;
2077 }
2078
2079 fns = TREE_OPERAND (template_id, 0);
2080 explicit_targs = TREE_OPERAND (template_id, 1);
2081
2082 if (fns == error_mark_node)
2083 return error_mark_node;
2084
2085 /* Check for baselinks. */
2086 if (BASELINK_P (fns))
2087 fns = BASELINK_FUNCTIONS (fns);
2088
2089 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2090 {
2091 error ("%qD is not a function template", fns);
2092 return error_mark_node;
2093 }
2094 else if (VAR_P (decl) && !variable_template_p (fns))
2095 {
2096 error ("%qD is not a variable template", fns);
2097 return error_mark_node;
2098 }
2099
2100 /* Count the number of template headers specified for this
2101 specialization. */
2102 header_count = 0;
2103 for (b = current_binding_level;
2104 b->kind == sk_template_parms;
2105 b = b->level_chain)
2106 ++header_count;
2107
2108 tree orig_fns = fns;
2109
2110 if (variable_template_p (fns))
2111 {
2112 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2113 targs = coerce_template_parms (parms, explicit_targs, fns,
2114 tf_warning_or_error,
2115 /*req_all*/true, /*use_defarg*/true);
2116 if (targs != error_mark_node)
2117 templates = tree_cons (targs, fns, templates);
2118 }
2119 else for (lkp_iterator iter (fns); iter; ++iter)
2120 {
2121 tree fn = *iter;
2122
2123 if (TREE_CODE (fn) == TEMPLATE_DECL)
2124 {
2125 tree decl_arg_types;
2126 tree fn_arg_types;
2127 tree insttype;
2128
2129 /* In case of explicit specialization, we need to check if
2130 the number of template headers appearing in the specialization
2131 is correct. This is usually done in check_explicit_specialization,
2132 but the check done there cannot be exhaustive when specializing
2133 member functions. Consider the following code:
2134
2135 template <> void A<int>::f(int);
2136 template <> template <> void A<int>::f(int);
2137
2138 Assuming that A<int> is not itself an explicit specialization
2139 already, the first line specializes "f" which is a non-template
2140 member function, whilst the second line specializes "f" which
2141 is a template member function. So both lines are syntactically
2142 correct, and check_explicit_specialization does not reject
2143 them.
2144
2145 Here, we can do better, as we are matching the specialization
2146 against the declarations. We count the number of template
2147 headers, and we check if they match TEMPLATE_COUNT + 1
2148 (TEMPLATE_COUNT is the number of qualifying template classes,
2149 plus there must be another header for the member template
2150 itself).
2151
2152 Notice that if header_count is zero, this is not a
2153 specialization but rather a template instantiation, so there
2154 is no check we can perform here. */
2155 if (header_count && header_count != template_count + 1)
2156 continue;
2157
2158 /* Check that the number of template arguments at the
2159 innermost level for DECL is the same as for FN. */
2160 if (current_binding_level->kind == sk_template_parms
2161 && !current_binding_level->explicit_spec_p
2162 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2163 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2164 (current_template_parms))))
2165 continue;
2166
2167 /* DECL might be a specialization of FN. */
2168 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2169 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2170
2171 /* For a non-static member function, we need to make sure
2172 that the const qualification is the same. Since
2173 get_bindings does not try to merge the "this" parameter,
2174 we must do the comparison explicitly. */
2175 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2176 {
2177 if (!same_type_p (TREE_VALUE (fn_arg_types),
2178 TREE_VALUE (decl_arg_types)))
2179 continue;
2180
2181 /* And the ref-qualification. */
2182 if (type_memfn_rqual (TREE_TYPE (decl))
2183 != type_memfn_rqual (TREE_TYPE (fn)))
2184 continue;
2185 }
2186
2187 /* Skip the "this" parameter and, for constructors of
2188 classes with virtual bases, the VTT parameter. A
2189 full specialization of a constructor will have a VTT
2190 parameter, but a template never will. */
2191 decl_arg_types
2192 = skip_artificial_parms_for (decl, decl_arg_types);
2193 fn_arg_types
2194 = skip_artificial_parms_for (fn, fn_arg_types);
2195
2196 /* Function templates cannot be specializations; there are
2197 no partial specializations of functions. Therefore, if
2198 the type of DECL does not match FN, there is no
2199 match.
2200
2201 Note that it should never be the case that we have both
2202 candidates added here, and for regular member functions
2203 below. */
2204 if (tsk == tsk_template)
2205 {
2206 if (compparms (fn_arg_types, decl_arg_types))
2207 candidates = tree_cons (NULL_TREE, fn, candidates);
2208 continue;
2209 }
2210
2211 /* See whether this function might be a specialization of this
2212 template. Suppress access control because we might be trying
2213 to make this specialization a friend, and we have already done
2214 access control for the declaration of the specialization. */
2215 push_deferring_access_checks (dk_no_check);
2216 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2217 pop_deferring_access_checks ();
2218
2219 if (!targs)
2220 /* We cannot deduce template arguments that when used to
2221 specialize TMPL will produce DECL. */
2222 continue;
2223
2224 if (uses_template_parms (targs))
2225 /* We deduced something involving 'auto', which isn't a valid
2226 template argument. */
2227 continue;
2228
2229 /* Remove, from the set of candidates, all those functions
2230 whose constraints are not satisfied. */
2231 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2232 continue;
2233
2234 // Then, try to form the new function type.
2235 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2236 if (insttype == error_mark_node)
2237 continue;
2238 fn_arg_types
2239 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2240 if (!compparms (fn_arg_types, decl_arg_types))
2241 continue;
2242
2243 /* Save this template, and the arguments deduced. */
2244 templates = tree_cons (targs, fn, templates);
2245 }
2246 else if (need_member_template)
2247 /* FN is an ordinary member function, and we need a
2248 specialization of a member template. */
2249 ;
2250 else if (TREE_CODE (fn) != FUNCTION_DECL)
2251 /* We can get IDENTIFIER_NODEs here in certain erroneous
2252 cases. */
2253 ;
2254 else if (!DECL_FUNCTION_MEMBER_P (fn))
2255 /* This is just an ordinary non-member function. Nothing can
2256 be a specialization of that. */
2257 ;
2258 else if (DECL_ARTIFICIAL (fn))
2259 /* Cannot specialize functions that are created implicitly. */
2260 ;
2261 else
2262 {
2263 tree decl_arg_types;
2264
2265 /* This is an ordinary member function. However, since
2266 we're here, we can assume its enclosing class is a
2267 template class. For example,
2268
2269 template <typename T> struct S { void f(); };
2270 template <> void S<int>::f() {}
2271
2272 Here, S<int>::f is a non-template, but S<int> is a
2273 template class. If FN has the same type as DECL, we
2274 might be in business. */
2275
2276 if (!DECL_TEMPLATE_INFO (fn))
2277 /* Its enclosing class is an explicit specialization
2278 of a template class. This is not a candidate. */
2279 continue;
2280
2281 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2282 TREE_TYPE (TREE_TYPE (fn))))
2283 /* The return types differ. */
2284 continue;
2285
2286 /* Adjust the type of DECL in case FN is a static member. */
2287 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2288 if (DECL_STATIC_FUNCTION_P (fn)
2289 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2290 decl_arg_types = TREE_CHAIN (decl_arg_types);
2291
2292 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2293 decl_arg_types))
2294 continue;
2295
2296 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2297 && (type_memfn_rqual (TREE_TYPE (decl))
2298 != type_memfn_rqual (TREE_TYPE (fn))))
2299 continue;
2300
2301 // If the deduced arguments do not satisfy the constraints,
2302 // this is not a candidate.
2303 if (flag_concepts && !constraints_satisfied_p (fn))
2304 continue;
2305
2306 // Add the candidate.
2307 candidates = tree_cons (NULL_TREE, fn, candidates);
2308 }
2309 }
2310
2311 if (templates && TREE_CHAIN (templates))
2312 {
2313 /* We have:
2314
2315 [temp.expl.spec]
2316
2317 It is possible for a specialization with a given function
2318 signature to be instantiated from more than one function
2319 template. In such cases, explicit specification of the
2320 template arguments must be used to uniquely identify the
2321 function template specialization being specialized.
2322
2323 Note that here, there's no suggestion that we're supposed to
2324 determine which of the candidate templates is most
2325 specialized. However, we, also have:
2326
2327 [temp.func.order]
2328
2329 Partial ordering of overloaded function template
2330 declarations is used in the following contexts to select
2331 the function template to which a function template
2332 specialization refers:
2333
2334 -- when an explicit specialization refers to a function
2335 template.
2336
2337 So, we do use the partial ordering rules, at least for now.
2338 This extension can only serve to make invalid programs valid,
2339 so it's safe. And, there is strong anecdotal evidence that
2340 the committee intended the partial ordering rules to apply;
2341 the EDG front end has that behavior, and John Spicer claims
2342 that the committee simply forgot to delete the wording in
2343 [temp.expl.spec]. */
2344 tree tmpl = most_specialized_instantiation (templates);
2345 if (tmpl != error_mark_node)
2346 {
2347 templates = tmpl;
2348 TREE_CHAIN (templates) = NULL_TREE;
2349 }
2350 }
2351
2352 // Concepts allows multiple declarations of member functions
2353 // with the same signature. Like above, we need to rely on
2354 // on the partial ordering of those candidates to determine which
2355 // is the best.
2356 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2357 {
2358 if (tree cand = most_constrained_function (candidates))
2359 {
2360 candidates = cand;
2361 TREE_CHAIN (cand) = NULL_TREE;
2362 }
2363 }
2364
2365 if (templates == NULL_TREE && candidates == NULL_TREE)
2366 {
2367 error ("template-id %qD for %q+D does not match any template "
2368 "declaration", template_id, decl);
2369 if (header_count && header_count != template_count + 1)
2370 inform (input_location, "saw %d %<template<>%>, need %d for "
2371 "specializing a member function template",
2372 header_count, template_count + 1);
2373 else
2374 print_candidates (orig_fns);
2375 return error_mark_node;
2376 }
2377 else if ((templates && TREE_CHAIN (templates))
2378 || (candidates && TREE_CHAIN (candidates))
2379 || (templates && candidates))
2380 {
2381 error ("ambiguous template specialization %qD for %q+D",
2382 template_id, decl);
2383 candidates = chainon (candidates, templates);
2384 print_candidates (candidates);
2385 return error_mark_node;
2386 }
2387
2388 /* We have one, and exactly one, match. */
2389 if (candidates)
2390 {
2391 tree fn = TREE_VALUE (candidates);
2392 *targs_out = copy_node (DECL_TI_ARGS (fn));
2393
2394 // Propagate the candidate's constraints to the declaration.
2395 set_constraints (decl, get_constraints (fn));
2396
2397 /* DECL is a re-declaration or partial instantiation of a template
2398 function. */
2399 if (TREE_CODE (fn) == TEMPLATE_DECL)
2400 return fn;
2401 /* It was a specialization of an ordinary member function in a
2402 template class. */
2403 return DECL_TI_TEMPLATE (fn);
2404 }
2405
2406 /* It was a specialization of a template. */
2407 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2408 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2409 {
2410 *targs_out = copy_node (targs);
2411 SET_TMPL_ARGS_LEVEL (*targs_out,
2412 TMPL_ARGS_DEPTH (*targs_out),
2413 TREE_PURPOSE (templates));
2414 }
2415 else
2416 *targs_out = TREE_PURPOSE (templates);
2417 return TREE_VALUE (templates);
2418 }
2419
2420 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2421 but with the default argument values filled in from those in the
2422 TMPL_TYPES. */
2423
2424 static tree
2425 copy_default_args_to_explicit_spec_1 (tree spec_types,
2426 tree tmpl_types)
2427 {
2428 tree new_spec_types;
2429
2430 if (!spec_types)
2431 return NULL_TREE;
2432
2433 if (spec_types == void_list_node)
2434 return void_list_node;
2435
2436 /* Substitute into the rest of the list. */
2437 new_spec_types =
2438 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2439 TREE_CHAIN (tmpl_types));
2440
2441 /* Add the default argument for this parameter. */
2442 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2443 TREE_VALUE (spec_types),
2444 new_spec_types);
2445 }
2446
2447 /* DECL is an explicit specialization. Replicate default arguments
2448 from the template it specializes. (That way, code like:
2449
2450 template <class T> void f(T = 3);
2451 template <> void f(double);
2452 void g () { f (); }
2453
2454 works, as required.) An alternative approach would be to look up
2455 the correct default arguments at the call-site, but this approach
2456 is consistent with how implicit instantiations are handled. */
2457
2458 static void
2459 copy_default_args_to_explicit_spec (tree decl)
2460 {
2461 tree tmpl;
2462 tree spec_types;
2463 tree tmpl_types;
2464 tree new_spec_types;
2465 tree old_type;
2466 tree new_type;
2467 tree t;
2468 tree object_type = NULL_TREE;
2469 tree in_charge = NULL_TREE;
2470 tree vtt = NULL_TREE;
2471
2472 /* See if there's anything we need to do. */
2473 tmpl = DECL_TI_TEMPLATE (decl);
2474 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2475 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2476 if (TREE_PURPOSE (t))
2477 break;
2478 if (!t)
2479 return;
2480
2481 old_type = TREE_TYPE (decl);
2482 spec_types = TYPE_ARG_TYPES (old_type);
2483
2484 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2485 {
2486 /* Remove the this pointer, but remember the object's type for
2487 CV quals. */
2488 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2489 spec_types = TREE_CHAIN (spec_types);
2490 tmpl_types = TREE_CHAIN (tmpl_types);
2491
2492 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2493 {
2494 /* DECL may contain more parameters than TMPL due to the extra
2495 in-charge parameter in constructors and destructors. */
2496 in_charge = spec_types;
2497 spec_types = TREE_CHAIN (spec_types);
2498 }
2499 if (DECL_HAS_VTT_PARM_P (decl))
2500 {
2501 vtt = spec_types;
2502 spec_types = TREE_CHAIN (spec_types);
2503 }
2504 }
2505
2506 /* Compute the merged default arguments. */
2507 new_spec_types =
2508 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2509
2510 /* Compute the new FUNCTION_TYPE. */
2511 if (object_type)
2512 {
2513 if (vtt)
2514 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2515 TREE_VALUE (vtt),
2516 new_spec_types);
2517
2518 if (in_charge)
2519 /* Put the in-charge parameter back. */
2520 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2521 TREE_VALUE (in_charge),
2522 new_spec_types);
2523
2524 new_type = build_method_type_directly (object_type,
2525 TREE_TYPE (old_type),
2526 new_spec_types);
2527 }
2528 else
2529 new_type = build_function_type (TREE_TYPE (old_type),
2530 new_spec_types);
2531 new_type = cp_build_type_attribute_variant (new_type,
2532 TYPE_ATTRIBUTES (old_type));
2533 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2534
2535 TREE_TYPE (decl) = new_type;
2536 }
2537
2538 /* Return the number of template headers we expect to see for a definition
2539 or specialization of CTYPE or one of its non-template members. */
2540
2541 int
2542 num_template_headers_for_class (tree ctype)
2543 {
2544 int num_templates = 0;
2545
2546 while (ctype && CLASS_TYPE_P (ctype))
2547 {
2548 /* You're supposed to have one `template <...>' for every
2549 template class, but you don't need one for a full
2550 specialization. For example:
2551
2552 template <class T> struct S{};
2553 template <> struct S<int> { void f(); };
2554 void S<int>::f () {}
2555
2556 is correct; there shouldn't be a `template <>' for the
2557 definition of `S<int>::f'. */
2558 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2559 /* If CTYPE does not have template information of any
2560 kind, then it is not a template, nor is it nested
2561 within a template. */
2562 break;
2563 if (explicit_class_specialization_p (ctype))
2564 break;
2565 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2566 ++num_templates;
2567
2568 ctype = TYPE_CONTEXT (ctype);
2569 }
2570
2571 return num_templates;
2572 }
2573
2574 /* Do a simple sanity check on the template headers that precede the
2575 variable declaration DECL. */
2576
2577 void
2578 check_template_variable (tree decl)
2579 {
2580 tree ctx = CP_DECL_CONTEXT (decl);
2581 int wanted = num_template_headers_for_class (ctx);
2582 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2583 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2584 {
2585 if (cxx_dialect < cxx14)
2586 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2587 "variable templates only available with "
2588 "-std=c++14 or -std=gnu++14");
2589
2590 // Namespace-scope variable templates should have a template header.
2591 ++wanted;
2592 }
2593 if (template_header_count > wanted)
2594 {
2595 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2596 "too many template headers for %qD "
2597 "(should be %d)",
2598 decl, wanted);
2599 if (warned && CLASS_TYPE_P (ctx)
2600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2601 inform (DECL_SOURCE_LOCATION (decl),
2602 "members of an explicitly specialized class are defined "
2603 "without a template header");
2604 }
2605 }
2606
2607 /* An explicit specialization whose declarator-id or class-head-name is not
2608 qualified shall be declared in the nearest enclosing namespace of the
2609 template, or, if the namespace is inline (7.3.1), any namespace from its
2610 enclosing namespace set.
2611
2612 If the name declared in the explicit instantiation is an unqualified name,
2613 the explicit instantiation shall appear in the namespace where its template
2614 is declared or, if that namespace is inline (7.3.1), any namespace from its
2615 enclosing namespace set. */
2616
2617 void
2618 check_unqualified_spec_or_inst (tree t, location_t loc)
2619 {
2620 tree tmpl = most_general_template (t);
2621 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2622 && !is_nested_namespace (current_namespace,
2623 CP_DECL_CONTEXT (tmpl), true))
2624 {
2625 if (processing_specialization)
2626 permerror (loc, "explicit specialization of %qD outside its "
2627 "namespace must use a nested-name-specifier", tmpl);
2628 else if (processing_explicit_instantiation
2629 && cxx_dialect >= cxx11)
2630 /* This was allowed in C++98, so only pedwarn. */
2631 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2632 "outside its namespace must use a nested-name-"
2633 "specifier", tmpl);
2634 }
2635 }
2636
2637 /* Warn for a template specialization SPEC that is missing some of a set
2638 of function or type attributes that the template TEMPL is declared with.
2639 ATTRLIST is a list of additional attributes that SPEC should be taken
2640 to ultimately be declared with. */
2641
2642 static void
2643 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2644 {
2645 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2646 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2647
2648 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2649 return;
2650
2651 /* Avoid warning if either declaration or its type is deprecated. */
2652 if (TREE_DEPRECATED (tmpl)
2653 || TREE_DEPRECATED (spec))
2654 return;
2655
2656 tree tmpl_type = TREE_TYPE (tmpl);
2657 tree spec_type = TREE_TYPE (spec);
2658
2659 if (TREE_DEPRECATED (tmpl_type)
2660 || TREE_DEPRECATED (spec_type)
2661 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2662 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2663 return;
2664
2665 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2666 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2667
2668 if (!spec_attrs[0])
2669 spec_attrs[0] = attrlist;
2670 else if (!spec_attrs[1])
2671 spec_attrs[1] = attrlist;
2672
2673 /* Avoid warning if the primary has no attributes. */
2674 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2675 return;
2676
2677 /* Avoid warning if either declaration contains an attribute on
2678 the white list below. */
2679 const char* const whitelist[] = {
2680 "error", "warning"
2681 };
2682
2683 for (unsigned i = 0; i != 2; ++i)
2684 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2685 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2686 || lookup_attribute (whitelist[j], spec_attrs[i]))
2687 return;
2688
2689 /* Avoid warning if the difference between the primary and
2690 the specialization is not in one of the attributes below. */
2691 const char* const blacklist[] = {
2692 "alloc_align", "alloc_size", "assume_aligned", "format",
2693 "format_arg", "malloc", "nonnull"
2694 };
2695
2696 /* Put together a list of the black listed attributes that the primary
2697 template is declared with that the specialization is not, in case
2698 it's not apparent from the most recent declaration of the primary. */
2699 unsigned nattrs = 0;
2700 pretty_printer str;
2701
2702 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2703 {
2704 for (unsigned j = 0; j != 2; ++j)
2705 {
2706 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2707 continue;
2708
2709 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2710 {
2711 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2712 break;
2713
2714 if (nattrs)
2715 pp_string (&str, ", ");
2716 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2717 pp_string (&str, blacklist[i]);
2718 pp_end_quote (&str, pp_show_color (global_dc->printer));
2719 ++nattrs;
2720 }
2721 }
2722 }
2723
2724 if (!nattrs)
2725 return;
2726
2727 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2728 "explicit specialization %q#D may be missing attributes",
2729 spec))
2730 inform (DECL_SOURCE_LOCATION (tmpl),
2731 nattrs > 1
2732 ? G_("missing primary template attributes %s")
2733 : G_("missing primary template attribute %s"),
2734 pp_formatted_text (&str));
2735 }
2736
2737 /* Check to see if the function just declared, as indicated in
2738 DECLARATOR, and in DECL, is a specialization of a function
2739 template. We may also discover that the declaration is an explicit
2740 instantiation at this point.
2741
2742 Returns DECL, or an equivalent declaration that should be used
2743 instead if all goes well. Issues an error message if something is
2744 amiss. Returns error_mark_node if the error is not easily
2745 recoverable.
2746
2747 FLAGS is a bitmask consisting of the following flags:
2748
2749 2: The function has a definition.
2750 4: The function is a friend.
2751
2752 The TEMPLATE_COUNT is the number of references to qualifying
2753 template classes that appeared in the name of the function. For
2754 example, in
2755
2756 template <class T> struct S { void f(); };
2757 void S<int>::f();
2758
2759 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2760 classes are not counted in the TEMPLATE_COUNT, so that in
2761
2762 template <class T> struct S {};
2763 template <> struct S<int> { void f(); }
2764 template <> void S<int>::f();
2765
2766 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2767 invalid; there should be no template <>.)
2768
2769 If the function is a specialization, it is marked as such via
2770 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2771 is set up correctly, and it is added to the list of specializations
2772 for that template. */
2773
2774 tree
2775 check_explicit_specialization (tree declarator,
2776 tree decl,
2777 int template_count,
2778 int flags,
2779 tree attrlist)
2780 {
2781 int have_def = flags & 2;
2782 int is_friend = flags & 4;
2783 bool is_concept = flags & 8;
2784 int specialization = 0;
2785 int explicit_instantiation = 0;
2786 int member_specialization = 0;
2787 tree ctype = DECL_CLASS_CONTEXT (decl);
2788 tree dname = DECL_NAME (decl);
2789 tmpl_spec_kind tsk;
2790
2791 if (is_friend)
2792 {
2793 if (!processing_specialization)
2794 tsk = tsk_none;
2795 else
2796 tsk = tsk_excessive_parms;
2797 }
2798 else
2799 tsk = current_tmpl_spec_kind (template_count);
2800
2801 switch (tsk)
2802 {
2803 case tsk_none:
2804 if (processing_specialization && !VAR_P (decl))
2805 {
2806 specialization = 1;
2807 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2808 }
2809 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2810 {
2811 if (is_friend)
2812 /* This could be something like:
2813
2814 template <class T> void f(T);
2815 class S { friend void f<>(int); } */
2816 specialization = 1;
2817 else
2818 {
2819 /* This case handles bogus declarations like template <>
2820 template <class T> void f<int>(); */
2821
2822 error ("template-id %qD in declaration of primary template",
2823 declarator);
2824 return decl;
2825 }
2826 }
2827 break;
2828
2829 case tsk_invalid_member_spec:
2830 /* The error has already been reported in
2831 check_specialization_scope. */
2832 return error_mark_node;
2833
2834 case tsk_invalid_expl_inst:
2835 error ("template parameter list used in explicit instantiation");
2836
2837 /* Fall through. */
2838
2839 case tsk_expl_inst:
2840 if (have_def)
2841 error ("definition provided for explicit instantiation");
2842
2843 explicit_instantiation = 1;
2844 break;
2845
2846 case tsk_excessive_parms:
2847 case tsk_insufficient_parms:
2848 if (tsk == tsk_excessive_parms)
2849 error ("too many template parameter lists in declaration of %qD",
2850 decl);
2851 else if (template_header_count)
2852 error("too few template parameter lists in declaration of %qD", decl);
2853 else
2854 error("explicit specialization of %qD must be introduced by "
2855 "%<template <>%>", decl);
2856
2857 /* Fall through. */
2858 case tsk_expl_spec:
2859 if (is_concept)
2860 error ("explicit specialization declared %<concept%>");
2861
2862 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2863 /* In cases like template<> constexpr bool v = true;
2864 We'll give an error in check_template_variable. */
2865 break;
2866
2867 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2868 if (ctype)
2869 member_specialization = 1;
2870 else
2871 specialization = 1;
2872 break;
2873
2874 case tsk_template:
2875 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2876 {
2877 /* This case handles bogus declarations like template <>
2878 template <class T> void f<int>(); */
2879
2880 if (!uses_template_parms (declarator))
2881 error ("template-id %qD in declaration of primary template",
2882 declarator);
2883 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2884 {
2885 /* Partial specialization of variable template. */
2886 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2887 specialization = 1;
2888 goto ok;
2889 }
2890 else if (cxx_dialect < cxx14)
2891 error ("non-type partial specialization %qD "
2892 "is not allowed", declarator);
2893 else
2894 error ("non-class, non-variable partial specialization %qD "
2895 "is not allowed", declarator);
2896 return decl;
2897 ok:;
2898 }
2899
2900 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2901 /* This is a specialization of a member template, without
2902 specialization the containing class. Something like:
2903
2904 template <class T> struct S {
2905 template <class U> void f (U);
2906 };
2907 template <> template <class U> void S<int>::f(U) {}
2908
2909 That's a specialization -- but of the entire template. */
2910 specialization = 1;
2911 break;
2912
2913 default:
2914 gcc_unreachable ();
2915 }
2916
2917 if ((specialization || member_specialization)
2918 /* This doesn't apply to variable templates. */
2919 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2920 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2921 {
2922 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2923 for (; t; t = TREE_CHAIN (t))
2924 if (TREE_PURPOSE (t))
2925 {
2926 permerror (input_location,
2927 "default argument specified in explicit specialization");
2928 break;
2929 }
2930 }
2931
2932 if (specialization || member_specialization || explicit_instantiation)
2933 {
2934 tree tmpl = NULL_TREE;
2935 tree targs = NULL_TREE;
2936 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2937
2938 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2939 if (!was_template_id)
2940 {
2941 tree fns;
2942
2943 gcc_assert (identifier_p (declarator));
2944 if (ctype)
2945 fns = dname;
2946 else
2947 {
2948 /* If there is no class context, the explicit instantiation
2949 must be at namespace scope. */
2950 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2951
2952 /* Find the namespace binding, using the declaration
2953 context. */
2954 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2955 false, true);
2956 if (fns == error_mark_node)
2957 /* If lookup fails, look for a friend declaration so we can
2958 give a better diagnostic. */
2959 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2960 /*type*/false, /*complain*/true,
2961 /*hidden*/true);
2962
2963 if (fns == error_mark_node || !is_overloaded_fn (fns))
2964 {
2965 error ("%qD is not a template function", dname);
2966 fns = error_mark_node;
2967 }
2968 }
2969
2970 declarator = lookup_template_function (fns, NULL_TREE);
2971 }
2972
2973 if (declarator == error_mark_node)
2974 return error_mark_node;
2975
2976 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2977 {
2978 if (!explicit_instantiation)
2979 /* A specialization in class scope. This is invalid,
2980 but the error will already have been flagged by
2981 check_specialization_scope. */
2982 return error_mark_node;
2983 else
2984 {
2985 /* It's not valid to write an explicit instantiation in
2986 class scope, e.g.:
2987
2988 class C { template void f(); }
2989
2990 This case is caught by the parser. However, on
2991 something like:
2992
2993 template class C { void f(); };
2994
2995 (which is invalid) we can get here. The error will be
2996 issued later. */
2997 ;
2998 }
2999
3000 return decl;
3001 }
3002 else if (ctype != NULL_TREE
3003 && (identifier_p (TREE_OPERAND (declarator, 0))))
3004 {
3005 // We'll match variable templates in start_decl.
3006 if (VAR_P (decl))
3007 return decl;
3008
3009 /* Find the list of functions in ctype that have the same
3010 name as the declared function. */
3011 tree name = TREE_OPERAND (declarator, 0);
3012
3013 if (constructor_name_p (name, ctype))
3014 {
3015 if (DECL_CONSTRUCTOR_P (decl)
3016 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3017 : !CLASSTYPE_DESTRUCTOR (ctype))
3018 {
3019 /* From [temp.expl.spec]:
3020
3021 If such an explicit specialization for the member
3022 of a class template names an implicitly-declared
3023 special member function (clause _special_), the
3024 program is ill-formed.
3025
3026 Similar language is found in [temp.explicit]. */
3027 error ("specialization of implicitly-declared special member function");
3028 return error_mark_node;
3029 }
3030
3031 name = DECL_NAME (decl);
3032 }
3033
3034 /* For a type-conversion operator, We might be looking for
3035 `operator int' which will be a specialization of
3036 `operator T'. Grab all the conversion operators, and
3037 then select from them. */
3038 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3039 ? conv_op_identifier : name);
3040
3041 if (fns == NULL_TREE)
3042 {
3043 error ("no member function %qD declared in %qT", name, ctype);
3044 return error_mark_node;
3045 }
3046 else
3047 TREE_OPERAND (declarator, 0) = fns;
3048 }
3049
3050 /* Figure out what exactly is being specialized at this point.
3051 Note that for an explicit instantiation, even one for a
3052 member function, we cannot tell a priori whether the
3053 instantiation is for a member template, or just a member
3054 function of a template class. Even if a member template is
3055 being instantiated, the member template arguments may be
3056 elided if they can be deduced from the rest of the
3057 declaration. */
3058 tmpl = determine_specialization (declarator, decl,
3059 &targs,
3060 member_specialization,
3061 template_count,
3062 tsk);
3063
3064 if (!tmpl || tmpl == error_mark_node)
3065 /* We couldn't figure out what this declaration was
3066 specializing. */
3067 return error_mark_node;
3068 else
3069 {
3070 if (TREE_CODE (decl) == FUNCTION_DECL
3071 && DECL_HIDDEN_FRIEND_P (tmpl))
3072 {
3073 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3074 "friend declaration %qD is not visible to "
3075 "explicit specialization", tmpl))
3076 inform (DECL_SOURCE_LOCATION (tmpl),
3077 "friend declaration here");
3078 }
3079 else if (!ctype && !is_friend
3080 && CP_DECL_CONTEXT (decl) == current_namespace)
3081 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3082
3083 tree gen_tmpl = most_general_template (tmpl);
3084
3085 if (explicit_instantiation)
3086 {
3087 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3088 is done by do_decl_instantiation later. */
3089
3090 int arg_depth = TMPL_ARGS_DEPTH (targs);
3091 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3092
3093 if (arg_depth > parm_depth)
3094 {
3095 /* If TMPL is not the most general template (for
3096 example, if TMPL is a friend template that is
3097 injected into namespace scope), then there will
3098 be too many levels of TARGS. Remove some of them
3099 here. */
3100 int i;
3101 tree new_targs;
3102
3103 new_targs = make_tree_vec (parm_depth);
3104 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3105 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3106 = TREE_VEC_ELT (targs, i);
3107 targs = new_targs;
3108 }
3109
3110 return instantiate_template (tmpl, targs, tf_error);
3111 }
3112
3113 /* If we thought that the DECL was a member function, but it
3114 turns out to be specializing a static member function,
3115 make DECL a static member function as well. */
3116 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3117 && DECL_STATIC_FUNCTION_P (tmpl)
3118 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3119 revert_static_member_fn (decl);
3120
3121 /* If this is a specialization of a member template of a
3122 template class, we want to return the TEMPLATE_DECL, not
3123 the specialization of it. */
3124 if (tsk == tsk_template && !was_template_id)
3125 {
3126 tree result = DECL_TEMPLATE_RESULT (tmpl);
3127 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3128 DECL_INITIAL (result) = NULL_TREE;
3129 if (have_def)
3130 {
3131 tree parm;
3132 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3133 DECL_SOURCE_LOCATION (result)
3134 = DECL_SOURCE_LOCATION (decl);
3135 /* We want to use the argument list specified in the
3136 definition, not in the original declaration. */
3137 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3138 for (parm = DECL_ARGUMENTS (result); parm;
3139 parm = DECL_CHAIN (parm))
3140 DECL_CONTEXT (parm) = result;
3141 }
3142 return register_specialization (tmpl, gen_tmpl, targs,
3143 is_friend, 0);
3144 }
3145
3146 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3147 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3148
3149 if (was_template_id)
3150 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3151
3152 /* Inherit default function arguments from the template
3153 DECL is specializing. */
3154 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3155 copy_default_args_to_explicit_spec (decl);
3156
3157 /* This specialization has the same protection as the
3158 template it specializes. */
3159 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3160 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3161
3162 /* 7.1.1-1 [dcl.stc]
3163
3164 A storage-class-specifier shall not be specified in an
3165 explicit specialization...
3166
3167 The parser rejects these, so unless action is taken here,
3168 explicit function specializations will always appear with
3169 global linkage.
3170
3171 The action recommended by the C++ CWG in response to C++
3172 defect report 605 is to make the storage class and linkage
3173 of the explicit specialization match the templated function:
3174
3175 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3176 */
3177 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3178 {
3179 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3180 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3181
3182 /* A concept cannot be specialized. */
3183 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3184 {
3185 error ("explicit specialization of function concept %qD",
3186 gen_tmpl);
3187 return error_mark_node;
3188 }
3189
3190 /* This specialization has the same linkage and visibility as
3191 the function template it specializes. */
3192 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3193 if (! TREE_PUBLIC (decl))
3194 {
3195 DECL_INTERFACE_KNOWN (decl) = 1;
3196 DECL_NOT_REALLY_EXTERN (decl) = 1;
3197 }
3198 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3199 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3200 {
3201 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3202 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3203 }
3204 }
3205
3206 /* If DECL is a friend declaration, declared using an
3207 unqualified name, the namespace associated with DECL may
3208 have been set incorrectly. For example, in:
3209
3210 template <typename T> void f(T);
3211 namespace N {
3212 struct S { friend void f<int>(int); }
3213 }
3214
3215 we will have set the DECL_CONTEXT for the friend
3216 declaration to N, rather than to the global namespace. */
3217 if (DECL_NAMESPACE_SCOPE_P (decl))
3218 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3219
3220 if (is_friend && !have_def)
3221 /* This is not really a declaration of a specialization.
3222 It's just the name of an instantiation. But, it's not
3223 a request for an instantiation, either. */
3224 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3225 else if (TREE_CODE (decl) == FUNCTION_DECL)
3226 /* A specialization is not necessarily COMDAT. */
3227 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3228 && DECL_DECLARED_INLINE_P (decl));
3229 else if (VAR_P (decl))
3230 DECL_COMDAT (decl) = false;
3231
3232 /* If this is a full specialization, register it so that we can find
3233 it again. Partial specializations will be registered in
3234 process_partial_specialization. */
3235 if (!processing_template_decl)
3236 {
3237 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3238
3239 decl = register_specialization (decl, gen_tmpl, targs,
3240 is_friend, 0);
3241 }
3242
3243
3244 /* A 'structor should already have clones. */
3245 gcc_assert (decl == error_mark_node
3246 || variable_template_p (tmpl)
3247 || !(DECL_CONSTRUCTOR_P (decl)
3248 || DECL_DESTRUCTOR_P (decl))
3249 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3250 }
3251 }
3252
3253 return decl;
3254 }
3255
3256 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3257 parameters. These are represented in the same format used for
3258 DECL_TEMPLATE_PARMS. */
3259
3260 int
3261 comp_template_parms (const_tree parms1, const_tree parms2)
3262 {
3263 const_tree p1;
3264 const_tree p2;
3265
3266 if (parms1 == parms2)
3267 return 1;
3268
3269 for (p1 = parms1, p2 = parms2;
3270 p1 != NULL_TREE && p2 != NULL_TREE;
3271 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3272 {
3273 tree t1 = TREE_VALUE (p1);
3274 tree t2 = TREE_VALUE (p2);
3275 int i;
3276
3277 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3278 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3279
3280 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3281 return 0;
3282
3283 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3284 {
3285 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3286 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3287
3288 /* If either of the template parameters are invalid, assume
3289 they match for the sake of error recovery. */
3290 if (error_operand_p (parm1) || error_operand_p (parm2))
3291 return 1;
3292
3293 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3294 return 0;
3295
3296 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3297 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3298 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3299 continue;
3300 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3301 return 0;
3302 }
3303 }
3304
3305 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3306 /* One set of parameters has more parameters lists than the
3307 other. */
3308 return 0;
3309
3310 return 1;
3311 }
3312
3313 /* Determine whether PARM is a parameter pack. */
3314
3315 bool
3316 template_parameter_pack_p (const_tree parm)
3317 {
3318 /* Determine if we have a non-type template parameter pack. */
3319 if (TREE_CODE (parm) == PARM_DECL)
3320 return (DECL_TEMPLATE_PARM_P (parm)
3321 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3322 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3323 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3324
3325 /* If this is a list of template parameters, we could get a
3326 TYPE_DECL or a TEMPLATE_DECL. */
3327 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3328 parm = TREE_TYPE (parm);
3329
3330 /* Otherwise it must be a type template parameter. */
3331 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3332 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3333 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3334 }
3335
3336 /* Determine if T is a function parameter pack. */
3337
3338 bool
3339 function_parameter_pack_p (const_tree t)
3340 {
3341 if (t && TREE_CODE (t) == PARM_DECL)
3342 return DECL_PACK_P (t);
3343 return false;
3344 }
3345
3346 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3347 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3348
3349 tree
3350 get_function_template_decl (const_tree primary_func_tmpl_inst)
3351 {
3352 if (! primary_func_tmpl_inst
3353 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3354 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3355 return NULL;
3356
3357 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3358 }
3359
3360 /* Return true iff the function parameter PARAM_DECL was expanded
3361 from the function parameter pack PACK. */
3362
3363 bool
3364 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3365 {
3366 if (DECL_ARTIFICIAL (param_decl)
3367 || !function_parameter_pack_p (pack))
3368 return false;
3369
3370 /* The parameter pack and its pack arguments have the same
3371 DECL_PARM_INDEX. */
3372 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3373 }
3374
3375 /* Determine whether ARGS describes a variadic template args list,
3376 i.e., one that is terminated by a template argument pack. */
3377
3378 static bool
3379 template_args_variadic_p (tree args)
3380 {
3381 int nargs;
3382 tree last_parm;
3383
3384 if (args == NULL_TREE)
3385 return false;
3386
3387 args = INNERMOST_TEMPLATE_ARGS (args);
3388 nargs = TREE_VEC_LENGTH (args);
3389
3390 if (nargs == 0)
3391 return false;
3392
3393 last_parm = TREE_VEC_ELT (args, nargs - 1);
3394
3395 return ARGUMENT_PACK_P (last_parm);
3396 }
3397
3398 /* Generate a new name for the parameter pack name NAME (an
3399 IDENTIFIER_NODE) that incorporates its */
3400
3401 static tree
3402 make_ith_pack_parameter_name (tree name, int i)
3403 {
3404 /* Munge the name to include the parameter index. */
3405 #define NUMBUF_LEN 128
3406 char numbuf[NUMBUF_LEN];
3407 char* newname;
3408 int newname_len;
3409
3410 if (name == NULL_TREE)
3411 return name;
3412 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3413 newname_len = IDENTIFIER_LENGTH (name)
3414 + strlen (numbuf) + 2;
3415 newname = (char*)alloca (newname_len);
3416 snprintf (newname, newname_len,
3417 "%s#%i", IDENTIFIER_POINTER (name), i);
3418 return get_identifier (newname);
3419 }
3420
3421 /* Return true if T is a primary function, class or alias template
3422 specialization, not including the template pattern. */
3423
3424 bool
3425 primary_template_specialization_p (const_tree t)
3426 {
3427 if (!t)
3428 return false;
3429
3430 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3431 return (DECL_LANG_SPECIFIC (t)
3432 && DECL_USE_TEMPLATE (t)
3433 && DECL_TEMPLATE_INFO (t)
3434 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3435 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3436 return (CLASSTYPE_TEMPLATE_INFO (t)
3437 && CLASSTYPE_USE_TEMPLATE (t)
3438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3439 else if (alias_template_specialization_p (t))
3440 return true;
3441 return false;
3442 }
3443
3444 /* Return true if PARM is a template template parameter. */
3445
3446 bool
3447 template_template_parameter_p (const_tree parm)
3448 {
3449 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3450 }
3451
3452 /* Return true iff PARM is a DECL representing a type template
3453 parameter. */
3454
3455 bool
3456 template_type_parameter_p (const_tree parm)
3457 {
3458 return (parm
3459 && (TREE_CODE (parm) == TYPE_DECL
3460 || TREE_CODE (parm) == TEMPLATE_DECL)
3461 && DECL_TEMPLATE_PARM_P (parm));
3462 }
3463
3464 /* Return the template parameters of T if T is a
3465 primary template instantiation, NULL otherwise. */
3466
3467 tree
3468 get_primary_template_innermost_parameters (const_tree t)
3469 {
3470 tree parms = NULL, template_info = NULL;
3471
3472 if ((template_info = get_template_info (t))
3473 && primary_template_specialization_p (t))
3474 parms = INNERMOST_TEMPLATE_PARMS
3475 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3476
3477 return parms;
3478 }
3479
3480 /* Return the template parameters of the LEVELth level from the full list
3481 of template parameters PARMS. */
3482
3483 tree
3484 get_template_parms_at_level (tree parms, int level)
3485 {
3486 tree p;
3487 if (!parms
3488 || TREE_CODE (parms) != TREE_LIST
3489 || level > TMPL_PARMS_DEPTH (parms))
3490 return NULL_TREE;
3491
3492 for (p = parms; p; p = TREE_CHAIN (p))
3493 if (TMPL_PARMS_DEPTH (p) == level)
3494 return p;
3495
3496 return NULL_TREE;
3497 }
3498
3499 /* Returns the template arguments of T if T is a template instantiation,
3500 NULL otherwise. */
3501
3502 tree
3503 get_template_innermost_arguments (const_tree t)
3504 {
3505 tree args = NULL, template_info = NULL;
3506
3507 if ((template_info = get_template_info (t))
3508 && TI_ARGS (template_info))
3509 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3510
3511 return args;
3512 }
3513
3514 /* Return the argument pack elements of T if T is a template argument pack,
3515 NULL otherwise. */
3516
3517 tree
3518 get_template_argument_pack_elems (const_tree t)
3519 {
3520 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3521 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3522 return NULL;
3523
3524 return ARGUMENT_PACK_ARGS (t);
3525 }
3526
3527 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3528 ARGUMENT_PACK_SELECT represents. */
3529
3530 static tree
3531 argument_pack_select_arg (tree t)
3532 {
3533 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3534 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3535
3536 /* If the selected argument is an expansion E, that most likely means we were
3537 called from gen_elem_of_pack_expansion_instantiation during the
3538 substituting of an argument pack (of which the Ith element is a pack
3539 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3540 In this case, the Ith element resulting from this substituting is going to
3541 be a pack expansion, which pattern is the pattern of E. Let's return the
3542 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3543 resulting pack expansion from it. */
3544 if (PACK_EXPANSION_P (arg))
3545 {
3546 /* Make sure we aren't throwing away arg info. */
3547 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3548 arg = PACK_EXPANSION_PATTERN (arg);
3549 }
3550
3551 return arg;
3552 }
3553
3554
3555 /* True iff FN is a function representing a built-in variadic parameter
3556 pack. */
3557
3558 bool
3559 builtin_pack_fn_p (tree fn)
3560 {
3561 if (!fn
3562 || TREE_CODE (fn) != FUNCTION_DECL
3563 || !DECL_IS_BUILTIN (fn))
3564 return false;
3565
3566 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3567 return true;
3568
3569 return false;
3570 }
3571
3572 /* True iff CALL is a call to a function representing a built-in variadic
3573 parameter pack. */
3574
3575 static bool
3576 builtin_pack_call_p (tree call)
3577 {
3578 if (TREE_CODE (call) != CALL_EXPR)
3579 return false;
3580 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3581 }
3582
3583 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3584
3585 static tree
3586 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3587 tree in_decl)
3588 {
3589 tree ohi = CALL_EXPR_ARG (call, 0);
3590 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3591 false/*fn*/, true/*int_cst*/);
3592
3593 if (value_dependent_expression_p (hi))
3594 {
3595 if (hi != ohi)
3596 {
3597 call = copy_node (call);
3598 CALL_EXPR_ARG (call, 0) = hi;
3599 }
3600 tree ex = make_pack_expansion (call, complain);
3601 tree vec = make_tree_vec (1);
3602 TREE_VEC_ELT (vec, 0) = ex;
3603 return vec;
3604 }
3605 else
3606 {
3607 hi = cxx_constant_value (hi);
3608 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3609
3610 /* Calculate the largest value of len that won't make the size of the vec
3611 overflow an int. The compiler will exceed resource limits long before
3612 this, but it seems a decent place to diagnose. */
3613 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3614
3615 if (len < 0 || len > max)
3616 {
3617 if ((complain & tf_error)
3618 && hi != error_mark_node)
3619 error ("argument to __integer_pack must be between 0 and %d", max);
3620 return error_mark_node;
3621 }
3622
3623 tree vec = make_tree_vec (len);
3624
3625 for (int i = 0; i < len; ++i)
3626 TREE_VEC_ELT (vec, i) = size_int (i);
3627
3628 return vec;
3629 }
3630 }
3631
3632 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3633 CALL. */
3634
3635 static tree
3636 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3637 tree in_decl)
3638 {
3639 if (!builtin_pack_call_p (call))
3640 return NULL_TREE;
3641
3642 tree fn = CALL_EXPR_FN (call);
3643
3644 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3645 return expand_integer_pack (call, args, complain, in_decl);
3646
3647 return NULL_TREE;
3648 }
3649
3650 /* Structure used to track the progress of find_parameter_packs_r. */
3651 struct find_parameter_pack_data
3652 {
3653 /* TREE_LIST that will contain all of the parameter packs found by
3654 the traversal. */
3655 tree* parameter_packs;
3656
3657 /* Set of AST nodes that have been visited by the traversal. */
3658 hash_set<tree> *visited;
3659
3660 /* True iff we're making a type pack expansion. */
3661 bool type_pack_expansion_p;
3662 };
3663
3664 /* Identifies all of the argument packs that occur in a template
3665 argument and appends them to the TREE_LIST inside DATA, which is a
3666 find_parameter_pack_data structure. This is a subroutine of
3667 make_pack_expansion and uses_parameter_packs. */
3668 static tree
3669 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3670 {
3671 tree t = *tp;
3672 struct find_parameter_pack_data* ppd =
3673 (struct find_parameter_pack_data*)data;
3674 bool parameter_pack_p = false;
3675
3676 /* Handle type aliases/typedefs. */
3677 if (TYPE_ALIAS_P (t))
3678 {
3679 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3680 cp_walk_tree (&TI_ARGS (tinfo),
3681 &find_parameter_packs_r,
3682 ppd, ppd->visited);
3683 *walk_subtrees = 0;
3684 return NULL_TREE;
3685 }
3686
3687 /* Identify whether this is a parameter pack or not. */
3688 switch (TREE_CODE (t))
3689 {
3690 case TEMPLATE_PARM_INDEX:
3691 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3692 parameter_pack_p = true;
3693 break;
3694
3695 case TEMPLATE_TYPE_PARM:
3696 t = TYPE_MAIN_VARIANT (t);
3697 /* FALLTHRU */
3698 case TEMPLATE_TEMPLATE_PARM:
3699 /* If the placeholder appears in the decl-specifier-seq of a function
3700 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3701 is a pack expansion, the invented template parameter is a template
3702 parameter pack. */
3703 if (ppd->type_pack_expansion_p && is_auto (t))
3704 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3705 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3706 parameter_pack_p = true;
3707 break;
3708
3709 case FIELD_DECL:
3710 case PARM_DECL:
3711 if (DECL_PACK_P (t))
3712 {
3713 /* We don't want to walk into the type of a PARM_DECL,
3714 because we don't want to see the type parameter pack. */
3715 *walk_subtrees = 0;
3716 parameter_pack_p = true;
3717 }
3718 break;
3719
3720 case VAR_DECL:
3721 if (DECL_PACK_P (t))
3722 {
3723 /* We don't want to walk into the type of a variadic capture proxy,
3724 because we don't want to see the type parameter pack. */
3725 *walk_subtrees = 0;
3726 parameter_pack_p = true;
3727 }
3728 else if (variable_template_specialization_p (t))
3729 {
3730 cp_walk_tree (&DECL_TI_ARGS (t),
3731 find_parameter_packs_r,
3732 ppd, ppd->visited);
3733 *walk_subtrees = 0;
3734 }
3735 break;
3736
3737 case CALL_EXPR:
3738 if (builtin_pack_call_p (t))
3739 parameter_pack_p = true;
3740 break;
3741
3742 case BASES:
3743 parameter_pack_p = true;
3744 break;
3745 default:
3746 /* Not a parameter pack. */
3747 break;
3748 }
3749
3750 if (parameter_pack_p)
3751 {
3752 /* Add this parameter pack to the list. */
3753 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3754 }
3755
3756 if (TYPE_P (t))
3757 cp_walk_tree (&TYPE_CONTEXT (t),
3758 &find_parameter_packs_r, ppd, ppd->visited);
3759
3760 /* This switch statement will return immediately if we don't find a
3761 parameter pack. */
3762 switch (TREE_CODE (t))
3763 {
3764 case TEMPLATE_PARM_INDEX:
3765 return NULL_TREE;
3766
3767 case BOUND_TEMPLATE_TEMPLATE_PARM:
3768 /* Check the template itself. */
3769 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3770 &find_parameter_packs_r, ppd, ppd->visited);
3771 /* Check the template arguments. */
3772 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3773 ppd->visited);
3774 *walk_subtrees = 0;
3775 return NULL_TREE;
3776
3777 case TEMPLATE_TYPE_PARM:
3778 case TEMPLATE_TEMPLATE_PARM:
3779 return NULL_TREE;
3780
3781 case PARM_DECL:
3782 return NULL_TREE;
3783
3784 case DECL_EXPR:
3785 /* Ignore the declaration of a capture proxy for a parameter pack. */
3786 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3787 *walk_subtrees = 0;
3788 return NULL_TREE;
3789
3790 case RECORD_TYPE:
3791 if (TYPE_PTRMEMFUNC_P (t))
3792 return NULL_TREE;
3793 /* Fall through. */
3794
3795 case UNION_TYPE:
3796 case ENUMERAL_TYPE:
3797 if (TYPE_TEMPLATE_INFO (t))
3798 cp_walk_tree (&TYPE_TI_ARGS (t),
3799 &find_parameter_packs_r, ppd, ppd->visited);
3800
3801 *walk_subtrees = 0;
3802 return NULL_TREE;
3803
3804 case TEMPLATE_DECL:
3805 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3806 return NULL_TREE;
3807 gcc_fallthrough();
3808
3809 case CONSTRUCTOR:
3810 cp_walk_tree (&TREE_TYPE (t),
3811 &find_parameter_packs_r, ppd, ppd->visited);
3812 return NULL_TREE;
3813
3814 case TYPENAME_TYPE:
3815 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3816 ppd, ppd->visited);
3817 *walk_subtrees = 0;
3818 return NULL_TREE;
3819
3820 case TYPE_PACK_EXPANSION:
3821 case EXPR_PACK_EXPANSION:
3822 *walk_subtrees = 0;
3823 return NULL_TREE;
3824
3825 case INTEGER_TYPE:
3826 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3827 ppd, ppd->visited);
3828 *walk_subtrees = 0;
3829 return NULL_TREE;
3830
3831 case IDENTIFIER_NODE:
3832 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3833 ppd->visited);
3834 *walk_subtrees = 0;
3835 return NULL_TREE;
3836
3837 case LAMBDA_EXPR:
3838 {
3839 /* Look at explicit captures. */
3840 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3841 cap; cap = TREE_CHAIN (cap))
3842 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3843 ppd->visited);
3844 /* Since we defer implicit capture, look in the parms and body. */
3845 tree fn = lambda_function (t);
3846 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3847 ppd->visited);
3848 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3849 ppd->visited);
3850 *walk_subtrees = 0;
3851 return NULL_TREE;
3852 }
3853
3854 case DECLTYPE_TYPE:
3855 {
3856 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3857 type_pack_expansion_p to false so that any placeholders
3858 within the expression don't get marked as parameter packs. */
3859 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3860 ppd->type_pack_expansion_p = false;
3861 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3862 ppd, ppd->visited);
3863 ppd->type_pack_expansion_p = type_pack_expansion_p;
3864 *walk_subtrees = 0;
3865 return NULL_TREE;
3866 }
3867
3868 default:
3869 return NULL_TREE;
3870 }
3871
3872 return NULL_TREE;
3873 }
3874
3875 /* Determines if the expression or type T uses any parameter packs. */
3876 bool
3877 uses_parameter_packs (tree t)
3878 {
3879 tree parameter_packs = NULL_TREE;
3880 struct find_parameter_pack_data ppd;
3881 ppd.parameter_packs = &parameter_packs;
3882 ppd.visited = new hash_set<tree>;
3883 ppd.type_pack_expansion_p = false;
3884 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3885 delete ppd.visited;
3886 return parameter_packs != NULL_TREE;
3887 }
3888
3889 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3890 representation a base-class initializer into a parameter pack
3891 expansion. If all goes well, the resulting node will be an
3892 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3893 respectively. */
3894 tree
3895 make_pack_expansion (tree arg, tsubst_flags_t complain)
3896 {
3897 tree result;
3898 tree parameter_packs = NULL_TREE;
3899 bool for_types = false;
3900 struct find_parameter_pack_data ppd;
3901
3902 if (!arg || arg == error_mark_node)
3903 return arg;
3904
3905 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3906 {
3907 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3908 class initializer. In this case, the TREE_PURPOSE will be a
3909 _TYPE node (representing the base class expansion we're
3910 initializing) and the TREE_VALUE will be a TREE_LIST
3911 containing the initialization arguments.
3912
3913 The resulting expansion looks somewhat different from most
3914 expansions. Rather than returning just one _EXPANSION, we
3915 return a TREE_LIST whose TREE_PURPOSE is a
3916 TYPE_PACK_EXPANSION containing the bases that will be
3917 initialized. The TREE_VALUE will be identical to the
3918 original TREE_VALUE, which is a list of arguments that will
3919 be passed to each base. We do not introduce any new pack
3920 expansion nodes into the TREE_VALUE (although it is possible
3921 that some already exist), because the TREE_PURPOSE and
3922 TREE_VALUE all need to be expanded together with the same
3923 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3924 resulting TREE_PURPOSE will mention the parameter packs in
3925 both the bases and the arguments to the bases. */
3926 tree purpose;
3927 tree value;
3928 tree parameter_packs = NULL_TREE;
3929
3930 /* Determine which parameter packs will be used by the base
3931 class expansion. */
3932 ppd.visited = new hash_set<tree>;
3933 ppd.parameter_packs = &parameter_packs;
3934 ppd.type_pack_expansion_p = true;
3935 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3936 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3937 &ppd, ppd.visited);
3938
3939 if (parameter_packs == NULL_TREE)
3940 {
3941 if (complain & tf_error)
3942 error ("base initializer expansion %qT contains no parameter packs",
3943 arg);
3944 delete ppd.visited;
3945 return error_mark_node;
3946 }
3947
3948 if (TREE_VALUE (arg) != void_type_node)
3949 {
3950 /* Collect the sets of parameter packs used in each of the
3951 initialization arguments. */
3952 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3953 {
3954 /* Determine which parameter packs will be expanded in this
3955 argument. */
3956 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3957 &ppd, ppd.visited);
3958 }
3959 }
3960
3961 delete ppd.visited;
3962
3963 /* Create the pack expansion type for the base type. */
3964 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3965 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3966 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3967 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3968
3969 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3970 they will rarely be compared to anything. */
3971 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3972
3973 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3974 }
3975
3976 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3977 for_types = true;
3978
3979 /* Build the PACK_EXPANSION_* node. */
3980 result = for_types
3981 ? cxx_make_type (TYPE_PACK_EXPANSION)
3982 : make_node (EXPR_PACK_EXPANSION);
3983 SET_PACK_EXPANSION_PATTERN (result, arg);
3984 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3985 {
3986 /* Propagate type and const-expression information. */
3987 TREE_TYPE (result) = TREE_TYPE (arg);
3988 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3989 /* Mark this read now, since the expansion might be length 0. */
3990 mark_exp_read (arg);
3991 }
3992 else
3993 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3994 they will rarely be compared to anything. */
3995 SET_TYPE_STRUCTURAL_EQUALITY (result);
3996
3997 /* Determine which parameter packs will be expanded. */
3998 ppd.parameter_packs = &parameter_packs;
3999 ppd.visited = new hash_set<tree>;
4000 ppd.type_pack_expansion_p = TYPE_P (arg);
4001 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4002 delete ppd.visited;
4003
4004 /* Make sure we found some parameter packs. */
4005 if (parameter_packs == NULL_TREE)
4006 {
4007 if (complain & tf_error)
4008 {
4009 if (TYPE_P (arg))
4010 error ("expansion pattern %qT contains no parameter packs", arg);
4011 else
4012 error ("expansion pattern %qE contains no parameter packs", arg);
4013 }
4014 return error_mark_node;
4015 }
4016 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4017
4018 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4019
4020 return result;
4021 }
4022
4023 /* Checks T for any "bare" parameter packs, which have not yet been
4024 expanded, and issues an error if any are found. This operation can
4025 only be done on full expressions or types (e.g., an expression
4026 statement, "if" condition, etc.), because we could have expressions like:
4027
4028 foo(f(g(h(args)))...)
4029
4030 where "args" is a parameter pack. check_for_bare_parameter_packs
4031 should not be called for the subexpressions args, h(args),
4032 g(h(args)), or f(g(h(args))), because we would produce erroneous
4033 error messages.
4034
4035 Returns TRUE and emits an error if there were bare parameter packs,
4036 returns FALSE otherwise. */
4037 bool
4038 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4039 {
4040 tree parameter_packs = NULL_TREE;
4041 struct find_parameter_pack_data ppd;
4042
4043 if (!processing_template_decl || !t || t == error_mark_node)
4044 return false;
4045
4046 /* A lambda might use a parameter pack from the containing context. */
4047 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4048 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4049 return false;
4050
4051 if (TREE_CODE (t) == TYPE_DECL)
4052 t = TREE_TYPE (t);
4053
4054 ppd.parameter_packs = &parameter_packs;
4055 ppd.visited = new hash_set<tree>;
4056 ppd.type_pack_expansion_p = false;
4057 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4058 delete ppd.visited;
4059
4060 if (parameter_packs)
4061 {
4062 if (loc == UNKNOWN_LOCATION)
4063 loc = cp_expr_loc_or_loc (t, input_location);
4064 error_at (loc, "parameter packs not expanded with %<...%>:");
4065 while (parameter_packs)
4066 {
4067 tree pack = TREE_VALUE (parameter_packs);
4068 tree name = NULL_TREE;
4069
4070 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4071 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4072 name = TYPE_NAME (pack);
4073 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4074 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4075 else if (TREE_CODE (pack) == CALL_EXPR)
4076 name = DECL_NAME (CALL_EXPR_FN (pack));
4077 else
4078 name = DECL_NAME (pack);
4079
4080 if (name)
4081 inform (loc, " %qD", name);
4082 else
4083 inform (loc, " <anonymous>");
4084
4085 parameter_packs = TREE_CHAIN (parameter_packs);
4086 }
4087
4088 return true;
4089 }
4090
4091 return false;
4092 }
4093
4094 /* Expand any parameter packs that occur in the template arguments in
4095 ARGS. */
4096 tree
4097 expand_template_argument_pack (tree args)
4098 {
4099 if (args == error_mark_node)
4100 return error_mark_node;
4101
4102 tree result_args = NULL_TREE;
4103 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4104 int num_result_args = -1;
4105 int non_default_args_count = -1;
4106
4107 /* First, determine if we need to expand anything, and the number of
4108 slots we'll need. */
4109 for (in_arg = 0; in_arg < nargs; ++in_arg)
4110 {
4111 tree arg = TREE_VEC_ELT (args, in_arg);
4112 if (arg == NULL_TREE)
4113 return args;
4114 if (ARGUMENT_PACK_P (arg))
4115 {
4116 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4117 if (num_result_args < 0)
4118 num_result_args = in_arg + num_packed;
4119 else
4120 num_result_args += num_packed;
4121 }
4122 else
4123 {
4124 if (num_result_args >= 0)
4125 num_result_args++;
4126 }
4127 }
4128
4129 /* If no expansion is necessary, we're done. */
4130 if (num_result_args < 0)
4131 return args;
4132
4133 /* Expand arguments. */
4134 result_args = make_tree_vec (num_result_args);
4135 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4136 non_default_args_count =
4137 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4138 for (in_arg = 0; in_arg < nargs; ++in_arg)
4139 {
4140 tree arg = TREE_VEC_ELT (args, in_arg);
4141 if (ARGUMENT_PACK_P (arg))
4142 {
4143 tree packed = ARGUMENT_PACK_ARGS (arg);
4144 int i, num_packed = TREE_VEC_LENGTH (packed);
4145 for (i = 0; i < num_packed; ++i, ++out_arg)
4146 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4147 if (non_default_args_count > 0)
4148 non_default_args_count += num_packed - 1;
4149 }
4150 else
4151 {
4152 TREE_VEC_ELT (result_args, out_arg) = arg;
4153 ++out_arg;
4154 }
4155 }
4156 if (non_default_args_count >= 0)
4157 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4158 return result_args;
4159 }
4160
4161 /* Checks if DECL shadows a template parameter.
4162
4163 [temp.local]: A template-parameter shall not be redeclared within its
4164 scope (including nested scopes).
4165
4166 Emits an error and returns TRUE if the DECL shadows a parameter,
4167 returns FALSE otherwise. */
4168
4169 bool
4170 check_template_shadow (tree decl)
4171 {
4172 tree olddecl;
4173
4174 /* If we're not in a template, we can't possibly shadow a template
4175 parameter. */
4176 if (!current_template_parms)
4177 return true;
4178
4179 /* Figure out what we're shadowing. */
4180 decl = OVL_FIRST (decl);
4181 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4182
4183 /* If there's no previous binding for this name, we're not shadowing
4184 anything, let alone a template parameter. */
4185 if (!olddecl)
4186 return true;
4187
4188 /* If we're not shadowing a template parameter, we're done. Note
4189 that OLDDECL might be an OVERLOAD (or perhaps even an
4190 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4191 node. */
4192 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4193 return true;
4194
4195 /* We check for decl != olddecl to avoid bogus errors for using a
4196 name inside a class. We check TPFI to avoid duplicate errors for
4197 inline member templates. */
4198 if (decl == olddecl
4199 || (DECL_TEMPLATE_PARM_P (decl)
4200 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4201 return true;
4202
4203 /* Don't complain about the injected class name, as we've already
4204 complained about the class itself. */
4205 if (DECL_SELF_REFERENCE_P (decl))
4206 return false;
4207
4208 if (DECL_TEMPLATE_PARM_P (decl))
4209 error ("declaration of template parameter %q+D shadows "
4210 "template parameter", decl);
4211 else
4212 error ("declaration of %q+#D shadows template parameter", decl);
4213 inform (DECL_SOURCE_LOCATION (olddecl),
4214 "template parameter %qD declared here", olddecl);
4215 return false;
4216 }
4217
4218 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4219 ORIG_LEVEL, DECL, and TYPE. */
4220
4221 static tree
4222 build_template_parm_index (int index,
4223 int level,
4224 int orig_level,
4225 tree decl,
4226 tree type)
4227 {
4228 tree t = make_node (TEMPLATE_PARM_INDEX);
4229 TEMPLATE_PARM_IDX (t) = index;
4230 TEMPLATE_PARM_LEVEL (t) = level;
4231 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4232 TEMPLATE_PARM_DECL (t) = decl;
4233 TREE_TYPE (t) = type;
4234 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4235 TREE_READONLY (t) = TREE_READONLY (decl);
4236
4237 return t;
4238 }
4239
4240 /* Find the canonical type parameter for the given template type
4241 parameter. Returns the canonical type parameter, which may be TYPE
4242 if no such parameter existed. */
4243
4244 static tree
4245 canonical_type_parameter (tree type)
4246 {
4247 tree list;
4248 int idx = TEMPLATE_TYPE_IDX (type);
4249 if (!canonical_template_parms)
4250 vec_alloc (canonical_template_parms, idx + 1);
4251
4252 if (canonical_template_parms->length () <= (unsigned) idx)
4253 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4254
4255 list = (*canonical_template_parms)[idx];
4256 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4257 list = TREE_CHAIN (list);
4258
4259 if (list)
4260 return TREE_VALUE (list);
4261 else
4262 {
4263 (*canonical_template_parms)[idx]
4264 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4265 return type;
4266 }
4267 }
4268
4269 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4270 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4271 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4272 new one is created. */
4273
4274 static tree
4275 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4276 tsubst_flags_t complain)
4277 {
4278 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4279 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4280 != TEMPLATE_PARM_LEVEL (index) - levels)
4281 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4282 {
4283 tree orig_decl = TEMPLATE_PARM_DECL (index);
4284 tree decl, t;
4285
4286 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4287 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4288 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4289 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4290 DECL_ARTIFICIAL (decl) = 1;
4291 SET_DECL_TEMPLATE_PARM_P (decl);
4292
4293 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4294 TEMPLATE_PARM_LEVEL (index) - levels,
4295 TEMPLATE_PARM_ORIG_LEVEL (index),
4296 decl, type);
4297 TEMPLATE_PARM_DESCENDANTS (index) = t;
4298 TEMPLATE_PARM_PARAMETER_PACK (t)
4299 = TEMPLATE_PARM_PARAMETER_PACK (index);
4300
4301 /* Template template parameters need this. */
4302 if (TREE_CODE (decl) == TEMPLATE_DECL)
4303 {
4304 DECL_TEMPLATE_RESULT (decl)
4305 = build_decl (DECL_SOURCE_LOCATION (decl),
4306 TYPE_DECL, DECL_NAME (decl), type);
4307 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4308 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4309 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4310 }
4311 }
4312
4313 return TEMPLATE_PARM_DESCENDANTS (index);
4314 }
4315
4316 /* Process information from new template parameter PARM and append it
4317 to the LIST being built. This new parameter is a non-type
4318 parameter iff IS_NON_TYPE is true. This new parameter is a
4319 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4320 is in PARM_LOC. */
4321
4322 tree
4323 process_template_parm (tree list, location_t parm_loc, tree parm,
4324 bool is_non_type, bool is_parameter_pack)
4325 {
4326 tree decl = 0;
4327 int idx = 0;
4328
4329 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4330 tree defval = TREE_PURPOSE (parm);
4331 tree constr = TREE_TYPE (parm);
4332
4333 if (list)
4334 {
4335 tree p = tree_last (list);
4336
4337 if (p && TREE_VALUE (p) != error_mark_node)
4338 {
4339 p = TREE_VALUE (p);
4340 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4341 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4342 else
4343 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4344 }
4345
4346 ++idx;
4347 }
4348
4349 if (is_non_type)
4350 {
4351 parm = TREE_VALUE (parm);
4352
4353 SET_DECL_TEMPLATE_PARM_P (parm);
4354
4355 if (TREE_TYPE (parm) != error_mark_node)
4356 {
4357 /* [temp.param]
4358
4359 The top-level cv-qualifiers on the template-parameter are
4360 ignored when determining its type. */
4361 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4362 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4363 TREE_TYPE (parm) = error_mark_node;
4364 else if (uses_parameter_packs (TREE_TYPE (parm))
4365 && !is_parameter_pack
4366 /* If we're in a nested template parameter list, the template
4367 template parameter could be a parameter pack. */
4368 && processing_template_parmlist == 1)
4369 {
4370 /* This template parameter is not a parameter pack, but it
4371 should be. Complain about "bare" parameter packs. */
4372 check_for_bare_parameter_packs (TREE_TYPE (parm));
4373
4374 /* Recover by calling this a parameter pack. */
4375 is_parameter_pack = true;
4376 }
4377 }
4378
4379 /* A template parameter is not modifiable. */
4380 TREE_CONSTANT (parm) = 1;
4381 TREE_READONLY (parm) = 1;
4382 decl = build_decl (parm_loc,
4383 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4384 TREE_CONSTANT (decl) = 1;
4385 TREE_READONLY (decl) = 1;
4386 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4387 = build_template_parm_index (idx, processing_template_decl,
4388 processing_template_decl,
4389 decl, TREE_TYPE (parm));
4390
4391 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4392 = is_parameter_pack;
4393 }
4394 else
4395 {
4396 tree t;
4397 parm = TREE_VALUE (TREE_VALUE (parm));
4398
4399 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4400 {
4401 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4402 /* This is for distinguishing between real templates and template
4403 template parameters */
4404 TREE_TYPE (parm) = t;
4405 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4406 decl = parm;
4407 }
4408 else
4409 {
4410 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4411 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4412 decl = build_decl (parm_loc,
4413 TYPE_DECL, parm, t);
4414 }
4415
4416 TYPE_NAME (t) = decl;
4417 TYPE_STUB_DECL (t) = decl;
4418 parm = decl;
4419 TEMPLATE_TYPE_PARM_INDEX (t)
4420 = build_template_parm_index (idx, processing_template_decl,
4421 processing_template_decl,
4422 decl, TREE_TYPE (parm));
4423 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4424 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4425 }
4426 DECL_ARTIFICIAL (decl) = 1;
4427 SET_DECL_TEMPLATE_PARM_P (decl);
4428
4429 /* Build requirements for the type/template parameter.
4430 This must be done after SET_DECL_TEMPLATE_PARM_P or
4431 process_template_parm could fail. */
4432 tree reqs = finish_shorthand_constraint (parm, constr);
4433
4434 pushdecl (decl);
4435
4436 /* Build the parameter node linking the parameter declaration,
4437 its default argument (if any), and its constraints (if any). */
4438 parm = build_tree_list (defval, parm);
4439 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4440
4441 return chainon (list, parm);
4442 }
4443
4444 /* The end of a template parameter list has been reached. Process the
4445 tree list into a parameter vector, converting each parameter into a more
4446 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4447 as PARM_DECLs. */
4448
4449 tree
4450 end_template_parm_list (tree parms)
4451 {
4452 int nparms;
4453 tree parm, next;
4454 tree saved_parmlist = make_tree_vec (list_length (parms));
4455
4456 /* Pop the dummy parameter level and add the real one. */
4457 current_template_parms = TREE_CHAIN (current_template_parms);
4458
4459 current_template_parms
4460 = tree_cons (size_int (processing_template_decl),
4461 saved_parmlist, current_template_parms);
4462
4463 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4464 {
4465 next = TREE_CHAIN (parm);
4466 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4467 TREE_CHAIN (parm) = NULL_TREE;
4468 }
4469
4470 --processing_template_parmlist;
4471
4472 return saved_parmlist;
4473 }
4474
4475 // Explicitly indicate the end of the template parameter list. We assume
4476 // that the current template parameters have been constructed and/or
4477 // managed explicitly, as when creating new template template parameters
4478 // from a shorthand constraint.
4479 void
4480 end_template_parm_list ()
4481 {
4482 --processing_template_parmlist;
4483 }
4484
4485 /* end_template_decl is called after a template declaration is seen. */
4486
4487 void
4488 end_template_decl (void)
4489 {
4490 reset_specialization ();
4491
4492 if (! processing_template_decl)
4493 return;
4494
4495 /* This matches the pushlevel in begin_template_parm_list. */
4496 finish_scope ();
4497
4498 --processing_template_decl;
4499 current_template_parms = TREE_CHAIN (current_template_parms);
4500 }
4501
4502 /* Takes a TREE_LIST representing a template parameter and convert it
4503 into an argument suitable to be passed to the type substitution
4504 functions. Note that If the TREE_LIST contains an error_mark
4505 node, the returned argument is error_mark_node. */
4506
4507 tree
4508 template_parm_to_arg (tree t)
4509 {
4510
4511 if (t == NULL_TREE
4512 || TREE_CODE (t) != TREE_LIST)
4513 return t;
4514
4515 if (error_operand_p (TREE_VALUE (t)))
4516 return error_mark_node;
4517
4518 t = TREE_VALUE (t);
4519
4520 if (TREE_CODE (t) == TYPE_DECL
4521 || TREE_CODE (t) == TEMPLATE_DECL)
4522 {
4523 t = TREE_TYPE (t);
4524
4525 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4526 {
4527 /* Turn this argument into a TYPE_ARGUMENT_PACK
4528 with a single element, which expands T. */
4529 tree vec = make_tree_vec (1);
4530 if (CHECKING_P)
4531 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4532
4533 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4534
4535 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4536 SET_ARGUMENT_PACK_ARGS (t, vec);
4537 }
4538 }
4539 else
4540 {
4541 t = DECL_INITIAL (t);
4542
4543 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4544 {
4545 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4546 with a single element, which expands T. */
4547 tree vec = make_tree_vec (1);
4548 if (CHECKING_P)
4549 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4550
4551 t = convert_from_reference (t);
4552 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4553
4554 t = make_node (NONTYPE_ARGUMENT_PACK);
4555 SET_ARGUMENT_PACK_ARGS (t, vec);
4556 }
4557 else
4558 t = convert_from_reference (t);
4559 }
4560 return t;
4561 }
4562
4563 /* Given a single level of template parameters (a TREE_VEC), return it
4564 as a set of template arguments. */
4565
4566 static tree
4567 template_parms_level_to_args (tree parms)
4568 {
4569 tree a = copy_node (parms);
4570 TREE_TYPE (a) = NULL_TREE;
4571 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4572 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4573
4574 if (CHECKING_P)
4575 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4576
4577 return a;
4578 }
4579
4580 /* Given a set of template parameters, return them as a set of template
4581 arguments. The template parameters are represented as a TREE_VEC, in
4582 the form documented in cp-tree.h for template arguments. */
4583
4584 static tree
4585 template_parms_to_args (tree parms)
4586 {
4587 tree header;
4588 tree args = NULL_TREE;
4589 int length = TMPL_PARMS_DEPTH (parms);
4590 int l = length;
4591
4592 /* If there is only one level of template parameters, we do not
4593 create a TREE_VEC of TREE_VECs. Instead, we return a single
4594 TREE_VEC containing the arguments. */
4595 if (length > 1)
4596 args = make_tree_vec (length);
4597
4598 for (header = parms; header; header = TREE_CHAIN (header))
4599 {
4600 tree a = template_parms_level_to_args (TREE_VALUE (header));
4601
4602 if (length > 1)
4603 TREE_VEC_ELT (args, --l) = a;
4604 else
4605 args = a;
4606 }
4607
4608 return args;
4609 }
4610
4611 /* Within the declaration of a template, return the currently active
4612 template parameters as an argument TREE_VEC. */
4613
4614 static tree
4615 current_template_args (void)
4616 {
4617 return template_parms_to_args (current_template_parms);
4618 }
4619
4620 /* Update the declared TYPE by doing any lookups which were thought to be
4621 dependent, but are not now that we know the SCOPE of the declarator. */
4622
4623 tree
4624 maybe_update_decl_type (tree orig_type, tree scope)
4625 {
4626 tree type = orig_type;
4627
4628 if (type == NULL_TREE)
4629 return type;
4630
4631 if (TREE_CODE (orig_type) == TYPE_DECL)
4632 type = TREE_TYPE (type);
4633
4634 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4635 && dependent_type_p (type)
4636 /* Don't bother building up the args in this case. */
4637 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4638 {
4639 /* tsubst in the args corresponding to the template parameters,
4640 including auto if present. Most things will be unchanged, but
4641 make_typename_type and tsubst_qualified_id will resolve
4642 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4643 tree args = current_template_args ();
4644 tree auto_node = type_uses_auto (type);
4645 tree pushed;
4646 if (auto_node)
4647 {
4648 tree auto_vec = make_tree_vec (1);
4649 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4650 args = add_to_template_args (args, auto_vec);
4651 }
4652 pushed = push_scope (scope);
4653 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4654 if (pushed)
4655 pop_scope (scope);
4656 }
4657
4658 if (type == error_mark_node)
4659 return orig_type;
4660
4661 if (TREE_CODE (orig_type) == TYPE_DECL)
4662 {
4663 if (same_type_p (type, TREE_TYPE (orig_type)))
4664 type = orig_type;
4665 else
4666 type = TYPE_NAME (type);
4667 }
4668 return type;
4669 }
4670
4671 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4672 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4673 the new template is a member template. */
4674
4675 static tree
4676 build_template_decl (tree decl, tree parms, bool member_template_p)
4677 {
4678 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4679 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4680 DECL_TEMPLATE_PARMS (tmpl) = parms;
4681 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4682 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4683 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4684
4685 return tmpl;
4686 }
4687
4688 struct template_parm_data
4689 {
4690 /* The level of the template parameters we are currently
4691 processing. */
4692 int level;
4693
4694 /* The index of the specialization argument we are currently
4695 processing. */
4696 int current_arg;
4697
4698 /* An array whose size is the number of template parameters. The
4699 elements are nonzero if the parameter has been used in any one
4700 of the arguments processed so far. */
4701 int* parms;
4702
4703 /* An array whose size is the number of template arguments. The
4704 elements are nonzero if the argument makes use of template
4705 parameters of this level. */
4706 int* arg_uses_template_parms;
4707 };
4708
4709 /* Subroutine of push_template_decl used to see if each template
4710 parameter in a partial specialization is used in the explicit
4711 argument list. If T is of the LEVEL given in DATA (which is
4712 treated as a template_parm_data*), then DATA->PARMS is marked
4713 appropriately. */
4714
4715 static int
4716 mark_template_parm (tree t, void* data)
4717 {
4718 int level;
4719 int idx;
4720 struct template_parm_data* tpd = (struct template_parm_data*) data;
4721
4722 template_parm_level_and_index (t, &level, &idx);
4723
4724 if (level == tpd->level)
4725 {
4726 tpd->parms[idx] = 1;
4727 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4728 }
4729
4730 /* In C++17 the type of a non-type argument is a deduced context. */
4731 if (cxx_dialect >= cxx17
4732 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4733 for_each_template_parm (TREE_TYPE (t),
4734 &mark_template_parm,
4735 data,
4736 NULL,
4737 /*include_nondeduced_p=*/false);
4738
4739 /* Return zero so that for_each_template_parm will continue the
4740 traversal of the tree; we want to mark *every* template parm. */
4741 return 0;
4742 }
4743
4744 /* Process the partial specialization DECL. */
4745
4746 static tree
4747 process_partial_specialization (tree decl)
4748 {
4749 tree type = TREE_TYPE (decl);
4750 tree tinfo = get_template_info (decl);
4751 tree maintmpl = TI_TEMPLATE (tinfo);
4752 tree specargs = TI_ARGS (tinfo);
4753 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4754 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4755 tree inner_parms;
4756 tree inst;
4757 int nargs = TREE_VEC_LENGTH (inner_args);
4758 int ntparms;
4759 int i;
4760 bool did_error_intro = false;
4761 struct template_parm_data tpd;
4762 struct template_parm_data tpd2;
4763
4764 gcc_assert (current_template_parms);
4765
4766 /* A concept cannot be specialized. */
4767 if (flag_concepts && variable_concept_p (maintmpl))
4768 {
4769 error ("specialization of variable concept %q#D", maintmpl);
4770 return error_mark_node;
4771 }
4772
4773 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4774 ntparms = TREE_VEC_LENGTH (inner_parms);
4775
4776 /* We check that each of the template parameters given in the
4777 partial specialization is used in the argument list to the
4778 specialization. For example:
4779
4780 template <class T> struct S;
4781 template <class T> struct S<T*>;
4782
4783 The second declaration is OK because `T*' uses the template
4784 parameter T, whereas
4785
4786 template <class T> struct S<int>;
4787
4788 is no good. Even trickier is:
4789
4790 template <class T>
4791 struct S1
4792 {
4793 template <class U>
4794 struct S2;
4795 template <class U>
4796 struct S2<T>;
4797 };
4798
4799 The S2<T> declaration is actually invalid; it is a
4800 full-specialization. Of course,
4801
4802 template <class U>
4803 struct S2<T (*)(U)>;
4804
4805 or some such would have been OK. */
4806 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4807 tpd.parms = XALLOCAVEC (int, ntparms);
4808 memset (tpd.parms, 0, sizeof (int) * ntparms);
4809
4810 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4811 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4812 for (i = 0; i < nargs; ++i)
4813 {
4814 tpd.current_arg = i;
4815 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4816 &mark_template_parm,
4817 &tpd,
4818 NULL,
4819 /*include_nondeduced_p=*/false);
4820 }
4821 for (i = 0; i < ntparms; ++i)
4822 if (tpd.parms[i] == 0)
4823 {
4824 /* One of the template parms was not used in a deduced context in the
4825 specialization. */
4826 if (!did_error_intro)
4827 {
4828 error ("template parameters not deducible in "
4829 "partial specialization:");
4830 did_error_intro = true;
4831 }
4832
4833 inform (input_location, " %qD",
4834 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4835 }
4836
4837 if (did_error_intro)
4838 return error_mark_node;
4839
4840 /* [temp.class.spec]
4841
4842 The argument list of the specialization shall not be identical to
4843 the implicit argument list of the primary template. */
4844 tree main_args
4845 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4846 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4847 && (!flag_concepts
4848 || !strictly_subsumes (current_template_constraints (),
4849 get_constraints (maintmpl))))
4850 {
4851 if (!flag_concepts)
4852 error ("partial specialization %q+D does not specialize "
4853 "any template arguments; to define the primary template, "
4854 "remove the template argument list", decl);
4855 else
4856 error ("partial specialization %q+D does not specialize any "
4857 "template arguments and is not more constrained than "
4858 "the primary template; to define the primary template, "
4859 "remove the template argument list", decl);
4860 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4861 }
4862
4863 /* A partial specialization that replaces multiple parameters of the
4864 primary template with a pack expansion is less specialized for those
4865 parameters. */
4866 if (nargs < DECL_NTPARMS (maintmpl))
4867 {
4868 error ("partial specialization is not more specialized than the "
4869 "primary template because it replaces multiple parameters "
4870 "with a pack expansion");
4871 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4872 /* Avoid crash in process_partial_specialization. */
4873 return decl;
4874 }
4875
4876 /* If we aren't in a dependent class, we can actually try deduction. */
4877 else if (tpd.level == 1
4878 /* FIXME we should be able to handle a partial specialization of a
4879 partial instantiation, but currently we can't (c++/41727). */
4880 && TMPL_ARGS_DEPTH (specargs) == 1
4881 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4882 {
4883 if (permerror (input_location, "partial specialization %qD is not "
4884 "more specialized than", decl))
4885 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4886 maintmpl);
4887 }
4888
4889 /* [temp.class.spec]
4890
4891 A partially specialized non-type argument expression shall not
4892 involve template parameters of the partial specialization except
4893 when the argument expression is a simple identifier.
4894
4895 The type of a template parameter corresponding to a specialized
4896 non-type argument shall not be dependent on a parameter of the
4897 specialization.
4898
4899 Also, we verify that pack expansions only occur at the
4900 end of the argument list. */
4901 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4902 tpd2.parms = 0;
4903 for (i = 0; i < nargs; ++i)
4904 {
4905 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4906 tree arg = TREE_VEC_ELT (inner_args, i);
4907 tree packed_args = NULL_TREE;
4908 int j, len = 1;
4909
4910 if (ARGUMENT_PACK_P (arg))
4911 {
4912 /* Extract the arguments from the argument pack. We'll be
4913 iterating over these in the following loop. */
4914 packed_args = ARGUMENT_PACK_ARGS (arg);
4915 len = TREE_VEC_LENGTH (packed_args);
4916 }
4917
4918 for (j = 0; j < len; j++)
4919 {
4920 if (packed_args)
4921 /* Get the Jth argument in the parameter pack. */
4922 arg = TREE_VEC_ELT (packed_args, j);
4923
4924 if (PACK_EXPANSION_P (arg))
4925 {
4926 /* Pack expansions must come at the end of the
4927 argument list. */
4928 if ((packed_args && j < len - 1)
4929 || (!packed_args && i < nargs - 1))
4930 {
4931 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4932 error ("parameter pack argument %qE must be at the "
4933 "end of the template argument list", arg);
4934 else
4935 error ("parameter pack argument %qT must be at the "
4936 "end of the template argument list", arg);
4937 }
4938 }
4939
4940 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4941 /* We only care about the pattern. */
4942 arg = PACK_EXPANSION_PATTERN (arg);
4943
4944 if (/* These first two lines are the `non-type' bit. */
4945 !TYPE_P (arg)
4946 && TREE_CODE (arg) != TEMPLATE_DECL
4947 /* This next two lines are the `argument expression is not just a
4948 simple identifier' condition and also the `specialized
4949 non-type argument' bit. */
4950 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4951 && !(REFERENCE_REF_P (arg)
4952 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4953 {
4954 if ((!packed_args && tpd.arg_uses_template_parms[i])
4955 || (packed_args && uses_template_parms (arg)))
4956 error ("template argument %qE involves template parameter(s)",
4957 arg);
4958 else
4959 {
4960 /* Look at the corresponding template parameter,
4961 marking which template parameters its type depends
4962 upon. */
4963 tree type = TREE_TYPE (parm);
4964
4965 if (!tpd2.parms)
4966 {
4967 /* We haven't yet initialized TPD2. Do so now. */
4968 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4969 /* The number of parameters here is the number in the
4970 main template, which, as checked in the assertion
4971 above, is NARGS. */
4972 tpd2.parms = XALLOCAVEC (int, nargs);
4973 tpd2.level =
4974 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4975 }
4976
4977 /* Mark the template parameters. But this time, we're
4978 looking for the template parameters of the main
4979 template, not in the specialization. */
4980 tpd2.current_arg = i;
4981 tpd2.arg_uses_template_parms[i] = 0;
4982 memset (tpd2.parms, 0, sizeof (int) * nargs);
4983 for_each_template_parm (type,
4984 &mark_template_parm,
4985 &tpd2,
4986 NULL,
4987 /*include_nondeduced_p=*/false);
4988
4989 if (tpd2.arg_uses_template_parms [i])
4990 {
4991 /* The type depended on some template parameters.
4992 If they are fully specialized in the
4993 specialization, that's OK. */
4994 int j;
4995 int count = 0;
4996 for (j = 0; j < nargs; ++j)
4997 if (tpd2.parms[j] != 0
4998 && tpd.arg_uses_template_parms [j])
4999 ++count;
5000 if (count != 0)
5001 error_n (input_location, count,
5002 "type %qT of template argument %qE depends "
5003 "on a template parameter",
5004 "type %qT of template argument %qE depends "
5005 "on template parameters",
5006 type,
5007 arg);
5008 }
5009 }
5010 }
5011 }
5012 }
5013
5014 /* We should only get here once. */
5015 if (TREE_CODE (decl) == TYPE_DECL)
5016 gcc_assert (!COMPLETE_TYPE_P (type));
5017
5018 // Build the template decl.
5019 tree tmpl = build_template_decl (decl, current_template_parms,
5020 DECL_MEMBER_TEMPLATE_P (maintmpl));
5021 TREE_TYPE (tmpl) = type;
5022 DECL_TEMPLATE_RESULT (tmpl) = decl;
5023 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5024 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5025 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5026
5027 /* Give template template parms a DECL_CONTEXT of the template
5028 for which they are a parameter. */
5029 for (i = 0; i < ntparms; ++i)
5030 {
5031 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5032 if (TREE_CODE (parm) == TEMPLATE_DECL)
5033 DECL_CONTEXT (parm) = tmpl;
5034 }
5035
5036 if (VAR_P (decl))
5037 /* We didn't register this in check_explicit_specialization so we could
5038 wait until the constraints were set. */
5039 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5040 else
5041 associate_classtype_constraints (type);
5042
5043 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5044 = tree_cons (specargs, tmpl,
5045 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5046 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5047
5048 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5049 inst = TREE_CHAIN (inst))
5050 {
5051 tree instance = TREE_VALUE (inst);
5052 if (TYPE_P (instance)
5053 ? (COMPLETE_TYPE_P (instance)
5054 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5055 : DECL_TEMPLATE_INSTANTIATION (instance))
5056 {
5057 tree spec = most_specialized_partial_spec (instance, tf_none);
5058 tree inst_decl = (DECL_P (instance)
5059 ? instance : TYPE_NAME (instance));
5060 if (!spec)
5061 /* OK */;
5062 else if (spec == error_mark_node)
5063 permerror (input_location,
5064 "declaration of %qD ambiguates earlier template "
5065 "instantiation for %qD", decl, inst_decl);
5066 else if (TREE_VALUE (spec) == tmpl)
5067 permerror (input_location,
5068 "partial specialization of %qD after instantiation "
5069 "of %qD", decl, inst_decl);
5070 }
5071 }
5072
5073 return decl;
5074 }
5075
5076 /* PARM is a template parameter of some form; return the corresponding
5077 TEMPLATE_PARM_INDEX. */
5078
5079 static tree
5080 get_template_parm_index (tree parm)
5081 {
5082 if (TREE_CODE (parm) == PARM_DECL
5083 || TREE_CODE (parm) == CONST_DECL)
5084 parm = DECL_INITIAL (parm);
5085 else if (TREE_CODE (parm) == TYPE_DECL
5086 || TREE_CODE (parm) == TEMPLATE_DECL)
5087 parm = TREE_TYPE (parm);
5088 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5089 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5090 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5091 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5092 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5093 return parm;
5094 }
5095
5096 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5097 parameter packs used by the template parameter PARM. */
5098
5099 static void
5100 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5101 {
5102 /* A type parm can't refer to another parm. */
5103 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5104 return;
5105 else if (TREE_CODE (parm) == PARM_DECL)
5106 {
5107 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5108 ppd, ppd->visited);
5109 return;
5110 }
5111
5112 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5113
5114 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5115 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5116 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5117 }
5118
5119 /* PARM is a template parameter pack. Return any parameter packs used in
5120 its type or the type of any of its template parameters. If there are
5121 any such packs, it will be instantiated into a fixed template parameter
5122 list by partial instantiation rather than be fully deduced. */
5123
5124 tree
5125 fixed_parameter_pack_p (tree parm)
5126 {
5127 /* This can only be true in a member template. */
5128 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5129 return NULL_TREE;
5130 /* This can only be true for a parameter pack. */
5131 if (!template_parameter_pack_p (parm))
5132 return NULL_TREE;
5133 /* A type parm can't refer to another parm. */
5134 if (TREE_CODE (parm) == TYPE_DECL)
5135 return NULL_TREE;
5136
5137 tree parameter_packs = NULL_TREE;
5138 struct find_parameter_pack_data ppd;
5139 ppd.parameter_packs = &parameter_packs;
5140 ppd.visited = new hash_set<tree>;
5141 ppd.type_pack_expansion_p = false;
5142
5143 fixed_parameter_pack_p_1 (parm, &ppd);
5144
5145 delete ppd.visited;
5146 return parameter_packs;
5147 }
5148
5149 /* Check that a template declaration's use of default arguments and
5150 parameter packs is not invalid. Here, PARMS are the template
5151 parameters. IS_PRIMARY is true if DECL is the thing declared by
5152 a primary template. IS_PARTIAL is true if DECL is a partial
5153 specialization.
5154
5155 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5156 function template declaration or a friend class template
5157 declaration. In the function case, 1 indicates a declaration, 2
5158 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5159 emitted for extraneous default arguments.
5160
5161 Returns TRUE if there were no errors found, FALSE otherwise. */
5162
5163 bool
5164 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5165 bool is_partial, int is_friend_decl)
5166 {
5167 const char *msg;
5168 int last_level_to_check;
5169 tree parm_level;
5170 bool no_errors = true;
5171
5172 /* [temp.param]
5173
5174 A default template-argument shall not be specified in a
5175 function template declaration or a function template definition, nor
5176 in the template-parameter-list of the definition of a member of a
5177 class template. */
5178
5179 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5180 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5181 /* You can't have a function template declaration in a local
5182 scope, nor you can you define a member of a class template in a
5183 local scope. */
5184 return true;
5185
5186 if ((TREE_CODE (decl) == TYPE_DECL
5187 && TREE_TYPE (decl)
5188 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5189 || (TREE_CODE (decl) == FUNCTION_DECL
5190 && LAMBDA_FUNCTION_P (decl)))
5191 /* A lambda doesn't have an explicit declaration; don't complain
5192 about the parms of the enclosing class. */
5193 return true;
5194
5195 if (current_class_type
5196 && !TYPE_BEING_DEFINED (current_class_type)
5197 && DECL_LANG_SPECIFIC (decl)
5198 && DECL_DECLARES_FUNCTION_P (decl)
5199 /* If this is either a friend defined in the scope of the class
5200 or a member function. */
5201 && (DECL_FUNCTION_MEMBER_P (decl)
5202 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5203 : DECL_FRIEND_CONTEXT (decl)
5204 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5205 : false)
5206 /* And, if it was a member function, it really was defined in
5207 the scope of the class. */
5208 && (!DECL_FUNCTION_MEMBER_P (decl)
5209 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5210 /* We already checked these parameters when the template was
5211 declared, so there's no need to do it again now. This function
5212 was defined in class scope, but we're processing its body now
5213 that the class is complete. */
5214 return true;
5215
5216 /* Core issue 226 (C++0x only): the following only applies to class
5217 templates. */
5218 if (is_primary
5219 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5220 {
5221 /* [temp.param]
5222
5223 If a template-parameter has a default template-argument, all
5224 subsequent template-parameters shall have a default
5225 template-argument supplied. */
5226 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5227 {
5228 tree inner_parms = TREE_VALUE (parm_level);
5229 int ntparms = TREE_VEC_LENGTH (inner_parms);
5230 int seen_def_arg_p = 0;
5231 int i;
5232
5233 for (i = 0; i < ntparms; ++i)
5234 {
5235 tree parm = TREE_VEC_ELT (inner_parms, i);
5236
5237 if (parm == error_mark_node)
5238 continue;
5239
5240 if (TREE_PURPOSE (parm))
5241 seen_def_arg_p = 1;
5242 else if (seen_def_arg_p
5243 && !template_parameter_pack_p (TREE_VALUE (parm)))
5244 {
5245 error ("no default argument for %qD", TREE_VALUE (parm));
5246 /* For better subsequent error-recovery, we indicate that
5247 there should have been a default argument. */
5248 TREE_PURPOSE (parm) = error_mark_node;
5249 no_errors = false;
5250 }
5251 else if (!is_partial
5252 && !is_friend_decl
5253 /* Don't complain about an enclosing partial
5254 specialization. */
5255 && parm_level == parms
5256 && TREE_CODE (decl) == TYPE_DECL
5257 && i < ntparms - 1
5258 && template_parameter_pack_p (TREE_VALUE (parm))
5259 /* A fixed parameter pack will be partially
5260 instantiated into a fixed length list. */
5261 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5262 {
5263 /* A primary class template can only have one
5264 parameter pack, at the end of the template
5265 parameter list. */
5266
5267 error ("parameter pack %q+D must be at the end of the"
5268 " template parameter list", TREE_VALUE (parm));
5269
5270 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5271 = error_mark_node;
5272 no_errors = false;
5273 }
5274 }
5275 }
5276 }
5277
5278 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5279 || is_partial
5280 || !is_primary
5281 || is_friend_decl)
5282 /* For an ordinary class template, default template arguments are
5283 allowed at the innermost level, e.g.:
5284 template <class T = int>
5285 struct S {};
5286 but, in a partial specialization, they're not allowed even
5287 there, as we have in [temp.class.spec]:
5288
5289 The template parameter list of a specialization shall not
5290 contain default template argument values.
5291
5292 So, for a partial specialization, or for a function template
5293 (in C++98/C++03), we look at all of them. */
5294 ;
5295 else
5296 /* But, for a primary class template that is not a partial
5297 specialization we look at all template parameters except the
5298 innermost ones. */
5299 parms = TREE_CHAIN (parms);
5300
5301 /* Figure out what error message to issue. */
5302 if (is_friend_decl == 2)
5303 msg = G_("default template arguments may not be used in function template "
5304 "friend re-declaration");
5305 else if (is_friend_decl)
5306 msg = G_("default template arguments may not be used in template "
5307 "friend declarations");
5308 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5309 msg = G_("default template arguments may not be used in function templates "
5310 "without -std=c++11 or -std=gnu++11");
5311 else if (is_partial)
5312 msg = G_("default template arguments may not be used in "
5313 "partial specializations");
5314 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5315 msg = G_("default argument for template parameter for class enclosing %qD");
5316 else
5317 /* Per [temp.param]/9, "A default template-argument shall not be
5318 specified in the template-parameter-lists of the definition of
5319 a member of a class template that appears outside of the member's
5320 class.", thus if we aren't handling a member of a class template
5321 there is no need to examine the parameters. */
5322 return true;
5323
5324 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5325 /* If we're inside a class definition, there's no need to
5326 examine the parameters to the class itself. On the one
5327 hand, they will be checked when the class is defined, and,
5328 on the other, default arguments are valid in things like:
5329 template <class T = double>
5330 struct S { template <class U> void f(U); };
5331 Here the default argument for `S' has no bearing on the
5332 declaration of `f'. */
5333 last_level_to_check = template_class_depth (current_class_type) + 1;
5334 else
5335 /* Check everything. */
5336 last_level_to_check = 0;
5337
5338 for (parm_level = parms;
5339 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5340 parm_level = TREE_CHAIN (parm_level))
5341 {
5342 tree inner_parms = TREE_VALUE (parm_level);
5343 int i;
5344 int ntparms;
5345
5346 ntparms = TREE_VEC_LENGTH (inner_parms);
5347 for (i = 0; i < ntparms; ++i)
5348 {
5349 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5350 continue;
5351
5352 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5353 {
5354 if (msg)
5355 {
5356 no_errors = false;
5357 if (is_friend_decl == 2)
5358 return no_errors;
5359
5360 error (msg, decl);
5361 msg = 0;
5362 }
5363
5364 /* Clear out the default argument so that we are not
5365 confused later. */
5366 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5367 }
5368 }
5369
5370 /* At this point, if we're still interested in issuing messages,
5371 they must apply to classes surrounding the object declared. */
5372 if (msg)
5373 msg = G_("default argument for template parameter for class "
5374 "enclosing %qD");
5375 }
5376
5377 return no_errors;
5378 }
5379
5380 /* Worker for push_template_decl_real, called via
5381 for_each_template_parm. DATA is really an int, indicating the
5382 level of the parameters we are interested in. If T is a template
5383 parameter of that level, return nonzero. */
5384
5385 static int
5386 template_parm_this_level_p (tree t, void* data)
5387 {
5388 int this_level = *(int *)data;
5389 int level;
5390
5391 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5392 level = TEMPLATE_PARM_LEVEL (t);
5393 else
5394 level = TEMPLATE_TYPE_LEVEL (t);
5395 return level == this_level;
5396 }
5397
5398 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5399 DATA is really an int, indicating the innermost outer level of parameters.
5400 If T is a template parameter of that level or further out, return
5401 nonzero. */
5402
5403 static int
5404 template_parm_outer_level (tree t, void *data)
5405 {
5406 int this_level = *(int *)data;
5407 int level;
5408
5409 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5410 level = TEMPLATE_PARM_LEVEL (t);
5411 else
5412 level = TEMPLATE_TYPE_LEVEL (t);
5413 return level <= this_level;
5414 }
5415
5416 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5417 parameters given by current_template_args, or reuses a
5418 previously existing one, if appropriate. Returns the DECL, or an
5419 equivalent one, if it is replaced via a call to duplicate_decls.
5420
5421 If IS_FRIEND is true, DECL is a friend declaration. */
5422
5423 tree
5424 push_template_decl_real (tree decl, bool is_friend)
5425 {
5426 tree tmpl;
5427 tree args;
5428 tree info;
5429 tree ctx;
5430 bool is_primary;
5431 bool is_partial;
5432 int new_template_p = 0;
5433 /* True if the template is a member template, in the sense of
5434 [temp.mem]. */
5435 bool member_template_p = false;
5436
5437 if (decl == error_mark_node || !current_template_parms)
5438 return error_mark_node;
5439
5440 /* See if this is a partial specialization. */
5441 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5442 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5443 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5444 || (VAR_P (decl)
5445 && DECL_LANG_SPECIFIC (decl)
5446 && DECL_TEMPLATE_SPECIALIZATION (decl)
5447 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5448
5449 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5450 is_friend = true;
5451
5452 if (is_friend)
5453 /* For a friend, we want the context of the friend, not
5454 the type of which it is a friend. */
5455 ctx = CP_DECL_CONTEXT (decl);
5456 else if (CP_DECL_CONTEXT (decl)
5457 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5458 /* In the case of a virtual function, we want the class in which
5459 it is defined. */
5460 ctx = CP_DECL_CONTEXT (decl);
5461 else
5462 /* Otherwise, if we're currently defining some class, the DECL
5463 is assumed to be a member of the class. */
5464 ctx = current_scope ();
5465
5466 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5467 ctx = NULL_TREE;
5468
5469 if (!DECL_CONTEXT (decl))
5470 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5471
5472 /* See if this is a primary template. */
5473 if (is_friend && ctx
5474 && uses_template_parms_level (ctx, processing_template_decl))
5475 /* A friend template that specifies a class context, i.e.
5476 template <typename T> friend void A<T>::f();
5477 is not primary. */
5478 is_primary = false;
5479 else if (TREE_CODE (decl) == TYPE_DECL
5480 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5481 is_primary = false;
5482 else
5483 is_primary = template_parm_scope_p ();
5484
5485 if (is_primary)
5486 {
5487 warning (OPT_Wtemplates, "template %qD declared", decl);
5488
5489 if (DECL_CLASS_SCOPE_P (decl))
5490 member_template_p = true;
5491 if (TREE_CODE (decl) == TYPE_DECL
5492 && anon_aggrname_p (DECL_NAME (decl)))
5493 {
5494 error ("template class without a name");
5495 return error_mark_node;
5496 }
5497 else if (TREE_CODE (decl) == FUNCTION_DECL)
5498 {
5499 if (member_template_p)
5500 {
5501 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5502 error ("member template %qD may not have virt-specifiers", decl);
5503 }
5504 if (DECL_DESTRUCTOR_P (decl))
5505 {
5506 /* [temp.mem]
5507
5508 A destructor shall not be a member template. */
5509 error ("destructor %qD declared as member template", decl);
5510 return error_mark_node;
5511 }
5512 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5513 && (!prototype_p (TREE_TYPE (decl))
5514 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5515 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5516 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5517 == void_list_node)))
5518 {
5519 /* [basic.stc.dynamic.allocation]
5520
5521 An allocation function can be a function
5522 template. ... Template allocation functions shall
5523 have two or more parameters. */
5524 error ("invalid template declaration of %qD", decl);
5525 return error_mark_node;
5526 }
5527 }
5528 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5529 && CLASS_TYPE_P (TREE_TYPE (decl)))
5530 {
5531 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5532 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5533 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5534 {
5535 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5536 if (TREE_CODE (t) == TYPE_DECL)
5537 t = TREE_TYPE (t);
5538 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5539 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5540 }
5541 }
5542 else if (TREE_CODE (decl) == TYPE_DECL
5543 && TYPE_DECL_ALIAS_P (decl))
5544 /* alias-declaration */
5545 gcc_assert (!DECL_ARTIFICIAL (decl));
5546 else if (VAR_P (decl))
5547 /* C++14 variable template. */;
5548 else
5549 {
5550 error ("template declaration of %q#D", decl);
5551 return error_mark_node;
5552 }
5553 }
5554
5555 /* Check to see that the rules regarding the use of default
5556 arguments are not being violated. We check args for a friend
5557 functions when we know whether it's a definition, introducing
5558 declaration or re-declaration. */
5559 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5560 check_default_tmpl_args (decl, current_template_parms,
5561 is_primary, is_partial, is_friend);
5562
5563 /* Ensure that there are no parameter packs in the type of this
5564 declaration that have not been expanded. */
5565 if (TREE_CODE (decl) == FUNCTION_DECL)
5566 {
5567 /* Check each of the arguments individually to see if there are
5568 any bare parameter packs. */
5569 tree type = TREE_TYPE (decl);
5570 tree arg = DECL_ARGUMENTS (decl);
5571 tree argtype = TYPE_ARG_TYPES (type);
5572
5573 while (arg && argtype)
5574 {
5575 if (!DECL_PACK_P (arg)
5576 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5577 {
5578 /* This is a PARM_DECL that contains unexpanded parameter
5579 packs. We have already complained about this in the
5580 check_for_bare_parameter_packs call, so just replace
5581 these types with ERROR_MARK_NODE. */
5582 TREE_TYPE (arg) = error_mark_node;
5583 TREE_VALUE (argtype) = error_mark_node;
5584 }
5585
5586 arg = DECL_CHAIN (arg);
5587 argtype = TREE_CHAIN (argtype);
5588 }
5589
5590 /* Check for bare parameter packs in the return type and the
5591 exception specifiers. */
5592 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5593 /* Errors were already issued, set return type to int
5594 as the frontend doesn't expect error_mark_node as
5595 the return type. */
5596 TREE_TYPE (type) = integer_type_node;
5597 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5598 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5599 }
5600 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5601 && TYPE_DECL_ALIAS_P (decl))
5602 ? DECL_ORIGINAL_TYPE (decl)
5603 : TREE_TYPE (decl)))
5604 {
5605 TREE_TYPE (decl) = error_mark_node;
5606 return error_mark_node;
5607 }
5608
5609 if (is_partial)
5610 return process_partial_specialization (decl);
5611
5612 args = current_template_args ();
5613
5614 if (!ctx
5615 || TREE_CODE (ctx) == FUNCTION_DECL
5616 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5617 || (TREE_CODE (decl) == TYPE_DECL
5618 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5619 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5620 {
5621 if (DECL_LANG_SPECIFIC (decl)
5622 && DECL_TEMPLATE_INFO (decl)
5623 && DECL_TI_TEMPLATE (decl))
5624 tmpl = DECL_TI_TEMPLATE (decl);
5625 /* If DECL is a TYPE_DECL for a class-template, then there won't
5626 be DECL_LANG_SPECIFIC. The information equivalent to
5627 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5628 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5629 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5630 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5631 {
5632 /* Since a template declaration already existed for this
5633 class-type, we must be redeclaring it here. Make sure
5634 that the redeclaration is valid. */
5635 redeclare_class_template (TREE_TYPE (decl),
5636 current_template_parms,
5637 current_template_constraints ());
5638 /* We don't need to create a new TEMPLATE_DECL; just use the
5639 one we already had. */
5640 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5641 }
5642 else
5643 {
5644 tmpl = build_template_decl (decl, current_template_parms,
5645 member_template_p);
5646 new_template_p = 1;
5647
5648 if (DECL_LANG_SPECIFIC (decl)
5649 && DECL_TEMPLATE_SPECIALIZATION (decl))
5650 {
5651 /* A specialization of a member template of a template
5652 class. */
5653 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5654 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5655 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5656 }
5657 }
5658 }
5659 else
5660 {
5661 tree a, t, current, parms;
5662 int i;
5663 tree tinfo = get_template_info (decl);
5664
5665 if (!tinfo)
5666 {
5667 error ("template definition of non-template %q#D", decl);
5668 return error_mark_node;
5669 }
5670
5671 tmpl = TI_TEMPLATE (tinfo);
5672
5673 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5674 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5675 && DECL_TEMPLATE_SPECIALIZATION (decl)
5676 && DECL_MEMBER_TEMPLATE_P (tmpl))
5677 {
5678 tree new_tmpl;
5679
5680 /* The declaration is a specialization of a member
5681 template, declared outside the class. Therefore, the
5682 innermost template arguments will be NULL, so we
5683 replace them with the arguments determined by the
5684 earlier call to check_explicit_specialization. */
5685 args = DECL_TI_ARGS (decl);
5686
5687 new_tmpl
5688 = build_template_decl (decl, current_template_parms,
5689 member_template_p);
5690 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5691 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5692 DECL_TI_TEMPLATE (decl) = new_tmpl;
5693 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5694 DECL_TEMPLATE_INFO (new_tmpl)
5695 = build_template_info (tmpl, args);
5696
5697 register_specialization (new_tmpl,
5698 most_general_template (tmpl),
5699 args,
5700 is_friend, 0);
5701 return decl;
5702 }
5703
5704 /* Make sure the template headers we got make sense. */
5705
5706 parms = DECL_TEMPLATE_PARMS (tmpl);
5707 i = TMPL_PARMS_DEPTH (parms);
5708 if (TMPL_ARGS_DEPTH (args) != i)
5709 {
5710 error ("expected %d levels of template parms for %q#D, got %d",
5711 i, decl, TMPL_ARGS_DEPTH (args));
5712 DECL_INTERFACE_KNOWN (decl) = 1;
5713 return error_mark_node;
5714 }
5715 else
5716 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5717 {
5718 a = TMPL_ARGS_LEVEL (args, i);
5719 t = INNERMOST_TEMPLATE_PARMS (parms);
5720
5721 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5722 {
5723 if (current == decl)
5724 error ("got %d template parameters for %q#D",
5725 TREE_VEC_LENGTH (a), decl);
5726 else
5727 error ("got %d template parameters for %q#T",
5728 TREE_VEC_LENGTH (a), current);
5729 error (" but %d required", TREE_VEC_LENGTH (t));
5730 /* Avoid crash in import_export_decl. */
5731 DECL_INTERFACE_KNOWN (decl) = 1;
5732 return error_mark_node;
5733 }
5734
5735 if (current == decl)
5736 current = ctx;
5737 else if (current == NULL_TREE)
5738 /* Can happen in erroneous input. */
5739 break;
5740 else
5741 current = get_containing_scope (current);
5742 }
5743
5744 /* Check that the parms are used in the appropriate qualifying scopes
5745 in the declarator. */
5746 if (!comp_template_args
5747 (TI_ARGS (tinfo),
5748 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5749 {
5750 error ("template arguments to %qD do not match original "
5751 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5752 if (!uses_template_parms (TI_ARGS (tinfo)))
5753 inform (input_location, "use %<template<>%> for"
5754 " an explicit specialization");
5755 /* Avoid crash in import_export_decl. */
5756 DECL_INTERFACE_KNOWN (decl) = 1;
5757 return error_mark_node;
5758 }
5759 }
5760
5761 DECL_TEMPLATE_RESULT (tmpl) = decl;
5762 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5763
5764 /* Push template declarations for global functions and types. Note
5765 that we do not try to push a global template friend declared in a
5766 template class; such a thing may well depend on the template
5767 parameters of the class. */
5768 if (new_template_p && !ctx
5769 && !(is_friend && template_class_depth (current_class_type) > 0))
5770 {
5771 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5772 if (tmpl == error_mark_node)
5773 return error_mark_node;
5774
5775 /* Hide template friend classes that haven't been declared yet. */
5776 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5777 {
5778 DECL_ANTICIPATED (tmpl) = 1;
5779 DECL_FRIEND_P (tmpl) = 1;
5780 }
5781 }
5782
5783 if (is_primary)
5784 {
5785 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5786
5787 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5788
5789 /* Give template template parms a DECL_CONTEXT of the template
5790 for which they are a parameter. */
5791 parms = INNERMOST_TEMPLATE_PARMS (parms);
5792 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5793 {
5794 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5795 if (TREE_CODE (parm) == TEMPLATE_DECL)
5796 DECL_CONTEXT (parm) = tmpl;
5797 }
5798
5799 if (TREE_CODE (decl) == TYPE_DECL
5800 && TYPE_DECL_ALIAS_P (decl)
5801 && complex_alias_template_p (tmpl))
5802 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5803 }
5804
5805 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5806 back to its most general template. If TMPL is a specialization,
5807 ARGS may only have the innermost set of arguments. Add the missing
5808 argument levels if necessary. */
5809 if (DECL_TEMPLATE_INFO (tmpl))
5810 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5811
5812 info = build_template_info (tmpl, args);
5813
5814 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5815 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5816 else
5817 {
5818 if (is_primary)
5819 retrofit_lang_decl (decl);
5820 if (DECL_LANG_SPECIFIC (decl))
5821 DECL_TEMPLATE_INFO (decl) = info;
5822 }
5823
5824 if (flag_implicit_templates
5825 && !is_friend
5826 && TREE_PUBLIC (decl)
5827 && VAR_OR_FUNCTION_DECL_P (decl))
5828 /* Set DECL_COMDAT on template instantiations; if we force
5829 them to be emitted by explicit instantiation or -frepo,
5830 mark_needed will tell cgraph to do the right thing. */
5831 DECL_COMDAT (decl) = true;
5832
5833 return DECL_TEMPLATE_RESULT (tmpl);
5834 }
5835
5836 tree
5837 push_template_decl (tree decl)
5838 {
5839 return push_template_decl_real (decl, false);
5840 }
5841
5842 /* FN is an inheriting constructor that inherits from the constructor
5843 template INHERITED; turn FN into a constructor template with a matching
5844 template header. */
5845
5846 tree
5847 add_inherited_template_parms (tree fn, tree inherited)
5848 {
5849 tree inner_parms
5850 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5851 inner_parms = copy_node (inner_parms);
5852 tree parms
5853 = tree_cons (size_int (processing_template_decl + 1),
5854 inner_parms, current_template_parms);
5855 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5856 tree args = template_parms_to_args (parms);
5857 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5858 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5859 DECL_TEMPLATE_RESULT (tmpl) = fn;
5860 DECL_ARTIFICIAL (tmpl) = true;
5861 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5862 return tmpl;
5863 }
5864
5865 /* Called when a class template TYPE is redeclared with the indicated
5866 template PARMS, e.g.:
5867
5868 template <class T> struct S;
5869 template <class T> struct S {}; */
5870
5871 bool
5872 redeclare_class_template (tree type, tree parms, tree cons)
5873 {
5874 tree tmpl;
5875 tree tmpl_parms;
5876 int i;
5877
5878 if (!TYPE_TEMPLATE_INFO (type))
5879 {
5880 error ("%qT is not a template type", type);
5881 return false;
5882 }
5883
5884 tmpl = TYPE_TI_TEMPLATE (type);
5885 if (!PRIMARY_TEMPLATE_P (tmpl))
5886 /* The type is nested in some template class. Nothing to worry
5887 about here; there are no new template parameters for the nested
5888 type. */
5889 return true;
5890
5891 if (!parms)
5892 {
5893 error ("template specifiers not specified in declaration of %qD",
5894 tmpl);
5895 return false;
5896 }
5897
5898 parms = INNERMOST_TEMPLATE_PARMS (parms);
5899 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5900
5901 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5902 {
5903 error_n (input_location, TREE_VEC_LENGTH (parms),
5904 "redeclared with %d template parameter",
5905 "redeclared with %d template parameters",
5906 TREE_VEC_LENGTH (parms));
5907 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5908 "previous declaration %qD used %d template parameter",
5909 "previous declaration %qD used %d template parameters",
5910 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5911 return false;
5912 }
5913
5914 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5915 {
5916 tree tmpl_parm;
5917 tree parm;
5918 tree tmpl_default;
5919 tree parm_default;
5920
5921 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5922 || TREE_VEC_ELT (parms, i) == error_mark_node)
5923 continue;
5924
5925 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5926 if (error_operand_p (tmpl_parm))
5927 return false;
5928
5929 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5930 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5931 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5932
5933 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5934 TEMPLATE_DECL. */
5935 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5936 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5937 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5938 || (TREE_CODE (tmpl_parm) != PARM_DECL
5939 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5940 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5941 || (TREE_CODE (tmpl_parm) == PARM_DECL
5942 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5943 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5944 {
5945 error ("template parameter %q+#D", tmpl_parm);
5946 error ("redeclared here as %q#D", parm);
5947 return false;
5948 }
5949
5950 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5951 {
5952 /* We have in [temp.param]:
5953
5954 A template-parameter may not be given default arguments
5955 by two different declarations in the same scope. */
5956 error_at (input_location, "redefinition of default argument for %q#D", parm);
5957 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5958 "original definition appeared here");
5959 return false;
5960 }
5961
5962 if (parm_default != NULL_TREE)
5963 /* Update the previous template parameters (which are the ones
5964 that will really count) with the new default value. */
5965 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5966 else if (tmpl_default != NULL_TREE)
5967 /* Update the new parameters, too; they'll be used as the
5968 parameters for any members. */
5969 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5970
5971 /* Give each template template parm in this redeclaration a
5972 DECL_CONTEXT of the template for which they are a parameter. */
5973 if (TREE_CODE (parm) == TEMPLATE_DECL)
5974 {
5975 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5976 DECL_CONTEXT (parm) = tmpl;
5977 }
5978
5979 if (TREE_CODE (parm) == TYPE_DECL)
5980 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5981 }
5982
5983 // Cannot redeclare a class template with a different set of constraints.
5984 if (!equivalent_constraints (get_constraints (tmpl), cons))
5985 {
5986 error_at (input_location, "redeclaration %q#D with different "
5987 "constraints", tmpl);
5988 inform (DECL_SOURCE_LOCATION (tmpl),
5989 "original declaration appeared here");
5990 }
5991
5992 return true;
5993 }
5994
5995 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5996 to be used when the caller has already checked
5997 (processing_template_decl
5998 && !instantiation_dependent_expression_p (expr)
5999 && potential_constant_expression (expr))
6000 and cleared processing_template_decl. */
6001
6002 tree
6003 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6004 {
6005 return tsubst_copy_and_build (expr,
6006 /*args=*/NULL_TREE,
6007 complain,
6008 /*in_decl=*/NULL_TREE,
6009 /*function_p=*/false,
6010 /*integral_constant_expression_p=*/true);
6011 }
6012
6013 /* Simplify EXPR if it is a non-dependent expression. Returns the
6014 (possibly simplified) expression. */
6015
6016 tree
6017 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6018 {
6019 if (expr == NULL_TREE)
6020 return NULL_TREE;
6021
6022 /* If we're in a template, but EXPR isn't value dependent, simplify
6023 it. We're supposed to treat:
6024
6025 template <typename T> void f(T[1 + 1]);
6026 template <typename T> void f(T[2]);
6027
6028 as two declarations of the same function, for example. */
6029 if (processing_template_decl
6030 && is_nondependent_constant_expression (expr))
6031 {
6032 processing_template_decl_sentinel s;
6033 expr = instantiate_non_dependent_expr_internal (expr, complain);
6034 }
6035 return expr;
6036 }
6037
6038 tree
6039 instantiate_non_dependent_expr (tree expr)
6040 {
6041 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6042 }
6043
6044 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6045 an uninstantiated expression. */
6046
6047 tree
6048 instantiate_non_dependent_or_null (tree expr)
6049 {
6050 if (expr == NULL_TREE)
6051 return NULL_TREE;
6052 if (processing_template_decl)
6053 {
6054 if (!is_nondependent_constant_expression (expr))
6055 expr = NULL_TREE;
6056 else
6057 {
6058 processing_template_decl_sentinel s;
6059 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6060 }
6061 }
6062 return expr;
6063 }
6064
6065 /* True iff T is a specialization of a variable template. */
6066
6067 bool
6068 variable_template_specialization_p (tree t)
6069 {
6070 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6071 return false;
6072 tree tmpl = DECL_TI_TEMPLATE (t);
6073 return variable_template_p (tmpl);
6074 }
6075
6076 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6077 template declaration, or a TYPE_DECL for an alias declaration. */
6078
6079 bool
6080 alias_type_or_template_p (tree t)
6081 {
6082 if (t == NULL_TREE)
6083 return false;
6084 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6085 || (TYPE_P (t)
6086 && TYPE_NAME (t)
6087 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6088 || DECL_ALIAS_TEMPLATE_P (t));
6089 }
6090
6091 /* Return TRUE iff T is a specialization of an alias template. */
6092
6093 bool
6094 alias_template_specialization_p (const_tree t)
6095 {
6096 /* It's an alias template specialization if it's an alias and its
6097 TYPE_NAME is a specialization of a primary template. */
6098 if (TYPE_ALIAS_P (t))
6099 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6100 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6101
6102 return false;
6103 }
6104
6105 /* An alias template is complex from a SFINAE perspective if a template-id
6106 using that alias can be ill-formed when the expansion is not, as with
6107 the void_t template. We determine this by checking whether the
6108 expansion for the alias template uses all its template parameters. */
6109
6110 struct uses_all_template_parms_data
6111 {
6112 int level;
6113 bool *seen;
6114 };
6115
6116 static int
6117 uses_all_template_parms_r (tree t, void *data_)
6118 {
6119 struct uses_all_template_parms_data &data
6120 = *(struct uses_all_template_parms_data*)data_;
6121 tree idx = get_template_parm_index (t);
6122
6123 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6124 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6125 return 0;
6126 }
6127
6128 static bool
6129 complex_alias_template_p (const_tree tmpl)
6130 {
6131 struct uses_all_template_parms_data data;
6132 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6133 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6134 data.level = TMPL_PARMS_DEPTH (parms);
6135 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6136 data.seen = XALLOCAVEC (bool, len);
6137 for (int i = 0; i < len; ++i)
6138 data.seen[i] = false;
6139
6140 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6141 for (int i = 0; i < len; ++i)
6142 if (!data.seen[i])
6143 return true;
6144 return false;
6145 }
6146
6147 /* Return TRUE iff T is a specialization of a complex alias template with
6148 dependent template-arguments. */
6149
6150 bool
6151 dependent_alias_template_spec_p (const_tree t)
6152 {
6153 if (!alias_template_specialization_p (t))
6154 return false;
6155
6156 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6157 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6158 return false;
6159
6160 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6161 if (!any_dependent_template_arguments_p (args))
6162 return false;
6163
6164 return true;
6165 }
6166
6167 /* Return the number of innermost template parameters in TMPL. */
6168
6169 static int
6170 num_innermost_template_parms (tree tmpl)
6171 {
6172 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6173 return TREE_VEC_LENGTH (parms);
6174 }
6175
6176 /* Return either TMPL or another template that it is equivalent to under DR
6177 1286: An alias that just changes the name of a template is equivalent to
6178 the other template. */
6179
6180 static tree
6181 get_underlying_template (tree tmpl)
6182 {
6183 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6184 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6185 {
6186 /* Determine if the alias is equivalent to an underlying template. */
6187 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6188 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6189 if (!tinfo)
6190 break;
6191
6192 tree underlying = TI_TEMPLATE (tinfo);
6193 if (!PRIMARY_TEMPLATE_P (underlying)
6194 || (num_innermost_template_parms (tmpl)
6195 != num_innermost_template_parms (underlying)))
6196 break;
6197
6198 tree alias_args = INNERMOST_TEMPLATE_ARGS
6199 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6200 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6201 break;
6202
6203 /* Alias is equivalent. Strip it and repeat. */
6204 tmpl = underlying;
6205 }
6206
6207 return tmpl;
6208 }
6209
6210 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6211 must be a reference-to-function or a pointer-to-function type, as specified
6212 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6213 and check that the resulting function has external linkage. */
6214
6215 static tree
6216 convert_nontype_argument_function (tree type, tree expr,
6217 tsubst_flags_t complain)
6218 {
6219 tree fns = expr;
6220 tree fn, fn_no_ptr;
6221 linkage_kind linkage;
6222
6223 fn = instantiate_type (type, fns, tf_none);
6224 if (fn == error_mark_node)
6225 return error_mark_node;
6226
6227 if (value_dependent_expression_p (fn))
6228 goto accept;
6229
6230 fn_no_ptr = strip_fnptr_conv (fn);
6231 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6232 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6233 if (BASELINK_P (fn_no_ptr))
6234 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6235
6236 /* [temp.arg.nontype]/1
6237
6238 A template-argument for a non-type, non-template template-parameter
6239 shall be one of:
6240 [...]
6241 -- the address of an object or function with external [C++11: or
6242 internal] linkage. */
6243
6244 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6245 {
6246 if (complain & tf_error)
6247 {
6248 error ("%qE is not a valid template argument for type %qT",
6249 expr, type);
6250 if (TYPE_PTR_P (type))
6251 inform (input_location, "it must be the address of a function "
6252 "with external linkage");
6253 else
6254 inform (input_location, "it must be the name of a function with "
6255 "external linkage");
6256 }
6257 return NULL_TREE;
6258 }
6259
6260 linkage = decl_linkage (fn_no_ptr);
6261 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6262 {
6263 if (complain & tf_error)
6264 {
6265 if (cxx_dialect >= cxx11)
6266 error ("%qE is not a valid template argument for type %qT "
6267 "because %qD has no linkage",
6268 expr, type, fn_no_ptr);
6269 else
6270 error ("%qE is not a valid template argument for type %qT "
6271 "because %qD does not have external linkage",
6272 expr, type, fn_no_ptr);
6273 }
6274 return NULL_TREE;
6275 }
6276
6277 accept:
6278 if (TYPE_REF_P (type))
6279 {
6280 if (REFERENCE_REF_P (fn))
6281 fn = TREE_OPERAND (fn, 0);
6282 else
6283 fn = build_address (fn);
6284 }
6285 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6286 fn = build_nop (type, fn);
6287
6288 return fn;
6289 }
6290
6291 /* Subroutine of convert_nontype_argument.
6292 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6293 Emit an error otherwise. */
6294
6295 static bool
6296 check_valid_ptrmem_cst_expr (tree type, tree expr,
6297 tsubst_flags_t complain)
6298 {
6299 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6300 tree orig_expr = expr;
6301 STRIP_NOPS (expr);
6302 if (null_ptr_cst_p (expr))
6303 return true;
6304 if (TREE_CODE (expr) == PTRMEM_CST
6305 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6306 PTRMEM_CST_CLASS (expr)))
6307 return true;
6308 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6309 return true;
6310 if (processing_template_decl
6311 && TREE_CODE (expr) == ADDR_EXPR
6312 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6313 return true;
6314 if (complain & tf_error)
6315 {
6316 error_at (loc, "%qE is not a valid template argument for type %qT",
6317 orig_expr, type);
6318 if (TREE_CODE (expr) != PTRMEM_CST)
6319 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6320 else
6321 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6322 }
6323 return false;
6324 }
6325
6326 /* Returns TRUE iff the address of OP is value-dependent.
6327
6328 14.6.2.4 [temp.dep.temp]:
6329 A non-integral non-type template-argument is dependent if its type is
6330 dependent or it has either of the following forms
6331 qualified-id
6332 & qualified-id
6333 and contains a nested-name-specifier which specifies a class-name that
6334 names a dependent type.
6335
6336 We generalize this to just say that the address of a member of a
6337 dependent class is value-dependent; the above doesn't cover the
6338 address of a static data member named with an unqualified-id. */
6339
6340 static bool
6341 has_value_dependent_address (tree op)
6342 {
6343 /* We could use get_inner_reference here, but there's no need;
6344 this is only relevant for template non-type arguments, which
6345 can only be expressed as &id-expression. */
6346 if (DECL_P (op))
6347 {
6348 tree ctx = CP_DECL_CONTEXT (op);
6349 if (TYPE_P (ctx) && dependent_type_p (ctx))
6350 return true;
6351 }
6352
6353 return false;
6354 }
6355
6356 /* The next set of functions are used for providing helpful explanatory
6357 diagnostics for failed overload resolution. Their messages should be
6358 indented by two spaces for consistency with the messages in
6359 call.c */
6360
6361 static int
6362 unify_success (bool /*explain_p*/)
6363 {
6364 return 0;
6365 }
6366
6367 /* Other failure functions should call this one, to provide a single function
6368 for setting a breakpoint on. */
6369
6370 static int
6371 unify_invalid (bool /*explain_p*/)
6372 {
6373 return 1;
6374 }
6375
6376 static int
6377 unify_parameter_deduction_failure (bool explain_p, tree parm)
6378 {
6379 if (explain_p)
6380 inform (input_location,
6381 " couldn't deduce template parameter %qD", parm);
6382 return unify_invalid (explain_p);
6383 }
6384
6385 static int
6386 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6387 {
6388 if (explain_p)
6389 inform (input_location,
6390 " types %qT and %qT have incompatible cv-qualifiers",
6391 parm, arg);
6392 return unify_invalid (explain_p);
6393 }
6394
6395 static int
6396 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6397 {
6398 if (explain_p)
6399 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6400 return unify_invalid (explain_p);
6401 }
6402
6403 static int
6404 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6405 {
6406 if (explain_p)
6407 inform (input_location,
6408 " template parameter %qD is not a parameter pack, but "
6409 "argument %qD is",
6410 parm, arg);
6411 return unify_invalid (explain_p);
6412 }
6413
6414 static int
6415 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6416 {
6417 if (explain_p)
6418 inform (input_location,
6419 " template argument %qE does not match "
6420 "pointer-to-member constant %qE",
6421 arg, parm);
6422 return unify_invalid (explain_p);
6423 }
6424
6425 static int
6426 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6427 {
6428 if (explain_p)
6429 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6430 return unify_invalid (explain_p);
6431 }
6432
6433 static int
6434 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6435 {
6436 if (explain_p)
6437 inform (input_location,
6438 " inconsistent parameter pack deduction with %qT and %qT",
6439 old_arg, new_arg);
6440 return unify_invalid (explain_p);
6441 }
6442
6443 static int
6444 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6445 {
6446 if (explain_p)
6447 {
6448 if (TYPE_P (parm))
6449 inform (input_location,
6450 " deduced conflicting types for parameter %qT (%qT and %qT)",
6451 parm, first, second);
6452 else
6453 inform (input_location,
6454 " deduced conflicting values for non-type parameter "
6455 "%qE (%qE and %qE)", parm, first, second);
6456 }
6457 return unify_invalid (explain_p);
6458 }
6459
6460 static int
6461 unify_vla_arg (bool explain_p, tree arg)
6462 {
6463 if (explain_p)
6464 inform (input_location,
6465 " variable-sized array type %qT is not "
6466 "a valid template argument",
6467 arg);
6468 return unify_invalid (explain_p);
6469 }
6470
6471 static int
6472 unify_method_type_error (bool explain_p, tree arg)
6473 {
6474 if (explain_p)
6475 inform (input_location,
6476 " member function type %qT is not a valid template argument",
6477 arg);
6478 return unify_invalid (explain_p);
6479 }
6480
6481 static int
6482 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6483 {
6484 if (explain_p)
6485 {
6486 if (least_p)
6487 inform_n (input_location, wanted,
6488 " candidate expects at least %d argument, %d provided",
6489 " candidate expects at least %d arguments, %d provided",
6490 wanted, have);
6491 else
6492 inform_n (input_location, wanted,
6493 " candidate expects %d argument, %d provided",
6494 " candidate expects %d arguments, %d provided",
6495 wanted, have);
6496 }
6497 return unify_invalid (explain_p);
6498 }
6499
6500 static int
6501 unify_too_many_arguments (bool explain_p, int have, int wanted)
6502 {
6503 return unify_arity (explain_p, have, wanted);
6504 }
6505
6506 static int
6507 unify_too_few_arguments (bool explain_p, int have, int wanted,
6508 bool least_p = false)
6509 {
6510 return unify_arity (explain_p, have, wanted, least_p);
6511 }
6512
6513 static int
6514 unify_arg_conversion (bool explain_p, tree to_type,
6515 tree from_type, tree arg)
6516 {
6517 if (explain_p)
6518 inform (cp_expr_loc_or_loc (arg, input_location),
6519 " cannot convert %qE (type %qT) to type %qT",
6520 arg, from_type, to_type);
6521 return unify_invalid (explain_p);
6522 }
6523
6524 static int
6525 unify_no_common_base (bool explain_p, enum template_base_result r,
6526 tree parm, tree arg)
6527 {
6528 if (explain_p)
6529 switch (r)
6530 {
6531 case tbr_ambiguous_baseclass:
6532 inform (input_location, " %qT is an ambiguous base class of %qT",
6533 parm, arg);
6534 break;
6535 default:
6536 inform (input_location, " %qT is not derived from %qT", arg, parm);
6537 break;
6538 }
6539 return unify_invalid (explain_p);
6540 }
6541
6542 static int
6543 unify_inconsistent_template_template_parameters (bool explain_p)
6544 {
6545 if (explain_p)
6546 inform (input_location,
6547 " template parameters of a template template argument are "
6548 "inconsistent with other deduced template arguments");
6549 return unify_invalid (explain_p);
6550 }
6551
6552 static int
6553 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6554 {
6555 if (explain_p)
6556 inform (input_location,
6557 " can't deduce a template for %qT from non-template type %qT",
6558 parm, arg);
6559 return unify_invalid (explain_p);
6560 }
6561
6562 static int
6563 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6564 {
6565 if (explain_p)
6566 inform (input_location,
6567 " template argument %qE does not match %qE", arg, parm);
6568 return unify_invalid (explain_p);
6569 }
6570
6571 /* Attempt to convert the non-type template parameter EXPR to the
6572 indicated TYPE. If the conversion is successful, return the
6573 converted value. If the conversion is unsuccessful, return
6574 NULL_TREE if we issued an error message, or error_mark_node if we
6575 did not. We issue error messages for out-and-out bad template
6576 parameters, but not simply because the conversion failed, since we
6577 might be just trying to do argument deduction. Both TYPE and EXPR
6578 must be non-dependent.
6579
6580 The conversion follows the special rules described in
6581 [temp.arg.nontype], and it is much more strict than an implicit
6582 conversion.
6583
6584 This function is called twice for each template argument (see
6585 lookup_template_class for a more accurate description of this
6586 problem). This means that we need to handle expressions which
6587 are not valid in a C++ source, but can be created from the
6588 first call (for instance, casts to perform conversions). These
6589 hacks can go away after we fix the double coercion problem. */
6590
6591 static tree
6592 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6593 {
6594 tree expr_type;
6595 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6596 tree orig_expr = expr;
6597
6598 /* Detect immediately string literals as invalid non-type argument.
6599 This special-case is not needed for correctness (we would easily
6600 catch this later), but only to provide better diagnostic for this
6601 common user mistake. As suggested by DR 100, we do not mention
6602 linkage issues in the diagnostic as this is not the point. */
6603 /* FIXME we're making this OK. */
6604 if (TREE_CODE (expr) == STRING_CST)
6605 {
6606 if (complain & tf_error)
6607 error ("%qE is not a valid template argument for type %qT "
6608 "because string literals can never be used in this context",
6609 expr, type);
6610 return NULL_TREE;
6611 }
6612
6613 /* Add the ADDR_EXPR now for the benefit of
6614 value_dependent_expression_p. */
6615 if (TYPE_PTROBV_P (type)
6616 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6617 {
6618 expr = decay_conversion (expr, complain);
6619 if (expr == error_mark_node)
6620 return error_mark_node;
6621 }
6622
6623 /* If we are in a template, EXPR may be non-dependent, but still
6624 have a syntactic, rather than semantic, form. For example, EXPR
6625 might be a SCOPE_REF, rather than the VAR_DECL to which the
6626 SCOPE_REF refers. Preserving the qualifying scope is necessary
6627 so that access checking can be performed when the template is
6628 instantiated -- but here we need the resolved form so that we can
6629 convert the argument. */
6630 bool non_dep = false;
6631 if (TYPE_REF_OBJ_P (type)
6632 && has_value_dependent_address (expr))
6633 /* If we want the address and it's value-dependent, don't fold. */;
6634 else if (processing_template_decl
6635 && is_nondependent_constant_expression (expr))
6636 non_dep = true;
6637 if (error_operand_p (expr))
6638 return error_mark_node;
6639 expr_type = TREE_TYPE (expr);
6640
6641 /* If the argument is non-dependent, perform any conversions in
6642 non-dependent context as well. */
6643 processing_template_decl_sentinel s (non_dep);
6644 if (non_dep)
6645 expr = instantiate_non_dependent_expr_internal (expr, complain);
6646
6647 if (value_dependent_expression_p (expr))
6648 expr = canonicalize_expr_argument (expr, complain);
6649
6650 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6651 to a non-type argument of "nullptr". */
6652 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6653 expr = fold_simple (convert (type, expr));
6654
6655 /* In C++11, integral or enumeration non-type template arguments can be
6656 arbitrary constant expressions. Pointer and pointer to
6657 member arguments can be general constant expressions that evaluate
6658 to a null value, but otherwise still need to be of a specific form. */
6659 if (cxx_dialect >= cxx11)
6660 {
6661 if (TREE_CODE (expr) == PTRMEM_CST)
6662 /* A PTRMEM_CST is already constant, and a valid template
6663 argument for a parameter of pointer to member type, we just want
6664 to leave it in that form rather than lower it to a
6665 CONSTRUCTOR. */;
6666 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6667 || cxx_dialect >= cxx17)
6668 {
6669 /* C++17: A template-argument for a non-type template-parameter shall
6670 be a converted constant expression (8.20) of the type of the
6671 template-parameter. */
6672 expr = build_converted_constant_expr (type, expr, complain);
6673 if (expr == error_mark_node)
6674 return error_mark_node;
6675 expr = maybe_constant_value (expr);
6676 expr = convert_from_reference (expr);
6677 }
6678 else if (TYPE_PTR_OR_PTRMEM_P (type))
6679 {
6680 tree folded = maybe_constant_value (expr);
6681 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6682 : null_member_pointer_value_p (folded))
6683 expr = folded;
6684 }
6685 }
6686
6687 if (TYPE_REF_P (type))
6688 expr = mark_lvalue_use (expr);
6689 else
6690 expr = mark_rvalue_use (expr);
6691
6692 /* HACK: Due to double coercion, we can get a
6693 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6694 which is the tree that we built on the first call (see
6695 below when coercing to reference to object or to reference to
6696 function). We just strip everything and get to the arg.
6697 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6698 for examples. */
6699 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6700 {
6701 tree probe_type, probe = expr;
6702 if (REFERENCE_REF_P (probe))
6703 probe = TREE_OPERAND (probe, 0);
6704 probe_type = TREE_TYPE (probe);
6705 if (TREE_CODE (probe) == NOP_EXPR)
6706 {
6707 /* ??? Maybe we could use convert_from_reference here, but we
6708 would need to relax its constraints because the NOP_EXPR
6709 could actually change the type to something more cv-qualified,
6710 and this is not folded by convert_from_reference. */
6711 tree addr = TREE_OPERAND (probe, 0);
6712 if (TYPE_REF_P (probe_type)
6713 && TREE_CODE (addr) == ADDR_EXPR
6714 && TYPE_PTR_P (TREE_TYPE (addr))
6715 && (same_type_ignoring_top_level_qualifiers_p
6716 (TREE_TYPE (probe_type),
6717 TREE_TYPE (TREE_TYPE (addr)))))
6718 {
6719 expr = TREE_OPERAND (addr, 0);
6720 expr_type = TREE_TYPE (probe_type);
6721 }
6722 }
6723 }
6724
6725 /* [temp.arg.nontype]/5, bullet 1
6726
6727 For a non-type template-parameter of integral or enumeration type,
6728 integral promotions (_conv.prom_) and integral conversions
6729 (_conv.integral_) are applied. */
6730 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6731 {
6732 if (cxx_dialect < cxx11)
6733 {
6734 tree t = build_converted_constant_expr (type, expr, complain);
6735 t = maybe_constant_value (t);
6736 if (t != error_mark_node)
6737 expr = t;
6738 }
6739
6740 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6741 return error_mark_node;
6742
6743 /* Notice that there are constant expressions like '4 % 0' which
6744 do not fold into integer constants. */
6745 if (TREE_CODE (expr) != INTEGER_CST
6746 && !value_dependent_expression_p (expr))
6747 {
6748 if (complain & tf_error)
6749 {
6750 int errs = errorcount, warns = warningcount + werrorcount;
6751 if (!require_potential_constant_expression (expr))
6752 expr = error_mark_node;
6753 else
6754 expr = cxx_constant_value (expr);
6755 if (errorcount > errs || warningcount + werrorcount > warns)
6756 inform (loc, "in template argument for type %qT", type);
6757 if (expr == error_mark_node)
6758 return NULL_TREE;
6759 /* else cxx_constant_value complained but gave us
6760 a real constant, so go ahead. */
6761 if (TREE_CODE (expr) != INTEGER_CST)
6762 {
6763 /* Some assemble time constant expressions like
6764 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6765 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6766 as we can emit them into .rodata initializers of
6767 variables, yet they can't fold into an INTEGER_CST at
6768 compile time. Refuse them here. */
6769 gcc_checking_assert (reduced_constant_expression_p (expr));
6770 error_at (loc, "template argument %qE for type %qT not "
6771 "a constant integer", expr, type);
6772 return NULL_TREE;
6773 }
6774 }
6775 else
6776 return NULL_TREE;
6777 }
6778
6779 /* Avoid typedef problems. */
6780 if (TREE_TYPE (expr) != type)
6781 expr = fold_convert (type, expr);
6782 }
6783 /* [temp.arg.nontype]/5, bullet 2
6784
6785 For a non-type template-parameter of type pointer to object,
6786 qualification conversions (_conv.qual_) and the array-to-pointer
6787 conversion (_conv.array_) are applied. */
6788 else if (TYPE_PTROBV_P (type))
6789 {
6790 tree decayed = expr;
6791
6792 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6793 decay_conversion or an explicit cast. If it's a problematic cast,
6794 we'll complain about it below. */
6795 if (TREE_CODE (expr) == NOP_EXPR)
6796 {
6797 tree probe = expr;
6798 STRIP_NOPS (probe);
6799 if (TREE_CODE (probe) == ADDR_EXPR
6800 && TYPE_PTR_P (TREE_TYPE (probe)))
6801 {
6802 expr = probe;
6803 expr_type = TREE_TYPE (expr);
6804 }
6805 }
6806
6807 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6808
6809 A template-argument for a non-type, non-template template-parameter
6810 shall be one of: [...]
6811
6812 -- the name of a non-type template-parameter;
6813 -- the address of an object or function with external linkage, [...]
6814 expressed as "& id-expression" where the & is optional if the name
6815 refers to a function or array, or if the corresponding
6816 template-parameter is a reference.
6817
6818 Here, we do not care about functions, as they are invalid anyway
6819 for a parameter of type pointer-to-object. */
6820
6821 if (value_dependent_expression_p (expr))
6822 /* Non-type template parameters are OK. */
6823 ;
6824 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6825 /* Null pointer values are OK in C++11. */;
6826 else if (TREE_CODE (expr) != ADDR_EXPR)
6827 {
6828 if (VAR_P (expr))
6829 {
6830 if (complain & tf_error)
6831 error ("%qD is not a valid template argument "
6832 "because %qD is a variable, not the address of "
6833 "a variable", orig_expr, expr);
6834 return NULL_TREE;
6835 }
6836 if (INDIRECT_TYPE_P (expr_type))
6837 {
6838 if (complain & tf_error)
6839 error ("%qE is not a valid template argument for %qT "
6840 "because it is not the address of a variable",
6841 orig_expr, type);
6842 return NULL_TREE;
6843 }
6844 /* Other values, like integer constants, might be valid
6845 non-type arguments of some other type. */
6846 return error_mark_node;
6847 }
6848 else
6849 {
6850 tree decl = TREE_OPERAND (expr, 0);
6851
6852 if (!VAR_P (decl))
6853 {
6854 if (complain & tf_error)
6855 error ("%qE is not a valid template argument of type %qT "
6856 "because %qE is not a variable", orig_expr, type, decl);
6857 return NULL_TREE;
6858 }
6859 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6860 {
6861 if (complain & tf_error)
6862 error ("%qE is not a valid template argument of type %qT "
6863 "because %qD does not have external linkage",
6864 orig_expr, type, decl);
6865 return NULL_TREE;
6866 }
6867 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6868 && decl_linkage (decl) == lk_none)
6869 {
6870 if (complain & tf_error)
6871 error ("%qE is not a valid template argument of type %qT "
6872 "because %qD has no linkage", orig_expr, type, decl);
6873 return NULL_TREE;
6874 }
6875 /* C++17: For a non-type template-parameter of reference or pointer
6876 type, the value of the constant expression shall not refer to (or
6877 for a pointer type, shall not be the address of):
6878 * a subobject (4.5),
6879 * a temporary object (15.2),
6880 * a string literal (5.13.5),
6881 * the result of a typeid expression (8.2.8), or
6882 * a predefined __func__ variable (11.4.1). */
6883 else if (DECL_ARTIFICIAL (decl))
6884 {
6885 if (complain & tf_error)
6886 error ("the address of %qD is not a valid template argument",
6887 decl);
6888 return NULL_TREE;
6889 }
6890 else if (!same_type_ignoring_top_level_qualifiers_p
6891 (strip_array_types (TREE_TYPE (type)),
6892 strip_array_types (TREE_TYPE (decl))))
6893 {
6894 if (complain & tf_error)
6895 error ("the address of the %qT subobject of %qD is not a "
6896 "valid template argument", TREE_TYPE (type), decl);
6897 return NULL_TREE;
6898 }
6899 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6900 {
6901 if (complain & tf_error)
6902 error ("the address of %qD is not a valid template argument "
6903 "because it does not have static storage duration",
6904 decl);
6905 return NULL_TREE;
6906 }
6907 }
6908
6909 expr = decayed;
6910
6911 expr = perform_qualification_conversions (type, expr);
6912 if (expr == error_mark_node)
6913 return error_mark_node;
6914 }
6915 /* [temp.arg.nontype]/5, bullet 3
6916
6917 For a non-type template-parameter of type reference to object, no
6918 conversions apply. The type referred to by the reference may be more
6919 cv-qualified than the (otherwise identical) type of the
6920 template-argument. The template-parameter is bound directly to the
6921 template-argument, which must be an lvalue. */
6922 else if (TYPE_REF_OBJ_P (type))
6923 {
6924 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6925 expr_type))
6926 return error_mark_node;
6927
6928 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6929 {
6930 if (complain & tf_error)
6931 error ("%qE is not a valid template argument for type %qT "
6932 "because of conflicts in cv-qualification", expr, type);
6933 return NULL_TREE;
6934 }
6935
6936 if (!lvalue_p (expr))
6937 {
6938 if (complain & tf_error)
6939 error ("%qE is not a valid template argument for type %qT "
6940 "because it is not an lvalue", expr, type);
6941 return NULL_TREE;
6942 }
6943
6944 /* [temp.arg.nontype]/1
6945
6946 A template-argument for a non-type, non-template template-parameter
6947 shall be one of: [...]
6948
6949 -- the address of an object or function with external linkage. */
6950 if (INDIRECT_REF_P (expr)
6951 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6952 {
6953 expr = TREE_OPERAND (expr, 0);
6954 if (DECL_P (expr))
6955 {
6956 if (complain & tf_error)
6957 error ("%q#D is not a valid template argument for type %qT "
6958 "because a reference variable does not have a constant "
6959 "address", expr, type);
6960 return NULL_TREE;
6961 }
6962 }
6963
6964 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6965 && value_dependent_expression_p (expr))
6966 /* OK, dependent reference. We don't want to ask whether a DECL is
6967 itself value-dependent, since what we want here is its address. */;
6968 else
6969 {
6970 if (!DECL_P (expr))
6971 {
6972 if (complain & tf_error)
6973 error ("%qE is not a valid template argument for type %qT "
6974 "because it is not an object with linkage",
6975 expr, type);
6976 return NULL_TREE;
6977 }
6978
6979 /* DR 1155 allows internal linkage in C++11 and up. */
6980 linkage_kind linkage = decl_linkage (expr);
6981 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6982 {
6983 if (complain & tf_error)
6984 error ("%qE is not a valid template argument for type %qT "
6985 "because object %qD does not have linkage",
6986 expr, type, expr);
6987 return NULL_TREE;
6988 }
6989
6990 expr = build_address (expr);
6991 }
6992
6993 if (!same_type_p (type, TREE_TYPE (expr)))
6994 expr = build_nop (type, expr);
6995 }
6996 /* [temp.arg.nontype]/5, bullet 4
6997
6998 For a non-type template-parameter of type pointer to function, only
6999 the function-to-pointer conversion (_conv.func_) is applied. If the
7000 template-argument represents a set of overloaded functions (or a
7001 pointer to such), the matching function is selected from the set
7002 (_over.over_). */
7003 else if (TYPE_PTRFN_P (type))
7004 {
7005 /* If the argument is a template-id, we might not have enough
7006 context information to decay the pointer. */
7007 if (!type_unknown_p (expr_type))
7008 {
7009 expr = decay_conversion (expr, complain);
7010 if (expr == error_mark_node)
7011 return error_mark_node;
7012 }
7013
7014 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7015 /* Null pointer values are OK in C++11. */
7016 return perform_qualification_conversions (type, expr);
7017
7018 expr = convert_nontype_argument_function (type, expr, complain);
7019 if (!expr || expr == error_mark_node)
7020 return expr;
7021 }
7022 /* [temp.arg.nontype]/5, bullet 5
7023
7024 For a non-type template-parameter of type reference to function, no
7025 conversions apply. If the template-argument represents a set of
7026 overloaded functions, the matching function is selected from the set
7027 (_over.over_). */
7028 else if (TYPE_REFFN_P (type))
7029 {
7030 if (TREE_CODE (expr) == ADDR_EXPR)
7031 {
7032 if (complain & tf_error)
7033 {
7034 error ("%qE is not a valid template argument for type %qT "
7035 "because it is a pointer", expr, type);
7036 inform (input_location, "try using %qE instead",
7037 TREE_OPERAND (expr, 0));
7038 }
7039 return NULL_TREE;
7040 }
7041
7042 expr = convert_nontype_argument_function (type, expr, complain);
7043 if (!expr || expr == error_mark_node)
7044 return expr;
7045 }
7046 /* [temp.arg.nontype]/5, bullet 6
7047
7048 For a non-type template-parameter of type pointer to member function,
7049 no conversions apply. If the template-argument represents a set of
7050 overloaded member functions, the matching member function is selected
7051 from the set (_over.over_). */
7052 else if (TYPE_PTRMEMFUNC_P (type))
7053 {
7054 expr = instantiate_type (type, expr, tf_none);
7055 if (expr == error_mark_node)
7056 return error_mark_node;
7057
7058 /* [temp.arg.nontype] bullet 1 says the pointer to member
7059 expression must be a pointer-to-member constant. */
7060 if (!value_dependent_expression_p (expr)
7061 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7062 return NULL_TREE;
7063
7064 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7065 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7066 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7067 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7068 }
7069 /* [temp.arg.nontype]/5, bullet 7
7070
7071 For a non-type template-parameter of type pointer to data member,
7072 qualification conversions (_conv.qual_) are applied. */
7073 else if (TYPE_PTRDATAMEM_P (type))
7074 {
7075 /* [temp.arg.nontype] bullet 1 says the pointer to member
7076 expression must be a pointer-to-member constant. */
7077 if (!value_dependent_expression_p (expr)
7078 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7079 return NULL_TREE;
7080
7081 expr = perform_qualification_conversions (type, expr);
7082 if (expr == error_mark_node)
7083 return expr;
7084 }
7085 else if (NULLPTR_TYPE_P (type))
7086 {
7087 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7088 {
7089 if (complain & tf_error)
7090 error ("%qE is not a valid template argument for type %qT "
7091 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7092 return NULL_TREE;
7093 }
7094 return expr;
7095 }
7096 /* A template non-type parameter must be one of the above. */
7097 else
7098 gcc_unreachable ();
7099
7100 /* Sanity check: did we actually convert the argument to the
7101 right type? */
7102 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7103 (type, TREE_TYPE (expr)));
7104 return convert_from_reference (expr);
7105 }
7106
7107 /* Subroutine of coerce_template_template_parms, which returns 1 if
7108 PARM_PARM and ARG_PARM match using the rule for the template
7109 parameters of template template parameters. Both PARM and ARG are
7110 template parameters; the rest of the arguments are the same as for
7111 coerce_template_template_parms.
7112 */
7113 static int
7114 coerce_template_template_parm (tree parm,
7115 tree arg,
7116 tsubst_flags_t complain,
7117 tree in_decl,
7118 tree outer_args)
7119 {
7120 if (arg == NULL_TREE || error_operand_p (arg)
7121 || parm == NULL_TREE || error_operand_p (parm))
7122 return 0;
7123
7124 if (TREE_CODE (arg) != TREE_CODE (parm))
7125 return 0;
7126
7127 switch (TREE_CODE (parm))
7128 {
7129 case TEMPLATE_DECL:
7130 /* We encounter instantiations of templates like
7131 template <template <template <class> class> class TT>
7132 class C; */
7133 {
7134 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7135 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7136
7137 if (!coerce_template_template_parms
7138 (parmparm, argparm, complain, in_decl, outer_args))
7139 return 0;
7140 }
7141 /* Fall through. */
7142
7143 case TYPE_DECL:
7144 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7145 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7146 /* Argument is a parameter pack but parameter is not. */
7147 return 0;
7148 break;
7149
7150 case PARM_DECL:
7151 /* The tsubst call is used to handle cases such as
7152
7153 template <int> class C {};
7154 template <class T, template <T> class TT> class D {};
7155 D<int, C> d;
7156
7157 i.e. the parameter list of TT depends on earlier parameters. */
7158 if (!uses_template_parms (TREE_TYPE (arg)))
7159 {
7160 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7161 if (!uses_template_parms (t)
7162 && !same_type_p (t, TREE_TYPE (arg)))
7163 return 0;
7164 }
7165
7166 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7167 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7168 /* Argument is a parameter pack but parameter is not. */
7169 return 0;
7170
7171 break;
7172
7173 default:
7174 gcc_unreachable ();
7175 }
7176
7177 return 1;
7178 }
7179
7180 /* Coerce template argument list ARGLIST for use with template
7181 template-parameter TEMPL. */
7182
7183 static tree
7184 coerce_template_args_for_ttp (tree templ, tree arglist,
7185 tsubst_flags_t complain)
7186 {
7187 /* Consider an example where a template template parameter declared as
7188
7189 template <class T, class U = std::allocator<T> > class TT
7190
7191 The template parameter level of T and U are one level larger than
7192 of TT. To proper process the default argument of U, say when an
7193 instantiation `TT<int>' is seen, we need to build the full
7194 arguments containing {int} as the innermost level. Outer levels,
7195 available when not appearing as default template argument, can be
7196 obtained from the arguments of the enclosing template.
7197
7198 Suppose that TT is later substituted with std::vector. The above
7199 instantiation is `TT<int, std::allocator<T> >' with TT at
7200 level 1, and T at level 2, while the template arguments at level 1
7201 becomes {std::vector} and the inner level 2 is {int}. */
7202
7203 tree outer = DECL_CONTEXT (templ);
7204 if (outer)
7205 {
7206 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7207 /* We want arguments for the partial specialization, not arguments for
7208 the primary template. */
7209 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7210 else
7211 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7212 }
7213 else if (current_template_parms)
7214 {
7215 /* This is an argument of the current template, so we haven't set
7216 DECL_CONTEXT yet. */
7217 tree relevant_template_parms;
7218
7219 /* Parameter levels that are greater than the level of the given
7220 template template parm are irrelevant. */
7221 relevant_template_parms = current_template_parms;
7222 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7223 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7224 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7225
7226 outer = template_parms_to_args (relevant_template_parms);
7227 }
7228
7229 if (outer)
7230 arglist = add_to_template_args (outer, arglist);
7231
7232 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7233 return coerce_template_parms (parmlist, arglist, templ,
7234 complain,
7235 /*require_all_args=*/true,
7236 /*use_default_args=*/true);
7237 }
7238
7239 /* A cache of template template parameters with match-all default
7240 arguments. */
7241 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7242 static void
7243 store_defaulted_ttp (tree v, tree t)
7244 {
7245 if (!defaulted_ttp_cache)
7246 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7247 defaulted_ttp_cache->put (v, t);
7248 }
7249 static tree
7250 lookup_defaulted_ttp (tree v)
7251 {
7252 if (defaulted_ttp_cache)
7253 if (tree *p = defaulted_ttp_cache->get (v))
7254 return *p;
7255 return NULL_TREE;
7256 }
7257
7258 /* T is a bound template template-parameter. Copy its arguments into default
7259 arguments of the template template-parameter's template parameters. */
7260
7261 static tree
7262 add_defaults_to_ttp (tree otmpl)
7263 {
7264 if (tree c = lookup_defaulted_ttp (otmpl))
7265 return c;
7266
7267 tree ntmpl = copy_node (otmpl);
7268
7269 tree ntype = copy_node (TREE_TYPE (otmpl));
7270 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7271 TYPE_MAIN_VARIANT (ntype) = ntype;
7272 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7273 TYPE_NAME (ntype) = ntmpl;
7274 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7275
7276 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7277 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7278 TEMPLATE_PARM_DECL (idx) = ntmpl;
7279 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7280
7281 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7282 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7283 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7284 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7285 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7286 {
7287 tree o = TREE_VEC_ELT (vec, i);
7288 if (!template_parameter_pack_p (TREE_VALUE (o)))
7289 {
7290 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7291 TREE_PURPOSE (n) = any_targ_node;
7292 }
7293 }
7294
7295 store_defaulted_ttp (otmpl, ntmpl);
7296 return ntmpl;
7297 }
7298
7299 /* ARG is a bound potential template template-argument, and PARGS is a list
7300 of arguments for the corresponding template template-parameter. Adjust
7301 PARGS as appropriate for application to ARG's template, and if ARG is a
7302 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7303 arguments to the template template parameter. */
7304
7305 static tree
7306 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7307 {
7308 ++processing_template_decl;
7309 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7310 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7311 {
7312 /* When comparing two template template-parameters in partial ordering,
7313 rewrite the one currently being used as an argument to have default
7314 arguments for all parameters. */
7315 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7316 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7317 if (pargs != error_mark_node)
7318 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7319 TYPE_TI_ARGS (arg));
7320 }
7321 else
7322 {
7323 tree aparms
7324 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7325 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7326 /*require_all*/true,
7327 /*use_default*/true);
7328 }
7329 --processing_template_decl;
7330 return pargs;
7331 }
7332
7333 /* Subroutine of unify for the case when PARM is a
7334 BOUND_TEMPLATE_TEMPLATE_PARM. */
7335
7336 static int
7337 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7338 bool explain_p)
7339 {
7340 tree parmvec = TYPE_TI_ARGS (parm);
7341 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7342
7343 /* The template template parm might be variadic and the argument
7344 not, so flatten both argument lists. */
7345 parmvec = expand_template_argument_pack (parmvec);
7346 argvec = expand_template_argument_pack (argvec);
7347
7348 if (flag_new_ttp)
7349 {
7350 /* In keeping with P0522R0, adjust P's template arguments
7351 to apply to A's template; then flatten it again. */
7352 tree nparmvec = parmvec;
7353 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7354 nparmvec = expand_template_argument_pack (nparmvec);
7355
7356 if (unify (tparms, targs, nparmvec, argvec,
7357 UNIFY_ALLOW_NONE, explain_p))
7358 return 1;
7359
7360 /* If the P0522 adjustment eliminated a pack expansion, deduce
7361 empty packs. */
7362 if (flag_new_ttp
7363 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7364 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7365 DEDUCE_EXACT, /*sub*/true, explain_p))
7366 return 1;
7367 }
7368 else
7369 {
7370 /* Deduce arguments T, i from TT<T> or TT<i>.
7371 We check each element of PARMVEC and ARGVEC individually
7372 rather than the whole TREE_VEC since they can have
7373 different number of elements, which is allowed under N2555. */
7374
7375 int len = TREE_VEC_LENGTH (parmvec);
7376
7377 /* Check if the parameters end in a pack, making them
7378 variadic. */
7379 int parm_variadic_p = 0;
7380 if (len > 0
7381 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7382 parm_variadic_p = 1;
7383
7384 for (int i = 0; i < len - parm_variadic_p; ++i)
7385 /* If the template argument list of P contains a pack
7386 expansion that is not the last template argument, the
7387 entire template argument list is a non-deduced
7388 context. */
7389 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7390 return unify_success (explain_p);
7391
7392 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7393 return unify_too_few_arguments (explain_p,
7394 TREE_VEC_LENGTH (argvec), len);
7395
7396 for (int i = 0; i < len - parm_variadic_p; ++i)
7397 if (unify (tparms, targs,
7398 TREE_VEC_ELT (parmvec, i),
7399 TREE_VEC_ELT (argvec, i),
7400 UNIFY_ALLOW_NONE, explain_p))
7401 return 1;
7402
7403 if (parm_variadic_p
7404 && unify_pack_expansion (tparms, targs,
7405 parmvec, argvec,
7406 DEDUCE_EXACT,
7407 /*subr=*/true, explain_p))
7408 return 1;
7409 }
7410
7411 return 0;
7412 }
7413
7414 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7415 template template parameters. Both PARM_PARMS and ARG_PARMS are
7416 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7417 or PARM_DECL.
7418
7419 Consider the example:
7420 template <class T> class A;
7421 template<template <class U> class TT> class B;
7422
7423 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7424 the parameters to A, and OUTER_ARGS contains A. */
7425
7426 static int
7427 coerce_template_template_parms (tree parm_parms,
7428 tree arg_parms,
7429 tsubst_flags_t complain,
7430 tree in_decl,
7431 tree outer_args)
7432 {
7433 int nparms, nargs, i;
7434 tree parm, arg;
7435 int variadic_p = 0;
7436
7437 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7438 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7439
7440 nparms = TREE_VEC_LENGTH (parm_parms);
7441 nargs = TREE_VEC_LENGTH (arg_parms);
7442
7443 if (flag_new_ttp)
7444 {
7445 /* P0522R0: A template template-parameter P is at least as specialized as
7446 a template template-argument A if, given the following rewrite to two
7447 function templates, the function template corresponding to P is at
7448 least as specialized as the function template corresponding to A
7449 according to the partial ordering rules for function templates
7450 ([temp.func.order]). Given an invented class template X with the
7451 template parameter list of A (including default arguments):
7452
7453 * Each of the two function templates has the same template parameters,
7454 respectively, as P or A.
7455
7456 * Each function template has a single function parameter whose type is
7457 a specialization of X with template arguments corresponding to the
7458 template parameters from the respective function template where, for
7459 each template parameter PP in the template parameter list of the
7460 function template, a corresponding template argument AA is formed. If
7461 PP declares a parameter pack, then AA is the pack expansion
7462 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7463
7464 If the rewrite produces an invalid type, then P is not at least as
7465 specialized as A. */
7466
7467 /* So coerce P's args to apply to A's parms, and then deduce between A's
7468 args and the converted args. If that succeeds, A is at least as
7469 specialized as P, so they match.*/
7470 tree pargs = template_parms_level_to_args (parm_parms);
7471 ++processing_template_decl;
7472 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7473 /*require_all*/true, /*use_default*/true);
7474 --processing_template_decl;
7475 if (pargs != error_mark_node)
7476 {
7477 tree targs = make_tree_vec (nargs);
7478 tree aargs = template_parms_level_to_args (arg_parms);
7479 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7480 /*explain*/false))
7481 return 1;
7482 }
7483 }
7484
7485 /* Determine whether we have a parameter pack at the end of the
7486 template template parameter's template parameter list. */
7487 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7488 {
7489 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7490
7491 if (error_operand_p (parm))
7492 return 0;
7493
7494 switch (TREE_CODE (parm))
7495 {
7496 case TEMPLATE_DECL:
7497 case TYPE_DECL:
7498 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7499 variadic_p = 1;
7500 break;
7501
7502 case PARM_DECL:
7503 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7504 variadic_p = 1;
7505 break;
7506
7507 default:
7508 gcc_unreachable ();
7509 }
7510 }
7511
7512 if (nargs != nparms
7513 && !(variadic_p && nargs >= nparms - 1))
7514 return 0;
7515
7516 /* Check all of the template parameters except the parameter pack at
7517 the end (if any). */
7518 for (i = 0; i < nparms - variadic_p; ++i)
7519 {
7520 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7521 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7522 continue;
7523
7524 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7525 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7526
7527 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7528 outer_args))
7529 return 0;
7530
7531 }
7532
7533 if (variadic_p)
7534 {
7535 /* Check each of the template parameters in the template
7536 argument against the template parameter pack at the end of
7537 the template template parameter. */
7538 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7539 return 0;
7540
7541 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7542
7543 for (; i < nargs; ++i)
7544 {
7545 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7546 continue;
7547
7548 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7549
7550 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7551 outer_args))
7552 return 0;
7553 }
7554 }
7555
7556 return 1;
7557 }
7558
7559 /* Verifies that the deduced template arguments (in TARGS) for the
7560 template template parameters (in TPARMS) represent valid bindings,
7561 by comparing the template parameter list of each template argument
7562 to the template parameter list of its corresponding template
7563 template parameter, in accordance with DR150. This
7564 routine can only be called after all template arguments have been
7565 deduced. It will return TRUE if all of the template template
7566 parameter bindings are okay, FALSE otherwise. */
7567 bool
7568 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7569 {
7570 int i, ntparms = TREE_VEC_LENGTH (tparms);
7571 bool ret = true;
7572
7573 /* We're dealing with template parms in this process. */
7574 ++processing_template_decl;
7575
7576 targs = INNERMOST_TEMPLATE_ARGS (targs);
7577
7578 for (i = 0; i < ntparms; ++i)
7579 {
7580 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7581 tree targ = TREE_VEC_ELT (targs, i);
7582
7583 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7584 {
7585 tree packed_args = NULL_TREE;
7586 int idx, len = 1;
7587
7588 if (ARGUMENT_PACK_P (targ))
7589 {
7590 /* Look inside the argument pack. */
7591 packed_args = ARGUMENT_PACK_ARGS (targ);
7592 len = TREE_VEC_LENGTH (packed_args);
7593 }
7594
7595 for (idx = 0; idx < len; ++idx)
7596 {
7597 tree targ_parms = NULL_TREE;
7598
7599 if (packed_args)
7600 /* Extract the next argument from the argument
7601 pack. */
7602 targ = TREE_VEC_ELT (packed_args, idx);
7603
7604 if (PACK_EXPANSION_P (targ))
7605 /* Look at the pattern of the pack expansion. */
7606 targ = PACK_EXPANSION_PATTERN (targ);
7607
7608 /* Extract the template parameters from the template
7609 argument. */
7610 if (TREE_CODE (targ) == TEMPLATE_DECL)
7611 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7612 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7613 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7614
7615 /* Verify that we can coerce the template template
7616 parameters from the template argument to the template
7617 parameter. This requires an exact match. */
7618 if (targ_parms
7619 && !coerce_template_template_parms
7620 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7621 targ_parms,
7622 tf_none,
7623 tparm,
7624 targs))
7625 {
7626 ret = false;
7627 goto out;
7628 }
7629 }
7630 }
7631 }
7632
7633 out:
7634
7635 --processing_template_decl;
7636 return ret;
7637 }
7638
7639 /* Since type attributes aren't mangled, we need to strip them from
7640 template type arguments. */
7641
7642 static tree
7643 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7644 {
7645 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7646 return arg;
7647 bool removed_attributes = false;
7648 tree canon = strip_typedefs (arg, &removed_attributes);
7649 if (removed_attributes
7650 && (complain & tf_warning))
7651 warning (OPT_Wignored_attributes,
7652 "ignoring attributes on template argument %qT", arg);
7653 return canon;
7654 }
7655
7656 /* And from inside dependent non-type arguments like sizeof(Type). */
7657
7658 static tree
7659 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7660 {
7661 if (!arg || arg == error_mark_node)
7662 return arg;
7663 bool removed_attributes = false;
7664 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7665 if (removed_attributes
7666 && (complain & tf_warning))
7667 warning (OPT_Wignored_attributes,
7668 "ignoring attributes in template argument %qE", arg);
7669 return canon;
7670 }
7671
7672 // A template declaration can be substituted for a constrained
7673 // template template parameter only when the argument is more
7674 // constrained than the parameter.
7675 static bool
7676 is_compatible_template_arg (tree parm, tree arg)
7677 {
7678 tree parm_cons = get_constraints (parm);
7679
7680 /* For now, allow constrained template template arguments
7681 and unconstrained template template parameters. */
7682 if (parm_cons == NULL_TREE)
7683 return true;
7684
7685 tree arg_cons = get_constraints (arg);
7686
7687 // If the template parameter is constrained, we need to rewrite its
7688 // constraints in terms of the ARG's template parameters. This ensures
7689 // that all of the template parameter types will have the same depth.
7690 //
7691 // Note that this is only valid when coerce_template_template_parm is
7692 // true for the innermost template parameters of PARM and ARG. In other
7693 // words, because coercion is successful, this conversion will be valid.
7694 if (parm_cons)
7695 {
7696 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7697 parm_cons = tsubst_constraint_info (parm_cons,
7698 INNERMOST_TEMPLATE_ARGS (args),
7699 tf_none, NULL_TREE);
7700 if (parm_cons == error_mark_node)
7701 return false;
7702 }
7703
7704 return subsumes (parm_cons, arg_cons);
7705 }
7706
7707 // Convert a placeholder argument into a binding to the original
7708 // parameter. The original parameter is saved as the TREE_TYPE of
7709 // ARG.
7710 static inline tree
7711 convert_wildcard_argument (tree parm, tree arg)
7712 {
7713 TREE_TYPE (arg) = parm;
7714 return arg;
7715 }
7716
7717 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7718 because one of them is dependent. But we need to represent the
7719 conversion for the benefit of cp_tree_equal. */
7720
7721 static tree
7722 maybe_convert_nontype_argument (tree type, tree arg)
7723 {
7724 /* Auto parms get no conversion. */
7725 if (type_uses_auto (type))
7726 return arg;
7727 /* We don't need or want to add this conversion now if we're going to use the
7728 argument for deduction. */
7729 if (value_dependent_expression_p (arg))
7730 return arg;
7731
7732 type = cv_unqualified (type);
7733 tree argtype = TREE_TYPE (arg);
7734 if (same_type_p (type, argtype))
7735 return arg;
7736
7737 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7738 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7739 return arg;
7740 }
7741
7742 /* Convert the indicated template ARG as necessary to match the
7743 indicated template PARM. Returns the converted ARG, or
7744 error_mark_node if the conversion was unsuccessful. Error and
7745 warning messages are issued under control of COMPLAIN. This
7746 conversion is for the Ith parameter in the parameter list. ARGS is
7747 the full set of template arguments deduced so far. */
7748
7749 static tree
7750 convert_template_argument (tree parm,
7751 tree arg,
7752 tree args,
7753 tsubst_flags_t complain,
7754 int i,
7755 tree in_decl)
7756 {
7757 tree orig_arg;
7758 tree val;
7759 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7760
7761 if (parm == error_mark_node)
7762 return error_mark_node;
7763
7764 /* Trivially convert placeholders. */
7765 if (TREE_CODE (arg) == WILDCARD_DECL)
7766 return convert_wildcard_argument (parm, arg);
7767
7768 if (arg == any_targ_node)
7769 return arg;
7770
7771 if (TREE_CODE (arg) == TREE_LIST
7772 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7773 {
7774 /* The template argument was the name of some
7775 member function. That's usually
7776 invalid, but static members are OK. In any
7777 case, grab the underlying fields/functions
7778 and issue an error later if required. */
7779 orig_arg = TREE_VALUE (arg);
7780 TREE_TYPE (arg) = unknown_type_node;
7781 }
7782
7783 orig_arg = arg;
7784
7785 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7786 requires_type = (TREE_CODE (parm) == TYPE_DECL
7787 || requires_tmpl_type);
7788
7789 /* When determining whether an argument pack expansion is a template,
7790 look at the pattern. */
7791 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7792 arg = PACK_EXPANSION_PATTERN (arg);
7793
7794 /* Deal with an injected-class-name used as a template template arg. */
7795 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7796 {
7797 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7798 if (TREE_CODE (t) == TEMPLATE_DECL)
7799 {
7800 if (cxx_dialect >= cxx11)
7801 /* OK under DR 1004. */;
7802 else if (complain & tf_warning_or_error)
7803 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7804 " used as template template argument", TYPE_NAME (arg));
7805 else if (flag_pedantic_errors)
7806 t = arg;
7807
7808 arg = t;
7809 }
7810 }
7811
7812 is_tmpl_type =
7813 ((TREE_CODE (arg) == TEMPLATE_DECL
7814 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7815 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7816 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7817 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7818
7819 if (is_tmpl_type
7820 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7821 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7822 arg = TYPE_STUB_DECL (arg);
7823
7824 is_type = TYPE_P (arg) || is_tmpl_type;
7825
7826 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7827 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7828 {
7829 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7830 {
7831 if (complain & tf_error)
7832 error ("invalid use of destructor %qE as a type", orig_arg);
7833 return error_mark_node;
7834 }
7835
7836 permerror (input_location,
7837 "to refer to a type member of a template parameter, "
7838 "use %<typename %E%>", orig_arg);
7839
7840 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7841 TREE_OPERAND (arg, 1),
7842 typename_type,
7843 complain);
7844 arg = orig_arg;
7845 is_type = 1;
7846 }
7847 if (is_type != requires_type)
7848 {
7849 if (in_decl)
7850 {
7851 if (complain & tf_error)
7852 {
7853 error ("type/value mismatch at argument %d in template "
7854 "parameter list for %qD",
7855 i + 1, in_decl);
7856 if (is_type)
7857 inform (input_location,
7858 " expected a constant of type %qT, got %qT",
7859 TREE_TYPE (parm),
7860 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7861 else if (requires_tmpl_type)
7862 inform (input_location,
7863 " expected a class template, got %qE", orig_arg);
7864 else
7865 inform (input_location,
7866 " expected a type, got %qE", orig_arg);
7867 }
7868 }
7869 return error_mark_node;
7870 }
7871 if (is_tmpl_type ^ requires_tmpl_type)
7872 {
7873 if (in_decl && (complain & tf_error))
7874 {
7875 error ("type/value mismatch at argument %d in template "
7876 "parameter list for %qD",
7877 i + 1, in_decl);
7878 if (is_tmpl_type)
7879 inform (input_location,
7880 " expected a type, got %qT", DECL_NAME (arg));
7881 else
7882 inform (input_location,
7883 " expected a class template, got %qT", orig_arg);
7884 }
7885 return error_mark_node;
7886 }
7887
7888 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7889 /* We already did the appropriate conversion when packing args. */
7890 val = orig_arg;
7891 else if (is_type)
7892 {
7893 if (requires_tmpl_type)
7894 {
7895 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7896 /* The number of argument required is not known yet.
7897 Just accept it for now. */
7898 val = orig_arg;
7899 else
7900 {
7901 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7902 tree argparm;
7903
7904 /* Strip alias templates that are equivalent to another
7905 template. */
7906 arg = get_underlying_template (arg);
7907 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7908
7909 if (coerce_template_template_parms (parmparm, argparm,
7910 complain, in_decl,
7911 args))
7912 {
7913 val = arg;
7914
7915 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7916 TEMPLATE_DECL. */
7917 if (val != error_mark_node)
7918 {
7919 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7920 val = TREE_TYPE (val);
7921 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7922 val = make_pack_expansion (val, complain);
7923 }
7924 }
7925 else
7926 {
7927 if (in_decl && (complain & tf_error))
7928 {
7929 error ("type/value mismatch at argument %d in "
7930 "template parameter list for %qD",
7931 i + 1, in_decl);
7932 inform (input_location,
7933 " expected a template of type %qD, got %qT",
7934 parm, orig_arg);
7935 }
7936
7937 val = error_mark_node;
7938 }
7939
7940 // Check that the constraints are compatible before allowing the
7941 // substitution.
7942 if (val != error_mark_node)
7943 if (!is_compatible_template_arg (parm, arg))
7944 {
7945 if (in_decl && (complain & tf_error))
7946 {
7947 error ("constraint mismatch at argument %d in "
7948 "template parameter list for %qD",
7949 i + 1, in_decl);
7950 inform (input_location, " expected %qD but got %qD",
7951 parm, arg);
7952 }
7953 val = error_mark_node;
7954 }
7955 }
7956 }
7957 else
7958 val = orig_arg;
7959 /* We only form one instance of each template specialization.
7960 Therefore, if we use a non-canonical variant (i.e., a
7961 typedef), any future messages referring to the type will use
7962 the typedef, which is confusing if those future uses do not
7963 themselves also use the typedef. */
7964 if (TYPE_P (val))
7965 val = canonicalize_type_argument (val, complain);
7966 }
7967 else
7968 {
7969 tree t = TREE_TYPE (parm);
7970
7971 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
7972 > TMPL_ARGS_DEPTH (args))
7973 /* We don't have enough levels of args to do any substitution. This
7974 can happen in the context of -fnew-ttp-matching. */;
7975 else if (tree a = type_uses_auto (t))
7976 {
7977 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7978 if (t == error_mark_node)
7979 return error_mark_node;
7980 }
7981 else
7982 t = tsubst (t, args, complain, in_decl);
7983
7984 if (invalid_nontype_parm_type_p (t, complain))
7985 return error_mark_node;
7986
7987 if (!type_dependent_expression_p (orig_arg)
7988 && !uses_template_parms (t))
7989 /* We used to call digest_init here. However, digest_init
7990 will report errors, which we don't want when complain
7991 is zero. More importantly, digest_init will try too
7992 hard to convert things: for example, `0' should not be
7993 converted to pointer type at this point according to
7994 the standard. Accepting this is not merely an
7995 extension, since deciding whether or not these
7996 conversions can occur is part of determining which
7997 function template to call, or whether a given explicit
7998 argument specification is valid. */
7999 val = convert_nontype_argument (t, orig_arg, complain);
8000 else
8001 {
8002 val = canonicalize_expr_argument (orig_arg, complain);
8003 val = maybe_convert_nontype_argument (t, val);
8004 }
8005
8006
8007 if (val == NULL_TREE)
8008 val = error_mark_node;
8009 else if (val == error_mark_node && (complain & tf_error))
8010 error ("could not convert template argument %qE from %qT to %qT",
8011 orig_arg, TREE_TYPE (orig_arg), t);
8012
8013 if (INDIRECT_REF_P (val))
8014 {
8015 /* Reject template arguments that are references to built-in
8016 functions with no library fallbacks. */
8017 const_tree inner = TREE_OPERAND (val, 0);
8018 const_tree innertype = TREE_TYPE (inner);
8019 if (innertype
8020 && TYPE_REF_P (innertype)
8021 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8022 && TREE_OPERAND_LENGTH (inner) > 0
8023 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8024 return error_mark_node;
8025 }
8026
8027 if (TREE_CODE (val) == SCOPE_REF)
8028 {
8029 /* Strip typedefs from the SCOPE_REF. */
8030 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8031 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8032 complain);
8033 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8034 QUALIFIED_NAME_IS_TEMPLATE (val));
8035 }
8036 }
8037
8038 return val;
8039 }
8040
8041 /* Coerces the remaining template arguments in INNER_ARGS (from
8042 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8043 Returns the coerced argument pack. PARM_IDX is the position of this
8044 parameter in the template parameter list. ARGS is the original
8045 template argument list. */
8046 static tree
8047 coerce_template_parameter_pack (tree parms,
8048 int parm_idx,
8049 tree args,
8050 tree inner_args,
8051 int arg_idx,
8052 tree new_args,
8053 int* lost,
8054 tree in_decl,
8055 tsubst_flags_t complain)
8056 {
8057 tree parm = TREE_VEC_ELT (parms, parm_idx);
8058 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8059 tree packed_args;
8060 tree argument_pack;
8061 tree packed_parms = NULL_TREE;
8062
8063 if (arg_idx > nargs)
8064 arg_idx = nargs;
8065
8066 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8067 {
8068 /* When the template parameter is a non-type template parameter pack
8069 or template template parameter pack whose type or template
8070 parameters use parameter packs, we know exactly how many arguments
8071 we are looking for. Build a vector of the instantiated decls for
8072 these template parameters in PACKED_PARMS. */
8073 /* We can't use make_pack_expansion here because it would interpret a
8074 _DECL as a use rather than a declaration. */
8075 tree decl = TREE_VALUE (parm);
8076 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8077 SET_PACK_EXPANSION_PATTERN (exp, decl);
8078 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8079 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8080
8081 TREE_VEC_LENGTH (args)--;
8082 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8083 TREE_VEC_LENGTH (args)++;
8084
8085 if (packed_parms == error_mark_node)
8086 return error_mark_node;
8087
8088 /* If we're doing a partial instantiation of a member template,
8089 verify that all of the types used for the non-type
8090 template parameter pack are, in fact, valid for non-type
8091 template parameters. */
8092 if (arg_idx < nargs
8093 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8094 {
8095 int j, len = TREE_VEC_LENGTH (packed_parms);
8096 for (j = 0; j < len; ++j)
8097 {
8098 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8099 if (invalid_nontype_parm_type_p (t, complain))
8100 return error_mark_node;
8101 }
8102 /* We don't know how many args we have yet, just
8103 use the unconverted ones for now. */
8104 return NULL_TREE;
8105 }
8106
8107 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8108 }
8109 /* Check if we have a placeholder pack, which indicates we're
8110 in the context of a introduction list. In that case we want
8111 to match this pack to the single placeholder. */
8112 else if (arg_idx < nargs
8113 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8114 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8115 {
8116 nargs = arg_idx + 1;
8117 packed_args = make_tree_vec (1);
8118 }
8119 else
8120 packed_args = make_tree_vec (nargs - arg_idx);
8121
8122 /* Convert the remaining arguments, which will be a part of the
8123 parameter pack "parm". */
8124 int first_pack_arg = arg_idx;
8125 for (; arg_idx < nargs; ++arg_idx)
8126 {
8127 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8128 tree actual_parm = TREE_VALUE (parm);
8129 int pack_idx = arg_idx - first_pack_arg;
8130
8131 if (packed_parms)
8132 {
8133 /* Once we've packed as many args as we have types, stop. */
8134 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8135 break;
8136 else if (PACK_EXPANSION_P (arg))
8137 /* We don't know how many args we have yet, just
8138 use the unconverted ones for now. */
8139 return NULL_TREE;
8140 else
8141 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8142 }
8143
8144 if (arg == error_mark_node)
8145 {
8146 if (complain & tf_error)
8147 error ("template argument %d is invalid", arg_idx + 1);
8148 }
8149 else
8150 arg = convert_template_argument (actual_parm,
8151 arg, new_args, complain, parm_idx,
8152 in_decl);
8153 if (arg == error_mark_node)
8154 (*lost)++;
8155 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8156 }
8157
8158 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8159 && TREE_VEC_LENGTH (packed_args) > 0)
8160 {
8161 if (complain & tf_error)
8162 error ("wrong number of template arguments (%d, should be %d)",
8163 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8164 return error_mark_node;
8165 }
8166
8167 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8168 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8169 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8170 else
8171 {
8172 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8173 TREE_CONSTANT (argument_pack) = 1;
8174 }
8175
8176 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8177 if (CHECKING_P)
8178 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8179 TREE_VEC_LENGTH (packed_args));
8180 return argument_pack;
8181 }
8182
8183 /* Returns the number of pack expansions in the template argument vector
8184 ARGS. */
8185
8186 static int
8187 pack_expansion_args_count (tree args)
8188 {
8189 int i;
8190 int count = 0;
8191 if (args)
8192 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8193 {
8194 tree elt = TREE_VEC_ELT (args, i);
8195 if (elt && PACK_EXPANSION_P (elt))
8196 ++count;
8197 }
8198 return count;
8199 }
8200
8201 /* Convert all template arguments to their appropriate types, and
8202 return a vector containing the innermost resulting template
8203 arguments. If any error occurs, return error_mark_node. Error and
8204 warning messages are issued under control of COMPLAIN.
8205
8206 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8207 for arguments not specified in ARGS. Otherwise, if
8208 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8209 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8210 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8211 ARGS. */
8212
8213 static tree
8214 coerce_template_parms (tree parms,
8215 tree args,
8216 tree in_decl,
8217 tsubst_flags_t complain,
8218 bool require_all_args,
8219 bool use_default_args)
8220 {
8221 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8222 tree orig_inner_args;
8223 tree inner_args;
8224 tree new_args;
8225 tree new_inner_args;
8226 int saved_unevaluated_operand;
8227 int saved_inhibit_evaluation_warnings;
8228
8229 /* When used as a boolean value, indicates whether this is a
8230 variadic template parameter list. Since it's an int, we can also
8231 subtract it from nparms to get the number of non-variadic
8232 parameters. */
8233 int variadic_p = 0;
8234 int variadic_args_p = 0;
8235 int post_variadic_parms = 0;
8236
8237 /* Adjustment to nparms for fixed parameter packs. */
8238 int fixed_pack_adjust = 0;
8239 int fixed_packs = 0;
8240 int missing = 0;
8241
8242 /* Likewise for parameters with default arguments. */
8243 int default_p = 0;
8244
8245 if (args == error_mark_node)
8246 return error_mark_node;
8247
8248 nparms = TREE_VEC_LENGTH (parms);
8249
8250 /* Determine if there are any parameter packs or default arguments. */
8251 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8252 {
8253 tree parm = TREE_VEC_ELT (parms, parm_idx);
8254 if (variadic_p)
8255 ++post_variadic_parms;
8256 if (template_parameter_pack_p (TREE_VALUE (parm)))
8257 ++variadic_p;
8258 if (TREE_PURPOSE (parm))
8259 ++default_p;
8260 }
8261
8262 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8263 /* If there are no parameters that follow a parameter pack, we need to
8264 expand any argument packs so that we can deduce a parameter pack from
8265 some non-packed args followed by an argument pack, as in variadic85.C.
8266 If there are such parameters, we need to leave argument packs intact
8267 so the arguments are assigned properly. This can happen when dealing
8268 with a nested class inside a partial specialization of a class
8269 template, as in variadic92.C, or when deducing a template parameter pack
8270 from a sub-declarator, as in variadic114.C. */
8271 if (!post_variadic_parms)
8272 inner_args = expand_template_argument_pack (inner_args);
8273
8274 /* Count any pack expansion args. */
8275 variadic_args_p = pack_expansion_args_count (inner_args);
8276
8277 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8278 if ((nargs - variadic_args_p > nparms && !variadic_p)
8279 || (nargs < nparms - variadic_p
8280 && require_all_args
8281 && !variadic_args_p
8282 && (!use_default_args
8283 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8284 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8285 {
8286 bad_nargs:
8287 if (complain & tf_error)
8288 {
8289 if (variadic_p || default_p)
8290 {
8291 nparms -= variadic_p + default_p;
8292 error ("wrong number of template arguments "
8293 "(%d, should be at least %d)", nargs, nparms);
8294 }
8295 else
8296 error ("wrong number of template arguments "
8297 "(%d, should be %d)", nargs, nparms);
8298
8299 if (in_decl)
8300 inform (DECL_SOURCE_LOCATION (in_decl),
8301 "provided for %qD", in_decl);
8302 }
8303
8304 return error_mark_node;
8305 }
8306 /* We can't pass a pack expansion to a non-pack parameter of an alias
8307 template (DR 1430). */
8308 else if (in_decl
8309 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8310 || concept_template_p (in_decl))
8311 && variadic_args_p
8312 && nargs - variadic_args_p < nparms - variadic_p)
8313 {
8314 if (complain & tf_error)
8315 {
8316 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8317 {
8318 tree arg = TREE_VEC_ELT (inner_args, i);
8319 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8320
8321 if (PACK_EXPANSION_P (arg)
8322 && !template_parameter_pack_p (parm))
8323 {
8324 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8325 error_at (location_of (arg),
8326 "pack expansion argument for non-pack parameter "
8327 "%qD of alias template %qD", parm, in_decl);
8328 else
8329 error_at (location_of (arg),
8330 "pack expansion argument for non-pack parameter "
8331 "%qD of concept %qD", parm, in_decl);
8332 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8333 goto found;
8334 }
8335 }
8336 gcc_unreachable ();
8337 found:;
8338 }
8339 return error_mark_node;
8340 }
8341
8342 /* We need to evaluate the template arguments, even though this
8343 template-id may be nested within a "sizeof". */
8344 saved_unevaluated_operand = cp_unevaluated_operand;
8345 cp_unevaluated_operand = 0;
8346 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8347 c_inhibit_evaluation_warnings = 0;
8348 new_inner_args = make_tree_vec (nparms);
8349 new_args = add_outermost_template_args (args, new_inner_args);
8350 int pack_adjust = 0;
8351 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8352 {
8353 tree arg;
8354 tree parm;
8355
8356 /* Get the Ith template parameter. */
8357 parm = TREE_VEC_ELT (parms, parm_idx);
8358
8359 if (parm == error_mark_node)
8360 {
8361 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8362 continue;
8363 }
8364
8365 /* Calculate the next argument. */
8366 if (arg_idx < nargs)
8367 arg = TREE_VEC_ELT (inner_args, arg_idx);
8368 else
8369 arg = NULL_TREE;
8370
8371 if (template_parameter_pack_p (TREE_VALUE (parm))
8372 && !(arg && ARGUMENT_PACK_P (arg)))
8373 {
8374 /* Some arguments will be placed in the
8375 template parameter pack PARM. */
8376 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8377 inner_args, arg_idx,
8378 new_args, &lost,
8379 in_decl, complain);
8380
8381 if (arg == NULL_TREE)
8382 {
8383 /* We don't know how many args we have yet, just use the
8384 unconverted (and still packed) ones for now. */
8385 new_inner_args = orig_inner_args;
8386 arg_idx = nargs;
8387 break;
8388 }
8389
8390 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8391
8392 /* Store this argument. */
8393 if (arg == error_mark_node)
8394 {
8395 lost++;
8396 /* We are done with all of the arguments. */
8397 arg_idx = nargs;
8398 break;
8399 }
8400 else
8401 {
8402 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8403 arg_idx += pack_adjust;
8404 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8405 {
8406 ++fixed_packs;
8407 fixed_pack_adjust += pack_adjust;
8408 }
8409 }
8410
8411 continue;
8412 }
8413 else if (arg)
8414 {
8415 if (PACK_EXPANSION_P (arg))
8416 {
8417 /* "If every valid specialization of a variadic template
8418 requires an empty template parameter pack, the template is
8419 ill-formed, no diagnostic required." So check that the
8420 pattern works with this parameter. */
8421 tree pattern = PACK_EXPANSION_PATTERN (arg);
8422 tree conv = convert_template_argument (TREE_VALUE (parm),
8423 pattern, new_args,
8424 complain, parm_idx,
8425 in_decl);
8426 if (conv == error_mark_node)
8427 {
8428 if (complain & tf_error)
8429 inform (input_location, "so any instantiation with a "
8430 "non-empty parameter pack would be ill-formed");
8431 ++lost;
8432 }
8433 else if (TYPE_P (conv) && !TYPE_P (pattern))
8434 /* Recover from missing typename. */
8435 TREE_VEC_ELT (inner_args, arg_idx)
8436 = make_pack_expansion (conv, complain);
8437
8438 /* We don't know how many args we have yet, just
8439 use the unconverted ones for now. */
8440 new_inner_args = inner_args;
8441 arg_idx = nargs;
8442 break;
8443 }
8444 }
8445 else if (require_all_args)
8446 {
8447 /* There must be a default arg in this case. */
8448 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8449 complain, in_decl);
8450 /* The position of the first default template argument,
8451 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8452 Record that. */
8453 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8454 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8455 arg_idx - pack_adjust);
8456 }
8457 else
8458 break;
8459
8460 if (arg == error_mark_node)
8461 {
8462 if (complain & tf_error)
8463 error ("template argument %d is invalid", arg_idx + 1);
8464 }
8465 else if (!arg)
8466 {
8467 /* This can occur if there was an error in the template
8468 parameter list itself (which we would already have
8469 reported) that we are trying to recover from, e.g., a class
8470 template with a parameter list such as
8471 template<typename..., typename> (cpp0x/variadic150.C). */
8472 ++lost;
8473
8474 /* This can also happen with a fixed parameter pack (71834). */
8475 if (arg_idx >= nargs)
8476 ++missing;
8477 }
8478 else
8479 arg = convert_template_argument (TREE_VALUE (parm),
8480 arg, new_args, complain,
8481 parm_idx, in_decl);
8482
8483 if (arg == error_mark_node)
8484 lost++;
8485 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8486 }
8487 cp_unevaluated_operand = saved_unevaluated_operand;
8488 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8489
8490 if (missing || arg_idx < nargs - variadic_args_p)
8491 {
8492 /* If we had fixed parameter packs, we didn't know how many arguments we
8493 actually needed earlier; now we do. */
8494 nparms += fixed_pack_adjust;
8495 variadic_p -= fixed_packs;
8496 goto bad_nargs;
8497 }
8498
8499 if (arg_idx < nargs)
8500 {
8501 /* We had some pack expansion arguments that will only work if the packs
8502 are empty, but wait until instantiation time to complain.
8503 See variadic-ttp3.C. */
8504 int len = nparms + (nargs - arg_idx);
8505 tree args = make_tree_vec (len);
8506 int i = 0;
8507 for (; i < nparms; ++i)
8508 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8509 for (; i < len; ++i, ++arg_idx)
8510 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8511 arg_idx - pack_adjust);
8512 new_inner_args = args;
8513 }
8514
8515 if (lost)
8516 {
8517 gcc_assert (!(complain & tf_error) || seen_error ());
8518 return error_mark_node;
8519 }
8520
8521 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8522 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8523 TREE_VEC_LENGTH (new_inner_args));
8524
8525 return new_inner_args;
8526 }
8527
8528 /* Convert all template arguments to their appropriate types, and
8529 return a vector containing the innermost resulting template
8530 arguments. If any error occurs, return error_mark_node. Error and
8531 warning messages are not issued.
8532
8533 Note that no function argument deduction is performed, and default
8534 arguments are used to fill in unspecified arguments. */
8535 tree
8536 coerce_template_parms (tree parms, tree args, tree in_decl)
8537 {
8538 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8539 }
8540
8541 /* Convert all template arguments to their appropriate type, and
8542 instantiate default arguments as needed. This returns a vector
8543 containing the innermost resulting template arguments, or
8544 error_mark_node if unsuccessful. */
8545 tree
8546 coerce_template_parms (tree parms, tree args, tree in_decl,
8547 tsubst_flags_t complain)
8548 {
8549 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8550 }
8551
8552 /* Like coerce_template_parms. If PARMS represents all template
8553 parameters levels, this function returns a vector of vectors
8554 representing all the resulting argument levels. Note that in this
8555 case, only the innermost arguments are coerced because the
8556 outermost ones are supposed to have been coerced already.
8557
8558 Otherwise, if PARMS represents only (the innermost) vector of
8559 parameters, this function returns a vector containing just the
8560 innermost resulting arguments. */
8561
8562 static tree
8563 coerce_innermost_template_parms (tree parms,
8564 tree args,
8565 tree in_decl,
8566 tsubst_flags_t complain,
8567 bool require_all_args,
8568 bool use_default_args)
8569 {
8570 int parms_depth = TMPL_PARMS_DEPTH (parms);
8571 int args_depth = TMPL_ARGS_DEPTH (args);
8572 tree coerced_args;
8573
8574 if (parms_depth > 1)
8575 {
8576 coerced_args = make_tree_vec (parms_depth);
8577 tree level;
8578 int cur_depth;
8579
8580 for (level = parms, cur_depth = parms_depth;
8581 parms_depth > 0 && level != NULL_TREE;
8582 level = TREE_CHAIN (level), --cur_depth)
8583 {
8584 tree l;
8585 if (cur_depth == args_depth)
8586 l = coerce_template_parms (TREE_VALUE (level),
8587 args, in_decl, complain,
8588 require_all_args,
8589 use_default_args);
8590 else
8591 l = TMPL_ARGS_LEVEL (args, cur_depth);
8592
8593 if (l == error_mark_node)
8594 return error_mark_node;
8595
8596 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8597 }
8598 }
8599 else
8600 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8601 args, in_decl, complain,
8602 require_all_args,
8603 use_default_args);
8604 return coerced_args;
8605 }
8606
8607 /* Returns 1 if template args OT and NT are equivalent. */
8608
8609 int
8610 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8611 {
8612 if (nt == ot)
8613 return 1;
8614 if (nt == NULL_TREE || ot == NULL_TREE)
8615 return false;
8616 if (nt == any_targ_node || ot == any_targ_node)
8617 return true;
8618
8619 if (TREE_CODE (nt) == TREE_VEC)
8620 /* For member templates */
8621 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8622 else if (PACK_EXPANSION_P (ot))
8623 return (PACK_EXPANSION_P (nt)
8624 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8625 PACK_EXPANSION_PATTERN (nt))
8626 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8627 PACK_EXPANSION_EXTRA_ARGS (nt)));
8628 else if (ARGUMENT_PACK_P (ot))
8629 {
8630 int i, len;
8631 tree opack, npack;
8632
8633 if (!ARGUMENT_PACK_P (nt))
8634 return 0;
8635
8636 opack = ARGUMENT_PACK_ARGS (ot);
8637 npack = ARGUMENT_PACK_ARGS (nt);
8638 len = TREE_VEC_LENGTH (opack);
8639 if (TREE_VEC_LENGTH (npack) != len)
8640 return 0;
8641 for (i = 0; i < len; ++i)
8642 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8643 TREE_VEC_ELT (npack, i)))
8644 return 0;
8645 return 1;
8646 }
8647 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8648 gcc_unreachable ();
8649 else if (TYPE_P (nt))
8650 {
8651 if (!TYPE_P (ot))
8652 return false;
8653 /* Don't treat an alias template specialization with dependent
8654 arguments as equivalent to its underlying type when used as a
8655 template argument; we need them to be distinct so that we
8656 substitute into the specialization arguments at instantiation
8657 time. And aliases can't be equivalent without being ==, so
8658 we don't need to look any deeper.
8659
8660 During partial ordering, however, we need to treat them normally so
8661 that we can order uses of the same alias with different
8662 cv-qualification (79960). */
8663 if (!partial_order
8664 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8665 return false;
8666 else
8667 return same_type_p (ot, nt);
8668 }
8669 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8670 return 0;
8671 else
8672 {
8673 /* Try to treat a template non-type argument that has been converted
8674 to the parameter type as equivalent to one that hasn't yet. */
8675 for (enum tree_code code1 = TREE_CODE (ot);
8676 CONVERT_EXPR_CODE_P (code1)
8677 || code1 == NON_LVALUE_EXPR;
8678 code1 = TREE_CODE (ot))
8679 ot = TREE_OPERAND (ot, 0);
8680 for (enum tree_code code2 = TREE_CODE (nt);
8681 CONVERT_EXPR_CODE_P (code2)
8682 || code2 == NON_LVALUE_EXPR;
8683 code2 = TREE_CODE (nt))
8684 nt = TREE_OPERAND (nt, 0);
8685
8686 return cp_tree_equal (ot, nt);
8687 }
8688 }
8689
8690 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8691 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8692 NEWARG_PTR with the offending arguments if they are non-NULL. */
8693
8694 int
8695 comp_template_args (tree oldargs, tree newargs,
8696 tree *oldarg_ptr, tree *newarg_ptr,
8697 bool partial_order)
8698 {
8699 int i;
8700
8701 if (oldargs == newargs)
8702 return 1;
8703
8704 if (!oldargs || !newargs)
8705 return 0;
8706
8707 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8708 return 0;
8709
8710 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8711 {
8712 tree nt = TREE_VEC_ELT (newargs, i);
8713 tree ot = TREE_VEC_ELT (oldargs, i);
8714
8715 if (! template_args_equal (ot, nt, partial_order))
8716 {
8717 if (oldarg_ptr != NULL)
8718 *oldarg_ptr = ot;
8719 if (newarg_ptr != NULL)
8720 *newarg_ptr = nt;
8721 return 0;
8722 }
8723 }
8724 return 1;
8725 }
8726
8727 inline bool
8728 comp_template_args_porder (tree oargs, tree nargs)
8729 {
8730 return comp_template_args (oargs, nargs, NULL, NULL, true);
8731 }
8732
8733 /* Implement a freelist interface for objects of type T.
8734
8735 Head is a separate object, rather than a regular member, so that we
8736 can define it as a GTY deletable pointer, which is highly
8737 desirable. A data member could be declared that way, but then the
8738 containing object would implicitly get GTY((user)), which would
8739 prevent us from instantiating freelists as global objects.
8740 Although this way we can create freelist global objects, they're
8741 such thin wrappers that instantiating temporaries at every use
8742 loses nothing and saves permanent storage for the freelist object.
8743
8744 Member functions next, anew, poison and reinit have default
8745 implementations that work for most of the types we're interested
8746 in, but if they don't work for some type, they should be explicitly
8747 specialized. See the comments before them for requirements, and
8748 the example specializations for the tree_list_freelist. */
8749 template <typename T>
8750 class freelist
8751 {
8752 /* Return the next object in a chain. We could just do type
8753 punning, but if we access the object with its underlying type, we
8754 avoid strict-aliasing trouble. This needs only work between
8755 poison and reinit. */
8756 static T *&next (T *obj) { return obj->next; }
8757
8758 /* Return a newly allocated, uninitialized or minimally-initialized
8759 object of type T. Any initialization performed by anew should
8760 either remain across the life of the object and the execution of
8761 poison, or be redone by reinit. */
8762 static T *anew () { return ggc_alloc<T> (); }
8763
8764 /* Optionally scribble all over the bits holding the object, so that
8765 they become (mostly?) uninitialized memory. This is called while
8766 preparing to make the object part of the free list. */
8767 static void poison (T *obj) {
8768 T *p ATTRIBUTE_UNUSED = obj;
8769 T **q ATTRIBUTE_UNUSED = &next (obj);
8770
8771 #ifdef ENABLE_GC_CHECKING
8772 /* Poison the data, to indicate the data is garbage. */
8773 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8774 memset (p, 0xa5, sizeof (*p));
8775 #endif
8776 /* Let valgrind know the object is free. */
8777 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8778
8779 /* Let valgrind know the next portion of the object is available,
8780 but uninitialized. */
8781 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8782 }
8783
8784 /* Bring an object that underwent at least one lifecycle after anew
8785 and before the most recent free and poison, back to a usable
8786 state, reinitializing whatever is needed for it to be
8787 functionally equivalent to an object just allocated and returned
8788 by anew. This may poison or clear the next field, used by
8789 freelist housekeeping after poison was called. */
8790 static void reinit (T *obj) {
8791 T **q ATTRIBUTE_UNUSED = &next (obj);
8792
8793 #ifdef ENABLE_GC_CHECKING
8794 memset (q, 0xa5, sizeof (*q));
8795 #endif
8796 /* Let valgrind know the entire object is available, but
8797 uninitialized. */
8798 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8799 }
8800
8801 /* Reference a GTY-deletable pointer that points to the first object
8802 in the free list proper. */
8803 T *&head;
8804 public:
8805 /* Construct a freelist object chaining objects off of HEAD. */
8806 freelist (T *&head) : head(head) {}
8807
8808 /* Add OBJ to the free object list. The former head becomes OBJ's
8809 successor. */
8810 void free (T *obj)
8811 {
8812 poison (obj);
8813 next (obj) = head;
8814 head = obj;
8815 }
8816
8817 /* Take an object from the free list, if one is available, or
8818 allocate a new one. Objects taken from the free list should be
8819 regarded as filled with garbage, except for bits that are
8820 configured to be preserved across free and alloc. */
8821 T *alloc ()
8822 {
8823 if (head)
8824 {
8825 T *obj = head;
8826 head = next (head);
8827 reinit (obj);
8828 return obj;
8829 }
8830 else
8831 return anew ();
8832 }
8833 };
8834
8835 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8836 want to allocate a TREE_LIST using the usual interface, and ensure
8837 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8838 build_tree_list logic in reinit, so this could go out of sync. */
8839 template <>
8840 inline tree &
8841 freelist<tree_node>::next (tree obj)
8842 {
8843 return TREE_CHAIN (obj);
8844 }
8845 template <>
8846 inline tree
8847 freelist<tree_node>::anew ()
8848 {
8849 return build_tree_list (NULL, NULL);
8850 }
8851 template <>
8852 inline void
8853 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8854 {
8855 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8856 tree p ATTRIBUTE_UNUSED = obj;
8857 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8858 tree *q ATTRIBUTE_UNUSED = &next (obj);
8859
8860 #ifdef ENABLE_GC_CHECKING
8861 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8862
8863 /* Poison the data, to indicate the data is garbage. */
8864 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8865 memset (p, 0xa5, size);
8866 #endif
8867 /* Let valgrind know the object is free. */
8868 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8869 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8870 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8871 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8872
8873 #ifdef ENABLE_GC_CHECKING
8874 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8875 /* Keep TREE_CHAIN functional. */
8876 TREE_SET_CODE (obj, TREE_LIST);
8877 #else
8878 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8879 #endif
8880 }
8881 template <>
8882 inline void
8883 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8884 {
8885 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8886
8887 #ifdef ENABLE_GC_CHECKING
8888 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8889 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8890 memset (obj, 0, sizeof (tree_list));
8891 #endif
8892
8893 /* Let valgrind know the entire object is available, but
8894 uninitialized. */
8895 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8896
8897 #ifdef ENABLE_GC_CHECKING
8898 TREE_SET_CODE (obj, TREE_LIST);
8899 #else
8900 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8901 #endif
8902 }
8903
8904 /* Point to the first object in the TREE_LIST freelist. */
8905 static GTY((deletable)) tree tree_list_freelist_head;
8906 /* Return the/an actual TREE_LIST freelist. */
8907 static inline freelist<tree_node>
8908 tree_list_freelist ()
8909 {
8910 return tree_list_freelist_head;
8911 }
8912
8913 /* Point to the first object in the tinst_level freelist. */
8914 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
8915 /* Return the/an actual tinst_level freelist. */
8916 static inline freelist<tinst_level>
8917 tinst_level_freelist ()
8918 {
8919 return tinst_level_freelist_head;
8920 }
8921
8922 /* Point to the first object in the pending_template freelist. */
8923 static GTY((deletable)) pending_template *pending_template_freelist_head;
8924 /* Return the/an actual pending_template freelist. */
8925 static inline freelist<pending_template>
8926 pending_template_freelist ()
8927 {
8928 return pending_template_freelist_head;
8929 }
8930
8931 /* Build the TREE_LIST object out of a split list, store it
8932 permanently, and return it. */
8933 tree
8934 tinst_level::to_list ()
8935 {
8936 gcc_assert (split_list_p ());
8937 tree ret = tree_list_freelist ().alloc ();
8938 TREE_PURPOSE (ret) = tldcl;
8939 TREE_VALUE (ret) = targs;
8940 tldcl = ret;
8941 targs = NULL;
8942 gcc_assert (tree_list_p ());
8943 return ret;
8944 }
8945
8946 const unsigned short tinst_level::refcount_infinity;
8947
8948 /* Increment OBJ's refcount unless it is already infinite. */
8949 static tinst_level *
8950 inc_refcount_use (tinst_level *obj)
8951 {
8952 if (obj && obj->refcount != tinst_level::refcount_infinity)
8953 ++obj->refcount;
8954 return obj;
8955 }
8956
8957 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8958 void
8959 tinst_level::free (tinst_level *obj)
8960 {
8961 if (obj->tree_list_p ())
8962 tree_list_freelist ().free (obj->get_node ());
8963 tinst_level_freelist ().free (obj);
8964 }
8965
8966 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
8967 OBJ's DECL and OBJ, and start over with the tinst_level object that
8968 used to be referenced by OBJ's NEXT. */
8969 static void
8970 dec_refcount_use (tinst_level *obj)
8971 {
8972 while (obj
8973 && obj->refcount != tinst_level::refcount_infinity
8974 && !--obj->refcount)
8975 {
8976 tinst_level *next = obj->next;
8977 tinst_level::free (obj);
8978 obj = next;
8979 }
8980 }
8981
8982 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
8983 and of the former PTR. Omitting the second argument is equivalent
8984 to passing (T*)NULL; this is allowed because passing the
8985 zero-valued integral constant NULL confuses type deduction and/or
8986 overload resolution. */
8987 template <typename T>
8988 static void
8989 set_refcount_ptr (T *& ptr, T *obj = NULL)
8990 {
8991 T *save = ptr;
8992 ptr = inc_refcount_use (obj);
8993 dec_refcount_use (save);
8994 }
8995
8996 static void
8997 add_pending_template (tree d)
8998 {
8999 tree ti = (TYPE_P (d)
9000 ? CLASSTYPE_TEMPLATE_INFO (d)
9001 : DECL_TEMPLATE_INFO (d));
9002 struct pending_template *pt;
9003 int level;
9004
9005 if (TI_PENDING_TEMPLATE_FLAG (ti))
9006 return;
9007
9008 /* We are called both from instantiate_decl, where we've already had a
9009 tinst_level pushed, and instantiate_template, where we haven't.
9010 Compensate. */
9011 gcc_assert (TREE_CODE (d) != TREE_LIST);
9012 level = !current_tinst_level
9013 || current_tinst_level->maybe_get_node () != d;
9014
9015 if (level)
9016 push_tinst_level (d);
9017
9018 pt = pending_template_freelist ().alloc ();
9019 pt->next = NULL;
9020 pt->tinst = NULL;
9021 set_refcount_ptr (pt->tinst, current_tinst_level);
9022 if (last_pending_template)
9023 last_pending_template->next = pt;
9024 else
9025 pending_templates = pt;
9026
9027 last_pending_template = pt;
9028
9029 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9030
9031 if (level)
9032 pop_tinst_level ();
9033 }
9034
9035
9036 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9037 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9038 documentation for TEMPLATE_ID_EXPR. */
9039
9040 tree
9041 lookup_template_function (tree fns, tree arglist)
9042 {
9043 tree type;
9044
9045 if (fns == error_mark_node || arglist == error_mark_node)
9046 return error_mark_node;
9047
9048 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9049
9050 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9051 {
9052 error ("%q#D is not a function template", fns);
9053 return error_mark_node;
9054 }
9055
9056 if (BASELINK_P (fns))
9057 {
9058 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9059 unknown_type_node,
9060 BASELINK_FUNCTIONS (fns),
9061 arglist);
9062 return fns;
9063 }
9064
9065 type = TREE_TYPE (fns);
9066 if (TREE_CODE (fns) == OVERLOAD || !type)
9067 type = unknown_type_node;
9068
9069 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
9070 }
9071
9072 /* Within the scope of a template class S<T>, the name S gets bound
9073 (in build_self_reference) to a TYPE_DECL for the class, not a
9074 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9075 or one of its enclosing classes, and that type is a template,
9076 return the associated TEMPLATE_DECL. Otherwise, the original
9077 DECL is returned.
9078
9079 Also handle the case when DECL is a TREE_LIST of ambiguous
9080 injected-class-names from different bases. */
9081
9082 tree
9083 maybe_get_template_decl_from_type_decl (tree decl)
9084 {
9085 if (decl == NULL_TREE)
9086 return decl;
9087
9088 /* DR 176: A lookup that finds an injected-class-name (10.2
9089 [class.member.lookup]) can result in an ambiguity in certain cases
9090 (for example, if it is found in more than one base class). If all of
9091 the injected-class-names that are found refer to specializations of
9092 the same class template, and if the name is followed by a
9093 template-argument-list, the reference refers to the class template
9094 itself and not a specialization thereof, and is not ambiguous. */
9095 if (TREE_CODE (decl) == TREE_LIST)
9096 {
9097 tree t, tmpl = NULL_TREE;
9098 for (t = decl; t; t = TREE_CHAIN (t))
9099 {
9100 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9101 if (!tmpl)
9102 tmpl = elt;
9103 else if (tmpl != elt)
9104 break;
9105 }
9106 if (tmpl && t == NULL_TREE)
9107 return tmpl;
9108 else
9109 return decl;
9110 }
9111
9112 return (decl != NULL_TREE
9113 && DECL_SELF_REFERENCE_P (decl)
9114 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9115 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9116 }
9117
9118 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9119 parameters, find the desired type.
9120
9121 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9122
9123 IN_DECL, if non-NULL, is the template declaration we are trying to
9124 instantiate.
9125
9126 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9127 the class we are looking up.
9128
9129 Issue error and warning messages under control of COMPLAIN.
9130
9131 If the template class is really a local class in a template
9132 function, then the FUNCTION_CONTEXT is the function in which it is
9133 being instantiated.
9134
9135 ??? Note that this function is currently called *twice* for each
9136 template-id: the first time from the parser, while creating the
9137 incomplete type (finish_template_type), and the second type during the
9138 real instantiation (instantiate_template_class). This is surely something
9139 that we want to avoid. It also causes some problems with argument
9140 coercion (see convert_nontype_argument for more information on this). */
9141
9142 static tree
9143 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9144 int entering_scope, tsubst_flags_t complain)
9145 {
9146 tree templ = NULL_TREE, parmlist;
9147 tree t;
9148 spec_entry **slot;
9149 spec_entry *entry;
9150 spec_entry elt;
9151 hashval_t hash;
9152
9153 if (identifier_p (d1))
9154 {
9155 tree value = innermost_non_namespace_value (d1);
9156 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9157 templ = value;
9158 else
9159 {
9160 if (context)
9161 push_decl_namespace (context);
9162 templ = lookup_name (d1);
9163 templ = maybe_get_template_decl_from_type_decl (templ);
9164 if (context)
9165 pop_decl_namespace ();
9166 }
9167 if (templ)
9168 context = DECL_CONTEXT (templ);
9169 }
9170 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9171 {
9172 tree type = TREE_TYPE (d1);
9173
9174 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9175 an implicit typename for the second A. Deal with it. */
9176 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9177 type = TREE_TYPE (type);
9178
9179 if (CLASSTYPE_TEMPLATE_INFO (type))
9180 {
9181 templ = CLASSTYPE_TI_TEMPLATE (type);
9182 d1 = DECL_NAME (templ);
9183 }
9184 }
9185 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9186 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9187 {
9188 templ = TYPE_TI_TEMPLATE (d1);
9189 d1 = DECL_NAME (templ);
9190 }
9191 else if (DECL_TYPE_TEMPLATE_P (d1))
9192 {
9193 templ = d1;
9194 d1 = DECL_NAME (templ);
9195 context = DECL_CONTEXT (templ);
9196 }
9197 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9198 {
9199 templ = d1;
9200 d1 = DECL_NAME (templ);
9201 }
9202
9203 /* Issue an error message if we didn't find a template. */
9204 if (! templ)
9205 {
9206 if (complain & tf_error)
9207 error ("%qT is not a template", d1);
9208 return error_mark_node;
9209 }
9210
9211 if (TREE_CODE (templ) != TEMPLATE_DECL
9212 /* Make sure it's a user visible template, if it was named by
9213 the user. */
9214 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9215 && !PRIMARY_TEMPLATE_P (templ)))
9216 {
9217 if (complain & tf_error)
9218 {
9219 error ("non-template type %qT used as a template", d1);
9220 if (in_decl)
9221 error ("for template declaration %q+D", in_decl);
9222 }
9223 return error_mark_node;
9224 }
9225
9226 complain &= ~tf_user;
9227
9228 /* An alias that just changes the name of a template is equivalent to the
9229 other template, so if any of the arguments are pack expansions, strip
9230 the alias to avoid problems with a pack expansion passed to a non-pack
9231 alias template parameter (DR 1430). */
9232 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9233 templ = get_underlying_template (templ);
9234
9235 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9236 {
9237 tree parm;
9238 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9239 if (arglist2 == error_mark_node
9240 || (!uses_template_parms (arglist2)
9241 && check_instantiated_args (templ, arglist2, complain)))
9242 return error_mark_node;
9243
9244 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9245 return parm;
9246 }
9247 else
9248 {
9249 tree template_type = TREE_TYPE (templ);
9250 tree gen_tmpl;
9251 tree type_decl;
9252 tree found = NULL_TREE;
9253 int arg_depth;
9254 int parm_depth;
9255 int is_dependent_type;
9256 int use_partial_inst_tmpl = false;
9257
9258 if (template_type == error_mark_node)
9259 /* An error occurred while building the template TEMPL, and a
9260 diagnostic has most certainly been emitted for that
9261 already. Let's propagate that error. */
9262 return error_mark_node;
9263
9264 gen_tmpl = most_general_template (templ);
9265 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9266 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9267 arg_depth = TMPL_ARGS_DEPTH (arglist);
9268
9269 if (arg_depth == 1 && parm_depth > 1)
9270 {
9271 /* We've been given an incomplete set of template arguments.
9272 For example, given:
9273
9274 template <class T> struct S1 {
9275 template <class U> struct S2 {};
9276 template <class U> struct S2<U*> {};
9277 };
9278
9279 we will be called with an ARGLIST of `U*', but the
9280 TEMPLATE will be `template <class T> template
9281 <class U> struct S1<T>::S2'. We must fill in the missing
9282 arguments. */
9283 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9284 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9285 arg_depth = TMPL_ARGS_DEPTH (arglist);
9286 }
9287
9288 /* Now we should have enough arguments. */
9289 gcc_assert (parm_depth == arg_depth);
9290
9291 /* From here on, we're only interested in the most general
9292 template. */
9293
9294 /* Calculate the BOUND_ARGS. These will be the args that are
9295 actually tsubst'd into the definition to create the
9296 instantiation. */
9297 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9298 complain,
9299 /*require_all_args=*/true,
9300 /*use_default_args=*/true);
9301
9302 if (arglist == error_mark_node)
9303 /* We were unable to bind the arguments. */
9304 return error_mark_node;
9305
9306 /* In the scope of a template class, explicit references to the
9307 template class refer to the type of the template, not any
9308 instantiation of it. For example, in:
9309
9310 template <class T> class C { void f(C<T>); }
9311
9312 the `C<T>' is just the same as `C'. Outside of the
9313 class, however, such a reference is an instantiation. */
9314 if (entering_scope
9315 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9316 || currently_open_class (template_type))
9317 {
9318 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9319
9320 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9321 return template_type;
9322 }
9323
9324 /* If we already have this specialization, return it. */
9325 elt.tmpl = gen_tmpl;
9326 elt.args = arglist;
9327 elt.spec = NULL_TREE;
9328 hash = spec_hasher::hash (&elt);
9329 entry = type_specializations->find_with_hash (&elt, hash);
9330
9331 if (entry)
9332 return entry->spec;
9333
9334 /* If the the template's constraints are not satisfied,
9335 then we cannot form a valid type.
9336
9337 Note that the check is deferred until after the hash
9338 lookup. This prevents redundant checks on previously
9339 instantiated specializations. */
9340 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9341 {
9342 if (complain & tf_error)
9343 {
9344 error ("template constraint failure");
9345 diagnose_constraints (input_location, gen_tmpl, arglist);
9346 }
9347 return error_mark_node;
9348 }
9349
9350 is_dependent_type = uses_template_parms (arglist);
9351
9352 /* If the deduced arguments are invalid, then the binding
9353 failed. */
9354 if (!is_dependent_type
9355 && check_instantiated_args (gen_tmpl,
9356 INNERMOST_TEMPLATE_ARGS (arglist),
9357 complain))
9358 return error_mark_node;
9359
9360 if (!is_dependent_type
9361 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9362 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9363 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9364 {
9365 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9366 DECL_NAME (gen_tmpl),
9367 /*tag_scope=*/ts_global);
9368 return found;
9369 }
9370
9371 context = DECL_CONTEXT (gen_tmpl);
9372 if (context && TYPE_P (context))
9373 {
9374 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9375 context = complete_type (context);
9376 }
9377 else
9378 context = tsubst (context, arglist, complain, in_decl);
9379
9380 if (context == error_mark_node)
9381 return error_mark_node;
9382
9383 if (!context)
9384 context = global_namespace;
9385
9386 /* Create the type. */
9387 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9388 {
9389 /* The user referred to a specialization of an alias
9390 template represented by GEN_TMPL.
9391
9392 [temp.alias]/2 says:
9393
9394 When a template-id refers to the specialization of an
9395 alias template, it is equivalent to the associated
9396 type obtained by substitution of its
9397 template-arguments for the template-parameters in the
9398 type-id of the alias template. */
9399
9400 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9401 /* Note that the call above (by indirectly calling
9402 register_specialization in tsubst_decl) registers the
9403 TYPE_DECL representing the specialization of the alias
9404 template. So next time someone substitutes ARGLIST for
9405 the template parms into the alias template (GEN_TMPL),
9406 she'll get that TYPE_DECL back. */
9407
9408 if (t == error_mark_node)
9409 return t;
9410 }
9411 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9412 {
9413 if (!is_dependent_type)
9414 {
9415 set_current_access_from_decl (TYPE_NAME (template_type));
9416 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9417 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9418 arglist, complain, in_decl),
9419 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9420 arglist, complain, in_decl),
9421 SCOPED_ENUM_P (template_type), NULL);
9422
9423 if (t == error_mark_node)
9424 return t;
9425 }
9426 else
9427 {
9428 /* We don't want to call start_enum for this type, since
9429 the values for the enumeration constants may involve
9430 template parameters. And, no one should be interested
9431 in the enumeration constants for such a type. */
9432 t = cxx_make_type (ENUMERAL_TYPE);
9433 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9434 }
9435 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9436 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9437 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9438 }
9439 else if (CLASS_TYPE_P (template_type))
9440 {
9441 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9442 instantiated here. */
9443 gcc_assert (!LAMBDA_TYPE_P (template_type));
9444
9445 t = make_class_type (TREE_CODE (template_type));
9446 CLASSTYPE_DECLARED_CLASS (t)
9447 = CLASSTYPE_DECLARED_CLASS (template_type);
9448 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9449
9450 /* A local class. Make sure the decl gets registered properly. */
9451 if (context == current_function_decl)
9452 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9453 == error_mark_node)
9454 return error_mark_node;
9455
9456 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9457 /* This instantiation is another name for the primary
9458 template type. Set the TYPE_CANONICAL field
9459 appropriately. */
9460 TYPE_CANONICAL (t) = template_type;
9461 else if (any_template_arguments_need_structural_equality_p (arglist))
9462 /* Some of the template arguments require structural
9463 equality testing, so this template class requires
9464 structural equality testing. */
9465 SET_TYPE_STRUCTURAL_EQUALITY (t);
9466 }
9467 else
9468 gcc_unreachable ();
9469
9470 /* If we called start_enum or pushtag above, this information
9471 will already be set up. */
9472 if (!TYPE_NAME (t))
9473 {
9474 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9475
9476 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9477 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9478 DECL_SOURCE_LOCATION (type_decl)
9479 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9480 }
9481 else
9482 type_decl = TYPE_NAME (t);
9483
9484 if (CLASS_TYPE_P (template_type))
9485 {
9486 TREE_PRIVATE (type_decl)
9487 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9488 TREE_PROTECTED (type_decl)
9489 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9490 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9491 {
9492 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9493 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9494 }
9495 }
9496
9497 if (OVERLOAD_TYPE_P (t)
9498 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9499 {
9500 static const char *tags[] = {"abi_tag", "may_alias"};
9501
9502 for (unsigned ix = 0; ix != 2; ix++)
9503 {
9504 tree attributes
9505 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9506
9507 if (attributes)
9508 TYPE_ATTRIBUTES (t)
9509 = tree_cons (TREE_PURPOSE (attributes),
9510 TREE_VALUE (attributes),
9511 TYPE_ATTRIBUTES (t));
9512 }
9513 }
9514
9515 /* Let's consider the explicit specialization of a member
9516 of a class template specialization that is implicitly instantiated,
9517 e.g.:
9518 template<class T>
9519 struct S
9520 {
9521 template<class U> struct M {}; //#0
9522 };
9523
9524 template<>
9525 template<>
9526 struct S<int>::M<char> //#1
9527 {
9528 int i;
9529 };
9530 [temp.expl.spec]/4 says this is valid.
9531
9532 In this case, when we write:
9533 S<int>::M<char> m;
9534
9535 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9536 the one of #0.
9537
9538 When we encounter #1, we want to store the partial instantiation
9539 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9540
9541 For all cases other than this "explicit specialization of member of a
9542 class template", we just want to store the most general template into
9543 the CLASSTYPE_TI_TEMPLATE of M.
9544
9545 This case of "explicit specialization of member of a class template"
9546 only happens when:
9547 1/ the enclosing class is an instantiation of, and therefore not
9548 the same as, the context of the most general template, and
9549 2/ we aren't looking at the partial instantiation itself, i.e.
9550 the innermost arguments are not the same as the innermost parms of
9551 the most general template.
9552
9553 So it's only when 1/ and 2/ happens that we want to use the partial
9554 instantiation of the member template in lieu of its most general
9555 template. */
9556
9557 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9558 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9559 /* the enclosing class must be an instantiation... */
9560 && CLASS_TYPE_P (context)
9561 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9562 {
9563 TREE_VEC_LENGTH (arglist)--;
9564 ++processing_template_decl;
9565 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9566 tree partial_inst_args =
9567 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9568 arglist, complain, NULL_TREE);
9569 --processing_template_decl;
9570 TREE_VEC_LENGTH (arglist)++;
9571 if (partial_inst_args == error_mark_node)
9572 return error_mark_node;
9573 use_partial_inst_tmpl =
9574 /*...and we must not be looking at the partial instantiation
9575 itself. */
9576 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9577 partial_inst_args);
9578 }
9579
9580 if (!use_partial_inst_tmpl)
9581 /* This case is easy; there are no member templates involved. */
9582 found = gen_tmpl;
9583 else
9584 {
9585 /* This is a full instantiation of a member template. Find
9586 the partial instantiation of which this is an instance. */
9587
9588 /* Temporarily reduce by one the number of levels in the ARGLIST
9589 so as to avoid comparing the last set of arguments. */
9590 TREE_VEC_LENGTH (arglist)--;
9591 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9592 TREE_VEC_LENGTH (arglist)++;
9593 /* FOUND is either a proper class type, or an alias
9594 template specialization. In the later case, it's a
9595 TYPE_DECL, resulting from the substituting of arguments
9596 for parameters in the TYPE_DECL of the alias template
9597 done earlier. So be careful while getting the template
9598 of FOUND. */
9599 found = (TREE_CODE (found) == TEMPLATE_DECL
9600 ? found
9601 : (TREE_CODE (found) == TYPE_DECL
9602 ? DECL_TI_TEMPLATE (found)
9603 : CLASSTYPE_TI_TEMPLATE (found)));
9604 }
9605
9606 // Build template info for the new specialization.
9607 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9608
9609 elt.spec = t;
9610 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9611 entry = ggc_alloc<spec_entry> ();
9612 *entry = elt;
9613 *slot = entry;
9614
9615 /* Note this use of the partial instantiation so we can check it
9616 later in maybe_process_partial_specialization. */
9617 DECL_TEMPLATE_INSTANTIATIONS (found)
9618 = tree_cons (arglist, t,
9619 DECL_TEMPLATE_INSTANTIATIONS (found));
9620
9621 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9622 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9623 /* Now that the type has been registered on the instantiations
9624 list, we set up the enumerators. Because the enumeration
9625 constants may involve the enumeration type itself, we make
9626 sure to register the type first, and then create the
9627 constants. That way, doing tsubst_expr for the enumeration
9628 constants won't result in recursive calls here; we'll find
9629 the instantiation and exit above. */
9630 tsubst_enum (template_type, t, arglist);
9631
9632 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9633 /* If the type makes use of template parameters, the
9634 code that generates debugging information will crash. */
9635 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9636
9637 /* Possibly limit visibility based on template args. */
9638 TREE_PUBLIC (type_decl) = 1;
9639 determine_visibility (type_decl);
9640
9641 inherit_targ_abi_tags (t);
9642
9643 return t;
9644 }
9645 }
9646
9647 /* Wrapper for lookup_template_class_1. */
9648
9649 tree
9650 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9651 int entering_scope, tsubst_flags_t complain)
9652 {
9653 tree ret;
9654 timevar_push (TV_TEMPLATE_INST);
9655 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9656 entering_scope, complain);
9657 timevar_pop (TV_TEMPLATE_INST);
9658 return ret;
9659 }
9660
9661 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9662
9663 tree
9664 lookup_template_variable (tree templ, tree arglist)
9665 {
9666 /* The type of the expression is NULL_TREE since the template-id could refer
9667 to an explicit or partial specialization. */
9668 tree type = NULL_TREE;
9669 if (flag_concepts && variable_concept_p (templ))
9670 /* Except that concepts are always bool. */
9671 type = boolean_type_node;
9672 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9673 }
9674
9675 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9676
9677 tree
9678 finish_template_variable (tree var, tsubst_flags_t complain)
9679 {
9680 tree templ = TREE_OPERAND (var, 0);
9681 tree arglist = TREE_OPERAND (var, 1);
9682
9683 /* We never want to return a VAR_DECL for a variable concept, since they
9684 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9685 bool concept_p = flag_concepts && variable_concept_p (templ);
9686 if (concept_p && processing_template_decl)
9687 return var;
9688
9689 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9690 arglist = add_outermost_template_args (tmpl_args, arglist);
9691
9692 templ = most_general_template (templ);
9693 tree parms = DECL_TEMPLATE_PARMS (templ);
9694 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9695 /*req_all*/true,
9696 /*use_default*/true);
9697
9698 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9699 {
9700 if (complain & tf_error)
9701 {
9702 error ("use of invalid variable template %qE", var);
9703 diagnose_constraints (location_of (var), templ, arglist);
9704 }
9705 return error_mark_node;
9706 }
9707
9708 /* If a template-id refers to a specialization of a variable
9709 concept, then the expression is true if and only if the
9710 concept's constraints are satisfied by the given template
9711 arguments.
9712
9713 NOTE: This is an extension of Concepts Lite TS that
9714 allows constraints to be used in expressions. */
9715 if (concept_p)
9716 {
9717 tree decl = DECL_TEMPLATE_RESULT (templ);
9718 return evaluate_variable_concept (decl, arglist);
9719 }
9720
9721 return instantiate_template (templ, arglist, complain);
9722 }
9723
9724 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9725 TARGS template args, and instantiate it if it's not dependent. */
9726
9727 tree
9728 lookup_and_finish_template_variable (tree templ, tree targs,
9729 tsubst_flags_t complain)
9730 {
9731 templ = lookup_template_variable (templ, targs);
9732 if (!any_dependent_template_arguments_p (targs))
9733 {
9734 templ = finish_template_variable (templ, complain);
9735 mark_used (templ);
9736 }
9737
9738 return convert_from_reference (templ);
9739 }
9740
9741 \f
9742 struct pair_fn_data
9743 {
9744 tree_fn_t fn;
9745 tree_fn_t any_fn;
9746 void *data;
9747 /* True when we should also visit template parameters that occur in
9748 non-deduced contexts. */
9749 bool include_nondeduced_p;
9750 hash_set<tree> *visited;
9751 };
9752
9753 /* Called from for_each_template_parm via walk_tree. */
9754
9755 static tree
9756 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9757 {
9758 tree t = *tp;
9759 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9760 tree_fn_t fn = pfd->fn;
9761 void *data = pfd->data;
9762 tree result = NULL_TREE;
9763
9764 #define WALK_SUBTREE(NODE) \
9765 do \
9766 { \
9767 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9768 pfd->include_nondeduced_p, \
9769 pfd->any_fn); \
9770 if (result) goto out; \
9771 } \
9772 while (0)
9773
9774 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9775 return t;
9776
9777 if (TYPE_P (t)
9778 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9779 WALK_SUBTREE (TYPE_CONTEXT (t));
9780
9781 switch (TREE_CODE (t))
9782 {
9783 case RECORD_TYPE:
9784 if (TYPE_PTRMEMFUNC_P (t))
9785 break;
9786 /* Fall through. */
9787
9788 case UNION_TYPE:
9789 case ENUMERAL_TYPE:
9790 if (!TYPE_TEMPLATE_INFO (t))
9791 *walk_subtrees = 0;
9792 else
9793 WALK_SUBTREE (TYPE_TI_ARGS (t));
9794 break;
9795
9796 case INTEGER_TYPE:
9797 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9798 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9799 break;
9800
9801 case METHOD_TYPE:
9802 /* Since we're not going to walk subtrees, we have to do this
9803 explicitly here. */
9804 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9805 /* Fall through. */
9806
9807 case FUNCTION_TYPE:
9808 /* Check the return type. */
9809 WALK_SUBTREE (TREE_TYPE (t));
9810
9811 /* Check the parameter types. Since default arguments are not
9812 instantiated until they are needed, the TYPE_ARG_TYPES may
9813 contain expressions that involve template parameters. But,
9814 no-one should be looking at them yet. And, once they're
9815 instantiated, they don't contain template parameters, so
9816 there's no point in looking at them then, either. */
9817 {
9818 tree parm;
9819
9820 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9821 WALK_SUBTREE (TREE_VALUE (parm));
9822
9823 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9824 want walk_tree walking into them itself. */
9825 *walk_subtrees = 0;
9826 }
9827
9828 if (flag_noexcept_type)
9829 {
9830 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9831 if (spec)
9832 WALK_SUBTREE (TREE_PURPOSE (spec));
9833 }
9834 break;
9835
9836 case TYPEOF_TYPE:
9837 case DECLTYPE_TYPE:
9838 case UNDERLYING_TYPE:
9839 if (pfd->include_nondeduced_p
9840 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9841 pfd->visited,
9842 pfd->include_nondeduced_p,
9843 pfd->any_fn))
9844 return error_mark_node;
9845 *walk_subtrees = false;
9846 break;
9847
9848 case FUNCTION_DECL:
9849 case VAR_DECL:
9850 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9851 WALK_SUBTREE (DECL_TI_ARGS (t));
9852 /* Fall through. */
9853
9854 case PARM_DECL:
9855 case CONST_DECL:
9856 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9857 WALK_SUBTREE (DECL_INITIAL (t));
9858 if (DECL_CONTEXT (t)
9859 && pfd->include_nondeduced_p)
9860 WALK_SUBTREE (DECL_CONTEXT (t));
9861 break;
9862
9863 case BOUND_TEMPLATE_TEMPLATE_PARM:
9864 /* Record template parameters such as `T' inside `TT<T>'. */
9865 WALK_SUBTREE (TYPE_TI_ARGS (t));
9866 /* Fall through. */
9867
9868 case TEMPLATE_TEMPLATE_PARM:
9869 case TEMPLATE_TYPE_PARM:
9870 case TEMPLATE_PARM_INDEX:
9871 if (fn && (*fn)(t, data))
9872 return t;
9873 else if (!fn)
9874 return t;
9875 break;
9876
9877 case TEMPLATE_DECL:
9878 /* A template template parameter is encountered. */
9879 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9880 WALK_SUBTREE (TREE_TYPE (t));
9881
9882 /* Already substituted template template parameter */
9883 *walk_subtrees = 0;
9884 break;
9885
9886 case TYPENAME_TYPE:
9887 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9888 partial instantiation. */
9889 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9890 break;
9891
9892 case CONSTRUCTOR:
9893 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9894 && pfd->include_nondeduced_p)
9895 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9896 break;
9897
9898 case INDIRECT_REF:
9899 case COMPONENT_REF:
9900 /* If there's no type, then this thing must be some expression
9901 involving template parameters. */
9902 if (!fn && !TREE_TYPE (t))
9903 return error_mark_node;
9904 break;
9905
9906 case MODOP_EXPR:
9907 case CAST_EXPR:
9908 case IMPLICIT_CONV_EXPR:
9909 case REINTERPRET_CAST_EXPR:
9910 case CONST_CAST_EXPR:
9911 case STATIC_CAST_EXPR:
9912 case DYNAMIC_CAST_EXPR:
9913 case ARROW_EXPR:
9914 case DOTSTAR_EXPR:
9915 case TYPEID_EXPR:
9916 case PSEUDO_DTOR_EXPR:
9917 if (!fn)
9918 return error_mark_node;
9919 break;
9920
9921 default:
9922 break;
9923 }
9924
9925 #undef WALK_SUBTREE
9926
9927 /* We didn't find any template parameters we liked. */
9928 out:
9929 return result;
9930 }
9931
9932 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9933 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9934 call FN with the parameter and the DATA.
9935 If FN returns nonzero, the iteration is terminated, and
9936 for_each_template_parm returns 1. Otherwise, the iteration
9937 continues. If FN never returns a nonzero value, the value
9938 returned by for_each_template_parm is 0. If FN is NULL, it is
9939 considered to be the function which always returns 1.
9940
9941 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9942 parameters that occur in non-deduced contexts. When false, only
9943 visits those template parameters that can be deduced. */
9944
9945 static tree
9946 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9947 hash_set<tree> *visited,
9948 bool include_nondeduced_p,
9949 tree_fn_t any_fn)
9950 {
9951 struct pair_fn_data pfd;
9952 tree result;
9953
9954 /* Set up. */
9955 pfd.fn = fn;
9956 pfd.any_fn = any_fn;
9957 pfd.data = data;
9958 pfd.include_nondeduced_p = include_nondeduced_p;
9959
9960 /* Walk the tree. (Conceptually, we would like to walk without
9961 duplicates, but for_each_template_parm_r recursively calls
9962 for_each_template_parm, so we would need to reorganize a fair
9963 bit to use walk_tree_without_duplicates, so we keep our own
9964 visited list.) */
9965 if (visited)
9966 pfd.visited = visited;
9967 else
9968 pfd.visited = new hash_set<tree>;
9969 result = cp_walk_tree (&t,
9970 for_each_template_parm_r,
9971 &pfd,
9972 pfd.visited);
9973
9974 /* Clean up. */
9975 if (!visited)
9976 {
9977 delete pfd.visited;
9978 pfd.visited = 0;
9979 }
9980
9981 return result;
9982 }
9983
9984 /* Returns true if T depends on any template parameter. */
9985
9986 int
9987 uses_template_parms (tree t)
9988 {
9989 if (t == NULL_TREE)
9990 return false;
9991
9992 bool dependent_p;
9993 int saved_processing_template_decl;
9994
9995 saved_processing_template_decl = processing_template_decl;
9996 if (!saved_processing_template_decl)
9997 processing_template_decl = 1;
9998 if (TYPE_P (t))
9999 dependent_p = dependent_type_p (t);
10000 else if (TREE_CODE (t) == TREE_VEC)
10001 dependent_p = any_dependent_template_arguments_p (t);
10002 else if (TREE_CODE (t) == TREE_LIST)
10003 dependent_p = (uses_template_parms (TREE_VALUE (t))
10004 || uses_template_parms (TREE_CHAIN (t)));
10005 else if (TREE_CODE (t) == TYPE_DECL)
10006 dependent_p = dependent_type_p (TREE_TYPE (t));
10007 else if (DECL_P (t)
10008 || EXPR_P (t)
10009 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10010 || TREE_CODE (t) == OVERLOAD
10011 || BASELINK_P (t)
10012 || identifier_p (t)
10013 || TREE_CODE (t) == TRAIT_EXPR
10014 || TREE_CODE (t) == CONSTRUCTOR
10015 || CONSTANT_CLASS_P (t))
10016 dependent_p = (type_dependent_expression_p (t)
10017 || value_dependent_expression_p (t));
10018 else
10019 {
10020 gcc_assert (t == error_mark_node);
10021 dependent_p = false;
10022 }
10023
10024 processing_template_decl = saved_processing_template_decl;
10025
10026 return dependent_p;
10027 }
10028
10029 /* Returns true iff current_function_decl is an incompletely instantiated
10030 template. Useful instead of processing_template_decl because the latter
10031 is set to 0 during instantiate_non_dependent_expr. */
10032
10033 bool
10034 in_template_function (void)
10035 {
10036 tree fn = current_function_decl;
10037 bool ret;
10038 ++processing_template_decl;
10039 ret = (fn && DECL_LANG_SPECIFIC (fn)
10040 && DECL_TEMPLATE_INFO (fn)
10041 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10042 --processing_template_decl;
10043 return ret;
10044 }
10045
10046 /* Returns true if T depends on any template parameter with level LEVEL. */
10047
10048 bool
10049 uses_template_parms_level (tree t, int level)
10050 {
10051 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10052 /*include_nondeduced_p=*/true);
10053 }
10054
10055 /* Returns true if the signature of DECL depends on any template parameter from
10056 its enclosing class. */
10057
10058 bool
10059 uses_outer_template_parms (tree decl)
10060 {
10061 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10062 if (depth == 0)
10063 return false;
10064 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10065 &depth, NULL, /*include_nondeduced_p=*/true))
10066 return true;
10067 if (PRIMARY_TEMPLATE_P (decl)
10068 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10069 (DECL_TEMPLATE_PARMS (decl)),
10070 template_parm_outer_level,
10071 &depth, NULL, /*include_nondeduced_p=*/true))
10072 return true;
10073 tree ci = get_constraints (decl);
10074 if (ci)
10075 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10076 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10077 &depth, NULL, /*nondeduced*/true))
10078 return true;
10079 return false;
10080 }
10081
10082 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10083 ill-formed translation unit, i.e. a variable or function that isn't
10084 usable in a constant expression. */
10085
10086 static inline bool
10087 neglectable_inst_p (tree d)
10088 {
10089 return (d && DECL_P (d)
10090 && !undeduced_auto_decl (d)
10091 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10092 : decl_maybe_constant_var_p (d)));
10093 }
10094
10095 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10096 neglectable and instantiated from within an erroneous instantiation. */
10097
10098 static bool
10099 limit_bad_template_recursion (tree decl)
10100 {
10101 struct tinst_level *lev = current_tinst_level;
10102 int errs = errorcount + sorrycount;
10103 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10104 return false;
10105
10106 for (; lev; lev = lev->next)
10107 if (neglectable_inst_p (lev->maybe_get_node ()))
10108 break;
10109
10110 return (lev && errs > lev->errors);
10111 }
10112
10113 static int tinst_depth;
10114 extern int max_tinst_depth;
10115 int depth_reached;
10116
10117 static GTY(()) struct tinst_level *last_error_tinst_level;
10118
10119 /* We're starting to instantiate D; record the template instantiation context
10120 at LOC for diagnostics and to restore it later. */
10121
10122 static bool
10123 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10124 {
10125 struct tinst_level *new_level;
10126
10127 if (tinst_depth >= max_tinst_depth)
10128 {
10129 /* Tell error.c not to try to instantiate any templates. */
10130 at_eof = 2;
10131 fatal_error (input_location,
10132 "template instantiation depth exceeds maximum of %d"
10133 " (use -ftemplate-depth= to increase the maximum)",
10134 max_tinst_depth);
10135 return false;
10136 }
10137
10138 /* If the current instantiation caused problems, don't let it instantiate
10139 anything else. Do allow deduction substitution and decls usable in
10140 constant expressions. */
10141 if (!targs && limit_bad_template_recursion (tldcl))
10142 return false;
10143
10144 /* When not -quiet, dump template instantiations other than functions, since
10145 announce_function will take care of those. */
10146 if (!quiet_flag && !targs
10147 && TREE_CODE (tldcl) != TREE_LIST
10148 && TREE_CODE (tldcl) != FUNCTION_DECL)
10149 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10150
10151 new_level = tinst_level_freelist ().alloc ();
10152 new_level->tldcl = tldcl;
10153 new_level->targs = targs;
10154 new_level->locus = loc;
10155 new_level->errors = errorcount + sorrycount;
10156 new_level->next = NULL;
10157 new_level->refcount = 0;
10158 set_refcount_ptr (new_level->next, current_tinst_level);
10159 set_refcount_ptr (current_tinst_level, new_level);
10160
10161 ++tinst_depth;
10162 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10163 depth_reached = tinst_depth;
10164
10165 return true;
10166 }
10167
10168 /* We're starting substitution of TMPL<ARGS>; record the template
10169 substitution context for diagnostics and to restore it later. */
10170
10171 static bool
10172 push_tinst_level (tree tmpl, tree args)
10173 {
10174 return push_tinst_level_loc (tmpl, args, input_location);
10175 }
10176
10177 /* We're starting to instantiate D; record INPUT_LOCATION and the
10178 template instantiation context for diagnostics and to restore it
10179 later. */
10180
10181 bool
10182 push_tinst_level (tree d)
10183 {
10184 return push_tinst_level_loc (d, input_location);
10185 }
10186
10187 /* Likewise, but record LOC as the program location. */
10188
10189 bool
10190 push_tinst_level_loc (tree d, location_t loc)
10191 {
10192 gcc_assert (TREE_CODE (d) != TREE_LIST);
10193 return push_tinst_level_loc (d, NULL, loc);
10194 }
10195
10196 /* We're done instantiating this template; return to the instantiation
10197 context. */
10198
10199 void
10200 pop_tinst_level (void)
10201 {
10202 /* Restore the filename and line number stashed away when we started
10203 this instantiation. */
10204 input_location = current_tinst_level->locus;
10205 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10206 --tinst_depth;
10207 }
10208
10209 /* We're instantiating a deferred template; restore the template
10210 instantiation context in which the instantiation was requested, which
10211 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10212
10213 static tree
10214 reopen_tinst_level (struct tinst_level *level)
10215 {
10216 struct tinst_level *t;
10217
10218 tinst_depth = 0;
10219 for (t = level; t; t = t->next)
10220 ++tinst_depth;
10221
10222 set_refcount_ptr (current_tinst_level, level);
10223 pop_tinst_level ();
10224 if (current_tinst_level)
10225 current_tinst_level->errors = errorcount+sorrycount;
10226 return level->maybe_get_node ();
10227 }
10228
10229 /* Returns the TINST_LEVEL which gives the original instantiation
10230 context. */
10231
10232 struct tinst_level *
10233 outermost_tinst_level (void)
10234 {
10235 struct tinst_level *level = current_tinst_level;
10236 if (level)
10237 while (level->next)
10238 level = level->next;
10239 return level;
10240 }
10241
10242 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10243 vector of template arguments, as for tsubst.
10244
10245 Returns an appropriate tsubst'd friend declaration. */
10246
10247 static tree
10248 tsubst_friend_function (tree decl, tree args)
10249 {
10250 tree new_friend;
10251
10252 if (TREE_CODE (decl) == FUNCTION_DECL
10253 && DECL_TEMPLATE_INSTANTIATION (decl)
10254 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10255 /* This was a friend declared with an explicit template
10256 argument list, e.g.:
10257
10258 friend void f<>(T);
10259
10260 to indicate that f was a template instantiation, not a new
10261 function declaration. Now, we have to figure out what
10262 instantiation of what template. */
10263 {
10264 tree template_id, arglist, fns;
10265 tree new_args;
10266 tree tmpl;
10267 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10268
10269 /* Friend functions are looked up in the containing namespace scope.
10270 We must enter that scope, to avoid finding member functions of the
10271 current class with same name. */
10272 push_nested_namespace (ns);
10273 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10274 tf_warning_or_error, NULL_TREE,
10275 /*integral_constant_expression_p=*/false);
10276 pop_nested_namespace (ns);
10277 arglist = tsubst (DECL_TI_ARGS (decl), args,
10278 tf_warning_or_error, NULL_TREE);
10279 template_id = lookup_template_function (fns, arglist);
10280
10281 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10282 tmpl = determine_specialization (template_id, new_friend,
10283 &new_args,
10284 /*need_member_template=*/0,
10285 TREE_VEC_LENGTH (args),
10286 tsk_none);
10287 return instantiate_template (tmpl, new_args, tf_error);
10288 }
10289
10290 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10291
10292 /* The NEW_FRIEND will look like an instantiation, to the
10293 compiler, but is not an instantiation from the point of view of
10294 the language. For example, we might have had:
10295
10296 template <class T> struct S {
10297 template <class U> friend void f(T, U);
10298 };
10299
10300 Then, in S<int>, template <class U> void f(int, U) is not an
10301 instantiation of anything. */
10302 if (new_friend == error_mark_node)
10303 return error_mark_node;
10304
10305 DECL_USE_TEMPLATE (new_friend) = 0;
10306 if (TREE_CODE (decl) == TEMPLATE_DECL)
10307 {
10308 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10309 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10310 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10311 }
10312
10313 /* The mangled name for the NEW_FRIEND is incorrect. The function
10314 is not a template instantiation and should not be mangled like
10315 one. Therefore, we forget the mangling here; we'll recompute it
10316 later if we need it. */
10317 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10318 {
10319 SET_DECL_RTL (new_friend, NULL);
10320 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10321 }
10322
10323 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10324 {
10325 tree old_decl;
10326 tree new_friend_template_info;
10327 tree new_friend_result_template_info;
10328 tree ns;
10329 int new_friend_is_defn;
10330
10331 /* We must save some information from NEW_FRIEND before calling
10332 duplicate decls since that function will free NEW_FRIEND if
10333 possible. */
10334 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10335 new_friend_is_defn =
10336 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10337 (template_for_substitution (new_friend)))
10338 != NULL_TREE);
10339 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10340 {
10341 /* This declaration is a `primary' template. */
10342 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10343
10344 new_friend_result_template_info
10345 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10346 }
10347 else
10348 new_friend_result_template_info = NULL_TREE;
10349
10350 /* Inside pushdecl_namespace_level, we will push into the
10351 current namespace. However, the friend function should go
10352 into the namespace of the template. */
10353 ns = decl_namespace_context (new_friend);
10354 push_nested_namespace (ns);
10355 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10356 pop_nested_namespace (ns);
10357
10358 if (old_decl == error_mark_node)
10359 return error_mark_node;
10360
10361 if (old_decl != new_friend)
10362 {
10363 /* This new friend declaration matched an existing
10364 declaration. For example, given:
10365
10366 template <class T> void f(T);
10367 template <class U> class C {
10368 template <class T> friend void f(T) {}
10369 };
10370
10371 the friend declaration actually provides the definition
10372 of `f', once C has been instantiated for some type. So,
10373 old_decl will be the out-of-class template declaration,
10374 while new_friend is the in-class definition.
10375
10376 But, if `f' was called before this point, the
10377 instantiation of `f' will have DECL_TI_ARGS corresponding
10378 to `T' but not to `U', references to which might appear
10379 in the definition of `f'. Previously, the most general
10380 template for an instantiation of `f' was the out-of-class
10381 version; now it is the in-class version. Therefore, we
10382 run through all specialization of `f', adding to their
10383 DECL_TI_ARGS appropriately. In particular, they need a
10384 new set of outer arguments, corresponding to the
10385 arguments for this class instantiation.
10386
10387 The same situation can arise with something like this:
10388
10389 friend void f(int);
10390 template <class T> class C {
10391 friend void f(T) {}
10392 };
10393
10394 when `C<int>' is instantiated. Now, `f(int)' is defined
10395 in the class. */
10396
10397 if (!new_friend_is_defn)
10398 /* On the other hand, if the in-class declaration does
10399 *not* provide a definition, then we don't want to alter
10400 existing definitions. We can just leave everything
10401 alone. */
10402 ;
10403 else
10404 {
10405 tree new_template = TI_TEMPLATE (new_friend_template_info);
10406 tree new_args = TI_ARGS (new_friend_template_info);
10407
10408 /* Overwrite whatever template info was there before, if
10409 any, with the new template information pertaining to
10410 the declaration. */
10411 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10412
10413 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10414 {
10415 /* We should have called reregister_specialization in
10416 duplicate_decls. */
10417 gcc_assert (retrieve_specialization (new_template,
10418 new_args, 0)
10419 == old_decl);
10420
10421 /* Instantiate it if the global has already been used. */
10422 if (DECL_ODR_USED (old_decl))
10423 instantiate_decl (old_decl, /*defer_ok=*/true,
10424 /*expl_inst_class_mem_p=*/false);
10425 }
10426 else
10427 {
10428 tree t;
10429
10430 /* Indicate that the old function template is a partial
10431 instantiation. */
10432 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10433 = new_friend_result_template_info;
10434
10435 gcc_assert (new_template
10436 == most_general_template (new_template));
10437 gcc_assert (new_template != old_decl);
10438
10439 /* Reassign any specializations already in the hash table
10440 to the new more general template, and add the
10441 additional template args. */
10442 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10443 t != NULL_TREE;
10444 t = TREE_CHAIN (t))
10445 {
10446 tree spec = TREE_VALUE (t);
10447 spec_entry elt;
10448
10449 elt.tmpl = old_decl;
10450 elt.args = DECL_TI_ARGS (spec);
10451 elt.spec = NULL_TREE;
10452
10453 decl_specializations->remove_elt (&elt);
10454
10455 DECL_TI_ARGS (spec)
10456 = add_outermost_template_args (new_args,
10457 DECL_TI_ARGS (spec));
10458
10459 register_specialization
10460 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10461
10462 }
10463 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10464 }
10465 }
10466
10467 /* The information from NEW_FRIEND has been merged into OLD_DECL
10468 by duplicate_decls. */
10469 new_friend = old_decl;
10470 }
10471 }
10472 else
10473 {
10474 tree context = DECL_CONTEXT (new_friend);
10475 bool dependent_p;
10476
10477 /* In the code
10478 template <class T> class C {
10479 template <class U> friend void C1<U>::f (); // case 1
10480 friend void C2<T>::f (); // case 2
10481 };
10482 we only need to make sure CONTEXT is a complete type for
10483 case 2. To distinguish between the two cases, we note that
10484 CONTEXT of case 1 remains dependent type after tsubst while
10485 this isn't true for case 2. */
10486 ++processing_template_decl;
10487 dependent_p = dependent_type_p (context);
10488 --processing_template_decl;
10489
10490 if (!dependent_p
10491 && !complete_type_or_else (context, NULL_TREE))
10492 return error_mark_node;
10493
10494 if (COMPLETE_TYPE_P (context))
10495 {
10496 tree fn = new_friend;
10497 /* do_friend adds the TEMPLATE_DECL for any member friend
10498 template even if it isn't a member template, i.e.
10499 template <class T> friend A<T>::f();
10500 Look through it in that case. */
10501 if (TREE_CODE (fn) == TEMPLATE_DECL
10502 && !PRIMARY_TEMPLATE_P (fn))
10503 fn = DECL_TEMPLATE_RESULT (fn);
10504 /* Check to see that the declaration is really present, and,
10505 possibly obtain an improved declaration. */
10506 fn = check_classfn (context, fn, NULL_TREE);
10507
10508 if (fn)
10509 new_friend = fn;
10510 }
10511 }
10512
10513 return new_friend;
10514 }
10515
10516 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10517 template arguments, as for tsubst.
10518
10519 Returns an appropriate tsubst'd friend type or error_mark_node on
10520 failure. */
10521
10522 static tree
10523 tsubst_friend_class (tree friend_tmpl, tree args)
10524 {
10525 tree tmpl;
10526
10527 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10528 {
10529 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10530 return TREE_TYPE (tmpl);
10531 }
10532
10533 tree context = CP_DECL_CONTEXT (friend_tmpl);
10534 if (TREE_CODE (context) == NAMESPACE_DECL)
10535 push_nested_namespace (context);
10536 else
10537 push_nested_class (context);
10538
10539 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10540 /*non_class=*/false, /*block_p=*/false,
10541 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10542
10543 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10544 {
10545 /* The friend template has already been declared. Just
10546 check to see that the declarations match, and install any new
10547 default parameters. We must tsubst the default parameters,
10548 of course. We only need the innermost template parameters
10549 because that is all that redeclare_class_template will look
10550 at. */
10551 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10552 > TMPL_ARGS_DEPTH (args))
10553 {
10554 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10555 args, tf_warning_or_error);
10556 location_t saved_input_location = input_location;
10557 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10558 tree cons = get_constraints (tmpl);
10559 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10560 input_location = saved_input_location;
10561 }
10562 }
10563 else
10564 {
10565 /* The friend template has not already been declared. In this
10566 case, the instantiation of the template class will cause the
10567 injection of this template into the namespace scope. */
10568 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10569
10570 if (tmpl != error_mark_node)
10571 {
10572 /* The new TMPL is not an instantiation of anything, so we
10573 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10574 for the new type because that is supposed to be the
10575 corresponding template decl, i.e., TMPL. */
10576 DECL_USE_TEMPLATE (tmpl) = 0;
10577 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10578 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10579 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10580 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10581
10582 /* It is hidden. */
10583 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10584 DECL_ANTICIPATED (tmpl)
10585 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10586
10587 /* Inject this template into the enclosing namspace scope. */
10588 tmpl = pushdecl_namespace_level (tmpl, true);
10589 }
10590 }
10591
10592 if (TREE_CODE (context) == NAMESPACE_DECL)
10593 pop_nested_namespace (context);
10594 else
10595 pop_nested_class ();
10596
10597 return TREE_TYPE (tmpl);
10598 }
10599
10600 /* Returns zero if TYPE cannot be completed later due to circularity.
10601 Otherwise returns one. */
10602
10603 static int
10604 can_complete_type_without_circularity (tree type)
10605 {
10606 if (type == NULL_TREE || type == error_mark_node)
10607 return 0;
10608 else if (COMPLETE_TYPE_P (type))
10609 return 1;
10610 else if (TREE_CODE (type) == ARRAY_TYPE)
10611 return can_complete_type_without_circularity (TREE_TYPE (type));
10612 else if (CLASS_TYPE_P (type)
10613 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10614 return 0;
10615 else
10616 return 1;
10617 }
10618
10619 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10620 tsubst_flags_t, tree);
10621
10622 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10623 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10624
10625 static tree
10626 tsubst_attribute (tree t, tree *decl_p, tree args,
10627 tsubst_flags_t complain, tree in_decl)
10628 {
10629 gcc_assert (ATTR_IS_DEPENDENT (t));
10630
10631 tree val = TREE_VALUE (t);
10632 if (val == NULL_TREE)
10633 /* Nothing to do. */;
10634 else if ((flag_openmp || flag_openmp_simd)
10635 && is_attribute_p ("omp declare simd",
10636 get_attribute_name (t)))
10637 {
10638 tree clauses = TREE_VALUE (val);
10639 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10640 complain, in_decl);
10641 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10642 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10643 tree parms = DECL_ARGUMENTS (*decl_p);
10644 clauses
10645 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10646 if (clauses)
10647 val = build_tree_list (NULL_TREE, clauses);
10648 else
10649 val = NULL_TREE;
10650 }
10651 /* If the first attribute argument is an identifier, don't
10652 pass it through tsubst. Attributes like mode, format,
10653 cleanup and several target specific attributes expect it
10654 unmodified. */
10655 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10656 {
10657 tree chain
10658 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10659 /*integral_constant_expression_p=*/false);
10660 if (chain != TREE_CHAIN (val))
10661 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10662 }
10663 else if (PACK_EXPANSION_P (val))
10664 {
10665 /* An attribute pack expansion. */
10666 tree purp = TREE_PURPOSE (t);
10667 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10668 if (pack == error_mark_node)
10669 return error_mark_node;
10670 int len = TREE_VEC_LENGTH (pack);
10671 tree list = NULL_TREE;
10672 tree *q = &list;
10673 for (int i = 0; i < len; ++i)
10674 {
10675 tree elt = TREE_VEC_ELT (pack, i);
10676 *q = build_tree_list (purp, elt);
10677 q = &TREE_CHAIN (*q);
10678 }
10679 return list;
10680 }
10681 else
10682 val = tsubst_expr (val, args, complain, in_decl,
10683 /*integral_constant_expression_p=*/false);
10684
10685 if (val != TREE_VALUE (t))
10686 return build_tree_list (TREE_PURPOSE (t), val);
10687 return t;
10688 }
10689
10690 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10691 unchanged or a new TREE_LIST chain. */
10692
10693 static tree
10694 tsubst_attributes (tree attributes, tree args,
10695 tsubst_flags_t complain, tree in_decl)
10696 {
10697 tree last_dep = NULL_TREE;
10698
10699 for (tree t = attributes; t; t = TREE_CHAIN (t))
10700 if (ATTR_IS_DEPENDENT (t))
10701 {
10702 last_dep = t;
10703 attributes = copy_list (attributes);
10704 break;
10705 }
10706
10707 if (last_dep)
10708 for (tree *p = &attributes; *p; )
10709 {
10710 tree t = *p;
10711 if (ATTR_IS_DEPENDENT (t))
10712 {
10713 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10714 if (subst != t)
10715 {
10716 *p = subst;
10717 while (*p)
10718 p = &TREE_CHAIN (*p);
10719 *p = TREE_CHAIN (t);
10720 continue;
10721 }
10722 }
10723 p = &TREE_CHAIN (*p);
10724 }
10725
10726 return attributes;
10727 }
10728
10729 /* Apply any attributes which had to be deferred until instantiation
10730 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10731 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10732
10733 static void
10734 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10735 tree args, tsubst_flags_t complain, tree in_decl)
10736 {
10737 tree last_dep = NULL_TREE;
10738 tree t;
10739 tree *p;
10740
10741 if (attributes == NULL_TREE)
10742 return;
10743
10744 if (DECL_P (*decl_p))
10745 {
10746 if (TREE_TYPE (*decl_p) == error_mark_node)
10747 return;
10748 p = &DECL_ATTRIBUTES (*decl_p);
10749 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10750 to our attributes parameter. */
10751 gcc_assert (*p == attributes);
10752 }
10753 else
10754 {
10755 p = &TYPE_ATTRIBUTES (*decl_p);
10756 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10757 lookup_template_class_1, and should be preserved. */
10758 gcc_assert (*p != attributes);
10759 while (*p)
10760 p = &TREE_CHAIN (*p);
10761 }
10762
10763 for (t = attributes; t; t = TREE_CHAIN (t))
10764 if (ATTR_IS_DEPENDENT (t))
10765 {
10766 last_dep = t;
10767 attributes = copy_list (attributes);
10768 break;
10769 }
10770
10771 *p = attributes;
10772 if (last_dep)
10773 {
10774 tree late_attrs = NULL_TREE;
10775 tree *q = &late_attrs;
10776
10777 for (; *p; )
10778 {
10779 t = *p;
10780 if (ATTR_IS_DEPENDENT (t))
10781 {
10782 *p = TREE_CHAIN (t);
10783 TREE_CHAIN (t) = NULL_TREE;
10784 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10785 while (*q)
10786 q = &TREE_CHAIN (*q);
10787 }
10788 else
10789 p = &TREE_CHAIN (t);
10790 }
10791
10792 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10793 }
10794 }
10795
10796 /* Perform (or defer) access check for typedefs that were referenced
10797 from within the template TMPL code.
10798 This is a subroutine of instantiate_decl and instantiate_class_template.
10799 TMPL is the template to consider and TARGS is the list of arguments of
10800 that template. */
10801
10802 static void
10803 perform_typedefs_access_check (tree tmpl, tree targs)
10804 {
10805 location_t saved_location;
10806 unsigned i;
10807 qualified_typedef_usage_t *iter;
10808
10809 if (!tmpl
10810 || (!CLASS_TYPE_P (tmpl)
10811 && TREE_CODE (tmpl) != FUNCTION_DECL))
10812 return;
10813
10814 saved_location = input_location;
10815 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10816 {
10817 tree type_decl = iter->typedef_decl;
10818 tree type_scope = iter->context;
10819
10820 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10821 continue;
10822
10823 if (uses_template_parms (type_decl))
10824 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10825 if (uses_template_parms (type_scope))
10826 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10827
10828 /* Make access check error messages point to the location
10829 of the use of the typedef. */
10830 input_location = iter->locus;
10831 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10832 type_decl, type_decl,
10833 tf_warning_or_error);
10834 }
10835 input_location = saved_location;
10836 }
10837
10838 static tree
10839 instantiate_class_template_1 (tree type)
10840 {
10841 tree templ, args, pattern, t, member;
10842 tree typedecl;
10843 tree pbinfo;
10844 tree base_list;
10845 unsigned int saved_maximum_field_alignment;
10846 tree fn_context;
10847
10848 if (type == error_mark_node)
10849 return error_mark_node;
10850
10851 if (COMPLETE_OR_OPEN_TYPE_P (type)
10852 || uses_template_parms (type))
10853 return type;
10854
10855 /* Figure out which template is being instantiated. */
10856 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10857 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10858
10859 /* Mark the type as in the process of being defined. */
10860 TYPE_BEING_DEFINED (type) = 1;
10861
10862 /* We may be in the middle of deferred access check. Disable
10863 it now. */
10864 deferring_access_check_sentinel acs (dk_no_deferred);
10865
10866 /* Determine what specialization of the original template to
10867 instantiate. */
10868 t = most_specialized_partial_spec (type, tf_warning_or_error);
10869 if (t == error_mark_node)
10870 return error_mark_node;
10871 else if (t)
10872 {
10873 /* This TYPE is actually an instantiation of a partial
10874 specialization. We replace the innermost set of ARGS with
10875 the arguments appropriate for substitution. For example,
10876 given:
10877
10878 template <class T> struct S {};
10879 template <class T> struct S<T*> {};
10880
10881 and supposing that we are instantiating S<int*>, ARGS will
10882 presently be {int*} -- but we need {int}. */
10883 pattern = TREE_TYPE (t);
10884 args = TREE_PURPOSE (t);
10885 }
10886 else
10887 {
10888 pattern = TREE_TYPE (templ);
10889 args = CLASSTYPE_TI_ARGS (type);
10890 }
10891
10892 /* If the template we're instantiating is incomplete, then clearly
10893 there's nothing we can do. */
10894 if (!COMPLETE_TYPE_P (pattern))
10895 {
10896 /* We can try again later. */
10897 TYPE_BEING_DEFINED (type) = 0;
10898 return type;
10899 }
10900
10901 /* If we've recursively instantiated too many templates, stop. */
10902 if (! push_tinst_level (type))
10903 return type;
10904
10905 int saved_unevaluated_operand = cp_unevaluated_operand;
10906 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10907
10908 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10909 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10910 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10911 fn_context = error_mark_node;
10912 if (!fn_context)
10913 push_to_top_level ();
10914 else
10915 {
10916 cp_unevaluated_operand = 0;
10917 c_inhibit_evaluation_warnings = 0;
10918 }
10919 /* Use #pragma pack from the template context. */
10920 saved_maximum_field_alignment = maximum_field_alignment;
10921 maximum_field_alignment = TYPE_PRECISION (pattern);
10922
10923 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10924
10925 /* Set the input location to the most specialized template definition.
10926 This is needed if tsubsting causes an error. */
10927 typedecl = TYPE_MAIN_DECL (pattern);
10928 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10929 DECL_SOURCE_LOCATION (typedecl);
10930
10931 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10932 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10933 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10934 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10935 if (ANON_AGGR_TYPE_P (pattern))
10936 SET_ANON_AGGR_TYPE_P (type);
10937 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10938 {
10939 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10940 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10941 /* Adjust visibility for template arguments. */
10942 determine_visibility (TYPE_MAIN_DECL (type));
10943 }
10944 if (CLASS_TYPE_P (type))
10945 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10946
10947 pbinfo = TYPE_BINFO (pattern);
10948
10949 /* We should never instantiate a nested class before its enclosing
10950 class; we need to look up the nested class by name before we can
10951 instantiate it, and that lookup should instantiate the enclosing
10952 class. */
10953 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10954 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10955
10956 base_list = NULL_TREE;
10957 if (BINFO_N_BASE_BINFOS (pbinfo))
10958 {
10959 tree pbase_binfo;
10960 tree pushed_scope;
10961 int i;
10962
10963 /* We must enter the scope containing the type, as that is where
10964 the accessibility of types named in dependent bases are
10965 looked up from. */
10966 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10967
10968 /* Substitute into each of the bases to determine the actual
10969 basetypes. */
10970 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10971 {
10972 tree base;
10973 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10974 tree expanded_bases = NULL_TREE;
10975 int idx, len = 1;
10976
10977 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10978 {
10979 expanded_bases =
10980 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10981 args, tf_error, NULL_TREE);
10982 if (expanded_bases == error_mark_node)
10983 continue;
10984
10985 len = TREE_VEC_LENGTH (expanded_bases);
10986 }
10987
10988 for (idx = 0; idx < len; idx++)
10989 {
10990 if (expanded_bases)
10991 /* Extract the already-expanded base class. */
10992 base = TREE_VEC_ELT (expanded_bases, idx);
10993 else
10994 /* Substitute to figure out the base class. */
10995 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10996 NULL_TREE);
10997
10998 if (base == error_mark_node)
10999 continue;
11000
11001 base_list = tree_cons (access, base, base_list);
11002 if (BINFO_VIRTUAL_P (pbase_binfo))
11003 TREE_TYPE (base_list) = integer_type_node;
11004 }
11005 }
11006
11007 /* The list is now in reverse order; correct that. */
11008 base_list = nreverse (base_list);
11009
11010 if (pushed_scope)
11011 pop_scope (pushed_scope);
11012 }
11013 /* Now call xref_basetypes to set up all the base-class
11014 information. */
11015 xref_basetypes (type, base_list);
11016
11017 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11018 (int) ATTR_FLAG_TYPE_IN_PLACE,
11019 args, tf_error, NULL_TREE);
11020 fixup_attribute_variants (type);
11021
11022 /* Now that our base classes are set up, enter the scope of the
11023 class, so that name lookups into base classes, etc. will work
11024 correctly. This is precisely analogous to what we do in
11025 begin_class_definition when defining an ordinary non-template
11026 class, except we also need to push the enclosing classes. */
11027 push_nested_class (type);
11028
11029 /* Now members are processed in the order of declaration. */
11030 for (member = CLASSTYPE_DECL_LIST (pattern);
11031 member; member = TREE_CHAIN (member))
11032 {
11033 tree t = TREE_VALUE (member);
11034
11035 if (TREE_PURPOSE (member))
11036 {
11037 if (TYPE_P (t))
11038 {
11039 if (LAMBDA_TYPE_P (t))
11040 /* A closure type for a lambda in an NSDMI or default argument.
11041 Ignore it; it will be regenerated when needed. */
11042 continue;
11043
11044 /* Build new CLASSTYPE_NESTED_UTDS. */
11045
11046 tree newtag;
11047 bool class_template_p;
11048
11049 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11050 && TYPE_LANG_SPECIFIC (t)
11051 && CLASSTYPE_IS_TEMPLATE (t));
11052 /* If the member is a class template, then -- even after
11053 substitution -- there may be dependent types in the
11054 template argument list for the class. We increment
11055 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11056 that function will assume that no types are dependent
11057 when outside of a template. */
11058 if (class_template_p)
11059 ++processing_template_decl;
11060 newtag = tsubst (t, args, tf_error, NULL_TREE);
11061 if (class_template_p)
11062 --processing_template_decl;
11063 if (newtag == error_mark_node)
11064 continue;
11065
11066 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11067 {
11068 tree name = TYPE_IDENTIFIER (t);
11069
11070 if (class_template_p)
11071 /* Unfortunately, lookup_template_class sets
11072 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11073 instantiation (i.e., for the type of a member
11074 template class nested within a template class.)
11075 This behavior is required for
11076 maybe_process_partial_specialization to work
11077 correctly, but is not accurate in this case;
11078 the TAG is not an instantiation of anything.
11079 (The corresponding TEMPLATE_DECL is an
11080 instantiation, but the TYPE is not.) */
11081 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11082
11083 /* Now, we call pushtag to put this NEWTAG into the scope of
11084 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11085 pushtag calling push_template_decl. We don't have to do
11086 this for enums because it will already have been done in
11087 tsubst_enum. */
11088 if (name)
11089 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11090 pushtag (name, newtag, /*tag_scope=*/ts_current);
11091 }
11092 }
11093 else if (DECL_DECLARES_FUNCTION_P (t))
11094 {
11095 tree r;
11096
11097 if (TREE_CODE (t) == TEMPLATE_DECL)
11098 ++processing_template_decl;
11099 r = tsubst (t, args, tf_error, NULL_TREE);
11100 if (TREE_CODE (t) == TEMPLATE_DECL)
11101 --processing_template_decl;
11102 set_current_access_from_decl (r);
11103 finish_member_declaration (r);
11104 /* Instantiate members marked with attribute used. */
11105 if (r != error_mark_node && DECL_PRESERVE_P (r))
11106 mark_used (r);
11107 if (TREE_CODE (r) == FUNCTION_DECL
11108 && DECL_OMP_DECLARE_REDUCTION_P (r))
11109 cp_check_omp_declare_reduction (r);
11110 }
11111 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11112 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11113 /* A closure type for a lambda in an NSDMI or default argument.
11114 Ignore it; it will be regenerated when needed. */;
11115 else
11116 {
11117 /* Build new TYPE_FIELDS. */
11118 if (TREE_CODE (t) == STATIC_ASSERT)
11119 {
11120 tree condition;
11121
11122 ++c_inhibit_evaluation_warnings;
11123 condition =
11124 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11125 tf_warning_or_error, NULL_TREE,
11126 /*integral_constant_expression_p=*/true);
11127 --c_inhibit_evaluation_warnings;
11128
11129 finish_static_assert (condition,
11130 STATIC_ASSERT_MESSAGE (t),
11131 STATIC_ASSERT_SOURCE_LOCATION (t),
11132 /*member_p=*/true);
11133 }
11134 else if (TREE_CODE (t) != CONST_DECL)
11135 {
11136 tree r;
11137 tree vec = NULL_TREE;
11138 int len = 1;
11139
11140 /* The file and line for this declaration, to
11141 assist in error message reporting. Since we
11142 called push_tinst_level above, we don't need to
11143 restore these. */
11144 input_location = DECL_SOURCE_LOCATION (t);
11145
11146 if (TREE_CODE (t) == TEMPLATE_DECL)
11147 ++processing_template_decl;
11148 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11149 if (TREE_CODE (t) == TEMPLATE_DECL)
11150 --processing_template_decl;
11151
11152 if (TREE_CODE (r) == TREE_VEC)
11153 {
11154 /* A capture pack became multiple fields. */
11155 vec = r;
11156 len = TREE_VEC_LENGTH (vec);
11157 }
11158
11159 for (int i = 0; i < len; ++i)
11160 {
11161 if (vec)
11162 r = TREE_VEC_ELT (vec, i);
11163 if (VAR_P (r))
11164 {
11165 /* In [temp.inst]:
11166
11167 [t]he initialization (and any associated
11168 side-effects) of a static data member does
11169 not occur unless the static data member is
11170 itself used in a way that requires the
11171 definition of the static data member to
11172 exist.
11173
11174 Therefore, we do not substitute into the
11175 initialized for the static data member here. */
11176 finish_static_data_member_decl
11177 (r,
11178 /*init=*/NULL_TREE,
11179 /*init_const_expr_p=*/false,
11180 /*asmspec_tree=*/NULL_TREE,
11181 /*flags=*/0);
11182 /* Instantiate members marked with attribute used. */
11183 if (r != error_mark_node && DECL_PRESERVE_P (r))
11184 mark_used (r);
11185 }
11186 else if (TREE_CODE (r) == FIELD_DECL)
11187 {
11188 /* Determine whether R has a valid type and can be
11189 completed later. If R is invalid, then its type
11190 is replaced by error_mark_node. */
11191 tree rtype = TREE_TYPE (r);
11192 if (can_complete_type_without_circularity (rtype))
11193 complete_type (rtype);
11194
11195 if (!complete_or_array_type_p (rtype))
11196 {
11197 /* If R's type couldn't be completed and
11198 it isn't a flexible array member (whose
11199 type is incomplete by definition) give
11200 an error. */
11201 cxx_incomplete_type_error (r, rtype);
11202 TREE_TYPE (r) = error_mark_node;
11203 }
11204 else if (TREE_CODE (rtype) == ARRAY_TYPE
11205 && TYPE_DOMAIN (rtype) == NULL_TREE
11206 && (TREE_CODE (type) == UNION_TYPE
11207 || TREE_CODE (type) == QUAL_UNION_TYPE))
11208 {
11209 error ("flexible array member %qD in union", r);
11210 TREE_TYPE (r) = error_mark_node;
11211 }
11212 }
11213
11214 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11215 such a thing will already have been added to the field
11216 list by tsubst_enum in finish_member_declaration in the
11217 CLASSTYPE_NESTED_UTDS case above. */
11218 if (!(TREE_CODE (r) == TYPE_DECL
11219 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11220 && DECL_ARTIFICIAL (r)))
11221 {
11222 set_current_access_from_decl (r);
11223 finish_member_declaration (r);
11224 }
11225 }
11226 }
11227 }
11228 }
11229 else
11230 {
11231 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11232 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11233 {
11234 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11235
11236 tree friend_type = t;
11237 bool adjust_processing_template_decl = false;
11238
11239 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11240 {
11241 /* template <class T> friend class C; */
11242 friend_type = tsubst_friend_class (friend_type, args);
11243 adjust_processing_template_decl = true;
11244 }
11245 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11246 {
11247 /* template <class T> friend class C::D; */
11248 friend_type = tsubst (friend_type, args,
11249 tf_warning_or_error, NULL_TREE);
11250 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11251 friend_type = TREE_TYPE (friend_type);
11252 adjust_processing_template_decl = true;
11253 }
11254 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11255 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11256 {
11257 /* This could be either
11258
11259 friend class T::C;
11260
11261 when dependent_type_p is false or
11262
11263 template <class U> friend class T::C;
11264
11265 otherwise. */
11266 /* Bump processing_template_decl in case this is something like
11267 template <class T> friend struct A<T>::B. */
11268 ++processing_template_decl;
11269 friend_type = tsubst (friend_type, args,
11270 tf_warning_or_error, NULL_TREE);
11271 if (dependent_type_p (friend_type))
11272 adjust_processing_template_decl = true;
11273 --processing_template_decl;
11274 }
11275 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11276 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11277 && TYPE_HIDDEN_P (friend_type))
11278 {
11279 /* friend class C;
11280
11281 where C hasn't been declared yet. Let's lookup name
11282 from namespace scope directly, bypassing any name that
11283 come from dependent base class. */
11284 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11285
11286 /* The call to xref_tag_from_type does injection for friend
11287 classes. */
11288 push_nested_namespace (ns);
11289 friend_type =
11290 xref_tag_from_type (friend_type, NULL_TREE,
11291 /*tag_scope=*/ts_current);
11292 pop_nested_namespace (ns);
11293 }
11294 else if (uses_template_parms (friend_type))
11295 /* friend class C<T>; */
11296 friend_type = tsubst (friend_type, args,
11297 tf_warning_or_error, NULL_TREE);
11298 /* Otherwise it's
11299
11300 friend class C;
11301
11302 where C is already declared or
11303
11304 friend class C<int>;
11305
11306 We don't have to do anything in these cases. */
11307
11308 if (adjust_processing_template_decl)
11309 /* Trick make_friend_class into realizing that the friend
11310 we're adding is a template, not an ordinary class. It's
11311 important that we use make_friend_class since it will
11312 perform some error-checking and output cross-reference
11313 information. */
11314 ++processing_template_decl;
11315
11316 if (friend_type != error_mark_node)
11317 make_friend_class (type, friend_type, /*complain=*/false);
11318
11319 if (adjust_processing_template_decl)
11320 --processing_template_decl;
11321 }
11322 else
11323 {
11324 /* Build new DECL_FRIENDLIST. */
11325 tree r;
11326
11327 /* The file and line for this declaration, to
11328 assist in error message reporting. Since we
11329 called push_tinst_level above, we don't need to
11330 restore these. */
11331 input_location = DECL_SOURCE_LOCATION (t);
11332
11333 if (TREE_CODE (t) == TEMPLATE_DECL)
11334 {
11335 ++processing_template_decl;
11336 push_deferring_access_checks (dk_no_check);
11337 }
11338
11339 r = tsubst_friend_function (t, args);
11340 add_friend (type, r, /*complain=*/false);
11341 if (TREE_CODE (t) == TEMPLATE_DECL)
11342 {
11343 pop_deferring_access_checks ();
11344 --processing_template_decl;
11345 }
11346 }
11347 }
11348 }
11349
11350 if (fn_context)
11351 {
11352 /* Restore these before substituting into the lambda capture
11353 initializers. */
11354 cp_unevaluated_operand = saved_unevaluated_operand;
11355 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11356 }
11357
11358 /* Set the file and line number information to whatever is given for
11359 the class itself. This puts error messages involving generated
11360 implicit functions at a predictable point, and the same point
11361 that would be used for non-template classes. */
11362 input_location = DECL_SOURCE_LOCATION (typedecl);
11363
11364 unreverse_member_declarations (type);
11365 finish_struct_1 (type);
11366 TYPE_BEING_DEFINED (type) = 0;
11367
11368 /* We don't instantiate default arguments for member functions. 14.7.1:
11369
11370 The implicit instantiation of a class template specialization causes
11371 the implicit instantiation of the declarations, but not of the
11372 definitions or default arguments, of the class member functions,
11373 member classes, static data members and member templates.... */
11374
11375 /* Some typedefs referenced from within the template code need to be access
11376 checked at template instantiation time, i.e now. These types were
11377 added to the template at parsing time. Let's get those and perform
11378 the access checks then. */
11379 perform_typedefs_access_check (pattern, args);
11380 perform_deferred_access_checks (tf_warning_or_error);
11381 pop_nested_class ();
11382 maximum_field_alignment = saved_maximum_field_alignment;
11383 if (!fn_context)
11384 pop_from_top_level ();
11385 pop_tinst_level ();
11386
11387 /* The vtable for a template class can be emitted in any translation
11388 unit in which the class is instantiated. When there is no key
11389 method, however, finish_struct_1 will already have added TYPE to
11390 the keyed_classes. */
11391 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11392 vec_safe_push (keyed_classes, type);
11393
11394 return type;
11395 }
11396
11397 /* Wrapper for instantiate_class_template_1. */
11398
11399 tree
11400 instantiate_class_template (tree type)
11401 {
11402 tree ret;
11403 timevar_push (TV_TEMPLATE_INST);
11404 ret = instantiate_class_template_1 (type);
11405 timevar_pop (TV_TEMPLATE_INST);
11406 return ret;
11407 }
11408
11409 static tree
11410 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11411 {
11412 tree r;
11413
11414 if (!t)
11415 r = t;
11416 else if (TYPE_P (t))
11417 r = tsubst (t, args, complain, in_decl);
11418 else
11419 {
11420 if (!(complain & tf_warning))
11421 ++c_inhibit_evaluation_warnings;
11422 r = tsubst_expr (t, args, complain, in_decl,
11423 /*integral_constant_expression_p=*/true);
11424 if (!(complain & tf_warning))
11425 --c_inhibit_evaluation_warnings;
11426 }
11427 return r;
11428 }
11429
11430 /* Given a function parameter pack TMPL_PARM and some function parameters
11431 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11432 and set *SPEC_P to point at the next point in the list. */
11433
11434 tree
11435 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11436 {
11437 /* Collect all of the extra "packed" parameters into an
11438 argument pack. */
11439 tree parmvec;
11440 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11441 tree spec_parm = *spec_p;
11442 int i, len;
11443
11444 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11445 if (tmpl_parm
11446 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11447 break;
11448
11449 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11450 parmvec = make_tree_vec (len);
11451 spec_parm = *spec_p;
11452 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11453 {
11454 tree elt = spec_parm;
11455 if (DECL_PACK_P (elt))
11456 elt = make_pack_expansion (elt);
11457 TREE_VEC_ELT (parmvec, i) = elt;
11458 }
11459
11460 /* Build the argument packs. */
11461 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11462 *spec_p = spec_parm;
11463
11464 return argpack;
11465 }
11466
11467 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11468 NONTYPE_ARGUMENT_PACK. */
11469
11470 static tree
11471 make_fnparm_pack (tree spec_parm)
11472 {
11473 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11474 }
11475
11476 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11477 pack expansion with no extra args, 2 if it has extra args, or 0
11478 if it is not a pack expansion. */
11479
11480 static int
11481 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11482 {
11483 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11484 if (i >= TREE_VEC_LENGTH (vec))
11485 return 0;
11486 tree elt = TREE_VEC_ELT (vec, i);
11487 if (DECL_P (elt))
11488 /* A decl pack is itself an expansion. */
11489 elt = TREE_TYPE (elt);
11490 if (!PACK_EXPANSION_P (elt))
11491 return 0;
11492 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11493 return 2;
11494 return 1;
11495 }
11496
11497
11498 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11499
11500 static tree
11501 make_argument_pack_select (tree arg_pack, unsigned index)
11502 {
11503 tree aps = make_node (ARGUMENT_PACK_SELECT);
11504
11505 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11506 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11507
11508 return aps;
11509 }
11510
11511 /* This is a subroutine of tsubst_pack_expansion.
11512
11513 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11514 mechanism to store the (non complete list of) arguments of the
11515 substitution and return a non substituted pack expansion, in order
11516 to wait for when we have enough arguments to really perform the
11517 substitution. */
11518
11519 static bool
11520 use_pack_expansion_extra_args_p (tree parm_packs,
11521 int arg_pack_len,
11522 bool has_empty_arg)
11523 {
11524 /* If one pack has an expansion and another pack has a normal
11525 argument or if one pack has an empty argument and an another
11526 one hasn't then tsubst_pack_expansion cannot perform the
11527 substitution and need to fall back on the
11528 PACK_EXPANSION_EXTRA mechanism. */
11529 if (parm_packs == NULL_TREE)
11530 return false;
11531 else if (has_empty_arg)
11532 return true;
11533
11534 bool has_expansion_arg = false;
11535 for (int i = 0 ; i < arg_pack_len; ++i)
11536 {
11537 bool has_non_expansion_arg = false;
11538 for (tree parm_pack = parm_packs;
11539 parm_pack;
11540 parm_pack = TREE_CHAIN (parm_pack))
11541 {
11542 tree arg = TREE_VALUE (parm_pack);
11543
11544 int exp = argument_pack_element_is_expansion_p (arg, i);
11545 if (exp == 2)
11546 /* We can't substitute a pack expansion with extra args into
11547 our pattern. */
11548 return true;
11549 else if (exp)
11550 has_expansion_arg = true;
11551 else
11552 has_non_expansion_arg = true;
11553 }
11554
11555 if (has_expansion_arg && has_non_expansion_arg)
11556 return true;
11557 }
11558 return false;
11559 }
11560
11561 /* [temp.variadic]/6 says that:
11562
11563 The instantiation of a pack expansion [...]
11564 produces a list E1,E2, ..., En, where N is the number of elements
11565 in the pack expansion parameters.
11566
11567 This subroutine of tsubst_pack_expansion produces one of these Ei.
11568
11569 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11570 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11571 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11572 INDEX is the index 'i' of the element Ei to produce. ARGS,
11573 COMPLAIN, and IN_DECL are the same parameters as for the
11574 tsubst_pack_expansion function.
11575
11576 The function returns the resulting Ei upon successful completion,
11577 or error_mark_node.
11578
11579 Note that this function possibly modifies the ARGS parameter, so
11580 it's the responsibility of the caller to restore it. */
11581
11582 static tree
11583 gen_elem_of_pack_expansion_instantiation (tree pattern,
11584 tree parm_packs,
11585 unsigned index,
11586 tree args /* This parm gets
11587 modified. */,
11588 tsubst_flags_t complain,
11589 tree in_decl)
11590 {
11591 tree t;
11592 bool ith_elem_is_expansion = false;
11593
11594 /* For each parameter pack, change the substitution of the parameter
11595 pack to the ith argument in its argument pack, then expand the
11596 pattern. */
11597 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11598 {
11599 tree parm = TREE_PURPOSE (pack);
11600 tree arg_pack = TREE_VALUE (pack);
11601 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11602
11603 ith_elem_is_expansion |=
11604 argument_pack_element_is_expansion_p (arg_pack, index);
11605
11606 /* Select the Ith argument from the pack. */
11607 if (TREE_CODE (parm) == PARM_DECL
11608 || VAR_P (parm)
11609 || TREE_CODE (parm) == FIELD_DECL)
11610 {
11611 if (index == 0)
11612 {
11613 aps = make_argument_pack_select (arg_pack, index);
11614 if (!mark_used (parm, complain) && !(complain & tf_error))
11615 return error_mark_node;
11616 register_local_specialization (aps, parm);
11617 }
11618 else
11619 aps = retrieve_local_specialization (parm);
11620 }
11621 else
11622 {
11623 int idx, level;
11624 template_parm_level_and_index (parm, &level, &idx);
11625
11626 if (index == 0)
11627 {
11628 aps = make_argument_pack_select (arg_pack, index);
11629 /* Update the corresponding argument. */
11630 TMPL_ARG (args, level, idx) = aps;
11631 }
11632 else
11633 /* Re-use the ARGUMENT_PACK_SELECT. */
11634 aps = TMPL_ARG (args, level, idx);
11635 }
11636 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11637 }
11638
11639 /* Substitute into the PATTERN with the (possibly altered)
11640 arguments. */
11641 if (pattern == in_decl)
11642 /* Expanding a fixed parameter pack from
11643 coerce_template_parameter_pack. */
11644 t = tsubst_decl (pattern, args, complain);
11645 else if (pattern == error_mark_node)
11646 t = error_mark_node;
11647 else if (constraint_p (pattern))
11648 {
11649 if (processing_template_decl)
11650 t = tsubst_constraint (pattern, args, complain, in_decl);
11651 else
11652 t = (constraints_satisfied_p (pattern, args)
11653 ? boolean_true_node : boolean_false_node);
11654 }
11655 else if (!TYPE_P (pattern))
11656 t = tsubst_expr (pattern, args, complain, in_decl,
11657 /*integral_constant_expression_p=*/false);
11658 else
11659 t = tsubst (pattern, args, complain, in_decl);
11660
11661 /* If the Ith argument pack element is a pack expansion, then
11662 the Ith element resulting from the substituting is going to
11663 be a pack expansion as well. */
11664 if (ith_elem_is_expansion)
11665 t = make_pack_expansion (t, complain);
11666
11667 return t;
11668 }
11669
11670 /* When the unexpanded parameter pack in a fold expression expands to an empty
11671 sequence, the value of the expression is as follows; the program is
11672 ill-formed if the operator is not listed in this table.
11673
11674 && true
11675 || false
11676 , void() */
11677
11678 tree
11679 expand_empty_fold (tree t, tsubst_flags_t complain)
11680 {
11681 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11682 if (!FOLD_EXPR_MODIFY_P (t))
11683 switch (code)
11684 {
11685 case TRUTH_ANDIF_EXPR:
11686 return boolean_true_node;
11687 case TRUTH_ORIF_EXPR:
11688 return boolean_false_node;
11689 case COMPOUND_EXPR:
11690 return void_node;
11691 default:
11692 break;
11693 }
11694
11695 if (complain & tf_error)
11696 error_at (location_of (t),
11697 "fold of empty expansion over %O", code);
11698 return error_mark_node;
11699 }
11700
11701 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11702 form an expression that combines the two terms using the
11703 operator of T. */
11704
11705 static tree
11706 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11707 {
11708 tree op = FOLD_EXPR_OP (t);
11709 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11710
11711 // Handle compound assignment operators.
11712 if (FOLD_EXPR_MODIFY_P (t))
11713 return build_x_modify_expr (input_location, left, code, right, complain);
11714
11715 switch (code)
11716 {
11717 case COMPOUND_EXPR:
11718 return build_x_compound_expr (input_location, left, right, complain);
11719 case DOTSTAR_EXPR:
11720 return build_m_component_ref (left, right, complain);
11721 default:
11722 return build_x_binary_op (input_location, code,
11723 left, TREE_CODE (left),
11724 right, TREE_CODE (right),
11725 /*overload=*/NULL,
11726 complain);
11727 }
11728 }
11729
11730 /* Substitute ARGS into the pack of a fold expression T. */
11731
11732 static inline tree
11733 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11734 {
11735 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11736 }
11737
11738 /* Substitute ARGS into the pack of a fold expression T. */
11739
11740 static inline tree
11741 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11742 {
11743 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11744 }
11745
11746 /* Expand a PACK of arguments into a grouped as left fold.
11747 Given a pack containing elements A0, A1, ..., An and an
11748 operator @, this builds the expression:
11749
11750 ((A0 @ A1) @ A2) ... @ An
11751
11752 Note that PACK must not be empty.
11753
11754 The operator is defined by the original fold expression T. */
11755
11756 static tree
11757 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11758 {
11759 tree left = TREE_VEC_ELT (pack, 0);
11760 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11761 {
11762 tree right = TREE_VEC_ELT (pack, i);
11763 left = fold_expression (t, left, right, complain);
11764 }
11765 return left;
11766 }
11767
11768 /* Substitute into a unary left fold expression. */
11769
11770 static tree
11771 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11772 tree in_decl)
11773 {
11774 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11775 if (pack == error_mark_node)
11776 return error_mark_node;
11777 if (PACK_EXPANSION_P (pack))
11778 {
11779 tree r = copy_node (t);
11780 FOLD_EXPR_PACK (r) = pack;
11781 return r;
11782 }
11783 if (TREE_VEC_LENGTH (pack) == 0)
11784 return expand_empty_fold (t, complain);
11785 else
11786 return expand_left_fold (t, pack, complain);
11787 }
11788
11789 /* Substitute into a binary left fold expression.
11790
11791 Do ths by building a single (non-empty) vector of argumnts and
11792 building the expression from those elements. */
11793
11794 static tree
11795 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11796 tree in_decl)
11797 {
11798 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11799 if (pack == error_mark_node)
11800 return error_mark_node;
11801 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11802 if (init == error_mark_node)
11803 return error_mark_node;
11804
11805 if (PACK_EXPANSION_P (pack))
11806 {
11807 tree r = copy_node (t);
11808 FOLD_EXPR_PACK (r) = pack;
11809 FOLD_EXPR_INIT (r) = init;
11810 return r;
11811 }
11812
11813 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11814 TREE_VEC_ELT (vec, 0) = init;
11815 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11816 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11817
11818 return expand_left_fold (t, vec, complain);
11819 }
11820
11821 /* Expand a PACK of arguments into a grouped as right fold.
11822 Given a pack containing elementns A0, A1, ..., and an
11823 operator @, this builds the expression:
11824
11825 A0@ ... (An-2 @ (An-1 @ An))
11826
11827 Note that PACK must not be empty.
11828
11829 The operator is defined by the original fold expression T. */
11830
11831 tree
11832 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11833 {
11834 // Build the expression.
11835 int n = TREE_VEC_LENGTH (pack);
11836 tree right = TREE_VEC_ELT (pack, n - 1);
11837 for (--n; n != 0; --n)
11838 {
11839 tree left = TREE_VEC_ELT (pack, n - 1);
11840 right = fold_expression (t, left, right, complain);
11841 }
11842 return right;
11843 }
11844
11845 /* Substitute into a unary right fold expression. */
11846
11847 static tree
11848 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11849 tree in_decl)
11850 {
11851 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11852 if (pack == error_mark_node)
11853 return error_mark_node;
11854 if (PACK_EXPANSION_P (pack))
11855 {
11856 tree r = copy_node (t);
11857 FOLD_EXPR_PACK (r) = pack;
11858 return r;
11859 }
11860 if (TREE_VEC_LENGTH (pack) == 0)
11861 return expand_empty_fold (t, complain);
11862 else
11863 return expand_right_fold (t, pack, complain);
11864 }
11865
11866 /* Substitute into a binary right fold expression.
11867
11868 Do ths by building a single (non-empty) vector of arguments and
11869 building the expression from those elements. */
11870
11871 static tree
11872 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11873 tree in_decl)
11874 {
11875 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11876 if (pack == error_mark_node)
11877 return error_mark_node;
11878 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11879 if (init == error_mark_node)
11880 return error_mark_node;
11881
11882 if (PACK_EXPANSION_P (pack))
11883 {
11884 tree r = copy_node (t);
11885 FOLD_EXPR_PACK (r) = pack;
11886 FOLD_EXPR_INIT (r) = init;
11887 return r;
11888 }
11889
11890 int n = TREE_VEC_LENGTH (pack);
11891 tree vec = make_tree_vec (n + 1);
11892 for (int i = 0; i < n; ++i)
11893 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11894 TREE_VEC_ELT (vec, n) = init;
11895
11896 return expand_right_fold (t, vec, complain);
11897 }
11898
11899 /* Walk through the pattern of a pack expansion, adding everything in
11900 local_specializations to a list. */
11901
11902 struct el_data
11903 {
11904 hash_set<tree> internal;
11905 tree extra;
11906 tsubst_flags_t complain;
11907
11908 el_data (tsubst_flags_t c)
11909 : extra (NULL_TREE), complain (c) {}
11910 };
11911 static tree
11912 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11913 {
11914 el_data &data = *reinterpret_cast<el_data*>(data_);
11915 tree *extra = &data.extra;
11916 tsubst_flags_t complain = data.complain;
11917
11918 if (TYPE_P (*tp) && typedef_variant_p (*tp))
11919 /* Remember local typedefs (85214). */
11920 tp = &TYPE_NAME (*tp);
11921
11922 if (TREE_CODE (*tp) == DECL_EXPR)
11923 data.internal.add (DECL_EXPR_DECL (*tp));
11924 else if (tree spec = retrieve_local_specialization (*tp))
11925 {
11926 if (data.internal.contains (*tp))
11927 /* Don't mess with variables declared within the pattern. */
11928 return NULL_TREE;
11929 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11930 {
11931 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11932 tree args = ARGUMENT_PACK_ARGS (spec);
11933 if (TREE_VEC_LENGTH (args) == 1)
11934 {
11935 tree elt = TREE_VEC_ELT (args, 0);
11936 if (PACK_EXPANSION_P (elt))
11937 elt = PACK_EXPANSION_PATTERN (elt);
11938 if (DECL_PACK_P (elt))
11939 spec = elt;
11940 }
11941 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11942 {
11943 /* Handle lambda capture here, since we aren't doing any
11944 substitution now, and so tsubst_copy won't call
11945 process_outer_var_ref. */
11946 tree args = ARGUMENT_PACK_ARGS (spec);
11947 int len = TREE_VEC_LENGTH (args);
11948 for (int i = 0; i < len; ++i)
11949 {
11950 tree arg = TREE_VEC_ELT (args, i);
11951 tree carg = arg;
11952 if (outer_automatic_var_p (arg))
11953 carg = process_outer_var_ref (arg, complain);
11954 if (carg != arg)
11955 {
11956 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11957 proxies. */
11958 if (i == 0)
11959 {
11960 spec = copy_node (spec);
11961 args = copy_node (args);
11962 SET_ARGUMENT_PACK_ARGS (spec, args);
11963 register_local_specialization (spec, *tp);
11964 }
11965 TREE_VEC_ELT (args, i) = carg;
11966 }
11967 }
11968 }
11969 }
11970 if (outer_automatic_var_p (spec))
11971 spec = process_outer_var_ref (spec, complain);
11972 *extra = tree_cons (*tp, spec, *extra);
11973 }
11974 return NULL_TREE;
11975 }
11976 static tree
11977 extract_local_specs (tree pattern, tsubst_flags_t complain)
11978 {
11979 el_data data (complain);
11980 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11981 return data.extra;
11982 }
11983
11984 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
11985 for use in PACK_EXPANSION_EXTRA_ARGS. */
11986
11987 tree
11988 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
11989 {
11990 tree extra = args;
11991 if (local_specializations)
11992 if (tree locals = extract_local_specs (pattern, complain))
11993 extra = tree_cons (NULL_TREE, extra, locals);
11994 return extra;
11995 }
11996
11997 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
11998 normal template args to ARGS. */
11999
12000 tree
12001 add_extra_args (tree extra, tree args)
12002 {
12003 if (extra && TREE_CODE (extra) == TREE_LIST)
12004 {
12005 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12006 {
12007 /* The partial instantiation involved local declarations collected in
12008 extract_local_specs; map from the general template to our local
12009 context. */
12010 tree gen = TREE_PURPOSE (elt);
12011 tree inst = TREE_VALUE (elt);
12012 if (DECL_P (inst))
12013 if (tree local = retrieve_local_specialization (inst))
12014 inst = local;
12015 /* else inst is already a full instantiation of the pack. */
12016 register_local_specialization (inst, gen);
12017 }
12018 gcc_assert (!TREE_PURPOSE (extra));
12019 extra = TREE_VALUE (extra);
12020 }
12021 return add_to_template_args (extra, args);
12022 }
12023
12024 /* Substitute ARGS into T, which is an pack expansion
12025 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12026 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12027 (if only a partial substitution could be performed) or
12028 ERROR_MARK_NODE if there was an error. */
12029 tree
12030 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12031 tree in_decl)
12032 {
12033 tree pattern;
12034 tree pack, packs = NULL_TREE;
12035 bool unsubstituted_packs = false;
12036 bool unsubstituted_fn_pack = false;
12037 int i, len = -1;
12038 tree result;
12039 hash_map<tree, tree> *saved_local_specializations = NULL;
12040 bool need_local_specializations = false;
12041 int levels;
12042
12043 gcc_assert (PACK_EXPANSION_P (t));
12044 pattern = PACK_EXPANSION_PATTERN (t);
12045
12046 /* Add in any args remembered from an earlier partial instantiation. */
12047 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12048
12049 levels = TMPL_ARGS_DEPTH (args);
12050
12051 /* Determine the argument packs that will instantiate the parameter
12052 packs used in the expansion expression. While we're at it,
12053 compute the number of arguments to be expanded and make sure it
12054 is consistent. */
12055 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12056 pack = TREE_CHAIN (pack))
12057 {
12058 tree parm_pack = TREE_VALUE (pack);
12059 tree arg_pack = NULL_TREE;
12060 tree orig_arg = NULL_TREE;
12061 int level = 0;
12062
12063 if (TREE_CODE (parm_pack) == BASES)
12064 {
12065 gcc_assert (parm_pack == pattern);
12066 if (BASES_DIRECT (parm_pack))
12067 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12068 args, complain,
12069 in_decl, false),
12070 complain);
12071 else
12072 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12073 args, complain, in_decl,
12074 false), complain);
12075 }
12076 else if (builtin_pack_call_p (parm_pack))
12077 {
12078 if (parm_pack != pattern)
12079 {
12080 if (complain & tf_error)
12081 sorry ("%qE is not the entire pattern of the pack expansion",
12082 parm_pack);
12083 return error_mark_node;
12084 }
12085 return expand_builtin_pack_call (parm_pack, args,
12086 complain, in_decl);
12087 }
12088 else if (TREE_CODE (parm_pack) == PARM_DECL)
12089 {
12090 /* We know we have correct local_specializations if this
12091 expansion is at function scope, or if we're dealing with a
12092 local parameter in a requires expression; for the latter,
12093 tsubst_requires_expr set it up appropriately. */
12094 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12095 arg_pack = retrieve_local_specialization (parm_pack);
12096 else
12097 /* We can't rely on local_specializations for a parameter
12098 name used later in a function declaration (such as in a
12099 late-specified return type). Even if it exists, it might
12100 have the wrong value for a recursive call. */
12101 need_local_specializations = true;
12102
12103 if (!arg_pack)
12104 {
12105 /* This parameter pack was used in an unevaluated context. Just
12106 make a dummy decl, since it's only used for its type. */
12107 ++cp_unevaluated_operand;
12108 arg_pack = tsubst_decl (parm_pack, args, complain);
12109 --cp_unevaluated_operand;
12110 if (arg_pack && DECL_PACK_P (arg_pack))
12111 /* Partial instantiation of the parm_pack, we can't build
12112 up an argument pack yet. */
12113 arg_pack = NULL_TREE;
12114 else
12115 arg_pack = make_fnparm_pack (arg_pack);
12116 }
12117 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12118 /* This argument pack isn't fully instantiated yet. We set this
12119 flag rather than clear arg_pack because we do want to do the
12120 optimization below, and we don't want to substitute directly
12121 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12122 where it isn't expected). */
12123 unsubstituted_fn_pack = true;
12124 }
12125 else if (is_normal_capture_proxy (parm_pack))
12126 {
12127 arg_pack = retrieve_local_specialization (parm_pack);
12128 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12129 unsubstituted_fn_pack = true;
12130 }
12131 else
12132 {
12133 int idx;
12134 template_parm_level_and_index (parm_pack, &level, &idx);
12135
12136 if (level <= levels)
12137 arg_pack = TMPL_ARG (args, level, idx);
12138 }
12139
12140 orig_arg = arg_pack;
12141 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12142 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12143
12144 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12145 /* This can only happen if we forget to expand an argument
12146 pack somewhere else. Just return an error, silently. */
12147 {
12148 result = make_tree_vec (1);
12149 TREE_VEC_ELT (result, 0) = error_mark_node;
12150 return result;
12151 }
12152
12153 if (arg_pack)
12154 {
12155 int my_len =
12156 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12157
12158 /* Don't bother trying to do a partial substitution with
12159 incomplete packs; we'll try again after deduction. */
12160 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12161 return t;
12162
12163 if (len < 0)
12164 len = my_len;
12165 else if (len != my_len
12166 && !unsubstituted_fn_pack)
12167 {
12168 if (!(complain & tf_error))
12169 /* Fail quietly. */;
12170 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12171 error ("mismatched argument pack lengths while expanding %qT",
12172 pattern);
12173 else
12174 error ("mismatched argument pack lengths while expanding %qE",
12175 pattern);
12176 return error_mark_node;
12177 }
12178
12179 /* Keep track of the parameter packs and their corresponding
12180 argument packs. */
12181 packs = tree_cons (parm_pack, arg_pack, packs);
12182 TREE_TYPE (packs) = orig_arg;
12183 }
12184 else
12185 {
12186 /* We can't substitute for this parameter pack. We use a flag as
12187 well as the missing_level counter because function parameter
12188 packs don't have a level. */
12189 gcc_assert (processing_template_decl || is_auto (parm_pack));
12190 unsubstituted_packs = true;
12191 }
12192 }
12193
12194 /* If the expansion is just T..., return the matching argument pack, unless
12195 we need to call convert_from_reference on all the elements. This is an
12196 important optimization; see c++/68422. */
12197 if (!unsubstituted_packs
12198 && TREE_PURPOSE (packs) == pattern)
12199 {
12200 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12201
12202 /* If the argument pack is a single pack expansion, pull it out. */
12203 if (TREE_VEC_LENGTH (args) == 1
12204 && pack_expansion_args_count (args))
12205 return TREE_VEC_ELT (args, 0);
12206
12207 /* Types need no adjustment, nor does sizeof..., and if we still have
12208 some pack expansion args we won't do anything yet. */
12209 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12210 || PACK_EXPANSION_SIZEOF_P (t)
12211 || pack_expansion_args_count (args))
12212 return args;
12213 /* Also optimize expression pack expansions if we can tell that the
12214 elements won't have reference type. */
12215 tree type = TREE_TYPE (pattern);
12216 if (type && !TYPE_REF_P (type)
12217 && !PACK_EXPANSION_P (type)
12218 && !WILDCARD_TYPE_P (type))
12219 return args;
12220 /* Otherwise use the normal path so we get convert_from_reference. */
12221 }
12222
12223 /* We cannot expand this expansion expression, because we don't have
12224 all of the argument packs we need. */
12225 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12226 {
12227 /* We got some full packs, but we can't substitute them in until we
12228 have values for all the packs. So remember these until then. */
12229
12230 t = make_pack_expansion (pattern, complain);
12231 PACK_EXPANSION_EXTRA_ARGS (t)
12232 = build_extra_args (pattern, args, complain);
12233 return t;
12234 }
12235 else if (unsubstituted_packs)
12236 {
12237 /* There were no real arguments, we're just replacing a parameter
12238 pack with another version of itself. Substitute into the
12239 pattern and return a PACK_EXPANSION_*. The caller will need to
12240 deal with that. */
12241 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12242 t = tsubst_expr (pattern, args, complain, in_decl,
12243 /*integral_constant_expression_p=*/false);
12244 else
12245 t = tsubst (pattern, args, complain, in_decl);
12246 t = make_pack_expansion (t, complain);
12247 return t;
12248 }
12249
12250 gcc_assert (len >= 0);
12251
12252 if (need_local_specializations)
12253 {
12254 /* We're in a late-specified return type, so create our own local
12255 specializations map; the current map is either NULL or (in the
12256 case of recursive unification) might have bindings that we don't
12257 want to use or alter. */
12258 saved_local_specializations = local_specializations;
12259 local_specializations = new hash_map<tree, tree>;
12260 }
12261
12262 /* For each argument in each argument pack, substitute into the
12263 pattern. */
12264 result = make_tree_vec (len);
12265 tree elem_args = copy_template_args (args);
12266 for (i = 0; i < len; ++i)
12267 {
12268 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12269 i,
12270 elem_args, complain,
12271 in_decl);
12272 TREE_VEC_ELT (result, i) = t;
12273 if (t == error_mark_node)
12274 {
12275 result = error_mark_node;
12276 break;
12277 }
12278 }
12279
12280 /* Update ARGS to restore the substitution from parameter packs to
12281 their argument packs. */
12282 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12283 {
12284 tree parm = TREE_PURPOSE (pack);
12285
12286 if (TREE_CODE (parm) == PARM_DECL
12287 || VAR_P (parm)
12288 || TREE_CODE (parm) == FIELD_DECL)
12289 register_local_specialization (TREE_TYPE (pack), parm);
12290 else
12291 {
12292 int idx, level;
12293
12294 if (TREE_VALUE (pack) == NULL_TREE)
12295 continue;
12296
12297 template_parm_level_and_index (parm, &level, &idx);
12298
12299 /* Update the corresponding argument. */
12300 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12301 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12302 TREE_TYPE (pack);
12303 else
12304 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12305 }
12306 }
12307
12308 if (need_local_specializations)
12309 {
12310 delete local_specializations;
12311 local_specializations = saved_local_specializations;
12312 }
12313
12314 /* If the dependent pack arguments were such that we end up with only a
12315 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12316 if (len == 1 && TREE_CODE (result) == TREE_VEC
12317 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12318 return TREE_VEC_ELT (result, 0);
12319
12320 return result;
12321 }
12322
12323 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12324 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12325 parameter packs; all parms generated from a function parameter pack will
12326 have the same DECL_PARM_INDEX. */
12327
12328 tree
12329 get_pattern_parm (tree parm, tree tmpl)
12330 {
12331 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12332 tree patparm;
12333
12334 if (DECL_ARTIFICIAL (parm))
12335 {
12336 for (patparm = DECL_ARGUMENTS (pattern);
12337 patparm; patparm = DECL_CHAIN (patparm))
12338 if (DECL_ARTIFICIAL (patparm)
12339 && DECL_NAME (parm) == DECL_NAME (patparm))
12340 break;
12341 }
12342 else
12343 {
12344 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12345 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12346 gcc_assert (DECL_PARM_INDEX (patparm)
12347 == DECL_PARM_INDEX (parm));
12348 }
12349
12350 return patparm;
12351 }
12352
12353 /* Make an argument pack out of the TREE_VEC VEC. */
12354
12355 static tree
12356 make_argument_pack (tree vec)
12357 {
12358 tree pack;
12359 tree elt = TREE_VEC_ELT (vec, 0);
12360 if (TYPE_P (elt))
12361 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12362 else
12363 {
12364 pack = make_node (NONTYPE_ARGUMENT_PACK);
12365 TREE_CONSTANT (pack) = 1;
12366 }
12367 SET_ARGUMENT_PACK_ARGS (pack, vec);
12368 return pack;
12369 }
12370
12371 /* Return an exact copy of template args T that can be modified
12372 independently. */
12373
12374 static tree
12375 copy_template_args (tree t)
12376 {
12377 if (t == error_mark_node)
12378 return t;
12379
12380 int len = TREE_VEC_LENGTH (t);
12381 tree new_vec = make_tree_vec (len);
12382
12383 for (int i = 0; i < len; ++i)
12384 {
12385 tree elt = TREE_VEC_ELT (t, i);
12386 if (elt && TREE_CODE (elt) == TREE_VEC)
12387 elt = copy_template_args (elt);
12388 TREE_VEC_ELT (new_vec, i) = elt;
12389 }
12390
12391 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12392 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12393
12394 return new_vec;
12395 }
12396
12397 /* Substitute ARGS into the vector or list of template arguments T. */
12398
12399 static tree
12400 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12401 {
12402 tree orig_t = t;
12403 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12404 tree *elts;
12405
12406 if (t == error_mark_node)
12407 return error_mark_node;
12408
12409 len = TREE_VEC_LENGTH (t);
12410 elts = XALLOCAVEC (tree, len);
12411
12412 for (i = 0; i < len; i++)
12413 {
12414 tree orig_arg = TREE_VEC_ELT (t, i);
12415 tree new_arg;
12416
12417 if (TREE_CODE (orig_arg) == TREE_VEC)
12418 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12419 else if (PACK_EXPANSION_P (orig_arg))
12420 {
12421 /* Substitute into an expansion expression. */
12422 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12423
12424 if (TREE_CODE (new_arg) == TREE_VEC)
12425 /* Add to the expanded length adjustment the number of
12426 expanded arguments. We subtract one from this
12427 measurement, because the argument pack expression
12428 itself is already counted as 1 in
12429 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12430 the argument pack is empty. */
12431 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12432 }
12433 else if (ARGUMENT_PACK_P (orig_arg))
12434 {
12435 /* Substitute into each of the arguments. */
12436 new_arg = TYPE_P (orig_arg)
12437 ? cxx_make_type (TREE_CODE (orig_arg))
12438 : make_node (TREE_CODE (orig_arg));
12439
12440 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12441 args, complain, in_decl);
12442 if (pack_args == error_mark_node)
12443 new_arg = error_mark_node;
12444 else
12445 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12446
12447 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12448 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12449 }
12450 else
12451 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12452
12453 if (new_arg == error_mark_node)
12454 return error_mark_node;
12455
12456 elts[i] = new_arg;
12457 if (new_arg != orig_arg)
12458 need_new = 1;
12459 }
12460
12461 if (!need_new)
12462 return t;
12463
12464 /* Make space for the expanded arguments coming from template
12465 argument packs. */
12466 t = make_tree_vec (len + expanded_len_adjust);
12467 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12468 arguments for a member template.
12469 In that case each TREE_VEC in ORIG_T represents a level of template
12470 arguments, and ORIG_T won't carry any non defaulted argument count.
12471 It will rather be the nested TREE_VECs that will carry one.
12472 In other words, ORIG_T carries a non defaulted argument count only
12473 if it doesn't contain any nested TREE_VEC. */
12474 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12475 {
12476 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12477 count += expanded_len_adjust;
12478 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12479 }
12480 for (i = 0, out = 0; i < len; i++)
12481 {
12482 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12483 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12484 && TREE_CODE (elts[i]) == TREE_VEC)
12485 {
12486 int idx;
12487
12488 /* Now expand the template argument pack "in place". */
12489 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12490 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12491 }
12492 else
12493 {
12494 TREE_VEC_ELT (t, out) = elts[i];
12495 out++;
12496 }
12497 }
12498
12499 return t;
12500 }
12501
12502 /* Substitute ARGS into one level PARMS of template parameters. */
12503
12504 static tree
12505 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12506 {
12507 if (parms == error_mark_node)
12508 return error_mark_node;
12509
12510 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12511
12512 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12513 {
12514 tree tuple = TREE_VEC_ELT (parms, i);
12515
12516 if (tuple == error_mark_node)
12517 continue;
12518
12519 TREE_VEC_ELT (new_vec, i) =
12520 tsubst_template_parm (tuple, args, complain);
12521 }
12522
12523 return new_vec;
12524 }
12525
12526 /* Return the result of substituting ARGS into the template parameters
12527 given by PARMS. If there are m levels of ARGS and m + n levels of
12528 PARMS, then the result will contain n levels of PARMS. For
12529 example, if PARMS is `template <class T> template <class U>
12530 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12531 result will be `template <int*, double, class V>'. */
12532
12533 static tree
12534 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12535 {
12536 tree r = NULL_TREE;
12537 tree* new_parms;
12538
12539 /* When substituting into a template, we must set
12540 PROCESSING_TEMPLATE_DECL as the template parameters may be
12541 dependent if they are based on one-another, and the dependency
12542 predicates are short-circuit outside of templates. */
12543 ++processing_template_decl;
12544
12545 for (new_parms = &r;
12546 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12547 new_parms = &(TREE_CHAIN (*new_parms)),
12548 parms = TREE_CHAIN (parms))
12549 {
12550 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12551 args, complain);
12552 *new_parms =
12553 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12554 - TMPL_ARGS_DEPTH (args)),
12555 new_vec, NULL_TREE);
12556 }
12557
12558 --processing_template_decl;
12559
12560 return r;
12561 }
12562
12563 /* Return the result of substituting ARGS into one template parameter
12564 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12565 parameter and which TREE_PURPOSE is the default argument of the
12566 template parameter. */
12567
12568 static tree
12569 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12570 {
12571 tree default_value, parm_decl;
12572
12573 if (args == NULL_TREE
12574 || t == NULL_TREE
12575 || t == error_mark_node)
12576 return t;
12577
12578 gcc_assert (TREE_CODE (t) == TREE_LIST);
12579
12580 default_value = TREE_PURPOSE (t);
12581 parm_decl = TREE_VALUE (t);
12582
12583 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12584 if (TREE_CODE (parm_decl) == PARM_DECL
12585 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12586 parm_decl = error_mark_node;
12587 default_value = tsubst_template_arg (default_value, args,
12588 complain, NULL_TREE);
12589
12590 return build_tree_list (default_value, parm_decl);
12591 }
12592
12593 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12594 type T. If T is not an aggregate or enumeration type, it is
12595 handled as if by tsubst. IN_DECL is as for tsubst. If
12596 ENTERING_SCOPE is nonzero, T is the context for a template which
12597 we are presently tsubst'ing. Return the substituted value. */
12598
12599 static tree
12600 tsubst_aggr_type (tree t,
12601 tree args,
12602 tsubst_flags_t complain,
12603 tree in_decl,
12604 int entering_scope)
12605 {
12606 if (t == NULL_TREE)
12607 return NULL_TREE;
12608
12609 switch (TREE_CODE (t))
12610 {
12611 case RECORD_TYPE:
12612 if (TYPE_PTRMEMFUNC_P (t))
12613 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12614
12615 /* Fall through. */
12616 case ENUMERAL_TYPE:
12617 case UNION_TYPE:
12618 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12619 {
12620 tree argvec;
12621 tree context;
12622 tree r;
12623 int saved_unevaluated_operand;
12624 int saved_inhibit_evaluation_warnings;
12625
12626 /* In "sizeof(X<I>)" we need to evaluate "I". */
12627 saved_unevaluated_operand = cp_unevaluated_operand;
12628 cp_unevaluated_operand = 0;
12629 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12630 c_inhibit_evaluation_warnings = 0;
12631
12632 /* First, determine the context for the type we are looking
12633 up. */
12634 context = TYPE_CONTEXT (t);
12635 if (context && TYPE_P (context))
12636 {
12637 context = tsubst_aggr_type (context, args, complain,
12638 in_decl, /*entering_scope=*/1);
12639 /* If context is a nested class inside a class template,
12640 it may still need to be instantiated (c++/33959). */
12641 context = complete_type (context);
12642 }
12643
12644 /* Then, figure out what arguments are appropriate for the
12645 type we are trying to find. For example, given:
12646
12647 template <class T> struct S;
12648 template <class T, class U> void f(T, U) { S<U> su; }
12649
12650 and supposing that we are instantiating f<int, double>,
12651 then our ARGS will be {int, double}, but, when looking up
12652 S we only want {double}. */
12653 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12654 complain, in_decl);
12655 if (argvec == error_mark_node)
12656 r = error_mark_node;
12657 else
12658 {
12659 r = lookup_template_class (t, argvec, in_decl, context,
12660 entering_scope, complain);
12661 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12662 }
12663
12664 cp_unevaluated_operand = saved_unevaluated_operand;
12665 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12666
12667 return r;
12668 }
12669 else
12670 /* This is not a template type, so there's nothing to do. */
12671 return t;
12672
12673 default:
12674 return tsubst (t, args, complain, in_decl);
12675 }
12676 }
12677
12678 static GTY((cache)) tree_cache_map *defarg_inst;
12679
12680 /* Substitute into the default argument ARG (a default argument for
12681 FN), which has the indicated TYPE. */
12682
12683 tree
12684 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12685 tsubst_flags_t complain)
12686 {
12687 int errs = errorcount + sorrycount;
12688
12689 /* This can happen in invalid code. */
12690 if (TREE_CODE (arg) == DEFAULT_ARG)
12691 return arg;
12692
12693 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12694 parm = chain_index (parmnum, parm);
12695 tree parmtype = TREE_TYPE (parm);
12696 if (DECL_BY_REFERENCE (parm))
12697 parmtype = TREE_TYPE (parmtype);
12698 if (parmtype == error_mark_node)
12699 return error_mark_node;
12700
12701 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12702
12703 tree *slot;
12704 if (defarg_inst && (slot = defarg_inst->get (parm)))
12705 return *slot;
12706
12707 /* This default argument came from a template. Instantiate the
12708 default argument here, not in tsubst. In the case of
12709 something like:
12710
12711 template <class T>
12712 struct S {
12713 static T t();
12714 void f(T = t());
12715 };
12716
12717 we must be careful to do name lookup in the scope of S<T>,
12718 rather than in the current class. */
12719 push_to_top_level ();
12720 push_access_scope (fn);
12721 start_lambda_scope (parm);
12722
12723 /* The default argument expression may cause implicitly defined
12724 member functions to be synthesized, which will result in garbage
12725 collection. We must treat this situation as if we were within
12726 the body of function so as to avoid collecting live data on the
12727 stack. */
12728 ++function_depth;
12729 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12730 complain, NULL_TREE,
12731 /*integral_constant_expression_p=*/false);
12732 --function_depth;
12733
12734 finish_lambda_scope ();
12735
12736 if (errorcount+sorrycount > errs
12737 && (complain & tf_warning_or_error))
12738 inform (input_location,
12739 " when instantiating default argument for call to %qD", fn);
12740
12741 /* Make sure the default argument is reasonable. */
12742 arg = check_default_argument (type, arg, complain);
12743
12744 pop_access_scope (fn);
12745 pop_from_top_level ();
12746
12747 if (arg != error_mark_node && !cp_unevaluated_operand)
12748 {
12749 if (!defarg_inst)
12750 defarg_inst = tree_cache_map::create_ggc (37);
12751 defarg_inst->put (parm, arg);
12752 }
12753
12754 return arg;
12755 }
12756
12757 /* Substitute into all the default arguments for FN. */
12758
12759 static void
12760 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12761 {
12762 tree arg;
12763 tree tmpl_args;
12764
12765 tmpl_args = DECL_TI_ARGS (fn);
12766
12767 /* If this function is not yet instantiated, we certainly don't need
12768 its default arguments. */
12769 if (uses_template_parms (tmpl_args))
12770 return;
12771 /* Don't do this again for clones. */
12772 if (DECL_CLONED_FUNCTION_P (fn))
12773 return;
12774
12775 int i = 0;
12776 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12777 arg;
12778 arg = TREE_CHAIN (arg), ++i)
12779 if (TREE_PURPOSE (arg))
12780 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12781 TREE_VALUE (arg),
12782 TREE_PURPOSE (arg),
12783 complain);
12784 }
12785
12786 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12787
12788 static tree
12789 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12790 tree lambda_fntype)
12791 {
12792 tree gen_tmpl, argvec;
12793 hashval_t hash = 0;
12794 tree in_decl = t;
12795
12796 /* Nobody should be tsubst'ing into non-template functions. */
12797 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12798
12799 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12800 {
12801 /* If T is not dependent, just return it. */
12802 if (!uses_template_parms (DECL_TI_ARGS (t)))
12803 return t;
12804
12805 /* Calculate the most general template of which R is a
12806 specialization. */
12807 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12808
12809 /* We're substituting a lambda function under tsubst_lambda_expr but not
12810 directly from it; find the matching function we're already inside.
12811 But don't do this if T is a generic lambda with a single level of
12812 template parms, as in that case we're doing a normal instantiation. */
12813 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12814 && (!generic_lambda_fn_p (t)
12815 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12816 return enclosing_instantiation_of (t);
12817
12818 /* Calculate the complete set of arguments used to
12819 specialize R. */
12820 argvec = tsubst_template_args (DECL_TI_ARGS
12821 (DECL_TEMPLATE_RESULT
12822 (DECL_TI_TEMPLATE (t))),
12823 args, complain, in_decl);
12824 if (argvec == error_mark_node)
12825 return error_mark_node;
12826
12827 /* Check to see if we already have this specialization. */
12828 if (!lambda_fntype)
12829 {
12830 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12831 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12832 return spec;
12833 }
12834
12835 /* We can see more levels of arguments than parameters if
12836 there was a specialization of a member template, like
12837 this:
12838
12839 template <class T> struct S { template <class U> void f(); }
12840 template <> template <class U> void S<int>::f(U);
12841
12842 Here, we'll be substituting into the specialization,
12843 because that's where we can find the code we actually
12844 want to generate, but we'll have enough arguments for
12845 the most general template.
12846
12847 We also deal with the peculiar case:
12848
12849 template <class T> struct S {
12850 template <class U> friend void f();
12851 };
12852 template <class U> void f() {}
12853 template S<int>;
12854 template void f<double>();
12855
12856 Here, the ARGS for the instantiation of will be {int,
12857 double}. But, we only need as many ARGS as there are
12858 levels of template parameters in CODE_PATTERN. We are
12859 careful not to get fooled into reducing the ARGS in
12860 situations like:
12861
12862 template <class T> struct S { template <class U> void f(U); }
12863 template <class T> template <> void S<T>::f(int) {}
12864
12865 which we can spot because the pattern will be a
12866 specialization in this case. */
12867 int args_depth = TMPL_ARGS_DEPTH (args);
12868 int parms_depth =
12869 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12870
12871 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12872 args = get_innermost_template_args (args, parms_depth);
12873 }
12874 else
12875 {
12876 /* This special case arises when we have something like this:
12877
12878 template <class T> struct S {
12879 friend void f<int>(int, double);
12880 };
12881
12882 Here, the DECL_TI_TEMPLATE for the friend declaration
12883 will be an IDENTIFIER_NODE. We are being called from
12884 tsubst_friend_function, and we want only to create a
12885 new decl (R) with appropriate types so that we can call
12886 determine_specialization. */
12887 gen_tmpl = NULL_TREE;
12888 argvec = NULL_TREE;
12889 }
12890
12891 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12892 : NULL_TREE);
12893 tree ctx = closure ? closure : DECL_CONTEXT (t);
12894 bool member = ctx && TYPE_P (ctx);
12895
12896 if (member && !closure)
12897 ctx = tsubst_aggr_type (ctx, args,
12898 complain, t, /*entering_scope=*/1);
12899
12900 tree type = (lambda_fntype ? lambda_fntype
12901 : tsubst (TREE_TYPE (t), args,
12902 complain | tf_fndecl_type, in_decl));
12903 if (type == error_mark_node)
12904 return error_mark_node;
12905
12906 /* If we hit excessive deduction depth, the type is bogus even if
12907 it isn't error_mark_node, so don't build a decl. */
12908 if (excessive_deduction_depth)
12909 return error_mark_node;
12910
12911 /* We do NOT check for matching decls pushed separately at this
12912 point, as they may not represent instantiations of this
12913 template, and in any case are considered separate under the
12914 discrete model. */
12915 tree r = copy_decl (t);
12916 DECL_USE_TEMPLATE (r) = 0;
12917 TREE_TYPE (r) = type;
12918 /* Clear out the mangled name and RTL for the instantiation. */
12919 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12920 SET_DECL_RTL (r, NULL);
12921 /* Leave DECL_INITIAL set on deleted instantiations. */
12922 if (!DECL_DELETED_FN (r))
12923 DECL_INITIAL (r) = NULL_TREE;
12924 DECL_CONTEXT (r) = ctx;
12925
12926 /* OpenMP UDRs have the only argument a reference to the declared
12927 type. We want to diagnose if the declared type is a reference,
12928 which is invalid, but as references to references are usually
12929 quietly merged, diagnose it here. */
12930 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12931 {
12932 tree argtype
12933 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12934 argtype = tsubst (argtype, args, complain, in_decl);
12935 if (TYPE_REF_P (argtype))
12936 error_at (DECL_SOURCE_LOCATION (t),
12937 "reference type %qT in "
12938 "%<#pragma omp declare reduction%>", argtype);
12939 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12940 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12941 argtype);
12942 }
12943
12944 if (member && DECL_CONV_FN_P (r))
12945 /* Type-conversion operator. Reconstruct the name, in
12946 case it's the name of one of the template's parameters. */
12947 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12948
12949 tree parms = DECL_ARGUMENTS (t);
12950 if (closure)
12951 parms = DECL_CHAIN (parms);
12952 parms = tsubst (parms, args, complain, t);
12953 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12954 DECL_CONTEXT (parm) = r;
12955 if (closure)
12956 {
12957 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12958 DECL_CHAIN (tparm) = parms;
12959 parms = tparm;
12960 }
12961 DECL_ARGUMENTS (r) = parms;
12962 DECL_RESULT (r) = NULL_TREE;
12963
12964 TREE_STATIC (r) = 0;
12965 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12966 DECL_EXTERNAL (r) = 1;
12967 /* If this is an instantiation of a function with internal
12968 linkage, we already know what object file linkage will be
12969 assigned to the instantiation. */
12970 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12971 DECL_DEFER_OUTPUT (r) = 0;
12972 DECL_CHAIN (r) = NULL_TREE;
12973 DECL_PENDING_INLINE_INFO (r) = 0;
12974 DECL_PENDING_INLINE_P (r) = 0;
12975 DECL_SAVED_TREE (r) = NULL_TREE;
12976 DECL_STRUCT_FUNCTION (r) = NULL;
12977 TREE_USED (r) = 0;
12978 /* We'll re-clone as appropriate in instantiate_template. */
12979 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12980
12981 /* If we aren't complaining now, return on error before we register
12982 the specialization so that we'll complain eventually. */
12983 if ((complain & tf_error) == 0
12984 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12985 && !grok_op_properties (r, /*complain=*/false))
12986 return error_mark_node;
12987
12988 /* When instantiating a constrained member, substitute
12989 into the constraints to create a new constraint. */
12990 if (tree ci = get_constraints (t))
12991 if (member)
12992 {
12993 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12994 set_constraints (r, ci);
12995 }
12996
12997 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12998 this in the special friend case mentioned above where
12999 GEN_TMPL is NULL. */
13000 if (gen_tmpl && !closure)
13001 {
13002 DECL_TEMPLATE_INFO (r)
13003 = build_template_info (gen_tmpl, argvec);
13004 SET_DECL_IMPLICIT_INSTANTIATION (r);
13005
13006 tree new_r
13007 = register_specialization (r, gen_tmpl, argvec, false, hash);
13008 if (new_r != r)
13009 /* We instantiated this while substituting into
13010 the type earlier (template/friend54.C). */
13011 return new_r;
13012
13013 /* We're not supposed to instantiate default arguments
13014 until they are called, for a template. But, for a
13015 declaration like:
13016
13017 template <class T> void f ()
13018 { extern void g(int i = T()); }
13019
13020 we should do the substitution when the template is
13021 instantiated. We handle the member function case in
13022 instantiate_class_template since the default arguments
13023 might refer to other members of the class. */
13024 if (!member
13025 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13026 && !uses_template_parms (argvec))
13027 tsubst_default_arguments (r, complain);
13028 }
13029 else
13030 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13031
13032 /* Copy the list of befriending classes. */
13033 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13034 *friends;
13035 friends = &TREE_CHAIN (*friends))
13036 {
13037 *friends = copy_node (*friends);
13038 TREE_VALUE (*friends)
13039 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13040 }
13041
13042 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13043 {
13044 maybe_retrofit_in_chrg (r);
13045 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13046 return error_mark_node;
13047 /* If this is an instantiation of a member template, clone it.
13048 If it isn't, that'll be handled by
13049 clone_constructors_and_destructors. */
13050 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13051 clone_function_decl (r, /*update_methods=*/false);
13052 }
13053 else if ((complain & tf_error) != 0
13054 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13055 && !grok_op_properties (r, /*complain=*/true))
13056 return error_mark_node;
13057
13058 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13059 SET_DECL_FRIEND_CONTEXT (r,
13060 tsubst (DECL_FRIEND_CONTEXT (t),
13061 args, complain, in_decl));
13062
13063 /* Possibly limit visibility based on template args. */
13064 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13065 if (DECL_VISIBILITY_SPECIFIED (t))
13066 {
13067 DECL_VISIBILITY_SPECIFIED (r) = 0;
13068 DECL_ATTRIBUTES (r)
13069 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13070 }
13071 determine_visibility (r);
13072 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13073 && !processing_template_decl)
13074 defaulted_late_check (r);
13075
13076 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13077 args, complain, in_decl);
13078 return r;
13079 }
13080
13081 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13082
13083 static tree
13084 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13085 tree lambda_fntype)
13086 {
13087 /* We can get here when processing a member function template,
13088 member class template, or template template parameter. */
13089 tree decl = DECL_TEMPLATE_RESULT (t);
13090 tree in_decl = t;
13091 tree spec;
13092 tree tmpl_args;
13093 tree full_args;
13094 tree r;
13095 hashval_t hash = 0;
13096
13097 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13098 {
13099 /* Template template parameter is treated here. */
13100 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13101 if (new_type == error_mark_node)
13102 r = error_mark_node;
13103 /* If we get a real template back, return it. This can happen in
13104 the context of most_specialized_partial_spec. */
13105 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13106 r = new_type;
13107 else
13108 /* The new TEMPLATE_DECL was built in
13109 reduce_template_parm_level. */
13110 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13111 return r;
13112 }
13113
13114 if (!lambda_fntype)
13115 {
13116 /* We might already have an instance of this template.
13117 The ARGS are for the surrounding class type, so the
13118 full args contain the tsubst'd args for the context,
13119 plus the innermost args from the template decl. */
13120 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13121 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13122 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13123 /* Because this is a template, the arguments will still be
13124 dependent, even after substitution. If
13125 PROCESSING_TEMPLATE_DECL is not set, the dependency
13126 predicates will short-circuit. */
13127 ++processing_template_decl;
13128 full_args = tsubst_template_args (tmpl_args, args,
13129 complain, in_decl);
13130 --processing_template_decl;
13131 if (full_args == error_mark_node)
13132 return error_mark_node;
13133
13134 /* If this is a default template template argument,
13135 tsubst might not have changed anything. */
13136 if (full_args == tmpl_args)
13137 return t;
13138
13139 hash = hash_tmpl_and_args (t, full_args);
13140 spec = retrieve_specialization (t, full_args, hash);
13141 if (spec != NULL_TREE)
13142 return spec;
13143 }
13144
13145 /* Make a new template decl. It will be similar to the
13146 original, but will record the current template arguments.
13147 We also create a new function declaration, which is just
13148 like the old one, but points to this new template, rather
13149 than the old one. */
13150 r = copy_decl (t);
13151 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13152 DECL_CHAIN (r) = NULL_TREE;
13153
13154 // Build new template info linking to the original template decl.
13155 if (!lambda_fntype)
13156 {
13157 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13158 SET_DECL_IMPLICIT_INSTANTIATION (r);
13159 }
13160 else
13161 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13162
13163 /* The template parameters for this new template are all the
13164 template parameters for the old template, except the
13165 outermost level of parameters. */
13166 DECL_TEMPLATE_PARMS (r)
13167 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13168 complain);
13169
13170 if (TREE_CODE (decl) == TYPE_DECL
13171 && !TYPE_DECL_ALIAS_P (decl))
13172 {
13173 tree new_type;
13174 ++processing_template_decl;
13175 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13176 --processing_template_decl;
13177 if (new_type == error_mark_node)
13178 return error_mark_node;
13179
13180 TREE_TYPE (r) = new_type;
13181 /* For a partial specialization, we need to keep pointing to
13182 the primary template. */
13183 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13184 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13185 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13186 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13187 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13188 }
13189 else
13190 {
13191 tree new_decl;
13192 ++processing_template_decl;
13193 if (TREE_CODE (decl) == FUNCTION_DECL)
13194 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13195 else
13196 new_decl = tsubst (decl, args, complain, in_decl);
13197 --processing_template_decl;
13198 if (new_decl == error_mark_node)
13199 return error_mark_node;
13200
13201 DECL_TEMPLATE_RESULT (r) = new_decl;
13202 TREE_TYPE (r) = TREE_TYPE (new_decl);
13203 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13204 if (lambda_fntype)
13205 {
13206 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13207 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13208 }
13209 else
13210 {
13211 DECL_TI_TEMPLATE (new_decl) = r;
13212 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13213 }
13214 }
13215
13216 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13217 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13218
13219 if (PRIMARY_TEMPLATE_P (t))
13220 DECL_PRIMARY_TEMPLATE (r) = r;
13221
13222 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13223 && !lambda_fntype)
13224 /* Record this non-type partial instantiation. */
13225 register_specialization (r, t,
13226 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13227 false, hash);
13228
13229 return r;
13230 }
13231
13232 /* True if FN is the op() for a lambda in an uninstantiated template. */
13233
13234 bool
13235 lambda_fn_in_template_p (tree fn)
13236 {
13237 if (!fn || !LAMBDA_FUNCTION_P (fn))
13238 return false;
13239 tree closure = DECL_CONTEXT (fn);
13240 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13241 }
13242
13243 /* We're instantiating a variable from template function TCTX. Return the
13244 corresponding current enclosing scope. This gets complicated because lambda
13245 functions in templates are regenerated rather than instantiated, but generic
13246 lambda functions are subsequently instantiated. */
13247
13248 static tree
13249 enclosing_instantiation_of (tree otctx)
13250 {
13251 tree tctx = otctx;
13252 tree fn = current_function_decl;
13253 int lambda_count = 0;
13254
13255 for (; tctx && lambda_fn_in_template_p (tctx);
13256 tctx = decl_function_context (tctx))
13257 ++lambda_count;
13258 for (; fn; fn = decl_function_context (fn))
13259 {
13260 tree ofn = fn;
13261 int flambda_count = 0;
13262 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
13263 fn = decl_function_context (fn))
13264 ++flambda_count;
13265 if ((fn && DECL_TEMPLATE_INFO (fn))
13266 ? most_general_template (fn) != most_general_template (tctx)
13267 : fn != tctx)
13268 continue;
13269 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13270 || DECL_CONV_FN_P (ofn));
13271 return ofn;
13272 }
13273 gcc_unreachable ();
13274 }
13275
13276 /* Substitute the ARGS into the T, which is a _DECL. Return the
13277 result of the substitution. Issue error and warning messages under
13278 control of COMPLAIN. */
13279
13280 static tree
13281 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13282 {
13283 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13284 location_t saved_loc;
13285 tree r = NULL_TREE;
13286 tree in_decl = t;
13287 hashval_t hash = 0;
13288
13289 /* Set the filename and linenumber to improve error-reporting. */
13290 saved_loc = input_location;
13291 input_location = DECL_SOURCE_LOCATION (t);
13292
13293 switch (TREE_CODE (t))
13294 {
13295 case TEMPLATE_DECL:
13296 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13297 break;
13298
13299 case FUNCTION_DECL:
13300 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13301 break;
13302
13303 case PARM_DECL:
13304 {
13305 tree type = NULL_TREE;
13306 int i, len = 1;
13307 tree expanded_types = NULL_TREE;
13308 tree prev_r = NULL_TREE;
13309 tree first_r = NULL_TREE;
13310
13311 if (DECL_PACK_P (t))
13312 {
13313 /* If there is a local specialization that isn't a
13314 parameter pack, it means that we're doing a "simple"
13315 substitution from inside tsubst_pack_expansion. Just
13316 return the local specialization (which will be a single
13317 parm). */
13318 tree spec = retrieve_local_specialization (t);
13319 if (spec
13320 && TREE_CODE (spec) == PARM_DECL
13321 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13322 RETURN (spec);
13323
13324 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13325 the parameters in this function parameter pack. */
13326 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13327 complain, in_decl);
13328 if (TREE_CODE (expanded_types) == TREE_VEC)
13329 {
13330 len = TREE_VEC_LENGTH (expanded_types);
13331
13332 /* Zero-length parameter packs are boring. Just substitute
13333 into the chain. */
13334 if (len == 0)
13335 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13336 TREE_CHAIN (t)));
13337 }
13338 else
13339 {
13340 /* All we did was update the type. Make a note of that. */
13341 type = expanded_types;
13342 expanded_types = NULL_TREE;
13343 }
13344 }
13345
13346 /* Loop through all of the parameters we'll build. When T is
13347 a function parameter pack, LEN is the number of expanded
13348 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13349 r = NULL_TREE;
13350 for (i = 0; i < len; ++i)
13351 {
13352 prev_r = r;
13353 r = copy_node (t);
13354 if (DECL_TEMPLATE_PARM_P (t))
13355 SET_DECL_TEMPLATE_PARM_P (r);
13356
13357 if (expanded_types)
13358 /* We're on the Ith parameter of the function parameter
13359 pack. */
13360 {
13361 /* Get the Ith type. */
13362 type = TREE_VEC_ELT (expanded_types, i);
13363
13364 /* Rename the parameter to include the index. */
13365 DECL_NAME (r)
13366 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13367 }
13368 else if (!type)
13369 /* We're dealing with a normal parameter. */
13370 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13371
13372 type = type_decays_to (type);
13373 TREE_TYPE (r) = type;
13374 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13375
13376 if (DECL_INITIAL (r))
13377 {
13378 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13379 DECL_INITIAL (r) = TREE_TYPE (r);
13380 else
13381 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13382 complain, in_decl);
13383 }
13384
13385 DECL_CONTEXT (r) = NULL_TREE;
13386
13387 if (!DECL_TEMPLATE_PARM_P (r))
13388 DECL_ARG_TYPE (r) = type_passed_as (type);
13389
13390 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13391 args, complain, in_decl);
13392
13393 /* Keep track of the first new parameter we
13394 generate. That's what will be returned to the
13395 caller. */
13396 if (!first_r)
13397 first_r = r;
13398
13399 /* Build a proper chain of parameters when substituting
13400 into a function parameter pack. */
13401 if (prev_r)
13402 DECL_CHAIN (prev_r) = r;
13403 }
13404
13405 /* If cp_unevaluated_operand is set, we're just looking for a
13406 single dummy parameter, so don't keep going. */
13407 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13408 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13409 complain, DECL_CHAIN (t));
13410
13411 /* FIRST_R contains the start of the chain we've built. */
13412 r = first_r;
13413 }
13414 break;
13415
13416 case FIELD_DECL:
13417 {
13418 tree type = NULL_TREE;
13419 tree vec = NULL_TREE;
13420 tree expanded_types = NULL_TREE;
13421 int len = 1;
13422
13423 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13424 {
13425 /* This field is a lambda capture pack. Return a TREE_VEC of
13426 the expanded fields to instantiate_class_template_1. */
13427 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13428 complain, in_decl);
13429 if (TREE_CODE (expanded_types) == TREE_VEC)
13430 {
13431 len = TREE_VEC_LENGTH (expanded_types);
13432 vec = make_tree_vec (len);
13433 }
13434 else
13435 {
13436 /* All we did was update the type. Make a note of that. */
13437 type = expanded_types;
13438 expanded_types = NULL_TREE;
13439 }
13440 }
13441
13442 for (int i = 0; i < len; ++i)
13443 {
13444 r = copy_decl (t);
13445 if (expanded_types)
13446 {
13447 type = TREE_VEC_ELT (expanded_types, i);
13448 DECL_NAME (r)
13449 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13450 }
13451 else if (!type)
13452 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13453
13454 if (type == error_mark_node)
13455 RETURN (error_mark_node);
13456 TREE_TYPE (r) = type;
13457 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13458
13459 if (DECL_C_BIT_FIELD (r))
13460 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13461 number of bits. */
13462 DECL_BIT_FIELD_REPRESENTATIVE (r)
13463 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13464 complain, in_decl,
13465 /*integral_constant_expression_p=*/true);
13466 if (DECL_INITIAL (t))
13467 {
13468 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13469 NSDMI in perform_member_init. Still set DECL_INITIAL
13470 so that we know there is one. */
13471 DECL_INITIAL (r) = void_node;
13472 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13473 retrofit_lang_decl (r);
13474 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13475 }
13476 /* We don't have to set DECL_CONTEXT here; it is set by
13477 finish_member_declaration. */
13478 DECL_CHAIN (r) = NULL_TREE;
13479
13480 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13481 args, complain, in_decl);
13482
13483 if (vec)
13484 TREE_VEC_ELT (vec, i) = r;
13485 }
13486
13487 if (vec)
13488 r = vec;
13489 }
13490 break;
13491
13492 case USING_DECL:
13493 /* We reach here only for member using decls. We also need to check
13494 uses_template_parms because DECL_DEPENDENT_P is not set for a
13495 using-declaration that designates a member of the current
13496 instantiation (c++/53549). */
13497 if (DECL_DEPENDENT_P (t)
13498 || uses_template_parms (USING_DECL_SCOPE (t)))
13499 {
13500 tree scope = USING_DECL_SCOPE (t);
13501 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13502 if (PACK_EXPANSION_P (scope))
13503 {
13504 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13505 int len = TREE_VEC_LENGTH (vec);
13506 r = make_tree_vec (len);
13507 for (int i = 0; i < len; ++i)
13508 {
13509 tree escope = TREE_VEC_ELT (vec, i);
13510 tree elt = do_class_using_decl (escope, name);
13511 if (!elt)
13512 {
13513 r = error_mark_node;
13514 break;
13515 }
13516 else
13517 {
13518 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13519 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13520 }
13521 TREE_VEC_ELT (r, i) = elt;
13522 }
13523 }
13524 else
13525 {
13526 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13527 complain, in_decl);
13528 r = do_class_using_decl (inst_scope, name);
13529 if (!r)
13530 r = error_mark_node;
13531 else
13532 {
13533 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13534 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13535 }
13536 }
13537 }
13538 else
13539 {
13540 r = copy_node (t);
13541 DECL_CHAIN (r) = NULL_TREE;
13542 }
13543 break;
13544
13545 case TYPE_DECL:
13546 case VAR_DECL:
13547 {
13548 tree argvec = NULL_TREE;
13549 tree gen_tmpl = NULL_TREE;
13550 tree spec;
13551 tree tmpl = NULL_TREE;
13552 tree ctx;
13553 tree type = NULL_TREE;
13554 bool local_p;
13555
13556 if (TREE_TYPE (t) == error_mark_node)
13557 RETURN (error_mark_node);
13558
13559 if (TREE_CODE (t) == TYPE_DECL
13560 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13561 {
13562 /* If this is the canonical decl, we don't have to
13563 mess with instantiations, and often we can't (for
13564 typename, template type parms and such). Note that
13565 TYPE_NAME is not correct for the above test if
13566 we've copied the type for a typedef. */
13567 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13568 if (type == error_mark_node)
13569 RETURN (error_mark_node);
13570 r = TYPE_NAME (type);
13571 break;
13572 }
13573
13574 /* Check to see if we already have the specialization we
13575 need. */
13576 spec = NULL_TREE;
13577 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13578 {
13579 /* T is a static data member or namespace-scope entity.
13580 We have to substitute into namespace-scope variables
13581 (not just variable templates) because of cases like:
13582
13583 template <class T> void f() { extern T t; }
13584
13585 where the entity referenced is not known until
13586 instantiation time. */
13587 local_p = false;
13588 ctx = DECL_CONTEXT (t);
13589 if (DECL_CLASS_SCOPE_P (t))
13590 {
13591 ctx = tsubst_aggr_type (ctx, args,
13592 complain,
13593 in_decl, /*entering_scope=*/1);
13594 /* If CTX is unchanged, then T is in fact the
13595 specialization we want. That situation occurs when
13596 referencing a static data member within in its own
13597 class. We can use pointer equality, rather than
13598 same_type_p, because DECL_CONTEXT is always
13599 canonical... */
13600 if (ctx == DECL_CONTEXT (t)
13601 /* ... unless T is a member template; in which
13602 case our caller can be willing to create a
13603 specialization of that template represented
13604 by T. */
13605 && !(DECL_TI_TEMPLATE (t)
13606 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13607 spec = t;
13608 }
13609
13610 if (!spec)
13611 {
13612 tmpl = DECL_TI_TEMPLATE (t);
13613 gen_tmpl = most_general_template (tmpl);
13614 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13615 if (argvec != error_mark_node)
13616 argvec = (coerce_innermost_template_parms
13617 (DECL_TEMPLATE_PARMS (gen_tmpl),
13618 argvec, t, complain,
13619 /*all*/true, /*defarg*/true));
13620 if (argvec == error_mark_node)
13621 RETURN (error_mark_node);
13622 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13623 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13624 }
13625 }
13626 else
13627 {
13628 /* A local variable. */
13629 local_p = true;
13630 /* Subsequent calls to pushdecl will fill this in. */
13631 ctx = NULL_TREE;
13632 /* Unless this is a reference to a static variable from an
13633 enclosing function, in which case we need to fill it in now. */
13634 if (TREE_STATIC (t))
13635 {
13636 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13637 if (fn != current_function_decl)
13638 ctx = fn;
13639 }
13640 spec = retrieve_local_specialization (t);
13641 }
13642 /* If we already have the specialization we need, there is
13643 nothing more to do. */
13644 if (spec)
13645 {
13646 r = spec;
13647 break;
13648 }
13649
13650 /* Create a new node for the specialization we need. */
13651 if (type == NULL_TREE)
13652 {
13653 if (is_typedef_decl (t))
13654 type = DECL_ORIGINAL_TYPE (t);
13655 else
13656 type = TREE_TYPE (t);
13657 if (VAR_P (t)
13658 && VAR_HAD_UNKNOWN_BOUND (t)
13659 && type != error_mark_node)
13660 type = strip_array_domain (type);
13661 tree sub_args = args;
13662 if (tree auto_node = type_uses_auto (type))
13663 {
13664 /* Mask off any template args past the variable's context so we
13665 don't replace the auto with an unrelated argument. */
13666 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13667 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13668 if (extra > 0)
13669 /* This should never happen with the new lambda instantiation
13670 model, but keep the handling just in case. */
13671 gcc_assert (!CHECKING_P),
13672 sub_args = strip_innermost_template_args (args, extra);
13673 }
13674 type = tsubst (type, sub_args, complain, in_decl);
13675 /* Substituting the type might have recursively instantiated this
13676 same alias (c++/86171). */
13677 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13678 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13679 {
13680 r = spec;
13681 break;
13682 }
13683 }
13684 r = copy_decl (t);
13685 if (VAR_P (r))
13686 {
13687 DECL_INITIALIZED_P (r) = 0;
13688 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13689 if (type == error_mark_node)
13690 RETURN (error_mark_node);
13691 if (TREE_CODE (type) == FUNCTION_TYPE)
13692 {
13693 /* It may seem that this case cannot occur, since:
13694
13695 typedef void f();
13696 void g() { f x; }
13697
13698 declares a function, not a variable. However:
13699
13700 typedef void f();
13701 template <typename T> void g() { T t; }
13702 template void g<f>();
13703
13704 is an attempt to declare a variable with function
13705 type. */
13706 error ("variable %qD has function type",
13707 /* R is not yet sufficiently initialized, so we
13708 just use its name. */
13709 DECL_NAME (r));
13710 RETURN (error_mark_node);
13711 }
13712 type = complete_type (type);
13713 /* Wait until cp_finish_decl to set this again, to handle
13714 circular dependency (template/instantiate6.C). */
13715 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13716 type = check_var_type (DECL_NAME (r), type);
13717
13718 if (DECL_HAS_VALUE_EXPR_P (t))
13719 {
13720 tree ve = DECL_VALUE_EXPR (t);
13721 ve = tsubst_expr (ve, args, complain, in_decl,
13722 /*constant_expression_p=*/false);
13723 if (REFERENCE_REF_P (ve))
13724 {
13725 gcc_assert (TYPE_REF_P (type));
13726 ve = TREE_OPERAND (ve, 0);
13727 }
13728 SET_DECL_VALUE_EXPR (r, ve);
13729 }
13730 if (CP_DECL_THREAD_LOCAL_P (r)
13731 && !processing_template_decl)
13732 set_decl_tls_model (r, decl_default_tls_model (r));
13733 }
13734 else if (DECL_SELF_REFERENCE_P (t))
13735 SET_DECL_SELF_REFERENCE_P (r);
13736 TREE_TYPE (r) = type;
13737 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13738 DECL_CONTEXT (r) = ctx;
13739 /* Clear out the mangled name and RTL for the instantiation. */
13740 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13741 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13742 SET_DECL_RTL (r, NULL);
13743 /* The initializer must not be expanded until it is required;
13744 see [temp.inst]. */
13745 DECL_INITIAL (r) = NULL_TREE;
13746 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13747 if (VAR_P (r))
13748 {
13749 if (DECL_LANG_SPECIFIC (r))
13750 SET_DECL_DEPENDENT_INIT_P (r, false);
13751
13752 SET_DECL_MODE (r, VOIDmode);
13753
13754 /* Possibly limit visibility based on template args. */
13755 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13756 if (DECL_VISIBILITY_SPECIFIED (t))
13757 {
13758 DECL_VISIBILITY_SPECIFIED (r) = 0;
13759 DECL_ATTRIBUTES (r)
13760 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13761 }
13762 determine_visibility (r);
13763 }
13764
13765 if (!local_p)
13766 {
13767 /* A static data member declaration is always marked
13768 external when it is declared in-class, even if an
13769 initializer is present. We mimic the non-template
13770 processing here. */
13771 DECL_EXTERNAL (r) = 1;
13772 if (DECL_NAMESPACE_SCOPE_P (t))
13773 DECL_NOT_REALLY_EXTERN (r) = 1;
13774
13775 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13776 SET_DECL_IMPLICIT_INSTANTIATION (r);
13777 register_specialization (r, gen_tmpl, argvec, false, hash);
13778 }
13779 else
13780 {
13781 if (DECL_LANG_SPECIFIC (r))
13782 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13783 if (!cp_unevaluated_operand)
13784 register_local_specialization (r, t);
13785 }
13786
13787 DECL_CHAIN (r) = NULL_TREE;
13788
13789 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13790 /*flags=*/0,
13791 args, complain, in_decl);
13792
13793 /* Preserve a typedef that names a type. */
13794 if (is_typedef_decl (r) && type != error_mark_node)
13795 {
13796 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13797 set_underlying_type (r);
13798 if (TYPE_DECL_ALIAS_P (r))
13799 /* An alias template specialization can be dependent
13800 even if its underlying type is not. */
13801 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13802 }
13803
13804 layout_decl (r, 0);
13805 }
13806 break;
13807
13808 default:
13809 gcc_unreachable ();
13810 }
13811 #undef RETURN
13812
13813 out:
13814 /* Restore the file and line information. */
13815 input_location = saved_loc;
13816
13817 return r;
13818 }
13819
13820 /* Substitute into the ARG_TYPES of a function type.
13821 If END is a TREE_CHAIN, leave it and any following types
13822 un-substituted. */
13823
13824 static tree
13825 tsubst_arg_types (tree arg_types,
13826 tree args,
13827 tree end,
13828 tsubst_flags_t complain,
13829 tree in_decl)
13830 {
13831 tree remaining_arg_types;
13832 tree type = NULL_TREE;
13833 int i = 1;
13834 tree expanded_args = NULL_TREE;
13835 tree default_arg;
13836
13837 if (!arg_types || arg_types == void_list_node || arg_types == end)
13838 return arg_types;
13839
13840 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13841 args, end, complain, in_decl);
13842 if (remaining_arg_types == error_mark_node)
13843 return error_mark_node;
13844
13845 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13846 {
13847 /* For a pack expansion, perform substitution on the
13848 entire expression. Later on, we'll handle the arguments
13849 one-by-one. */
13850 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13851 args, complain, in_decl);
13852
13853 if (TREE_CODE (expanded_args) == TREE_VEC)
13854 /* So that we'll spin through the parameters, one by one. */
13855 i = TREE_VEC_LENGTH (expanded_args);
13856 else
13857 {
13858 /* We only partially substituted into the parameter
13859 pack. Our type is TYPE_PACK_EXPANSION. */
13860 type = expanded_args;
13861 expanded_args = NULL_TREE;
13862 }
13863 }
13864
13865 while (i > 0) {
13866 --i;
13867
13868 if (expanded_args)
13869 type = TREE_VEC_ELT (expanded_args, i);
13870 else if (!type)
13871 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13872
13873 if (type == error_mark_node)
13874 return error_mark_node;
13875 if (VOID_TYPE_P (type))
13876 {
13877 if (complain & tf_error)
13878 {
13879 error ("invalid parameter type %qT", type);
13880 if (in_decl)
13881 error ("in declaration %q+D", in_decl);
13882 }
13883 return error_mark_node;
13884 }
13885 /* DR 657. */
13886 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13887 return error_mark_node;
13888
13889 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13890 top-level qualifiers as required. */
13891 type = cv_unqualified (type_decays_to (type));
13892
13893 /* We do not substitute into default arguments here. The standard
13894 mandates that they be instantiated only when needed, which is
13895 done in build_over_call. */
13896 default_arg = TREE_PURPOSE (arg_types);
13897
13898 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13899 since the new op() won't have any associated template arguments for us
13900 to refer to later. */
13901 if (lambda_fn_in_template_p (in_decl))
13902 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13903 false/*fn*/, false/*constexpr*/);
13904
13905 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13906 {
13907 /* We've instantiated a template before its default arguments
13908 have been parsed. This can happen for a nested template
13909 class, and is not an error unless we require the default
13910 argument in a call of this function. */
13911 remaining_arg_types =
13912 tree_cons (default_arg, type, remaining_arg_types);
13913 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13914 }
13915 else
13916 remaining_arg_types =
13917 hash_tree_cons (default_arg, type, remaining_arg_types);
13918 }
13919
13920 return remaining_arg_types;
13921 }
13922
13923 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13924 *not* handle the exception-specification for FNTYPE, because the
13925 initial substitution of explicitly provided template parameters
13926 during argument deduction forbids substitution into the
13927 exception-specification:
13928
13929 [temp.deduct]
13930
13931 All references in the function type of the function template to the
13932 corresponding template parameters are replaced by the specified tem-
13933 plate argument values. If a substitution in a template parameter or
13934 in the function type of the function template results in an invalid
13935 type, type deduction fails. [Note: The equivalent substitution in
13936 exception specifications is done only when the function is instanti-
13937 ated, at which point a program is ill-formed if the substitution
13938 results in an invalid type.] */
13939
13940 static tree
13941 tsubst_function_type (tree t,
13942 tree args,
13943 tsubst_flags_t complain,
13944 tree in_decl)
13945 {
13946 tree return_type;
13947 tree arg_types = NULL_TREE;
13948 tree fntype;
13949
13950 /* The TYPE_CONTEXT is not used for function/method types. */
13951 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13952
13953 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13954 failure. */
13955 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13956
13957 if (late_return_type_p)
13958 {
13959 /* Substitute the argument types. */
13960 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13961 complain, in_decl);
13962 if (arg_types == error_mark_node)
13963 return error_mark_node;
13964
13965 tree save_ccp = current_class_ptr;
13966 tree save_ccr = current_class_ref;
13967 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13968 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13969 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13970 if (do_inject)
13971 {
13972 /* DR 1207: 'this' is in scope in the trailing return type. */
13973 inject_this_parameter (this_type, cp_type_quals (this_type));
13974 }
13975
13976 /* Substitute the return type. */
13977 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13978
13979 if (do_inject)
13980 {
13981 current_class_ptr = save_ccp;
13982 current_class_ref = save_ccr;
13983 }
13984 }
13985 else
13986 /* Substitute the return type. */
13987 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13988
13989 if (return_type == error_mark_node)
13990 return error_mark_node;
13991 /* DR 486 clarifies that creation of a function type with an
13992 invalid return type is a deduction failure. */
13993 if (TREE_CODE (return_type) == ARRAY_TYPE
13994 || TREE_CODE (return_type) == FUNCTION_TYPE)
13995 {
13996 if (complain & tf_error)
13997 {
13998 if (TREE_CODE (return_type) == ARRAY_TYPE)
13999 error ("function returning an array");
14000 else
14001 error ("function returning a function");
14002 }
14003 return error_mark_node;
14004 }
14005 /* And DR 657. */
14006 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14007 return error_mark_node;
14008
14009 if (!late_return_type_p)
14010 {
14011 /* Substitute the argument types. */
14012 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14013 complain, in_decl);
14014 if (arg_types == error_mark_node)
14015 return error_mark_node;
14016 }
14017
14018 /* Construct a new type node and return it. */
14019 if (TREE_CODE (t) == FUNCTION_TYPE)
14020 {
14021 fntype = build_function_type (return_type, arg_types);
14022 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14023 }
14024 else
14025 {
14026 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14027 /* Don't pick up extra function qualifiers from the basetype. */
14028 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14029 if (! MAYBE_CLASS_TYPE_P (r))
14030 {
14031 /* [temp.deduct]
14032
14033 Type deduction may fail for any of the following
14034 reasons:
14035
14036 -- Attempting to create "pointer to member of T" when T
14037 is not a class type. */
14038 if (complain & tf_error)
14039 error ("creating pointer to member function of non-class type %qT",
14040 r);
14041 return error_mark_node;
14042 }
14043
14044 fntype = build_method_type_directly (r, return_type,
14045 TREE_CHAIN (arg_types));
14046 }
14047 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14048
14049 /* See comment above. */
14050 tree raises = NULL_TREE;
14051 cp_ref_qualifier rqual = type_memfn_rqual (t);
14052 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14053
14054 return fntype;
14055 }
14056
14057 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14058 ARGS into that specification, and return the substituted
14059 specification. If there is no specification, return NULL_TREE. */
14060
14061 static tree
14062 tsubst_exception_specification (tree fntype,
14063 tree args,
14064 tsubst_flags_t complain,
14065 tree in_decl,
14066 bool defer_ok)
14067 {
14068 tree specs;
14069 tree new_specs;
14070
14071 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14072 new_specs = NULL_TREE;
14073 if (specs && TREE_PURPOSE (specs))
14074 {
14075 /* A noexcept-specifier. */
14076 tree expr = TREE_PURPOSE (specs);
14077 if (TREE_CODE (expr) == INTEGER_CST)
14078 new_specs = expr;
14079 else if (defer_ok)
14080 {
14081 /* Defer instantiation of noexcept-specifiers to avoid
14082 excessive instantiations (c++/49107). */
14083 new_specs = make_node (DEFERRED_NOEXCEPT);
14084 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14085 {
14086 /* We already partially instantiated this member template,
14087 so combine the new args with the old. */
14088 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14089 = DEFERRED_NOEXCEPT_PATTERN (expr);
14090 DEFERRED_NOEXCEPT_ARGS (new_specs)
14091 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14092 }
14093 else
14094 {
14095 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14096 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14097 }
14098 }
14099 else
14100 new_specs = tsubst_copy_and_build
14101 (expr, args, complain, in_decl, /*function_p=*/false,
14102 /*integral_constant_expression_p=*/true);
14103 new_specs = build_noexcept_spec (new_specs, complain);
14104 }
14105 else if (specs)
14106 {
14107 if (! TREE_VALUE (specs))
14108 new_specs = specs;
14109 else
14110 while (specs)
14111 {
14112 tree spec;
14113 int i, len = 1;
14114 tree expanded_specs = NULL_TREE;
14115
14116 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14117 {
14118 /* Expand the pack expansion type. */
14119 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14120 args, complain,
14121 in_decl);
14122
14123 if (expanded_specs == error_mark_node)
14124 return error_mark_node;
14125 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14126 len = TREE_VEC_LENGTH (expanded_specs);
14127 else
14128 {
14129 /* We're substituting into a member template, so
14130 we got a TYPE_PACK_EXPANSION back. Add that
14131 expansion and move on. */
14132 gcc_assert (TREE_CODE (expanded_specs)
14133 == TYPE_PACK_EXPANSION);
14134 new_specs = add_exception_specifier (new_specs,
14135 expanded_specs,
14136 complain);
14137 specs = TREE_CHAIN (specs);
14138 continue;
14139 }
14140 }
14141
14142 for (i = 0; i < len; ++i)
14143 {
14144 if (expanded_specs)
14145 spec = TREE_VEC_ELT (expanded_specs, i);
14146 else
14147 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14148 if (spec == error_mark_node)
14149 return spec;
14150 new_specs = add_exception_specifier (new_specs, spec,
14151 complain);
14152 }
14153
14154 specs = TREE_CHAIN (specs);
14155 }
14156 }
14157 return new_specs;
14158 }
14159
14160 /* Take the tree structure T and replace template parameters used
14161 therein with the argument vector ARGS. IN_DECL is an associated
14162 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14163 Issue error and warning messages under control of COMPLAIN. Note
14164 that we must be relatively non-tolerant of extensions here, in
14165 order to preserve conformance; if we allow substitutions that
14166 should not be allowed, we may allow argument deductions that should
14167 not succeed, and therefore report ambiguous overload situations
14168 where there are none. In theory, we could allow the substitution,
14169 but indicate that it should have failed, and allow our caller to
14170 make sure that the right thing happens, but we don't try to do this
14171 yet.
14172
14173 This function is used for dealing with types, decls and the like;
14174 for expressions, use tsubst_expr or tsubst_copy. */
14175
14176 tree
14177 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14178 {
14179 enum tree_code code;
14180 tree type, r = NULL_TREE;
14181
14182 if (t == NULL_TREE || t == error_mark_node
14183 || t == integer_type_node
14184 || t == void_type_node
14185 || t == char_type_node
14186 || t == unknown_type_node
14187 || TREE_CODE (t) == NAMESPACE_DECL
14188 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14189 return t;
14190
14191 if (DECL_P (t))
14192 return tsubst_decl (t, args, complain);
14193
14194 if (args == NULL_TREE)
14195 return t;
14196
14197 code = TREE_CODE (t);
14198
14199 if (code == IDENTIFIER_NODE)
14200 type = IDENTIFIER_TYPE_VALUE (t);
14201 else
14202 type = TREE_TYPE (t);
14203
14204 gcc_assert (type != unknown_type_node);
14205
14206 /* Reuse typedefs. We need to do this to handle dependent attributes,
14207 such as attribute aligned. */
14208 if (TYPE_P (t)
14209 && typedef_variant_p (t))
14210 {
14211 tree decl = TYPE_NAME (t);
14212
14213 if (alias_template_specialization_p (t))
14214 {
14215 /* DECL represents an alias template and we want to
14216 instantiate it. */
14217 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14218 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14219 r = instantiate_alias_template (tmpl, gen_args, complain);
14220 }
14221 else if (DECL_CLASS_SCOPE_P (decl)
14222 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14223 && uses_template_parms (DECL_CONTEXT (decl)))
14224 {
14225 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14226 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14227 r = retrieve_specialization (tmpl, gen_args, 0);
14228 }
14229 else if (DECL_FUNCTION_SCOPE_P (decl)
14230 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14231 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14232 r = retrieve_local_specialization (decl);
14233 else
14234 /* The typedef is from a non-template context. */
14235 return t;
14236
14237 if (r)
14238 {
14239 r = TREE_TYPE (r);
14240 r = cp_build_qualified_type_real
14241 (r, cp_type_quals (t) | cp_type_quals (r),
14242 complain | tf_ignore_bad_quals);
14243 return r;
14244 }
14245 else
14246 {
14247 /* We don't have an instantiation yet, so drop the typedef. */
14248 int quals = cp_type_quals (t);
14249 t = DECL_ORIGINAL_TYPE (decl);
14250 t = cp_build_qualified_type_real (t, quals,
14251 complain | tf_ignore_bad_quals);
14252 }
14253 }
14254
14255 bool fndecl_type = (complain & tf_fndecl_type);
14256 complain &= ~tf_fndecl_type;
14257
14258 if (type
14259 && code != TYPENAME_TYPE
14260 && code != TEMPLATE_TYPE_PARM
14261 && code != TEMPLATE_PARM_INDEX
14262 && code != IDENTIFIER_NODE
14263 && code != FUNCTION_TYPE
14264 && code != METHOD_TYPE)
14265 type = tsubst (type, args, complain, in_decl);
14266 if (type == error_mark_node)
14267 return error_mark_node;
14268
14269 switch (code)
14270 {
14271 case RECORD_TYPE:
14272 case UNION_TYPE:
14273 case ENUMERAL_TYPE:
14274 return tsubst_aggr_type (t, args, complain, in_decl,
14275 /*entering_scope=*/0);
14276
14277 case ERROR_MARK:
14278 case IDENTIFIER_NODE:
14279 case VOID_TYPE:
14280 case REAL_TYPE:
14281 case COMPLEX_TYPE:
14282 case VECTOR_TYPE:
14283 case BOOLEAN_TYPE:
14284 case NULLPTR_TYPE:
14285 case LANG_TYPE:
14286 return t;
14287
14288 case INTEGER_TYPE:
14289 if (t == integer_type_node)
14290 return t;
14291
14292 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14293 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14294 return t;
14295
14296 {
14297 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14298
14299 max = tsubst_expr (omax, args, complain, in_decl,
14300 /*integral_constant_expression_p=*/false);
14301
14302 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14303 needed. */
14304 if (TREE_CODE (max) == NOP_EXPR
14305 && TREE_SIDE_EFFECTS (omax)
14306 && !TREE_TYPE (max))
14307 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14308
14309 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14310 with TREE_SIDE_EFFECTS that indicates this is not an integral
14311 constant expression. */
14312 if (processing_template_decl
14313 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14314 {
14315 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14316 TREE_SIDE_EFFECTS (max) = 1;
14317 }
14318
14319 return compute_array_index_type (NULL_TREE, max, complain);
14320 }
14321
14322 case TEMPLATE_TYPE_PARM:
14323 case TEMPLATE_TEMPLATE_PARM:
14324 case BOUND_TEMPLATE_TEMPLATE_PARM:
14325 case TEMPLATE_PARM_INDEX:
14326 {
14327 int idx;
14328 int level;
14329 int levels;
14330 tree arg = NULL_TREE;
14331
14332 /* Early in template argument deduction substitution, we don't
14333 want to reduce the level of 'auto', or it will be confused
14334 with a normal template parm in subsequent deduction. */
14335 if (is_auto (t) && (complain & tf_partial))
14336 return t;
14337
14338 r = NULL_TREE;
14339
14340 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14341 template_parm_level_and_index (t, &level, &idx);
14342
14343 levels = TMPL_ARGS_DEPTH (args);
14344 if (level <= levels
14345 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14346 {
14347 arg = TMPL_ARG (args, level, idx);
14348
14349 /* See through ARGUMENT_PACK_SELECT arguments. */
14350 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14351 arg = argument_pack_select_arg (arg);
14352 }
14353
14354 if (arg == error_mark_node)
14355 return error_mark_node;
14356 else if (arg != NULL_TREE)
14357 {
14358 if (ARGUMENT_PACK_P (arg))
14359 /* If ARG is an argument pack, we don't actually want to
14360 perform a substitution here, because substitutions
14361 for argument packs are only done
14362 element-by-element. We can get to this point when
14363 substituting the type of a non-type template
14364 parameter pack, when that type actually contains
14365 template parameter packs from an outer template, e.g.,
14366
14367 template<typename... Types> struct A {
14368 template<Types... Values> struct B { };
14369 }; */
14370 return t;
14371
14372 if (code == TEMPLATE_TYPE_PARM)
14373 {
14374 int quals;
14375 gcc_assert (TYPE_P (arg));
14376
14377 quals = cp_type_quals (arg) | cp_type_quals (t);
14378
14379 return cp_build_qualified_type_real
14380 (arg, quals, complain | tf_ignore_bad_quals);
14381 }
14382 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14383 {
14384 /* We are processing a type constructed from a
14385 template template parameter. */
14386 tree argvec = tsubst (TYPE_TI_ARGS (t),
14387 args, complain, in_decl);
14388 if (argvec == error_mark_node)
14389 return error_mark_node;
14390
14391 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14392 || TREE_CODE (arg) == TEMPLATE_DECL
14393 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14394
14395 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14396 /* Consider this code:
14397
14398 template <template <class> class Template>
14399 struct Internal {
14400 template <class Arg> using Bind = Template<Arg>;
14401 };
14402
14403 template <template <class> class Template, class Arg>
14404 using Instantiate = Template<Arg>; //#0
14405
14406 template <template <class> class Template,
14407 class Argument>
14408 using Bind =
14409 Instantiate<Internal<Template>::template Bind,
14410 Argument>; //#1
14411
14412 When #1 is parsed, the
14413 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14414 parameter `Template' in #0 matches the
14415 UNBOUND_CLASS_TEMPLATE representing the argument
14416 `Internal<Template>::template Bind'; We then want
14417 to assemble the type `Bind<Argument>' that can't
14418 be fully created right now, because
14419 `Internal<Template>' not being complete, the Bind
14420 template cannot be looked up in that context. So
14421 we need to "store" `Bind<Argument>' for later
14422 when the context of Bind becomes complete. Let's
14423 store that in a TYPENAME_TYPE. */
14424 return make_typename_type (TYPE_CONTEXT (arg),
14425 build_nt (TEMPLATE_ID_EXPR,
14426 TYPE_IDENTIFIER (arg),
14427 argvec),
14428 typename_type,
14429 complain);
14430
14431 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14432 are resolving nested-types in the signature of a
14433 member function templates. Otherwise ARG is a
14434 TEMPLATE_DECL and is the real template to be
14435 instantiated. */
14436 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14437 arg = TYPE_NAME (arg);
14438
14439 r = lookup_template_class (arg,
14440 argvec, in_decl,
14441 DECL_CONTEXT (arg),
14442 /*entering_scope=*/0,
14443 complain);
14444 return cp_build_qualified_type_real
14445 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14446 }
14447 else if (code == TEMPLATE_TEMPLATE_PARM)
14448 return arg;
14449 else
14450 /* TEMPLATE_PARM_INDEX. */
14451 return convert_from_reference (unshare_expr (arg));
14452 }
14453
14454 if (level == 1)
14455 /* This can happen during the attempted tsubst'ing in
14456 unify. This means that we don't yet have any information
14457 about the template parameter in question. */
14458 return t;
14459
14460 /* If we get here, we must have been looking at a parm for a
14461 more deeply nested template. Make a new version of this
14462 template parameter, but with a lower level. */
14463 switch (code)
14464 {
14465 case TEMPLATE_TYPE_PARM:
14466 case TEMPLATE_TEMPLATE_PARM:
14467 case BOUND_TEMPLATE_TEMPLATE_PARM:
14468 if (cp_type_quals (t))
14469 {
14470 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14471 r = cp_build_qualified_type_real
14472 (r, cp_type_quals (t),
14473 complain | (code == TEMPLATE_TYPE_PARM
14474 ? tf_ignore_bad_quals : 0));
14475 }
14476 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14477 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14478 && (r = (TEMPLATE_PARM_DESCENDANTS
14479 (TEMPLATE_TYPE_PARM_INDEX (t))))
14480 && (r = TREE_TYPE (r))
14481 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14482 /* Break infinite recursion when substituting the constraints
14483 of a constrained placeholder. */;
14484 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14485 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14486 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14487 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14488 r = TEMPLATE_PARM_DESCENDANTS (arg))
14489 && (TEMPLATE_PARM_LEVEL (r)
14490 == TEMPLATE_PARM_LEVEL (arg) - levels))
14491 /* Cache the simple case of lowering a type parameter. */
14492 r = TREE_TYPE (r);
14493 else
14494 {
14495 r = copy_type (t);
14496 TEMPLATE_TYPE_PARM_INDEX (r)
14497 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14498 r, levels, args, complain);
14499 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14500 TYPE_MAIN_VARIANT (r) = r;
14501 TYPE_POINTER_TO (r) = NULL_TREE;
14502 TYPE_REFERENCE_TO (r) = NULL_TREE;
14503
14504 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14505 {
14506 /* Propagate constraints on placeholders. */
14507 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14508 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14509 = tsubst_constraint (constr, args, complain, in_decl);
14510 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14511 {
14512 pl = tsubst_copy (pl, args, complain, in_decl);
14513 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14514 }
14515 }
14516
14517 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14518 /* We have reduced the level of the template
14519 template parameter, but not the levels of its
14520 template parameters, so canonical_type_parameter
14521 will not be able to find the canonical template
14522 template parameter for this level. Thus, we
14523 require structural equality checking to compare
14524 TEMPLATE_TEMPLATE_PARMs. */
14525 SET_TYPE_STRUCTURAL_EQUALITY (r);
14526 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14527 SET_TYPE_STRUCTURAL_EQUALITY (r);
14528 else
14529 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14530
14531 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14532 {
14533 tree tinfo = TYPE_TEMPLATE_INFO (t);
14534 /* We might need to substitute into the types of non-type
14535 template parameters. */
14536 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14537 complain, in_decl);
14538 if (tmpl == error_mark_node)
14539 return error_mark_node;
14540 tree argvec = tsubst (TI_ARGS (tinfo), args,
14541 complain, in_decl);
14542 if (argvec == error_mark_node)
14543 return error_mark_node;
14544
14545 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14546 = build_template_info (tmpl, argvec);
14547 }
14548 }
14549 break;
14550
14551 case TEMPLATE_PARM_INDEX:
14552 /* OK, now substitute the type of the non-type parameter. We
14553 couldn't do it earlier because it might be an auto parameter,
14554 and we wouldn't need to if we had an argument. */
14555 type = tsubst (type, args, complain, in_decl);
14556 if (type == error_mark_node)
14557 return error_mark_node;
14558 r = reduce_template_parm_level (t, type, levels, args, complain);
14559 break;
14560
14561 default:
14562 gcc_unreachable ();
14563 }
14564
14565 return r;
14566 }
14567
14568 case TREE_LIST:
14569 {
14570 tree purpose, value, chain;
14571
14572 if (t == void_list_node)
14573 return t;
14574
14575 purpose = TREE_PURPOSE (t);
14576 if (purpose)
14577 {
14578 purpose = tsubst (purpose, args, complain, in_decl);
14579 if (purpose == error_mark_node)
14580 return error_mark_node;
14581 }
14582 value = TREE_VALUE (t);
14583 if (value)
14584 {
14585 value = tsubst (value, args, complain, in_decl);
14586 if (value == error_mark_node)
14587 return error_mark_node;
14588 }
14589 chain = TREE_CHAIN (t);
14590 if (chain && chain != void_type_node)
14591 {
14592 chain = tsubst (chain, args, complain, in_decl);
14593 if (chain == error_mark_node)
14594 return error_mark_node;
14595 }
14596 if (purpose == TREE_PURPOSE (t)
14597 && value == TREE_VALUE (t)
14598 && chain == TREE_CHAIN (t))
14599 return t;
14600 return hash_tree_cons (purpose, value, chain);
14601 }
14602
14603 case TREE_BINFO:
14604 /* We should never be tsubsting a binfo. */
14605 gcc_unreachable ();
14606
14607 case TREE_VEC:
14608 /* A vector of template arguments. */
14609 gcc_assert (!type);
14610 return tsubst_template_args (t, args, complain, in_decl);
14611
14612 case POINTER_TYPE:
14613 case REFERENCE_TYPE:
14614 {
14615 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14616 return t;
14617
14618 /* [temp.deduct]
14619
14620 Type deduction may fail for any of the following
14621 reasons:
14622
14623 -- Attempting to create a pointer to reference type.
14624 -- Attempting to create a reference to a reference type or
14625 a reference to void.
14626
14627 Core issue 106 says that creating a reference to a reference
14628 during instantiation is no longer a cause for failure. We
14629 only enforce this check in strict C++98 mode. */
14630 if ((TYPE_REF_P (type)
14631 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14632 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14633 {
14634 static location_t last_loc;
14635
14636 /* We keep track of the last time we issued this error
14637 message to avoid spewing a ton of messages during a
14638 single bad template instantiation. */
14639 if (complain & tf_error
14640 && last_loc != input_location)
14641 {
14642 if (VOID_TYPE_P (type))
14643 error ("forming reference to void");
14644 else if (code == POINTER_TYPE)
14645 error ("forming pointer to reference type %qT", type);
14646 else
14647 error ("forming reference to reference type %qT", type);
14648 last_loc = input_location;
14649 }
14650
14651 return error_mark_node;
14652 }
14653 else if (TREE_CODE (type) == FUNCTION_TYPE
14654 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14655 || type_memfn_rqual (type) != REF_QUAL_NONE))
14656 {
14657 if (complain & tf_error)
14658 {
14659 if (code == POINTER_TYPE)
14660 error ("forming pointer to qualified function type %qT",
14661 type);
14662 else
14663 error ("forming reference to qualified function type %qT",
14664 type);
14665 }
14666 return error_mark_node;
14667 }
14668 else if (code == POINTER_TYPE)
14669 {
14670 r = build_pointer_type (type);
14671 if (TREE_CODE (type) == METHOD_TYPE)
14672 r = build_ptrmemfunc_type (r);
14673 }
14674 else if (TYPE_REF_P (type))
14675 /* In C++0x, during template argument substitution, when there is an
14676 attempt to create a reference to a reference type, reference
14677 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14678
14679 "If a template-argument for a template-parameter T names a type
14680 that is a reference to a type A, an attempt to create the type
14681 'lvalue reference to cv T' creates the type 'lvalue reference to
14682 A,' while an attempt to create the type type rvalue reference to
14683 cv T' creates the type T"
14684 */
14685 r = cp_build_reference_type
14686 (TREE_TYPE (type),
14687 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14688 else
14689 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14690 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14691
14692 if (r != error_mark_node)
14693 /* Will this ever be needed for TYPE_..._TO values? */
14694 layout_type (r);
14695
14696 return r;
14697 }
14698 case OFFSET_TYPE:
14699 {
14700 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14701 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14702 {
14703 /* [temp.deduct]
14704
14705 Type deduction may fail for any of the following
14706 reasons:
14707
14708 -- Attempting to create "pointer to member of T" when T
14709 is not a class type. */
14710 if (complain & tf_error)
14711 error ("creating pointer to member of non-class type %qT", r);
14712 return error_mark_node;
14713 }
14714 if (TYPE_REF_P (type))
14715 {
14716 if (complain & tf_error)
14717 error ("creating pointer to member reference type %qT", type);
14718 return error_mark_node;
14719 }
14720 if (VOID_TYPE_P (type))
14721 {
14722 if (complain & tf_error)
14723 error ("creating pointer to member of type void");
14724 return error_mark_node;
14725 }
14726 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14727 if (TREE_CODE (type) == FUNCTION_TYPE)
14728 {
14729 /* The type of the implicit object parameter gets its
14730 cv-qualifiers from the FUNCTION_TYPE. */
14731 tree memptr;
14732 tree method_type
14733 = build_memfn_type (type, r, type_memfn_quals (type),
14734 type_memfn_rqual (type));
14735 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14736 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14737 complain);
14738 }
14739 else
14740 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14741 cp_type_quals (t),
14742 complain);
14743 }
14744 case FUNCTION_TYPE:
14745 case METHOD_TYPE:
14746 {
14747 tree fntype;
14748 tree specs;
14749 fntype = tsubst_function_type (t, args, complain, in_decl);
14750 if (fntype == error_mark_node)
14751 return error_mark_node;
14752
14753 /* Substitute the exception specification. */
14754 specs = tsubst_exception_specification (t, args, complain, in_decl,
14755 /*defer_ok*/fndecl_type);
14756 if (specs == error_mark_node)
14757 return error_mark_node;
14758 if (specs)
14759 fntype = build_exception_variant (fntype, specs);
14760 return fntype;
14761 }
14762 case ARRAY_TYPE:
14763 {
14764 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14765 if (domain == error_mark_node)
14766 return error_mark_node;
14767
14768 /* As an optimization, we avoid regenerating the array type if
14769 it will obviously be the same as T. */
14770 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14771 return t;
14772
14773 /* These checks should match the ones in create_array_type_for_decl.
14774
14775 [temp.deduct]
14776
14777 The deduction may fail for any of the following reasons:
14778
14779 -- Attempting to create an array with an element type that
14780 is void, a function type, or a reference type, or [DR337]
14781 an abstract class type. */
14782 if (VOID_TYPE_P (type)
14783 || TREE_CODE (type) == FUNCTION_TYPE
14784 || (TREE_CODE (type) == ARRAY_TYPE
14785 && TYPE_DOMAIN (type) == NULL_TREE)
14786 || TYPE_REF_P (type))
14787 {
14788 if (complain & tf_error)
14789 error ("creating array of %qT", type);
14790 return error_mark_node;
14791 }
14792
14793 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14794 return error_mark_node;
14795
14796 r = build_cplus_array_type (type, domain);
14797
14798 if (!valid_array_size_p (input_location, r, in_decl,
14799 (complain & tf_error)))
14800 return error_mark_node;
14801
14802 if (TYPE_USER_ALIGN (t))
14803 {
14804 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14805 TYPE_USER_ALIGN (r) = 1;
14806 }
14807
14808 return r;
14809 }
14810
14811 case TYPENAME_TYPE:
14812 {
14813 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14814 in_decl, /*entering_scope=*/1);
14815 if (ctx == error_mark_node)
14816 return error_mark_node;
14817
14818 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14819 complain, in_decl);
14820 if (f == error_mark_node)
14821 return error_mark_node;
14822
14823 if (!MAYBE_CLASS_TYPE_P (ctx))
14824 {
14825 if (complain & tf_error)
14826 error ("%qT is not a class, struct, or union type", ctx);
14827 return error_mark_node;
14828 }
14829 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14830 {
14831 /* Normally, make_typename_type does not require that the CTX
14832 have complete type in order to allow things like:
14833
14834 template <class T> struct S { typename S<T>::X Y; };
14835
14836 But, such constructs have already been resolved by this
14837 point, so here CTX really should have complete type, unless
14838 it's a partial instantiation. */
14839 ctx = complete_type (ctx);
14840 if (!COMPLETE_TYPE_P (ctx))
14841 {
14842 if (complain & tf_error)
14843 cxx_incomplete_type_error (NULL_TREE, ctx);
14844 return error_mark_node;
14845 }
14846 }
14847
14848 f = make_typename_type (ctx, f, typename_type,
14849 complain | tf_keep_type_decl);
14850 if (f == error_mark_node)
14851 return f;
14852 if (TREE_CODE (f) == TYPE_DECL)
14853 {
14854 complain |= tf_ignore_bad_quals;
14855 f = TREE_TYPE (f);
14856 }
14857
14858 if (TREE_CODE (f) != TYPENAME_TYPE)
14859 {
14860 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14861 {
14862 if (complain & tf_error)
14863 error ("%qT resolves to %qT, which is not an enumeration type",
14864 t, f);
14865 else
14866 return error_mark_node;
14867 }
14868 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14869 {
14870 if (complain & tf_error)
14871 error ("%qT resolves to %qT, which is is not a class type",
14872 t, f);
14873 else
14874 return error_mark_node;
14875 }
14876 }
14877
14878 return cp_build_qualified_type_real
14879 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14880 }
14881
14882 case UNBOUND_CLASS_TEMPLATE:
14883 {
14884 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14885 in_decl, /*entering_scope=*/1);
14886 tree name = TYPE_IDENTIFIER (t);
14887 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14888
14889 if (ctx == error_mark_node || name == error_mark_node)
14890 return error_mark_node;
14891
14892 if (parm_list)
14893 parm_list = tsubst_template_parms (parm_list, args, complain);
14894 return make_unbound_class_template (ctx, name, parm_list, complain);
14895 }
14896
14897 case TYPEOF_TYPE:
14898 {
14899 tree type;
14900
14901 ++cp_unevaluated_operand;
14902 ++c_inhibit_evaluation_warnings;
14903
14904 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14905 complain, in_decl,
14906 /*integral_constant_expression_p=*/false);
14907
14908 --cp_unevaluated_operand;
14909 --c_inhibit_evaluation_warnings;
14910
14911 type = finish_typeof (type);
14912 return cp_build_qualified_type_real (type,
14913 cp_type_quals (t)
14914 | cp_type_quals (type),
14915 complain);
14916 }
14917
14918 case DECLTYPE_TYPE:
14919 {
14920 tree type;
14921
14922 ++cp_unevaluated_operand;
14923 ++c_inhibit_evaluation_warnings;
14924
14925 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14926 complain|tf_decltype, in_decl,
14927 /*function_p*/false,
14928 /*integral_constant_expression*/false);
14929
14930 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14931 {
14932 if (type == NULL_TREE)
14933 {
14934 if (complain & tf_error)
14935 error ("empty initializer in lambda init-capture");
14936 type = error_mark_node;
14937 }
14938 else if (TREE_CODE (type) == TREE_LIST)
14939 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14940 }
14941
14942 --cp_unevaluated_operand;
14943 --c_inhibit_evaluation_warnings;
14944
14945 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14946 type = lambda_capture_field_type (type,
14947 DECLTYPE_FOR_INIT_CAPTURE (t),
14948 DECLTYPE_FOR_REF_CAPTURE (t));
14949 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14950 type = lambda_proxy_type (type);
14951 else
14952 {
14953 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14954 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14955 && EXPR_P (type))
14956 /* In a template ~id could be either a complement expression
14957 or an unqualified-id naming a destructor; if instantiating
14958 it produces an expression, it's not an id-expression or
14959 member access. */
14960 id = false;
14961 type = finish_decltype_type (type, id, complain);
14962 }
14963 return cp_build_qualified_type_real (type,
14964 cp_type_quals (t)
14965 | cp_type_quals (type),
14966 complain | tf_ignore_bad_quals);
14967 }
14968
14969 case UNDERLYING_TYPE:
14970 {
14971 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14972 complain, in_decl);
14973 return finish_underlying_type (type);
14974 }
14975
14976 case TYPE_ARGUMENT_PACK:
14977 case NONTYPE_ARGUMENT_PACK:
14978 {
14979 tree r;
14980
14981 if (code == NONTYPE_ARGUMENT_PACK)
14982 r = make_node (code);
14983 else
14984 r = cxx_make_type (code);
14985
14986 tree pack_args = ARGUMENT_PACK_ARGS (t);
14987 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14988 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14989
14990 return r;
14991 }
14992
14993 case VOID_CST:
14994 case INTEGER_CST:
14995 case REAL_CST:
14996 case STRING_CST:
14997 case PLUS_EXPR:
14998 case MINUS_EXPR:
14999 case NEGATE_EXPR:
15000 case NOP_EXPR:
15001 case INDIRECT_REF:
15002 case ADDR_EXPR:
15003 case CALL_EXPR:
15004 case ARRAY_REF:
15005 case SCOPE_REF:
15006 /* We should use one of the expression tsubsts for these codes. */
15007 gcc_unreachable ();
15008
15009 default:
15010 sorry ("use of %qs in template", get_tree_code_name (code));
15011 return error_mark_node;
15012 }
15013 }
15014
15015 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15016 expression on the left-hand side of the "." or "->" operator. We
15017 only do the lookup if we had a dependent BASELINK. Otherwise we
15018 adjust it onto the instantiated heirarchy. */
15019
15020 static tree
15021 tsubst_baselink (tree baselink, tree object_type,
15022 tree args, tsubst_flags_t complain, tree in_decl)
15023 {
15024 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15025 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15026 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15027
15028 tree optype = BASELINK_OPTYPE (baselink);
15029 optype = tsubst (optype, args, complain, in_decl);
15030
15031 tree template_args = NULL_TREE;
15032 bool template_id_p = false;
15033 tree fns = BASELINK_FUNCTIONS (baselink);
15034 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15035 {
15036 template_id_p = true;
15037 template_args = TREE_OPERAND (fns, 1);
15038 fns = TREE_OPERAND (fns, 0);
15039 if (template_args)
15040 template_args = tsubst_template_args (template_args, args,
15041 complain, in_decl);
15042 }
15043
15044 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15045 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15046 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15047
15048 if (dependent_p)
15049 {
15050 tree name = OVL_NAME (fns);
15051 if (IDENTIFIER_CONV_OP_P (name))
15052 name = make_conv_op_name (optype);
15053
15054 if (name == complete_dtor_identifier)
15055 /* Treat as-if non-dependent below. */
15056 dependent_p = false;
15057
15058 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15059 if (!baselink)
15060 {
15061 if ((complain & tf_error)
15062 && constructor_name_p (name, qualifying_scope))
15063 error ("cannot call constructor %<%T::%D%> directly",
15064 qualifying_scope, name);
15065 return error_mark_node;
15066 }
15067
15068 if (BASELINK_P (baselink))
15069 fns = BASELINK_FUNCTIONS (baselink);
15070 }
15071 else
15072 /* We're going to overwrite pieces below, make a duplicate. */
15073 baselink = copy_node (baselink);
15074
15075 /* If lookup found a single function, mark it as used at this point.
15076 (If lookup found multiple functions the one selected later by
15077 overload resolution will be marked as used at that point.) */
15078 if (!template_id_p && !really_overloaded_fn (fns))
15079 {
15080 tree fn = OVL_FIRST (fns);
15081 bool ok = mark_used (fn, complain);
15082 if (!ok && !(complain & tf_error))
15083 return error_mark_node;
15084 if (ok && BASELINK_P (baselink))
15085 /* We might have instantiated an auto function. */
15086 TREE_TYPE (baselink) = TREE_TYPE (fn);
15087 }
15088
15089 if (BASELINK_P (baselink))
15090 {
15091 /* Add back the template arguments, if present. */
15092 if (template_id_p)
15093 BASELINK_FUNCTIONS (baselink)
15094 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15095
15096 /* Update the conversion operator type. */
15097 BASELINK_OPTYPE (baselink) = optype;
15098 }
15099
15100 if (!object_type)
15101 object_type = current_class_type;
15102
15103 if (qualified_p || !dependent_p)
15104 {
15105 baselink = adjust_result_of_qualified_name_lookup (baselink,
15106 qualifying_scope,
15107 object_type);
15108 if (!qualified_p)
15109 /* We need to call adjust_result_of_qualified_name_lookup in case the
15110 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15111 so that we still get virtual function binding. */
15112 BASELINK_QUALIFIED_P (baselink) = false;
15113 }
15114
15115 return baselink;
15116 }
15117
15118 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15119 true if the qualified-id will be a postfix-expression in-and-of
15120 itself; false if more of the postfix-expression follows the
15121 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15122 of "&". */
15123
15124 static tree
15125 tsubst_qualified_id (tree qualified_id, tree args,
15126 tsubst_flags_t complain, tree in_decl,
15127 bool done, bool address_p)
15128 {
15129 tree expr;
15130 tree scope;
15131 tree name;
15132 bool is_template;
15133 tree template_args;
15134 location_t loc = UNKNOWN_LOCATION;
15135
15136 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15137
15138 /* Figure out what name to look up. */
15139 name = TREE_OPERAND (qualified_id, 1);
15140 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15141 {
15142 is_template = true;
15143 loc = EXPR_LOCATION (name);
15144 template_args = TREE_OPERAND (name, 1);
15145 if (template_args)
15146 template_args = tsubst_template_args (template_args, args,
15147 complain, in_decl);
15148 if (template_args == error_mark_node)
15149 return error_mark_node;
15150 name = TREE_OPERAND (name, 0);
15151 }
15152 else
15153 {
15154 is_template = false;
15155 template_args = NULL_TREE;
15156 }
15157
15158 /* Substitute into the qualifying scope. When there are no ARGS, we
15159 are just trying to simplify a non-dependent expression. In that
15160 case the qualifying scope may be dependent, and, in any case,
15161 substituting will not help. */
15162 scope = TREE_OPERAND (qualified_id, 0);
15163 if (args)
15164 {
15165 scope = tsubst (scope, args, complain, in_decl);
15166 expr = tsubst_copy (name, args, complain, in_decl);
15167 }
15168 else
15169 expr = name;
15170
15171 if (dependent_scope_p (scope))
15172 {
15173 if (is_template)
15174 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15175 tree r = build_qualified_name (NULL_TREE, scope, expr,
15176 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15177 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15178 return r;
15179 }
15180
15181 if (!BASELINK_P (name) && !DECL_P (expr))
15182 {
15183 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15184 {
15185 /* A BIT_NOT_EXPR is used to represent a destructor. */
15186 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15187 {
15188 error ("qualifying type %qT does not match destructor name ~%qT",
15189 scope, TREE_OPERAND (expr, 0));
15190 expr = error_mark_node;
15191 }
15192 else
15193 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15194 /*is_type_p=*/0, false);
15195 }
15196 else
15197 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15198 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15199 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15200 {
15201 if (complain & tf_error)
15202 {
15203 error ("dependent-name %qE is parsed as a non-type, but "
15204 "instantiation yields a type", qualified_id);
15205 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15206 }
15207 return error_mark_node;
15208 }
15209 }
15210
15211 if (DECL_P (expr))
15212 {
15213 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15214 scope);
15215 /* Remember that there was a reference to this entity. */
15216 if (!mark_used (expr, complain) && !(complain & tf_error))
15217 return error_mark_node;
15218 }
15219
15220 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15221 {
15222 if (complain & tf_error)
15223 qualified_name_lookup_error (scope,
15224 TREE_OPERAND (qualified_id, 1),
15225 expr, input_location);
15226 return error_mark_node;
15227 }
15228
15229 if (is_template)
15230 {
15231 /* We may be repeating a check already done during parsing, but
15232 if it was well-formed and passed then, it will pass again
15233 now, and if it didn't, we wouldn't have got here. The case
15234 we want to catch is when we couldn't tell then, and can now,
15235 namely when templ prior to substitution was an
15236 identifier. */
15237 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15238 return error_mark_node;
15239
15240 if (variable_template_p (expr))
15241 expr = lookup_and_finish_template_variable (expr, template_args,
15242 complain);
15243 else
15244 expr = lookup_template_function (expr, template_args);
15245 }
15246
15247 if (expr == error_mark_node && complain & tf_error)
15248 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15249 expr, input_location);
15250 else if (TYPE_P (scope))
15251 {
15252 expr = (adjust_result_of_qualified_name_lookup
15253 (expr, scope, current_nonlambda_class_type ()));
15254 expr = (finish_qualified_id_expr
15255 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15256 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15257 /*template_arg_p=*/false, complain));
15258 }
15259
15260 /* Expressions do not generally have reference type. */
15261 if (TREE_CODE (expr) != SCOPE_REF
15262 /* However, if we're about to form a pointer-to-member, we just
15263 want the referenced member referenced. */
15264 && TREE_CODE (expr) != OFFSET_REF)
15265 expr = convert_from_reference (expr);
15266
15267 if (REF_PARENTHESIZED_P (qualified_id))
15268 expr = force_paren_expr (expr);
15269
15270 return expr;
15271 }
15272
15273 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15274 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15275 for tsubst. */
15276
15277 static tree
15278 tsubst_init (tree init, tree decl, tree args,
15279 tsubst_flags_t complain, tree in_decl)
15280 {
15281 if (!init)
15282 return NULL_TREE;
15283
15284 init = tsubst_expr (init, args, complain, in_decl, false);
15285
15286 if (!init && TREE_TYPE (decl) != error_mark_node)
15287 {
15288 /* If we had an initializer but it
15289 instantiated to nothing,
15290 value-initialize the object. This will
15291 only occur when the initializer was a
15292 pack expansion where the parameter packs
15293 used in that expansion were of length
15294 zero. */
15295 init = build_value_init (TREE_TYPE (decl),
15296 complain);
15297 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15298 init = get_target_expr_sfinae (init, complain);
15299 if (TREE_CODE (init) == TARGET_EXPR)
15300 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15301 }
15302
15303 return init;
15304 }
15305
15306 /* Like tsubst, but deals with expressions. This function just replaces
15307 template parms; to finish processing the resultant expression, use
15308 tsubst_copy_and_build or tsubst_expr. */
15309
15310 static tree
15311 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15312 {
15313 enum tree_code code;
15314 tree r;
15315
15316 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15317 return t;
15318
15319 code = TREE_CODE (t);
15320
15321 switch (code)
15322 {
15323 case PARM_DECL:
15324 r = retrieve_local_specialization (t);
15325
15326 if (r == NULL_TREE)
15327 {
15328 /* We get here for a use of 'this' in an NSDMI. */
15329 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15330 return current_class_ptr;
15331
15332 /* This can happen for a parameter name used later in a function
15333 declaration (such as in a late-specified return type). Just
15334 make a dummy decl, since it's only used for its type. */
15335 gcc_assert (cp_unevaluated_operand != 0);
15336 r = tsubst_decl (t, args, complain);
15337 /* Give it the template pattern as its context; its true context
15338 hasn't been instantiated yet and this is good enough for
15339 mangling. */
15340 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15341 }
15342
15343 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15344 r = argument_pack_select_arg (r);
15345 if (!mark_used (r, complain) && !(complain & tf_error))
15346 return error_mark_node;
15347 return r;
15348
15349 case CONST_DECL:
15350 {
15351 tree enum_type;
15352 tree v;
15353
15354 if (DECL_TEMPLATE_PARM_P (t))
15355 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15356 /* There is no need to substitute into namespace-scope
15357 enumerators. */
15358 if (DECL_NAMESPACE_SCOPE_P (t))
15359 return t;
15360 /* If ARGS is NULL, then T is known to be non-dependent. */
15361 if (args == NULL_TREE)
15362 return scalar_constant_value (t);
15363
15364 /* Unfortunately, we cannot just call lookup_name here.
15365 Consider:
15366
15367 template <int I> int f() {
15368 enum E { a = I };
15369 struct S { void g() { E e = a; } };
15370 };
15371
15372 When we instantiate f<7>::S::g(), say, lookup_name is not
15373 clever enough to find f<7>::a. */
15374 enum_type
15375 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15376 /*entering_scope=*/0);
15377
15378 for (v = TYPE_VALUES (enum_type);
15379 v != NULL_TREE;
15380 v = TREE_CHAIN (v))
15381 if (TREE_PURPOSE (v) == DECL_NAME (t))
15382 return TREE_VALUE (v);
15383
15384 /* We didn't find the name. That should never happen; if
15385 name-lookup found it during preliminary parsing, we
15386 should find it again here during instantiation. */
15387 gcc_unreachable ();
15388 }
15389 return t;
15390
15391 case FIELD_DECL:
15392 if (DECL_CONTEXT (t))
15393 {
15394 tree ctx;
15395
15396 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15397 /*entering_scope=*/1);
15398 if (ctx != DECL_CONTEXT (t))
15399 {
15400 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15401 if (!r)
15402 {
15403 if (complain & tf_error)
15404 error ("using invalid field %qD", t);
15405 return error_mark_node;
15406 }
15407 return r;
15408 }
15409 }
15410
15411 return t;
15412
15413 case VAR_DECL:
15414 case FUNCTION_DECL:
15415 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15416 r = tsubst (t, args, complain, in_decl);
15417 else if (local_variable_p (t)
15418 && uses_template_parms (DECL_CONTEXT (t)))
15419 {
15420 r = retrieve_local_specialization (t);
15421 if (r == NULL_TREE)
15422 {
15423 /* First try name lookup to find the instantiation. */
15424 r = lookup_name (DECL_NAME (t));
15425 if (r && !is_capture_proxy (r))
15426 {
15427 /* Make sure that the one we found is the one we want. */
15428 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15429 if (ctx != DECL_CONTEXT (r))
15430 r = NULL_TREE;
15431 }
15432
15433 if (r)
15434 /* OK */;
15435 else
15436 {
15437 /* This can happen for a variable used in a
15438 late-specified return type of a local lambda, or for a
15439 local static or constant. Building a new VAR_DECL
15440 should be OK in all those cases. */
15441 r = tsubst_decl (t, args, complain);
15442 if (local_specializations)
15443 /* Avoid infinite recursion (79640). */
15444 register_local_specialization (r, t);
15445 if (decl_maybe_constant_var_p (r))
15446 {
15447 /* We can't call cp_finish_decl, so handle the
15448 initializer by hand. */
15449 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15450 complain, in_decl);
15451 if (!processing_template_decl)
15452 init = maybe_constant_init (init);
15453 if (processing_template_decl
15454 ? potential_constant_expression (init)
15455 : reduced_constant_expression_p (init))
15456 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15457 = TREE_CONSTANT (r) = true;
15458 DECL_INITIAL (r) = init;
15459 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15460 TREE_TYPE (r)
15461 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15462 complain, adc_variable_type);
15463 }
15464 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15465 || decl_constant_var_p (r)
15466 || errorcount || sorrycount);
15467 if (!processing_template_decl
15468 && !TREE_STATIC (r))
15469 r = process_outer_var_ref (r, complain);
15470 }
15471 /* Remember this for subsequent uses. */
15472 if (local_specializations)
15473 register_local_specialization (r, t);
15474 }
15475 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15476 r = argument_pack_select_arg (r);
15477 }
15478 else
15479 r = t;
15480 if (!mark_used (r, complain))
15481 return error_mark_node;
15482 return r;
15483
15484 case NAMESPACE_DECL:
15485 return t;
15486
15487 case OVERLOAD:
15488 /* An OVERLOAD will always be a non-dependent overload set; an
15489 overload set from function scope will just be represented with an
15490 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15491 gcc_assert (!uses_template_parms (t));
15492 /* We must have marked any lookups as persistent. */
15493 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15494 return t;
15495
15496 case BASELINK:
15497 return tsubst_baselink (t, current_nonlambda_class_type (),
15498 args, complain, in_decl);
15499
15500 case TEMPLATE_DECL:
15501 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15502 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15503 args, complain, in_decl);
15504 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15505 return tsubst (t, args, complain, in_decl);
15506 else if (DECL_CLASS_SCOPE_P (t)
15507 && uses_template_parms (DECL_CONTEXT (t)))
15508 {
15509 /* Template template argument like the following example need
15510 special treatment:
15511
15512 template <template <class> class TT> struct C {};
15513 template <class T> struct D {
15514 template <class U> struct E {};
15515 C<E> c; // #1
15516 };
15517 D<int> d; // #2
15518
15519 We are processing the template argument `E' in #1 for
15520 the template instantiation #2. Originally, `E' is a
15521 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15522 have to substitute this with one having context `D<int>'. */
15523
15524 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15525 if (dependent_scope_p (context))
15526 {
15527 /* When rewriting a constructor into a deduction guide, a
15528 non-dependent name can become dependent, so memtmpl<args>
15529 becomes context::template memtmpl<args>. */
15530 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15531 return build_qualified_name (type, context, DECL_NAME (t),
15532 /*template*/true);
15533 }
15534 return lookup_field (context, DECL_NAME(t), 0, false);
15535 }
15536 else
15537 /* Ordinary template template argument. */
15538 return t;
15539
15540 case NON_LVALUE_EXPR:
15541 case VIEW_CONVERT_EXPR:
15542 {
15543 /* Handle location wrappers by substituting the wrapped node
15544 first, *then* reusing the resulting type. Doing the type
15545 first ensures that we handle template parameters and
15546 parameter pack expansions. */
15547 gcc_assert (location_wrapper_p (t));
15548 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15549 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15550 }
15551
15552 case CAST_EXPR:
15553 case REINTERPRET_CAST_EXPR:
15554 case CONST_CAST_EXPR:
15555 case STATIC_CAST_EXPR:
15556 case DYNAMIC_CAST_EXPR:
15557 case IMPLICIT_CONV_EXPR:
15558 case CONVERT_EXPR:
15559 case NOP_EXPR:
15560 {
15561 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15562 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15563 return build1 (code, type, op0);
15564 }
15565
15566 case SIZEOF_EXPR:
15567 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15568 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15569 {
15570 tree expanded, op = TREE_OPERAND (t, 0);
15571 int len = 0;
15572
15573 if (SIZEOF_EXPR_TYPE_P (t))
15574 op = TREE_TYPE (op);
15575
15576 ++cp_unevaluated_operand;
15577 ++c_inhibit_evaluation_warnings;
15578 /* We only want to compute the number of arguments. */
15579 if (PACK_EXPANSION_P (op))
15580 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15581 else
15582 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15583 args, complain, in_decl);
15584 --cp_unevaluated_operand;
15585 --c_inhibit_evaluation_warnings;
15586
15587 if (TREE_CODE (expanded) == TREE_VEC)
15588 {
15589 len = TREE_VEC_LENGTH (expanded);
15590 /* Set TREE_USED for the benefit of -Wunused. */
15591 for (int i = 0; i < len; i++)
15592 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15593 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15594 }
15595
15596 if (expanded == error_mark_node)
15597 return error_mark_node;
15598 else if (PACK_EXPANSION_P (expanded)
15599 || (TREE_CODE (expanded) == TREE_VEC
15600 && pack_expansion_args_count (expanded)))
15601
15602 {
15603 if (PACK_EXPANSION_P (expanded))
15604 /* OK. */;
15605 else if (TREE_VEC_LENGTH (expanded) == 1)
15606 expanded = TREE_VEC_ELT (expanded, 0);
15607 else
15608 expanded = make_argument_pack (expanded);
15609
15610 if (TYPE_P (expanded))
15611 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15612 false,
15613 complain & tf_error);
15614 else
15615 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15616 complain & tf_error);
15617 }
15618 else
15619 return build_int_cst (size_type_node, len);
15620 }
15621 if (SIZEOF_EXPR_TYPE_P (t))
15622 {
15623 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15624 args, complain, in_decl);
15625 r = build1 (NOP_EXPR, r, error_mark_node);
15626 r = build1 (SIZEOF_EXPR,
15627 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15628 SIZEOF_EXPR_TYPE_P (r) = 1;
15629 return r;
15630 }
15631 /* Fall through */
15632
15633 case INDIRECT_REF:
15634 case NEGATE_EXPR:
15635 case TRUTH_NOT_EXPR:
15636 case BIT_NOT_EXPR:
15637 case ADDR_EXPR:
15638 case UNARY_PLUS_EXPR: /* Unary + */
15639 case ALIGNOF_EXPR:
15640 case AT_ENCODE_EXPR:
15641 case ARROW_EXPR:
15642 case THROW_EXPR:
15643 case TYPEID_EXPR:
15644 case REALPART_EXPR:
15645 case IMAGPART_EXPR:
15646 case PAREN_EXPR:
15647 {
15648 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15649 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15650 r = build1 (code, type, op0);
15651 if (code == ALIGNOF_EXPR)
15652 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15653 return r;
15654 }
15655
15656 case COMPONENT_REF:
15657 {
15658 tree object;
15659 tree name;
15660
15661 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15662 name = TREE_OPERAND (t, 1);
15663 if (TREE_CODE (name) == BIT_NOT_EXPR)
15664 {
15665 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15666 complain, in_decl);
15667 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15668 }
15669 else if (TREE_CODE (name) == SCOPE_REF
15670 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15671 {
15672 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15673 complain, in_decl);
15674 name = TREE_OPERAND (name, 1);
15675 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15676 complain, in_decl);
15677 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15678 name = build_qualified_name (/*type=*/NULL_TREE,
15679 base, name,
15680 /*template_p=*/false);
15681 }
15682 else if (BASELINK_P (name))
15683 name = tsubst_baselink (name,
15684 non_reference (TREE_TYPE (object)),
15685 args, complain,
15686 in_decl);
15687 else
15688 name = tsubst_copy (name, args, complain, in_decl);
15689 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15690 }
15691
15692 case PLUS_EXPR:
15693 case MINUS_EXPR:
15694 case MULT_EXPR:
15695 case TRUNC_DIV_EXPR:
15696 case CEIL_DIV_EXPR:
15697 case FLOOR_DIV_EXPR:
15698 case ROUND_DIV_EXPR:
15699 case EXACT_DIV_EXPR:
15700 case BIT_AND_EXPR:
15701 case BIT_IOR_EXPR:
15702 case BIT_XOR_EXPR:
15703 case TRUNC_MOD_EXPR:
15704 case FLOOR_MOD_EXPR:
15705 case TRUTH_ANDIF_EXPR:
15706 case TRUTH_ORIF_EXPR:
15707 case TRUTH_AND_EXPR:
15708 case TRUTH_OR_EXPR:
15709 case RSHIFT_EXPR:
15710 case LSHIFT_EXPR:
15711 case RROTATE_EXPR:
15712 case LROTATE_EXPR:
15713 case EQ_EXPR:
15714 case NE_EXPR:
15715 case MAX_EXPR:
15716 case MIN_EXPR:
15717 case LE_EXPR:
15718 case GE_EXPR:
15719 case LT_EXPR:
15720 case GT_EXPR:
15721 case COMPOUND_EXPR:
15722 case DOTSTAR_EXPR:
15723 case MEMBER_REF:
15724 case PREDECREMENT_EXPR:
15725 case PREINCREMENT_EXPR:
15726 case POSTDECREMENT_EXPR:
15727 case POSTINCREMENT_EXPR:
15728 {
15729 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15730 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15731 return build_nt (code, op0, op1);
15732 }
15733
15734 case SCOPE_REF:
15735 {
15736 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15737 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15738 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15739 QUALIFIED_NAME_IS_TEMPLATE (t));
15740 }
15741
15742 case ARRAY_REF:
15743 {
15744 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15745 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15746 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15747 }
15748
15749 case CALL_EXPR:
15750 {
15751 int n = VL_EXP_OPERAND_LENGTH (t);
15752 tree result = build_vl_exp (CALL_EXPR, n);
15753 int i;
15754 for (i = 0; i < n; i++)
15755 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15756 complain, in_decl);
15757 return result;
15758 }
15759
15760 case COND_EXPR:
15761 case MODOP_EXPR:
15762 case PSEUDO_DTOR_EXPR:
15763 case VEC_PERM_EXPR:
15764 {
15765 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15766 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15767 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15768 r = build_nt (code, op0, op1, op2);
15769 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15770 return r;
15771 }
15772
15773 case NEW_EXPR:
15774 {
15775 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15776 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15777 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15778 r = build_nt (code, op0, op1, op2);
15779 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15780 return r;
15781 }
15782
15783 case DELETE_EXPR:
15784 {
15785 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15786 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15787 r = build_nt (code, op0, op1);
15788 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15789 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15790 return r;
15791 }
15792
15793 case TEMPLATE_ID_EXPR:
15794 {
15795 /* Substituted template arguments */
15796 tree fn = TREE_OPERAND (t, 0);
15797 tree targs = TREE_OPERAND (t, 1);
15798
15799 fn = tsubst_copy (fn, args, complain, in_decl);
15800 if (targs)
15801 targs = tsubst_template_args (targs, args, complain, in_decl);
15802
15803 return lookup_template_function (fn, targs);
15804 }
15805
15806 case TREE_LIST:
15807 {
15808 tree purpose, value, chain;
15809
15810 if (t == void_list_node)
15811 return t;
15812
15813 purpose = TREE_PURPOSE (t);
15814 if (purpose)
15815 purpose = tsubst_copy (purpose, args, complain, in_decl);
15816 value = TREE_VALUE (t);
15817 if (value)
15818 value = tsubst_copy (value, args, complain, in_decl);
15819 chain = TREE_CHAIN (t);
15820 if (chain && chain != void_type_node)
15821 chain = tsubst_copy (chain, args, complain, in_decl);
15822 if (purpose == TREE_PURPOSE (t)
15823 && value == TREE_VALUE (t)
15824 && chain == TREE_CHAIN (t))
15825 return t;
15826 return tree_cons (purpose, value, chain);
15827 }
15828
15829 case RECORD_TYPE:
15830 case UNION_TYPE:
15831 case ENUMERAL_TYPE:
15832 case INTEGER_TYPE:
15833 case TEMPLATE_TYPE_PARM:
15834 case TEMPLATE_TEMPLATE_PARM:
15835 case BOUND_TEMPLATE_TEMPLATE_PARM:
15836 case TEMPLATE_PARM_INDEX:
15837 case POINTER_TYPE:
15838 case REFERENCE_TYPE:
15839 case OFFSET_TYPE:
15840 case FUNCTION_TYPE:
15841 case METHOD_TYPE:
15842 case ARRAY_TYPE:
15843 case TYPENAME_TYPE:
15844 case UNBOUND_CLASS_TEMPLATE:
15845 case TYPEOF_TYPE:
15846 case DECLTYPE_TYPE:
15847 case TYPE_DECL:
15848 return tsubst (t, args, complain, in_decl);
15849
15850 case USING_DECL:
15851 t = DECL_NAME (t);
15852 /* Fall through. */
15853 case IDENTIFIER_NODE:
15854 if (IDENTIFIER_CONV_OP_P (t))
15855 {
15856 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15857 return make_conv_op_name (new_type);
15858 }
15859 else
15860 return t;
15861
15862 case CONSTRUCTOR:
15863 /* This is handled by tsubst_copy_and_build. */
15864 gcc_unreachable ();
15865
15866 case VA_ARG_EXPR:
15867 {
15868 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15869 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15870 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15871 }
15872
15873 case CLEANUP_POINT_EXPR:
15874 /* We shouldn't have built any of these during initial template
15875 generation. Instead, they should be built during instantiation
15876 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15877 gcc_unreachable ();
15878
15879 case OFFSET_REF:
15880 {
15881 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15882 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15883 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15884 r = build2 (code, type, op0, op1);
15885 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15886 if (!mark_used (TREE_OPERAND (r, 1), complain)
15887 && !(complain & tf_error))
15888 return error_mark_node;
15889 return r;
15890 }
15891
15892 case EXPR_PACK_EXPANSION:
15893 error ("invalid use of pack expansion expression");
15894 return error_mark_node;
15895
15896 case NONTYPE_ARGUMENT_PACK:
15897 error ("use %<...%> to expand argument pack");
15898 return error_mark_node;
15899
15900 case VOID_CST:
15901 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15902 return t;
15903
15904 case INTEGER_CST:
15905 case REAL_CST:
15906 case STRING_CST:
15907 case COMPLEX_CST:
15908 {
15909 /* Instantiate any typedefs in the type. */
15910 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15911 r = fold_convert (type, t);
15912 gcc_assert (TREE_CODE (r) == code);
15913 return r;
15914 }
15915
15916 case PTRMEM_CST:
15917 /* These can sometimes show up in a partial instantiation, but never
15918 involve template parms. */
15919 gcc_assert (!uses_template_parms (t));
15920 return t;
15921
15922 case UNARY_LEFT_FOLD_EXPR:
15923 return tsubst_unary_left_fold (t, args, complain, in_decl);
15924 case UNARY_RIGHT_FOLD_EXPR:
15925 return tsubst_unary_right_fold (t, args, complain, in_decl);
15926 case BINARY_LEFT_FOLD_EXPR:
15927 return tsubst_binary_left_fold (t, args, complain, in_decl);
15928 case BINARY_RIGHT_FOLD_EXPR:
15929 return tsubst_binary_right_fold (t, args, complain, in_decl);
15930 case PREDICT_EXPR:
15931 return t;
15932
15933 case DEBUG_BEGIN_STMT:
15934 /* ??? There's no point in copying it for now, but maybe some
15935 day it will contain more information, such as a pointer back
15936 to the containing function, inlined copy or so. */
15937 return t;
15938
15939 default:
15940 /* We shouldn't get here, but keep going if !flag_checking. */
15941 if (flag_checking)
15942 gcc_unreachable ();
15943 return t;
15944 }
15945 }
15946
15947 /* Helper function for tsubst_omp_clauses, used for instantiation of
15948 OMP_CLAUSE_DECL of clauses. */
15949
15950 static tree
15951 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15952 tree in_decl)
15953 {
15954 if (decl == NULL_TREE)
15955 return NULL_TREE;
15956
15957 /* Handle an OpenMP array section represented as a TREE_LIST (or
15958 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15959 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15960 TREE_LIST. We can handle it exactly the same as an array section
15961 (purpose, value, and a chain), even though the nomenclature
15962 (low_bound, length, etc) is different. */
15963 if (TREE_CODE (decl) == TREE_LIST)
15964 {
15965 tree low_bound
15966 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15967 /*integral_constant_expression_p=*/false);
15968 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15969 /*integral_constant_expression_p=*/false);
15970 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15971 in_decl);
15972 if (TREE_PURPOSE (decl) == low_bound
15973 && TREE_VALUE (decl) == length
15974 && TREE_CHAIN (decl) == chain)
15975 return decl;
15976 tree ret = tree_cons (low_bound, length, chain);
15977 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15978 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15979 return ret;
15980 }
15981 tree ret = tsubst_expr (decl, args, complain, in_decl,
15982 /*integral_constant_expression_p=*/false);
15983 /* Undo convert_from_reference tsubst_expr could have called. */
15984 if (decl
15985 && REFERENCE_REF_P (ret)
15986 && !REFERENCE_REF_P (decl))
15987 ret = TREE_OPERAND (ret, 0);
15988 return ret;
15989 }
15990
15991 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15992
15993 static tree
15994 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15995 tree args, tsubst_flags_t complain, tree in_decl)
15996 {
15997 tree new_clauses = NULL_TREE, nc, oc;
15998 tree linear_no_step = NULL_TREE;
15999
16000 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16001 {
16002 nc = copy_node (oc);
16003 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16004 new_clauses = nc;
16005
16006 switch (OMP_CLAUSE_CODE (nc))
16007 {
16008 case OMP_CLAUSE_LASTPRIVATE:
16009 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16010 {
16011 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16012 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16013 in_decl, /*integral_constant_expression_p=*/false);
16014 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16015 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16016 }
16017 /* FALLTHRU */
16018 case OMP_CLAUSE_PRIVATE:
16019 case OMP_CLAUSE_SHARED:
16020 case OMP_CLAUSE_FIRSTPRIVATE:
16021 case OMP_CLAUSE_COPYIN:
16022 case OMP_CLAUSE_COPYPRIVATE:
16023 case OMP_CLAUSE_UNIFORM:
16024 case OMP_CLAUSE_DEPEND:
16025 case OMP_CLAUSE_FROM:
16026 case OMP_CLAUSE_TO:
16027 case OMP_CLAUSE_MAP:
16028 case OMP_CLAUSE_USE_DEVICE_PTR:
16029 case OMP_CLAUSE_IS_DEVICE_PTR:
16030 OMP_CLAUSE_DECL (nc)
16031 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16032 in_decl);
16033 break;
16034 case OMP_CLAUSE_TILE:
16035 case OMP_CLAUSE_IF:
16036 case OMP_CLAUSE_NUM_THREADS:
16037 case OMP_CLAUSE_SCHEDULE:
16038 case OMP_CLAUSE_COLLAPSE:
16039 case OMP_CLAUSE_FINAL:
16040 case OMP_CLAUSE_DEVICE:
16041 case OMP_CLAUSE_DIST_SCHEDULE:
16042 case OMP_CLAUSE_NUM_TEAMS:
16043 case OMP_CLAUSE_THREAD_LIMIT:
16044 case OMP_CLAUSE_SAFELEN:
16045 case OMP_CLAUSE_SIMDLEN:
16046 case OMP_CLAUSE_NUM_TASKS:
16047 case OMP_CLAUSE_GRAINSIZE:
16048 case OMP_CLAUSE_PRIORITY:
16049 case OMP_CLAUSE_ORDERED:
16050 case OMP_CLAUSE_HINT:
16051 case OMP_CLAUSE_NUM_GANGS:
16052 case OMP_CLAUSE_NUM_WORKERS:
16053 case OMP_CLAUSE_VECTOR_LENGTH:
16054 case OMP_CLAUSE_WORKER:
16055 case OMP_CLAUSE_VECTOR:
16056 case OMP_CLAUSE_ASYNC:
16057 case OMP_CLAUSE_WAIT:
16058 OMP_CLAUSE_OPERAND (nc, 0)
16059 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16060 in_decl, /*integral_constant_expression_p=*/false);
16061 break;
16062 case OMP_CLAUSE_REDUCTION:
16063 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16064 {
16065 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16066 if (TREE_CODE (placeholder) == SCOPE_REF)
16067 {
16068 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16069 complain, in_decl);
16070 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16071 = build_qualified_name (NULL_TREE, scope,
16072 TREE_OPERAND (placeholder, 1),
16073 false);
16074 }
16075 else
16076 gcc_assert (identifier_p (placeholder));
16077 }
16078 OMP_CLAUSE_DECL (nc)
16079 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16080 in_decl);
16081 break;
16082 case OMP_CLAUSE_GANG:
16083 case OMP_CLAUSE_ALIGNED:
16084 OMP_CLAUSE_DECL (nc)
16085 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16086 in_decl);
16087 OMP_CLAUSE_OPERAND (nc, 1)
16088 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16089 in_decl, /*integral_constant_expression_p=*/false);
16090 break;
16091 case OMP_CLAUSE_LINEAR:
16092 OMP_CLAUSE_DECL (nc)
16093 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16094 in_decl);
16095 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16096 {
16097 gcc_assert (!linear_no_step);
16098 linear_no_step = nc;
16099 }
16100 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16101 OMP_CLAUSE_LINEAR_STEP (nc)
16102 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16103 complain, in_decl);
16104 else
16105 OMP_CLAUSE_LINEAR_STEP (nc)
16106 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16107 in_decl,
16108 /*integral_constant_expression_p=*/false);
16109 break;
16110 case OMP_CLAUSE_NOWAIT:
16111 case OMP_CLAUSE_DEFAULT:
16112 case OMP_CLAUSE_UNTIED:
16113 case OMP_CLAUSE_MERGEABLE:
16114 case OMP_CLAUSE_INBRANCH:
16115 case OMP_CLAUSE_NOTINBRANCH:
16116 case OMP_CLAUSE_PROC_BIND:
16117 case OMP_CLAUSE_FOR:
16118 case OMP_CLAUSE_PARALLEL:
16119 case OMP_CLAUSE_SECTIONS:
16120 case OMP_CLAUSE_TASKGROUP:
16121 case OMP_CLAUSE_NOGROUP:
16122 case OMP_CLAUSE_THREADS:
16123 case OMP_CLAUSE_SIMD:
16124 case OMP_CLAUSE_DEFAULTMAP:
16125 case OMP_CLAUSE_INDEPENDENT:
16126 case OMP_CLAUSE_AUTO:
16127 case OMP_CLAUSE_SEQ:
16128 case OMP_CLAUSE_IF_PRESENT:
16129 case OMP_CLAUSE_FINALIZE:
16130 break;
16131 default:
16132 gcc_unreachable ();
16133 }
16134 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16135 switch (OMP_CLAUSE_CODE (nc))
16136 {
16137 case OMP_CLAUSE_SHARED:
16138 case OMP_CLAUSE_PRIVATE:
16139 case OMP_CLAUSE_FIRSTPRIVATE:
16140 case OMP_CLAUSE_LASTPRIVATE:
16141 case OMP_CLAUSE_COPYPRIVATE:
16142 case OMP_CLAUSE_LINEAR:
16143 case OMP_CLAUSE_REDUCTION:
16144 case OMP_CLAUSE_USE_DEVICE_PTR:
16145 case OMP_CLAUSE_IS_DEVICE_PTR:
16146 /* tsubst_expr on SCOPE_REF results in returning
16147 finish_non_static_data_member result. Undo that here. */
16148 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16149 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16150 == IDENTIFIER_NODE))
16151 {
16152 tree t = OMP_CLAUSE_DECL (nc);
16153 tree v = t;
16154 while (v)
16155 switch (TREE_CODE (v))
16156 {
16157 case COMPONENT_REF:
16158 case MEM_REF:
16159 case INDIRECT_REF:
16160 CASE_CONVERT:
16161 case POINTER_PLUS_EXPR:
16162 v = TREE_OPERAND (v, 0);
16163 continue;
16164 case PARM_DECL:
16165 if (DECL_CONTEXT (v) == current_function_decl
16166 && DECL_ARTIFICIAL (v)
16167 && DECL_NAME (v) == this_identifier)
16168 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16169 /* FALLTHRU */
16170 default:
16171 v = NULL_TREE;
16172 break;
16173 }
16174 }
16175 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16176 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16177 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16178 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16179 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16180 {
16181 tree decl = OMP_CLAUSE_DECL (nc);
16182 if (VAR_P (decl))
16183 {
16184 retrofit_lang_decl (decl);
16185 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16186 }
16187 }
16188 break;
16189 default:
16190 break;
16191 }
16192 }
16193
16194 new_clauses = nreverse (new_clauses);
16195 if (ort != C_ORT_OMP_DECLARE_SIMD)
16196 {
16197 new_clauses = finish_omp_clauses (new_clauses, ort);
16198 if (linear_no_step)
16199 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16200 if (nc == linear_no_step)
16201 {
16202 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16203 break;
16204 }
16205 }
16206 return new_clauses;
16207 }
16208
16209 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16210
16211 static tree
16212 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16213 tree in_decl)
16214 {
16215 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16216
16217 tree purpose, value, chain;
16218
16219 if (t == NULL)
16220 return t;
16221
16222 if (TREE_CODE (t) != TREE_LIST)
16223 return tsubst_copy_and_build (t, args, complain, in_decl,
16224 /*function_p=*/false,
16225 /*integral_constant_expression_p=*/false);
16226
16227 if (t == void_list_node)
16228 return t;
16229
16230 purpose = TREE_PURPOSE (t);
16231 if (purpose)
16232 purpose = RECUR (purpose);
16233 value = TREE_VALUE (t);
16234 if (value)
16235 {
16236 if (TREE_CODE (value) != LABEL_DECL)
16237 value = RECUR (value);
16238 else
16239 {
16240 value = lookup_label (DECL_NAME (value));
16241 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16242 TREE_USED (value) = 1;
16243 }
16244 }
16245 chain = TREE_CHAIN (t);
16246 if (chain && chain != void_type_node)
16247 chain = RECUR (chain);
16248 return tree_cons (purpose, value, chain);
16249 #undef RECUR
16250 }
16251
16252 /* Used to temporarily communicate the list of #pragma omp parallel
16253 clauses to #pragma omp for instantiation if they are combined
16254 together. */
16255
16256 static tree *omp_parallel_combined_clauses;
16257
16258 /* Substitute one OMP_FOR iterator. */
16259
16260 static void
16261 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
16262 tree initv, tree condv, tree incrv, tree *clauses,
16263 tree args, tsubst_flags_t complain, tree in_decl,
16264 bool integral_constant_expression_p)
16265 {
16266 #define RECUR(NODE) \
16267 tsubst_expr ((NODE), args, complain, in_decl, \
16268 integral_constant_expression_p)
16269 tree decl, init, cond, incr;
16270
16271 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16272 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16273
16274 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16275 {
16276 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16277 if (TREE_CODE (o) == TREE_LIST)
16278 TREE_VEC_ELT (orig_declv, i)
16279 = tree_cons (RECUR (TREE_PURPOSE (o)),
16280 RECUR (TREE_VALUE (o)), NULL_TREE);
16281 else
16282 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16283 }
16284
16285 decl = TREE_OPERAND (init, 0);
16286 init = TREE_OPERAND (init, 1);
16287 tree decl_expr = NULL_TREE;
16288 if (init && TREE_CODE (init) == DECL_EXPR)
16289 {
16290 /* We need to jump through some hoops to handle declarations in the
16291 init-statement, since we might need to handle auto deduction,
16292 but we need to keep control of initialization. */
16293 decl_expr = init;
16294 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16295 decl = tsubst_decl (decl, args, complain);
16296 }
16297 else
16298 {
16299 if (TREE_CODE (decl) == SCOPE_REF)
16300 {
16301 decl = RECUR (decl);
16302 if (TREE_CODE (decl) == COMPONENT_REF)
16303 {
16304 tree v = decl;
16305 while (v)
16306 switch (TREE_CODE (v))
16307 {
16308 case COMPONENT_REF:
16309 case MEM_REF:
16310 case INDIRECT_REF:
16311 CASE_CONVERT:
16312 case POINTER_PLUS_EXPR:
16313 v = TREE_OPERAND (v, 0);
16314 continue;
16315 case PARM_DECL:
16316 if (DECL_CONTEXT (v) == current_function_decl
16317 && DECL_ARTIFICIAL (v)
16318 && DECL_NAME (v) == this_identifier)
16319 {
16320 decl = TREE_OPERAND (decl, 1);
16321 decl = omp_privatize_field (decl, false);
16322 }
16323 /* FALLTHRU */
16324 default:
16325 v = NULL_TREE;
16326 break;
16327 }
16328 }
16329 }
16330 else
16331 decl = RECUR (decl);
16332 }
16333 init = RECUR (init);
16334
16335 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16336 if (auto_node && init)
16337 TREE_TYPE (decl)
16338 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16339
16340 gcc_assert (!type_dependent_expression_p (decl));
16341
16342 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16343 {
16344 if (decl_expr)
16345 {
16346 /* Declare the variable, but don't let that initialize it. */
16347 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16348 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16349 RECUR (decl_expr);
16350 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16351 }
16352
16353 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16354 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16355 if (TREE_CODE (incr) == MODIFY_EXPR)
16356 {
16357 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16358 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16359 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16360 NOP_EXPR, rhs, complain);
16361 }
16362 else
16363 incr = RECUR (incr);
16364 TREE_VEC_ELT (declv, i) = decl;
16365 TREE_VEC_ELT (initv, i) = init;
16366 TREE_VEC_ELT (condv, i) = cond;
16367 TREE_VEC_ELT (incrv, i) = incr;
16368 return;
16369 }
16370
16371 if (decl_expr)
16372 {
16373 /* Declare and initialize the variable. */
16374 RECUR (decl_expr);
16375 init = NULL_TREE;
16376 }
16377 else if (init)
16378 {
16379 tree *pc;
16380 int j;
16381 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16382 {
16383 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16384 {
16385 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16386 && OMP_CLAUSE_DECL (*pc) == decl)
16387 break;
16388 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16389 && OMP_CLAUSE_DECL (*pc) == decl)
16390 {
16391 if (j)
16392 break;
16393 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16394 tree c = *pc;
16395 *pc = OMP_CLAUSE_CHAIN (c);
16396 OMP_CLAUSE_CHAIN (c) = *clauses;
16397 *clauses = c;
16398 }
16399 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16400 && OMP_CLAUSE_DECL (*pc) == decl)
16401 {
16402 error ("iteration variable %qD should not be firstprivate",
16403 decl);
16404 *pc = OMP_CLAUSE_CHAIN (*pc);
16405 }
16406 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16407 && OMP_CLAUSE_DECL (*pc) == decl)
16408 {
16409 error ("iteration variable %qD should not be reduction",
16410 decl);
16411 *pc = OMP_CLAUSE_CHAIN (*pc);
16412 }
16413 else
16414 pc = &OMP_CLAUSE_CHAIN (*pc);
16415 }
16416 if (*pc)
16417 break;
16418 }
16419 if (*pc == NULL_TREE)
16420 {
16421 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16422 OMP_CLAUSE_DECL (c) = decl;
16423 c = finish_omp_clauses (c, C_ORT_OMP);
16424 if (c)
16425 {
16426 OMP_CLAUSE_CHAIN (c) = *clauses;
16427 *clauses = c;
16428 }
16429 }
16430 }
16431 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16432 if (COMPARISON_CLASS_P (cond))
16433 {
16434 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16435 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16436 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16437 }
16438 else
16439 cond = RECUR (cond);
16440 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16441 switch (TREE_CODE (incr))
16442 {
16443 case PREINCREMENT_EXPR:
16444 case PREDECREMENT_EXPR:
16445 case POSTINCREMENT_EXPR:
16446 case POSTDECREMENT_EXPR:
16447 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16448 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16449 break;
16450 case MODIFY_EXPR:
16451 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16452 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16453 {
16454 tree rhs = TREE_OPERAND (incr, 1);
16455 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16456 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16457 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16458 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16459 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16460 rhs0, rhs1));
16461 }
16462 else
16463 incr = RECUR (incr);
16464 break;
16465 case MODOP_EXPR:
16466 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16467 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16468 {
16469 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16470 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16471 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16472 TREE_TYPE (decl), lhs,
16473 RECUR (TREE_OPERAND (incr, 2))));
16474 }
16475 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16476 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16477 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16478 {
16479 tree rhs = TREE_OPERAND (incr, 2);
16480 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16481 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16482 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16483 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16484 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16485 rhs0, rhs1));
16486 }
16487 else
16488 incr = RECUR (incr);
16489 break;
16490 default:
16491 incr = RECUR (incr);
16492 break;
16493 }
16494
16495 TREE_VEC_ELT (declv, i) = decl;
16496 TREE_VEC_ELT (initv, i) = init;
16497 TREE_VEC_ELT (condv, i) = cond;
16498 TREE_VEC_ELT (incrv, i) = incr;
16499 #undef RECUR
16500 }
16501
16502 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16503 of OMP_TARGET's body. */
16504
16505 static tree
16506 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16507 {
16508 *walk_subtrees = 0;
16509 switch (TREE_CODE (*tp))
16510 {
16511 case OMP_TEAMS:
16512 return *tp;
16513 case BIND_EXPR:
16514 case STATEMENT_LIST:
16515 *walk_subtrees = 1;
16516 break;
16517 default:
16518 break;
16519 }
16520 return NULL_TREE;
16521 }
16522
16523 /* Helper function for tsubst_expr. For decomposition declaration
16524 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16525 also the corresponding decls representing the identifiers
16526 of the decomposition declaration. Return DECL if successful
16527 or error_mark_node otherwise, set *FIRST to the first decl
16528 in the list chained through DECL_CHAIN and *CNT to the number
16529 of such decls. */
16530
16531 static tree
16532 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16533 tsubst_flags_t complain, tree in_decl, tree *first,
16534 unsigned int *cnt)
16535 {
16536 tree decl2, decl3, prev = decl;
16537 *cnt = 0;
16538 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16539 for (decl2 = DECL_CHAIN (pattern_decl);
16540 decl2
16541 && VAR_P (decl2)
16542 && DECL_DECOMPOSITION_P (decl2)
16543 && DECL_NAME (decl2);
16544 decl2 = DECL_CHAIN (decl2))
16545 {
16546 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16547 {
16548 gcc_assert (errorcount);
16549 return error_mark_node;
16550 }
16551 (*cnt)++;
16552 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16553 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16554 tree v = DECL_VALUE_EXPR (decl2);
16555 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16556 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16557 decl3 = tsubst (decl2, args, complain, in_decl);
16558 SET_DECL_VALUE_EXPR (decl2, v);
16559 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16560 if (VAR_P (decl3))
16561 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16562 else
16563 {
16564 gcc_assert (errorcount);
16565 decl = error_mark_node;
16566 continue;
16567 }
16568 maybe_push_decl (decl3);
16569 if (error_operand_p (decl3))
16570 decl = error_mark_node;
16571 else if (decl != error_mark_node
16572 && DECL_CHAIN (decl3) != prev
16573 && decl != prev)
16574 {
16575 gcc_assert (errorcount);
16576 decl = error_mark_node;
16577 }
16578 else
16579 prev = decl3;
16580 }
16581 *first = prev;
16582 return decl;
16583 }
16584
16585 /* Like tsubst_copy for expressions, etc. but also does semantic
16586 processing. */
16587
16588 tree
16589 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16590 bool integral_constant_expression_p)
16591 {
16592 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16593 #define RECUR(NODE) \
16594 tsubst_expr ((NODE), args, complain, in_decl, \
16595 integral_constant_expression_p)
16596
16597 tree stmt, tmp;
16598 tree r;
16599 location_t loc;
16600
16601 if (t == NULL_TREE || t == error_mark_node)
16602 return t;
16603
16604 loc = input_location;
16605 if (location_t eloc = cp_expr_location (t))
16606 input_location = eloc;
16607 if (STATEMENT_CODE_P (TREE_CODE (t)))
16608 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16609
16610 switch (TREE_CODE (t))
16611 {
16612 case STATEMENT_LIST:
16613 {
16614 tree_stmt_iterator i;
16615 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16616 RECUR (tsi_stmt (i));
16617 break;
16618 }
16619
16620 case CTOR_INITIALIZER:
16621 finish_mem_initializers (tsubst_initializer_list
16622 (TREE_OPERAND (t, 0), args));
16623 break;
16624
16625 case RETURN_EXPR:
16626 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16627 break;
16628
16629 case EXPR_STMT:
16630 tmp = RECUR (EXPR_STMT_EXPR (t));
16631 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16632 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16633 else
16634 finish_expr_stmt (tmp);
16635 break;
16636
16637 case USING_STMT:
16638 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16639 /*attribs=*/NULL_TREE);
16640 break;
16641
16642 case DECL_EXPR:
16643 {
16644 tree decl, pattern_decl;
16645 tree init;
16646
16647 pattern_decl = decl = DECL_EXPR_DECL (t);
16648 if (TREE_CODE (decl) == LABEL_DECL)
16649 finish_label_decl (DECL_NAME (decl));
16650 else if (TREE_CODE (decl) == USING_DECL)
16651 {
16652 tree scope = USING_DECL_SCOPE (decl);
16653 tree name = DECL_NAME (decl);
16654
16655 scope = tsubst (scope, args, complain, in_decl);
16656 decl = lookup_qualified_name (scope, name,
16657 /*is_type_p=*/false,
16658 /*complain=*/false);
16659 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16660 qualified_name_lookup_error (scope, name, decl, input_location);
16661 else
16662 finish_local_using_decl (decl, scope, name);
16663 }
16664 else if (is_capture_proxy (decl)
16665 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16666 {
16667 /* We're in tsubst_lambda_expr, we've already inserted a new
16668 capture proxy, so look it up and register it. */
16669 tree inst;
16670 if (DECL_PACK_P (decl))
16671 {
16672 inst = (retrieve_local_specialization
16673 (DECL_CAPTURED_VARIABLE (decl)));
16674 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16675 }
16676 else
16677 {
16678 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16679 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16680 gcc_assert (inst != decl && is_capture_proxy (inst));
16681 }
16682 register_local_specialization (inst, decl);
16683 break;
16684 }
16685 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16686 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16687 /* Don't copy the old closure; we'll create a new one in
16688 tsubst_lambda_expr. */
16689 break;
16690 else
16691 {
16692 init = DECL_INITIAL (decl);
16693 decl = tsubst (decl, args, complain, in_decl);
16694 if (decl != error_mark_node)
16695 {
16696 /* By marking the declaration as instantiated, we avoid
16697 trying to instantiate it. Since instantiate_decl can't
16698 handle local variables, and since we've already done
16699 all that needs to be done, that's the right thing to
16700 do. */
16701 if (VAR_P (decl))
16702 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16703 if (VAR_P (decl) && !DECL_NAME (decl)
16704 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16705 /* Anonymous aggregates are a special case. */
16706 finish_anon_union (decl);
16707 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16708 {
16709 DECL_CONTEXT (decl) = current_function_decl;
16710 if (DECL_NAME (decl) == this_identifier)
16711 {
16712 tree lam = DECL_CONTEXT (current_function_decl);
16713 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16714 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16715 }
16716 insert_capture_proxy (decl);
16717 }
16718 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16719 /* We already did a pushtag. */;
16720 else if (TREE_CODE (decl) == FUNCTION_DECL
16721 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16722 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16723 {
16724 DECL_CONTEXT (decl) = NULL_TREE;
16725 pushdecl (decl);
16726 DECL_CONTEXT (decl) = current_function_decl;
16727 cp_check_omp_declare_reduction (decl);
16728 }
16729 else
16730 {
16731 int const_init = false;
16732 maybe_push_decl (decl);
16733 if (VAR_P (decl)
16734 && DECL_PRETTY_FUNCTION_P (decl))
16735 {
16736 /* For __PRETTY_FUNCTION__ we have to adjust the
16737 initializer. */
16738 const char *const name
16739 = cxx_printable_name (current_function_decl, 2);
16740 init = cp_fname_init (name, &TREE_TYPE (decl));
16741 }
16742 else
16743 init = tsubst_init (init, decl, args, complain, in_decl);
16744
16745 if (VAR_P (decl))
16746 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16747 (pattern_decl));
16748 if (VAR_P (decl)
16749 && DECL_DECOMPOSITION_P (decl)
16750 && TREE_TYPE (pattern_decl) != error_mark_node)
16751 {
16752 unsigned int cnt;
16753 tree first;
16754 tree ndecl
16755 = tsubst_decomp_names (decl, pattern_decl, args,
16756 complain, in_decl, &first, &cnt);
16757 if (ndecl != error_mark_node)
16758 cp_maybe_mangle_decomp (ndecl, first, cnt);
16759 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16760 if (ndecl != error_mark_node)
16761 cp_finish_decomp (ndecl, first, cnt);
16762 }
16763 else
16764 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16765 }
16766 }
16767 }
16768
16769 break;
16770 }
16771
16772 case FOR_STMT:
16773 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16774 RECUR (FOR_INIT_STMT (t));
16775 finish_init_stmt (stmt);
16776 tmp = RECUR (FOR_COND (t));
16777 finish_for_cond (tmp, stmt, false, 0);
16778 tmp = RECUR (FOR_EXPR (t));
16779 finish_for_expr (tmp, stmt);
16780 {
16781 bool prev = note_iteration_stmt_body_start ();
16782 RECUR (FOR_BODY (t));
16783 note_iteration_stmt_body_end (prev);
16784 }
16785 finish_for_stmt (stmt);
16786 break;
16787
16788 case RANGE_FOR_STMT:
16789 {
16790 /* Construct another range_for, if this is not a final
16791 substitution (for inside inside a generic lambda of a
16792 template). Otherwise convert to a regular for. */
16793 tree decl, expr;
16794 stmt = (processing_template_decl
16795 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16796 : begin_for_stmt (NULL_TREE, NULL_TREE));
16797 decl = RANGE_FOR_DECL (t);
16798 decl = tsubst (decl, args, complain, in_decl);
16799 maybe_push_decl (decl);
16800 expr = RECUR (RANGE_FOR_EXPR (t));
16801
16802 tree decomp_first = NULL_TREE;
16803 unsigned decomp_cnt = 0;
16804 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16805 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16806 complain, in_decl,
16807 &decomp_first, &decomp_cnt);
16808
16809 if (processing_template_decl)
16810 {
16811 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16812 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16813 finish_range_for_decl (stmt, decl, expr);
16814 }
16815 else
16816 {
16817 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16818 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16819 stmt = cp_convert_range_for (stmt, decl, expr,
16820 decomp_first, decomp_cnt,
16821 RANGE_FOR_IVDEP (t), unroll);
16822 }
16823
16824 bool prev = note_iteration_stmt_body_start ();
16825 RECUR (RANGE_FOR_BODY (t));
16826 note_iteration_stmt_body_end (prev);
16827 finish_for_stmt (stmt);
16828 }
16829 break;
16830
16831 case WHILE_STMT:
16832 stmt = begin_while_stmt ();
16833 tmp = RECUR (WHILE_COND (t));
16834 finish_while_stmt_cond (tmp, stmt, false, 0);
16835 {
16836 bool prev = note_iteration_stmt_body_start ();
16837 RECUR (WHILE_BODY (t));
16838 note_iteration_stmt_body_end (prev);
16839 }
16840 finish_while_stmt (stmt);
16841 break;
16842
16843 case DO_STMT:
16844 stmt = begin_do_stmt ();
16845 {
16846 bool prev = note_iteration_stmt_body_start ();
16847 RECUR (DO_BODY (t));
16848 note_iteration_stmt_body_end (prev);
16849 }
16850 finish_do_body (stmt);
16851 tmp = RECUR (DO_COND (t));
16852 finish_do_stmt (tmp, stmt, false, 0);
16853 break;
16854
16855 case IF_STMT:
16856 stmt = begin_if_stmt ();
16857 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16858 if (IF_STMT_CONSTEXPR_P (t))
16859 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
16860 tmp = RECUR (IF_COND (t));
16861 tmp = finish_if_stmt_cond (tmp, stmt);
16862 if (IF_STMT_CONSTEXPR_P (t)
16863 && instantiation_dependent_expression_p (tmp))
16864 {
16865 /* We're partially instantiating a generic lambda, but the condition
16866 of the constexpr if is still dependent. Don't substitute into the
16867 branches now, just remember the template arguments. */
16868 do_poplevel (IF_SCOPE (stmt));
16869 IF_COND (stmt) = IF_COND (t);
16870 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
16871 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
16872 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
16873 add_stmt (stmt);
16874 break;
16875 }
16876 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16877 /* Don't instantiate the THEN_CLAUSE. */;
16878 else
16879 {
16880 tree folded = fold_non_dependent_expr (tmp, complain);
16881 bool inhibit = integer_zerop (folded);
16882 if (inhibit)
16883 ++c_inhibit_evaluation_warnings;
16884 RECUR (THEN_CLAUSE (t));
16885 if (inhibit)
16886 --c_inhibit_evaluation_warnings;
16887 }
16888 finish_then_clause (stmt);
16889
16890 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16891 /* Don't instantiate the ELSE_CLAUSE. */;
16892 else if (ELSE_CLAUSE (t))
16893 {
16894 tree folded = fold_non_dependent_expr (tmp, complain);
16895 bool inhibit = integer_nonzerop (folded);
16896 begin_else_clause (stmt);
16897 if (inhibit)
16898 ++c_inhibit_evaluation_warnings;
16899 RECUR (ELSE_CLAUSE (t));
16900 if (inhibit)
16901 --c_inhibit_evaluation_warnings;
16902 finish_else_clause (stmt);
16903 }
16904
16905 finish_if_stmt (stmt);
16906 break;
16907
16908 case BIND_EXPR:
16909 if (BIND_EXPR_BODY_BLOCK (t))
16910 stmt = begin_function_body ();
16911 else
16912 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16913 ? BCS_TRY_BLOCK : 0);
16914
16915 RECUR (BIND_EXPR_BODY (t));
16916
16917 if (BIND_EXPR_BODY_BLOCK (t))
16918 finish_function_body (stmt);
16919 else
16920 finish_compound_stmt (stmt);
16921 break;
16922
16923 case BREAK_STMT:
16924 finish_break_stmt ();
16925 break;
16926
16927 case CONTINUE_STMT:
16928 finish_continue_stmt ();
16929 break;
16930
16931 case SWITCH_STMT:
16932 stmt = begin_switch_stmt ();
16933 tmp = RECUR (SWITCH_STMT_COND (t));
16934 finish_switch_cond (tmp, stmt);
16935 RECUR (SWITCH_STMT_BODY (t));
16936 finish_switch_stmt (stmt);
16937 break;
16938
16939 case CASE_LABEL_EXPR:
16940 {
16941 tree low = RECUR (CASE_LOW (t));
16942 tree high = RECUR (CASE_HIGH (t));
16943 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16944 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16945 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16946 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16947 }
16948 break;
16949
16950 case LABEL_EXPR:
16951 {
16952 tree decl = LABEL_EXPR_LABEL (t);
16953 tree label;
16954
16955 label = finish_label_stmt (DECL_NAME (decl));
16956 if (TREE_CODE (label) == LABEL_DECL)
16957 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16958 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16959 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16960 }
16961 break;
16962
16963 case GOTO_EXPR:
16964 tmp = GOTO_DESTINATION (t);
16965 if (TREE_CODE (tmp) != LABEL_DECL)
16966 /* Computed goto's must be tsubst'd into. On the other hand,
16967 non-computed gotos must not be; the identifier in question
16968 will have no binding. */
16969 tmp = RECUR (tmp);
16970 else
16971 tmp = DECL_NAME (tmp);
16972 finish_goto_stmt (tmp);
16973 break;
16974
16975 case ASM_EXPR:
16976 {
16977 tree string = RECUR (ASM_STRING (t));
16978 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16979 complain, in_decl);
16980 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16981 complain, in_decl);
16982 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16983 complain, in_decl);
16984 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16985 complain, in_decl);
16986 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16987 clobbers, labels);
16988 tree asm_expr = tmp;
16989 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16990 asm_expr = TREE_OPERAND (asm_expr, 0);
16991 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16992 }
16993 break;
16994
16995 case TRY_BLOCK:
16996 if (CLEANUP_P (t))
16997 {
16998 stmt = begin_try_block ();
16999 RECUR (TRY_STMTS (t));
17000 finish_cleanup_try_block (stmt);
17001 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17002 }
17003 else
17004 {
17005 tree compound_stmt = NULL_TREE;
17006
17007 if (FN_TRY_BLOCK_P (t))
17008 stmt = begin_function_try_block (&compound_stmt);
17009 else
17010 stmt = begin_try_block ();
17011
17012 RECUR (TRY_STMTS (t));
17013
17014 if (FN_TRY_BLOCK_P (t))
17015 finish_function_try_block (stmt);
17016 else
17017 finish_try_block (stmt);
17018
17019 RECUR (TRY_HANDLERS (t));
17020 if (FN_TRY_BLOCK_P (t))
17021 finish_function_handler_sequence (stmt, compound_stmt);
17022 else
17023 finish_handler_sequence (stmt);
17024 }
17025 break;
17026
17027 case HANDLER:
17028 {
17029 tree decl = HANDLER_PARMS (t);
17030
17031 if (decl)
17032 {
17033 decl = tsubst (decl, args, complain, in_decl);
17034 /* Prevent instantiate_decl from trying to instantiate
17035 this variable. We've already done all that needs to be
17036 done. */
17037 if (decl != error_mark_node)
17038 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17039 }
17040 stmt = begin_handler ();
17041 finish_handler_parms (decl, stmt);
17042 RECUR (HANDLER_BODY (t));
17043 finish_handler (stmt);
17044 }
17045 break;
17046
17047 case TAG_DEFN:
17048 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17049 if (CLASS_TYPE_P (tmp))
17050 {
17051 /* Local classes are not independent templates; they are
17052 instantiated along with their containing function. And this
17053 way we don't have to deal with pushing out of one local class
17054 to instantiate a member of another local class. */
17055 /* Closures are handled by the LAMBDA_EXPR. */
17056 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17057 complete_type (tmp);
17058 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17059 if ((VAR_P (fld)
17060 || (TREE_CODE (fld) == FUNCTION_DECL
17061 && !DECL_ARTIFICIAL (fld)))
17062 && DECL_TEMPLATE_INSTANTIATION (fld))
17063 instantiate_decl (fld, /*defer_ok=*/false,
17064 /*expl_inst_class=*/false);
17065 }
17066 break;
17067
17068 case STATIC_ASSERT:
17069 {
17070 tree condition;
17071
17072 ++c_inhibit_evaluation_warnings;
17073 condition =
17074 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17075 args,
17076 complain, in_decl,
17077 /*integral_constant_expression_p=*/true);
17078 --c_inhibit_evaluation_warnings;
17079
17080 finish_static_assert (condition,
17081 STATIC_ASSERT_MESSAGE (t),
17082 STATIC_ASSERT_SOURCE_LOCATION (t),
17083 /*member_p=*/false);
17084 }
17085 break;
17086
17087 case OACC_KERNELS:
17088 case OACC_PARALLEL:
17089 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17090 in_decl);
17091 stmt = begin_omp_parallel ();
17092 RECUR (OMP_BODY (t));
17093 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17094 break;
17095
17096 case OMP_PARALLEL:
17097 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17098 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17099 complain, in_decl);
17100 if (OMP_PARALLEL_COMBINED (t))
17101 omp_parallel_combined_clauses = &tmp;
17102 stmt = begin_omp_parallel ();
17103 RECUR (OMP_PARALLEL_BODY (t));
17104 gcc_assert (omp_parallel_combined_clauses == NULL);
17105 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17106 = OMP_PARALLEL_COMBINED (t);
17107 pop_omp_privatization_clauses (r);
17108 break;
17109
17110 case OMP_TASK:
17111 r = push_omp_privatization_clauses (false);
17112 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17113 complain, in_decl);
17114 stmt = begin_omp_task ();
17115 RECUR (OMP_TASK_BODY (t));
17116 finish_omp_task (tmp, stmt);
17117 pop_omp_privatization_clauses (r);
17118 break;
17119
17120 case OMP_FOR:
17121 case OMP_SIMD:
17122 case OMP_DISTRIBUTE:
17123 case OMP_TASKLOOP:
17124 case OACC_LOOP:
17125 {
17126 tree clauses, body, pre_body;
17127 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17128 tree orig_declv = NULL_TREE;
17129 tree incrv = NULL_TREE;
17130 enum c_omp_region_type ort = C_ORT_OMP;
17131 int i;
17132
17133 if (TREE_CODE (t) == OACC_LOOP)
17134 ort = C_ORT_ACC;
17135
17136 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17137 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17138 in_decl);
17139 if (OMP_FOR_INIT (t) != NULL_TREE)
17140 {
17141 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17142 if (OMP_FOR_ORIG_DECLS (t))
17143 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17144 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17145 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17146 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17147 }
17148
17149 stmt = begin_omp_structured_block ();
17150
17151 pre_body = push_stmt_list ();
17152 RECUR (OMP_FOR_PRE_BODY (t));
17153 pre_body = pop_stmt_list (pre_body);
17154
17155 if (OMP_FOR_INIT (t) != NULL_TREE)
17156 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17157 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
17158 incrv, &clauses, args, complain, in_decl,
17159 integral_constant_expression_p);
17160 omp_parallel_combined_clauses = NULL;
17161
17162 body = push_stmt_list ();
17163 RECUR (OMP_FOR_BODY (t));
17164 body = pop_stmt_list (body);
17165
17166 if (OMP_FOR_INIT (t) != NULL_TREE)
17167 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17168 orig_declv, initv, condv, incrv, body, pre_body,
17169 NULL, clauses);
17170 else
17171 {
17172 t = make_node (TREE_CODE (t));
17173 TREE_TYPE (t) = void_type_node;
17174 OMP_FOR_BODY (t) = body;
17175 OMP_FOR_PRE_BODY (t) = pre_body;
17176 OMP_FOR_CLAUSES (t) = clauses;
17177 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17178 add_stmt (t);
17179 }
17180
17181 add_stmt (finish_omp_structured_block (stmt));
17182 pop_omp_privatization_clauses (r);
17183 }
17184 break;
17185
17186 case OMP_SECTIONS:
17187 omp_parallel_combined_clauses = NULL;
17188 /* FALLTHRU */
17189 case OMP_SINGLE:
17190 case OMP_TEAMS:
17191 case OMP_CRITICAL:
17192 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17193 && OMP_TEAMS_COMBINED (t));
17194 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17195 in_decl);
17196 stmt = push_stmt_list ();
17197 RECUR (OMP_BODY (t));
17198 stmt = pop_stmt_list (stmt);
17199
17200 t = copy_node (t);
17201 OMP_BODY (t) = stmt;
17202 OMP_CLAUSES (t) = tmp;
17203 add_stmt (t);
17204 pop_omp_privatization_clauses (r);
17205 break;
17206
17207 case OACC_DATA:
17208 case OMP_TARGET_DATA:
17209 case OMP_TARGET:
17210 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17211 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17212 in_decl);
17213 keep_next_level (true);
17214 stmt = begin_omp_structured_block ();
17215
17216 RECUR (OMP_BODY (t));
17217 stmt = finish_omp_structured_block (stmt);
17218
17219 t = copy_node (t);
17220 OMP_BODY (t) = stmt;
17221 OMP_CLAUSES (t) = tmp;
17222 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17223 {
17224 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17225 if (teams)
17226 {
17227 /* For combined target teams, ensure the num_teams and
17228 thread_limit clause expressions are evaluated on the host,
17229 before entering the target construct. */
17230 tree c;
17231 for (c = OMP_TEAMS_CLAUSES (teams);
17232 c; c = OMP_CLAUSE_CHAIN (c))
17233 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17234 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17235 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17236 {
17237 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17238 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17239 if (expr == error_mark_node)
17240 continue;
17241 tmp = TARGET_EXPR_SLOT (expr);
17242 add_stmt (expr);
17243 OMP_CLAUSE_OPERAND (c, 0) = expr;
17244 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17245 OMP_CLAUSE_FIRSTPRIVATE);
17246 OMP_CLAUSE_DECL (tc) = tmp;
17247 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17248 OMP_TARGET_CLAUSES (t) = tc;
17249 }
17250 }
17251 }
17252 add_stmt (t);
17253 break;
17254
17255 case OACC_DECLARE:
17256 t = copy_node (t);
17257 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17258 complain, in_decl);
17259 OACC_DECLARE_CLAUSES (t) = tmp;
17260 add_stmt (t);
17261 break;
17262
17263 case OMP_TARGET_UPDATE:
17264 case OMP_TARGET_ENTER_DATA:
17265 case OMP_TARGET_EXIT_DATA:
17266 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17267 complain, in_decl);
17268 t = copy_node (t);
17269 OMP_STANDALONE_CLAUSES (t) = tmp;
17270 add_stmt (t);
17271 break;
17272
17273 case OACC_ENTER_DATA:
17274 case OACC_EXIT_DATA:
17275 case OACC_UPDATE:
17276 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17277 complain, in_decl);
17278 t = copy_node (t);
17279 OMP_STANDALONE_CLAUSES (t) = tmp;
17280 add_stmt (t);
17281 break;
17282
17283 case OMP_ORDERED:
17284 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17285 complain, in_decl);
17286 stmt = push_stmt_list ();
17287 RECUR (OMP_BODY (t));
17288 stmt = pop_stmt_list (stmt);
17289
17290 t = copy_node (t);
17291 OMP_BODY (t) = stmt;
17292 OMP_ORDERED_CLAUSES (t) = tmp;
17293 add_stmt (t);
17294 break;
17295
17296 case OMP_SECTION:
17297 case OMP_MASTER:
17298 case OMP_TASKGROUP:
17299 stmt = push_stmt_list ();
17300 RECUR (OMP_BODY (t));
17301 stmt = pop_stmt_list (stmt);
17302
17303 t = copy_node (t);
17304 OMP_BODY (t) = stmt;
17305 add_stmt (t);
17306 break;
17307
17308 case OMP_ATOMIC:
17309 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17310 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17311 {
17312 tree op1 = TREE_OPERAND (t, 1);
17313 tree rhs1 = NULL_TREE;
17314 tree lhs, rhs;
17315 if (TREE_CODE (op1) == COMPOUND_EXPR)
17316 {
17317 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17318 op1 = TREE_OPERAND (op1, 1);
17319 }
17320 lhs = RECUR (TREE_OPERAND (op1, 0));
17321 rhs = RECUR (TREE_OPERAND (op1, 1));
17322 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
17323 NULL_TREE, NULL_TREE, rhs1,
17324 OMP_ATOMIC_SEQ_CST (t));
17325 }
17326 else
17327 {
17328 tree op1 = TREE_OPERAND (t, 1);
17329 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17330 tree rhs1 = NULL_TREE;
17331 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17332 enum tree_code opcode = NOP_EXPR;
17333 if (code == OMP_ATOMIC_READ)
17334 {
17335 v = RECUR (TREE_OPERAND (op1, 0));
17336 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17337 }
17338 else if (code == OMP_ATOMIC_CAPTURE_OLD
17339 || code == OMP_ATOMIC_CAPTURE_NEW)
17340 {
17341 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17342 v = RECUR (TREE_OPERAND (op1, 0));
17343 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17344 if (TREE_CODE (op11) == COMPOUND_EXPR)
17345 {
17346 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17347 op11 = TREE_OPERAND (op11, 1);
17348 }
17349 lhs = RECUR (TREE_OPERAND (op11, 0));
17350 rhs = RECUR (TREE_OPERAND (op11, 1));
17351 opcode = TREE_CODE (op11);
17352 if (opcode == MODIFY_EXPR)
17353 opcode = NOP_EXPR;
17354 }
17355 else
17356 {
17357 code = OMP_ATOMIC;
17358 lhs = RECUR (TREE_OPERAND (op1, 0));
17359 rhs = RECUR (TREE_OPERAND (op1, 1));
17360 }
17361 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
17362 OMP_ATOMIC_SEQ_CST (t));
17363 }
17364 break;
17365
17366 case TRANSACTION_EXPR:
17367 {
17368 int flags = 0;
17369 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17370 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17371
17372 if (TRANSACTION_EXPR_IS_STMT (t))
17373 {
17374 tree body = TRANSACTION_EXPR_BODY (t);
17375 tree noex = NULL_TREE;
17376 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17377 {
17378 noex = MUST_NOT_THROW_COND (body);
17379 if (noex == NULL_TREE)
17380 noex = boolean_true_node;
17381 body = TREE_OPERAND (body, 0);
17382 }
17383 stmt = begin_transaction_stmt (input_location, NULL, flags);
17384 RECUR (body);
17385 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17386 }
17387 else
17388 {
17389 stmt = build_transaction_expr (EXPR_LOCATION (t),
17390 RECUR (TRANSACTION_EXPR_BODY (t)),
17391 flags, NULL_TREE);
17392 RETURN (stmt);
17393 }
17394 }
17395 break;
17396
17397 case MUST_NOT_THROW_EXPR:
17398 {
17399 tree op0 = RECUR (TREE_OPERAND (t, 0));
17400 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17401 RETURN (build_must_not_throw_expr (op0, cond));
17402 }
17403
17404 case EXPR_PACK_EXPANSION:
17405 error ("invalid use of pack expansion expression");
17406 RETURN (error_mark_node);
17407
17408 case NONTYPE_ARGUMENT_PACK:
17409 error ("use %<...%> to expand argument pack");
17410 RETURN (error_mark_node);
17411
17412 case COMPOUND_EXPR:
17413 tmp = RECUR (TREE_OPERAND (t, 0));
17414 if (tmp == NULL_TREE)
17415 /* If the first operand was a statement, we're done with it. */
17416 RETURN (RECUR (TREE_OPERAND (t, 1)));
17417 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17418 RECUR (TREE_OPERAND (t, 1)),
17419 complain));
17420
17421 case ANNOTATE_EXPR:
17422 tmp = RECUR (TREE_OPERAND (t, 0));
17423 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17424 TREE_TYPE (tmp), tmp,
17425 RECUR (TREE_OPERAND (t, 1)),
17426 RECUR (TREE_OPERAND (t, 2))));
17427
17428 default:
17429 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17430
17431 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17432 /*function_p=*/false,
17433 integral_constant_expression_p));
17434 }
17435
17436 RETURN (NULL_TREE);
17437 out:
17438 input_location = loc;
17439 return r;
17440 #undef RECUR
17441 #undef RETURN
17442 }
17443
17444 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17445 function. For description of the body see comment above
17446 cp_parser_omp_declare_reduction_exprs. */
17447
17448 static void
17449 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17450 {
17451 if (t == NULL_TREE || t == error_mark_node)
17452 return;
17453
17454 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17455
17456 tree_stmt_iterator tsi;
17457 int i;
17458 tree stmts[7];
17459 memset (stmts, 0, sizeof stmts);
17460 for (i = 0, tsi = tsi_start (t);
17461 i < 7 && !tsi_end_p (tsi);
17462 i++, tsi_next (&tsi))
17463 stmts[i] = tsi_stmt (tsi);
17464 gcc_assert (tsi_end_p (tsi));
17465
17466 if (i >= 3)
17467 {
17468 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17469 && TREE_CODE (stmts[1]) == DECL_EXPR);
17470 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17471 args, complain, in_decl);
17472 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17473 args, complain, in_decl);
17474 DECL_CONTEXT (omp_out) = current_function_decl;
17475 DECL_CONTEXT (omp_in) = current_function_decl;
17476 keep_next_level (true);
17477 tree block = begin_omp_structured_block ();
17478 tsubst_expr (stmts[2], args, complain, in_decl, false);
17479 block = finish_omp_structured_block (block);
17480 block = maybe_cleanup_point_expr_void (block);
17481 add_decl_expr (omp_out);
17482 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17483 TREE_NO_WARNING (omp_out) = 1;
17484 add_decl_expr (omp_in);
17485 finish_expr_stmt (block);
17486 }
17487 if (i >= 6)
17488 {
17489 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17490 && TREE_CODE (stmts[4]) == DECL_EXPR);
17491 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17492 args, complain, in_decl);
17493 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17494 args, complain, in_decl);
17495 DECL_CONTEXT (omp_priv) = current_function_decl;
17496 DECL_CONTEXT (omp_orig) = current_function_decl;
17497 keep_next_level (true);
17498 tree block = begin_omp_structured_block ();
17499 tsubst_expr (stmts[5], args, complain, in_decl, false);
17500 block = finish_omp_structured_block (block);
17501 block = maybe_cleanup_point_expr_void (block);
17502 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17503 add_decl_expr (omp_priv);
17504 add_decl_expr (omp_orig);
17505 finish_expr_stmt (block);
17506 if (i == 7)
17507 add_decl_expr (omp_orig);
17508 }
17509 }
17510
17511 /* T is a postfix-expression that is not being used in a function
17512 call. Return the substituted version of T. */
17513
17514 static tree
17515 tsubst_non_call_postfix_expression (tree t, tree args,
17516 tsubst_flags_t complain,
17517 tree in_decl)
17518 {
17519 if (TREE_CODE (t) == SCOPE_REF)
17520 t = tsubst_qualified_id (t, args, complain, in_decl,
17521 /*done=*/false, /*address_p=*/false);
17522 else
17523 t = tsubst_copy_and_build (t, args, complain, in_decl,
17524 /*function_p=*/false,
17525 /*integral_constant_expression_p=*/false);
17526
17527 return t;
17528 }
17529
17530 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17531 instantiation context. Instantiating a pack expansion containing a lambda
17532 might result in multiple lambdas all based on the same lambda in the
17533 template. */
17534
17535 tree
17536 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17537 {
17538 tree oldfn = lambda_function (t);
17539 in_decl = oldfn;
17540
17541 tree r = build_lambda_expr ();
17542
17543 LAMBDA_EXPR_LOCATION (r)
17544 = LAMBDA_EXPR_LOCATION (t);
17545 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17546 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17547 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17548
17549 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17550 /* A lambda in a default argument outside a class gets no
17551 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17552 tsubst_default_argument calls start_lambda_scope, so we need to
17553 specifically ignore it here, and use the global scope. */
17554 record_null_lambda_scope (r);
17555 else
17556 record_lambda_scope (r);
17557
17558 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17559 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17560
17561 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17562 cap = TREE_CHAIN (cap))
17563 {
17564 tree field = TREE_PURPOSE (cap);
17565 if (PACK_EXPANSION_P (field))
17566 field = PACK_EXPANSION_PATTERN (field);
17567 field = tsubst_decl (field, args, complain);
17568
17569 if (field == error_mark_node)
17570 return error_mark_node;
17571
17572 tree init = TREE_VALUE (cap);
17573 if (PACK_EXPANSION_P (init))
17574 init = tsubst_pack_expansion (init, args, complain, in_decl);
17575 else
17576 init = tsubst_copy_and_build (init, args, complain, in_decl,
17577 /*fn*/false, /*constexpr*/false);
17578
17579 if (TREE_CODE (field) == TREE_VEC)
17580 {
17581 int len = TREE_VEC_LENGTH (field);
17582 gcc_assert (TREE_CODE (init) == TREE_VEC
17583 && TREE_VEC_LENGTH (init) == len);
17584 for (int i = 0; i < len; ++i)
17585 LAMBDA_EXPR_CAPTURE_LIST (r)
17586 = tree_cons (TREE_VEC_ELT (field, i),
17587 TREE_VEC_ELT (init, i),
17588 LAMBDA_EXPR_CAPTURE_LIST (r));
17589 }
17590 else
17591 {
17592 LAMBDA_EXPR_CAPTURE_LIST (r)
17593 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17594
17595 if (id_equal (DECL_NAME (field), "__this"))
17596 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17597 }
17598 }
17599
17600 tree type = begin_lambda_type (r);
17601 if (type == error_mark_node)
17602 return error_mark_node;
17603
17604 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17605 determine_visibility (TYPE_NAME (type));
17606
17607 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17608
17609 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17610 ? DECL_TI_TEMPLATE (oldfn)
17611 : NULL_TREE);
17612
17613 tree fntype = static_fn_type (oldfn);
17614 if (oldtmpl)
17615 ++processing_template_decl;
17616 fntype = tsubst (fntype, args, complain, in_decl);
17617 if (oldtmpl)
17618 --processing_template_decl;
17619
17620 if (fntype == error_mark_node)
17621 r = error_mark_node;
17622 else
17623 {
17624 /* Fix the type of 'this'. */
17625 fntype = build_memfn_type (fntype, type,
17626 type_memfn_quals (fntype),
17627 type_memfn_rqual (fntype));
17628 tree fn, tmpl;
17629 if (oldtmpl)
17630 {
17631 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17632 fn = DECL_TEMPLATE_RESULT (tmpl);
17633 finish_member_declaration (tmpl);
17634 }
17635 else
17636 {
17637 tmpl = NULL_TREE;
17638 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17639 finish_member_declaration (fn);
17640 }
17641
17642 /* Let finish_function set this. */
17643 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17644
17645 bool nested = cfun;
17646 if (nested)
17647 push_function_context ();
17648 else
17649 /* Still increment function_depth so that we don't GC in the
17650 middle of an expression. */
17651 ++function_depth;
17652
17653 local_specialization_stack s (lss_copy);
17654
17655 tree body = start_lambda_function (fn, r);
17656
17657 register_parameter_specializations (oldfn, fn);
17658
17659 if (oldtmpl)
17660 {
17661 /* We might not partially instantiate some parts of the function, so
17662 copy these flags from the original template. */
17663 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
17664 current_function_returns_value = ol->returns_value;
17665 current_function_returns_null = ol->returns_null;
17666 current_function_returns_abnormally = ol->returns_abnormally;
17667 current_function_infinite_loop = ol->infinite_loop;
17668 }
17669
17670 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17671 /*constexpr*/false);
17672
17673 finish_lambda_function (body);
17674
17675 if (nested)
17676 pop_function_context ();
17677 else
17678 --function_depth;
17679
17680 /* The capture list was built up in reverse order; fix that now. */
17681 LAMBDA_EXPR_CAPTURE_LIST (r)
17682 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17683
17684 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17685
17686 maybe_add_lambda_conv_op (type);
17687 }
17688
17689 finish_struct (type, /*attr*/NULL_TREE);
17690
17691 insert_pending_capture_proxies ();
17692
17693 return r;
17694 }
17695
17696 /* Like tsubst but deals with expressions and performs semantic
17697 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17698
17699 tree
17700 tsubst_copy_and_build (tree t,
17701 tree args,
17702 tsubst_flags_t complain,
17703 tree in_decl,
17704 bool function_p,
17705 bool integral_constant_expression_p)
17706 {
17707 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17708 #define RECUR(NODE) \
17709 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17710 /*function_p=*/false, \
17711 integral_constant_expression_p)
17712
17713 tree retval, op1;
17714 location_t loc;
17715
17716 if (t == NULL_TREE || t == error_mark_node)
17717 return t;
17718
17719 loc = input_location;
17720 if (location_t eloc = cp_expr_location (t))
17721 input_location = eloc;
17722
17723 /* N3276 decltype magic only applies to calls at the top level or on the
17724 right side of a comma. */
17725 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17726 complain &= ~tf_decltype;
17727
17728 switch (TREE_CODE (t))
17729 {
17730 case USING_DECL:
17731 t = DECL_NAME (t);
17732 /* Fall through. */
17733 case IDENTIFIER_NODE:
17734 {
17735 tree decl;
17736 cp_id_kind idk;
17737 bool non_integral_constant_expression_p;
17738 const char *error_msg;
17739
17740 if (IDENTIFIER_CONV_OP_P (t))
17741 {
17742 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17743 t = make_conv_op_name (new_type);
17744 }
17745
17746 /* Look up the name. */
17747 decl = lookup_name (t);
17748
17749 /* By convention, expressions use ERROR_MARK_NODE to indicate
17750 failure, not NULL_TREE. */
17751 if (decl == NULL_TREE)
17752 decl = error_mark_node;
17753
17754 decl = finish_id_expression (t, decl, NULL_TREE,
17755 &idk,
17756 integral_constant_expression_p,
17757 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17758 &non_integral_constant_expression_p,
17759 /*template_p=*/false,
17760 /*done=*/true,
17761 /*address_p=*/false,
17762 /*template_arg_p=*/false,
17763 &error_msg,
17764 input_location);
17765 if (error_msg)
17766 error (error_msg);
17767 if (!function_p && identifier_p (decl))
17768 {
17769 if (complain & tf_error)
17770 unqualified_name_lookup_error (decl);
17771 decl = error_mark_node;
17772 }
17773 RETURN (decl);
17774 }
17775
17776 case TEMPLATE_ID_EXPR:
17777 {
17778 tree object;
17779 tree templ = RECUR (TREE_OPERAND (t, 0));
17780 tree targs = TREE_OPERAND (t, 1);
17781
17782 if (targs)
17783 targs = tsubst_template_args (targs, args, complain, in_decl);
17784 if (targs == error_mark_node)
17785 RETURN (error_mark_node);
17786
17787 if (TREE_CODE (templ) == SCOPE_REF)
17788 {
17789 tree name = TREE_OPERAND (templ, 1);
17790 tree tid = lookup_template_function (name, targs);
17791 TREE_OPERAND (templ, 1) = tid;
17792 RETURN (templ);
17793 }
17794
17795 if (variable_template_p (templ))
17796 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17797
17798 if (TREE_CODE (templ) == COMPONENT_REF)
17799 {
17800 object = TREE_OPERAND (templ, 0);
17801 templ = TREE_OPERAND (templ, 1);
17802 }
17803 else
17804 object = NULL_TREE;
17805 templ = lookup_template_function (templ, targs);
17806
17807 if (object)
17808 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17809 object, templ, NULL_TREE));
17810 else
17811 RETURN (baselink_for_fns (templ));
17812 }
17813
17814 case INDIRECT_REF:
17815 {
17816 tree r = RECUR (TREE_OPERAND (t, 0));
17817
17818 if (REFERENCE_REF_P (t))
17819 {
17820 /* A type conversion to reference type will be enclosed in
17821 such an indirect ref, but the substitution of the cast
17822 will have also added such an indirect ref. */
17823 r = convert_from_reference (r);
17824 }
17825 else
17826 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17827 complain|decltype_flag);
17828
17829 if (REF_PARENTHESIZED_P (t))
17830 r = force_paren_expr (r);
17831
17832 RETURN (r);
17833 }
17834
17835 case NOP_EXPR:
17836 {
17837 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17838 tree op0 = RECUR (TREE_OPERAND (t, 0));
17839 RETURN (build_nop (type, op0));
17840 }
17841
17842 case IMPLICIT_CONV_EXPR:
17843 {
17844 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17845 tree expr = RECUR (TREE_OPERAND (t, 0));
17846 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17847 {
17848 retval = copy_node (t);
17849 TREE_TYPE (retval) = type;
17850 TREE_OPERAND (retval, 0) = expr;
17851 RETURN (retval);
17852 }
17853 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17854 /* We'll pass this to convert_nontype_argument again, we don't need
17855 to actually perform any conversion here. */
17856 RETURN (expr);
17857 int flags = LOOKUP_IMPLICIT;
17858 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17859 flags = LOOKUP_NORMAL;
17860 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17861 flags));
17862 }
17863
17864 case CONVERT_EXPR:
17865 {
17866 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17867 tree op0 = RECUR (TREE_OPERAND (t, 0));
17868 if (op0 == error_mark_node)
17869 RETURN (error_mark_node);
17870 RETURN (build1 (CONVERT_EXPR, type, op0));
17871 }
17872
17873 case CAST_EXPR:
17874 case REINTERPRET_CAST_EXPR:
17875 case CONST_CAST_EXPR:
17876 case DYNAMIC_CAST_EXPR:
17877 case STATIC_CAST_EXPR:
17878 {
17879 tree type;
17880 tree op, r = NULL_TREE;
17881
17882 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17883 if (integral_constant_expression_p
17884 && !cast_valid_in_integral_constant_expression_p (type))
17885 {
17886 if (complain & tf_error)
17887 error ("a cast to a type other than an integral or "
17888 "enumeration type cannot appear in a constant-expression");
17889 RETURN (error_mark_node);
17890 }
17891
17892 op = RECUR (TREE_OPERAND (t, 0));
17893
17894 warning_sentinel s(warn_useless_cast);
17895 warning_sentinel s2(warn_ignored_qualifiers);
17896 switch (TREE_CODE (t))
17897 {
17898 case CAST_EXPR:
17899 r = build_functional_cast (type, op, complain);
17900 break;
17901 case REINTERPRET_CAST_EXPR:
17902 r = build_reinterpret_cast (type, op, complain);
17903 break;
17904 case CONST_CAST_EXPR:
17905 r = build_const_cast (type, op, complain);
17906 break;
17907 case DYNAMIC_CAST_EXPR:
17908 r = build_dynamic_cast (type, op, complain);
17909 break;
17910 case STATIC_CAST_EXPR:
17911 r = build_static_cast (type, op, complain);
17912 break;
17913 default:
17914 gcc_unreachable ();
17915 }
17916
17917 RETURN (r);
17918 }
17919
17920 case POSTDECREMENT_EXPR:
17921 case POSTINCREMENT_EXPR:
17922 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17923 args, complain, in_decl);
17924 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17925 complain|decltype_flag));
17926
17927 case PREDECREMENT_EXPR:
17928 case PREINCREMENT_EXPR:
17929 case NEGATE_EXPR:
17930 case BIT_NOT_EXPR:
17931 case ABS_EXPR:
17932 case TRUTH_NOT_EXPR:
17933 case UNARY_PLUS_EXPR: /* Unary + */
17934 case REALPART_EXPR:
17935 case IMAGPART_EXPR:
17936 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17937 RECUR (TREE_OPERAND (t, 0)),
17938 complain|decltype_flag));
17939
17940 case FIX_TRUNC_EXPR:
17941 gcc_unreachable ();
17942
17943 case ADDR_EXPR:
17944 op1 = TREE_OPERAND (t, 0);
17945 if (TREE_CODE (op1) == LABEL_DECL)
17946 RETURN (finish_label_address_expr (DECL_NAME (op1),
17947 EXPR_LOCATION (op1)));
17948 if (TREE_CODE (op1) == SCOPE_REF)
17949 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17950 /*done=*/true, /*address_p=*/true);
17951 else
17952 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17953 in_decl);
17954 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17955 complain|decltype_flag));
17956
17957 case PLUS_EXPR:
17958 case MINUS_EXPR:
17959 case MULT_EXPR:
17960 case TRUNC_DIV_EXPR:
17961 case CEIL_DIV_EXPR:
17962 case FLOOR_DIV_EXPR:
17963 case ROUND_DIV_EXPR:
17964 case EXACT_DIV_EXPR:
17965 case BIT_AND_EXPR:
17966 case BIT_IOR_EXPR:
17967 case BIT_XOR_EXPR:
17968 case TRUNC_MOD_EXPR:
17969 case FLOOR_MOD_EXPR:
17970 case TRUTH_ANDIF_EXPR:
17971 case TRUTH_ORIF_EXPR:
17972 case TRUTH_AND_EXPR:
17973 case TRUTH_OR_EXPR:
17974 case RSHIFT_EXPR:
17975 case LSHIFT_EXPR:
17976 case RROTATE_EXPR:
17977 case LROTATE_EXPR:
17978 case EQ_EXPR:
17979 case NE_EXPR:
17980 case MAX_EXPR:
17981 case MIN_EXPR:
17982 case LE_EXPR:
17983 case GE_EXPR:
17984 case LT_EXPR:
17985 case GT_EXPR:
17986 case MEMBER_REF:
17987 case DOTSTAR_EXPR:
17988 {
17989 warning_sentinel s1(warn_type_limits);
17990 warning_sentinel s2(warn_div_by_zero);
17991 warning_sentinel s3(warn_logical_op);
17992 warning_sentinel s4(warn_tautological_compare);
17993 tree op0 = RECUR (TREE_OPERAND (t, 0));
17994 tree op1 = RECUR (TREE_OPERAND (t, 1));
17995 tree r = build_x_binary_op
17996 (input_location, TREE_CODE (t),
17997 op0,
17998 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17999 ? ERROR_MARK
18000 : TREE_CODE (TREE_OPERAND (t, 0))),
18001 op1,
18002 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18003 ? ERROR_MARK
18004 : TREE_CODE (TREE_OPERAND (t, 1))),
18005 /*overload=*/NULL,
18006 complain|decltype_flag);
18007 if (EXPR_P (r) && TREE_NO_WARNING (t))
18008 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18009
18010 RETURN (r);
18011 }
18012
18013 case POINTER_PLUS_EXPR:
18014 {
18015 tree op0 = RECUR (TREE_OPERAND (t, 0));
18016 tree op1 = RECUR (TREE_OPERAND (t, 1));
18017 RETURN (fold_build_pointer_plus (op0, op1));
18018 }
18019
18020 case SCOPE_REF:
18021 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18022 /*address_p=*/false));
18023 case ARRAY_REF:
18024 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18025 args, complain, in_decl);
18026 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18027 RECUR (TREE_OPERAND (t, 1)),
18028 complain|decltype_flag));
18029
18030 case SIZEOF_EXPR:
18031 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18032 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18033 RETURN (tsubst_copy (t, args, complain, in_decl));
18034 /* Fall through */
18035
18036 case ALIGNOF_EXPR:
18037 {
18038 tree r;
18039
18040 op1 = TREE_OPERAND (t, 0);
18041 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18042 op1 = TREE_TYPE (op1);
18043 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18044 && ALIGNOF_EXPR_STD_P (t));
18045 if (!args)
18046 {
18047 /* When there are no ARGS, we are trying to evaluate a
18048 non-dependent expression from the parser. Trying to do
18049 the substitutions may not work. */
18050 if (!TYPE_P (op1))
18051 op1 = TREE_TYPE (op1);
18052 }
18053 else
18054 {
18055 ++cp_unevaluated_operand;
18056 ++c_inhibit_evaluation_warnings;
18057 if (TYPE_P (op1))
18058 op1 = tsubst (op1, args, complain, in_decl);
18059 else
18060 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18061 /*function_p=*/false,
18062 /*integral_constant_expression_p=*/
18063 false);
18064 --cp_unevaluated_operand;
18065 --c_inhibit_evaluation_warnings;
18066 }
18067 if (TYPE_P (op1))
18068 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18069 complain & tf_error);
18070 else
18071 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18072 complain & tf_error);
18073 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18074 {
18075 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18076 {
18077 if (!processing_template_decl && TYPE_P (op1))
18078 {
18079 r = build_min (SIZEOF_EXPR, size_type_node,
18080 build1 (NOP_EXPR, op1, error_mark_node));
18081 SIZEOF_EXPR_TYPE_P (r) = 1;
18082 }
18083 else
18084 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18085 TREE_SIDE_EFFECTS (r) = 0;
18086 TREE_READONLY (r) = 1;
18087 }
18088 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18089 }
18090 RETURN (r);
18091 }
18092
18093 case AT_ENCODE_EXPR:
18094 {
18095 op1 = TREE_OPERAND (t, 0);
18096 ++cp_unevaluated_operand;
18097 ++c_inhibit_evaluation_warnings;
18098 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18099 /*function_p=*/false,
18100 /*integral_constant_expression_p=*/false);
18101 --cp_unevaluated_operand;
18102 --c_inhibit_evaluation_warnings;
18103 RETURN (objc_build_encode_expr (op1));
18104 }
18105
18106 case NOEXCEPT_EXPR:
18107 op1 = TREE_OPERAND (t, 0);
18108 ++cp_unevaluated_operand;
18109 ++c_inhibit_evaluation_warnings;
18110 ++cp_noexcept_operand;
18111 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18112 /*function_p=*/false,
18113 /*integral_constant_expression_p=*/false);
18114 --cp_unevaluated_operand;
18115 --c_inhibit_evaluation_warnings;
18116 --cp_noexcept_operand;
18117 RETURN (finish_noexcept_expr (op1, complain));
18118
18119 case MODOP_EXPR:
18120 {
18121 warning_sentinel s(warn_div_by_zero);
18122 tree lhs = RECUR (TREE_OPERAND (t, 0));
18123 tree rhs = RECUR (TREE_OPERAND (t, 2));
18124 tree r = build_x_modify_expr
18125 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18126 complain|decltype_flag);
18127 /* TREE_NO_WARNING must be set if either the expression was
18128 parenthesized or it uses an operator such as >>= rather
18129 than plain assignment. In the former case, it was already
18130 set and must be copied. In the latter case,
18131 build_x_modify_expr sets it and it must not be reset
18132 here. */
18133 if (TREE_NO_WARNING (t))
18134 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18135
18136 RETURN (r);
18137 }
18138
18139 case ARROW_EXPR:
18140 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18141 args, complain, in_decl);
18142 /* Remember that there was a reference to this entity. */
18143 if (DECL_P (op1)
18144 && !mark_used (op1, complain) && !(complain & tf_error))
18145 RETURN (error_mark_node);
18146 RETURN (build_x_arrow (input_location, op1, complain));
18147
18148 case NEW_EXPR:
18149 {
18150 tree placement = RECUR (TREE_OPERAND (t, 0));
18151 tree init = RECUR (TREE_OPERAND (t, 3));
18152 vec<tree, va_gc> *placement_vec;
18153 vec<tree, va_gc> *init_vec;
18154 tree ret;
18155
18156 if (placement == NULL_TREE)
18157 placement_vec = NULL;
18158 else
18159 {
18160 placement_vec = make_tree_vector ();
18161 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18162 vec_safe_push (placement_vec, TREE_VALUE (placement));
18163 }
18164
18165 /* If there was an initializer in the original tree, but it
18166 instantiated to an empty list, then we should pass a
18167 non-NULL empty vector to tell build_new that it was an
18168 empty initializer() rather than no initializer. This can
18169 only happen when the initializer is a pack expansion whose
18170 parameter packs are of length zero. */
18171 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18172 init_vec = NULL;
18173 else
18174 {
18175 init_vec = make_tree_vector ();
18176 if (init == void_node)
18177 gcc_assert (init_vec != NULL);
18178 else
18179 {
18180 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18181 vec_safe_push (init_vec, TREE_VALUE (init));
18182 }
18183 }
18184
18185 /* Avoid passing an enclosing decl to valid_array_size_p. */
18186 in_decl = NULL_TREE;
18187
18188 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18189 tree op2 = RECUR (TREE_OPERAND (t, 2));
18190 ret = build_new (&placement_vec, op1, op2, &init_vec,
18191 NEW_EXPR_USE_GLOBAL (t),
18192 complain);
18193
18194 if (placement_vec != NULL)
18195 release_tree_vector (placement_vec);
18196 if (init_vec != NULL)
18197 release_tree_vector (init_vec);
18198
18199 RETURN (ret);
18200 }
18201
18202 case DELETE_EXPR:
18203 {
18204 tree op0 = RECUR (TREE_OPERAND (t, 0));
18205 tree op1 = RECUR (TREE_OPERAND (t, 1));
18206 RETURN (delete_sanity (op0, op1,
18207 DELETE_EXPR_USE_VEC (t),
18208 DELETE_EXPR_USE_GLOBAL (t),
18209 complain));
18210 }
18211
18212 case COMPOUND_EXPR:
18213 {
18214 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18215 complain & ~tf_decltype, in_decl,
18216 /*function_p=*/false,
18217 integral_constant_expression_p);
18218 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18219 op0,
18220 RECUR (TREE_OPERAND (t, 1)),
18221 complain|decltype_flag));
18222 }
18223
18224 case CALL_EXPR:
18225 {
18226 tree function;
18227 vec<tree, va_gc> *call_args;
18228 unsigned int nargs, i;
18229 bool qualified_p;
18230 bool koenig_p;
18231 tree ret;
18232
18233 function = CALL_EXPR_FN (t);
18234 /* Internal function with no arguments. */
18235 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18236 RETURN (t);
18237
18238 /* When we parsed the expression, we determined whether or
18239 not Koenig lookup should be performed. */
18240 koenig_p = KOENIG_LOOKUP_P (t);
18241 if (function == NULL_TREE)
18242 {
18243 koenig_p = false;
18244 qualified_p = false;
18245 }
18246 else if (TREE_CODE (function) == SCOPE_REF)
18247 {
18248 qualified_p = true;
18249 function = tsubst_qualified_id (function, args, complain, in_decl,
18250 /*done=*/false,
18251 /*address_p=*/false);
18252 }
18253 else if (koenig_p && identifier_p (function))
18254 {
18255 /* Do nothing; calling tsubst_copy_and_build on an identifier
18256 would incorrectly perform unqualified lookup again.
18257
18258 Note that we can also have an IDENTIFIER_NODE if the earlier
18259 unqualified lookup found a member function; in that case
18260 koenig_p will be false and we do want to do the lookup
18261 again to find the instantiated member function.
18262
18263 FIXME but doing that causes c++/15272, so we need to stop
18264 using IDENTIFIER_NODE in that situation. */
18265 qualified_p = false;
18266 }
18267 else
18268 {
18269 if (TREE_CODE (function) == COMPONENT_REF)
18270 {
18271 tree op = TREE_OPERAND (function, 1);
18272
18273 qualified_p = (TREE_CODE (op) == SCOPE_REF
18274 || (BASELINK_P (op)
18275 && BASELINK_QUALIFIED_P (op)));
18276 }
18277 else
18278 qualified_p = false;
18279
18280 if (TREE_CODE (function) == ADDR_EXPR
18281 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18282 /* Avoid error about taking the address of a constructor. */
18283 function = TREE_OPERAND (function, 0);
18284
18285 function = tsubst_copy_and_build (function, args, complain,
18286 in_decl,
18287 !qualified_p,
18288 integral_constant_expression_p);
18289
18290 if (BASELINK_P (function))
18291 qualified_p = true;
18292 }
18293
18294 nargs = call_expr_nargs (t);
18295 call_args = make_tree_vector ();
18296 for (i = 0; i < nargs; ++i)
18297 {
18298 tree arg = CALL_EXPR_ARG (t, i);
18299
18300 if (!PACK_EXPANSION_P (arg))
18301 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18302 else
18303 {
18304 /* Expand the pack expansion and push each entry onto
18305 CALL_ARGS. */
18306 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18307 if (TREE_CODE (arg) == TREE_VEC)
18308 {
18309 unsigned int len, j;
18310
18311 len = TREE_VEC_LENGTH (arg);
18312 for (j = 0; j < len; ++j)
18313 {
18314 tree value = TREE_VEC_ELT (arg, j);
18315 if (value != NULL_TREE)
18316 value = convert_from_reference (value);
18317 vec_safe_push (call_args, value);
18318 }
18319 }
18320 else
18321 {
18322 /* A partial substitution. Add one entry. */
18323 vec_safe_push (call_args, arg);
18324 }
18325 }
18326 }
18327
18328 /* We do not perform argument-dependent lookup if normal
18329 lookup finds a non-function, in accordance with the
18330 expected resolution of DR 218. */
18331 if (koenig_p
18332 && ((is_overloaded_fn (function)
18333 /* If lookup found a member function, the Koenig lookup is
18334 not appropriate, even if an unqualified-name was used
18335 to denote the function. */
18336 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18337 || identifier_p (function))
18338 /* Only do this when substitution turns a dependent call
18339 into a non-dependent call. */
18340 && type_dependent_expression_p_push (t)
18341 && !any_type_dependent_arguments_p (call_args))
18342 function = perform_koenig_lookup (function, call_args, tf_none);
18343
18344 if (function != NULL_TREE
18345 && identifier_p (function)
18346 && !any_type_dependent_arguments_p (call_args))
18347 {
18348 if (koenig_p && (complain & tf_warning_or_error))
18349 {
18350 /* For backwards compatibility and good diagnostics, try
18351 the unqualified lookup again if we aren't in SFINAE
18352 context. */
18353 tree unq = (tsubst_copy_and_build
18354 (function, args, complain, in_decl, true,
18355 integral_constant_expression_p));
18356 if (unq == error_mark_node)
18357 {
18358 release_tree_vector (call_args);
18359 RETURN (error_mark_node);
18360 }
18361
18362 if (unq != function)
18363 {
18364 /* In a lambda fn, we have to be careful to not
18365 introduce new this captures. Legacy code can't
18366 be using lambdas anyway, so it's ok to be
18367 stricter. */
18368 bool in_lambda = (current_class_type
18369 && LAMBDA_TYPE_P (current_class_type));
18370 char const *const msg
18371 = G_("%qD was not declared in this scope, "
18372 "and no declarations were found by "
18373 "argument-dependent lookup at the point "
18374 "of instantiation");
18375
18376 bool diag = true;
18377 if (in_lambda)
18378 error_at (cp_expr_loc_or_loc (t, input_location),
18379 msg, function);
18380 else
18381 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18382 msg, function);
18383 if (diag)
18384 {
18385 tree fn = unq;
18386
18387 if (INDIRECT_REF_P (fn))
18388 fn = TREE_OPERAND (fn, 0);
18389 if (is_overloaded_fn (fn))
18390 fn = get_first_fn (fn);
18391
18392 if (!DECL_P (fn))
18393 /* Can't say anything more. */;
18394 else if (DECL_CLASS_SCOPE_P (fn))
18395 {
18396 location_t loc = cp_expr_loc_or_loc (t,
18397 input_location);
18398 inform (loc,
18399 "declarations in dependent base %qT are "
18400 "not found by unqualified lookup",
18401 DECL_CLASS_CONTEXT (fn));
18402 if (current_class_ptr)
18403 inform (loc,
18404 "use %<this->%D%> instead", function);
18405 else
18406 inform (loc,
18407 "use %<%T::%D%> instead",
18408 current_class_name, function);
18409 }
18410 else
18411 inform (DECL_SOURCE_LOCATION (fn),
18412 "%qD declared here, later in the "
18413 "translation unit", fn);
18414 if (in_lambda)
18415 {
18416 release_tree_vector (call_args);
18417 RETURN (error_mark_node);
18418 }
18419 }
18420
18421 function = unq;
18422 }
18423 }
18424 if (identifier_p (function))
18425 {
18426 if (complain & tf_error)
18427 unqualified_name_lookup_error (function);
18428 release_tree_vector (call_args);
18429 RETURN (error_mark_node);
18430 }
18431 }
18432
18433 /* Remember that there was a reference to this entity. */
18434 if (function != NULL_TREE
18435 && DECL_P (function)
18436 && !mark_used (function, complain) && !(complain & tf_error))
18437 {
18438 release_tree_vector (call_args);
18439 RETURN (error_mark_node);
18440 }
18441
18442 /* Put back tf_decltype for the actual call. */
18443 complain |= decltype_flag;
18444
18445 if (function == NULL_TREE)
18446 switch (CALL_EXPR_IFN (t))
18447 {
18448 case IFN_LAUNDER:
18449 gcc_assert (nargs == 1);
18450 if (vec_safe_length (call_args) != 1)
18451 {
18452 error_at (cp_expr_loc_or_loc (t, input_location),
18453 "wrong number of arguments to "
18454 "%<__builtin_launder%>");
18455 ret = error_mark_node;
18456 }
18457 else
18458 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
18459 input_location),
18460 (*call_args)[0], complain);
18461 break;
18462
18463 default:
18464 /* Unsupported internal function with arguments. */
18465 gcc_unreachable ();
18466 }
18467 else if (TREE_CODE (function) == OFFSET_REF
18468 || TREE_CODE (function) == DOTSTAR_EXPR
18469 || TREE_CODE (function) == MEMBER_REF)
18470 ret = build_offset_ref_call_from_tree (function, &call_args,
18471 complain);
18472 else if (TREE_CODE (function) == COMPONENT_REF)
18473 {
18474 tree instance = TREE_OPERAND (function, 0);
18475 tree fn = TREE_OPERAND (function, 1);
18476
18477 if (processing_template_decl
18478 && (type_dependent_expression_p (instance)
18479 || (!BASELINK_P (fn)
18480 && TREE_CODE (fn) != FIELD_DECL)
18481 || type_dependent_expression_p (fn)
18482 || any_type_dependent_arguments_p (call_args)))
18483 ret = build_min_nt_call_vec (function, call_args);
18484 else if (!BASELINK_P (fn))
18485 ret = finish_call_expr (function, &call_args,
18486 /*disallow_virtual=*/false,
18487 /*koenig_p=*/false,
18488 complain);
18489 else
18490 ret = (build_new_method_call
18491 (instance, fn,
18492 &call_args, NULL_TREE,
18493 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18494 /*fn_p=*/NULL,
18495 complain));
18496 }
18497 else
18498 ret = finish_call_expr (function, &call_args,
18499 /*disallow_virtual=*/qualified_p,
18500 koenig_p,
18501 complain);
18502
18503 release_tree_vector (call_args);
18504
18505 if (ret != error_mark_node)
18506 {
18507 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18508 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18509 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18510 bool thk = CALL_FROM_THUNK_P (t);
18511 if (op || ord || rev || thk)
18512 {
18513 function = extract_call_expr (ret);
18514 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18515 CALL_EXPR_ORDERED_ARGS (function) = ord;
18516 CALL_EXPR_REVERSE_ARGS (function) = rev;
18517 if (thk)
18518 {
18519 if (TREE_CODE (function) == CALL_EXPR)
18520 CALL_FROM_THUNK_P (function) = true;
18521 else
18522 AGGR_INIT_FROM_THUNK_P (function) = true;
18523 /* The thunk location is not interesting. */
18524 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18525 }
18526 }
18527 }
18528
18529 RETURN (ret);
18530 }
18531
18532 case COND_EXPR:
18533 {
18534 tree cond = RECUR (TREE_OPERAND (t, 0));
18535 cond = mark_rvalue_use (cond);
18536 tree folded_cond = fold_non_dependent_expr (cond, complain);
18537 tree exp1, exp2;
18538
18539 if (TREE_CODE (folded_cond) == INTEGER_CST)
18540 {
18541 if (integer_zerop (folded_cond))
18542 {
18543 ++c_inhibit_evaluation_warnings;
18544 exp1 = RECUR (TREE_OPERAND (t, 1));
18545 --c_inhibit_evaluation_warnings;
18546 exp2 = RECUR (TREE_OPERAND (t, 2));
18547 }
18548 else
18549 {
18550 exp1 = RECUR (TREE_OPERAND (t, 1));
18551 ++c_inhibit_evaluation_warnings;
18552 exp2 = RECUR (TREE_OPERAND (t, 2));
18553 --c_inhibit_evaluation_warnings;
18554 }
18555 cond = folded_cond;
18556 }
18557 else
18558 {
18559 exp1 = RECUR (TREE_OPERAND (t, 1));
18560 exp2 = RECUR (TREE_OPERAND (t, 2));
18561 }
18562
18563 warning_sentinel s(warn_duplicated_branches);
18564 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18565 cond, exp1, exp2, complain));
18566 }
18567
18568 case PSEUDO_DTOR_EXPR:
18569 {
18570 tree op0 = RECUR (TREE_OPERAND (t, 0));
18571 tree op1 = RECUR (TREE_OPERAND (t, 1));
18572 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18573 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18574 input_location));
18575 }
18576
18577 case TREE_LIST:
18578 {
18579 tree purpose, value, chain;
18580
18581 if (t == void_list_node)
18582 RETURN (t);
18583
18584 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18585 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18586 {
18587 /* We have pack expansions, so expand those and
18588 create a new list out of it. */
18589 tree purposevec = NULL_TREE;
18590 tree valuevec = NULL_TREE;
18591 tree chain;
18592 int i, len = -1;
18593
18594 /* Expand the argument expressions. */
18595 if (TREE_PURPOSE (t))
18596 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18597 complain, in_decl);
18598 if (TREE_VALUE (t))
18599 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18600 complain, in_decl);
18601
18602 /* Build the rest of the list. */
18603 chain = TREE_CHAIN (t);
18604 if (chain && chain != void_type_node)
18605 chain = RECUR (chain);
18606
18607 /* Determine the number of arguments. */
18608 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18609 {
18610 len = TREE_VEC_LENGTH (purposevec);
18611 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18612 }
18613 else if (TREE_CODE (valuevec) == TREE_VEC)
18614 len = TREE_VEC_LENGTH (valuevec);
18615 else
18616 {
18617 /* Since we only performed a partial substitution into
18618 the argument pack, we only RETURN (a single list
18619 node. */
18620 if (purposevec == TREE_PURPOSE (t)
18621 && valuevec == TREE_VALUE (t)
18622 && chain == TREE_CHAIN (t))
18623 RETURN (t);
18624
18625 RETURN (tree_cons (purposevec, valuevec, chain));
18626 }
18627
18628 /* Convert the argument vectors into a TREE_LIST */
18629 i = len;
18630 while (i > 0)
18631 {
18632 /* Grab the Ith values. */
18633 i--;
18634 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18635 : NULL_TREE;
18636 value
18637 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18638 : NULL_TREE;
18639
18640 /* Build the list (backwards). */
18641 chain = tree_cons (purpose, value, chain);
18642 }
18643
18644 RETURN (chain);
18645 }
18646
18647 purpose = TREE_PURPOSE (t);
18648 if (purpose)
18649 purpose = RECUR (purpose);
18650 value = TREE_VALUE (t);
18651 if (value)
18652 value = RECUR (value);
18653 chain = TREE_CHAIN (t);
18654 if (chain && chain != void_type_node)
18655 chain = RECUR (chain);
18656 if (purpose == TREE_PURPOSE (t)
18657 && value == TREE_VALUE (t)
18658 && chain == TREE_CHAIN (t))
18659 RETURN (t);
18660 RETURN (tree_cons (purpose, value, chain));
18661 }
18662
18663 case COMPONENT_REF:
18664 {
18665 tree object;
18666 tree object_type;
18667 tree member;
18668 tree r;
18669
18670 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18671 args, complain, in_decl);
18672 /* Remember that there was a reference to this entity. */
18673 if (DECL_P (object)
18674 && !mark_used (object, complain) && !(complain & tf_error))
18675 RETURN (error_mark_node);
18676 object_type = TREE_TYPE (object);
18677
18678 member = TREE_OPERAND (t, 1);
18679 if (BASELINK_P (member))
18680 member = tsubst_baselink (member,
18681 non_reference (TREE_TYPE (object)),
18682 args, complain, in_decl);
18683 else
18684 member = tsubst_copy (member, args, complain, in_decl);
18685 if (member == error_mark_node)
18686 RETURN (error_mark_node);
18687
18688 if (TREE_CODE (member) == FIELD_DECL)
18689 {
18690 r = finish_non_static_data_member (member, object, NULL_TREE);
18691 if (TREE_CODE (r) == COMPONENT_REF)
18692 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18693 RETURN (r);
18694 }
18695 else if (type_dependent_expression_p (object))
18696 /* We can't do much here. */;
18697 else if (!CLASS_TYPE_P (object_type))
18698 {
18699 if (scalarish_type_p (object_type))
18700 {
18701 tree s = NULL_TREE;
18702 tree dtor = member;
18703
18704 if (TREE_CODE (dtor) == SCOPE_REF)
18705 {
18706 s = TREE_OPERAND (dtor, 0);
18707 dtor = TREE_OPERAND (dtor, 1);
18708 }
18709 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18710 {
18711 dtor = TREE_OPERAND (dtor, 0);
18712 if (TYPE_P (dtor))
18713 RETURN (finish_pseudo_destructor_expr
18714 (object, s, dtor, input_location));
18715 }
18716 }
18717 }
18718 else if (TREE_CODE (member) == SCOPE_REF
18719 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18720 {
18721 /* Lookup the template functions now that we know what the
18722 scope is. */
18723 tree scope = TREE_OPERAND (member, 0);
18724 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18725 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18726 member = lookup_qualified_name (scope, tmpl,
18727 /*is_type_p=*/false,
18728 /*complain=*/false);
18729 if (BASELINK_P (member))
18730 {
18731 BASELINK_FUNCTIONS (member)
18732 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18733 args);
18734 member = (adjust_result_of_qualified_name_lookup
18735 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18736 object_type));
18737 }
18738 else
18739 {
18740 qualified_name_lookup_error (scope, tmpl, member,
18741 input_location);
18742 RETURN (error_mark_node);
18743 }
18744 }
18745 else if (TREE_CODE (member) == SCOPE_REF
18746 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18747 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18748 {
18749 if (complain & tf_error)
18750 {
18751 if (TYPE_P (TREE_OPERAND (member, 0)))
18752 error ("%qT is not a class or namespace",
18753 TREE_OPERAND (member, 0));
18754 else
18755 error ("%qD is not a class or namespace",
18756 TREE_OPERAND (member, 0));
18757 }
18758 RETURN (error_mark_node);
18759 }
18760
18761 r = finish_class_member_access_expr (object, member,
18762 /*template_p=*/false,
18763 complain);
18764 if (TREE_CODE (r) == COMPONENT_REF)
18765 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18766 RETURN (r);
18767 }
18768
18769 case THROW_EXPR:
18770 RETURN (build_throw
18771 (RECUR (TREE_OPERAND (t, 0))));
18772
18773 case CONSTRUCTOR:
18774 {
18775 vec<constructor_elt, va_gc> *n;
18776 constructor_elt *ce;
18777 unsigned HOST_WIDE_INT idx;
18778 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18779 bool process_index_p;
18780 int newlen;
18781 bool need_copy_p = false;
18782 tree r;
18783
18784 if (type == error_mark_node)
18785 RETURN (error_mark_node);
18786
18787 /* We do not want to process the index of aggregate
18788 initializers as they are identifier nodes which will be
18789 looked up by digest_init. */
18790 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18791
18792 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18793 newlen = vec_safe_length (n);
18794 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18795 {
18796 if (ce->index && process_index_p
18797 /* An identifier index is looked up in the type
18798 being initialized, not the current scope. */
18799 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18800 ce->index = RECUR (ce->index);
18801
18802 if (PACK_EXPANSION_P (ce->value))
18803 {
18804 /* Substitute into the pack expansion. */
18805 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18806 in_decl);
18807
18808 if (ce->value == error_mark_node
18809 || PACK_EXPANSION_P (ce->value))
18810 ;
18811 else if (TREE_VEC_LENGTH (ce->value) == 1)
18812 /* Just move the argument into place. */
18813 ce->value = TREE_VEC_ELT (ce->value, 0);
18814 else
18815 {
18816 /* Update the length of the final CONSTRUCTOR
18817 arguments vector, and note that we will need to
18818 copy.*/
18819 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18820 need_copy_p = true;
18821 }
18822 }
18823 else
18824 ce->value = RECUR (ce->value);
18825 }
18826
18827 if (need_copy_p)
18828 {
18829 vec<constructor_elt, va_gc> *old_n = n;
18830
18831 vec_alloc (n, newlen);
18832 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18833 {
18834 if (TREE_CODE (ce->value) == TREE_VEC)
18835 {
18836 int i, len = TREE_VEC_LENGTH (ce->value);
18837 for (i = 0; i < len; ++i)
18838 CONSTRUCTOR_APPEND_ELT (n, 0,
18839 TREE_VEC_ELT (ce->value, i));
18840 }
18841 else
18842 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18843 }
18844 }
18845
18846 r = build_constructor (init_list_type_node, n);
18847 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18848
18849 if (TREE_HAS_CONSTRUCTOR (t))
18850 {
18851 fcl_t cl = fcl_functional;
18852 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18853 cl = fcl_c99;
18854 RETURN (finish_compound_literal (type, r, complain, cl));
18855 }
18856
18857 TREE_TYPE (r) = type;
18858 RETURN (r);
18859 }
18860
18861 case TYPEID_EXPR:
18862 {
18863 tree operand_0 = TREE_OPERAND (t, 0);
18864 if (TYPE_P (operand_0))
18865 {
18866 operand_0 = tsubst (operand_0, args, complain, in_decl);
18867 RETURN (get_typeid (operand_0, complain));
18868 }
18869 else
18870 {
18871 operand_0 = RECUR (operand_0);
18872 RETURN (build_typeid (operand_0, complain));
18873 }
18874 }
18875
18876 case VAR_DECL:
18877 if (!args)
18878 RETURN (t);
18879 /* Fall through */
18880
18881 case PARM_DECL:
18882 {
18883 tree r = tsubst_copy (t, args, complain, in_decl);
18884 /* ??? We're doing a subset of finish_id_expression here. */
18885 if (VAR_P (r)
18886 && !processing_template_decl
18887 && !cp_unevaluated_operand
18888 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18889 && CP_DECL_THREAD_LOCAL_P (r))
18890 {
18891 if (tree wrap = get_tls_wrapper_fn (r))
18892 /* Replace an evaluated use of the thread_local variable with
18893 a call to its wrapper. */
18894 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18895 }
18896 else if (outer_automatic_var_p (r))
18897 r = process_outer_var_ref (r, complain);
18898
18899 if (!TYPE_REF_P (TREE_TYPE (t)))
18900 /* If the original type was a reference, we'll be wrapped in
18901 the appropriate INDIRECT_REF. */
18902 r = convert_from_reference (r);
18903 RETURN (r);
18904 }
18905
18906 case VA_ARG_EXPR:
18907 {
18908 tree op0 = RECUR (TREE_OPERAND (t, 0));
18909 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18910 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18911 }
18912
18913 case OFFSETOF_EXPR:
18914 {
18915 tree object_ptr
18916 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18917 in_decl, /*function_p=*/false,
18918 /*integral_constant_expression_p=*/false);
18919 RETURN (finish_offsetof (object_ptr,
18920 RECUR (TREE_OPERAND (t, 0)),
18921 EXPR_LOCATION (t)));
18922 }
18923
18924 case ADDRESSOF_EXPR:
18925 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18926 RECUR (TREE_OPERAND (t, 0)), complain));
18927
18928 case TRAIT_EXPR:
18929 {
18930 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18931 complain, in_decl);
18932
18933 tree type2 = TRAIT_EXPR_TYPE2 (t);
18934 if (type2 && TREE_CODE (type2) == TREE_LIST)
18935 type2 = RECUR (type2);
18936 else if (type2)
18937 type2 = tsubst (type2, args, complain, in_decl);
18938
18939 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18940 }
18941
18942 case STMT_EXPR:
18943 {
18944 tree old_stmt_expr = cur_stmt_expr;
18945 tree stmt_expr = begin_stmt_expr ();
18946
18947 cur_stmt_expr = stmt_expr;
18948 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18949 integral_constant_expression_p);
18950 stmt_expr = finish_stmt_expr (stmt_expr, false);
18951 cur_stmt_expr = old_stmt_expr;
18952
18953 /* If the resulting list of expression statement is empty,
18954 fold it further into void_node. */
18955 if (empty_expr_stmt_p (stmt_expr))
18956 stmt_expr = void_node;
18957
18958 RETURN (stmt_expr);
18959 }
18960
18961 case LAMBDA_EXPR:
18962 {
18963 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18964
18965 RETURN (build_lambda_object (r));
18966 }
18967
18968 case TARGET_EXPR:
18969 /* We can get here for a constant initializer of non-dependent type.
18970 FIXME stop folding in cp_parser_initializer_clause. */
18971 {
18972 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18973 complain);
18974 RETURN (r);
18975 }
18976
18977 case TRANSACTION_EXPR:
18978 RETURN (tsubst_expr(t, args, complain, in_decl,
18979 integral_constant_expression_p));
18980
18981 case PAREN_EXPR:
18982 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18983
18984 case VEC_PERM_EXPR:
18985 {
18986 tree op0 = RECUR (TREE_OPERAND (t, 0));
18987 tree op1 = RECUR (TREE_OPERAND (t, 1));
18988 tree op2 = RECUR (TREE_OPERAND (t, 2));
18989 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18990 complain));
18991 }
18992
18993 case REQUIRES_EXPR:
18994 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18995
18996 case NON_LVALUE_EXPR:
18997 case VIEW_CONVERT_EXPR:
18998 /* We should only see these for location wrapper nodes, or within
18999 instantiate_non_dependent_expr (when args is NULL_TREE). */
19000 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
19001 if (location_wrapper_p (t))
19002 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19003 EXPR_LOCATION (t)));
19004 /* fallthrough. */
19005
19006 default:
19007 /* Handle Objective-C++ constructs, if appropriate. */
19008 {
19009 tree subst
19010 = objcp_tsubst_copy_and_build (t, args, complain,
19011 in_decl, /*function_p=*/false);
19012 if (subst)
19013 RETURN (subst);
19014 }
19015 RETURN (tsubst_copy (t, args, complain, in_decl));
19016 }
19017
19018 #undef RECUR
19019 #undef RETURN
19020 out:
19021 input_location = loc;
19022 return retval;
19023 }
19024
19025 /* Verify that the instantiated ARGS are valid. For type arguments,
19026 make sure that the type's linkage is ok. For non-type arguments,
19027 make sure they are constants if they are integral or enumerations.
19028 Emit an error under control of COMPLAIN, and return TRUE on error. */
19029
19030 static bool
19031 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19032 {
19033 if (dependent_template_arg_p (t))
19034 return false;
19035 if (ARGUMENT_PACK_P (t))
19036 {
19037 tree vec = ARGUMENT_PACK_ARGS (t);
19038 int len = TREE_VEC_LENGTH (vec);
19039 bool result = false;
19040 int i;
19041
19042 for (i = 0; i < len; ++i)
19043 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19044 result = true;
19045 return result;
19046 }
19047 else if (TYPE_P (t))
19048 {
19049 /* [basic.link]: A name with no linkage (notably, the name
19050 of a class or enumeration declared in a local scope)
19051 shall not be used to declare an entity with linkage.
19052 This implies that names with no linkage cannot be used as
19053 template arguments
19054
19055 DR 757 relaxes this restriction for C++0x. */
19056 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19057 : no_linkage_check (t, /*relaxed_p=*/false));
19058
19059 if (nt)
19060 {
19061 /* DR 488 makes use of a type with no linkage cause
19062 type deduction to fail. */
19063 if (complain & tf_error)
19064 {
19065 if (TYPE_UNNAMED_P (nt))
19066 error ("%qT is/uses unnamed type", t);
19067 else
19068 error ("template argument for %qD uses local type %qT",
19069 tmpl, t);
19070 }
19071 return true;
19072 }
19073 /* In order to avoid all sorts of complications, we do not
19074 allow variably-modified types as template arguments. */
19075 else if (variably_modified_type_p (t, NULL_TREE))
19076 {
19077 if (complain & tf_error)
19078 error ("%qT is a variably modified type", t);
19079 return true;
19080 }
19081 }
19082 /* Class template and alias template arguments should be OK. */
19083 else if (DECL_TYPE_TEMPLATE_P (t))
19084 ;
19085 /* A non-type argument of integral or enumerated type must be a
19086 constant. */
19087 else if (TREE_TYPE (t)
19088 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19089 && !REFERENCE_REF_P (t)
19090 && !TREE_CONSTANT (t))
19091 {
19092 if (complain & tf_error)
19093 error ("integral expression %qE is not constant", t);
19094 return true;
19095 }
19096 return false;
19097 }
19098
19099 static bool
19100 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19101 {
19102 int ix, len = DECL_NTPARMS (tmpl);
19103 bool result = false;
19104
19105 for (ix = 0; ix != len; ix++)
19106 {
19107 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19108 result = true;
19109 }
19110 if (result && (complain & tf_error))
19111 error (" trying to instantiate %qD", tmpl);
19112 return result;
19113 }
19114
19115 /* We're out of SFINAE context now, so generate diagnostics for the access
19116 errors we saw earlier when instantiating D from TMPL and ARGS. */
19117
19118 static void
19119 recheck_decl_substitution (tree d, tree tmpl, tree args)
19120 {
19121 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19122 tree type = TREE_TYPE (pattern);
19123 location_t loc = input_location;
19124
19125 push_access_scope (d);
19126 push_deferring_access_checks (dk_no_deferred);
19127 input_location = DECL_SOURCE_LOCATION (pattern);
19128 tsubst (type, args, tf_warning_or_error, d);
19129 input_location = loc;
19130 pop_deferring_access_checks ();
19131 pop_access_scope (d);
19132 }
19133
19134 /* Instantiate the indicated variable, function, or alias template TMPL with
19135 the template arguments in TARG_PTR. */
19136
19137 static tree
19138 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19139 {
19140 tree targ_ptr = orig_args;
19141 tree fndecl;
19142 tree gen_tmpl;
19143 tree spec;
19144 bool access_ok = true;
19145
19146 if (tmpl == error_mark_node)
19147 return error_mark_node;
19148
19149 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19150
19151 /* If this function is a clone, handle it specially. */
19152 if (DECL_CLONED_FUNCTION_P (tmpl))
19153 {
19154 tree spec;
19155 tree clone;
19156
19157 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19158 DECL_CLONED_FUNCTION. */
19159 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19160 targ_ptr, complain);
19161 if (spec == error_mark_node)
19162 return error_mark_node;
19163
19164 /* Look for the clone. */
19165 FOR_EACH_CLONE (clone, spec)
19166 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19167 return clone;
19168 /* We should always have found the clone by now. */
19169 gcc_unreachable ();
19170 return NULL_TREE;
19171 }
19172
19173 if (targ_ptr == error_mark_node)
19174 return error_mark_node;
19175
19176 /* Check to see if we already have this specialization. */
19177 gen_tmpl = most_general_template (tmpl);
19178 if (TMPL_ARGS_DEPTH (targ_ptr)
19179 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19180 /* targ_ptr only has the innermost template args, so add the outer ones
19181 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19182 the case of a non-dependent call within a template definition). */
19183 targ_ptr = (add_outermost_template_args
19184 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19185 targ_ptr));
19186
19187 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19188 but it doesn't seem to be on the hot path. */
19189 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19190
19191 gcc_assert (tmpl == gen_tmpl
19192 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19193 == spec)
19194 || fndecl == NULL_TREE);
19195
19196 if (spec != NULL_TREE)
19197 {
19198 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19199 {
19200 if (complain & tf_error)
19201 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19202 return error_mark_node;
19203 }
19204 return spec;
19205 }
19206
19207 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19208 complain))
19209 return error_mark_node;
19210
19211 /* We are building a FUNCTION_DECL, during which the access of its
19212 parameters and return types have to be checked. However this
19213 FUNCTION_DECL which is the desired context for access checking
19214 is not built yet. We solve this chicken-and-egg problem by
19215 deferring all checks until we have the FUNCTION_DECL. */
19216 push_deferring_access_checks (dk_deferred);
19217
19218 /* Instantiation of the function happens in the context of the function
19219 template, not the context of the overload resolution we're doing. */
19220 push_to_top_level ();
19221 /* If there are dependent arguments, e.g. because we're doing partial
19222 ordering, make sure processing_template_decl stays set. */
19223 if (uses_template_parms (targ_ptr))
19224 ++processing_template_decl;
19225 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19226 {
19227 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19228 complain, gen_tmpl, true);
19229 push_nested_class (ctx);
19230 }
19231
19232 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19233
19234 fndecl = NULL_TREE;
19235 if (VAR_P (pattern))
19236 {
19237 /* We need to determine if we're using a partial or explicit
19238 specialization now, because the type of the variable could be
19239 different. */
19240 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19241 tree elt = most_specialized_partial_spec (tid, complain);
19242 if (elt == error_mark_node)
19243 pattern = error_mark_node;
19244 else if (elt)
19245 {
19246 tree partial_tmpl = TREE_VALUE (elt);
19247 tree partial_args = TREE_PURPOSE (elt);
19248 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19249 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19250 }
19251 }
19252
19253 /* Substitute template parameters to obtain the specialization. */
19254 if (fndecl == NULL_TREE)
19255 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19256 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19257 pop_nested_class ();
19258 pop_from_top_level ();
19259
19260 if (fndecl == error_mark_node)
19261 {
19262 pop_deferring_access_checks ();
19263 return error_mark_node;
19264 }
19265
19266 /* The DECL_TI_TEMPLATE should always be the immediate parent
19267 template, not the most general template. */
19268 DECL_TI_TEMPLATE (fndecl) = tmpl;
19269 DECL_TI_ARGS (fndecl) = targ_ptr;
19270
19271 /* Now we know the specialization, compute access previously
19272 deferred. Do no access control for inheriting constructors,
19273 as we already checked access for the inherited constructor. */
19274 if (!(flag_new_inheriting_ctors
19275 && DECL_INHERITED_CTOR (fndecl)))
19276 {
19277 push_access_scope (fndecl);
19278 if (!perform_deferred_access_checks (complain))
19279 access_ok = false;
19280 pop_access_scope (fndecl);
19281 }
19282 pop_deferring_access_checks ();
19283
19284 /* If we've just instantiated the main entry point for a function,
19285 instantiate all the alternate entry points as well. We do this
19286 by cloning the instantiation of the main entry point, not by
19287 instantiating the template clones. */
19288 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19289 clone_function_decl (fndecl, /*update_methods=*/false);
19290
19291 if (!access_ok)
19292 {
19293 if (!(complain & tf_error))
19294 {
19295 /* Remember to reinstantiate when we're out of SFINAE so the user
19296 can see the errors. */
19297 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19298 }
19299 return error_mark_node;
19300 }
19301 return fndecl;
19302 }
19303
19304 /* Wrapper for instantiate_template_1. */
19305
19306 tree
19307 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19308 {
19309 tree ret;
19310 timevar_push (TV_TEMPLATE_INST);
19311 ret = instantiate_template_1 (tmpl, orig_args, complain);
19312 timevar_pop (TV_TEMPLATE_INST);
19313 return ret;
19314 }
19315
19316 /* Instantiate the alias template TMPL with ARGS. Also push a template
19317 instantiation level, which instantiate_template doesn't do because
19318 functions and variables have sufficient context established by the
19319 callers. */
19320
19321 static tree
19322 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19323 {
19324 if (tmpl == error_mark_node || args == error_mark_node)
19325 return error_mark_node;
19326 if (!push_tinst_level (tmpl, args))
19327 return error_mark_node;
19328
19329 args =
19330 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19331 args, tmpl, complain,
19332 /*require_all_args=*/true,
19333 /*use_default_args=*/true);
19334
19335 tree r = instantiate_template (tmpl, args, complain);
19336 pop_tinst_level ();
19337
19338 return r;
19339 }
19340
19341 /* PARM is a template parameter pack for FN. Returns true iff
19342 PARM is used in a deducible way in the argument list of FN. */
19343
19344 static bool
19345 pack_deducible_p (tree parm, tree fn)
19346 {
19347 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19348 for (; t; t = TREE_CHAIN (t))
19349 {
19350 tree type = TREE_VALUE (t);
19351 tree packs;
19352 if (!PACK_EXPANSION_P (type))
19353 continue;
19354 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19355 packs; packs = TREE_CHAIN (packs))
19356 if (template_args_equal (TREE_VALUE (packs), parm))
19357 {
19358 /* The template parameter pack is used in a function parameter
19359 pack. If this is the end of the parameter list, the
19360 template parameter pack is deducible. */
19361 if (TREE_CHAIN (t) == void_list_node)
19362 return true;
19363 else
19364 /* Otherwise, not. Well, it could be deduced from
19365 a non-pack parameter, but doing so would end up with
19366 a deduction mismatch, so don't bother. */
19367 return false;
19368 }
19369 }
19370 /* The template parameter pack isn't used in any function parameter
19371 packs, but it might be used deeper, e.g. tuple<Args...>. */
19372 return true;
19373 }
19374
19375 /* Subroutine of fn_type_unification: check non-dependent parms for
19376 convertibility. */
19377
19378 static int
19379 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19380 tree fn, unification_kind_t strict, int flags,
19381 struct conversion **convs, bool explain_p)
19382 {
19383 /* Non-constructor methods need to leave a conversion for 'this', which
19384 isn't included in nargs here. */
19385 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19386 && !DECL_CONSTRUCTOR_P (fn));
19387
19388 for (unsigned ia = 0;
19389 parms && parms != void_list_node && ia < nargs; )
19390 {
19391 tree parm = TREE_VALUE (parms);
19392
19393 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19394 && (!TREE_CHAIN (parms)
19395 || TREE_CHAIN (parms) == void_list_node))
19396 /* For a function parameter pack that occurs at the end of the
19397 parameter-declaration-list, the type A of each remaining
19398 argument of the call is compared with the type P of the
19399 declarator-id of the function parameter pack. */
19400 break;
19401
19402 parms = TREE_CHAIN (parms);
19403
19404 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19405 /* For a function parameter pack that does not occur at the
19406 end of the parameter-declaration-list, the type of the
19407 parameter pack is a non-deduced context. */
19408 continue;
19409
19410 if (!uses_template_parms (parm))
19411 {
19412 tree arg = args[ia];
19413 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19414 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19415
19416 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19417 conv_p, explain_p))
19418 return 1;
19419 }
19420
19421 ++ia;
19422 }
19423
19424 return 0;
19425 }
19426
19427 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19428 NARGS elements of the arguments that are being used when calling
19429 it. TARGS is a vector into which the deduced template arguments
19430 are placed.
19431
19432 Returns either a FUNCTION_DECL for the matching specialization of FN or
19433 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19434 true, diagnostics will be printed to explain why it failed.
19435
19436 If FN is a conversion operator, or we are trying to produce a specific
19437 specialization, RETURN_TYPE is the return type desired.
19438
19439 The EXPLICIT_TARGS are explicit template arguments provided via a
19440 template-id.
19441
19442 The parameter STRICT is one of:
19443
19444 DEDUCE_CALL:
19445 We are deducing arguments for a function call, as in
19446 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19447 deducing arguments for a call to the result of a conversion
19448 function template, as in [over.call.object].
19449
19450 DEDUCE_CONV:
19451 We are deducing arguments for a conversion function, as in
19452 [temp.deduct.conv].
19453
19454 DEDUCE_EXACT:
19455 We are deducing arguments when doing an explicit instantiation
19456 as in [temp.explicit], when determining an explicit specialization
19457 as in [temp.expl.spec], or when taking the address of a function
19458 template, as in [temp.deduct.funcaddr]. */
19459
19460 tree
19461 fn_type_unification (tree fn,
19462 tree explicit_targs,
19463 tree targs,
19464 const tree *args,
19465 unsigned int nargs,
19466 tree return_type,
19467 unification_kind_t strict,
19468 int flags,
19469 struct conversion **convs,
19470 bool explain_p,
19471 bool decltype_p)
19472 {
19473 tree parms;
19474 tree fntype;
19475 tree decl = NULL_TREE;
19476 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19477 bool ok;
19478 static int deduction_depth;
19479
19480 tree orig_fn = fn;
19481 if (flag_new_inheriting_ctors)
19482 fn = strip_inheriting_ctors (fn);
19483
19484 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19485 tree r = error_mark_node;
19486
19487 tree full_targs = targs;
19488 if (TMPL_ARGS_DEPTH (targs)
19489 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19490 full_targs = (add_outermost_template_args
19491 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19492 targs));
19493
19494 if (decltype_p)
19495 complain |= tf_decltype;
19496
19497 /* In C++0x, it's possible to have a function template whose type depends
19498 on itself recursively. This is most obvious with decltype, but can also
19499 occur with enumeration scope (c++/48969). So we need to catch infinite
19500 recursion and reject the substitution at deduction time; this function
19501 will return error_mark_node for any repeated substitution.
19502
19503 This also catches excessive recursion such as when f<N> depends on
19504 f<N-1> across all integers, and returns error_mark_node for all the
19505 substitutions back up to the initial one.
19506
19507 This is, of course, not reentrant. */
19508 if (excessive_deduction_depth)
19509 return error_mark_node;
19510 ++deduction_depth;
19511
19512 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19513
19514 fntype = TREE_TYPE (fn);
19515 if (explicit_targs)
19516 {
19517 /* [temp.deduct]
19518
19519 The specified template arguments must match the template
19520 parameters in kind (i.e., type, nontype, template), and there
19521 must not be more arguments than there are parameters;
19522 otherwise type deduction fails.
19523
19524 Nontype arguments must match the types of the corresponding
19525 nontype template parameters, or must be convertible to the
19526 types of the corresponding nontype parameters as specified in
19527 _temp.arg.nontype_, otherwise type deduction fails.
19528
19529 All references in the function type of the function template
19530 to the corresponding template parameters are replaced by the
19531 specified template argument values. If a substitution in a
19532 template parameter or in the function type of the function
19533 template results in an invalid type, type deduction fails. */
19534 int i, len = TREE_VEC_LENGTH (tparms);
19535 location_t loc = input_location;
19536 bool incomplete = false;
19537
19538 if (explicit_targs == error_mark_node)
19539 goto fail;
19540
19541 if (TMPL_ARGS_DEPTH (explicit_targs)
19542 < TMPL_ARGS_DEPTH (full_targs))
19543 explicit_targs = add_outermost_template_args (full_targs,
19544 explicit_targs);
19545
19546 /* Adjust any explicit template arguments before entering the
19547 substitution context. */
19548 explicit_targs
19549 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19550 complain,
19551 /*require_all_args=*/false,
19552 /*use_default_args=*/false));
19553 if (explicit_targs == error_mark_node)
19554 goto fail;
19555
19556 /* Substitute the explicit args into the function type. This is
19557 necessary so that, for instance, explicitly declared function
19558 arguments can match null pointed constants. If we were given
19559 an incomplete set of explicit args, we must not do semantic
19560 processing during substitution as we could create partial
19561 instantiations. */
19562 for (i = 0; i < len; i++)
19563 {
19564 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19565 bool parameter_pack = false;
19566 tree targ = TREE_VEC_ELT (explicit_targs, i);
19567
19568 /* Dig out the actual parm. */
19569 if (TREE_CODE (parm) == TYPE_DECL
19570 || TREE_CODE (parm) == TEMPLATE_DECL)
19571 {
19572 parm = TREE_TYPE (parm);
19573 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19574 }
19575 else if (TREE_CODE (parm) == PARM_DECL)
19576 {
19577 parm = DECL_INITIAL (parm);
19578 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19579 }
19580
19581 if (!parameter_pack && targ == NULL_TREE)
19582 /* No explicit argument for this template parameter. */
19583 incomplete = true;
19584
19585 if (parameter_pack && pack_deducible_p (parm, fn))
19586 {
19587 /* Mark the argument pack as "incomplete". We could
19588 still deduce more arguments during unification.
19589 We remove this mark in type_unification_real. */
19590 if (targ)
19591 {
19592 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19593 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19594 = ARGUMENT_PACK_ARGS (targ);
19595 }
19596
19597 /* We have some incomplete argument packs. */
19598 incomplete = true;
19599 }
19600 }
19601
19602 if (!push_tinst_level (fn, explicit_targs))
19603 {
19604 excessive_deduction_depth = true;
19605 goto fail;
19606 }
19607 processing_template_decl += incomplete;
19608 input_location = DECL_SOURCE_LOCATION (fn);
19609 /* Ignore any access checks; we'll see them again in
19610 instantiate_template and they might have the wrong
19611 access path at this point. */
19612 push_deferring_access_checks (dk_deferred);
19613 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19614 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19615 pop_deferring_access_checks ();
19616 input_location = loc;
19617 processing_template_decl -= incomplete;
19618 pop_tinst_level ();
19619
19620 if (fntype == error_mark_node)
19621 goto fail;
19622
19623 /* Place the explicitly specified arguments in TARGS. */
19624 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19625 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19626 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19627 }
19628
19629 /* Never do unification on the 'this' parameter. */
19630 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19631
19632 if (return_type && strict == DEDUCE_CALL)
19633 {
19634 /* We're deducing for a call to the result of a template conversion
19635 function. The parms we really want are in return_type. */
19636 if (INDIRECT_TYPE_P (return_type))
19637 return_type = TREE_TYPE (return_type);
19638 parms = TYPE_ARG_TYPES (return_type);
19639 }
19640 else if (return_type)
19641 {
19642 tree *new_args;
19643
19644 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19645 new_args = XALLOCAVEC (tree, nargs + 1);
19646 new_args[0] = return_type;
19647 memcpy (new_args + 1, args, nargs * sizeof (tree));
19648 args = new_args;
19649 ++nargs;
19650 }
19651
19652 /* We allow incomplete unification without an error message here
19653 because the standard doesn't seem to explicitly prohibit it. Our
19654 callers must be ready to deal with unification failures in any
19655 event. */
19656
19657 /* If we aren't explaining yet, push tinst context so we can see where
19658 any errors (e.g. from class instantiations triggered by instantiation
19659 of default template arguments) come from. If we are explaining, this
19660 context is redundant. */
19661 if (!explain_p && !push_tinst_level (fn, targs))
19662 {
19663 excessive_deduction_depth = true;
19664 goto fail;
19665 }
19666
19667 /* type_unification_real will pass back any access checks from default
19668 template argument substitution. */
19669 vec<deferred_access_check, va_gc> *checks;
19670 checks = NULL;
19671
19672 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19673 full_targs, parms, args, nargs, /*subr=*/0,
19674 strict, &checks, explain_p);
19675 if (!explain_p)
19676 pop_tinst_level ();
19677 if (!ok)
19678 goto fail;
19679
19680 /* Now that we have bindings for all of the template arguments,
19681 ensure that the arguments deduced for the template template
19682 parameters have compatible template parameter lists. We cannot
19683 check this property before we have deduced all template
19684 arguments, because the template parameter types of a template
19685 template parameter might depend on prior template parameters
19686 deduced after the template template parameter. The following
19687 ill-formed example illustrates this issue:
19688
19689 template<typename T, template<T> class C> void f(C<5>, T);
19690
19691 template<int N> struct X {};
19692
19693 void g() {
19694 f(X<5>(), 5l); // error: template argument deduction fails
19695 }
19696
19697 The template parameter list of 'C' depends on the template type
19698 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19699 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19700 time that we deduce 'C'. */
19701 if (!template_template_parm_bindings_ok_p
19702 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19703 {
19704 unify_inconsistent_template_template_parameters (explain_p);
19705 goto fail;
19706 }
19707
19708 /* DR 1391: All parameters have args, now check non-dependent parms for
19709 convertibility. */
19710 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
19711 convs, explain_p))
19712 goto fail;
19713
19714 /* All is well so far. Now, check:
19715
19716 [temp.deduct]
19717
19718 When all template arguments have been deduced, all uses of
19719 template parameters in nondeduced contexts are replaced with
19720 the corresponding deduced argument values. If the
19721 substitution results in an invalid type, as described above,
19722 type deduction fails. */
19723 if (!push_tinst_level (fn, targs))
19724 {
19725 excessive_deduction_depth = true;
19726 goto fail;
19727 }
19728
19729 /* Also collect access checks from the instantiation. */
19730 reopen_deferring_access_checks (checks);
19731
19732 decl = instantiate_template (fn, targs, complain);
19733
19734 checks = get_deferred_access_checks ();
19735 pop_deferring_access_checks ();
19736
19737 pop_tinst_level ();
19738
19739 if (decl == error_mark_node)
19740 goto fail;
19741
19742 /* Now perform any access checks encountered during substitution. */
19743 push_access_scope (decl);
19744 ok = perform_access_checks (checks, complain);
19745 pop_access_scope (decl);
19746 if (!ok)
19747 goto fail;
19748
19749 /* If we're looking for an exact match, check that what we got
19750 is indeed an exact match. It might not be if some template
19751 parameters are used in non-deduced contexts. But don't check
19752 for an exact match if we have dependent template arguments;
19753 in that case we're doing partial ordering, and we already know
19754 that we have two candidates that will provide the actual type. */
19755 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19756 {
19757 tree substed = TREE_TYPE (decl);
19758 unsigned int i;
19759
19760 tree sarg
19761 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19762 if (return_type)
19763 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19764 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19765 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19766 {
19767 unify_type_mismatch (explain_p, args[i],
19768 TREE_VALUE (sarg));
19769 goto fail;
19770 }
19771 }
19772
19773 /* After doing deduction with the inherited constructor, actually return an
19774 instantiation of the inheriting constructor. */
19775 if (orig_fn != fn)
19776 decl = instantiate_template (orig_fn, targs, complain);
19777
19778 r = decl;
19779
19780 fail:
19781 --deduction_depth;
19782 if (excessive_deduction_depth)
19783 {
19784 if (deduction_depth == 0)
19785 /* Reset once we're all the way out. */
19786 excessive_deduction_depth = false;
19787 }
19788
19789 return r;
19790 }
19791
19792 /* Adjust types before performing type deduction, as described in
19793 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19794 sections are symmetric. PARM is the type of a function parameter
19795 or the return type of the conversion function. ARG is the type of
19796 the argument passed to the call, or the type of the value
19797 initialized with the result of the conversion function.
19798 ARG_EXPR is the original argument expression, which may be null. */
19799
19800 static int
19801 maybe_adjust_types_for_deduction (unification_kind_t strict,
19802 tree* parm,
19803 tree* arg,
19804 tree arg_expr)
19805 {
19806 int result = 0;
19807
19808 switch (strict)
19809 {
19810 case DEDUCE_CALL:
19811 break;
19812
19813 case DEDUCE_CONV:
19814 /* Swap PARM and ARG throughout the remainder of this
19815 function; the handling is precisely symmetric since PARM
19816 will initialize ARG rather than vice versa. */
19817 std::swap (parm, arg);
19818 break;
19819
19820 case DEDUCE_EXACT:
19821 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19822 too, but here handle it by stripping the reference from PARM
19823 rather than by adding it to ARG. */
19824 if (TYPE_REF_P (*parm)
19825 && TYPE_REF_IS_RVALUE (*parm)
19826 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19827 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19828 && TYPE_REF_P (*arg)
19829 && !TYPE_REF_IS_RVALUE (*arg))
19830 *parm = TREE_TYPE (*parm);
19831 /* Nothing else to do in this case. */
19832 return 0;
19833
19834 default:
19835 gcc_unreachable ();
19836 }
19837
19838 if (!TYPE_REF_P (*parm))
19839 {
19840 /* [temp.deduct.call]
19841
19842 If P is not a reference type:
19843
19844 --If A is an array type, the pointer type produced by the
19845 array-to-pointer standard conversion (_conv.array_) is
19846 used in place of A for type deduction; otherwise,
19847
19848 --If A is a function type, the pointer type produced by
19849 the function-to-pointer standard conversion
19850 (_conv.func_) is used in place of A for type deduction;
19851 otherwise,
19852
19853 --If A is a cv-qualified type, the top level
19854 cv-qualifiers of A's type are ignored for type
19855 deduction. */
19856 if (TREE_CODE (*arg) == ARRAY_TYPE)
19857 *arg = build_pointer_type (TREE_TYPE (*arg));
19858 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19859 *arg = build_pointer_type (*arg);
19860 else
19861 *arg = TYPE_MAIN_VARIANT (*arg);
19862 }
19863
19864 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19865 reference to a cv-unqualified template parameter that does not represent a
19866 template parameter of a class template (during class template argument
19867 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19868 an lvalue, the type "lvalue reference to A" is used in place of A for type
19869 deduction. */
19870 if (TYPE_REF_P (*parm)
19871 && TYPE_REF_IS_RVALUE (*parm)
19872 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19873 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19874 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19875 && (arg_expr ? lvalue_p (arg_expr)
19876 /* try_one_overload doesn't provide an arg_expr, but
19877 functions are always lvalues. */
19878 : TREE_CODE (*arg) == FUNCTION_TYPE))
19879 *arg = build_reference_type (*arg);
19880
19881 /* [temp.deduct.call]
19882
19883 If P is a cv-qualified type, the top level cv-qualifiers
19884 of P's type are ignored for type deduction. If P is a
19885 reference type, the type referred to by P is used for
19886 type deduction. */
19887 *parm = TYPE_MAIN_VARIANT (*parm);
19888 if (TYPE_REF_P (*parm))
19889 {
19890 *parm = TREE_TYPE (*parm);
19891 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19892 }
19893
19894 /* DR 322. For conversion deduction, remove a reference type on parm
19895 too (which has been swapped into ARG). */
19896 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
19897 *arg = TREE_TYPE (*arg);
19898
19899 return result;
19900 }
19901
19902 /* Subroutine of fn_type_unification. PARM is a function parameter of a
19903 template which doesn't contain any deducible template parameters; check if
19904 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19905 unify_one_argument. */
19906
19907 static int
19908 check_non_deducible_conversion (tree parm, tree arg, int strict,
19909 int flags, struct conversion **conv_p,
19910 bool explain_p)
19911 {
19912 tree type;
19913
19914 if (!TYPE_P (arg))
19915 type = TREE_TYPE (arg);
19916 else
19917 type = arg;
19918
19919 if (same_type_p (parm, type))
19920 return unify_success (explain_p);
19921
19922 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19923 if (strict == DEDUCE_CONV)
19924 {
19925 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
19926 return unify_success (explain_p);
19927 }
19928 else if (strict != DEDUCE_EXACT)
19929 {
19930 bool ok = false;
19931 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
19932 if (conv_p)
19933 /* Avoid recalculating this in add_function_candidate. */
19934 ok = (*conv_p
19935 = good_conversion (parm, type, conv_arg, flags, complain));
19936 else
19937 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
19938 if (ok)
19939 return unify_success (explain_p);
19940 }
19941
19942 if (strict == DEDUCE_EXACT)
19943 return unify_type_mismatch (explain_p, parm, arg);
19944 else
19945 return unify_arg_conversion (explain_p, parm, type, arg);
19946 }
19947
19948 static bool uses_deducible_template_parms (tree type);
19949
19950 /* Returns true iff the expression EXPR is one from which a template
19951 argument can be deduced. In other words, if it's an undecorated
19952 use of a template non-type parameter. */
19953
19954 static bool
19955 deducible_expression (tree expr)
19956 {
19957 /* Strip implicit conversions. */
19958 while (CONVERT_EXPR_P (expr))
19959 expr = TREE_OPERAND (expr, 0);
19960 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19961 }
19962
19963 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19964 deducible way; that is, if it has a max value of <PARM> - 1. */
19965
19966 static bool
19967 deducible_array_bound (tree domain)
19968 {
19969 if (domain == NULL_TREE)
19970 return false;
19971
19972 tree max = TYPE_MAX_VALUE (domain);
19973 if (TREE_CODE (max) != MINUS_EXPR)
19974 return false;
19975
19976 return deducible_expression (TREE_OPERAND (max, 0));
19977 }
19978
19979 /* Returns true iff the template arguments ARGS use a template parameter
19980 in a deducible way. */
19981
19982 static bool
19983 deducible_template_args (tree args)
19984 {
19985 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19986 {
19987 bool deducible;
19988 tree elt = TREE_VEC_ELT (args, i);
19989 if (ARGUMENT_PACK_P (elt))
19990 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19991 else
19992 {
19993 if (PACK_EXPANSION_P (elt))
19994 elt = PACK_EXPANSION_PATTERN (elt);
19995 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19996 deducible = true;
19997 else if (TYPE_P (elt))
19998 deducible = uses_deducible_template_parms (elt);
19999 else
20000 deducible = deducible_expression (elt);
20001 }
20002 if (deducible)
20003 return true;
20004 }
20005 return false;
20006 }
20007
20008 /* Returns true iff TYPE contains any deducible references to template
20009 parameters, as per 14.8.2.5. */
20010
20011 static bool
20012 uses_deducible_template_parms (tree type)
20013 {
20014 if (PACK_EXPANSION_P (type))
20015 type = PACK_EXPANSION_PATTERN (type);
20016
20017 /* T
20018 cv-list T
20019 TT<T>
20020 TT<i>
20021 TT<> */
20022 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20023 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20024 return true;
20025
20026 /* T*
20027 T&
20028 T&& */
20029 if (INDIRECT_TYPE_P (type))
20030 return uses_deducible_template_parms (TREE_TYPE (type));
20031
20032 /* T[integer-constant ]
20033 type [i] */
20034 if (TREE_CODE (type) == ARRAY_TYPE)
20035 return (uses_deducible_template_parms (TREE_TYPE (type))
20036 || deducible_array_bound (TYPE_DOMAIN (type)));
20037
20038 /* T type ::*
20039 type T::*
20040 T T::*
20041 T (type ::*)()
20042 type (T::*)()
20043 type (type ::*)(T)
20044 type (T::*)(T)
20045 T (type ::*)(T)
20046 T (T::*)()
20047 T (T::*)(T) */
20048 if (TYPE_PTRMEM_P (type))
20049 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20050 || (uses_deducible_template_parms
20051 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20052
20053 /* template-name <T> (where template-name refers to a class template)
20054 template-name <i> (where template-name refers to a class template) */
20055 if (CLASS_TYPE_P (type)
20056 && CLASSTYPE_TEMPLATE_INFO (type)
20057 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20058 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20059 (CLASSTYPE_TI_ARGS (type)));
20060
20061 /* type (T)
20062 T()
20063 T(T) */
20064 if (TREE_CODE (type) == FUNCTION_TYPE
20065 || TREE_CODE (type) == METHOD_TYPE)
20066 {
20067 if (uses_deducible_template_parms (TREE_TYPE (type)))
20068 return true;
20069 tree parm = TYPE_ARG_TYPES (type);
20070 if (TREE_CODE (type) == METHOD_TYPE)
20071 parm = TREE_CHAIN (parm);
20072 for (; parm; parm = TREE_CHAIN (parm))
20073 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20074 return true;
20075 }
20076
20077 return false;
20078 }
20079
20080 /* Subroutine of type_unification_real and unify_pack_expansion to
20081 handle unification of a single P/A pair. Parameters are as
20082 for those functions. */
20083
20084 static int
20085 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20086 int subr, unification_kind_t strict,
20087 bool explain_p)
20088 {
20089 tree arg_expr = NULL_TREE;
20090 int arg_strict;
20091
20092 if (arg == error_mark_node || parm == error_mark_node)
20093 return unify_invalid (explain_p);
20094 if (arg == unknown_type_node)
20095 /* We can't deduce anything from this, but we might get all the
20096 template args from other function args. */
20097 return unify_success (explain_p);
20098
20099 /* Implicit conversions (Clause 4) will be performed on a function
20100 argument to convert it to the type of the corresponding function
20101 parameter if the parameter type contains no template-parameters that
20102 participate in template argument deduction. */
20103 if (strict != DEDUCE_EXACT
20104 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20105 /* For function parameters with no deducible template parameters,
20106 just return. We'll check non-dependent conversions later. */
20107 return unify_success (explain_p);
20108
20109 switch (strict)
20110 {
20111 case DEDUCE_CALL:
20112 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20113 | UNIFY_ALLOW_MORE_CV_QUAL
20114 | UNIFY_ALLOW_DERIVED);
20115 break;
20116
20117 case DEDUCE_CONV:
20118 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20119 break;
20120
20121 case DEDUCE_EXACT:
20122 arg_strict = UNIFY_ALLOW_NONE;
20123 break;
20124
20125 default:
20126 gcc_unreachable ();
20127 }
20128
20129 /* We only do these transformations if this is the top-level
20130 parameter_type_list in a call or declaration matching; in other
20131 situations (nested function declarators, template argument lists) we
20132 won't be comparing a type to an expression, and we don't do any type
20133 adjustments. */
20134 if (!subr)
20135 {
20136 if (!TYPE_P (arg))
20137 {
20138 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20139 if (type_unknown_p (arg))
20140 {
20141 /* [temp.deduct.type] A template-argument can be
20142 deduced from a pointer to function or pointer
20143 to member function argument if the set of
20144 overloaded functions does not contain function
20145 templates and at most one of a set of
20146 overloaded functions provides a unique
20147 match. */
20148 resolve_overloaded_unification (tparms, targs, parm,
20149 arg, strict,
20150 arg_strict, explain_p);
20151 /* If a unique match was not found, this is a
20152 non-deduced context, so we still succeed. */
20153 return unify_success (explain_p);
20154 }
20155
20156 arg_expr = arg;
20157 arg = unlowered_expr_type (arg);
20158 if (arg == error_mark_node)
20159 return unify_invalid (explain_p);
20160 }
20161
20162 arg_strict |=
20163 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20164 }
20165 else
20166 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20167 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20168 return unify_template_argument_mismatch (explain_p, parm, arg);
20169
20170 /* For deduction from an init-list we need the actual list. */
20171 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20172 arg = arg_expr;
20173 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20174 }
20175
20176 /* for_each_template_parm callback that always returns 0. */
20177
20178 static int
20179 zero_r (tree, void *)
20180 {
20181 return 0;
20182 }
20183
20184 /* for_each_template_parm any_fn callback to handle deduction of a template
20185 type argument from the type of an array bound. */
20186
20187 static int
20188 array_deduction_r (tree t, void *data)
20189 {
20190 tree_pair_p d = (tree_pair_p)data;
20191 tree &tparms = d->purpose;
20192 tree &targs = d->value;
20193
20194 if (TREE_CODE (t) == ARRAY_TYPE)
20195 if (tree dom = TYPE_DOMAIN (t))
20196 if (tree max = TYPE_MAX_VALUE (dom))
20197 {
20198 if (TREE_CODE (max) == MINUS_EXPR)
20199 max = TREE_OPERAND (max, 0);
20200 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20201 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20202 UNIFY_ALLOW_NONE, /*explain*/false);
20203 }
20204
20205 /* Keep walking. */
20206 return 0;
20207 }
20208
20209 /* Try to deduce any not-yet-deduced template type arguments from the type of
20210 an array bound. This is handled separately from unify because 14.8.2.5 says
20211 "The type of a type parameter is only deduced from an array bound if it is
20212 not otherwise deduced." */
20213
20214 static void
20215 try_array_deduction (tree tparms, tree targs, tree parm)
20216 {
20217 tree_pair_s data = { tparms, targs };
20218 hash_set<tree> visited;
20219 for_each_template_parm (parm, zero_r, &data, &visited,
20220 /*nondeduced*/false, array_deduction_r);
20221 }
20222
20223 /* Most parms like fn_type_unification.
20224
20225 If SUBR is 1, we're being called recursively (to unify the
20226 arguments of a function or method parameter of a function
20227 template).
20228
20229 CHECKS is a pointer to a vector of access checks encountered while
20230 substituting default template arguments. */
20231
20232 static int
20233 type_unification_real (tree tparms,
20234 tree full_targs,
20235 tree xparms,
20236 const tree *xargs,
20237 unsigned int xnargs,
20238 int subr,
20239 unification_kind_t strict,
20240 vec<deferred_access_check, va_gc> **checks,
20241 bool explain_p)
20242 {
20243 tree parm, arg;
20244 int i;
20245 int ntparms = TREE_VEC_LENGTH (tparms);
20246 int saw_undeduced = 0;
20247 tree parms;
20248 const tree *args;
20249 unsigned int nargs;
20250 unsigned int ia;
20251
20252 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20253 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20254 gcc_assert (ntparms > 0);
20255
20256 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20257
20258 /* Reset the number of non-defaulted template arguments contained
20259 in TARGS. */
20260 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20261
20262 again:
20263 parms = xparms;
20264 args = xargs;
20265 nargs = xnargs;
20266
20267 ia = 0;
20268 while (parms && parms != void_list_node
20269 && ia < nargs)
20270 {
20271 parm = TREE_VALUE (parms);
20272
20273 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20274 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20275 /* For a function parameter pack that occurs at the end of the
20276 parameter-declaration-list, the type A of each remaining
20277 argument of the call is compared with the type P of the
20278 declarator-id of the function parameter pack. */
20279 break;
20280
20281 parms = TREE_CHAIN (parms);
20282
20283 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20284 /* For a function parameter pack that does not occur at the
20285 end of the parameter-declaration-list, the type of the
20286 parameter pack is a non-deduced context. */
20287 continue;
20288
20289 arg = args[ia];
20290 ++ia;
20291
20292 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20293 explain_p))
20294 return 1;
20295 }
20296
20297 if (parms
20298 && parms != void_list_node
20299 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20300 {
20301 /* Unify the remaining arguments with the pack expansion type. */
20302 tree argvec;
20303 tree parmvec = make_tree_vec (1);
20304
20305 /* Allocate a TREE_VEC and copy in all of the arguments */
20306 argvec = make_tree_vec (nargs - ia);
20307 for (i = 0; ia < nargs; ++ia, ++i)
20308 TREE_VEC_ELT (argvec, i) = args[ia];
20309
20310 /* Copy the parameter into parmvec. */
20311 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20312 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20313 /*subr=*/subr, explain_p))
20314 return 1;
20315
20316 /* Advance to the end of the list of parameters. */
20317 parms = TREE_CHAIN (parms);
20318 }
20319
20320 /* Fail if we've reached the end of the parm list, and more args
20321 are present, and the parm list isn't variadic. */
20322 if (ia < nargs && parms == void_list_node)
20323 return unify_too_many_arguments (explain_p, nargs, ia);
20324 /* Fail if parms are left and they don't have default values and
20325 they aren't all deduced as empty packs (c++/57397). This is
20326 consistent with sufficient_parms_p. */
20327 if (parms && parms != void_list_node
20328 && TREE_PURPOSE (parms) == NULL_TREE)
20329 {
20330 unsigned int count = nargs;
20331 tree p = parms;
20332 bool type_pack_p;
20333 do
20334 {
20335 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20336 if (!type_pack_p)
20337 count++;
20338 p = TREE_CHAIN (p);
20339 }
20340 while (p && p != void_list_node);
20341 if (count != nargs)
20342 return unify_too_few_arguments (explain_p, ia, count,
20343 type_pack_p);
20344 }
20345
20346 if (!subr)
20347 {
20348 tsubst_flags_t complain = (explain_p
20349 ? tf_warning_or_error
20350 : tf_none);
20351 bool tried_array_deduction = (cxx_dialect < cxx17);
20352
20353 for (i = 0; i < ntparms; i++)
20354 {
20355 tree targ = TREE_VEC_ELT (targs, i);
20356 tree tparm = TREE_VEC_ELT (tparms, i);
20357
20358 /* Clear the "incomplete" flags on all argument packs now so that
20359 substituting them into later default arguments works. */
20360 if (targ && ARGUMENT_PACK_P (targ))
20361 {
20362 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20363 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20364 }
20365
20366 if (targ || tparm == error_mark_node)
20367 continue;
20368 tparm = TREE_VALUE (tparm);
20369
20370 if (TREE_CODE (tparm) == TYPE_DECL
20371 && !tried_array_deduction)
20372 {
20373 try_array_deduction (tparms, targs, xparms);
20374 tried_array_deduction = true;
20375 if (TREE_VEC_ELT (targs, i))
20376 continue;
20377 }
20378
20379 /* If this is an undeduced nontype parameter that depends on
20380 a type parameter, try another pass; its type may have been
20381 deduced from a later argument than the one from which
20382 this parameter can be deduced. */
20383 if (TREE_CODE (tparm) == PARM_DECL
20384 && uses_template_parms (TREE_TYPE (tparm))
20385 && saw_undeduced < 2)
20386 {
20387 saw_undeduced = 1;
20388 continue;
20389 }
20390
20391 /* Core issue #226 (C++0x) [temp.deduct]:
20392
20393 If a template argument has not been deduced, its
20394 default template argument, if any, is used.
20395
20396 When we are in C++98 mode, TREE_PURPOSE will either
20397 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20398 to explicitly check cxx_dialect here. */
20399 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20400 /* OK, there is a default argument. Wait until after the
20401 conversion check to do substitution. */
20402 continue;
20403
20404 /* If the type parameter is a parameter pack, then it will
20405 be deduced to an empty parameter pack. */
20406 if (template_parameter_pack_p (tparm))
20407 {
20408 tree arg;
20409
20410 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20411 {
20412 arg = make_node (NONTYPE_ARGUMENT_PACK);
20413 TREE_CONSTANT (arg) = 1;
20414 }
20415 else
20416 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
20417
20418 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
20419
20420 TREE_VEC_ELT (targs, i) = arg;
20421 continue;
20422 }
20423
20424 return unify_parameter_deduction_failure (explain_p, tparm);
20425 }
20426
20427 /* Now substitute into the default template arguments. */
20428 for (i = 0; i < ntparms; i++)
20429 {
20430 tree targ = TREE_VEC_ELT (targs, i);
20431 tree tparm = TREE_VEC_ELT (tparms, i);
20432
20433 if (targ || tparm == error_mark_node)
20434 continue;
20435 tree parm = TREE_VALUE (tparm);
20436 tree arg = TREE_PURPOSE (tparm);
20437 reopen_deferring_access_checks (*checks);
20438 location_t save_loc = input_location;
20439 if (DECL_P (parm))
20440 input_location = DECL_SOURCE_LOCATION (parm);
20441
20442 if (saw_undeduced == 1
20443 && TREE_CODE (parm) == PARM_DECL
20444 && uses_template_parms (TREE_TYPE (parm)))
20445 {
20446 /* The type of this non-type parameter depends on undeduced
20447 parameters. Don't try to use its default argument yet,
20448 since we might deduce an argument for it on the next pass,
20449 but do check whether the arguments we already have cause
20450 substitution failure, so that that happens before we try
20451 later default arguments (78489). */
20452 ++processing_template_decl;
20453 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20454 NULL_TREE);
20455 --processing_template_decl;
20456 if (type == error_mark_node)
20457 arg = error_mark_node;
20458 else
20459 arg = NULL_TREE;
20460 }
20461 else
20462 {
20463 tree substed = NULL_TREE;
20464 if (saw_undeduced == 1 && processing_template_decl == 0)
20465 {
20466 /* First instatiate in template context, in case we still
20467 depend on undeduced template parameters. */
20468 ++processing_template_decl;
20469 substed = tsubst_template_arg (arg, full_targs, complain,
20470 NULL_TREE);
20471 --processing_template_decl;
20472 if (substed != error_mark_node
20473 && !uses_template_parms (substed))
20474 /* We replaced all the tparms, substitute again out of
20475 template context. */
20476 substed = NULL_TREE;
20477 }
20478 if (!substed)
20479 substed = tsubst_template_arg (arg, full_targs, complain,
20480 NULL_TREE);
20481
20482 if (!uses_template_parms (substed))
20483 arg = convert_template_argument (parm, substed, full_targs,
20484 complain, i, NULL_TREE);
20485 else if (saw_undeduced == 1)
20486 arg = NULL_TREE;
20487 else
20488 arg = error_mark_node;
20489 }
20490
20491 input_location = save_loc;
20492 *checks = get_deferred_access_checks ();
20493 pop_deferring_access_checks ();
20494
20495 if (arg == error_mark_node)
20496 return 1;
20497 else if (arg)
20498 {
20499 TREE_VEC_ELT (targs, i) = arg;
20500 /* The position of the first default template argument,
20501 is also the number of non-defaulted arguments in TARGS.
20502 Record that. */
20503 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20504 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20505 }
20506 }
20507
20508 if (saw_undeduced++ == 1)
20509 goto again;
20510 }
20511
20512 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20513 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20514
20515 return unify_success (explain_p);
20516 }
20517
20518 /* Subroutine of type_unification_real. Args are like the variables
20519 at the call site. ARG is an overloaded function (or template-id);
20520 we try deducing template args from each of the overloads, and if
20521 only one succeeds, we go with that. Modifies TARGS and returns
20522 true on success. */
20523
20524 static bool
20525 resolve_overloaded_unification (tree tparms,
20526 tree targs,
20527 tree parm,
20528 tree arg,
20529 unification_kind_t strict,
20530 int sub_strict,
20531 bool explain_p)
20532 {
20533 tree tempargs = copy_node (targs);
20534 int good = 0;
20535 tree goodfn = NULL_TREE;
20536 bool addr_p;
20537
20538 if (TREE_CODE (arg) == ADDR_EXPR)
20539 {
20540 arg = TREE_OPERAND (arg, 0);
20541 addr_p = true;
20542 }
20543 else
20544 addr_p = false;
20545
20546 if (TREE_CODE (arg) == COMPONENT_REF)
20547 /* Handle `&x' where `x' is some static or non-static member
20548 function name. */
20549 arg = TREE_OPERAND (arg, 1);
20550
20551 if (TREE_CODE (arg) == OFFSET_REF)
20552 arg = TREE_OPERAND (arg, 1);
20553
20554 /* Strip baselink information. */
20555 if (BASELINK_P (arg))
20556 arg = BASELINK_FUNCTIONS (arg);
20557
20558 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20559 {
20560 /* If we got some explicit template args, we need to plug them into
20561 the affected templates before we try to unify, in case the
20562 explicit args will completely resolve the templates in question. */
20563
20564 int ok = 0;
20565 tree expl_subargs = TREE_OPERAND (arg, 1);
20566 arg = TREE_OPERAND (arg, 0);
20567
20568 for (lkp_iterator iter (arg); iter; ++iter)
20569 {
20570 tree fn = *iter;
20571 tree subargs, elem;
20572
20573 if (TREE_CODE (fn) != TEMPLATE_DECL)
20574 continue;
20575
20576 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20577 expl_subargs, NULL_TREE, tf_none,
20578 /*require_all_args=*/true,
20579 /*use_default_args=*/true);
20580 if (subargs != error_mark_node
20581 && !any_dependent_template_arguments_p (subargs))
20582 {
20583 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20584 if (try_one_overload (tparms, targs, tempargs, parm,
20585 elem, strict, sub_strict, addr_p, explain_p)
20586 && (!goodfn || !same_type_p (goodfn, elem)))
20587 {
20588 goodfn = elem;
20589 ++good;
20590 }
20591 }
20592 else if (subargs)
20593 ++ok;
20594 }
20595 /* If no templates (or more than one) are fully resolved by the
20596 explicit arguments, this template-id is a non-deduced context; it
20597 could still be OK if we deduce all template arguments for the
20598 enclosing call through other arguments. */
20599 if (good != 1)
20600 good = ok;
20601 }
20602 else if (TREE_CODE (arg) != OVERLOAD
20603 && TREE_CODE (arg) != FUNCTION_DECL)
20604 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20605 -- but the deduction does not succeed because the expression is
20606 not just the function on its own. */
20607 return false;
20608 else
20609 for (lkp_iterator iter (arg); iter; ++iter)
20610 {
20611 tree fn = *iter;
20612 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20613 strict, sub_strict, addr_p, explain_p)
20614 && (!goodfn || !decls_match (goodfn, fn)))
20615 {
20616 goodfn = fn;
20617 ++good;
20618 }
20619 }
20620
20621 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20622 to function or pointer to member function argument if the set of
20623 overloaded functions does not contain function templates and at most
20624 one of a set of overloaded functions provides a unique match.
20625
20626 So if we found multiple possibilities, we return success but don't
20627 deduce anything. */
20628
20629 if (good == 1)
20630 {
20631 int i = TREE_VEC_LENGTH (targs);
20632 for (; i--; )
20633 if (TREE_VEC_ELT (tempargs, i))
20634 {
20635 tree old = TREE_VEC_ELT (targs, i);
20636 tree new_ = TREE_VEC_ELT (tempargs, i);
20637 if (new_ && old && ARGUMENT_PACK_P (old)
20638 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20639 /* Don't forget explicit template arguments in a pack. */
20640 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20641 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20642 TREE_VEC_ELT (targs, i) = new_;
20643 }
20644 }
20645 if (good)
20646 return true;
20647
20648 return false;
20649 }
20650
20651 /* Core DR 115: In contexts where deduction is done and fails, or in
20652 contexts where deduction is not done, if a template argument list is
20653 specified and it, along with any default template arguments, identifies
20654 a single function template specialization, then the template-id is an
20655 lvalue for the function template specialization. */
20656
20657 tree
20658 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20659 {
20660 tree expr, offset, baselink;
20661 bool addr;
20662
20663 if (!type_unknown_p (orig_expr))
20664 return orig_expr;
20665
20666 expr = orig_expr;
20667 addr = false;
20668 offset = NULL_TREE;
20669 baselink = NULL_TREE;
20670
20671 if (TREE_CODE (expr) == ADDR_EXPR)
20672 {
20673 expr = TREE_OPERAND (expr, 0);
20674 addr = true;
20675 }
20676 if (TREE_CODE (expr) == OFFSET_REF)
20677 {
20678 offset = expr;
20679 expr = TREE_OPERAND (expr, 1);
20680 }
20681 if (BASELINK_P (expr))
20682 {
20683 baselink = expr;
20684 expr = BASELINK_FUNCTIONS (expr);
20685 }
20686
20687 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20688 {
20689 int good = 0;
20690 tree goodfn = NULL_TREE;
20691
20692 /* If we got some explicit template args, we need to plug them into
20693 the affected templates before we try to unify, in case the
20694 explicit args will completely resolve the templates in question. */
20695
20696 tree expl_subargs = TREE_OPERAND (expr, 1);
20697 tree arg = TREE_OPERAND (expr, 0);
20698 tree badfn = NULL_TREE;
20699 tree badargs = NULL_TREE;
20700
20701 for (lkp_iterator iter (arg); iter; ++iter)
20702 {
20703 tree fn = *iter;
20704 tree subargs, elem;
20705
20706 if (TREE_CODE (fn) != TEMPLATE_DECL)
20707 continue;
20708
20709 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20710 expl_subargs, NULL_TREE, tf_none,
20711 /*require_all_args=*/true,
20712 /*use_default_args=*/true);
20713 if (subargs != error_mark_node
20714 && !any_dependent_template_arguments_p (subargs))
20715 {
20716 elem = instantiate_template (fn, subargs, tf_none);
20717 if (elem == error_mark_node)
20718 {
20719 badfn = fn;
20720 badargs = subargs;
20721 }
20722 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20723 {
20724 goodfn = elem;
20725 ++good;
20726 }
20727 }
20728 }
20729 if (good == 1)
20730 {
20731 mark_used (goodfn);
20732 expr = goodfn;
20733 if (baselink)
20734 expr = build_baselink (BASELINK_BINFO (baselink),
20735 BASELINK_ACCESS_BINFO (baselink),
20736 expr, BASELINK_OPTYPE (baselink));
20737 if (offset)
20738 {
20739 tree base
20740 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20741 expr = build_offset_ref (base, expr, addr, complain);
20742 }
20743 if (addr)
20744 expr = cp_build_addr_expr (expr, complain);
20745 return expr;
20746 }
20747 else if (good == 0 && badargs && (complain & tf_error))
20748 /* There were no good options and at least one bad one, so let the
20749 user know what the problem is. */
20750 instantiate_template (badfn, badargs, complain);
20751 }
20752 return orig_expr;
20753 }
20754
20755 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20756 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20757 different overloads deduce different arguments for a given parm.
20758 ADDR_P is true if the expression for which deduction is being
20759 performed was of the form "& fn" rather than simply "fn".
20760
20761 Returns 1 on success. */
20762
20763 static int
20764 try_one_overload (tree tparms,
20765 tree orig_targs,
20766 tree targs,
20767 tree parm,
20768 tree arg,
20769 unification_kind_t strict,
20770 int sub_strict,
20771 bool addr_p,
20772 bool explain_p)
20773 {
20774 int nargs;
20775 tree tempargs;
20776 int i;
20777
20778 if (arg == error_mark_node)
20779 return 0;
20780
20781 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20782 to function or pointer to member function argument if the set of
20783 overloaded functions does not contain function templates and at most
20784 one of a set of overloaded functions provides a unique match.
20785
20786 So if this is a template, just return success. */
20787
20788 if (uses_template_parms (arg))
20789 return 1;
20790
20791 if (TREE_CODE (arg) == METHOD_TYPE)
20792 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20793 else if (addr_p)
20794 arg = build_pointer_type (arg);
20795
20796 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20797
20798 /* We don't copy orig_targs for this because if we have already deduced
20799 some template args from previous args, unify would complain when we
20800 try to deduce a template parameter for the same argument, even though
20801 there isn't really a conflict. */
20802 nargs = TREE_VEC_LENGTH (targs);
20803 tempargs = make_tree_vec (nargs);
20804
20805 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20806 return 0;
20807
20808 /* First make sure we didn't deduce anything that conflicts with
20809 explicitly specified args. */
20810 for (i = nargs; i--; )
20811 {
20812 tree elt = TREE_VEC_ELT (tempargs, i);
20813 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20814
20815 if (!elt)
20816 /*NOP*/;
20817 else if (uses_template_parms (elt))
20818 /* Since we're unifying against ourselves, we will fill in
20819 template args used in the function parm list with our own
20820 template parms. Discard them. */
20821 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20822 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20823 {
20824 /* Check that the argument at each index of the deduced argument pack
20825 is equivalent to the corresponding explicitly specified argument.
20826 We may have deduced more arguments than were explicitly specified,
20827 and that's OK. */
20828
20829 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20830 that's wrong if we deduce the same argument pack from multiple
20831 function arguments: it's only incomplete the first time. */
20832
20833 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20834 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20835
20836 if (TREE_VEC_LENGTH (deduced_pack)
20837 < TREE_VEC_LENGTH (explicit_pack))
20838 return 0;
20839
20840 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20841 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20842 TREE_VEC_ELT (deduced_pack, j)))
20843 return 0;
20844 }
20845 else if (oldelt && !template_args_equal (oldelt, elt))
20846 return 0;
20847 }
20848
20849 for (i = nargs; i--; )
20850 {
20851 tree elt = TREE_VEC_ELT (tempargs, i);
20852
20853 if (elt)
20854 TREE_VEC_ELT (targs, i) = elt;
20855 }
20856
20857 return 1;
20858 }
20859
20860 /* PARM is a template class (perhaps with unbound template
20861 parameters). ARG is a fully instantiated type. If ARG can be
20862 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20863 TARGS are as for unify. */
20864
20865 static tree
20866 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20867 bool explain_p)
20868 {
20869 tree copy_of_targs;
20870
20871 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20872 return NULL_TREE;
20873 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20874 /* Matches anything. */;
20875 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20876 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20877 return NULL_TREE;
20878
20879 /* We need to make a new template argument vector for the call to
20880 unify. If we used TARGS, we'd clutter it up with the result of
20881 the attempted unification, even if this class didn't work out.
20882 We also don't want to commit ourselves to all the unifications
20883 we've already done, since unification is supposed to be done on
20884 an argument-by-argument basis. In other words, consider the
20885 following pathological case:
20886
20887 template <int I, int J, int K>
20888 struct S {};
20889
20890 template <int I, int J>
20891 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20892
20893 template <int I, int J, int K>
20894 void f(S<I, J, K>, S<I, I, I>);
20895
20896 void g() {
20897 S<0, 0, 0> s0;
20898 S<0, 1, 2> s2;
20899
20900 f(s0, s2);
20901 }
20902
20903 Now, by the time we consider the unification involving `s2', we
20904 already know that we must have `f<0, 0, 0>'. But, even though
20905 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20906 because there are two ways to unify base classes of S<0, 1, 2>
20907 with S<I, I, I>. If we kept the already deduced knowledge, we
20908 would reject the possibility I=1. */
20909 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20910
20911 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20912 {
20913 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20914 return NULL_TREE;
20915 return arg;
20916 }
20917
20918 /* If unification failed, we're done. */
20919 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20920 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20921 return NULL_TREE;
20922
20923 return arg;
20924 }
20925
20926 /* Given a template type PARM and a class type ARG, find the unique
20927 base type in ARG that is an instance of PARM. We do not examine
20928 ARG itself; only its base-classes. If there is not exactly one
20929 appropriate base class, return NULL_TREE. PARM may be the type of
20930 a partial specialization, as well as a plain template type. Used
20931 by unify. */
20932
20933 static enum template_base_result
20934 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20935 bool explain_p, tree *result)
20936 {
20937 tree rval = NULL_TREE;
20938 tree binfo;
20939
20940 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20941
20942 binfo = TYPE_BINFO (complete_type (arg));
20943 if (!binfo)
20944 {
20945 /* The type could not be completed. */
20946 *result = NULL_TREE;
20947 return tbr_incomplete_type;
20948 }
20949
20950 /* Walk in inheritance graph order. The search order is not
20951 important, and this avoids multiple walks of virtual bases. */
20952 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20953 {
20954 tree r = try_class_unification (tparms, targs, parm,
20955 BINFO_TYPE (binfo), explain_p);
20956
20957 if (r)
20958 {
20959 /* If there is more than one satisfactory baseclass, then:
20960
20961 [temp.deduct.call]
20962
20963 If they yield more than one possible deduced A, the type
20964 deduction fails.
20965
20966 applies. */
20967 if (rval && !same_type_p (r, rval))
20968 {
20969 *result = NULL_TREE;
20970 return tbr_ambiguous_baseclass;
20971 }
20972
20973 rval = r;
20974 }
20975 }
20976
20977 *result = rval;
20978 return tbr_success;
20979 }
20980
20981 /* Returns the level of DECL, which declares a template parameter. */
20982
20983 static int
20984 template_decl_level (tree decl)
20985 {
20986 switch (TREE_CODE (decl))
20987 {
20988 case TYPE_DECL:
20989 case TEMPLATE_DECL:
20990 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20991
20992 case PARM_DECL:
20993 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20994
20995 default:
20996 gcc_unreachable ();
20997 }
20998 return 0;
20999 }
21000
21001 /* Decide whether ARG can be unified with PARM, considering only the
21002 cv-qualifiers of each type, given STRICT as documented for unify.
21003 Returns nonzero iff the unification is OK on that basis. */
21004
21005 static int
21006 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21007 {
21008 int arg_quals = cp_type_quals (arg);
21009 int parm_quals = cp_type_quals (parm);
21010
21011 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21012 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21013 {
21014 /* Although a CVR qualifier is ignored when being applied to a
21015 substituted template parameter ([8.3.2]/1 for example), that
21016 does not allow us to unify "const T" with "int&" because both
21017 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21018 It is ok when we're allowing additional CV qualifiers
21019 at the outer level [14.8.2.1]/3,1st bullet. */
21020 if ((TYPE_REF_P (arg)
21021 || TREE_CODE (arg) == FUNCTION_TYPE
21022 || TREE_CODE (arg) == METHOD_TYPE)
21023 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21024 return 0;
21025
21026 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21027 && (parm_quals & TYPE_QUAL_RESTRICT))
21028 return 0;
21029 }
21030
21031 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21032 && (arg_quals & parm_quals) != parm_quals)
21033 return 0;
21034
21035 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21036 && (parm_quals & arg_quals) != arg_quals)
21037 return 0;
21038
21039 return 1;
21040 }
21041
21042 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21043 void
21044 template_parm_level_and_index (tree parm, int* level, int* index)
21045 {
21046 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21047 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21048 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21049 {
21050 *index = TEMPLATE_TYPE_IDX (parm);
21051 *level = TEMPLATE_TYPE_LEVEL (parm);
21052 }
21053 else
21054 {
21055 *index = TEMPLATE_PARM_IDX (parm);
21056 *level = TEMPLATE_PARM_LEVEL (parm);
21057 }
21058 }
21059
21060 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21061 do { \
21062 if (unify (TP, TA, P, A, S, EP)) \
21063 return 1; \
21064 } while (0)
21065
21066 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21067 expansion at the end of PACKED_PARMS. Returns 0 if the type
21068 deduction succeeds, 1 otherwise. STRICT is the same as in
21069 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21070 function call argument list. We'll need to adjust the arguments to make them
21071 types. SUBR tells us if this is from a recursive call to
21072 type_unification_real, or for comparing two template argument
21073 lists. */
21074
21075 static int
21076 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21077 tree packed_args, unification_kind_t strict,
21078 bool subr, bool explain_p)
21079 {
21080 tree parm
21081 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21082 tree pattern = PACK_EXPANSION_PATTERN (parm);
21083 tree pack, packs = NULL_TREE;
21084 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21085
21086 /* Add in any args remembered from an earlier partial instantiation. */
21087 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21088 int levels = TMPL_ARGS_DEPTH (targs);
21089
21090 packed_args = expand_template_argument_pack (packed_args);
21091
21092 int len = TREE_VEC_LENGTH (packed_args);
21093
21094 /* Determine the parameter packs we will be deducing from the
21095 pattern, and record their current deductions. */
21096 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21097 pack; pack = TREE_CHAIN (pack))
21098 {
21099 tree parm_pack = TREE_VALUE (pack);
21100 int idx, level;
21101
21102 /* Only template parameter packs can be deduced, not e.g. function
21103 parameter packs or __bases or __integer_pack. */
21104 if (!TEMPLATE_PARM_P (parm_pack))
21105 continue;
21106
21107 /* Determine the index and level of this parameter pack. */
21108 template_parm_level_and_index (parm_pack, &level, &idx);
21109 if (level < levels)
21110 continue;
21111
21112 /* Keep track of the parameter packs and their corresponding
21113 argument packs. */
21114 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21115 TREE_TYPE (packs) = make_tree_vec (len - start);
21116 }
21117
21118 /* Loop through all of the arguments that have not yet been
21119 unified and unify each with the pattern. */
21120 for (i = start; i < len; i++)
21121 {
21122 tree parm;
21123 bool any_explicit = false;
21124 tree arg = TREE_VEC_ELT (packed_args, i);
21125
21126 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21127 or the element of its argument pack at the current index if
21128 this argument was explicitly specified. */
21129 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21130 {
21131 int idx, level;
21132 tree arg, pargs;
21133 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21134
21135 arg = NULL_TREE;
21136 if (TREE_VALUE (pack)
21137 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21138 && (i - start < TREE_VEC_LENGTH (pargs)))
21139 {
21140 any_explicit = true;
21141 arg = TREE_VEC_ELT (pargs, i - start);
21142 }
21143 TMPL_ARG (targs, level, idx) = arg;
21144 }
21145
21146 /* If we had explicit template arguments, substitute them into the
21147 pattern before deduction. */
21148 if (any_explicit)
21149 {
21150 /* Some arguments might still be unspecified or dependent. */
21151 bool dependent;
21152 ++processing_template_decl;
21153 dependent = any_dependent_template_arguments_p (targs);
21154 if (!dependent)
21155 --processing_template_decl;
21156 parm = tsubst (pattern, targs,
21157 explain_p ? tf_warning_or_error : tf_none,
21158 NULL_TREE);
21159 if (dependent)
21160 --processing_template_decl;
21161 if (parm == error_mark_node)
21162 return 1;
21163 }
21164 else
21165 parm = pattern;
21166
21167 /* Unify the pattern with the current argument. */
21168 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21169 explain_p))
21170 return 1;
21171
21172 /* For each parameter pack, collect the deduced value. */
21173 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21174 {
21175 int idx, level;
21176 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21177
21178 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21179 TMPL_ARG (targs, level, idx);
21180 }
21181 }
21182
21183 /* Verify that the results of unification with the parameter packs
21184 produce results consistent with what we've seen before, and make
21185 the deduced argument packs available. */
21186 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21187 {
21188 tree old_pack = TREE_VALUE (pack);
21189 tree new_args = TREE_TYPE (pack);
21190 int i, len = TREE_VEC_LENGTH (new_args);
21191 int idx, level;
21192 bool nondeduced_p = false;
21193
21194 /* By default keep the original deduced argument pack.
21195 If necessary, more specific code is going to update the
21196 resulting deduced argument later down in this function. */
21197 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21198 TMPL_ARG (targs, level, idx) = old_pack;
21199
21200 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21201 actually deduce anything. */
21202 for (i = 0; i < len && !nondeduced_p; ++i)
21203 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21204 nondeduced_p = true;
21205 if (nondeduced_p)
21206 continue;
21207
21208 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21209 {
21210 /* If we had fewer function args than explicit template args,
21211 just use the explicits. */
21212 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21213 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21214 if (len < explicit_len)
21215 new_args = explicit_args;
21216 }
21217
21218 if (!old_pack)
21219 {
21220 tree result;
21221 /* Build the deduced *_ARGUMENT_PACK. */
21222 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21223 {
21224 result = make_node (NONTYPE_ARGUMENT_PACK);
21225 TREE_CONSTANT (result) = 1;
21226 }
21227 else
21228 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21229
21230 SET_ARGUMENT_PACK_ARGS (result, new_args);
21231
21232 /* Note the deduced argument packs for this parameter
21233 pack. */
21234 TMPL_ARG (targs, level, idx) = result;
21235 }
21236 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21237 && (ARGUMENT_PACK_ARGS (old_pack)
21238 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21239 {
21240 /* We only had the explicitly-provided arguments before, but
21241 now we have a complete set of arguments. */
21242 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21243
21244 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21245 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21246 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21247 }
21248 else
21249 {
21250 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21251 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21252
21253 if (!comp_template_args (old_args, new_args,
21254 &bad_old_arg, &bad_new_arg))
21255 /* Inconsistent unification of this parameter pack. */
21256 return unify_parameter_pack_inconsistent (explain_p,
21257 bad_old_arg,
21258 bad_new_arg);
21259 }
21260 }
21261
21262 return unify_success (explain_p);
21263 }
21264
21265 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21266 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21267 parameters and return value are as for unify. */
21268
21269 static int
21270 unify_array_domain (tree tparms, tree targs,
21271 tree parm_dom, tree arg_dom,
21272 bool explain_p)
21273 {
21274 tree parm_max;
21275 tree arg_max;
21276 bool parm_cst;
21277 bool arg_cst;
21278
21279 /* Our representation of array types uses "N - 1" as the
21280 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21281 not an integer constant. We cannot unify arbitrarily
21282 complex expressions, so we eliminate the MINUS_EXPRs
21283 here. */
21284 parm_max = TYPE_MAX_VALUE (parm_dom);
21285 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21286 if (!parm_cst)
21287 {
21288 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21289 parm_max = TREE_OPERAND (parm_max, 0);
21290 }
21291 arg_max = TYPE_MAX_VALUE (arg_dom);
21292 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21293 if (!arg_cst)
21294 {
21295 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21296 trying to unify the type of a variable with the type
21297 of a template parameter. For example:
21298
21299 template <unsigned int N>
21300 void f (char (&) [N]);
21301 int g();
21302 void h(int i) {
21303 char a[g(i)];
21304 f(a);
21305 }
21306
21307 Here, the type of the ARG will be "int [g(i)]", and
21308 may be a SAVE_EXPR, etc. */
21309 if (TREE_CODE (arg_max) != MINUS_EXPR)
21310 return unify_vla_arg (explain_p, arg_dom);
21311 arg_max = TREE_OPERAND (arg_max, 0);
21312 }
21313
21314 /* If only one of the bounds used a MINUS_EXPR, compensate
21315 by adding one to the other bound. */
21316 if (parm_cst && !arg_cst)
21317 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21318 integer_type_node,
21319 parm_max,
21320 integer_one_node);
21321 else if (arg_cst && !parm_cst)
21322 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21323 integer_type_node,
21324 arg_max,
21325 integer_one_node);
21326
21327 return unify (tparms, targs, parm_max, arg_max,
21328 UNIFY_ALLOW_INTEGER, explain_p);
21329 }
21330
21331 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21332
21333 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21334
21335 static pa_kind_t
21336 pa_kind (tree t)
21337 {
21338 if (PACK_EXPANSION_P (t))
21339 t = PACK_EXPANSION_PATTERN (t);
21340 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21341 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21342 || DECL_TYPE_TEMPLATE_P (t))
21343 return pa_tmpl;
21344 else if (TYPE_P (t))
21345 return pa_type;
21346 else
21347 return pa_expr;
21348 }
21349
21350 /* Deduce the value of template parameters. TPARMS is the (innermost)
21351 set of template parameters to a template. TARGS is the bindings
21352 for those template parameters, as determined thus far; TARGS may
21353 include template arguments for outer levels of template parameters
21354 as well. PARM is a parameter to a template function, or a
21355 subcomponent of that parameter; ARG is the corresponding argument.
21356 This function attempts to match PARM with ARG in a manner
21357 consistent with the existing assignments in TARGS. If more values
21358 are deduced, then TARGS is updated.
21359
21360 Returns 0 if the type deduction succeeds, 1 otherwise. The
21361 parameter STRICT is a bitwise or of the following flags:
21362
21363 UNIFY_ALLOW_NONE:
21364 Require an exact match between PARM and ARG.
21365 UNIFY_ALLOW_MORE_CV_QUAL:
21366 Allow the deduced ARG to be more cv-qualified (by qualification
21367 conversion) than ARG.
21368 UNIFY_ALLOW_LESS_CV_QUAL:
21369 Allow the deduced ARG to be less cv-qualified than ARG.
21370 UNIFY_ALLOW_DERIVED:
21371 Allow the deduced ARG to be a template base class of ARG,
21372 or a pointer to a template base class of the type pointed to by
21373 ARG.
21374 UNIFY_ALLOW_INTEGER:
21375 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21376 case for more information.
21377 UNIFY_ALLOW_OUTER_LEVEL:
21378 This is the outermost level of a deduction. Used to determine validity
21379 of qualification conversions. A valid qualification conversion must
21380 have const qualified pointers leading up to the inner type which
21381 requires additional CV quals, except at the outer level, where const
21382 is not required [conv.qual]. It would be normal to set this flag in
21383 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21384 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21385 This is the outermost level of a deduction, and PARM can be more CV
21386 qualified at this point.
21387 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21388 This is the outermost level of a deduction, and PARM can be less CV
21389 qualified at this point. */
21390
21391 static int
21392 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
21393 bool explain_p)
21394 {
21395 int idx;
21396 tree targ;
21397 tree tparm;
21398 int strict_in = strict;
21399 tsubst_flags_t complain = (explain_p
21400 ? tf_warning_or_error
21401 : tf_none);
21402
21403 /* I don't think this will do the right thing with respect to types.
21404 But the only case I've seen it in so far has been array bounds, where
21405 signedness is the only information lost, and I think that will be
21406 okay. */
21407 while (CONVERT_EXPR_P (parm))
21408 parm = TREE_OPERAND (parm, 0);
21409
21410 if (arg == error_mark_node)
21411 return unify_invalid (explain_p);
21412 if (arg == unknown_type_node
21413 || arg == init_list_type_node)
21414 /* We can't deduce anything from this, but we might get all the
21415 template args from other function args. */
21416 return unify_success (explain_p);
21417
21418 if (parm == any_targ_node || arg == any_targ_node)
21419 return unify_success (explain_p);
21420
21421 /* If PARM uses template parameters, then we can't bail out here,
21422 even if ARG == PARM, since we won't record unifications for the
21423 template parameters. We might need them if we're trying to
21424 figure out which of two things is more specialized. */
21425 if (arg == parm && !uses_template_parms (parm))
21426 return unify_success (explain_p);
21427
21428 /* Handle init lists early, so the rest of the function can assume
21429 we're dealing with a type. */
21430 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21431 {
21432 tree elt, elttype;
21433 unsigned i;
21434 tree orig_parm = parm;
21435
21436 /* Replace T with std::initializer_list<T> for deduction. */
21437 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21438 && flag_deduce_init_list)
21439 parm = listify (parm);
21440
21441 if (!is_std_init_list (parm)
21442 && TREE_CODE (parm) != ARRAY_TYPE)
21443 /* We can only deduce from an initializer list argument if the
21444 parameter is std::initializer_list or an array; otherwise this
21445 is a non-deduced context. */
21446 return unify_success (explain_p);
21447
21448 if (TREE_CODE (parm) == ARRAY_TYPE)
21449 elttype = TREE_TYPE (parm);
21450 else
21451 {
21452 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21453 /* Deduction is defined in terms of a single type, so just punt
21454 on the (bizarre) std::initializer_list<T...>. */
21455 if (PACK_EXPANSION_P (elttype))
21456 return unify_success (explain_p);
21457 }
21458
21459 if (strict != DEDUCE_EXACT
21460 && TYPE_P (elttype)
21461 && !uses_deducible_template_parms (elttype))
21462 /* If ELTTYPE has no deducible template parms, skip deduction from
21463 the list elements. */;
21464 else
21465 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21466 {
21467 int elt_strict = strict;
21468
21469 if (elt == error_mark_node)
21470 return unify_invalid (explain_p);
21471
21472 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21473 {
21474 tree type = TREE_TYPE (elt);
21475 if (type == error_mark_node)
21476 return unify_invalid (explain_p);
21477 /* It should only be possible to get here for a call. */
21478 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21479 elt_strict |= maybe_adjust_types_for_deduction
21480 (DEDUCE_CALL, &elttype, &type, elt);
21481 elt = type;
21482 }
21483
21484 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21485 explain_p);
21486 }
21487
21488 if (TREE_CODE (parm) == ARRAY_TYPE
21489 && deducible_array_bound (TYPE_DOMAIN (parm)))
21490 {
21491 /* Also deduce from the length of the initializer list. */
21492 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21493 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21494 if (idx == error_mark_node)
21495 return unify_invalid (explain_p);
21496 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21497 idx, explain_p);
21498 }
21499
21500 /* If the std::initializer_list<T> deduction worked, replace the
21501 deduced A with std::initializer_list<A>. */
21502 if (orig_parm != parm)
21503 {
21504 idx = TEMPLATE_TYPE_IDX (orig_parm);
21505 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21506 targ = listify (targ);
21507 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21508 }
21509 return unify_success (explain_p);
21510 }
21511
21512 /* If parm and arg aren't the same kind of thing (template, type, or
21513 expression), fail early. */
21514 if (pa_kind (parm) != pa_kind (arg))
21515 return unify_invalid (explain_p);
21516
21517 /* Immediately reject some pairs that won't unify because of
21518 cv-qualification mismatches. */
21519 if (TREE_CODE (arg) == TREE_CODE (parm)
21520 && TYPE_P (arg)
21521 /* It is the elements of the array which hold the cv quals of an array
21522 type, and the elements might be template type parms. We'll check
21523 when we recurse. */
21524 && TREE_CODE (arg) != ARRAY_TYPE
21525 /* We check the cv-qualifiers when unifying with template type
21526 parameters below. We want to allow ARG `const T' to unify with
21527 PARM `T' for example, when computing which of two templates
21528 is more specialized, for example. */
21529 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21530 && !check_cv_quals_for_unify (strict_in, arg, parm))
21531 return unify_cv_qual_mismatch (explain_p, parm, arg);
21532
21533 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21534 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21535 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21536 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21537 strict &= ~UNIFY_ALLOW_DERIVED;
21538 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21539 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21540
21541 switch (TREE_CODE (parm))
21542 {
21543 case TYPENAME_TYPE:
21544 case SCOPE_REF:
21545 case UNBOUND_CLASS_TEMPLATE:
21546 /* In a type which contains a nested-name-specifier, template
21547 argument values cannot be deduced for template parameters used
21548 within the nested-name-specifier. */
21549 return unify_success (explain_p);
21550
21551 case TEMPLATE_TYPE_PARM:
21552 case TEMPLATE_TEMPLATE_PARM:
21553 case BOUND_TEMPLATE_TEMPLATE_PARM:
21554 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21555 if (error_operand_p (tparm))
21556 return unify_invalid (explain_p);
21557
21558 if (TEMPLATE_TYPE_LEVEL (parm)
21559 != template_decl_level (tparm))
21560 /* The PARM is not one we're trying to unify. Just check
21561 to see if it matches ARG. */
21562 {
21563 if (TREE_CODE (arg) == TREE_CODE (parm)
21564 && (is_auto (parm) ? is_auto (arg)
21565 : same_type_p (parm, arg)))
21566 return unify_success (explain_p);
21567 else
21568 return unify_type_mismatch (explain_p, parm, arg);
21569 }
21570 idx = TEMPLATE_TYPE_IDX (parm);
21571 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21572 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21573 if (error_operand_p (tparm))
21574 return unify_invalid (explain_p);
21575
21576 /* Check for mixed types and values. */
21577 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21578 && TREE_CODE (tparm) != TYPE_DECL)
21579 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21580 && TREE_CODE (tparm) != TEMPLATE_DECL))
21581 gcc_unreachable ();
21582
21583 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21584 {
21585 if ((strict_in & UNIFY_ALLOW_DERIVED)
21586 && CLASS_TYPE_P (arg))
21587 {
21588 /* First try to match ARG directly. */
21589 tree t = try_class_unification (tparms, targs, parm, arg,
21590 explain_p);
21591 if (!t)
21592 {
21593 /* Otherwise, look for a suitable base of ARG, as below. */
21594 enum template_base_result r;
21595 r = get_template_base (tparms, targs, parm, arg,
21596 explain_p, &t);
21597 if (!t)
21598 return unify_no_common_base (explain_p, r, parm, arg);
21599 arg = t;
21600 }
21601 }
21602 /* ARG must be constructed from a template class or a template
21603 template parameter. */
21604 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21605 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21606 return unify_template_deduction_failure (explain_p, parm, arg);
21607
21608 /* Deduce arguments T, i from TT<T> or TT<i>. */
21609 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21610 return 1;
21611
21612 arg = TYPE_TI_TEMPLATE (arg);
21613
21614 /* Fall through to deduce template name. */
21615 }
21616
21617 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21618 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21619 {
21620 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21621
21622 /* Simple cases: Value already set, does match or doesn't. */
21623 if (targ != NULL_TREE && template_args_equal (targ, arg))
21624 return unify_success (explain_p);
21625 else if (targ)
21626 return unify_inconsistency (explain_p, parm, targ, arg);
21627 }
21628 else
21629 {
21630 /* If PARM is `const T' and ARG is only `int', we don't have
21631 a match unless we are allowing additional qualification.
21632 If ARG is `const int' and PARM is just `T' that's OK;
21633 that binds `const int' to `T'. */
21634 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21635 arg, parm))
21636 return unify_cv_qual_mismatch (explain_p, parm, arg);
21637
21638 /* Consider the case where ARG is `const volatile int' and
21639 PARM is `const T'. Then, T should be `volatile int'. */
21640 arg = cp_build_qualified_type_real
21641 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21642 if (arg == error_mark_node)
21643 return unify_invalid (explain_p);
21644
21645 /* Simple cases: Value already set, does match or doesn't. */
21646 if (targ != NULL_TREE && same_type_p (targ, arg))
21647 return unify_success (explain_p);
21648 else if (targ)
21649 return unify_inconsistency (explain_p, parm, targ, arg);
21650
21651 /* Make sure that ARG is not a variable-sized array. (Note
21652 that were talking about variable-sized arrays (like
21653 `int[n]'), rather than arrays of unknown size (like
21654 `int[]').) We'll get very confused by such a type since
21655 the bound of the array is not constant, and therefore
21656 not mangleable. Besides, such types are not allowed in
21657 ISO C++, so we can do as we please here. We do allow
21658 them for 'auto' deduction, since that isn't ABI-exposed. */
21659 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21660 return unify_vla_arg (explain_p, arg);
21661
21662 /* Strip typedefs as in convert_template_argument. */
21663 arg = canonicalize_type_argument (arg, tf_none);
21664 }
21665
21666 /* If ARG is a parameter pack or an expansion, we cannot unify
21667 against it unless PARM is also a parameter pack. */
21668 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21669 && !template_parameter_pack_p (parm))
21670 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21671
21672 /* If the argument deduction results is a METHOD_TYPE,
21673 then there is a problem.
21674 METHOD_TYPE doesn't map to any real C++ type the result of
21675 the deduction can not be of that type. */
21676 if (TREE_CODE (arg) == METHOD_TYPE)
21677 return unify_method_type_error (explain_p, arg);
21678
21679 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21680 return unify_success (explain_p);
21681
21682 case TEMPLATE_PARM_INDEX:
21683 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21684 if (error_operand_p (tparm))
21685 return unify_invalid (explain_p);
21686
21687 if (TEMPLATE_PARM_LEVEL (parm)
21688 != template_decl_level (tparm))
21689 {
21690 /* The PARM is not one we're trying to unify. Just check
21691 to see if it matches ARG. */
21692 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21693 && cp_tree_equal (parm, arg));
21694 if (result)
21695 unify_expression_unequal (explain_p, parm, arg);
21696 return result;
21697 }
21698
21699 idx = TEMPLATE_PARM_IDX (parm);
21700 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21701
21702 if (targ)
21703 {
21704 if ((strict & UNIFY_ALLOW_INTEGER)
21705 && TREE_TYPE (targ) && TREE_TYPE (arg)
21706 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21707 /* We're deducing from an array bound, the type doesn't matter. */
21708 arg = fold_convert (TREE_TYPE (targ), arg);
21709 int x = !cp_tree_equal (targ, arg);
21710 if (x)
21711 unify_inconsistency (explain_p, parm, targ, arg);
21712 return x;
21713 }
21714
21715 /* [temp.deduct.type] If, in the declaration of a function template
21716 with a non-type template-parameter, the non-type
21717 template-parameter is used in an expression in the function
21718 parameter-list and, if the corresponding template-argument is
21719 deduced, the template-argument type shall match the type of the
21720 template-parameter exactly, except that a template-argument
21721 deduced from an array bound may be of any integral type.
21722 The non-type parameter might use already deduced type parameters. */
21723 tparm = TREE_TYPE (parm);
21724 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21725 /* We don't have enough levels of args to do any substitution. This
21726 can happen in the context of -fnew-ttp-matching. */;
21727 else
21728 {
21729 ++processing_template_decl;
21730 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21731 --processing_template_decl;
21732
21733 if (tree a = type_uses_auto (tparm))
21734 {
21735 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21736 if (tparm == error_mark_node)
21737 return 1;
21738 }
21739 }
21740
21741 if (!TREE_TYPE (arg))
21742 /* Template-parameter dependent expression. Just accept it for now.
21743 It will later be processed in convert_template_argument. */
21744 ;
21745 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21746 non_reference (tparm)))
21747 /* OK */;
21748 else if ((strict & UNIFY_ALLOW_INTEGER)
21749 && CP_INTEGRAL_TYPE_P (tparm))
21750 /* Convert the ARG to the type of PARM; the deduced non-type
21751 template argument must exactly match the types of the
21752 corresponding parameter. */
21753 arg = fold (build_nop (tparm, arg));
21754 else if (uses_template_parms (tparm))
21755 {
21756 /* We haven't deduced the type of this parameter yet. */
21757 if (cxx_dialect >= cxx17
21758 /* We deduce from array bounds in try_array_deduction. */
21759 && !(strict & UNIFY_ALLOW_INTEGER))
21760 {
21761 /* Deduce it from the non-type argument. */
21762 tree atype = TREE_TYPE (arg);
21763 RECUR_AND_CHECK_FAILURE (tparms, targs,
21764 tparm, atype,
21765 UNIFY_ALLOW_NONE, explain_p);
21766 }
21767 else
21768 /* Try again later. */
21769 return unify_success (explain_p);
21770 }
21771 else
21772 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21773
21774 /* If ARG is a parameter pack or an expansion, we cannot unify
21775 against it unless PARM is also a parameter pack. */
21776 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21777 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21778 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21779
21780 {
21781 bool removed_attr = false;
21782 arg = strip_typedefs_expr (arg, &removed_attr);
21783 }
21784 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21785 return unify_success (explain_p);
21786
21787 case PTRMEM_CST:
21788 {
21789 /* A pointer-to-member constant can be unified only with
21790 another constant. */
21791 if (TREE_CODE (arg) != PTRMEM_CST)
21792 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21793
21794 /* Just unify the class member. It would be useless (and possibly
21795 wrong, depending on the strict flags) to unify also
21796 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21797 arg refer to the same variable, even if through different
21798 classes. For instance:
21799
21800 struct A { int x; };
21801 struct B : A { };
21802
21803 Unification of &A::x and &B::x must succeed. */
21804 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21805 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21806 }
21807
21808 case POINTER_TYPE:
21809 {
21810 if (!TYPE_PTR_P (arg))
21811 return unify_type_mismatch (explain_p, parm, arg);
21812
21813 /* [temp.deduct.call]
21814
21815 A can be another pointer or pointer to member type that can
21816 be converted to the deduced A via a qualification
21817 conversion (_conv.qual_).
21818
21819 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21820 This will allow for additional cv-qualification of the
21821 pointed-to types if appropriate. */
21822
21823 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21824 /* The derived-to-base conversion only persists through one
21825 level of pointers. */
21826 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21827
21828 return unify (tparms, targs, TREE_TYPE (parm),
21829 TREE_TYPE (arg), strict, explain_p);
21830 }
21831
21832 case REFERENCE_TYPE:
21833 if (!TYPE_REF_P (arg))
21834 return unify_type_mismatch (explain_p, parm, arg);
21835 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21836 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21837
21838 case ARRAY_TYPE:
21839 if (TREE_CODE (arg) != ARRAY_TYPE)
21840 return unify_type_mismatch (explain_p, parm, arg);
21841 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21842 != (TYPE_DOMAIN (arg) == NULL_TREE))
21843 return unify_type_mismatch (explain_p, parm, arg);
21844 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21845 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21846 if (TYPE_DOMAIN (parm) != NULL_TREE)
21847 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21848 TYPE_DOMAIN (arg), explain_p);
21849 return unify_success (explain_p);
21850
21851 case REAL_TYPE:
21852 case COMPLEX_TYPE:
21853 case VECTOR_TYPE:
21854 case INTEGER_TYPE:
21855 case BOOLEAN_TYPE:
21856 case ENUMERAL_TYPE:
21857 case VOID_TYPE:
21858 case NULLPTR_TYPE:
21859 if (TREE_CODE (arg) != TREE_CODE (parm))
21860 return unify_type_mismatch (explain_p, parm, arg);
21861
21862 /* We have already checked cv-qualification at the top of the
21863 function. */
21864 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21865 return unify_type_mismatch (explain_p, parm, arg);
21866
21867 /* As far as unification is concerned, this wins. Later checks
21868 will invalidate it if necessary. */
21869 return unify_success (explain_p);
21870
21871 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21872 /* Type INTEGER_CST can come from ordinary constant template args. */
21873 case INTEGER_CST:
21874 while (CONVERT_EXPR_P (arg))
21875 arg = TREE_OPERAND (arg, 0);
21876
21877 if (TREE_CODE (arg) != INTEGER_CST)
21878 return unify_template_argument_mismatch (explain_p, parm, arg);
21879 return (tree_int_cst_equal (parm, arg)
21880 ? unify_success (explain_p)
21881 : unify_template_argument_mismatch (explain_p, parm, arg));
21882
21883 case TREE_VEC:
21884 {
21885 int i, len, argslen;
21886 int parm_variadic_p = 0;
21887
21888 if (TREE_CODE (arg) != TREE_VEC)
21889 return unify_template_argument_mismatch (explain_p, parm, arg);
21890
21891 len = TREE_VEC_LENGTH (parm);
21892 argslen = TREE_VEC_LENGTH (arg);
21893
21894 /* Check for pack expansions in the parameters. */
21895 for (i = 0; i < len; ++i)
21896 {
21897 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21898 {
21899 if (i == len - 1)
21900 /* We can unify against something with a trailing
21901 parameter pack. */
21902 parm_variadic_p = 1;
21903 else
21904 /* [temp.deduct.type]/9: If the template argument list of
21905 P contains a pack expansion that is not the last
21906 template argument, the entire template argument list
21907 is a non-deduced context. */
21908 return unify_success (explain_p);
21909 }
21910 }
21911
21912 /* If we don't have enough arguments to satisfy the parameters
21913 (not counting the pack expression at the end), or we have
21914 too many arguments for a parameter list that doesn't end in
21915 a pack expression, we can't unify. */
21916 if (parm_variadic_p
21917 ? argslen < len - parm_variadic_p
21918 : argslen != len)
21919 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21920
21921 /* Unify all of the parameters that precede the (optional)
21922 pack expression. */
21923 for (i = 0; i < len - parm_variadic_p; ++i)
21924 {
21925 RECUR_AND_CHECK_FAILURE (tparms, targs,
21926 TREE_VEC_ELT (parm, i),
21927 TREE_VEC_ELT (arg, i),
21928 UNIFY_ALLOW_NONE, explain_p);
21929 }
21930 if (parm_variadic_p)
21931 return unify_pack_expansion (tparms, targs, parm, arg,
21932 DEDUCE_EXACT,
21933 /*subr=*/true, explain_p);
21934 return unify_success (explain_p);
21935 }
21936
21937 case RECORD_TYPE:
21938 case UNION_TYPE:
21939 if (TREE_CODE (arg) != TREE_CODE (parm))
21940 return unify_type_mismatch (explain_p, parm, arg);
21941
21942 if (TYPE_PTRMEMFUNC_P (parm))
21943 {
21944 if (!TYPE_PTRMEMFUNC_P (arg))
21945 return unify_type_mismatch (explain_p, parm, arg);
21946
21947 return unify (tparms, targs,
21948 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21949 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21950 strict, explain_p);
21951 }
21952 else if (TYPE_PTRMEMFUNC_P (arg))
21953 return unify_type_mismatch (explain_p, parm, arg);
21954
21955 if (CLASSTYPE_TEMPLATE_INFO (parm))
21956 {
21957 tree t = NULL_TREE;
21958
21959 if (strict_in & UNIFY_ALLOW_DERIVED)
21960 {
21961 /* First, we try to unify the PARM and ARG directly. */
21962 t = try_class_unification (tparms, targs,
21963 parm, arg, explain_p);
21964
21965 if (!t)
21966 {
21967 /* Fallback to the special case allowed in
21968 [temp.deduct.call]:
21969
21970 If P is a class, and P has the form
21971 template-id, then A can be a derived class of
21972 the deduced A. Likewise, if P is a pointer to
21973 a class of the form template-id, A can be a
21974 pointer to a derived class pointed to by the
21975 deduced A. */
21976 enum template_base_result r;
21977 r = get_template_base (tparms, targs, parm, arg,
21978 explain_p, &t);
21979
21980 if (!t)
21981 {
21982 /* Don't give the derived diagnostic if we're
21983 already dealing with the same template. */
21984 bool same_template
21985 = (CLASSTYPE_TEMPLATE_INFO (arg)
21986 && (CLASSTYPE_TI_TEMPLATE (parm)
21987 == CLASSTYPE_TI_TEMPLATE (arg)));
21988 return unify_no_common_base (explain_p && !same_template,
21989 r, parm, arg);
21990 }
21991 }
21992 }
21993 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21994 && (CLASSTYPE_TI_TEMPLATE (parm)
21995 == CLASSTYPE_TI_TEMPLATE (arg)))
21996 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21997 Then, we should unify `int' and `U'. */
21998 t = arg;
21999 else
22000 /* There's no chance of unification succeeding. */
22001 return unify_type_mismatch (explain_p, parm, arg);
22002
22003 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22004 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22005 }
22006 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22007 return unify_type_mismatch (explain_p, parm, arg);
22008 return unify_success (explain_p);
22009
22010 case METHOD_TYPE:
22011 case FUNCTION_TYPE:
22012 {
22013 unsigned int nargs;
22014 tree *args;
22015 tree a;
22016 unsigned int i;
22017
22018 if (TREE_CODE (arg) != TREE_CODE (parm))
22019 return unify_type_mismatch (explain_p, parm, arg);
22020
22021 /* CV qualifications for methods can never be deduced, they must
22022 match exactly. We need to check them explicitly here,
22023 because type_unification_real treats them as any other
22024 cv-qualified parameter. */
22025 if (TREE_CODE (parm) == METHOD_TYPE
22026 && (!check_cv_quals_for_unify
22027 (UNIFY_ALLOW_NONE,
22028 class_of_this_parm (arg),
22029 class_of_this_parm (parm))))
22030 return unify_cv_qual_mismatch (explain_p, parm, arg);
22031 if (TREE_CODE (arg) == FUNCTION_TYPE
22032 && type_memfn_quals (parm) != type_memfn_quals (arg))
22033 return unify_cv_qual_mismatch (explain_p, parm, arg);
22034 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22035 return unify_type_mismatch (explain_p, parm, arg);
22036
22037 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22038 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22039
22040 nargs = list_length (TYPE_ARG_TYPES (arg));
22041 args = XALLOCAVEC (tree, nargs);
22042 for (a = TYPE_ARG_TYPES (arg), i = 0;
22043 a != NULL_TREE && a != void_list_node;
22044 a = TREE_CHAIN (a), ++i)
22045 args[i] = TREE_VALUE (a);
22046 nargs = i;
22047
22048 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22049 args, nargs, 1, DEDUCE_EXACT,
22050 NULL, explain_p))
22051 return 1;
22052
22053 if (flag_noexcept_type)
22054 {
22055 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22056 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22057 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22058 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22059 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22060 && uses_template_parms (TREE_PURPOSE (pspec)))
22061 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22062 TREE_PURPOSE (aspec),
22063 UNIFY_ALLOW_NONE, explain_p);
22064 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22065 return unify_type_mismatch (explain_p, parm, arg);
22066 }
22067
22068 return 0;
22069 }
22070
22071 case OFFSET_TYPE:
22072 /* Unify a pointer to member with a pointer to member function, which
22073 deduces the type of the member as a function type. */
22074 if (TYPE_PTRMEMFUNC_P (arg))
22075 {
22076 /* Check top-level cv qualifiers */
22077 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22078 return unify_cv_qual_mismatch (explain_p, parm, arg);
22079
22080 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22081 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22082 UNIFY_ALLOW_NONE, explain_p);
22083
22084 /* Determine the type of the function we are unifying against. */
22085 tree fntype = static_fn_type (arg);
22086
22087 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22088 }
22089
22090 if (TREE_CODE (arg) != OFFSET_TYPE)
22091 return unify_type_mismatch (explain_p, parm, arg);
22092 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22093 TYPE_OFFSET_BASETYPE (arg),
22094 UNIFY_ALLOW_NONE, explain_p);
22095 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22096 strict, explain_p);
22097
22098 case CONST_DECL:
22099 if (DECL_TEMPLATE_PARM_P (parm))
22100 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22101 if (arg != scalar_constant_value (parm))
22102 return unify_template_argument_mismatch (explain_p, parm, arg);
22103 return unify_success (explain_p);
22104
22105 case FIELD_DECL:
22106 case TEMPLATE_DECL:
22107 /* Matched cases are handled by the ARG == PARM test above. */
22108 return unify_template_argument_mismatch (explain_p, parm, arg);
22109
22110 case VAR_DECL:
22111 /* We might get a variable as a non-type template argument in parm if the
22112 corresponding parameter is type-dependent. Make any necessary
22113 adjustments based on whether arg is a reference. */
22114 if (CONSTANT_CLASS_P (arg))
22115 parm = fold_non_dependent_expr (parm, complain);
22116 else if (REFERENCE_REF_P (arg))
22117 {
22118 tree sub = TREE_OPERAND (arg, 0);
22119 STRIP_NOPS (sub);
22120 if (TREE_CODE (sub) == ADDR_EXPR)
22121 arg = TREE_OPERAND (sub, 0);
22122 }
22123 /* Now use the normal expression code to check whether they match. */
22124 goto expr;
22125
22126 case TYPE_ARGUMENT_PACK:
22127 case NONTYPE_ARGUMENT_PACK:
22128 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22129 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22130
22131 case TYPEOF_TYPE:
22132 case DECLTYPE_TYPE:
22133 case UNDERLYING_TYPE:
22134 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22135 or UNDERLYING_TYPE nodes. */
22136 return unify_success (explain_p);
22137
22138 case ERROR_MARK:
22139 /* Unification fails if we hit an error node. */
22140 return unify_invalid (explain_p);
22141
22142 case INDIRECT_REF:
22143 if (REFERENCE_REF_P (parm))
22144 {
22145 bool pexp = PACK_EXPANSION_P (arg);
22146 if (pexp)
22147 arg = PACK_EXPANSION_PATTERN (arg);
22148 if (REFERENCE_REF_P (arg))
22149 arg = TREE_OPERAND (arg, 0);
22150 if (pexp)
22151 arg = make_pack_expansion (arg, complain);
22152 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22153 strict, explain_p);
22154 }
22155 /* FALLTHRU */
22156
22157 default:
22158 /* An unresolved overload is a nondeduced context. */
22159 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22160 return unify_success (explain_p);
22161 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22162 expr:
22163 /* We must be looking at an expression. This can happen with
22164 something like:
22165
22166 template <int I>
22167 void foo(S<I>, S<I + 2>);
22168
22169 This is a "nondeduced context":
22170
22171 [deduct.type]
22172
22173 The nondeduced contexts are:
22174
22175 --A type that is a template-id in which one or more of
22176 the template-arguments is an expression that references
22177 a template-parameter.
22178
22179 In these cases, we assume deduction succeeded, but don't
22180 actually infer any unifications. */
22181
22182 if (!uses_template_parms (parm)
22183 && !template_args_equal (parm, arg))
22184 return unify_expression_unequal (explain_p, parm, arg);
22185 else
22186 return unify_success (explain_p);
22187 }
22188 }
22189 #undef RECUR_AND_CHECK_FAILURE
22190 \f
22191 /* Note that DECL can be defined in this translation unit, if
22192 required. */
22193
22194 static void
22195 mark_definable (tree decl)
22196 {
22197 tree clone;
22198 DECL_NOT_REALLY_EXTERN (decl) = 1;
22199 FOR_EACH_CLONE (clone, decl)
22200 DECL_NOT_REALLY_EXTERN (clone) = 1;
22201 }
22202
22203 /* Called if RESULT is explicitly instantiated, or is a member of an
22204 explicitly instantiated class. */
22205
22206 void
22207 mark_decl_instantiated (tree result, int extern_p)
22208 {
22209 SET_DECL_EXPLICIT_INSTANTIATION (result);
22210
22211 /* If this entity has already been written out, it's too late to
22212 make any modifications. */
22213 if (TREE_ASM_WRITTEN (result))
22214 return;
22215
22216 /* For anonymous namespace we don't need to do anything. */
22217 if (decl_anon_ns_mem_p (result))
22218 {
22219 gcc_assert (!TREE_PUBLIC (result));
22220 return;
22221 }
22222
22223 if (TREE_CODE (result) != FUNCTION_DECL)
22224 /* The TREE_PUBLIC flag for function declarations will have been
22225 set correctly by tsubst. */
22226 TREE_PUBLIC (result) = 1;
22227
22228 /* This might have been set by an earlier implicit instantiation. */
22229 DECL_COMDAT (result) = 0;
22230
22231 if (extern_p)
22232 DECL_NOT_REALLY_EXTERN (result) = 0;
22233 else
22234 {
22235 mark_definable (result);
22236 mark_needed (result);
22237 /* Always make artificials weak. */
22238 if (DECL_ARTIFICIAL (result) && flag_weak)
22239 comdat_linkage (result);
22240 /* For WIN32 we also want to put explicit instantiations in
22241 linkonce sections. */
22242 else if (TREE_PUBLIC (result))
22243 maybe_make_one_only (result);
22244 if (TREE_CODE (result) == FUNCTION_DECL
22245 && DECL_TEMPLATE_INSTANTIATED (result))
22246 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22247 since start_preparsed_function wouldn't have if we had an earlier
22248 extern explicit instantiation. */
22249 DECL_EXTERNAL (result) = 0;
22250 }
22251
22252 /* If EXTERN_P, then this function will not be emitted -- unless
22253 followed by an explicit instantiation, at which point its linkage
22254 will be adjusted. If !EXTERN_P, then this function will be
22255 emitted here. In neither circumstance do we want
22256 import_export_decl to adjust the linkage. */
22257 DECL_INTERFACE_KNOWN (result) = 1;
22258 }
22259
22260 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22261 important template arguments. If any are missing, we check whether
22262 they're important by using error_mark_node for substituting into any
22263 args that were used for partial ordering (the ones between ARGS and END)
22264 and seeing if it bubbles up. */
22265
22266 static bool
22267 check_undeduced_parms (tree targs, tree args, tree end)
22268 {
22269 bool found = false;
22270 int i;
22271 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22272 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22273 {
22274 found = true;
22275 TREE_VEC_ELT (targs, i) = error_mark_node;
22276 }
22277 if (found)
22278 {
22279 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22280 if (substed == error_mark_node)
22281 return true;
22282 }
22283 return false;
22284 }
22285
22286 /* Given two function templates PAT1 and PAT2, return:
22287
22288 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22289 -1 if PAT2 is more specialized than PAT1.
22290 0 if neither is more specialized.
22291
22292 LEN indicates the number of parameters we should consider
22293 (defaulted parameters should not be considered).
22294
22295 The 1998 std underspecified function template partial ordering, and
22296 DR214 addresses the issue. We take pairs of arguments, one from
22297 each of the templates, and deduce them against each other. One of
22298 the templates will be more specialized if all the *other*
22299 template's arguments deduce against its arguments and at least one
22300 of its arguments *does* *not* deduce against the other template's
22301 corresponding argument. Deduction is done as for class templates.
22302 The arguments used in deduction have reference and top level cv
22303 qualifiers removed. Iff both arguments were originally reference
22304 types *and* deduction succeeds in both directions, an lvalue reference
22305 wins against an rvalue reference and otherwise the template
22306 with the more cv-qualified argument wins for that pairing (if
22307 neither is more cv-qualified, they both are equal). Unlike regular
22308 deduction, after all the arguments have been deduced in this way,
22309 we do *not* verify the deduced template argument values can be
22310 substituted into non-deduced contexts.
22311
22312 The logic can be a bit confusing here, because we look at deduce1 and
22313 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22314 can find template arguments for pat1 to make arg1 look like arg2, that
22315 means that arg2 is at least as specialized as arg1. */
22316
22317 int
22318 more_specialized_fn (tree pat1, tree pat2, int len)
22319 {
22320 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22321 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22322 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22323 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22324 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22325 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22326 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22327 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22328 tree origs1, origs2;
22329 bool lose1 = false;
22330 bool lose2 = false;
22331
22332 /* Remove the this parameter from non-static member functions. If
22333 one is a non-static member function and the other is not a static
22334 member function, remove the first parameter from that function
22335 also. This situation occurs for operator functions where we
22336 locate both a member function (with this pointer) and non-member
22337 operator (with explicit first operand). */
22338 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22339 {
22340 len--; /* LEN is the number of significant arguments for DECL1 */
22341 args1 = TREE_CHAIN (args1);
22342 if (!DECL_STATIC_FUNCTION_P (decl2))
22343 args2 = TREE_CHAIN (args2);
22344 }
22345 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22346 {
22347 args2 = TREE_CHAIN (args2);
22348 if (!DECL_STATIC_FUNCTION_P (decl1))
22349 {
22350 len--;
22351 args1 = TREE_CHAIN (args1);
22352 }
22353 }
22354
22355 /* If only one is a conversion operator, they are unordered. */
22356 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22357 return 0;
22358
22359 /* Consider the return type for a conversion function */
22360 if (DECL_CONV_FN_P (decl1))
22361 {
22362 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22363 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22364 len++;
22365 }
22366
22367 processing_template_decl++;
22368
22369 origs1 = args1;
22370 origs2 = args2;
22371
22372 while (len--
22373 /* Stop when an ellipsis is seen. */
22374 && args1 != NULL_TREE && args2 != NULL_TREE)
22375 {
22376 tree arg1 = TREE_VALUE (args1);
22377 tree arg2 = TREE_VALUE (args2);
22378 int deduce1, deduce2;
22379 int quals1 = -1;
22380 int quals2 = -1;
22381 int ref1 = 0;
22382 int ref2 = 0;
22383
22384 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22385 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22386 {
22387 /* When both arguments are pack expansions, we need only
22388 unify the patterns themselves. */
22389 arg1 = PACK_EXPANSION_PATTERN (arg1);
22390 arg2 = PACK_EXPANSION_PATTERN (arg2);
22391
22392 /* This is the last comparison we need to do. */
22393 len = 0;
22394 }
22395
22396 /* DR 1847: If a particular P contains no template-parameters that
22397 participate in template argument deduction, that P is not used to
22398 determine the ordering. */
22399 if (!uses_deducible_template_parms (arg1)
22400 && !uses_deducible_template_parms (arg2))
22401 goto next;
22402
22403 if (TYPE_REF_P (arg1))
22404 {
22405 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
22406 arg1 = TREE_TYPE (arg1);
22407 quals1 = cp_type_quals (arg1);
22408 }
22409
22410 if (TYPE_REF_P (arg2))
22411 {
22412 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22413 arg2 = TREE_TYPE (arg2);
22414 quals2 = cp_type_quals (arg2);
22415 }
22416
22417 arg1 = TYPE_MAIN_VARIANT (arg1);
22418 arg2 = TYPE_MAIN_VARIANT (arg2);
22419
22420 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22421 {
22422 int i, len2 = remaining_arguments (args2);
22423 tree parmvec = make_tree_vec (1);
22424 tree argvec = make_tree_vec (len2);
22425 tree ta = args2;
22426
22427 /* Setup the parameter vector, which contains only ARG1. */
22428 TREE_VEC_ELT (parmvec, 0) = arg1;
22429
22430 /* Setup the argument vector, which contains the remaining
22431 arguments. */
22432 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22433 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22434
22435 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22436 argvec, DEDUCE_EXACT,
22437 /*subr=*/true, /*explain_p=*/false)
22438 == 0);
22439
22440 /* We cannot deduce in the other direction, because ARG1 is
22441 a pack expansion but ARG2 is not. */
22442 deduce2 = 0;
22443 }
22444 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22445 {
22446 int i, len1 = remaining_arguments (args1);
22447 tree parmvec = make_tree_vec (1);
22448 tree argvec = make_tree_vec (len1);
22449 tree ta = args1;
22450
22451 /* Setup the parameter vector, which contains only ARG1. */
22452 TREE_VEC_ELT (parmvec, 0) = arg2;
22453
22454 /* Setup the argument vector, which contains the remaining
22455 arguments. */
22456 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22457 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22458
22459 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22460 argvec, DEDUCE_EXACT,
22461 /*subr=*/true, /*explain_p=*/false)
22462 == 0);
22463
22464 /* We cannot deduce in the other direction, because ARG2 is
22465 a pack expansion but ARG1 is not.*/
22466 deduce1 = 0;
22467 }
22468
22469 else
22470 {
22471 /* The normal case, where neither argument is a pack
22472 expansion. */
22473 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22474 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22475 == 0);
22476 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22477 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22478 == 0);
22479 }
22480
22481 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22482 arg2, then arg2 is not as specialized as arg1. */
22483 if (!deduce1)
22484 lose2 = true;
22485 if (!deduce2)
22486 lose1 = true;
22487
22488 /* "If, for a given type, deduction succeeds in both directions
22489 (i.e., the types are identical after the transformations above)
22490 and both P and A were reference types (before being replaced with
22491 the type referred to above):
22492 - if the type from the argument template was an lvalue reference and
22493 the type from the parameter template was not, the argument type is
22494 considered to be more specialized than the other; otherwise,
22495 - if the type from the argument template is more cv-qualified
22496 than the type from the parameter template (as described above),
22497 the argument type is considered to be more specialized than the other;
22498 otherwise,
22499 - neither type is more specialized than the other." */
22500
22501 if (deduce1 && deduce2)
22502 {
22503 if (ref1 && ref2 && ref1 != ref2)
22504 {
22505 if (ref1 > ref2)
22506 lose1 = true;
22507 else
22508 lose2 = true;
22509 }
22510 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22511 {
22512 if ((quals1 & quals2) == quals2)
22513 lose2 = true;
22514 if ((quals1 & quals2) == quals1)
22515 lose1 = true;
22516 }
22517 }
22518
22519 if (lose1 && lose2)
22520 /* We've failed to deduce something in either direction.
22521 These must be unordered. */
22522 break;
22523
22524 next:
22525
22526 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22527 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22528 /* We have already processed all of the arguments in our
22529 handing of the pack expansion type. */
22530 len = 0;
22531
22532 args1 = TREE_CHAIN (args1);
22533 args2 = TREE_CHAIN (args2);
22534 }
22535
22536 /* "In most cases, all template parameters must have values in order for
22537 deduction to succeed, but for partial ordering purposes a template
22538 parameter may remain without a value provided it is not used in the
22539 types being used for partial ordering."
22540
22541 Thus, if we are missing any of the targs1 we need to substitute into
22542 origs1, then pat2 is not as specialized as pat1. This can happen when
22543 there is a nondeduced context. */
22544 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22545 lose2 = true;
22546 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22547 lose1 = true;
22548
22549 processing_template_decl--;
22550
22551 /* If both deductions succeed, the partial ordering selects the more
22552 constrained template. */
22553 if (!lose1 && !lose2)
22554 {
22555 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22556 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22557 lose1 = !subsumes_constraints (c1, c2);
22558 lose2 = !subsumes_constraints (c2, c1);
22559 }
22560
22561 /* All things being equal, if the next argument is a pack expansion
22562 for one function but not for the other, prefer the
22563 non-variadic function. FIXME this is bogus; see c++/41958. */
22564 if (lose1 == lose2
22565 && args1 && TREE_VALUE (args1)
22566 && args2 && TREE_VALUE (args2))
22567 {
22568 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22569 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22570 }
22571
22572 if (lose1 == lose2)
22573 return 0;
22574 else if (!lose1)
22575 return 1;
22576 else
22577 return -1;
22578 }
22579
22580 /* Determine which of two partial specializations of TMPL is more
22581 specialized.
22582
22583 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22584 to the first partial specialization. The TREE_PURPOSE is the
22585 innermost set of template parameters for the partial
22586 specialization. PAT2 is similar, but for the second template.
22587
22588 Return 1 if the first partial specialization is more specialized;
22589 -1 if the second is more specialized; 0 if neither is more
22590 specialized.
22591
22592 See [temp.class.order] for information about determining which of
22593 two templates is more specialized. */
22594
22595 static int
22596 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22597 {
22598 tree targs;
22599 int winner = 0;
22600 bool any_deductions = false;
22601
22602 tree tmpl1 = TREE_VALUE (pat1);
22603 tree tmpl2 = TREE_VALUE (pat2);
22604 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22605 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22606
22607 /* Just like what happens for functions, if we are ordering between
22608 different template specializations, we may encounter dependent
22609 types in the arguments, and we need our dependency check functions
22610 to behave correctly. */
22611 ++processing_template_decl;
22612 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22613 if (targs)
22614 {
22615 --winner;
22616 any_deductions = true;
22617 }
22618
22619 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22620 if (targs)
22621 {
22622 ++winner;
22623 any_deductions = true;
22624 }
22625 --processing_template_decl;
22626
22627 /* If both deductions succeed, the partial ordering selects the more
22628 constrained template. */
22629 if (!winner && any_deductions)
22630 return more_constrained (tmpl1, tmpl2);
22631
22632 /* In the case of a tie where at least one of the templates
22633 has a parameter pack at the end, the template with the most
22634 non-packed parameters wins. */
22635 if (winner == 0
22636 && any_deductions
22637 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22638 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22639 {
22640 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22641 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22642 int len1 = TREE_VEC_LENGTH (args1);
22643 int len2 = TREE_VEC_LENGTH (args2);
22644
22645 /* We don't count the pack expansion at the end. */
22646 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22647 --len1;
22648 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22649 --len2;
22650
22651 if (len1 > len2)
22652 return 1;
22653 else if (len1 < len2)
22654 return -1;
22655 }
22656
22657 return winner;
22658 }
22659
22660 /* Return the template arguments that will produce the function signature
22661 DECL from the function template FN, with the explicit template
22662 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22663 also match. Return NULL_TREE if no satisfactory arguments could be
22664 found. */
22665
22666 static tree
22667 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22668 {
22669 int ntparms = DECL_NTPARMS (fn);
22670 tree targs = make_tree_vec (ntparms);
22671 tree decl_type = TREE_TYPE (decl);
22672 tree decl_arg_types;
22673 tree *args;
22674 unsigned int nargs, ix;
22675 tree arg;
22676
22677 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22678
22679 /* Never do unification on the 'this' parameter. */
22680 decl_arg_types = skip_artificial_parms_for (decl,
22681 TYPE_ARG_TYPES (decl_type));
22682
22683 nargs = list_length (decl_arg_types);
22684 args = XALLOCAVEC (tree, nargs);
22685 for (arg = decl_arg_types, ix = 0;
22686 arg != NULL_TREE && arg != void_list_node;
22687 arg = TREE_CHAIN (arg), ++ix)
22688 args[ix] = TREE_VALUE (arg);
22689
22690 if (fn_type_unification (fn, explicit_args, targs,
22691 args, ix,
22692 (check_rettype || DECL_CONV_FN_P (fn)
22693 ? TREE_TYPE (decl_type) : NULL_TREE),
22694 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
22695 /*explain_p=*/false,
22696 /*decltype*/false)
22697 == error_mark_node)
22698 return NULL_TREE;
22699
22700 return targs;
22701 }
22702
22703 /* Return the innermost template arguments that, when applied to a partial
22704 specialization SPEC_TMPL of TMPL, yield the ARGS.
22705
22706 For example, suppose we have:
22707
22708 template <class T, class U> struct S {};
22709 template <class T> struct S<T*, int> {};
22710
22711 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22712 partial specialization and the ARGS will be {double*, int}. The resulting
22713 vector will be {double}, indicating that `T' is bound to `double'. */
22714
22715 static tree
22716 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22717 {
22718 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22719 tree spec_args
22720 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22721 int i, ntparms = TREE_VEC_LENGTH (tparms);
22722 tree deduced_args;
22723 tree innermost_deduced_args;
22724
22725 innermost_deduced_args = make_tree_vec (ntparms);
22726 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22727 {
22728 deduced_args = copy_node (args);
22729 SET_TMPL_ARGS_LEVEL (deduced_args,
22730 TMPL_ARGS_DEPTH (deduced_args),
22731 innermost_deduced_args);
22732 }
22733 else
22734 deduced_args = innermost_deduced_args;
22735
22736 bool tried_array_deduction = (cxx_dialect < cxx17);
22737 again:
22738 if (unify (tparms, deduced_args,
22739 INNERMOST_TEMPLATE_ARGS (spec_args),
22740 INNERMOST_TEMPLATE_ARGS (args),
22741 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22742 return NULL_TREE;
22743
22744 for (i = 0; i < ntparms; ++i)
22745 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22746 {
22747 if (!tried_array_deduction)
22748 {
22749 try_array_deduction (tparms, innermost_deduced_args,
22750 INNERMOST_TEMPLATE_ARGS (spec_args));
22751 tried_array_deduction = true;
22752 if (TREE_VEC_ELT (innermost_deduced_args, i))
22753 goto again;
22754 }
22755 return NULL_TREE;
22756 }
22757
22758 if (!push_tinst_level (spec_tmpl, deduced_args))
22759 {
22760 excessive_deduction_depth = true;
22761 return NULL_TREE;
22762 }
22763
22764 /* Verify that nondeduced template arguments agree with the type
22765 obtained from argument deduction.
22766
22767 For example:
22768
22769 struct A { typedef int X; };
22770 template <class T, class U> struct C {};
22771 template <class T> struct C<T, typename T::X> {};
22772
22773 Then with the instantiation `C<A, int>', we can deduce that
22774 `T' is `A' but unify () does not check whether `typename T::X'
22775 is `int'. */
22776 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22777
22778 if (spec_args != error_mark_node)
22779 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22780 INNERMOST_TEMPLATE_ARGS (spec_args),
22781 tmpl, tf_none, false, false);
22782
22783 pop_tinst_level ();
22784
22785 if (spec_args == error_mark_node
22786 /* We only need to check the innermost arguments; the other
22787 arguments will always agree. */
22788 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22789 INNERMOST_TEMPLATE_ARGS (args)))
22790 return NULL_TREE;
22791
22792 /* Now that we have bindings for all of the template arguments,
22793 ensure that the arguments deduced for the template template
22794 parameters have compatible template parameter lists. See the use
22795 of template_template_parm_bindings_ok_p in fn_type_unification
22796 for more information. */
22797 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22798 return NULL_TREE;
22799
22800 return deduced_args;
22801 }
22802
22803 // Compare two function templates T1 and T2 by deducing bindings
22804 // from one against the other. If both deductions succeed, compare
22805 // constraints to see which is more constrained.
22806 static int
22807 more_specialized_inst (tree t1, tree t2)
22808 {
22809 int fate = 0;
22810 int count = 0;
22811
22812 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22813 {
22814 --fate;
22815 ++count;
22816 }
22817
22818 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22819 {
22820 ++fate;
22821 ++count;
22822 }
22823
22824 // If both deductions succeed, then one may be more constrained.
22825 if (count == 2 && fate == 0)
22826 fate = more_constrained (t1, t2);
22827
22828 return fate;
22829 }
22830
22831 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22832 Return the TREE_LIST node with the most specialized template, if
22833 any. If there is no most specialized template, the error_mark_node
22834 is returned.
22835
22836 Note that this function does not look at, or modify, the
22837 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22838 returned is one of the elements of INSTANTIATIONS, callers may
22839 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22840 and retrieve it from the value returned. */
22841
22842 tree
22843 most_specialized_instantiation (tree templates)
22844 {
22845 tree fn, champ;
22846
22847 ++processing_template_decl;
22848
22849 champ = templates;
22850 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22851 {
22852 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22853 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22854 if (fate == -1)
22855 champ = fn;
22856 else if (!fate)
22857 {
22858 /* Equally specialized, move to next function. If there
22859 is no next function, nothing's most specialized. */
22860 fn = TREE_CHAIN (fn);
22861 champ = fn;
22862 if (!fn)
22863 break;
22864 }
22865 }
22866
22867 if (champ)
22868 /* Now verify that champ is better than everything earlier in the
22869 instantiation list. */
22870 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22871 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22872 {
22873 champ = NULL_TREE;
22874 break;
22875 }
22876 }
22877
22878 processing_template_decl--;
22879
22880 if (!champ)
22881 return error_mark_node;
22882
22883 return champ;
22884 }
22885
22886 /* If DECL is a specialization of some template, return the most
22887 general such template. Otherwise, returns NULL_TREE.
22888
22889 For example, given:
22890
22891 template <class T> struct S { template <class U> void f(U); };
22892
22893 if TMPL is `template <class U> void S<int>::f(U)' this will return
22894 the full template. This function will not trace past partial
22895 specializations, however. For example, given in addition:
22896
22897 template <class T> struct S<T*> { template <class U> void f(U); };
22898
22899 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22900 `template <class T> template <class U> S<T*>::f(U)'. */
22901
22902 tree
22903 most_general_template (tree decl)
22904 {
22905 if (TREE_CODE (decl) != TEMPLATE_DECL)
22906 {
22907 if (tree tinfo = get_template_info (decl))
22908 decl = TI_TEMPLATE (tinfo);
22909 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22910 template friend, or a FIELD_DECL for a capture pack. */
22911 if (TREE_CODE (decl) != TEMPLATE_DECL)
22912 return NULL_TREE;
22913 }
22914
22915 /* Look for more and more general templates. */
22916 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22917 {
22918 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22919 (See cp-tree.h for details.) */
22920 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22921 break;
22922
22923 if (CLASS_TYPE_P (TREE_TYPE (decl))
22924 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22925 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22926 break;
22927
22928 /* Stop if we run into an explicitly specialized class template. */
22929 if (!DECL_NAMESPACE_SCOPE_P (decl)
22930 && DECL_CONTEXT (decl)
22931 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22932 break;
22933
22934 decl = DECL_TI_TEMPLATE (decl);
22935 }
22936
22937 return decl;
22938 }
22939
22940 /* Return the most specialized of the template partial specializations
22941 which can produce TARGET, a specialization of some class or variable
22942 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22943 a TEMPLATE_DECL node corresponding to the partial specialization, while
22944 the TREE_PURPOSE is the set of template arguments that must be
22945 substituted into the template pattern in order to generate TARGET.
22946
22947 If the choice of partial specialization is ambiguous, a diagnostic
22948 is issued, and the error_mark_node is returned. If there are no
22949 partial specializations matching TARGET, then NULL_TREE is
22950 returned, indicating that the primary template should be used. */
22951
22952 static tree
22953 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22954 {
22955 tree list = NULL_TREE;
22956 tree t;
22957 tree champ;
22958 int fate;
22959 bool ambiguous_p;
22960 tree outer_args = NULL_TREE;
22961 tree tmpl, args;
22962
22963 if (TYPE_P (target))
22964 {
22965 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22966 tmpl = TI_TEMPLATE (tinfo);
22967 args = TI_ARGS (tinfo);
22968 }
22969 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22970 {
22971 tmpl = TREE_OPERAND (target, 0);
22972 args = TREE_OPERAND (target, 1);
22973 }
22974 else if (VAR_P (target))
22975 {
22976 tree tinfo = DECL_TEMPLATE_INFO (target);
22977 tmpl = TI_TEMPLATE (tinfo);
22978 args = TI_ARGS (tinfo);
22979 }
22980 else
22981 gcc_unreachable ();
22982
22983 tree main_tmpl = most_general_template (tmpl);
22984
22985 /* For determining which partial specialization to use, only the
22986 innermost args are interesting. */
22987 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22988 {
22989 outer_args = strip_innermost_template_args (args, 1);
22990 args = INNERMOST_TEMPLATE_ARGS (args);
22991 }
22992
22993 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22994 {
22995 tree spec_args;
22996 tree spec_tmpl = TREE_VALUE (t);
22997
22998 if (outer_args)
22999 {
23000 /* Substitute in the template args from the enclosing class. */
23001 ++processing_template_decl;
23002 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23003 --processing_template_decl;
23004 }
23005
23006 if (spec_tmpl == error_mark_node)
23007 return error_mark_node;
23008
23009 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23010 if (spec_args)
23011 {
23012 if (outer_args)
23013 spec_args = add_to_template_args (outer_args, spec_args);
23014
23015 /* Keep the candidate only if the constraints are satisfied,
23016 or if we're not compiling with concepts. */
23017 if (!flag_concepts
23018 || constraints_satisfied_p (spec_tmpl, spec_args))
23019 {
23020 list = tree_cons (spec_args, TREE_VALUE (t), list);
23021 TREE_TYPE (list) = TREE_TYPE (t);
23022 }
23023 }
23024 }
23025
23026 if (! list)
23027 return NULL_TREE;
23028
23029 ambiguous_p = false;
23030 t = list;
23031 champ = t;
23032 t = TREE_CHAIN (t);
23033 for (; t; t = TREE_CHAIN (t))
23034 {
23035 fate = more_specialized_partial_spec (tmpl, champ, t);
23036 if (fate == 1)
23037 ;
23038 else
23039 {
23040 if (fate == 0)
23041 {
23042 t = TREE_CHAIN (t);
23043 if (! t)
23044 {
23045 ambiguous_p = true;
23046 break;
23047 }
23048 }
23049 champ = t;
23050 }
23051 }
23052
23053 if (!ambiguous_p)
23054 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23055 {
23056 fate = more_specialized_partial_spec (tmpl, champ, t);
23057 if (fate != 1)
23058 {
23059 ambiguous_p = true;
23060 break;
23061 }
23062 }
23063
23064 if (ambiguous_p)
23065 {
23066 const char *str;
23067 char *spaces = NULL;
23068 if (!(complain & tf_error))
23069 return error_mark_node;
23070 if (TYPE_P (target))
23071 error ("ambiguous template instantiation for %q#T", target);
23072 else
23073 error ("ambiguous template instantiation for %q#D", target);
23074 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23075 for (t = list; t; t = TREE_CHAIN (t))
23076 {
23077 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23078 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23079 "%s %#qS", spaces ? spaces : str, subst);
23080 spaces = spaces ? spaces : get_spaces (str);
23081 }
23082 free (spaces);
23083 return error_mark_node;
23084 }
23085
23086 return champ;
23087 }
23088
23089 /* Explicitly instantiate DECL. */
23090
23091 void
23092 do_decl_instantiation (tree decl, tree storage)
23093 {
23094 tree result = NULL_TREE;
23095 int extern_p = 0;
23096
23097 if (!decl || decl == error_mark_node)
23098 /* An error occurred, for which grokdeclarator has already issued
23099 an appropriate message. */
23100 return;
23101 else if (! DECL_LANG_SPECIFIC (decl))
23102 {
23103 error ("explicit instantiation of non-template %q#D", decl);
23104 return;
23105 }
23106
23107 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23108 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23109
23110 if (VAR_P (decl) && !var_templ)
23111 {
23112 /* There is an asymmetry here in the way VAR_DECLs and
23113 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23114 the latter, the DECL we get back will be marked as a
23115 template instantiation, and the appropriate
23116 DECL_TEMPLATE_INFO will be set up. This does not happen for
23117 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23118 should handle VAR_DECLs as it currently handles
23119 FUNCTION_DECLs. */
23120 if (!DECL_CLASS_SCOPE_P (decl))
23121 {
23122 error ("%qD is not a static data member of a class template", decl);
23123 return;
23124 }
23125 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23126 if (!result || !VAR_P (result))
23127 {
23128 error ("no matching template for %qD found", decl);
23129 return;
23130 }
23131 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23132 {
23133 error ("type %qT for explicit instantiation %qD does not match "
23134 "declared type %qT", TREE_TYPE (result), decl,
23135 TREE_TYPE (decl));
23136 return;
23137 }
23138 }
23139 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23140 {
23141 error ("explicit instantiation of %q#D", decl);
23142 return;
23143 }
23144 else
23145 result = decl;
23146
23147 /* Check for various error cases. Note that if the explicit
23148 instantiation is valid the RESULT will currently be marked as an
23149 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23150 until we get here. */
23151
23152 if (DECL_TEMPLATE_SPECIALIZATION (result))
23153 {
23154 /* DR 259 [temp.spec].
23155
23156 Both an explicit instantiation and a declaration of an explicit
23157 specialization shall not appear in a program unless the explicit
23158 instantiation follows a declaration of the explicit specialization.
23159
23160 For a given set of template parameters, if an explicit
23161 instantiation of a template appears after a declaration of an
23162 explicit specialization for that template, the explicit
23163 instantiation has no effect. */
23164 return;
23165 }
23166 else if (DECL_EXPLICIT_INSTANTIATION (result))
23167 {
23168 /* [temp.spec]
23169
23170 No program shall explicitly instantiate any template more
23171 than once.
23172
23173 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23174 the first instantiation was `extern' and the second is not,
23175 and EXTERN_P for the opposite case. */
23176 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23177 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23178 /* If an "extern" explicit instantiation follows an ordinary
23179 explicit instantiation, the template is instantiated. */
23180 if (extern_p)
23181 return;
23182 }
23183 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23184 {
23185 error ("no matching template for %qD found", result);
23186 return;
23187 }
23188 else if (!DECL_TEMPLATE_INFO (result))
23189 {
23190 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23191 return;
23192 }
23193
23194 if (storage == NULL_TREE)
23195 ;
23196 else if (storage == ridpointers[(int) RID_EXTERN])
23197 {
23198 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23199 pedwarn (input_location, OPT_Wpedantic,
23200 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23201 "instantiations");
23202 extern_p = 1;
23203 }
23204 else
23205 error ("storage class %qD applied to template instantiation", storage);
23206
23207 check_explicit_instantiation_namespace (result);
23208 mark_decl_instantiated (result, extern_p);
23209 if (! extern_p)
23210 instantiate_decl (result, /*defer_ok=*/true,
23211 /*expl_inst_class_mem_p=*/false);
23212 }
23213
23214 static void
23215 mark_class_instantiated (tree t, int extern_p)
23216 {
23217 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23218 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23219 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23220 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23221 if (! extern_p)
23222 {
23223 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23224 rest_of_type_compilation (t, 1);
23225 }
23226 }
23227
23228 /* Called from do_type_instantiation through binding_table_foreach to
23229 do recursive instantiation for the type bound in ENTRY. */
23230 static void
23231 bt_instantiate_type_proc (binding_entry entry, void *data)
23232 {
23233 tree storage = *(tree *) data;
23234
23235 if (MAYBE_CLASS_TYPE_P (entry->type)
23236 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23237 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23238 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23239 }
23240
23241 /* Perform an explicit instantiation of template class T. STORAGE, if
23242 non-null, is the RID for extern, inline or static. COMPLAIN is
23243 nonzero if this is called from the parser, zero if called recursively,
23244 since the standard is unclear (as detailed below). */
23245
23246 void
23247 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23248 {
23249 int extern_p = 0;
23250 int nomem_p = 0;
23251 int static_p = 0;
23252 int previous_instantiation_extern_p = 0;
23253
23254 if (TREE_CODE (t) == TYPE_DECL)
23255 t = TREE_TYPE (t);
23256
23257 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23258 {
23259 tree tmpl =
23260 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23261 if (tmpl)
23262 error ("explicit instantiation of non-class template %qD", tmpl);
23263 else
23264 error ("explicit instantiation of non-template type %qT", t);
23265 return;
23266 }
23267
23268 complete_type (t);
23269
23270 if (!COMPLETE_TYPE_P (t))
23271 {
23272 if (complain & tf_error)
23273 error ("explicit instantiation of %q#T before definition of template",
23274 t);
23275 return;
23276 }
23277
23278 if (storage != NULL_TREE)
23279 {
23280 if (!in_system_header_at (input_location))
23281 {
23282 if (storage == ridpointers[(int) RID_EXTERN])
23283 {
23284 if (cxx_dialect == cxx98)
23285 pedwarn (input_location, OPT_Wpedantic,
23286 "ISO C++ 1998 forbids the use of %<extern%> on "
23287 "explicit instantiations");
23288 }
23289 else
23290 pedwarn (input_location, OPT_Wpedantic,
23291 "ISO C++ forbids the use of %qE"
23292 " on explicit instantiations", storage);
23293 }
23294
23295 if (storage == ridpointers[(int) RID_INLINE])
23296 nomem_p = 1;
23297 else if (storage == ridpointers[(int) RID_EXTERN])
23298 extern_p = 1;
23299 else if (storage == ridpointers[(int) RID_STATIC])
23300 static_p = 1;
23301 else
23302 {
23303 error ("storage class %qD applied to template instantiation",
23304 storage);
23305 extern_p = 0;
23306 }
23307 }
23308
23309 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23310 {
23311 /* DR 259 [temp.spec].
23312
23313 Both an explicit instantiation and a declaration of an explicit
23314 specialization shall not appear in a program unless the explicit
23315 instantiation follows a declaration of the explicit specialization.
23316
23317 For a given set of template parameters, if an explicit
23318 instantiation of a template appears after a declaration of an
23319 explicit specialization for that template, the explicit
23320 instantiation has no effect. */
23321 return;
23322 }
23323 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23324 {
23325 /* [temp.spec]
23326
23327 No program shall explicitly instantiate any template more
23328 than once.
23329
23330 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23331 instantiation was `extern'. If EXTERN_P then the second is.
23332 These cases are OK. */
23333 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23334
23335 if (!previous_instantiation_extern_p && !extern_p
23336 && (complain & tf_error))
23337 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23338
23339 /* If we've already instantiated the template, just return now. */
23340 if (!CLASSTYPE_INTERFACE_ONLY (t))
23341 return;
23342 }
23343
23344 check_explicit_instantiation_namespace (TYPE_NAME (t));
23345 mark_class_instantiated (t, extern_p);
23346
23347 if (nomem_p)
23348 return;
23349
23350 /* In contrast to implicit instantiation, where only the
23351 declarations, and not the definitions, of members are
23352 instantiated, we have here:
23353
23354 [temp.explicit]
23355
23356 The explicit instantiation of a class template specialization
23357 implies the instantiation of all of its members not
23358 previously explicitly specialized in the translation unit
23359 containing the explicit instantiation.
23360
23361 Of course, we can't instantiate member template classes, since we
23362 don't have any arguments for them. Note that the standard is
23363 unclear on whether the instantiation of the members are
23364 *explicit* instantiations or not. However, the most natural
23365 interpretation is that it should be an explicit
23366 instantiation. */
23367 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23368 if ((VAR_P (fld)
23369 || (TREE_CODE (fld) == FUNCTION_DECL
23370 && !static_p
23371 && user_provided_p (fld)))
23372 && DECL_TEMPLATE_INSTANTIATION (fld))
23373 {
23374 mark_decl_instantiated (fld, extern_p);
23375 if (! extern_p)
23376 instantiate_decl (fld, /*defer_ok=*/true,
23377 /*expl_inst_class_mem_p=*/true);
23378 }
23379
23380 if (CLASSTYPE_NESTED_UTDS (t))
23381 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
23382 bt_instantiate_type_proc, &storage);
23383 }
23384
23385 /* Given a function DECL, which is a specialization of TMPL, modify
23386 DECL to be a re-instantiation of TMPL with the same template
23387 arguments. TMPL should be the template into which tsubst'ing
23388 should occur for DECL, not the most general template.
23389
23390 One reason for doing this is a scenario like this:
23391
23392 template <class T>
23393 void f(const T&, int i);
23394
23395 void g() { f(3, 7); }
23396
23397 template <class T>
23398 void f(const T& t, const int i) { }
23399
23400 Note that when the template is first instantiated, with
23401 instantiate_template, the resulting DECL will have no name for the
23402 first parameter, and the wrong type for the second. So, when we go
23403 to instantiate the DECL, we regenerate it. */
23404
23405 static void
23406 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
23407 {
23408 /* The arguments used to instantiate DECL, from the most general
23409 template. */
23410 tree code_pattern;
23411
23412 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
23413
23414 /* Make sure that we can see identifiers, and compute access
23415 correctly. */
23416 push_access_scope (decl);
23417
23418 if (TREE_CODE (decl) == FUNCTION_DECL)
23419 {
23420 tree decl_parm;
23421 tree pattern_parm;
23422 tree specs;
23423 int args_depth;
23424 int parms_depth;
23425
23426 args_depth = TMPL_ARGS_DEPTH (args);
23427 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23428 if (args_depth > parms_depth)
23429 args = get_innermost_template_args (args, parms_depth);
23430
23431 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23432 args, tf_error, NULL_TREE,
23433 /*defer_ok*/false);
23434 if (specs && specs != error_mark_node)
23435 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23436 specs);
23437
23438 /* Merge parameter declarations. */
23439 decl_parm = skip_artificial_parms_for (decl,
23440 DECL_ARGUMENTS (decl));
23441 pattern_parm
23442 = skip_artificial_parms_for (code_pattern,
23443 DECL_ARGUMENTS (code_pattern));
23444 while (decl_parm && !DECL_PACK_P (pattern_parm))
23445 {
23446 tree parm_type;
23447 tree attributes;
23448
23449 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23450 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23451 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23452 NULL_TREE);
23453 parm_type = type_decays_to (parm_type);
23454 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23455 TREE_TYPE (decl_parm) = parm_type;
23456 attributes = DECL_ATTRIBUTES (pattern_parm);
23457 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23458 {
23459 DECL_ATTRIBUTES (decl_parm) = attributes;
23460 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23461 }
23462 decl_parm = DECL_CHAIN (decl_parm);
23463 pattern_parm = DECL_CHAIN (pattern_parm);
23464 }
23465 /* Merge any parameters that match with the function parameter
23466 pack. */
23467 if (pattern_parm && DECL_PACK_P (pattern_parm))
23468 {
23469 int i, len;
23470 tree expanded_types;
23471 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23472 the parameters in this function parameter pack. */
23473 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23474 args, tf_error, NULL_TREE);
23475 len = TREE_VEC_LENGTH (expanded_types);
23476 for (i = 0; i < len; i++)
23477 {
23478 tree parm_type;
23479 tree attributes;
23480
23481 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23482 /* Rename the parameter to include the index. */
23483 DECL_NAME (decl_parm) =
23484 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23485 parm_type = TREE_VEC_ELT (expanded_types, i);
23486 parm_type = type_decays_to (parm_type);
23487 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23488 TREE_TYPE (decl_parm) = parm_type;
23489 attributes = DECL_ATTRIBUTES (pattern_parm);
23490 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23491 {
23492 DECL_ATTRIBUTES (decl_parm) = attributes;
23493 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23494 }
23495 decl_parm = DECL_CHAIN (decl_parm);
23496 }
23497 }
23498 /* Merge additional specifiers from the CODE_PATTERN. */
23499 if (DECL_DECLARED_INLINE_P (code_pattern)
23500 && !DECL_DECLARED_INLINE_P (decl))
23501 DECL_DECLARED_INLINE_P (decl) = 1;
23502 }
23503 else if (VAR_P (decl))
23504 {
23505 start_lambda_scope (decl);
23506 DECL_INITIAL (decl) =
23507 tsubst_expr (DECL_INITIAL (code_pattern), args,
23508 tf_error, DECL_TI_TEMPLATE (decl),
23509 /*integral_constant_expression_p=*/false);
23510 finish_lambda_scope ();
23511 if (VAR_HAD_UNKNOWN_BOUND (decl))
23512 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23513 tf_error, DECL_TI_TEMPLATE (decl));
23514 }
23515 else
23516 gcc_unreachable ();
23517
23518 pop_access_scope (decl);
23519 }
23520
23521 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23522 substituted to get DECL. */
23523
23524 tree
23525 template_for_substitution (tree decl)
23526 {
23527 tree tmpl = DECL_TI_TEMPLATE (decl);
23528
23529 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23530 for the instantiation. This is not always the most general
23531 template. Consider, for example:
23532
23533 template <class T>
23534 struct S { template <class U> void f();
23535 template <> void f<int>(); };
23536
23537 and an instantiation of S<double>::f<int>. We want TD to be the
23538 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23539 while (/* An instantiation cannot have a definition, so we need a
23540 more general template. */
23541 DECL_TEMPLATE_INSTANTIATION (tmpl)
23542 /* We must also deal with friend templates. Given:
23543
23544 template <class T> struct S {
23545 template <class U> friend void f() {};
23546 };
23547
23548 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23549 so far as the language is concerned, but that's still
23550 where we get the pattern for the instantiation from. On
23551 other hand, if the definition comes outside the class, say:
23552
23553 template <class T> struct S {
23554 template <class U> friend void f();
23555 };
23556 template <class U> friend void f() {}
23557
23558 we don't need to look any further. That's what the check for
23559 DECL_INITIAL is for. */
23560 || (TREE_CODE (decl) == FUNCTION_DECL
23561 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23562 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23563 {
23564 /* The present template, TD, should not be a definition. If it
23565 were a definition, we should be using it! Note that we
23566 cannot restructure the loop to just keep going until we find
23567 a template with a definition, since that might go too far if
23568 a specialization was declared, but not defined. */
23569
23570 /* Fetch the more general template. */
23571 tmpl = DECL_TI_TEMPLATE (tmpl);
23572 }
23573
23574 return tmpl;
23575 }
23576
23577 /* Returns true if we need to instantiate this template instance even if we
23578 know we aren't going to emit it. */
23579
23580 bool
23581 always_instantiate_p (tree decl)
23582 {
23583 /* We always instantiate inline functions so that we can inline them. An
23584 explicit instantiation declaration prohibits implicit instantiation of
23585 non-inline functions. With high levels of optimization, we would
23586 normally inline non-inline functions -- but we're not allowed to do
23587 that for "extern template" functions. Therefore, we check
23588 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23589 return ((TREE_CODE (decl) == FUNCTION_DECL
23590 && (DECL_DECLARED_INLINE_P (decl)
23591 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23592 /* And we need to instantiate static data members so that
23593 their initializers are available in integral constant
23594 expressions. */
23595 || (VAR_P (decl)
23596 && decl_maybe_constant_var_p (decl)));
23597 }
23598
23599 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23600 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23601 error, true otherwise. */
23602
23603 bool
23604 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23605 {
23606 tree fntype, spec, noex, clone;
23607
23608 /* Don't instantiate a noexcept-specification from template context. */
23609 if (processing_template_decl
23610 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
23611 return true;
23612
23613 if (DECL_CLONED_FUNCTION_P (fn))
23614 fn = DECL_CLONED_FUNCTION (fn);
23615 fntype = TREE_TYPE (fn);
23616 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23617
23618 if (!spec || !TREE_PURPOSE (spec))
23619 return true;
23620
23621 noex = TREE_PURPOSE (spec);
23622
23623 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23624 {
23625 static hash_set<tree>* fns = new hash_set<tree>;
23626 bool added = false;
23627 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23628 spec = get_defaulted_eh_spec (fn, complain);
23629 else if (!(added = !fns->add (fn)))
23630 {
23631 /* If hash_set::add returns true, the element was already there. */
23632 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
23633 DECL_SOURCE_LOCATION (fn));
23634 error_at (loc,
23635 "exception specification of %qD depends on itself",
23636 fn);
23637 spec = noexcept_false_spec;
23638 }
23639 else if (push_tinst_level (fn))
23640 {
23641 push_access_scope (fn);
23642 push_deferring_access_checks (dk_no_deferred);
23643 input_location = DECL_SOURCE_LOCATION (fn);
23644 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23645 DEFERRED_NOEXCEPT_ARGS (noex),
23646 tf_warning_or_error, fn,
23647 /*function_p=*/false,
23648 /*integral_constant_expression_p=*/true);
23649 spec = build_noexcept_spec (noex, tf_warning_or_error);
23650 pop_deferring_access_checks ();
23651 pop_access_scope (fn);
23652 pop_tinst_level ();
23653 if (spec == error_mark_node)
23654 spec = noexcept_false_spec;
23655 }
23656 else
23657 spec = noexcept_false_spec;
23658
23659 if (added)
23660 fns->remove (fn);
23661
23662 if (spec == error_mark_node)
23663 return false;
23664
23665 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23666 }
23667
23668 FOR_EACH_CLONE (clone, fn)
23669 {
23670 if (TREE_TYPE (clone) == fntype)
23671 TREE_TYPE (clone) = TREE_TYPE (fn);
23672 else
23673 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23674 }
23675
23676 return true;
23677 }
23678
23679 /* We're starting to process the function INST, an instantiation of PATTERN;
23680 add their parameters to local_specializations. */
23681
23682 static void
23683 register_parameter_specializations (tree pattern, tree inst)
23684 {
23685 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23686 tree spec_parm = DECL_ARGUMENTS (inst);
23687 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23688 {
23689 register_local_specialization (spec_parm, tmpl_parm);
23690 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23691 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23692 }
23693 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23694 {
23695 if (!DECL_PACK_P (tmpl_parm))
23696 {
23697 register_local_specialization (spec_parm, tmpl_parm);
23698 spec_parm = DECL_CHAIN (spec_parm);
23699 }
23700 else
23701 {
23702 /* Register the (value) argument pack as a specialization of
23703 TMPL_PARM, then move on. */
23704 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23705 register_local_specialization (argpack, tmpl_parm);
23706 }
23707 }
23708 gcc_assert (!spec_parm);
23709 }
23710
23711 /* Produce the definition of D, a _DECL generated from a template. If
23712 DEFER_OK is true, then we don't have to actually do the
23713 instantiation now; we just have to do it sometime. Normally it is
23714 an error if this is an explicit instantiation but D is undefined.
23715 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23716 instantiated class template. */
23717
23718 tree
23719 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23720 {
23721 tree tmpl = DECL_TI_TEMPLATE (d);
23722 tree gen_args;
23723 tree args;
23724 tree td;
23725 tree code_pattern;
23726 tree spec;
23727 tree gen_tmpl;
23728 bool pattern_defined;
23729 location_t saved_loc = input_location;
23730 int saved_unevaluated_operand = cp_unevaluated_operand;
23731 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23732 bool external_p;
23733 bool deleted_p;
23734
23735 /* This function should only be used to instantiate templates for
23736 functions and static member variables. */
23737 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23738
23739 /* A concept is never instantiated. */
23740 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23741
23742 /* Variables are never deferred; if instantiation is required, they
23743 are instantiated right away. That allows for better code in the
23744 case that an expression refers to the value of the variable --
23745 if the variable has a constant value the referring expression can
23746 take advantage of that fact. */
23747 if (VAR_P (d))
23748 defer_ok = false;
23749
23750 /* Don't instantiate cloned functions. Instead, instantiate the
23751 functions they cloned. */
23752 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23753 d = DECL_CLONED_FUNCTION (d);
23754
23755 if (DECL_TEMPLATE_INSTANTIATED (d)
23756 || (TREE_CODE (d) == FUNCTION_DECL
23757 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23758 || DECL_TEMPLATE_SPECIALIZATION (d))
23759 /* D has already been instantiated or explicitly specialized, so
23760 there's nothing for us to do here.
23761
23762 It might seem reasonable to check whether or not D is an explicit
23763 instantiation, and, if so, stop here. But when an explicit
23764 instantiation is deferred until the end of the compilation,
23765 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23766 the instantiation. */
23767 return d;
23768
23769 /* Check to see whether we know that this template will be
23770 instantiated in some other file, as with "extern template"
23771 extension. */
23772 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23773
23774 /* In general, we do not instantiate such templates. */
23775 if (external_p && !always_instantiate_p (d))
23776 return d;
23777
23778 gen_tmpl = most_general_template (tmpl);
23779 gen_args = DECL_TI_ARGS (d);
23780
23781 if (tmpl != gen_tmpl)
23782 /* We should already have the extra args. */
23783 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23784 == TMPL_ARGS_DEPTH (gen_args));
23785 /* And what's in the hash table should match D. */
23786 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23787 || spec == NULL_TREE);
23788
23789 /* This needs to happen before any tsubsting. */
23790 if (! push_tinst_level (d))
23791 return d;
23792
23793 timevar_push (TV_TEMPLATE_INST);
23794
23795 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23796 for the instantiation. */
23797 td = template_for_substitution (d);
23798 args = gen_args;
23799
23800 if (VAR_P (d))
23801 {
23802 /* Look up an explicit specialization, if any. */
23803 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23804 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23805 if (elt && elt != error_mark_node)
23806 {
23807 td = TREE_VALUE (elt);
23808 args = TREE_PURPOSE (elt);
23809 }
23810 }
23811
23812 code_pattern = DECL_TEMPLATE_RESULT (td);
23813
23814 /* We should never be trying to instantiate a member of a class
23815 template or partial specialization. */
23816 gcc_assert (d != code_pattern);
23817
23818 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23819 || DECL_TEMPLATE_SPECIALIZATION (td))
23820 /* In the case of a friend template whose definition is provided
23821 outside the class, we may have too many arguments. Drop the
23822 ones we don't need. The same is true for specializations. */
23823 args = get_innermost_template_args
23824 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23825
23826 if (TREE_CODE (d) == FUNCTION_DECL)
23827 {
23828 deleted_p = DECL_DELETED_FN (code_pattern);
23829 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23830 && DECL_INITIAL (code_pattern) != error_mark_node)
23831 || DECL_DEFAULTED_FN (code_pattern)
23832 || deleted_p);
23833 }
23834 else
23835 {
23836 deleted_p = false;
23837 if (DECL_CLASS_SCOPE_P (code_pattern))
23838 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23839 || DECL_INLINE_VAR_P (code_pattern));
23840 else
23841 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23842 }
23843
23844 /* We may be in the middle of deferred access check. Disable it now. */
23845 push_deferring_access_checks (dk_no_deferred);
23846
23847 /* Unless an explicit instantiation directive has already determined
23848 the linkage of D, remember that a definition is available for
23849 this entity. */
23850 if (pattern_defined
23851 && !DECL_INTERFACE_KNOWN (d)
23852 && !DECL_NOT_REALLY_EXTERN (d))
23853 mark_definable (d);
23854
23855 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23856 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23857 input_location = DECL_SOURCE_LOCATION (d);
23858
23859 /* If D is a member of an explicitly instantiated class template,
23860 and no definition is available, treat it like an implicit
23861 instantiation. */
23862 if (!pattern_defined && expl_inst_class_mem_p
23863 && DECL_EXPLICIT_INSTANTIATION (d))
23864 {
23865 /* Leave linkage flags alone on instantiations with anonymous
23866 visibility. */
23867 if (TREE_PUBLIC (d))
23868 {
23869 DECL_NOT_REALLY_EXTERN (d) = 0;
23870 DECL_INTERFACE_KNOWN (d) = 0;
23871 }
23872 SET_DECL_IMPLICIT_INSTANTIATION (d);
23873 }
23874
23875 /* Defer all other templates, unless we have been explicitly
23876 forbidden from doing so. */
23877 if (/* If there is no definition, we cannot instantiate the
23878 template. */
23879 ! pattern_defined
23880 /* If it's OK to postpone instantiation, do so. */
23881 || defer_ok
23882 /* If this is a static data member that will be defined
23883 elsewhere, we don't want to instantiate the entire data
23884 member, but we do want to instantiate the initializer so that
23885 we can substitute that elsewhere. */
23886 || (external_p && VAR_P (d))
23887 /* Handle here a deleted function too, avoid generating
23888 its body (c++/61080). */
23889 || deleted_p)
23890 {
23891 /* The definition of the static data member is now required so
23892 we must substitute the initializer. */
23893 if (VAR_P (d)
23894 && !DECL_INITIAL (d)
23895 && DECL_INITIAL (code_pattern))
23896 {
23897 tree ns;
23898 tree init;
23899 bool const_init = false;
23900 bool enter_context = DECL_CLASS_SCOPE_P (d);
23901
23902 ns = decl_namespace_context (d);
23903 push_nested_namespace (ns);
23904 if (enter_context)
23905 push_nested_class (DECL_CONTEXT (d));
23906 init = tsubst_expr (DECL_INITIAL (code_pattern),
23907 args,
23908 tf_warning_or_error, NULL_TREE,
23909 /*integral_constant_expression_p=*/false);
23910 /* If instantiating the initializer involved instantiating this
23911 again, don't call cp_finish_decl twice. */
23912 if (!DECL_INITIAL (d))
23913 {
23914 /* Make sure the initializer is still constant, in case of
23915 circular dependency (template/instantiate6.C). */
23916 const_init
23917 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23918 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23919 /*asmspec_tree=*/NULL_TREE,
23920 LOOKUP_ONLYCONVERTING);
23921 }
23922 if (enter_context)
23923 pop_nested_class ();
23924 pop_nested_namespace (ns);
23925 }
23926
23927 /* We restore the source position here because it's used by
23928 add_pending_template. */
23929 input_location = saved_loc;
23930
23931 if (at_eof && !pattern_defined
23932 && DECL_EXPLICIT_INSTANTIATION (d)
23933 && DECL_NOT_REALLY_EXTERN (d))
23934 /* [temp.explicit]
23935
23936 The definition of a non-exported function template, a
23937 non-exported member function template, or a non-exported
23938 member function or static data member of a class template
23939 shall be present in every translation unit in which it is
23940 explicitly instantiated. */
23941 permerror (input_location, "explicit instantiation of %qD "
23942 "but no definition available", d);
23943
23944 /* If we're in unevaluated context, we just wanted to get the
23945 constant value; this isn't an odr use, so don't queue
23946 a full instantiation. */
23947 if (cp_unevaluated_operand != 0)
23948 goto out;
23949 /* ??? Historically, we have instantiated inline functions, even
23950 when marked as "extern template". */
23951 if (!(external_p && VAR_P (d)))
23952 add_pending_template (d);
23953 goto out;
23954 }
23955 /* Tell the repository that D is available in this translation unit
23956 -- and see if it is supposed to be instantiated here. */
23957 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23958 {
23959 /* In a PCH file, despite the fact that the repository hasn't
23960 requested instantiation in the PCH it is still possible that
23961 an instantiation will be required in a file that includes the
23962 PCH. */
23963 if (pch_file)
23964 add_pending_template (d);
23965 /* Instantiate inline functions so that the inliner can do its
23966 job, even though we'll not be emitting a copy of this
23967 function. */
23968 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23969 goto out;
23970 }
23971
23972 bool push_to_top, nested;
23973 tree fn_context;
23974 fn_context = decl_function_context (d);
23975 if (LAMBDA_FUNCTION_P (d))
23976 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23977 fn_context = NULL_TREE;
23978 nested = current_function_decl != NULL_TREE;
23979 push_to_top = !(nested && fn_context == current_function_decl);
23980
23981 vec<tree> omp_privatization_save;
23982 if (nested)
23983 save_omp_privatization_clauses (omp_privatization_save);
23984
23985 if (push_to_top)
23986 push_to_top_level ();
23987 else
23988 {
23989 gcc_assert (!processing_template_decl);
23990 push_function_context ();
23991 cp_unevaluated_operand = 0;
23992 c_inhibit_evaluation_warnings = 0;
23993 }
23994
23995 /* Mark D as instantiated so that recursive calls to
23996 instantiate_decl do not try to instantiate it again. */
23997 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23998
23999 /* Regenerate the declaration in case the template has been modified
24000 by a subsequent redeclaration. */
24001 regenerate_decl_from_template (d, td, args);
24002
24003 /* We already set the file and line above. Reset them now in case
24004 they changed as a result of calling regenerate_decl_from_template. */
24005 input_location = DECL_SOURCE_LOCATION (d);
24006
24007 if (VAR_P (d))
24008 {
24009 tree init;
24010 bool const_init = false;
24011
24012 /* Clear out DECL_RTL; whatever was there before may not be right
24013 since we've reset the type of the declaration. */
24014 SET_DECL_RTL (d, NULL);
24015 DECL_IN_AGGR_P (d) = 0;
24016
24017 /* The initializer is placed in DECL_INITIAL by
24018 regenerate_decl_from_template so we don't need to
24019 push/pop_access_scope again here. Pull it out so that
24020 cp_finish_decl can process it. */
24021 init = DECL_INITIAL (d);
24022 DECL_INITIAL (d) = NULL_TREE;
24023 DECL_INITIALIZED_P (d) = 0;
24024
24025 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24026 initializer. That function will defer actual emission until
24027 we have a chance to determine linkage. */
24028 DECL_EXTERNAL (d) = 0;
24029
24030 /* Enter the scope of D so that access-checking works correctly. */
24031 bool enter_context = DECL_CLASS_SCOPE_P (d);
24032 if (enter_context)
24033 push_nested_class (DECL_CONTEXT (d));
24034
24035 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24036 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24037
24038 if (enter_context)
24039 pop_nested_class ();
24040
24041 if (variable_template_p (gen_tmpl))
24042 note_variable_template_instantiation (d);
24043 }
24044 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24045 synthesize_method (d);
24046 else if (TREE_CODE (d) == FUNCTION_DECL)
24047 {
24048 /* Set up the list of local specializations. */
24049 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24050 tree block = NULL_TREE;
24051
24052 /* Set up context. */
24053 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24054 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24055 block = push_stmt_list ();
24056 else
24057 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24058
24059 /* Some typedefs referenced from within the template code need to be
24060 access checked at template instantiation time, i.e now. These
24061 types were added to the template at parsing time. Let's get those
24062 and perform the access checks then. */
24063 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24064 args);
24065
24066 /* Create substitution entries for the parameters. */
24067 register_parameter_specializations (code_pattern, d);
24068
24069 /* Substitute into the body of the function. */
24070 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24071 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24072 tf_warning_or_error, tmpl);
24073 else
24074 {
24075 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24076 tf_warning_or_error, tmpl,
24077 /*integral_constant_expression_p=*/false);
24078
24079 /* Set the current input_location to the end of the function
24080 so that finish_function knows where we are. */
24081 input_location
24082 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24083
24084 /* Remember if we saw an infinite loop in the template. */
24085 current_function_infinite_loop
24086 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24087 }
24088
24089 /* Finish the function. */
24090 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24091 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24092 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24093 else
24094 {
24095 d = finish_function (/*inline_p=*/false);
24096 expand_or_defer_fn (d);
24097 }
24098
24099 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24100 cp_check_omp_declare_reduction (d);
24101 }
24102
24103 /* We're not deferring instantiation any more. */
24104 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24105
24106 if (push_to_top)
24107 pop_from_top_level ();
24108 else
24109 pop_function_context ();
24110
24111 if (nested)
24112 restore_omp_privatization_clauses (omp_privatization_save);
24113
24114 out:
24115 pop_deferring_access_checks ();
24116 timevar_pop (TV_TEMPLATE_INST);
24117 pop_tinst_level ();
24118 input_location = saved_loc;
24119 cp_unevaluated_operand = saved_unevaluated_operand;
24120 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24121
24122 return d;
24123 }
24124
24125 /* Run through the list of templates that we wish we could
24126 instantiate, and instantiate any we can. RETRIES is the
24127 number of times we retry pending template instantiation. */
24128
24129 void
24130 instantiate_pending_templates (int retries)
24131 {
24132 int reconsider;
24133 location_t saved_loc = input_location;
24134
24135 /* Instantiating templates may trigger vtable generation. This in turn
24136 may require further template instantiations. We place a limit here
24137 to avoid infinite loop. */
24138 if (pending_templates && retries >= max_tinst_depth)
24139 {
24140 tree decl = pending_templates->tinst->maybe_get_node ();
24141
24142 fatal_error (input_location,
24143 "template instantiation depth exceeds maximum of %d"
24144 " instantiating %q+D, possibly from virtual table generation"
24145 " (use -ftemplate-depth= to increase the maximum)",
24146 max_tinst_depth, decl);
24147 if (TREE_CODE (decl) == FUNCTION_DECL)
24148 /* Pretend that we defined it. */
24149 DECL_INITIAL (decl) = error_mark_node;
24150 return;
24151 }
24152
24153 do
24154 {
24155 struct pending_template **t = &pending_templates;
24156 struct pending_template *last = NULL;
24157 reconsider = 0;
24158 while (*t)
24159 {
24160 tree instantiation = reopen_tinst_level ((*t)->tinst);
24161 bool complete = false;
24162
24163 if (TYPE_P (instantiation))
24164 {
24165 if (!COMPLETE_TYPE_P (instantiation))
24166 {
24167 instantiate_class_template (instantiation);
24168 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24169 for (tree fld = TYPE_FIELDS (instantiation);
24170 fld; fld = TREE_CHAIN (fld))
24171 if ((VAR_P (fld)
24172 || (TREE_CODE (fld) == FUNCTION_DECL
24173 && !DECL_ARTIFICIAL (fld)))
24174 && DECL_TEMPLATE_INSTANTIATION (fld))
24175 instantiate_decl (fld,
24176 /*defer_ok=*/false,
24177 /*expl_inst_class_mem_p=*/false);
24178
24179 if (COMPLETE_TYPE_P (instantiation))
24180 reconsider = 1;
24181 }
24182
24183 complete = COMPLETE_TYPE_P (instantiation);
24184 }
24185 else
24186 {
24187 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24188 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24189 {
24190 instantiation
24191 = instantiate_decl (instantiation,
24192 /*defer_ok=*/false,
24193 /*expl_inst_class_mem_p=*/false);
24194 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24195 reconsider = 1;
24196 }
24197
24198 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24199 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24200 }
24201
24202 if (complete)
24203 {
24204 /* If INSTANTIATION has been instantiated, then we don't
24205 need to consider it again in the future. */
24206 struct pending_template *drop = *t;
24207 *t = (*t)->next;
24208 set_refcount_ptr (drop->tinst);
24209 pending_template_freelist ().free (drop);
24210 }
24211 else
24212 {
24213 last = *t;
24214 t = &(*t)->next;
24215 }
24216 tinst_depth = 0;
24217 set_refcount_ptr (current_tinst_level);
24218 }
24219 last_pending_template = last;
24220 }
24221 while (reconsider);
24222
24223 input_location = saved_loc;
24224 }
24225
24226 /* Substitute ARGVEC into T, which is a list of initializers for
24227 either base class or a non-static data member. The TREE_PURPOSEs
24228 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24229 instantiate_decl. */
24230
24231 static tree
24232 tsubst_initializer_list (tree t, tree argvec)
24233 {
24234 tree inits = NULL_TREE;
24235 tree target_ctor = error_mark_node;
24236
24237 for (; t; t = TREE_CHAIN (t))
24238 {
24239 tree decl;
24240 tree init;
24241 tree expanded_bases = NULL_TREE;
24242 tree expanded_arguments = NULL_TREE;
24243 int i, len = 1;
24244
24245 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24246 {
24247 tree expr;
24248 tree arg;
24249
24250 /* Expand the base class expansion type into separate base
24251 classes. */
24252 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24253 tf_warning_or_error,
24254 NULL_TREE);
24255 if (expanded_bases == error_mark_node)
24256 continue;
24257
24258 /* We'll be building separate TREE_LISTs of arguments for
24259 each base. */
24260 len = TREE_VEC_LENGTH (expanded_bases);
24261 expanded_arguments = make_tree_vec (len);
24262 for (i = 0; i < len; i++)
24263 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24264
24265 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24266 expand each argument in the TREE_VALUE of t. */
24267 expr = make_node (EXPR_PACK_EXPANSION);
24268 PACK_EXPANSION_LOCAL_P (expr) = true;
24269 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24270 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24271
24272 if (TREE_VALUE (t) == void_type_node)
24273 /* VOID_TYPE_NODE is used to indicate
24274 value-initialization. */
24275 {
24276 for (i = 0; i < len; i++)
24277 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24278 }
24279 else
24280 {
24281 /* Substitute parameter packs into each argument in the
24282 TREE_LIST. */
24283 in_base_initializer = 1;
24284 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24285 {
24286 tree expanded_exprs;
24287
24288 /* Expand the argument. */
24289 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24290 expanded_exprs
24291 = tsubst_pack_expansion (expr, argvec,
24292 tf_warning_or_error,
24293 NULL_TREE);
24294 if (expanded_exprs == error_mark_node)
24295 continue;
24296
24297 /* Prepend each of the expanded expressions to the
24298 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24299 for (i = 0; i < len; i++)
24300 {
24301 TREE_VEC_ELT (expanded_arguments, i) =
24302 tree_cons (NULL_TREE,
24303 TREE_VEC_ELT (expanded_exprs, i),
24304 TREE_VEC_ELT (expanded_arguments, i));
24305 }
24306 }
24307 in_base_initializer = 0;
24308
24309 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24310 since we built them backwards. */
24311 for (i = 0; i < len; i++)
24312 {
24313 TREE_VEC_ELT (expanded_arguments, i) =
24314 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24315 }
24316 }
24317 }
24318
24319 for (i = 0; i < len; ++i)
24320 {
24321 if (expanded_bases)
24322 {
24323 decl = TREE_VEC_ELT (expanded_bases, i);
24324 decl = expand_member_init (decl);
24325 init = TREE_VEC_ELT (expanded_arguments, i);
24326 }
24327 else
24328 {
24329 tree tmp;
24330 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
24331 tf_warning_or_error, NULL_TREE);
24332
24333 decl = expand_member_init (decl);
24334 if (decl && !DECL_P (decl))
24335 in_base_initializer = 1;
24336
24337 init = TREE_VALUE (t);
24338 tmp = init;
24339 if (init != void_type_node)
24340 init = tsubst_expr (init, argvec,
24341 tf_warning_or_error, NULL_TREE,
24342 /*integral_constant_expression_p=*/false);
24343 if (init == NULL_TREE && tmp != NULL_TREE)
24344 /* If we had an initializer but it instantiated to nothing,
24345 value-initialize the object. This will only occur when
24346 the initializer was a pack expansion where the parameter
24347 packs used in that expansion were of length zero. */
24348 init = void_type_node;
24349 in_base_initializer = 0;
24350 }
24351
24352 if (target_ctor != error_mark_node
24353 && init != error_mark_node)
24354 {
24355 error ("mem-initializer for %qD follows constructor delegation",
24356 decl);
24357 return inits;
24358 }
24359 /* Look for a target constructor. */
24360 if (init != error_mark_node
24361 && decl && CLASS_TYPE_P (decl)
24362 && same_type_p (decl, current_class_type))
24363 {
24364 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
24365 if (inits)
24366 {
24367 error ("constructor delegation follows mem-initializer for %qD",
24368 TREE_PURPOSE (inits));
24369 continue;
24370 }
24371 target_ctor = init;
24372 }
24373
24374 if (decl)
24375 {
24376 init = build_tree_list (decl, init);
24377 TREE_CHAIN (init) = inits;
24378 inits = init;
24379 }
24380 }
24381 }
24382 return inits;
24383 }
24384
24385 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24386
24387 static void
24388 set_current_access_from_decl (tree decl)
24389 {
24390 if (TREE_PRIVATE (decl))
24391 current_access_specifier = access_private_node;
24392 else if (TREE_PROTECTED (decl))
24393 current_access_specifier = access_protected_node;
24394 else
24395 current_access_specifier = access_public_node;
24396 }
24397
24398 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24399 is the instantiation (which should have been created with
24400 start_enum) and ARGS are the template arguments to use. */
24401
24402 static void
24403 tsubst_enum (tree tag, tree newtag, tree args)
24404 {
24405 tree e;
24406
24407 if (SCOPED_ENUM_P (newtag))
24408 begin_scope (sk_scoped_enum, newtag);
24409
24410 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
24411 {
24412 tree value;
24413 tree decl;
24414
24415 decl = TREE_VALUE (e);
24416 /* Note that in a template enum, the TREE_VALUE is the
24417 CONST_DECL, not the corresponding INTEGER_CST. */
24418 value = tsubst_expr (DECL_INITIAL (decl),
24419 args, tf_warning_or_error, NULL_TREE,
24420 /*integral_constant_expression_p=*/true);
24421
24422 /* Give this enumeration constant the correct access. */
24423 set_current_access_from_decl (decl);
24424
24425 /* Actually build the enumerator itself. Here we're assuming that
24426 enumerators can't have dependent attributes. */
24427 build_enumerator (DECL_NAME (decl), value, newtag,
24428 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24429 }
24430
24431 if (SCOPED_ENUM_P (newtag))
24432 finish_scope ();
24433
24434 finish_enum_value_list (newtag);
24435 finish_enum (newtag);
24436
24437 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24438 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24439 }
24440
24441 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24442 its type -- but without substituting the innermost set of template
24443 arguments. So, innermost set of template parameters will appear in
24444 the type. */
24445
24446 tree
24447 get_mostly_instantiated_function_type (tree decl)
24448 {
24449 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24450 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24451 }
24452
24453 /* Return truthvalue if we're processing a template different from
24454 the last one involved in diagnostics. */
24455 bool
24456 problematic_instantiation_changed (void)
24457 {
24458 return current_tinst_level != last_error_tinst_level;
24459 }
24460
24461 /* Remember current template involved in diagnostics. */
24462 void
24463 record_last_problematic_instantiation (void)
24464 {
24465 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
24466 }
24467
24468 struct tinst_level *
24469 current_instantiation (void)
24470 {
24471 return current_tinst_level;
24472 }
24473
24474 /* Return TRUE if current_function_decl is being instantiated, false
24475 otherwise. */
24476
24477 bool
24478 instantiating_current_function_p (void)
24479 {
24480 return (current_instantiation ()
24481 && (current_instantiation ()->maybe_get_node ()
24482 == current_function_decl));
24483 }
24484
24485 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24486 type. Return false for ok, true for disallowed. Issue error and
24487 inform messages under control of COMPLAIN. */
24488
24489 static bool
24490 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24491 {
24492 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24493 return false;
24494 else if (TYPE_PTR_P (type))
24495 return false;
24496 else if (TYPE_REF_P (type)
24497 && !TYPE_REF_IS_RVALUE (type))
24498 return false;
24499 else if (TYPE_PTRMEM_P (type))
24500 return false;
24501 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24502 return false;
24503 else if (TREE_CODE (type) == TYPENAME_TYPE)
24504 return false;
24505 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24506 return false;
24507 else if (TREE_CODE (type) == NULLPTR_TYPE)
24508 return false;
24509 /* A bound template template parm could later be instantiated to have a valid
24510 nontype parm type via an alias template. */
24511 else if (cxx_dialect >= cxx11
24512 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24513 return false;
24514
24515 if (complain & tf_error)
24516 {
24517 if (type == error_mark_node)
24518 inform (input_location, "invalid template non-type parameter");
24519 else
24520 error ("%q#T is not a valid type for a template non-type parameter",
24521 type);
24522 }
24523 return true;
24524 }
24525
24526 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24527 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24528
24529 static bool
24530 dependent_type_p_r (tree type)
24531 {
24532 tree scope;
24533
24534 /* [temp.dep.type]
24535
24536 A type is dependent if it is:
24537
24538 -- a template parameter. Template template parameters are types
24539 for us (since TYPE_P holds true for them) so we handle
24540 them here. */
24541 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24542 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24543 return true;
24544 /* -- a qualified-id with a nested-name-specifier which contains a
24545 class-name that names a dependent type or whose unqualified-id
24546 names a dependent type. */
24547 if (TREE_CODE (type) == TYPENAME_TYPE)
24548 return true;
24549
24550 /* An alias template specialization can be dependent even if the
24551 resulting type is not. */
24552 if (dependent_alias_template_spec_p (type))
24553 return true;
24554
24555 /* -- a cv-qualified type where the cv-unqualified type is
24556 dependent.
24557 No code is necessary for this bullet; the code below handles
24558 cv-qualified types, and we don't want to strip aliases with
24559 TYPE_MAIN_VARIANT because of DR 1558. */
24560 /* -- a compound type constructed from any dependent type. */
24561 if (TYPE_PTRMEM_P (type))
24562 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24563 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24564 (type)));
24565 else if (INDIRECT_TYPE_P (type))
24566 return dependent_type_p (TREE_TYPE (type));
24567 else if (TREE_CODE (type) == FUNCTION_TYPE
24568 || TREE_CODE (type) == METHOD_TYPE)
24569 {
24570 tree arg_type;
24571
24572 if (dependent_type_p (TREE_TYPE (type)))
24573 return true;
24574 for (arg_type = TYPE_ARG_TYPES (type);
24575 arg_type;
24576 arg_type = TREE_CHAIN (arg_type))
24577 if (dependent_type_p (TREE_VALUE (arg_type)))
24578 return true;
24579 if (cxx_dialect >= cxx17)
24580 /* A value-dependent noexcept-specifier makes the type dependent. */
24581 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24582 if (tree noex = TREE_PURPOSE (spec))
24583 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24584 affect overload resolution and treating it as dependent breaks
24585 things. */
24586 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24587 && value_dependent_expression_p (noex))
24588 return true;
24589 return false;
24590 }
24591 /* -- an array type constructed from any dependent type or whose
24592 size is specified by a constant expression that is
24593 value-dependent.
24594
24595 We checked for type- and value-dependence of the bounds in
24596 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24597 if (TREE_CODE (type) == ARRAY_TYPE)
24598 {
24599 if (TYPE_DOMAIN (type)
24600 && dependent_type_p (TYPE_DOMAIN (type)))
24601 return true;
24602 return dependent_type_p (TREE_TYPE (type));
24603 }
24604
24605 /* -- a template-id in which either the template name is a template
24606 parameter ... */
24607 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24608 return true;
24609 /* ... or any of the template arguments is a dependent type or
24610 an expression that is type-dependent or value-dependent. */
24611 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24612 && (any_dependent_template_arguments_p
24613 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24614 return true;
24615
24616 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24617 dependent; if the argument of the `typeof' expression is not
24618 type-dependent, then it should already been have resolved. */
24619 if (TREE_CODE (type) == TYPEOF_TYPE
24620 || TREE_CODE (type) == DECLTYPE_TYPE
24621 || TREE_CODE (type) == UNDERLYING_TYPE)
24622 return true;
24623
24624 /* A template argument pack is dependent if any of its packed
24625 arguments are. */
24626 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24627 {
24628 tree args = ARGUMENT_PACK_ARGS (type);
24629 int i, len = TREE_VEC_LENGTH (args);
24630 for (i = 0; i < len; ++i)
24631 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24632 return true;
24633 }
24634
24635 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24636 be template parameters. */
24637 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24638 return true;
24639
24640 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24641 return true;
24642
24643 /* The standard does not specifically mention types that are local
24644 to template functions or local classes, but they should be
24645 considered dependent too. For example:
24646
24647 template <int I> void f() {
24648 enum E { a = I };
24649 S<sizeof (E)> s;
24650 }
24651
24652 The size of `E' cannot be known until the value of `I' has been
24653 determined. Therefore, `E' must be considered dependent. */
24654 scope = TYPE_CONTEXT (type);
24655 if (scope && TYPE_P (scope))
24656 return dependent_type_p (scope);
24657 /* Don't use type_dependent_expression_p here, as it can lead
24658 to infinite recursion trying to determine whether a lambda
24659 nested in a lambda is dependent (c++/47687). */
24660 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24661 && DECL_LANG_SPECIFIC (scope)
24662 && DECL_TEMPLATE_INFO (scope)
24663 && (any_dependent_template_arguments_p
24664 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24665 return true;
24666
24667 /* Other types are non-dependent. */
24668 return false;
24669 }
24670
24671 /* Returns TRUE if TYPE is dependent, in the sense of
24672 [temp.dep.type]. Note that a NULL type is considered dependent. */
24673
24674 bool
24675 dependent_type_p (tree type)
24676 {
24677 /* If there are no template parameters in scope, then there can't be
24678 any dependent types. */
24679 if (!processing_template_decl)
24680 {
24681 /* If we are not processing a template, then nobody should be
24682 providing us with a dependent type. */
24683 gcc_assert (type);
24684 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24685 return false;
24686 }
24687
24688 /* If the type is NULL, we have not computed a type for the entity
24689 in question; in that case, the type is dependent. */
24690 if (!type)
24691 return true;
24692
24693 /* Erroneous types can be considered non-dependent. */
24694 if (type == error_mark_node)
24695 return false;
24696
24697 /* Getting here with global_type_node means we improperly called this
24698 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24699 gcc_checking_assert (type != global_type_node);
24700
24701 /* If we have not already computed the appropriate value for TYPE,
24702 do so now. */
24703 if (!TYPE_DEPENDENT_P_VALID (type))
24704 {
24705 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24706 TYPE_DEPENDENT_P_VALID (type) = 1;
24707 }
24708
24709 return TYPE_DEPENDENT_P (type);
24710 }
24711
24712 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24713 lookup. In other words, a dependent type that is not the current
24714 instantiation. */
24715
24716 bool
24717 dependent_scope_p (tree scope)
24718 {
24719 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24720 && !currently_open_class (scope));
24721 }
24722
24723 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24724 an unknown base of 'this' (and is therefore instantiation-dependent). */
24725
24726 static bool
24727 unknown_base_ref_p (tree t)
24728 {
24729 if (!current_class_ptr)
24730 return false;
24731
24732 tree mem = TREE_OPERAND (t, 1);
24733 if (shared_member_p (mem))
24734 return false;
24735
24736 tree cur = current_nonlambda_class_type ();
24737 if (!any_dependent_bases_p (cur))
24738 return false;
24739
24740 tree ctx = TREE_OPERAND (t, 0);
24741 if (DERIVED_FROM_P (ctx, cur))
24742 return false;
24743
24744 return true;
24745 }
24746
24747 /* T is a SCOPE_REF; return whether we need to consider it
24748 instantiation-dependent so that we can check access at instantiation
24749 time even though we know which member it resolves to. */
24750
24751 static bool
24752 instantiation_dependent_scope_ref_p (tree t)
24753 {
24754 if (DECL_P (TREE_OPERAND (t, 1))
24755 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24756 && !unknown_base_ref_p (t)
24757 && accessible_in_template_p (TREE_OPERAND (t, 0),
24758 TREE_OPERAND (t, 1)))
24759 return false;
24760 else
24761 return true;
24762 }
24763
24764 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24765 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24766 expression. */
24767
24768 /* Note that this predicate is not appropriate for general expressions;
24769 only constant expressions (that satisfy potential_constant_expression)
24770 can be tested for value dependence. */
24771
24772 bool
24773 value_dependent_expression_p (tree expression)
24774 {
24775 if (!processing_template_decl || expression == NULL_TREE)
24776 return false;
24777
24778 /* A type-dependent expression is also value-dependent. */
24779 if (type_dependent_expression_p (expression))
24780 return true;
24781
24782 switch (TREE_CODE (expression))
24783 {
24784 case BASELINK:
24785 /* A dependent member function of the current instantiation. */
24786 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24787
24788 case FUNCTION_DECL:
24789 /* A dependent member function of the current instantiation. */
24790 if (DECL_CLASS_SCOPE_P (expression)
24791 && dependent_type_p (DECL_CONTEXT (expression)))
24792 return true;
24793 break;
24794
24795 case IDENTIFIER_NODE:
24796 /* A name that has not been looked up -- must be dependent. */
24797 return true;
24798
24799 case TEMPLATE_PARM_INDEX:
24800 /* A non-type template parm. */
24801 return true;
24802
24803 case CONST_DECL:
24804 /* A non-type template parm. */
24805 if (DECL_TEMPLATE_PARM_P (expression))
24806 return true;
24807 return value_dependent_expression_p (DECL_INITIAL (expression));
24808
24809 case VAR_DECL:
24810 /* A constant with literal type and is initialized
24811 with an expression that is value-dependent. */
24812 if (DECL_DEPENDENT_INIT_P (expression)
24813 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24814 || TYPE_REF_P (TREE_TYPE (expression)))
24815 return true;
24816 if (DECL_HAS_VALUE_EXPR_P (expression))
24817 {
24818 tree value_expr = DECL_VALUE_EXPR (expression);
24819 if (value_dependent_expression_p (value_expr))
24820 return true;
24821 }
24822 return false;
24823
24824 case DYNAMIC_CAST_EXPR:
24825 case STATIC_CAST_EXPR:
24826 case CONST_CAST_EXPR:
24827 case REINTERPRET_CAST_EXPR:
24828 case CAST_EXPR:
24829 case IMPLICIT_CONV_EXPR:
24830 /* These expressions are value-dependent if the type to which
24831 the cast occurs is dependent or the expression being casted
24832 is value-dependent. */
24833 {
24834 tree type = TREE_TYPE (expression);
24835
24836 if (dependent_type_p (type))
24837 return true;
24838
24839 /* A functional cast has a list of operands. */
24840 expression = TREE_OPERAND (expression, 0);
24841 if (!expression)
24842 {
24843 /* If there are no operands, it must be an expression such
24844 as "int()". This should not happen for aggregate types
24845 because it would form non-constant expressions. */
24846 gcc_assert (cxx_dialect >= cxx11
24847 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24848
24849 return false;
24850 }
24851
24852 if (TREE_CODE (expression) == TREE_LIST)
24853 return any_value_dependent_elements_p (expression);
24854
24855 return value_dependent_expression_p (expression);
24856 }
24857
24858 case SIZEOF_EXPR:
24859 if (SIZEOF_EXPR_TYPE_P (expression))
24860 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24861 /* FALLTHRU */
24862 case ALIGNOF_EXPR:
24863 case TYPEID_EXPR:
24864 /* A `sizeof' expression is value-dependent if the operand is
24865 type-dependent or is a pack expansion. */
24866 expression = TREE_OPERAND (expression, 0);
24867 if (PACK_EXPANSION_P (expression))
24868 return true;
24869 else if (TYPE_P (expression))
24870 return dependent_type_p (expression);
24871 return instantiation_dependent_uneval_expression_p (expression);
24872
24873 case AT_ENCODE_EXPR:
24874 /* An 'encode' expression is value-dependent if the operand is
24875 type-dependent. */
24876 expression = TREE_OPERAND (expression, 0);
24877 return dependent_type_p (expression);
24878
24879 case NOEXCEPT_EXPR:
24880 expression = TREE_OPERAND (expression, 0);
24881 return instantiation_dependent_uneval_expression_p (expression);
24882
24883 case SCOPE_REF:
24884 /* All instantiation-dependent expressions should also be considered
24885 value-dependent. */
24886 return instantiation_dependent_scope_ref_p (expression);
24887
24888 case COMPONENT_REF:
24889 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24890 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24891
24892 case NONTYPE_ARGUMENT_PACK:
24893 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24894 is value-dependent. */
24895 {
24896 tree values = ARGUMENT_PACK_ARGS (expression);
24897 int i, len = TREE_VEC_LENGTH (values);
24898
24899 for (i = 0; i < len; ++i)
24900 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24901 return true;
24902
24903 return false;
24904 }
24905
24906 case TRAIT_EXPR:
24907 {
24908 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24909
24910 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24911 return true;
24912
24913 if (!type2)
24914 return false;
24915
24916 if (TREE_CODE (type2) != TREE_LIST)
24917 return dependent_type_p (type2);
24918
24919 for (; type2; type2 = TREE_CHAIN (type2))
24920 if (dependent_type_p (TREE_VALUE (type2)))
24921 return true;
24922
24923 return false;
24924 }
24925
24926 case MODOP_EXPR:
24927 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24928 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24929
24930 case ARRAY_REF:
24931 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24932 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24933
24934 case ADDR_EXPR:
24935 {
24936 tree op = TREE_OPERAND (expression, 0);
24937 return (value_dependent_expression_p (op)
24938 || has_value_dependent_address (op));
24939 }
24940
24941 case REQUIRES_EXPR:
24942 /* Treat all requires-expressions as value-dependent so
24943 we don't try to fold them. */
24944 return true;
24945
24946 case TYPE_REQ:
24947 return dependent_type_p (TREE_OPERAND (expression, 0));
24948
24949 case CALL_EXPR:
24950 {
24951 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24952 return true;
24953 tree fn = get_callee_fndecl (expression);
24954 int i, nargs;
24955 nargs = call_expr_nargs (expression);
24956 for (i = 0; i < nargs; ++i)
24957 {
24958 tree op = CALL_EXPR_ARG (expression, i);
24959 /* In a call to a constexpr member function, look through the
24960 implicit ADDR_EXPR on the object argument so that it doesn't
24961 cause the call to be considered value-dependent. We also
24962 look through it in potential_constant_expression. */
24963 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24964 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24965 && TREE_CODE (op) == ADDR_EXPR)
24966 op = TREE_OPERAND (op, 0);
24967 if (value_dependent_expression_p (op))
24968 return true;
24969 }
24970 return false;
24971 }
24972
24973 case TEMPLATE_ID_EXPR:
24974 return variable_concept_p (TREE_OPERAND (expression, 0));
24975
24976 case CONSTRUCTOR:
24977 {
24978 unsigned ix;
24979 tree val;
24980 if (dependent_type_p (TREE_TYPE (expression)))
24981 return true;
24982 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24983 if (value_dependent_expression_p (val))
24984 return true;
24985 return false;
24986 }
24987
24988 case STMT_EXPR:
24989 /* Treat a GNU statement expression as dependent to avoid crashing
24990 under instantiate_non_dependent_expr; it can't be constant. */
24991 return true;
24992
24993 default:
24994 /* A constant expression is value-dependent if any subexpression is
24995 value-dependent. */
24996 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24997 {
24998 case tcc_reference:
24999 case tcc_unary:
25000 case tcc_comparison:
25001 case tcc_binary:
25002 case tcc_expression:
25003 case tcc_vl_exp:
25004 {
25005 int i, len = cp_tree_operand_length (expression);
25006
25007 for (i = 0; i < len; i++)
25008 {
25009 tree t = TREE_OPERAND (expression, i);
25010
25011 /* In some cases, some of the operands may be missing.
25012 (For example, in the case of PREDECREMENT_EXPR, the
25013 amount to increment by may be missing.) That doesn't
25014 make the expression dependent. */
25015 if (t && value_dependent_expression_p (t))
25016 return true;
25017 }
25018 }
25019 break;
25020 default:
25021 break;
25022 }
25023 break;
25024 }
25025
25026 /* The expression is not value-dependent. */
25027 return false;
25028 }
25029
25030 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25031 [temp.dep.expr]. Note that an expression with no type is
25032 considered dependent. Other parts of the compiler arrange for an
25033 expression with type-dependent subexpressions to have no type, so
25034 this function doesn't have to be fully recursive. */
25035
25036 bool
25037 type_dependent_expression_p (tree expression)
25038 {
25039 if (!processing_template_decl)
25040 return false;
25041
25042 if (expression == NULL_TREE || expression == error_mark_node)
25043 return false;
25044
25045 STRIP_ANY_LOCATION_WRAPPER (expression);
25046
25047 /* An unresolved name is always dependent. */
25048 if (identifier_p (expression)
25049 || TREE_CODE (expression) == USING_DECL
25050 || TREE_CODE (expression) == WILDCARD_DECL)
25051 return true;
25052
25053 /* A fold expression is type-dependent. */
25054 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25055 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25056 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25057 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25058 return true;
25059
25060 /* Some expression forms are never type-dependent. */
25061 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25062 || TREE_CODE (expression) == SIZEOF_EXPR
25063 || TREE_CODE (expression) == ALIGNOF_EXPR
25064 || TREE_CODE (expression) == AT_ENCODE_EXPR
25065 || TREE_CODE (expression) == NOEXCEPT_EXPR
25066 || TREE_CODE (expression) == TRAIT_EXPR
25067 || TREE_CODE (expression) == TYPEID_EXPR
25068 || TREE_CODE (expression) == DELETE_EXPR
25069 || TREE_CODE (expression) == VEC_DELETE_EXPR
25070 || TREE_CODE (expression) == THROW_EXPR
25071 || TREE_CODE (expression) == REQUIRES_EXPR)
25072 return false;
25073
25074 /* The types of these expressions depends only on the type to which
25075 the cast occurs. */
25076 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25077 || TREE_CODE (expression) == STATIC_CAST_EXPR
25078 || TREE_CODE (expression) == CONST_CAST_EXPR
25079 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25080 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25081 || TREE_CODE (expression) == CAST_EXPR)
25082 return dependent_type_p (TREE_TYPE (expression));
25083
25084 /* The types of these expressions depends only on the type created
25085 by the expression. */
25086 if (TREE_CODE (expression) == NEW_EXPR
25087 || TREE_CODE (expression) == VEC_NEW_EXPR)
25088 {
25089 /* For NEW_EXPR tree nodes created inside a template, either
25090 the object type itself or a TREE_LIST may appear as the
25091 operand 1. */
25092 tree type = TREE_OPERAND (expression, 1);
25093 if (TREE_CODE (type) == TREE_LIST)
25094 /* This is an array type. We need to check array dimensions
25095 as well. */
25096 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25097 || value_dependent_expression_p
25098 (TREE_OPERAND (TREE_VALUE (type), 1));
25099 else
25100 return dependent_type_p (type);
25101 }
25102
25103 if (TREE_CODE (expression) == SCOPE_REF)
25104 {
25105 tree scope = TREE_OPERAND (expression, 0);
25106 tree name = TREE_OPERAND (expression, 1);
25107
25108 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25109 contains an identifier associated by name lookup with one or more
25110 declarations declared with a dependent type, or...a
25111 nested-name-specifier or qualified-id that names a member of an
25112 unknown specialization. */
25113 return (type_dependent_expression_p (name)
25114 || dependent_scope_p (scope));
25115 }
25116
25117 if (TREE_CODE (expression) == TEMPLATE_DECL
25118 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25119 return uses_outer_template_parms (expression);
25120
25121 if (TREE_CODE (expression) == STMT_EXPR)
25122 expression = stmt_expr_value_expr (expression);
25123
25124 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25125 {
25126 tree elt;
25127 unsigned i;
25128
25129 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25130 {
25131 if (type_dependent_expression_p (elt))
25132 return true;
25133 }
25134 return false;
25135 }
25136
25137 /* A static data member of the current instantiation with incomplete
25138 array type is type-dependent, as the definition and specializations
25139 can have different bounds. */
25140 if (VAR_P (expression)
25141 && DECL_CLASS_SCOPE_P (expression)
25142 && dependent_type_p (DECL_CONTEXT (expression))
25143 && VAR_HAD_UNKNOWN_BOUND (expression))
25144 return true;
25145
25146 /* An array of unknown bound depending on a variadic parameter, eg:
25147
25148 template<typename... Args>
25149 void foo (Args... args)
25150 {
25151 int arr[] = { args... };
25152 }
25153
25154 template<int... vals>
25155 void bar ()
25156 {
25157 int arr[] = { vals... };
25158 }
25159
25160 If the array has no length and has an initializer, it must be that
25161 we couldn't determine its length in cp_complete_array_type because
25162 it is dependent. */
25163 if (VAR_P (expression)
25164 && TREE_TYPE (expression) != NULL_TREE
25165 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25166 && !TYPE_DOMAIN (TREE_TYPE (expression))
25167 && DECL_INITIAL (expression))
25168 return true;
25169
25170 /* A function or variable template-id is type-dependent if it has any
25171 dependent template arguments. */
25172 if (VAR_OR_FUNCTION_DECL_P (expression)
25173 && DECL_LANG_SPECIFIC (expression)
25174 && DECL_TEMPLATE_INFO (expression))
25175 {
25176 /* Consider the innermost template arguments, since those are the ones
25177 that come from the template-id; the template arguments for the
25178 enclosing class do not make it type-dependent unless they are used in
25179 the type of the decl. */
25180 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
25181 && (any_dependent_template_arguments_p
25182 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25183 return true;
25184 }
25185
25186 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25187 type-dependent. Checking this is important for functions with auto return
25188 type, which looks like a dependent type. */
25189 if (TREE_CODE (expression) == FUNCTION_DECL
25190 && !(DECL_CLASS_SCOPE_P (expression)
25191 && dependent_type_p (DECL_CONTEXT (expression)))
25192 && !(DECL_LANG_SPECIFIC (expression)
25193 && DECL_FRIEND_P (expression)
25194 && (!DECL_FRIEND_CONTEXT (expression)
25195 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25196 && !DECL_LOCAL_FUNCTION_P (expression))
25197 {
25198 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25199 || undeduced_auto_decl (expression));
25200 return false;
25201 }
25202
25203 /* Always dependent, on the number of arguments if nothing else. */
25204 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25205 return true;
25206
25207 if (TREE_TYPE (expression) == unknown_type_node)
25208 {
25209 if (TREE_CODE (expression) == ADDR_EXPR)
25210 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25211 if (TREE_CODE (expression) == COMPONENT_REF
25212 || TREE_CODE (expression) == OFFSET_REF)
25213 {
25214 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25215 return true;
25216 expression = TREE_OPERAND (expression, 1);
25217 if (identifier_p (expression))
25218 return false;
25219 }
25220 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25221 if (TREE_CODE (expression) == SCOPE_REF)
25222 return false;
25223
25224 if (BASELINK_P (expression))
25225 {
25226 if (BASELINK_OPTYPE (expression)
25227 && dependent_type_p (BASELINK_OPTYPE (expression)))
25228 return true;
25229 expression = BASELINK_FUNCTIONS (expression);
25230 }
25231
25232 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25233 {
25234 if (any_dependent_template_arguments_p
25235 (TREE_OPERAND (expression, 1)))
25236 return true;
25237 expression = TREE_OPERAND (expression, 0);
25238 if (identifier_p (expression))
25239 return true;
25240 }
25241
25242 gcc_assert (TREE_CODE (expression) == OVERLOAD
25243 || TREE_CODE (expression) == FUNCTION_DECL);
25244
25245 for (lkp_iterator iter (expression); iter; ++iter)
25246 if (type_dependent_expression_p (*iter))
25247 return true;
25248
25249 return false;
25250 }
25251
25252 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25253
25254 /* Dependent type attributes might not have made it from the decl to
25255 the type yet. */
25256 if (DECL_P (expression)
25257 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25258 return true;
25259
25260 return (dependent_type_p (TREE_TYPE (expression)));
25261 }
25262
25263 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25264 type-dependent if the expression refers to a member of the current
25265 instantiation and the type of the referenced member is dependent, or the
25266 class member access expression refers to a member of an unknown
25267 specialization.
25268
25269 This function returns true if the OBJECT in such a class member access
25270 expression is of an unknown specialization. */
25271
25272 bool
25273 type_dependent_object_expression_p (tree object)
25274 {
25275 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25276 dependent. */
25277 if (TREE_CODE (object) == IDENTIFIER_NODE)
25278 return true;
25279 tree scope = TREE_TYPE (object);
25280 return (!scope || dependent_scope_p (scope));
25281 }
25282
25283 /* walk_tree callback function for instantiation_dependent_expression_p,
25284 below. Returns non-zero if a dependent subexpression is found. */
25285
25286 static tree
25287 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25288 void * /*data*/)
25289 {
25290 if (TYPE_P (*tp))
25291 {
25292 /* We don't have to worry about decltype currently because decltype
25293 of an instantiation-dependent expr is a dependent type. This
25294 might change depending on the resolution of DR 1172. */
25295 *walk_subtrees = false;
25296 return NULL_TREE;
25297 }
25298 enum tree_code code = TREE_CODE (*tp);
25299 switch (code)
25300 {
25301 /* Don't treat an argument list as dependent just because it has no
25302 TREE_TYPE. */
25303 case TREE_LIST:
25304 case TREE_VEC:
25305 case NONTYPE_ARGUMENT_PACK:
25306 return NULL_TREE;
25307
25308 case TEMPLATE_PARM_INDEX:
25309 return *tp;
25310
25311 /* Handle expressions with type operands. */
25312 case SIZEOF_EXPR:
25313 case ALIGNOF_EXPR:
25314 case TYPEID_EXPR:
25315 case AT_ENCODE_EXPR:
25316 {
25317 tree op = TREE_OPERAND (*tp, 0);
25318 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
25319 op = TREE_TYPE (op);
25320 if (TYPE_P (op))
25321 {
25322 if (dependent_type_p (op))
25323 return *tp;
25324 else
25325 {
25326 *walk_subtrees = false;
25327 return NULL_TREE;
25328 }
25329 }
25330 break;
25331 }
25332
25333 case COMPONENT_REF:
25334 if (identifier_p (TREE_OPERAND (*tp, 1)))
25335 /* In a template, finish_class_member_access_expr creates a
25336 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25337 type-dependent, so that we can check access control at
25338 instantiation time (PR 42277). See also Core issue 1273. */
25339 return *tp;
25340 break;
25341
25342 case SCOPE_REF:
25343 if (instantiation_dependent_scope_ref_p (*tp))
25344 return *tp;
25345 else
25346 break;
25347
25348 /* Treat statement-expressions as dependent. */
25349 case BIND_EXPR:
25350 return *tp;
25351
25352 /* Treat requires-expressions as dependent. */
25353 case REQUIRES_EXPR:
25354 return *tp;
25355
25356 case CALL_EXPR:
25357 /* Treat calls to function concepts as dependent. */
25358 if (function_concept_check_p (*tp))
25359 return *tp;
25360 break;
25361
25362 case TEMPLATE_ID_EXPR:
25363 /* And variable concepts. */
25364 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
25365 return *tp;
25366 break;
25367
25368 default:
25369 break;
25370 }
25371
25372 if (type_dependent_expression_p (*tp))
25373 return *tp;
25374 else
25375 return NULL_TREE;
25376 }
25377
25378 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25379 sense defined by the ABI:
25380
25381 "An expression is instantiation-dependent if it is type-dependent
25382 or value-dependent, or it has a subexpression that is type-dependent
25383 or value-dependent."
25384
25385 Except don't actually check value-dependence for unevaluated expressions,
25386 because in sizeof(i) we don't care about the value of i. Checking
25387 type-dependence will in turn check value-dependence of array bounds/template
25388 arguments as needed. */
25389
25390 bool
25391 instantiation_dependent_uneval_expression_p (tree expression)
25392 {
25393 tree result;
25394
25395 if (!processing_template_decl)
25396 return false;
25397
25398 if (expression == error_mark_node)
25399 return false;
25400
25401 result = cp_walk_tree_without_duplicates (&expression,
25402 instantiation_dependent_r, NULL);
25403 return result != NULL_TREE;
25404 }
25405
25406 /* As above, but also check value-dependence of the expression as a whole. */
25407
25408 bool
25409 instantiation_dependent_expression_p (tree expression)
25410 {
25411 return (instantiation_dependent_uneval_expression_p (expression)
25412 || value_dependent_expression_p (expression));
25413 }
25414
25415 /* Like type_dependent_expression_p, but it also works while not processing
25416 a template definition, i.e. during substitution or mangling. */
25417
25418 bool
25419 type_dependent_expression_p_push (tree expr)
25420 {
25421 bool b;
25422 ++processing_template_decl;
25423 b = type_dependent_expression_p (expr);
25424 --processing_template_decl;
25425 return b;
25426 }
25427
25428 /* Returns TRUE if ARGS contains a type-dependent expression. */
25429
25430 bool
25431 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25432 {
25433 unsigned int i;
25434 tree arg;
25435
25436 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25437 {
25438 if (type_dependent_expression_p (arg))
25439 return true;
25440 }
25441 return false;
25442 }
25443
25444 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25445 expressions) contains any type-dependent expressions. */
25446
25447 bool
25448 any_type_dependent_elements_p (const_tree list)
25449 {
25450 for (; list; list = TREE_CHAIN (list))
25451 if (type_dependent_expression_p (TREE_VALUE (list)))
25452 return true;
25453
25454 return false;
25455 }
25456
25457 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25458 expressions) contains any value-dependent expressions. */
25459
25460 bool
25461 any_value_dependent_elements_p (const_tree list)
25462 {
25463 for (; list; list = TREE_CHAIN (list))
25464 if (value_dependent_expression_p (TREE_VALUE (list)))
25465 return true;
25466
25467 return false;
25468 }
25469
25470 /* Returns TRUE if the ARG (a template argument) is dependent. */
25471
25472 bool
25473 dependent_template_arg_p (tree arg)
25474 {
25475 if (!processing_template_decl)
25476 return false;
25477
25478 /* Assume a template argument that was wrongly written by the user
25479 is dependent. This is consistent with what
25480 any_dependent_template_arguments_p [that calls this function]
25481 does. */
25482 if (!arg || arg == error_mark_node)
25483 return true;
25484
25485 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25486 arg = argument_pack_select_arg (arg);
25487
25488 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25489 return true;
25490 if (TREE_CODE (arg) == TEMPLATE_DECL)
25491 {
25492 if (DECL_TEMPLATE_PARM_P (arg))
25493 return true;
25494 /* A member template of a dependent class is not necessarily
25495 type-dependent, but it is a dependent template argument because it
25496 will be a member of an unknown specialization to that template. */
25497 tree scope = CP_DECL_CONTEXT (arg);
25498 return TYPE_P (scope) && dependent_type_p (scope);
25499 }
25500 else if (ARGUMENT_PACK_P (arg))
25501 {
25502 tree args = ARGUMENT_PACK_ARGS (arg);
25503 int i, len = TREE_VEC_LENGTH (args);
25504 for (i = 0; i < len; ++i)
25505 {
25506 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25507 return true;
25508 }
25509
25510 return false;
25511 }
25512 else if (TYPE_P (arg))
25513 return dependent_type_p (arg);
25514 else
25515 return (type_dependent_expression_p (arg)
25516 || value_dependent_expression_p (arg));
25517 }
25518
25519 /* Returns true if ARGS (a collection of template arguments) contains
25520 any types that require structural equality testing. */
25521
25522 bool
25523 any_template_arguments_need_structural_equality_p (tree args)
25524 {
25525 int i;
25526 int j;
25527
25528 if (!args)
25529 return false;
25530 if (args == error_mark_node)
25531 return true;
25532
25533 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25534 {
25535 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25536 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25537 {
25538 tree arg = TREE_VEC_ELT (level, j);
25539 tree packed_args = NULL_TREE;
25540 int k, len = 1;
25541
25542 if (ARGUMENT_PACK_P (arg))
25543 {
25544 /* Look inside the argument pack. */
25545 packed_args = ARGUMENT_PACK_ARGS (arg);
25546 len = TREE_VEC_LENGTH (packed_args);
25547 }
25548
25549 for (k = 0; k < len; ++k)
25550 {
25551 if (packed_args)
25552 arg = TREE_VEC_ELT (packed_args, k);
25553
25554 if (error_operand_p (arg))
25555 return true;
25556 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25557 continue;
25558 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25559 return true;
25560 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25561 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25562 return true;
25563 }
25564 }
25565 }
25566
25567 return false;
25568 }
25569
25570 /* Returns true if ARGS (a collection of template arguments) contains
25571 any dependent arguments. */
25572
25573 bool
25574 any_dependent_template_arguments_p (const_tree args)
25575 {
25576 int i;
25577 int j;
25578
25579 if (!args)
25580 return false;
25581 if (args == error_mark_node)
25582 return true;
25583
25584 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25585 {
25586 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25587 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25588 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25589 return true;
25590 }
25591
25592 return false;
25593 }
25594
25595 /* Returns true if ARGS contains any errors. */
25596
25597 bool
25598 any_erroneous_template_args_p (const_tree args)
25599 {
25600 int i;
25601 int j;
25602
25603 if (args == error_mark_node)
25604 return true;
25605
25606 if (args && TREE_CODE (args) != TREE_VEC)
25607 {
25608 if (tree ti = get_template_info (args))
25609 args = TI_ARGS (ti);
25610 else
25611 args = NULL_TREE;
25612 }
25613
25614 if (!args)
25615 return false;
25616
25617 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25618 {
25619 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25620 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25621 if (error_operand_p (TREE_VEC_ELT (level, j)))
25622 return true;
25623 }
25624
25625 return false;
25626 }
25627
25628 /* Returns TRUE if the template TMPL is type-dependent. */
25629
25630 bool
25631 dependent_template_p (tree tmpl)
25632 {
25633 if (TREE_CODE (tmpl) == OVERLOAD)
25634 {
25635 for (lkp_iterator iter (tmpl); iter; ++iter)
25636 if (dependent_template_p (*iter))
25637 return true;
25638 return false;
25639 }
25640
25641 /* Template template parameters are dependent. */
25642 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25643 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25644 return true;
25645 /* So are names that have not been looked up. */
25646 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25647 return true;
25648 return false;
25649 }
25650
25651 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25652
25653 bool
25654 dependent_template_id_p (tree tmpl, tree args)
25655 {
25656 return (dependent_template_p (tmpl)
25657 || any_dependent_template_arguments_p (args));
25658 }
25659
25660 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25661 are dependent. */
25662
25663 bool
25664 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25665 {
25666 int i;
25667
25668 if (!processing_template_decl)
25669 return false;
25670
25671 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25672 {
25673 tree decl = TREE_VEC_ELT (declv, i);
25674 tree init = TREE_VEC_ELT (initv, i);
25675 tree cond = TREE_VEC_ELT (condv, i);
25676 tree incr = TREE_VEC_ELT (incrv, i);
25677
25678 if (type_dependent_expression_p (decl)
25679 || TREE_CODE (decl) == SCOPE_REF)
25680 return true;
25681
25682 if (init && type_dependent_expression_p (init))
25683 return true;
25684
25685 if (type_dependent_expression_p (cond))
25686 return true;
25687
25688 if (COMPARISON_CLASS_P (cond)
25689 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25690 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25691 return true;
25692
25693 if (TREE_CODE (incr) == MODOP_EXPR)
25694 {
25695 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25696 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25697 return true;
25698 }
25699 else if (type_dependent_expression_p (incr))
25700 return true;
25701 else if (TREE_CODE (incr) == MODIFY_EXPR)
25702 {
25703 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25704 return true;
25705 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25706 {
25707 tree t = TREE_OPERAND (incr, 1);
25708 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25709 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25710 return true;
25711 }
25712 }
25713 }
25714
25715 return false;
25716 }
25717
25718 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25719 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25720 no such TYPE can be found. Note that this function peers inside
25721 uninstantiated templates and therefore should be used only in
25722 extremely limited situations. ONLY_CURRENT_P restricts this
25723 peering to the currently open classes hierarchy (which is required
25724 when comparing types). */
25725
25726 tree
25727 resolve_typename_type (tree type, bool only_current_p)
25728 {
25729 tree scope;
25730 tree name;
25731 tree decl;
25732 int quals;
25733 tree pushed_scope;
25734 tree result;
25735
25736 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25737
25738 scope = TYPE_CONTEXT (type);
25739 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25740 gcc_checking_assert (uses_template_parms (scope));
25741
25742 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25743 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25744 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25745 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25746 identifier of the TYPENAME_TYPE anymore.
25747 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25748 TYPENAME_TYPE instead, we avoid messing up with a possible
25749 typedef variant case. */
25750 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25751
25752 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25753 it first before we can figure out what NAME refers to. */
25754 if (TREE_CODE (scope) == TYPENAME_TYPE)
25755 {
25756 if (TYPENAME_IS_RESOLVING_P (scope))
25757 /* Given a class template A with a dependent base with nested type C,
25758 typedef typename A::C::C C will land us here, as trying to resolve
25759 the initial A::C leads to the local C typedef, which leads back to
25760 A::C::C. So we break the recursion now. */
25761 return type;
25762 else
25763 scope = resolve_typename_type (scope, only_current_p);
25764 }
25765 /* If we don't know what SCOPE refers to, then we cannot resolve the
25766 TYPENAME_TYPE. */
25767 if (!CLASS_TYPE_P (scope))
25768 return type;
25769 /* If this is a typedef, we don't want to look inside (c++/11987). */
25770 if (typedef_variant_p (type))
25771 return type;
25772 /* If SCOPE isn't the template itself, it will not have a valid
25773 TYPE_FIELDS list. */
25774 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25775 /* scope is either the template itself or a compatible instantiation
25776 like X<T>, so look up the name in the original template. */
25777 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25778 /* If scope has no fields, it can't be a current instantiation. Check this
25779 before currently_open_class to avoid infinite recursion (71515). */
25780 if (!TYPE_FIELDS (scope))
25781 return type;
25782 /* If the SCOPE is not the current instantiation, there's no reason
25783 to look inside it. */
25784 if (only_current_p && !currently_open_class (scope))
25785 return type;
25786 /* Enter the SCOPE so that name lookup will be resolved as if we
25787 were in the class definition. In particular, SCOPE will no
25788 longer be considered a dependent type. */
25789 pushed_scope = push_scope (scope);
25790 /* Look up the declaration. */
25791 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25792 tf_warning_or_error);
25793
25794 result = NULL_TREE;
25795
25796 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25797 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25798 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25799 if (!decl)
25800 /*nop*/;
25801 else if (identifier_p (fullname)
25802 && TREE_CODE (decl) == TYPE_DECL)
25803 {
25804 result = TREE_TYPE (decl);
25805 if (result == error_mark_node)
25806 result = NULL_TREE;
25807 }
25808 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25809 && DECL_CLASS_TEMPLATE_P (decl))
25810 {
25811 /* Obtain the template and the arguments. */
25812 tree tmpl = TREE_OPERAND (fullname, 0);
25813 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25814 {
25815 /* We get here with a plain identifier because a previous tentative
25816 parse of the nested-name-specifier as part of a ptr-operator saw
25817 ::template X<A>. The use of ::template is necessary in a
25818 ptr-operator, but wrong in a declarator-id.
25819
25820 [temp.names]: In a qualified-id of a declarator-id, the keyword
25821 template shall not appear at the top level. */
25822 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
25823 "keyword %<template%> not allowed in declarator-id");
25824 tmpl = decl;
25825 }
25826 tree args = TREE_OPERAND (fullname, 1);
25827 /* Instantiate the template. */
25828 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25829 /*entering_scope=*/true,
25830 tf_error | tf_user);
25831 if (result == error_mark_node)
25832 result = NULL_TREE;
25833 }
25834
25835 /* Leave the SCOPE. */
25836 if (pushed_scope)
25837 pop_scope (pushed_scope);
25838
25839 /* If we failed to resolve it, return the original typename. */
25840 if (!result)
25841 return type;
25842
25843 /* If lookup found a typename type, resolve that too. */
25844 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25845 {
25846 /* Ill-formed programs can cause infinite recursion here, so we
25847 must catch that. */
25848 TYPENAME_IS_RESOLVING_P (result) = 1;
25849 result = resolve_typename_type (result, only_current_p);
25850 TYPENAME_IS_RESOLVING_P (result) = 0;
25851 }
25852
25853 /* Qualify the resulting type. */
25854 quals = cp_type_quals (type);
25855 if (quals)
25856 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25857
25858 return result;
25859 }
25860
25861 /* EXPR is an expression which is not type-dependent. Return a proxy
25862 for EXPR that can be used to compute the types of larger
25863 expressions containing EXPR. */
25864
25865 tree
25866 build_non_dependent_expr (tree expr)
25867 {
25868 tree orig_expr = expr;
25869 tree inner_expr;
25870
25871 /* When checking, try to get a constant value for all non-dependent
25872 expressions in order to expose bugs in *_dependent_expression_p
25873 and constexpr. This can affect code generation, see PR70704, so
25874 only do this for -fchecking=2. */
25875 if (flag_checking > 1
25876 && cxx_dialect >= cxx11
25877 /* Don't do this during nsdmi parsing as it can lead to
25878 unexpected recursive instantiations. */
25879 && !parsing_nsdmi ()
25880 /* Don't do this during concept expansion either and for
25881 the same reason. */
25882 && !expanding_concept ())
25883 fold_non_dependent_expr (expr, tf_none);
25884
25885 STRIP_ANY_LOCATION_WRAPPER (expr);
25886
25887 /* Preserve OVERLOADs; the functions must be available to resolve
25888 types. */
25889 inner_expr = expr;
25890 if (TREE_CODE (inner_expr) == STMT_EXPR)
25891 inner_expr = stmt_expr_value_expr (inner_expr);
25892 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25893 inner_expr = TREE_OPERAND (inner_expr, 0);
25894 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25895 inner_expr = TREE_OPERAND (inner_expr, 1);
25896 if (is_overloaded_fn (inner_expr)
25897 || TREE_CODE (inner_expr) == OFFSET_REF)
25898 return orig_expr;
25899 /* There is no need to return a proxy for a variable or enumerator. */
25900 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
25901 return orig_expr;
25902 /* Preserve string constants; conversions from string constants to
25903 "char *" are allowed, even though normally a "const char *"
25904 cannot be used to initialize a "char *". */
25905 if (TREE_CODE (expr) == STRING_CST)
25906 return orig_expr;
25907 /* Preserve void and arithmetic constants, as an optimization -- there is no
25908 reason to create a new node. */
25909 if (TREE_CODE (expr) == VOID_CST
25910 || TREE_CODE (expr) == INTEGER_CST
25911 || TREE_CODE (expr) == REAL_CST)
25912 return orig_expr;
25913 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25914 There is at least one place where we want to know that a
25915 particular expression is a throw-expression: when checking a ?:
25916 expression, there are special rules if the second or third
25917 argument is a throw-expression. */
25918 if (TREE_CODE (expr) == THROW_EXPR)
25919 return orig_expr;
25920
25921 /* Don't wrap an initializer list, we need to be able to look inside. */
25922 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25923 return orig_expr;
25924
25925 /* Don't wrap a dummy object, we need to be able to test for it. */
25926 if (is_dummy_object (expr))
25927 return orig_expr;
25928
25929 if (TREE_CODE (expr) == COND_EXPR)
25930 return build3 (COND_EXPR,
25931 TREE_TYPE (expr),
25932 TREE_OPERAND (expr, 0),
25933 (TREE_OPERAND (expr, 1)
25934 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25935 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25936 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25937 if (TREE_CODE (expr) == COMPOUND_EXPR
25938 && !COMPOUND_EXPR_OVERLOADED (expr))
25939 return build2 (COMPOUND_EXPR,
25940 TREE_TYPE (expr),
25941 TREE_OPERAND (expr, 0),
25942 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25943
25944 /* If the type is unknown, it can't really be non-dependent */
25945 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25946
25947 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25948 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
25949 TREE_TYPE (expr), expr);
25950 }
25951
25952 /* ARGS is a vector of expressions as arguments to a function call.
25953 Replace the arguments with equivalent non-dependent expressions.
25954 This modifies ARGS in place. */
25955
25956 void
25957 make_args_non_dependent (vec<tree, va_gc> *args)
25958 {
25959 unsigned int ix;
25960 tree arg;
25961
25962 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25963 {
25964 tree newarg = build_non_dependent_expr (arg);
25965 if (newarg != arg)
25966 (*args)[ix] = newarg;
25967 }
25968 }
25969
25970 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25971 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25972 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25973
25974 static tree
25975 make_auto_1 (tree name, bool set_canonical)
25976 {
25977 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25978 TYPE_NAME (au) = build_decl (input_location,
25979 TYPE_DECL, name, au);
25980 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25981 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25982 (0, processing_template_decl + 1, processing_template_decl + 1,
25983 TYPE_NAME (au), NULL_TREE);
25984 if (set_canonical)
25985 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25986 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25987 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25988
25989 return au;
25990 }
25991
25992 tree
25993 make_decltype_auto (void)
25994 {
25995 return make_auto_1 (decltype_auto_identifier, true);
25996 }
25997
25998 tree
25999 make_auto (void)
26000 {
26001 return make_auto_1 (auto_identifier, true);
26002 }
26003
26004 /* Return a C++17 deduction placeholder for class template TMPL. */
26005
26006 tree
26007 make_template_placeholder (tree tmpl)
26008 {
26009 tree t = make_auto_1 (DECL_NAME (tmpl), true);
26010 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26011 return t;
26012 }
26013
26014 /* True iff T is a C++17 class template deduction placeholder. */
26015
26016 bool
26017 template_placeholder_p (tree t)
26018 {
26019 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26020 }
26021
26022 /* Make a "constrained auto" type-specifier. This is an
26023 auto type with constraints that must be associated after
26024 deduction. The constraint is formed from the given
26025 CONC and its optional sequence of arguments, which are
26026 non-null if written as partial-concept-id. */
26027
26028 tree
26029 make_constrained_auto (tree con, tree args)
26030 {
26031 tree type = make_auto_1 (auto_identifier, false);
26032
26033 /* Build the constraint. */
26034 tree tmpl = DECL_TI_TEMPLATE (con);
26035 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26036 expr = build_concept_check (expr, type, args);
26037
26038 tree constr = normalize_expression (expr);
26039 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26040
26041 /* Our canonical type depends on the constraint. */
26042 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26043
26044 /* Attach the constraint to the type declaration. */
26045 tree decl = TYPE_NAME (type);
26046 return decl;
26047 }
26048
26049 /* Given type ARG, return std::initializer_list<ARG>. */
26050
26051 static tree
26052 listify (tree arg)
26053 {
26054 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26055
26056 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26057 {
26058 gcc_rich_location richloc (input_location);
26059 maybe_add_include_fixit (&richloc, "<initializer_list>");
26060 error_at (&richloc,
26061 "deducing from brace-enclosed initializer list"
26062 " requires %<#include <initializer_list>%>");
26063
26064 return error_mark_node;
26065 }
26066 tree argvec = make_tree_vec (1);
26067 TREE_VEC_ELT (argvec, 0) = arg;
26068
26069 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26070 NULL_TREE, 0, tf_warning_or_error);
26071 }
26072
26073 /* Replace auto in TYPE with std::initializer_list<auto>. */
26074
26075 static tree
26076 listify_autos (tree type, tree auto_node)
26077 {
26078 tree init_auto = listify (auto_node);
26079 tree argvec = make_tree_vec (1);
26080 TREE_VEC_ELT (argvec, 0) = init_auto;
26081 if (processing_template_decl)
26082 argvec = add_to_template_args (current_template_args (), argvec);
26083 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26084 }
26085
26086 /* Hash traits for hashing possibly constrained 'auto'
26087 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26088
26089 struct auto_hash : default_hash_traits<tree>
26090 {
26091 static inline hashval_t hash (tree);
26092 static inline bool equal (tree, tree);
26093 };
26094
26095 /* Hash the 'auto' T. */
26096
26097 inline hashval_t
26098 auto_hash::hash (tree t)
26099 {
26100 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
26101 /* Matching constrained-type-specifiers denote the same template
26102 parameter, so hash the constraint. */
26103 return hash_placeholder_constraint (c);
26104 else
26105 /* But unconstrained autos are all separate, so just hash the pointer. */
26106 return iterative_hash_object (t, 0);
26107 }
26108
26109 /* Compare two 'auto's. */
26110
26111 inline bool
26112 auto_hash::equal (tree t1, tree t2)
26113 {
26114 if (t1 == t2)
26115 return true;
26116
26117 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26118 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26119
26120 /* Two unconstrained autos are distinct. */
26121 if (!c1 || !c2)
26122 return false;
26123
26124 return equivalent_placeholder_constraints (c1, c2);
26125 }
26126
26127 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26128 constrained) auto, add it to the vector. */
26129
26130 static int
26131 extract_autos_r (tree t, void *data)
26132 {
26133 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26134 if (is_auto (t))
26135 {
26136 /* All the autos were built with index 0; fix that up now. */
26137 tree *p = hash.find_slot (t, INSERT);
26138 unsigned idx;
26139 if (*p)
26140 /* If this is a repeated constrained-type-specifier, use the index we
26141 chose before. */
26142 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26143 else
26144 {
26145 /* Otherwise this is new, so use the current count. */
26146 *p = t;
26147 idx = hash.elements () - 1;
26148 }
26149 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26150 }
26151
26152 /* Always keep walking. */
26153 return 0;
26154 }
26155
26156 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26157 says they can appear anywhere in the type. */
26158
26159 static tree
26160 extract_autos (tree type)
26161 {
26162 hash_set<tree> visited;
26163 hash_table<auto_hash> hash (2);
26164
26165 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26166
26167 tree tree_vec = make_tree_vec (hash.elements());
26168 for (hash_table<auto_hash>::iterator iter = hash.begin();
26169 iter != hash.end(); ++iter)
26170 {
26171 tree elt = *iter;
26172 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26173 TREE_VEC_ELT (tree_vec, i)
26174 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26175 }
26176
26177 return tree_vec;
26178 }
26179
26180 /* The stem for deduction guide names. */
26181 const char *const dguide_base = "__dguide_";
26182
26183 /* Return the name for a deduction guide for class template TMPL. */
26184
26185 tree
26186 dguide_name (tree tmpl)
26187 {
26188 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26189 tree tname = TYPE_IDENTIFIER (type);
26190 char *buf = (char *) alloca (1 + strlen (dguide_base)
26191 + IDENTIFIER_LENGTH (tname));
26192 memcpy (buf, dguide_base, strlen (dguide_base));
26193 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26194 IDENTIFIER_LENGTH (tname) + 1);
26195 tree dname = get_identifier (buf);
26196 TREE_TYPE (dname) = type;
26197 return dname;
26198 }
26199
26200 /* True if NAME is the name of a deduction guide. */
26201
26202 bool
26203 dguide_name_p (tree name)
26204 {
26205 return (TREE_CODE (name) == IDENTIFIER_NODE
26206 && TREE_TYPE (name)
26207 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26208 strlen (dguide_base)));
26209 }
26210
26211 /* True if FN is a deduction guide. */
26212
26213 bool
26214 deduction_guide_p (const_tree fn)
26215 {
26216 if (DECL_P (fn))
26217 if (tree name = DECL_NAME (fn))
26218 return dguide_name_p (name);
26219 return false;
26220 }
26221
26222 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26223
26224 bool
26225 copy_guide_p (const_tree fn)
26226 {
26227 gcc_assert (deduction_guide_p (fn));
26228 if (!DECL_ARTIFICIAL (fn))
26229 return false;
26230 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26231 return (TREE_CHAIN (parms) == void_list_node
26232 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26233 }
26234
26235 /* True if FN is a guide generated from a constructor template. */
26236
26237 bool
26238 template_guide_p (const_tree fn)
26239 {
26240 gcc_assert (deduction_guide_p (fn));
26241 if (!DECL_ARTIFICIAL (fn))
26242 return false;
26243 tree tmpl = DECL_TI_TEMPLATE (fn);
26244 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26245 return PRIMARY_TEMPLATE_P (org);
26246 return false;
26247 }
26248
26249 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26250 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26251 template parameter types. Note that the handling of template template
26252 parameters relies on current_template_parms being set appropriately for the
26253 new template. */
26254
26255 static tree
26256 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26257 tree tsubst_args, tsubst_flags_t complain)
26258 {
26259 if (olddecl == error_mark_node)
26260 return error_mark_node;
26261
26262 tree oldidx = get_template_parm_index (olddecl);
26263
26264 tree newtype;
26265 if (TREE_CODE (olddecl) == TYPE_DECL
26266 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26267 {
26268 tree oldtype = TREE_TYPE (olddecl);
26269 newtype = cxx_make_type (TREE_CODE (oldtype));
26270 TYPE_MAIN_VARIANT (newtype) = newtype;
26271 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
26272 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
26273 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
26274 }
26275 else
26276 {
26277 newtype = TREE_TYPE (olddecl);
26278 if (type_uses_auto (newtype))
26279 {
26280 // Substitute once to fix references to other template parameters.
26281 newtype = tsubst (newtype, tsubst_args,
26282 complain|tf_partial, NULL_TREE);
26283 // Now substitute again to reduce the level of the auto.
26284 newtype = tsubst (newtype, current_template_args (),
26285 complain, NULL_TREE);
26286 }
26287 else
26288 newtype = tsubst (newtype, tsubst_args,
26289 complain, NULL_TREE);
26290 }
26291
26292 tree newdecl
26293 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
26294 DECL_NAME (olddecl), newtype);
26295 SET_DECL_TEMPLATE_PARM_P (newdecl);
26296
26297 tree newidx;
26298 if (TREE_CODE (olddecl) == TYPE_DECL
26299 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26300 {
26301 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
26302 = build_template_parm_index (index, level, level,
26303 newdecl, newtype);
26304 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26305 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26306 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
26307 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
26308
26309 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
26310 {
26311 DECL_TEMPLATE_RESULT (newdecl)
26312 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
26313 DECL_NAME (olddecl), newtype);
26314 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
26315 // First create a copy (ttargs) of tsubst_args with an
26316 // additional level for the template template parameter's own
26317 // template parameters (ttparms).
26318 tree ttparms = (INNERMOST_TEMPLATE_PARMS
26319 (DECL_TEMPLATE_PARMS (olddecl)));
26320 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
26321 tree ttargs = make_tree_vec (depth + 1);
26322 for (int i = 0; i < depth; ++i)
26323 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
26324 TREE_VEC_ELT (ttargs, depth)
26325 = template_parms_level_to_args (ttparms);
26326 // Substitute ttargs into ttparms to fix references to
26327 // other template parameters.
26328 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26329 complain|tf_partial);
26330 // Now substitute again with args based on tparms, to reduce
26331 // the level of the ttparms.
26332 ttargs = current_template_args ();
26333 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26334 complain);
26335 // Finally, tack the adjusted parms onto tparms.
26336 ttparms = tree_cons (size_int (depth), ttparms,
26337 current_template_parms);
26338 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
26339 }
26340 }
26341 else
26342 {
26343 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
26344 tree newconst
26345 = build_decl (DECL_SOURCE_LOCATION (oldconst),
26346 TREE_CODE (oldconst),
26347 DECL_NAME (oldconst), newtype);
26348 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
26349 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
26350 SET_DECL_TEMPLATE_PARM_P (newconst);
26351 newidx = build_template_parm_index (index, level, level,
26352 newconst, newtype);
26353 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26354 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26355 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
26356 }
26357
26358 return newdecl;
26359 }
26360
26361 /* Returns a C++17 class deduction guide template based on the constructor
26362 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26363 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26364
26365 static tree
26366 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
26367 {
26368 tree type, tparms, targs, fparms, fargs, ci;
26369 bool memtmpl = false;
26370 bool explicit_p;
26371 location_t loc;
26372 tree fn_tmpl = NULL_TREE;
26373
26374 if (TYPE_P (ctor))
26375 {
26376 type = ctor;
26377 bool copy_p = TYPE_REF_P (type);
26378 if (copy_p)
26379 {
26380 type = TREE_TYPE (type);
26381 fparms = tree_cons (NULL_TREE, type, void_list_node);
26382 }
26383 else
26384 fparms = void_list_node;
26385
26386 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
26387 tparms = DECL_TEMPLATE_PARMS (ctmpl);
26388 targs = CLASSTYPE_TI_ARGS (type);
26389 ci = NULL_TREE;
26390 fargs = NULL_TREE;
26391 loc = DECL_SOURCE_LOCATION (ctmpl);
26392 explicit_p = false;
26393 }
26394 else
26395 {
26396 ++processing_template_decl;
26397 bool ok = true;
26398
26399 fn_tmpl
26400 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
26401 : DECL_TI_TEMPLATE (ctor));
26402 if (outer_args)
26403 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
26404 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
26405
26406 type = DECL_CONTEXT (ctor);
26407
26408 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
26409 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26410 fully specialized args for the enclosing class. Strip those off, as
26411 the deduction guide won't have those template parameters. */
26412 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
26413 TMPL_PARMS_DEPTH (tparms));
26414 /* Discard the 'this' parameter. */
26415 fparms = FUNCTION_ARG_CHAIN (ctor);
26416 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
26417 ci = get_constraints (ctor);
26418 loc = DECL_SOURCE_LOCATION (ctor);
26419 explicit_p = DECL_NONCONVERTING_P (ctor);
26420
26421 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26422 {
26423 memtmpl = true;
26424
26425 /* For a member template constructor, we need to flatten the two
26426 template parameter lists into one, and then adjust the function
26427 signature accordingly. This gets...complicated. */
26428 tree save_parms = current_template_parms;
26429
26430 /* For a member template we should have two levels of parms/args, one
26431 for the class and one for the constructor. We stripped
26432 specialized args for further enclosing classes above. */
26433 const int depth = 2;
26434 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26435
26436 /* Template args for translating references to the two-level template
26437 parameters into references to the one-level template parameters we
26438 are creating. */
26439 tree tsubst_args = copy_node (targs);
26440 TMPL_ARGS_LEVEL (tsubst_args, depth)
26441 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
26442
26443 /* Template parms for the constructor template. */
26444 tree ftparms = TREE_VALUE (tparms);
26445 unsigned flen = TREE_VEC_LENGTH (ftparms);
26446 /* Template parms for the class template. */
26447 tparms = TREE_CHAIN (tparms);
26448 tree ctparms = TREE_VALUE (tparms);
26449 unsigned clen = TREE_VEC_LENGTH (ctparms);
26450 /* Template parms for the deduction guide start as a copy of the
26451 template parms for the class. We set current_template_parms for
26452 lookup_template_class_1. */
26453 current_template_parms = tparms = copy_node (tparms);
26454 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
26455 for (unsigned i = 0; i < clen; ++i)
26456 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
26457
26458 /* Now we need to rewrite the constructor parms to append them to the
26459 class parms. */
26460 for (unsigned i = 0; i < flen; ++i)
26461 {
26462 unsigned index = i + clen;
26463 unsigned level = 1;
26464 tree oldelt = TREE_VEC_ELT (ftparms, i);
26465 tree olddecl = TREE_VALUE (oldelt);
26466 tree newdecl = rewrite_template_parm (olddecl, index, level,
26467 tsubst_args, complain);
26468 if (newdecl == error_mark_node)
26469 ok = false;
26470 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
26471 tsubst_args, complain, ctor);
26472 tree list = build_tree_list (newdef, newdecl);
26473 TEMPLATE_PARM_CONSTRAINTS (list)
26474 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
26475 tsubst_args, complain, ctor);
26476 TREE_VEC_ELT (new_vec, index) = list;
26477 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26478 }
26479
26480 /* Now we have a final set of template parms to substitute into the
26481 function signature. */
26482 targs = template_parms_to_args (tparms);
26483 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26484 complain, ctor);
26485 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26486 if (ci)
26487 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26488
26489 current_template_parms = save_parms;
26490 }
26491
26492 --processing_template_decl;
26493 if (!ok)
26494 return error_mark_node;
26495 }
26496
26497 if (!memtmpl)
26498 {
26499 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26500 tparms = copy_node (tparms);
26501 INNERMOST_TEMPLATE_PARMS (tparms)
26502 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26503 }
26504
26505 tree fntype = build_function_type (type, fparms);
26506 tree ded_fn = build_lang_decl_loc (loc,
26507 FUNCTION_DECL,
26508 dguide_name (type), fntype);
26509 DECL_ARGUMENTS (ded_fn) = fargs;
26510 DECL_ARTIFICIAL (ded_fn) = true;
26511 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26512 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26513 DECL_ARTIFICIAL (ded_tmpl) = true;
26514 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26515 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26516 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26517 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26518 if (DECL_P (ctor))
26519 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26520 if (ci)
26521 set_constraints (ded_tmpl, ci);
26522
26523 return ded_tmpl;
26524 }
26525
26526 /* Deduce template arguments for the class template placeholder PTYPE for
26527 template TMPL based on the initializer INIT, and return the resulting
26528 type. */
26529
26530 static tree
26531 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26532 tsubst_flags_t complain)
26533 {
26534 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26535 {
26536 /* We should have handled this in the caller. */
26537 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26538 return ptype;
26539 if (complain & tf_error)
26540 error ("non-class template %qT used without template arguments", tmpl);
26541 return error_mark_node;
26542 }
26543
26544 tree type = TREE_TYPE (tmpl);
26545
26546 bool try_list_ctor = false;
26547
26548 vec<tree,va_gc> *args;
26549 if (init == NULL_TREE
26550 || TREE_CODE (init) == TREE_LIST)
26551 args = make_tree_vector_from_list (init);
26552 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26553 {
26554 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26555 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26556 {
26557 /* As an exception, the first phase in 16.3.1.7 (considering the
26558 initializer list as a single argument) is omitted if the
26559 initializer list consists of a single expression of type cv U,
26560 where U is a specialization of C or a class derived from a
26561 specialization of C. */
26562 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26563 tree etype = TREE_TYPE (elt);
26564
26565 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26566 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26567 int err = unify (tparms, targs, type, etype,
26568 UNIFY_ALLOW_DERIVED, /*explain*/false);
26569 if (err == 0)
26570 try_list_ctor = false;
26571 ggc_free (targs);
26572 }
26573 if (try_list_ctor || is_std_init_list (type))
26574 args = make_tree_vector_single (init);
26575 else
26576 args = make_tree_vector_from_ctor (init);
26577 }
26578 else
26579 args = make_tree_vector_single (init);
26580
26581 tree dname = dguide_name (tmpl);
26582 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26583 /*type*/false, /*complain*/false,
26584 /*hidden*/false);
26585 bool elided = false;
26586 if (cands == error_mark_node)
26587 cands = NULL_TREE;
26588
26589 /* Prune explicit deduction guides in copy-initialization context. */
26590 if (flags & LOOKUP_ONLYCONVERTING)
26591 {
26592 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26593 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26594 elided = true;
26595
26596 if (elided)
26597 {
26598 /* Found a nonconverting guide, prune the candidates. */
26599 tree pruned = NULL_TREE;
26600 for (lkp_iterator iter (cands); iter; ++iter)
26601 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26602 pruned = lookup_add (*iter, pruned);
26603
26604 cands = pruned;
26605 }
26606 }
26607
26608 tree outer_args = NULL_TREE;
26609 if (DECL_CLASS_SCOPE_P (tmpl)
26610 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26611 {
26612 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26613 type = TREE_TYPE (most_general_template (tmpl));
26614 }
26615
26616 bool saw_ctor = false;
26617 // FIXME cache artificial deduction guides
26618 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26619 {
26620 /* Skip inherited constructors. */
26621 if (iter.using_p ())
26622 continue;
26623
26624 tree guide = build_deduction_guide (*iter, outer_args, complain);
26625 if (guide == error_mark_node)
26626 return error_mark_node;
26627 if ((flags & LOOKUP_ONLYCONVERTING)
26628 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26629 elided = true;
26630 else
26631 cands = lookup_add (guide, cands);
26632
26633 saw_ctor = true;
26634 }
26635
26636 tree call = error_mark_node;
26637
26638 /* If this is list-initialization and the class has a list constructor, first
26639 try deducing from the list as a single argument, as [over.match.list]. */
26640 tree list_cands = NULL_TREE;
26641 if (try_list_ctor && cands)
26642 for (lkp_iterator iter (cands); iter; ++iter)
26643 {
26644 tree dg = *iter;
26645 if (is_list_ctor (dg))
26646 list_cands = lookup_add (dg, list_cands);
26647 }
26648 if (list_cands)
26649 {
26650 ++cp_unevaluated_operand;
26651 call = build_new_function_call (list_cands, &args, tf_decltype);
26652 --cp_unevaluated_operand;
26653
26654 if (call == error_mark_node)
26655 {
26656 /* That didn't work, now try treating the list as a sequence of
26657 arguments. */
26658 release_tree_vector (args);
26659 args = make_tree_vector_from_ctor (init);
26660 }
26661 }
26662
26663 /* Maybe generate an implicit deduction guide. */
26664 if (call == error_mark_node && args->length () < 2)
26665 {
26666 tree gtype = NULL_TREE;
26667
26668 if (args->length () == 1)
26669 /* Generate a copy guide. */
26670 gtype = build_reference_type (type);
26671 else if (!saw_ctor)
26672 /* Generate a default guide. */
26673 gtype = type;
26674
26675 if (gtype)
26676 {
26677 tree guide = build_deduction_guide (gtype, outer_args, complain);
26678 if (guide == error_mark_node)
26679 return error_mark_node;
26680 cands = lookup_add (guide, cands);
26681 }
26682 }
26683
26684 if (elided && !cands)
26685 {
26686 error ("cannot deduce template arguments for copy-initialization"
26687 " of %qT, as it has no non-explicit deduction guides or "
26688 "user-declared constructors", type);
26689 return error_mark_node;
26690 }
26691 else if (!cands && call == error_mark_node)
26692 {
26693 error ("cannot deduce template arguments of %qT, as it has no viable "
26694 "deduction guides", type);
26695 return error_mark_node;
26696 }
26697
26698 if (call == error_mark_node)
26699 {
26700 ++cp_unevaluated_operand;
26701 call = build_new_function_call (cands, &args, tf_decltype);
26702 --cp_unevaluated_operand;
26703 }
26704
26705 if (call == error_mark_node && (complain & tf_warning_or_error))
26706 {
26707 error ("class template argument deduction failed:");
26708
26709 ++cp_unevaluated_operand;
26710 call = build_new_function_call (cands, &args, complain | tf_decltype);
26711 --cp_unevaluated_operand;
26712
26713 if (elided)
26714 inform (input_location, "explicit deduction guides not considered "
26715 "for copy-initialization");
26716 }
26717
26718 release_tree_vector (args);
26719
26720 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26721 }
26722
26723 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26724 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26725 The CONTEXT determines the context in which auto deduction is performed
26726 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26727 OUTER_TARGS are used during template argument deduction
26728 (context == adc_unify) to properly substitute the result, and is ignored
26729 in other contexts.
26730
26731 For partial-concept-ids, extra args may be appended to the list of deduced
26732 template arguments prior to determining constraint satisfaction. */
26733
26734 tree
26735 do_auto_deduction (tree type, tree init, tree auto_node,
26736 tsubst_flags_t complain, auto_deduction_context context,
26737 tree outer_targs, int flags)
26738 {
26739 tree targs;
26740
26741 if (init == error_mark_node)
26742 return error_mark_node;
26743
26744 if (init && type_dependent_expression_p (init)
26745 && context != adc_unify)
26746 /* Defining a subset of type-dependent expressions that we can deduce
26747 from ahead of time isn't worth the trouble. */
26748 return type;
26749
26750 /* Similarly, we can't deduce from another undeduced decl. */
26751 if (init && undeduced_auto_decl (init))
26752 return type;
26753
26754 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26755 /* C++17 class template argument deduction. */
26756 return do_class_deduction (type, tmpl, init, flags, complain);
26757
26758 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26759 /* Nothing we can do with this, even in deduction context. */
26760 return type;
26761
26762 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26763 with either a new invented type template parameter U or, if the
26764 initializer is a braced-init-list (8.5.4), with
26765 std::initializer_list<U>. */
26766 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26767 {
26768 if (!DIRECT_LIST_INIT_P (init))
26769 type = listify_autos (type, auto_node);
26770 else if (CONSTRUCTOR_NELTS (init) == 1)
26771 init = CONSTRUCTOR_ELT (init, 0)->value;
26772 else
26773 {
26774 if (complain & tf_warning_or_error)
26775 {
26776 if (permerror (input_location, "direct-list-initialization of "
26777 "%<auto%> requires exactly one element"))
26778 inform (input_location,
26779 "for deduction to %<std::initializer_list%>, use copy-"
26780 "list-initialization (i.e. add %<=%> before the %<{%>)");
26781 }
26782 type = listify_autos (type, auto_node);
26783 }
26784 }
26785
26786 if (type == error_mark_node)
26787 return error_mark_node;
26788
26789 init = resolve_nondeduced_context (init, complain);
26790
26791 if (context == adc_decomp_type
26792 && auto_node == type
26793 && init != error_mark_node
26794 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26795 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26796 and initializer has array type, deduce cv-qualified array type. */
26797 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26798 complain);
26799 else if (AUTO_IS_DECLTYPE (auto_node))
26800 {
26801 bool id = (DECL_P (init)
26802 || ((TREE_CODE (init) == COMPONENT_REF
26803 || TREE_CODE (init) == SCOPE_REF)
26804 && !REF_PARENTHESIZED_P (init)));
26805 targs = make_tree_vec (1);
26806 TREE_VEC_ELT (targs, 0)
26807 = finish_decltype_type (init, id, tf_warning_or_error);
26808 if (type != auto_node)
26809 {
26810 if (complain & tf_error)
26811 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26812 return error_mark_node;
26813 }
26814 }
26815 else
26816 {
26817 tree parms = build_tree_list (NULL_TREE, type);
26818 tree tparms;
26819
26820 if (flag_concepts)
26821 tparms = extract_autos (type);
26822 else
26823 {
26824 tparms = make_tree_vec (1);
26825 TREE_VEC_ELT (tparms, 0)
26826 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26827 }
26828
26829 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26830 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26831 DEDUCE_CALL,
26832 NULL, /*explain_p=*/false);
26833 if (val > 0)
26834 {
26835 if (processing_template_decl)
26836 /* Try again at instantiation time. */
26837 return type;
26838 if (type && type != error_mark_node
26839 && (complain & tf_error))
26840 /* If type is error_mark_node a diagnostic must have been
26841 emitted by now. Also, having a mention to '<type error>'
26842 in the diagnostic is not really useful to the user. */
26843 {
26844 if (cfun && auto_node == current_function_auto_return_pattern
26845 && LAMBDA_FUNCTION_P (current_function_decl))
26846 error ("unable to deduce lambda return type from %qE", init);
26847 else
26848 error ("unable to deduce %qT from %qE", type, init);
26849 type_unification_real (tparms, targs, parms, &init, 1, 0,
26850 DEDUCE_CALL,
26851 NULL, /*explain_p=*/true);
26852 }
26853 return error_mark_node;
26854 }
26855 }
26856
26857 /* Check any placeholder constraints against the deduced type. */
26858 if (flag_concepts && !processing_template_decl)
26859 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26860 {
26861 /* Use the deduced type to check the associated constraints. If we
26862 have a partial-concept-id, rebuild the argument list so that
26863 we check using the extra arguments. */
26864 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26865 tree cargs = CHECK_CONSTR_ARGS (constr);
26866 if (TREE_VEC_LENGTH (cargs) > 1)
26867 {
26868 cargs = copy_node (cargs);
26869 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26870 }
26871 else
26872 cargs = targs;
26873 if (!constraints_satisfied_p (constr, cargs))
26874 {
26875 if (complain & tf_warning_or_error)
26876 {
26877 switch (context)
26878 {
26879 case adc_unspecified:
26880 case adc_unify:
26881 error("placeholder constraints not satisfied");
26882 break;
26883 case adc_variable_type:
26884 case adc_decomp_type:
26885 error ("deduced initializer does not satisfy "
26886 "placeholder constraints");
26887 break;
26888 case adc_return_type:
26889 error ("deduced return type does not satisfy "
26890 "placeholder constraints");
26891 break;
26892 case adc_requirement:
26893 error ("deduced expression type does not satisfy "
26894 "placeholder constraints");
26895 break;
26896 }
26897 diagnose_constraints (input_location, constr, targs);
26898 }
26899 return error_mark_node;
26900 }
26901 }
26902
26903 if (processing_template_decl && context != adc_unify)
26904 outer_targs = current_template_args ();
26905 targs = add_to_template_args (outer_targs, targs);
26906 return tsubst (type, targs, complain, NULL_TREE);
26907 }
26908
26909 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26910 result. */
26911
26912 tree
26913 splice_late_return_type (tree type, tree late_return_type)
26914 {
26915 if (is_auto (type))
26916 {
26917 if (late_return_type)
26918 return late_return_type;
26919
26920 tree idx = get_template_parm_index (type);
26921 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26922 /* In an abbreviated function template we didn't know we were dealing
26923 with a function template when we saw the auto return type, so update
26924 it to have the correct level. */
26925 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26926 }
26927 return type;
26928 }
26929
26930 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26931 'decltype(auto)' or a deduced class template. */
26932
26933 bool
26934 is_auto (const_tree type)
26935 {
26936 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26937 && (TYPE_IDENTIFIER (type) == auto_identifier
26938 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26939 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26940 return true;
26941 else
26942 return false;
26943 }
26944
26945 /* for_each_template_parm callback for type_uses_auto. */
26946
26947 int
26948 is_auto_r (tree tp, void */*data*/)
26949 {
26950 return is_auto (tp);
26951 }
26952
26953 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26954 a use of `auto'. Returns NULL_TREE otherwise. */
26955
26956 tree
26957 type_uses_auto (tree type)
26958 {
26959 if (type == NULL_TREE)
26960 return NULL_TREE;
26961 else if (flag_concepts)
26962 {
26963 /* The Concepts TS allows multiple autos in one type-specifier; just
26964 return the first one we find, do_auto_deduction will collect all of
26965 them. */
26966 if (uses_template_parms (type))
26967 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26968 /*visited*/NULL, /*nondeduced*/false);
26969 else
26970 return NULL_TREE;
26971 }
26972 else
26973 return find_type_usage (type, is_auto);
26974 }
26975
26976 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
26977 concepts are enabled, auto is acceptable in template arguments, but
26978 only when TEMPL identifies a template class. Return TRUE if any
26979 such errors were reported. */
26980
26981 bool
26982 check_auto_in_tmpl_args (tree tmpl, tree args)
26983 {
26984 /* If there were previous errors, nevermind. */
26985 if (!args || TREE_CODE (args) != TREE_VEC)
26986 return false;
26987
26988 /* If TMPL is an identifier, we're parsing and we can't tell yet
26989 whether TMPL is supposed to be a type, a function or a variable.
26990 We'll only be able to tell during template substitution, so we
26991 expect to be called again then. If concepts are enabled and we
26992 know we have a type, we're ok. */
26993 if (flag_concepts
26994 && (identifier_p (tmpl)
26995 || (DECL_P (tmpl)
26996 && (DECL_TYPE_TEMPLATE_P (tmpl)
26997 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
26998 return false;
26999
27000 /* Quickly search for any occurrences of auto; usually there won't
27001 be any, and then we'll avoid allocating the vector. */
27002 if (!type_uses_auto (args))
27003 return false;
27004
27005 bool errors = false;
27006
27007 tree vec = extract_autos (args);
27008 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27009 {
27010 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27011 error_at (DECL_SOURCE_LOCATION (xauto),
27012 "invalid use of %qT in template argument", xauto);
27013 errors = true;
27014 }
27015
27016 return errors;
27017 }
27018
27019 /* For a given template T, return the vector of typedefs referenced
27020 in T for which access check is needed at T instantiation time.
27021 T is either a FUNCTION_DECL or a RECORD_TYPE.
27022 Those typedefs were added to T by the function
27023 append_type_to_template_for_access_check. */
27024
27025 vec<qualified_typedef_usage_t, va_gc> *
27026 get_types_needing_access_check (tree t)
27027 {
27028 tree ti;
27029 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27030
27031 if (!t || t == error_mark_node)
27032 return NULL;
27033
27034 if (!(ti = get_template_info (t)))
27035 return NULL;
27036
27037 if (CLASS_TYPE_P (t)
27038 || TREE_CODE (t) == FUNCTION_DECL)
27039 {
27040 if (!TI_TEMPLATE (ti))
27041 return NULL;
27042
27043 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27044 }
27045
27046 return result;
27047 }
27048
27049 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27050 tied to T. That list of typedefs will be access checked at
27051 T instantiation time.
27052 T is either a FUNCTION_DECL or a RECORD_TYPE.
27053 TYPE_DECL is a TYPE_DECL node representing a typedef.
27054 SCOPE is the scope through which TYPE_DECL is accessed.
27055 LOCATION is the location of the usage point of TYPE_DECL.
27056
27057 This function is a subroutine of
27058 append_type_to_template_for_access_check. */
27059
27060 static void
27061 append_type_to_template_for_access_check_1 (tree t,
27062 tree type_decl,
27063 tree scope,
27064 location_t location)
27065 {
27066 qualified_typedef_usage_t typedef_usage;
27067 tree ti;
27068
27069 if (!t || t == error_mark_node)
27070 return;
27071
27072 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27073 || CLASS_TYPE_P (t))
27074 && type_decl
27075 && TREE_CODE (type_decl) == TYPE_DECL
27076 && scope);
27077
27078 if (!(ti = get_template_info (t)))
27079 return;
27080
27081 gcc_assert (TI_TEMPLATE (ti));
27082
27083 typedef_usage.typedef_decl = type_decl;
27084 typedef_usage.context = scope;
27085 typedef_usage.locus = location;
27086
27087 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27088 }
27089
27090 /* Append TYPE_DECL to the template TEMPL.
27091 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27092 At TEMPL instanciation time, TYPE_DECL will be checked to see
27093 if it can be accessed through SCOPE.
27094 LOCATION is the location of the usage point of TYPE_DECL.
27095
27096 e.g. consider the following code snippet:
27097
27098 class C
27099 {
27100 typedef int myint;
27101 };
27102
27103 template<class U> struct S
27104 {
27105 C::myint mi; // <-- usage point of the typedef C::myint
27106 };
27107
27108 S<char> s;
27109
27110 At S<char> instantiation time, we need to check the access of C::myint
27111 In other words, we need to check the access of the myint typedef through
27112 the C scope. For that purpose, this function will add the myint typedef
27113 and the scope C through which its being accessed to a list of typedefs
27114 tied to the template S. That list will be walked at template instantiation
27115 time and access check performed on each typedefs it contains.
27116 Note that this particular code snippet should yield an error because
27117 myint is private to C. */
27118
27119 void
27120 append_type_to_template_for_access_check (tree templ,
27121 tree type_decl,
27122 tree scope,
27123 location_t location)
27124 {
27125 qualified_typedef_usage_t *iter;
27126 unsigned i;
27127
27128 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27129
27130 /* Make sure we don't append the type to the template twice. */
27131 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27132 if (iter->typedef_decl == type_decl && scope == iter->context)
27133 return;
27134
27135 append_type_to_template_for_access_check_1 (templ, type_decl,
27136 scope, location);
27137 }
27138
27139 /* Convert the generic type parameters in PARM that match the types given in the
27140 range [START_IDX, END_IDX) from the current_template_parms into generic type
27141 packs. */
27142
27143 tree
27144 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27145 {
27146 tree current = current_template_parms;
27147 int depth = TMPL_PARMS_DEPTH (current);
27148 current = INNERMOST_TEMPLATE_PARMS (current);
27149 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27150
27151 for (int i = 0; i < start_idx; ++i)
27152 TREE_VEC_ELT (replacement, i)
27153 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27154
27155 for (int i = start_idx; i < end_idx; ++i)
27156 {
27157 /* Create a distinct parameter pack type from the current parm and add it
27158 to the replacement args to tsubst below into the generic function
27159 parameter. */
27160
27161 tree o = TREE_TYPE (TREE_VALUE
27162 (TREE_VEC_ELT (current, i)));
27163 tree t = copy_type (o);
27164 TEMPLATE_TYPE_PARM_INDEX (t)
27165 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27166 o, 0, 0, tf_none);
27167 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27168 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27169 TYPE_MAIN_VARIANT (t) = t;
27170 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27171 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27172 TREE_VEC_ELT (replacement, i) = t;
27173 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27174 }
27175
27176 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27177 TREE_VEC_ELT (replacement, i)
27178 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27179
27180 /* If there are more levels then build up the replacement with the outer
27181 template parms. */
27182 if (depth > 1)
27183 replacement = add_to_template_args (template_parms_to_args
27184 (TREE_CHAIN (current_template_parms)),
27185 replacement);
27186
27187 return tsubst (parm, replacement, tf_none, NULL_TREE);
27188 }
27189
27190 /* Entries in the decl_constraint hash table. */
27191 struct GTY((for_user)) constr_entry
27192 {
27193 tree decl;
27194 tree ci;
27195 };
27196
27197 /* Hashing function and equality for constraint entries. */
27198 struct constr_hasher : ggc_ptr_hash<constr_entry>
27199 {
27200 static hashval_t hash (constr_entry *e)
27201 {
27202 return (hashval_t)DECL_UID (e->decl);
27203 }
27204
27205 static bool equal (constr_entry *e1, constr_entry *e2)
27206 {
27207 return e1->decl == e2->decl;
27208 }
27209 };
27210
27211 /* A mapping from declarations to constraint information. Note that
27212 both templates and their underlying declarations are mapped to the
27213 same constraint information.
27214
27215 FIXME: This is defined in pt.c because garbage collection
27216 code is not being generated for constraint.cc. */
27217
27218 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27219
27220 /* Returns the template constraints of declaration T. If T is not
27221 constrained, return NULL_TREE. Note that T must be non-null. */
27222
27223 tree
27224 get_constraints (tree t)
27225 {
27226 if (!flag_concepts)
27227 return NULL_TREE;
27228
27229 gcc_assert (DECL_P (t));
27230 if (TREE_CODE (t) == TEMPLATE_DECL)
27231 t = DECL_TEMPLATE_RESULT (t);
27232 constr_entry elt = { t, NULL_TREE };
27233 constr_entry* found = decl_constraints->find (&elt);
27234 if (found)
27235 return found->ci;
27236 else
27237 return NULL_TREE;
27238 }
27239
27240 /* Associate the given constraint information CI with the declaration
27241 T. If T is a template, then the constraints are associated with
27242 its underlying declaration. Don't build associations if CI is
27243 NULL_TREE. */
27244
27245 void
27246 set_constraints (tree t, tree ci)
27247 {
27248 if (!ci)
27249 return;
27250 gcc_assert (t && flag_concepts);
27251 if (TREE_CODE (t) == TEMPLATE_DECL)
27252 t = DECL_TEMPLATE_RESULT (t);
27253 gcc_assert (!get_constraints (t));
27254 constr_entry elt = {t, ci};
27255 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
27256 constr_entry* entry = ggc_alloc<constr_entry> ();
27257 *entry = elt;
27258 *slot = entry;
27259 }
27260
27261 /* Remove the associated constraints of the declaration T. */
27262
27263 void
27264 remove_constraints (tree t)
27265 {
27266 gcc_assert (DECL_P (t));
27267 if (TREE_CODE (t) == TEMPLATE_DECL)
27268 t = DECL_TEMPLATE_RESULT (t);
27269
27270 constr_entry elt = {t, NULL_TREE};
27271 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
27272 if (slot)
27273 decl_constraints->clear_slot (slot);
27274 }
27275
27276 /* Memoized satisfaction results for declarations. This
27277 maps the pair (constraint_info, arguments) to the result computed
27278 by constraints_satisfied_p. */
27279
27280 struct GTY((for_user)) constraint_sat_entry
27281 {
27282 tree ci;
27283 tree args;
27284 tree result;
27285 };
27286
27287 /* Hashing function and equality for constraint entries. */
27288
27289 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
27290 {
27291 static hashval_t hash (constraint_sat_entry *e)
27292 {
27293 hashval_t val = iterative_hash_object(e->ci, 0);
27294 return iterative_hash_template_arg (e->args, val);
27295 }
27296
27297 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
27298 {
27299 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
27300 }
27301 };
27302
27303 /* Memoized satisfaction results for concept checks. */
27304
27305 struct GTY((for_user)) concept_spec_entry
27306 {
27307 tree tmpl;
27308 tree args;
27309 tree result;
27310 };
27311
27312 /* Hashing function and equality for constraint entries. */
27313
27314 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
27315 {
27316 static hashval_t hash (concept_spec_entry *e)
27317 {
27318 return hash_tmpl_and_args (e->tmpl, e->args);
27319 }
27320
27321 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
27322 {
27323 ++comparing_specializations;
27324 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
27325 --comparing_specializations;
27326 return eq;
27327 }
27328 };
27329
27330 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
27331 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
27332
27333 /* Search for a memoized satisfaction result. Returns one of the
27334 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27335
27336 tree
27337 lookup_constraint_satisfaction (tree ci, tree args)
27338 {
27339 constraint_sat_entry elt = { ci, args, NULL_TREE };
27340 constraint_sat_entry* found = constraint_memos->find (&elt);
27341 if (found)
27342 return found->result;
27343 else
27344 return NULL_TREE;
27345 }
27346
27347 /* Memoize the result of a satisfication test. Returns the saved result. */
27348
27349 tree
27350 memoize_constraint_satisfaction (tree ci, tree args, tree result)
27351 {
27352 constraint_sat_entry elt = {ci, args, result};
27353 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
27354 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
27355 *entry = elt;
27356 *slot = entry;
27357 return result;
27358 }
27359
27360 /* Search for a memoized satisfaction result for a concept. */
27361
27362 tree
27363 lookup_concept_satisfaction (tree tmpl, tree args)
27364 {
27365 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27366 concept_spec_entry* found = concept_memos->find (&elt);
27367 if (found)
27368 return found->result;
27369 else
27370 return NULL_TREE;
27371 }
27372
27373 /* Memoize the result of a concept check. Returns the saved result. */
27374
27375 tree
27376 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
27377 {
27378 concept_spec_entry elt = {tmpl, args, result};
27379 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
27380 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27381 *entry = elt;
27382 *slot = entry;
27383 return result;
27384 }
27385
27386 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
27387
27388 /* Returns a prior concept specialization. This returns the substituted
27389 and normalized constraints defined by the concept. */
27390
27391 tree
27392 get_concept_expansion (tree tmpl, tree args)
27393 {
27394 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27395 concept_spec_entry* found = concept_expansions->find (&elt);
27396 if (found)
27397 return found->result;
27398 else
27399 return NULL_TREE;
27400 }
27401
27402 /* Save a concept expansion for later. */
27403
27404 tree
27405 save_concept_expansion (tree tmpl, tree args, tree def)
27406 {
27407 concept_spec_entry elt = {tmpl, args, def};
27408 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
27409 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27410 *entry = elt;
27411 *slot = entry;
27412 return def;
27413 }
27414
27415 static hashval_t
27416 hash_subsumption_args (tree t1, tree t2)
27417 {
27418 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
27419 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
27420 int val = 0;
27421 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
27422 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
27423 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
27424 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
27425 return val;
27426 }
27427
27428 /* Compare the constraints of two subsumption entries. The LEFT1 and
27429 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27430 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27431
27432 static bool
27433 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
27434 {
27435 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
27436 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
27437 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
27438 CHECK_CONSTR_ARGS (right1)))
27439 return comp_template_args (CHECK_CONSTR_ARGS (left2),
27440 CHECK_CONSTR_ARGS (right2));
27441 return false;
27442 }
27443
27444 /* Key/value pair for learning and memoizing subsumption results. This
27445 associates a pair of check constraints (including arguments) with
27446 a boolean value indicating the result. */
27447
27448 struct GTY((for_user)) subsumption_entry
27449 {
27450 tree t1;
27451 tree t2;
27452 bool result;
27453 };
27454
27455 /* Hashing function and equality for constraint entries. */
27456
27457 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
27458 {
27459 static hashval_t hash (subsumption_entry *e)
27460 {
27461 return hash_subsumption_args (e->t1, e->t2);
27462 }
27463
27464 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
27465 {
27466 ++comparing_specializations;
27467 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
27468 --comparing_specializations;
27469 return eq;
27470 }
27471 };
27472
27473 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
27474
27475 /* Search for a previously cached subsumption result. */
27476
27477 bool*
27478 lookup_subsumption_result (tree t1, tree t2)
27479 {
27480 subsumption_entry elt = { t1, t2, false };
27481 subsumption_entry* found = subsumption_table->find (&elt);
27482 if (found)
27483 return &found->result;
27484 else
27485 return 0;
27486 }
27487
27488 /* Save a subsumption result. */
27489
27490 bool
27491 save_subsumption_result (tree t1, tree t2, bool result)
27492 {
27493 subsumption_entry elt = {t1, t2, result};
27494 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
27495 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
27496 *entry = elt;
27497 *slot = entry;
27498 return result;
27499 }
27500
27501 /* Set up the hash table for constraint association. */
27502
27503 void
27504 init_constraint_processing (void)
27505 {
27506 if (!flag_concepts)
27507 return;
27508
27509 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
27510 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
27511 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
27512 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
27513 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
27514 }
27515
27516 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27517 0..N-1. */
27518
27519 void
27520 declare_integer_pack (void)
27521 {
27522 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
27523 build_function_type_list (integer_type_node,
27524 integer_type_node,
27525 NULL_TREE),
27526 NULL_TREE, ECF_CONST);
27527 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
27528 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
27529 }
27530
27531 /* Set up the hash tables for template instantiations. */
27532
27533 void
27534 init_template_processing (void)
27535 {
27536 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27537 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27538
27539 if (cxx_dialect >= cxx11)
27540 declare_integer_pack ();
27541 }
27542
27543 /* Print stats about the template hash tables for -fstats. */
27544
27545 void
27546 print_template_statistics (void)
27547 {
27548 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
27549 "%f collisions\n", (long) decl_specializations->size (),
27550 (long) decl_specializations->elements (),
27551 decl_specializations->collisions ());
27552 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
27553 "%f collisions\n", (long) type_specializations->size (),
27554 (long) type_specializations->elements (),
27555 type_specializations->collisions ());
27556 }
27557
27558 #if CHECKING_P
27559
27560 namespace selftest {
27561
27562 /* Verify that build_non_dependent_expr () works, for various expressions,
27563 and that location wrappers don't affect the results. */
27564
27565 static void
27566 test_build_non_dependent_expr ()
27567 {
27568 location_t loc = BUILTINS_LOCATION;
27569
27570 /* Verify constants, without and with location wrappers. */
27571 tree int_cst = build_int_cst (integer_type_node, 42);
27572 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
27573
27574 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
27575 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
27576 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
27577
27578 tree string_lit = build_string (4, "foo");
27579 TREE_TYPE (string_lit) = char_array_type_node;
27580 string_lit = fix_string_type (string_lit);
27581 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27582
27583 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27584 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27585 ASSERT_EQ (wrapped_string_lit,
27586 build_non_dependent_expr (wrapped_string_lit));
27587 }
27588
27589 /* Verify that type_dependent_expression_p () works correctly, even
27590 in the presence of location wrapper nodes. */
27591
27592 static void
27593 test_type_dependent_expression_p ()
27594 {
27595 location_t loc = BUILTINS_LOCATION;
27596
27597 tree name = get_identifier ("foo");
27598
27599 /* If no templates are involved, nothing is type-dependent. */
27600 gcc_assert (!processing_template_decl);
27601 ASSERT_FALSE (type_dependent_expression_p (name));
27602
27603 ++processing_template_decl;
27604
27605 /* Within a template, an unresolved name is always type-dependent. */
27606 ASSERT_TRUE (type_dependent_expression_p (name));
27607
27608 /* Ensure it copes with NULL_TREE and errors. */
27609 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27610 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27611
27612 /* A USING_DECL in a template should be type-dependent, even if wrapped
27613 with a location wrapper (PR c++/83799). */
27614 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27615 TREE_TYPE (using_decl) = integer_type_node;
27616 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27617 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27618 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27619 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27620
27621 --processing_template_decl;
27622 }
27623
27624 /* Run all of the selftests within this file. */
27625
27626 void
27627 cp_pt_c_tests ()
27628 {
27629 test_build_non_dependent_expr ();
27630 test_type_dependent_expression_p ();
27631 }
27632
27633 } // namespace selftest
27634
27635 #endif /* #if CHECKING_P */
27636
27637 #include "gt-cp-pt.h"